@@ -14,6 +14,10 @@ import (
14
14
)
15
15
16
16
func scaleEncodeBytes (t * testing.T , b ... byte ) (encoded []byte ) {
17
+ return scaleEncodeByteSlice (t , b )
18
+ }
19
+
20
+ func scaleEncodeByteSlice (t * testing.T , b []byte ) (encoded []byte ) {
17
21
encoded , err := scale .Marshal (b )
18
22
require .NoError (t , err )
19
23
return encoded
@@ -98,66 +102,6 @@ func Test_Decode(t *testing.T) {
98
102
Dirty : true ,
99
103
},
100
104
},
101
- "branch with two inlined children" : {
102
- reader : bytes .NewReader (
103
- []byte {
104
- branchVariant .bits | 30 , // key length 30
105
- // Key data start
106
- 195 , 101 , 195 , 207 , 89 , 214 ,
107
- 113 , 235 , 114 , 218 , 14 , 122 ,
108
- 65 , 19 , 196 , 16 , 2 , 80 , 95 ,
109
- 14 , 123 , 144 , 18 , 9 , 107 ,
110
- 65 , 196 , 235 , 58 , 175 ,
111
- // Key data end
112
- 148 , 127 , 110 , 164 , 41 , 8 , 0 , 0 , 104 , 95 , 15 , 31 , 5 ,
113
- 21 , 244 , 98 , 205 , 207 , 132 , 224 , 241 , 214 , 4 , 93 , 252 ,
114
- 187 , 32 , 134 , 92 , 74 , 43 , 127 , 1 , 0 , 0 ,
115
- },
116
- ),
117
- n : & Node {
118
- Key : []byte {
119
- 12 , 3 , 6 , 5 , 12 , 3 ,
120
- 12 , 15 , 5 , 9 , 13 , 6 ,
121
- 7 , 1 , 14 , 11 , 7 , 2 ,
122
- 13 , 10 , 0 , 14 , 7 , 10 ,
123
- 4 , 1 , 1 , 3 , 12 , 4 ,
124
- },
125
- Descendants : 2 ,
126
- Children : []* Node {
127
- nil , nil , nil , nil ,
128
- {
129
- Key : []byte {
130
- 14 , 7 , 11 , 9 , 0 , 1 ,
131
- 2 , 0 , 9 , 6 , 11 , 4 ,
132
- 1 , 12 , 4 , 14 , 11 ,
133
- 3 , 10 , 10 , 15 , 9 ,
134
- 4 , 7 , 15 , 6 , 14 ,
135
- 10 , 4 , 2 , 9 ,
136
- },
137
- Value : []byte {0 , 0 },
138
- Dirty : true ,
139
- },
140
- nil , nil , nil , nil ,
141
- {
142
- Key : []byte {
143
- 15 , 1 , 15 , 0 , 5 , 1 ,
144
- 5 , 15 , 4 , 6 , 2 , 12 ,
145
- 13 , 12 , 15 , 8 , 4 ,
146
- 14 , 0 , 15 , 1 , 13 ,
147
- 6 , 0 , 4 , 5 , 13 ,
148
- 15 , 12 , 11 , 11 ,
149
- },
150
- Value : []byte {
151
- 134 , 92 , 74 , 43 ,
152
- 127 , 1 , 0 , 0 ,
153
- },
154
- Dirty : true ,
155
- },
156
- nil , nil , nil , nil , nil , nil ,
157
- },
158
- Dirty : true ,
159
- },
160
- },
161
105
}
162
106
163
107
for name , testCase := range testCases {
@@ -179,6 +123,13 @@ func Test_Decode(t *testing.T) {
179
123
func Test_decodeBranch (t * testing.T ) {
180
124
t .Parallel ()
181
125
126
+ const childHashLength = 32
127
+ childHash := make ([]byte , childHashLength )
128
+ for i := range childHash {
129
+ childHash [i ] = byte (i )
130
+ }
131
+ scaleEncodedChildHash := scaleEncodeByteSlice (t , childHash )
132
+
182
133
testCases := map [string ]struct {
183
134
reader io.Reader
184
135
variant byte
@@ -220,9 +171,9 @@ func Test_decodeBranch(t *testing.T) {
220
171
"success for branch variant" : {
221
172
reader : bytes .NewBuffer (
222
173
concatByteSlices ([][]byte {
223
- {9 }, // key data
224
- {0 , 4 }, // children bitmap
225
- scaleEncodeBytes ( t , 1 , 2 , 3 , 4 , 5 ), // child hash
174
+ {9 }, // key data
175
+ {0 , 4 }, // children bitmap
176
+ scaleEncodedChildHash ,
226
177
}),
227
178
),
228
179
variant : branchVariant .bits ,
@@ -233,7 +184,7 @@ func Test_decodeBranch(t *testing.T) {
233
184
nil , nil , nil , nil , nil ,
234
185
nil , nil , nil , nil , nil ,
235
186
{
236
- HashDigest : [] byte { 1 , 2 , 3 , 4 , 5 } ,
187
+ HashDigest : childHash ,
237
188
Dirty : true ,
238
189
},
239
190
}),
@@ -255,14 +206,12 @@ func Test_decodeBranch(t *testing.T) {
255
206
errMessage : "cannot decode value: EOF" ,
256
207
},
257
208
"success for branch with value" : {
258
- reader : bytes .NewBuffer (
259
- concatByteSlices ([][]byte {
260
- {9 }, // key data
261
- {0 , 4 }, // children bitmap
262
- scaleEncodeBytes (t , 7 , 8 , 9 ), // branch value
263
- scaleEncodeBytes (t , 1 , 2 , 3 , 4 , 5 ), // child hash
264
- }),
265
- ),
209
+ reader : bytes .NewBuffer (concatByteSlices ([][]byte {
210
+ {9 }, // key data
211
+ {0 , 4 }, // children bitmap
212
+ scaleEncodeBytes (t , 7 , 8 , 9 ), // branch value
213
+ scaleEncodedChildHash ,
214
+ })),
266
215
variant : branchWithValueVariant .bits ,
267
216
partialKeyLength : 1 ,
268
217
branch : & Node {
@@ -272,14 +221,71 @@ func Test_decodeBranch(t *testing.T) {
272
221
nil , nil , nil , nil , nil ,
273
222
nil , nil , nil , nil , nil ,
274
223
{
275
- HashDigest : [] byte { 1 , 2 , 3 , 4 , 5 } ,
224
+ HashDigest : childHash ,
276
225
Dirty : true ,
277
226
},
278
227
}),
279
228
Dirty : true ,
280
229
Descendants : 1 ,
281
230
},
282
231
},
232
+ "branch with inlined node decoding error" : {
233
+ reader : bytes .NewBuffer (concatByteSlices ([][]byte {
234
+ {1 }, // key data
235
+ {0b0000_0001 , 0b0000_0000 }, // children bitmap
236
+ scaleEncodeBytes (t , 1 ), // branch value
237
+ {0 }, // garbage inlined node
238
+ })),
239
+ variant : branchWithValueVariant .bits ,
240
+ partialKeyLength : 1 ,
241
+ errWrapped : io .EOF ,
242
+ errMessage : "decoding inlined child at index 0: " +
243
+ "decoding header: reading header byte: EOF" ,
244
+ },
245
+ "branch with inlined branch and leaf" : {
246
+ reader : bytes .NewBuffer (concatByteSlices ([][]byte {
247
+ {1 }, // key data
248
+ {0b0000_0011 , 0b0000_0000 }, // children bitmap
249
+ // top level inlined leaf less than 32 bytes
250
+ scaleEncodeByteSlice (t , concatByteSlices ([][]byte {
251
+ {leafVariant .bits | 1 }, // partial key length of 1
252
+ {2 }, // key data
253
+ scaleEncodeBytes (t , 2 ), // value data
254
+ })),
255
+ // top level inlined branch less than 32 bytes
256
+ scaleEncodeByteSlice (t , concatByteSlices ([][]byte {
257
+ {branchWithValueVariant .bits | 1 }, // partial key length of 1
258
+ {3 }, // key data
259
+ {0b0000_0001 , 0b0000_0000 }, // children bitmap
260
+ scaleEncodeBytes (t , 3 ), // branch value
261
+ // bottom level leaf
262
+ scaleEncodeByteSlice (t , concatByteSlices ([][]byte {
263
+ {leafVariant .bits | 1 }, // partial key length of 1
264
+ {4 }, // key data
265
+ scaleEncodeBytes (t , 4 ), // value data
266
+ })),
267
+ })),
268
+ })),
269
+ variant : branchVariant .bits ,
270
+ partialKeyLength : 1 ,
271
+ branch : & Node {
272
+ Key : []byte {1 },
273
+ Descendants : 3 ,
274
+ Children : padRightChildren ([]* Node {
275
+ {Key : []byte {2 }, Value : []byte {2 }, Dirty : true },
276
+ {
277
+ Key : []byte {3 },
278
+ Value : []byte {3 },
279
+ Dirty : true ,
280
+ Descendants : 1 ,
281
+ Children : padRightChildren ([]* Node {
282
+ {Key : []byte {4 }, Value : []byte {4 }, Dirty : true },
283
+ }),
284
+ },
285
+ }),
286
+ Dirty : true ,
287
+ },
288
+ },
283
289
}
284
290
285
291
for name , testCase := range testCases {
0 commit comments