Skip to content

Commit 33b8a50

Browse files
authored
fix: EoA pattern recognition (#463)
1 parent 3425fbd commit 33b8a50

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

encoding_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package verkle
22

33
import (
44
"bytes"
5+
"encoding/binary"
6+
"math/big"
57
"testing"
68

79
"github.com/crate-crypto/go-ipa/banderwagon"
@@ -62,11 +64,10 @@ func TestInvalidNodeEncoding(t *testing.T) {
6264

6365
func TestParseNodeEoA(t *testing.T) {
6466
values := make([][]byte, 256)
65-
values[0] = zero32[:]
66-
values[1] = EmptyCodeHash[:] // set empty code hash as balance, because why not
67-
values[2] = fourtyKeyTest[:] // set nonce to 64
68-
values[3] = EmptyCodeHash[:] // set empty code hash
69-
values[4] = zero32[:] // zero-size
67+
values[0] = make([]byte, 32)
68+
binary.BigEndian.PutUint64(values[0][8:], 64) // nonce = 64
69+
copy(values[0][30:], big.NewInt(1337).Bytes()) // balance = 1337
70+
values[1] = EmptyCodeHash[:] // set empty code hash as balance, because why not
7071
ln, err := NewLeafNode(ffx32KeyTest[:31], values)
7172
if err != nil {
7273
t.Fatalf("error creating leaf node: %v", err)
@@ -77,10 +78,8 @@ func TestParseNodeEoA(t *testing.T) {
7778
t.Fatalf("error serializing leaf node: %v", err)
7879
}
7980

80-
// TODO uncomment when the EoA serialization issue is fixed
81-
// if serialized[0] != eoAccountType {
82-
if serialized[0] != leafType {
83-
t.Fatalf("invalid encoding type, got %d, expected %d", serialized[0], leafType)
81+
if serialized[0] != eoAccountType {
82+
t.Fatalf("invalid encoding type, got %d, expected %d", serialized[0], eoAccountType)
8483
}
8584

8685
deserialized, err := ParseNode(serialized, 5)
@@ -101,24 +100,24 @@ func TestParseNodeEoA(t *testing.T) {
101100
t.Fatalf("invalid stem, got %x, expected %x", lnd.stem, ffx32KeyTest[:31])
102101
}
103102

104-
if !bytes.Equal(lnd.values[0], zero32[:]) {
105-
t.Fatalf("invalid version, got %x, expected %x", lnd.values[0], zero32[:])
103+
if lnd.values[0][0] != 0 {
104+
t.Fatalf("invalid version, got %x, expected %x", lnd.values[0][0], 0)
106105
}
107106

108-
if !bytes.Equal(lnd.values[1], EmptyCodeHash[:]) {
109-
t.Fatalf("invalid balance, got %x, expected %x", lnd.values[1], EmptyCodeHash[:])
107+
if cs := binary.BigEndian.Uint32(lnd.values[0][4:]); cs != 0 {
108+
t.Fatalf("invalid code size, got %x, expected %x", cs, 0)
110109
}
111110

112-
if !bytes.Equal(lnd.values[2], fourtyKeyTest[:]) {
113-
t.Fatalf("invalid nonce, got %x, expected %x", lnd.values[2], fourtyKeyTest[:])
111+
if nonce := binary.BigEndian.Uint64(lnd.values[0][8:]); nonce != 64 {
112+
t.Fatalf("invalid nonce, got %x, expected %x", nonce, 64)
114113
}
115114

116-
if !bytes.Equal(lnd.values[3], EmptyCodeHash[:]) {
117-
t.Fatalf("invalid code hash, got %x, expected %x", lnd.values[3], EmptyCodeHash[:])
115+
if balance := new(big.Int).SetBytes(lnd.values[0][16:]); balance.Cmp(big.NewInt(1337)) != 0 {
116+
t.Fatalf("invalid balance, got %x, expected %x", balance, 1337)
118117
}
119118

120-
if !bytes.Equal(lnd.values[4], zero32[:]) {
121-
t.Fatalf("invalid code size, got %x, expected %x", lnd.values[4], zero32[:])
119+
if !bytes.Equal(lnd.values[1], EmptyCodeHash[:]) {
120+
t.Fatalf("invalid balance, got %x, expected %x", lnd.values[1], EmptyCodeHash[:])
122121
}
123122

124123
if !lnd.c2.Equal(&banderwagon.Identity) {

tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,8 +1861,8 @@ func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2B
18611861
if isEoA {
18621862
switch i {
18631863
case 0:
1864-
// Version should be 0
1865-
isEoA = v != nil
1864+
// Version + reserved fields + code size should be 0
1865+
isEoA = v != nil && bytes.Equal(v[0:8], zero32[0:8])
18661866
case 1:
18671867
// Code hash should be the empty code hash
18681868
isEoA = v != nil && bytes.Equal(v, EmptyCodeHash[:])

0 commit comments

Comments
 (0)