Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
ConvertProof unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 13, 2023
1 parent dc32b58 commit 5160c3a
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 5 deletions.
11 changes: 11 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common

import (
"encoding/hex"
"strings"
)

// DecodeHex decodes hex encoded string to raw byte slice
func DecodeHex(input string) ([]byte, error) {
return hex.DecodeString(strings.TrimPrefix(input, "0x"))
}
3 changes: 2 additions & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

const (
HashLength = 32
HashLength = 32
ProofLength = 24
)

type Etherman struct {
Expand Down
37 changes: 37 additions & 0 deletions etherman/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package etherman

import (
"bytes"
"crypto/rand"
"encoding/hex"
)

func generateRandomHexString(length int) (string, error) {
numBytes := length / 2
randomBytes := make([]byte, numBytes)

if _, err := rand.Read(randomBytes); err != nil {
return "", err
}

return hex.EncodeToString(randomBytes), nil
}

func generateProof() (string, error) {
var buf bytes.Buffer
buf.WriteString("0x")

for i := 0; i < 2*ProofLength; i++ {
hash, err := generateRandomHexString(HashLength)
if err != nil {
return "", err
}

_, err = buf.WriteString(hash)
if err != nil {
return "", err
}
}

return buf.String(), nil
}
38 changes: 34 additions & 4 deletions etherman/proof.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
package etherman

import (
"bytes"
"fmt"
"strings"

"github.com/0xPolygon/cdk-validium-node/encoding"
)

type Proof [24][HashLength]byte
type Proof [ProofLength][HashLength]byte

func (p Proof) Equals(other Proof) bool {
for i := 0; i < len(p); i++ {
if !bytes.Equal(p[i][:], other[i][:]) {
return false
}
}
return true
}

func BytesToProof(data []byte) (Proof, error) {
// Check if the length of the input data is compatible with the Proof type
expectedLength := ProofLength * HashLength
if len(data) != expectedLength {
return Proof{}, fmt.Errorf("invalid byte slice length. Expected length: %d, Actual length: %d", expectedLength, len(data))
}

// Create a Proof type and copy the data into it
var proof Proof
for i := 0; i < ProofLength; i++ {
copy(proof[i][:], data[i*HashLength:(i+1)*HashLength])
}

return proof, nil
}

func ConvertProof(p string) (Proof, error) {
const expectedLength = 24*HashLength*2 + 2
const (
expectedLength = ProofLength*HashLength*2 + 2
rawHashLength = 2 * HashLength // 1 byte gets substituted with 2 hex digits (1 hex digit = nibble = 4 bits)
)

if len(p) != expectedLength {
return Proof{}, fmt.Errorf("invalid proof length. Expected length: %d, Actual length %d", expectedLength, len(p))
}
p = strings.TrimPrefix(p, "0x")
proof := Proof{}
for i := 0; i < 24; i++ {
data := p[i*64 : (i+1)*64]
for i := 0; i < ProofLength; i++ {
data := p[i*rawHashLength : (i+1)*rawHashLength]
p, err := encoding.DecodeBytes(&data)
if err != nil {
return Proof{}, fmt.Errorf("failed to decode proof, err: %w", err)
Expand Down
53 changes: 53 additions & 0 deletions etherman/proof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package etherman

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/0xPolygon/beethoven/common"
)

func TestConvertProof(t *testing.T) {
validInputProof, err := generateProof()
require.NoError(t, err)

validRawProof, err := common.DecodeHex(validInputProof)
require.NoError(t, err)

validProof, err := BytesToProof(validRawProof)
require.NoError(t, err)

tests := []struct {
name string
input string
expected Proof
expectedErr string
}{
{
name: "valid proof",
input: validInputProof,
expected: validProof,
expectedErr: "",
},
{
name: "invalid proof length",
input: "0x1234",
expectedErr: fmt.Sprintf("invalid proof length. Expected length: %d, Actual length %d", ProofLength*HashLength*2+2, 6),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ConvertProof(tt.input)

if tt.expectedErr != "" {
require.ErrorContains(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
require.True(t, result.Equals(tt.expected), "expected: %v, actual: %v", tt.expected, result)
}
})
}
}

0 comments on commit 5160c3a

Please sign in to comment.