-
Notifications
You must be signed in to change notification settings - Fork 67
/
encoding_test.go
192 lines (158 loc) · 5.24 KB
/
encoding_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package verkle
import (
"bytes"
"testing"
"github.com/crate-crypto/go-ipa/banderwagon"
)
func TestParseNodeEmptyPayload(t *testing.T) {
t.Parallel()
_, err := ParseNode([]byte{}, 0)
if err != errSerializedPayloadTooShort {
t.Fatalf("invalid error, got %v, expected %v", err, "unexpected EOF")
}
}
func TestLeafStemLength(t *testing.T) {
t.Parallel()
// Serialize a leaf with no values, but whose stem is 32 bytes. The
// serialization should trim the extra byte.
toolong := make([]byte, 32)
leaf, err := NewLeafNode(toolong, make([][]byte, NodeWidth))
if err != nil {
t.Fatal(err)
}
ser, err := leaf.Serialize()
if err != nil {
t.Fatal(err)
}
if len(ser) != nodeTypeSize+StemSize+bitlistSize+3*banderwagon.UncompressedSize {
t.Fatalf("invalid serialization when the stem is longer than 31 bytes: %x (%d bytes != %d)", ser, len(ser), nodeTypeSize+StemSize+bitlistSize+2*banderwagon.UncompressedSize)
}
}
func TestInvalidNodeEncoding(t *testing.T) {
t.Parallel()
// Test a short payload.
if _, err := ParseNode([]byte{leafType}, 0); err != errSerializedPayloadTooShort {
t.Fatalf("invalid error, got %v, expected %v", err, errSerializedPayloadTooShort)
}
// Test an invalid node type.
values := make([][]byte, NodeWidth)
values[42] = testValue
ln, err := NewLeafNode(ffx32KeyTest, values)
if err != nil {
t.Fatal(err)
}
lnbytes, err := ln.Serialize()
if err != nil {
t.Fatalf("serializing leaf node: %v", err)
}
lnbytes[0] = 0xc0 // Change the type of the node to something invalid.
if _, err := ParseNode(lnbytes, 0); err != ErrInvalidNodeEncoding {
t.Fatalf("invalid error, got %v, expected %v", err, ErrInvalidNodeEncoding)
}
}
func TestParseNodeEoA(t *testing.T) {
values := make([][]byte, 256)
values[0] = zero32[:]
values[1] = EmptyCodeHash[:] // set empty code hash as balance, because why not
values[2] = fourtyKeyTest[:] // set nonce to 64
values[3] = EmptyCodeHash[:] // set empty code hash
values[4] = zero32[:] // zero-size
ln, err := NewLeafNode(ffx32KeyTest[:31], values)
if err != nil {
t.Fatalf("error creating leaf node: %v", err)
}
serialized, err := ln.Serialize()
if err != nil {
t.Fatalf("error serializing leaf node: %v", err)
}
if serialized[0] != eoAccountType {
t.Fatalf("invalid encoding type, got %d, expected %d", serialized[0], eoAccountType)
}
deserialized, err := ParseNode(serialized, 5)
if err != nil {
t.Fatalf("error deserializing leaf node: %v", err)
}
lnd, ok := deserialized.(*LeafNode)
if !ok {
t.Fatalf("expected leaf node, got %T", deserialized)
}
if lnd.depth != 5 {
t.Fatalf("invalid depth, got %d, expected %d", lnd.depth, 5)
}
if !bytes.Equal(lnd.stem, ffx32KeyTest[:31]) {
t.Fatalf("invalid stem, got %x, expected %x", lnd.stem, ffx32KeyTest[:31])
}
if !bytes.Equal(lnd.values[0], zero32[:]) {
t.Fatalf("invalid version, got %x, expected %x", lnd.values[0], zero32[:])
}
if !bytes.Equal(lnd.values[1], EmptyCodeHash[:]) {
t.Fatalf("invalid balance, got %x, expected %x", lnd.values[1], EmptyCodeHash[:])
}
if !bytes.Equal(lnd.values[2], fourtyKeyTest[:]) {
t.Fatalf("invalid nonce, got %x, expected %x", lnd.values[2], fourtyKeyTest[:])
}
if !bytes.Equal(lnd.values[3], EmptyCodeHash[:]) {
t.Fatalf("invalid code hash, got %x, expected %x", lnd.values[3], EmptyCodeHash[:])
}
if !bytes.Equal(lnd.values[4], zero32[:]) {
t.Fatalf("invalid code size, got %x, expected %x", lnd.values[4], zero32[:])
}
if !lnd.c2.Equal(&banderwagon.Identity) {
t.Fatalf("invalid c2, got %x, expected %x", lnd.c2, banderwagon.Identity)
}
if !lnd.c1.Equal(ln.c1) {
t.Fatalf("invalid c1, got %x, expected %x", lnd.c1, ln.c1)
}
if !lnd.commitment.Equal(ln.commitment) {
t.Fatalf("invalid commitment, got %x, expected %x", lnd.commitment, ln.commitment)
}
}
func TestParseNodeSingleSlot(t *testing.T) {
values := make([][]byte, 256)
values[153] = EmptyCodeHash
ln, err := NewLeafNode(ffx32KeyTest[:31], values)
if err != nil {
t.Fatalf("error creating leaf node: %v", err)
}
serialized, err := ln.Serialize()
if err != nil {
t.Fatalf("error serializing leaf node: %v", err)
}
if serialized[0] != singleSlotType {
t.Fatalf("invalid encoding type, got %d, expected %d", serialized[0], singleSlotType)
}
deserialized, err := ParseNode(serialized, 5)
if err != nil {
t.Fatalf("error deserializing leaf node: %v", err)
}
lnd, ok := deserialized.(*LeafNode)
if !ok {
t.Fatalf("expected leaf node, got %T", deserialized)
}
if lnd.depth != 5 {
t.Fatalf("invalid depth, got %d, expected %d", lnd.depth, 5)
}
if !bytes.Equal(lnd.stem, ffx32KeyTest[:31]) {
t.Fatalf("invalid stem, got %x, expected %x", lnd.stem, ffx32KeyTest[:31])
}
for i := range values {
if i != 153 {
if lnd.values[i] != nil {
t.Fatalf("value %d, got %x, expected empty slot", i, lnd.values[i])
}
} else {
if !bytes.Equal(lnd.values[i], EmptyCodeHash[:]) {
t.Fatalf("got %x, expected empty slot", lnd.values[i])
}
}
}
if !lnd.c2.Equal(&banderwagon.Identity) {
t.Fatalf("invalid c2, got %x, expected %x", lnd.c2, banderwagon.Identity)
}
if !lnd.c1.Equal(ln.c1) {
t.Fatalf("invalid c1, got %x, expected %x", lnd.c1, ln.c1)
}
if !lnd.commitment.Equal(ln.commitment) {
t.Fatalf("invalid commitment, got %x, expected %x", lnd.commitment, ln.commitment)
}
}