@@ -11,11 +11,22 @@ import (
11
11
12
12
"github.com/ChainSafe/gossamer/internal/trie/node"
13
13
"github.com/ChainSafe/gossamer/lib/common"
14
- gomock "github.com/golang/mock/gomock"
15
14
"github.com/stretchr/testify/assert"
16
15
"github.com/stretchr/testify/require"
17
16
)
18
17
18
+ func Test_EmptyHash (t * testing.T ) {
19
+ t .Parallel ()
20
+
21
+ expected := common.Hash {
22
+ 0x3 , 0x17 , 0xa , 0x2e , 0x75 , 0x97 , 0xb7 , 0xb7 ,
23
+ 0xe3 , 0xd8 , 0x4c , 0x5 , 0x39 , 0x1d , 0x13 , 0x9a ,
24
+ 0x62 , 0xb1 , 0x57 , 0xe7 , 0x87 , 0x86 , 0xd8 , 0xc0 ,
25
+ 0x82 , 0xf2 , 0x9d , 0xcf , 0x4c , 0x11 , 0x13 , 0x14 ,
26
+ }
27
+ assert .Equal (t , expected , EmptyHash )
28
+ }
29
+
19
30
func Test_NewEmptyTrie (t * testing.T ) {
20
31
expectedTrie := & Trie {
21
32
childTries : make (map [common.Hash ]* Trie ),
@@ -290,100 +301,6 @@ func Test_Trie_RootNode(t *testing.T) {
290
301
assert .Equal (t , expectedRoot , root )
291
302
}
292
303
293
- //go:generate mockgen -destination=buffer_mock_test.go -package $GOPACKAGE github.com/ChainSafe/gossamer/internal/trie/node Buffer
294
-
295
- func Test_encodeRoot (t * testing.T ) {
296
- t .Parallel ()
297
-
298
- testCases := map [string ]struct {
299
- root * Node
300
- writeCalls []writeCall
301
- errWrapped error
302
- errMessage string
303
- expectedRoot * Node
304
- }{
305
- "nil root and no error" : {
306
- writeCalls : []writeCall {
307
- {written : []byte {0 }},
308
- },
309
- },
310
- "nil root and write error" : {
311
- writeCalls : []writeCall {
312
- {
313
- written : []byte {0 },
314
- err : errTest ,
315
- },
316
- },
317
- errWrapped : errTest ,
318
- errMessage : "cannot write nil root node to buffer: test error" ,
319
- },
320
- "root encoding error" : {
321
- root : & Node {
322
- Key : []byte {1 , 2 },
323
- SubValue : []byte {1 },
324
- },
325
- writeCalls : []writeCall {
326
- {
327
- written : []byte {66 },
328
- err : errTest ,
329
- },
330
- },
331
- errWrapped : errTest ,
332
- errMessage : "cannot encode header: test error" ,
333
- expectedRoot : & Node {
334
- Key : []byte {1 , 2 },
335
- SubValue : []byte {1 },
336
- },
337
- },
338
- "root encoding success" : {
339
- root : & Node {
340
- Key : []byte {1 , 2 },
341
- SubValue : []byte {1 },
342
- },
343
- writeCalls : []writeCall {
344
- {written : []byte {66 }},
345
- {written : []byte {18 }},
346
- {written : []byte {4 }},
347
- {written : []byte {1 }},
348
- },
349
- expectedRoot : & Node {
350
- Key : []byte {1 , 2 },
351
- SubValue : []byte {1 },
352
- },
353
- },
354
- }
355
-
356
- for name , testCase := range testCases {
357
- testCase := testCase
358
- t .Run (name , func (t * testing.T ) {
359
- t .Parallel ()
360
- ctrl := gomock .NewController (t )
361
-
362
- buffer := NewMockBuffer (ctrl )
363
-
364
- var previousCall * gomock.Call
365
- for _ , write := range testCase .writeCalls {
366
- call := buffer .EXPECT ().
367
- Write (write .written ).
368
- Return (write .n , write .err )
369
-
370
- if previousCall != nil {
371
- call .After (previousCall )
372
- }
373
- previousCall = call
374
- }
375
-
376
- err := encodeRoot (testCase .root , buffer )
377
-
378
- assert .ErrorIs (t , err , testCase .errWrapped )
379
- if testCase .errWrapped != nil {
380
- assert .EqualError (t , err , testCase .errMessage )
381
- }
382
- assert .Equal (t , testCase .expectedRoot , testCase .root )
383
- })
384
- }
385
- }
386
-
387
304
func Test_Trie_MustHash (t * testing.T ) {
388
305
t .Parallel ()
389
306
@@ -436,6 +353,12 @@ func Test_Trie_Hash(t *testing.T) {
436
353
root : & Node {
437
354
Key : []byte {1 , 2 , 3 },
438
355
SubValue : []byte {1 },
356
+ MerkleValue : []byte {
357
+ 0xa8 , 0x13 , 0x7c , 0xee , 0xb4 , 0xad , 0xea , 0xac ,
358
+ 0x9e , 0x5b , 0x37 , 0xe2 , 0x8e , 0x7d , 0x64 , 0x78 ,
359
+ 0xac , 0xba , 0xb0 , 0x6e , 0x90 , 0x76 , 0xe4 , 0x67 ,
360
+ 0xa1 , 0xd8 , 0xa2 , 0x29 , 0x4e , 0x4a , 0xd9 , 0xa3 ,
361
+ },
439
362
},
440
363
},
441
364
},
@@ -457,8 +380,14 @@ func Test_Trie_Hash(t *testing.T) {
457
380
0xf0 , 0xe , 0xd3 , 0x39 , 0x48 , 0x21 , 0xe3 , 0xdd },
458
381
expectedTrie : Trie {
459
382
root : & Node {
460
- Key : []byte {1 , 2 , 3 },
461
- SubValue : []byte ("branch" ),
383
+ Key : []byte {1 , 2 , 3 },
384
+ SubValue : []byte ("branch" ),
385
+ MerkleValue : []byte {
386
+ 0xaa , 0x7e , 0x57 , 0x48 , 0xb0 , 0x27 , 0x4d , 0x18 ,
387
+ 0xf5 , 0x1c , 0xfd , 0x36 , 0x4c , 0x4b , 0x56 , 0x4a ,
388
+ 0xf5 , 0x37 , 0x9d , 0xd7 , 0xcb , 0xf5 , 0x80 , 0x15 ,
389
+ 0xf0 , 0x0e , 0xd3 , 0x39 , 0x48 , 0x21 , 0xe3 , 0xdd ,
390
+ },
462
391
Descendants : 1 ,
463
392
Children : padRightChildren ([]* Node {
464
393
{
0 commit comments