@@ -143,6 +143,7 @@ type testHelper struct {
143
143
diskdb ethdb.Database
144
144
triedb * trie.Database
145
145
accTrie * trie.SecureTrie
146
+ nodes * trie.MergedNodeSet
146
147
}
147
148
148
149
func newHelper () * testHelper {
@@ -153,6 +154,7 @@ func newHelper() *testHelper {
153
154
diskdb : diskdb ,
154
155
triedb : triedb ,
155
156
accTrie : accTrie ,
157
+ nodes : trie .NewMergedNodeSet (),
156
158
}
157
159
}
158
160
@@ -184,17 +186,22 @@ func (t *testHelper) makeStorageTrie(stateRoot, owner common.Hash, keys []string
184
186
for i , k := range keys {
185
187
stTrie .Update ([]byte (k ), []byte (vals [i ]))
186
188
}
187
- var root common.Hash
188
189
if ! commit {
189
- root = stTrie .Hash ()
190
- } else {
191
- root , _ , _ = stTrie .Commit (nil )
190
+ return stTrie .Hash ().Bytes ()
191
+ }
192
+ root , nodes , _ := stTrie .Commit (false )
193
+ if nodes != nil {
194
+ t .nodes .Merge (nodes )
192
195
}
193
196
return root .Bytes ()
194
197
}
195
198
196
199
func (t * testHelper ) Commit () common.Hash {
197
- root , _ , _ := t .accTrie .Commit (nil )
200
+ root , nodes , _ := t .accTrie .Commit (true )
201
+ if nodes != nil {
202
+ t .nodes .Merge (nodes )
203
+ }
204
+ t .triedb .Update (t .nodes )
198
205
t .triedb .Commit (root , false , nil )
199
206
return root
200
207
}
@@ -378,7 +385,7 @@ func TestGenerateCorruptAccountTrie(t *testing.T) {
378
385
helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
379
386
helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4
380
387
381
- root , _ , _ := helper .accTrie . Commit (nil ) // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
388
+ root := helper .Commit () // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
382
389
383
390
// Delete an account trie leaf and ensure the generator chokes
384
391
helper .triedb .Commit (root , false , nil )
@@ -413,18 +420,8 @@ func TestGenerateMissingStorageTrie(t *testing.T) {
413
420
helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
414
421
stRoot = helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
415
422
helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
416
- root , _ , _ := helper .accTrie .Commit (nil )
417
-
418
- // We can only corrupt the disk database, so flush the tries out
419
- helper .triedb .Reference (
420
- common .BytesToHash (stRoot ),
421
- common .HexToHash ("0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e" ),
422
- )
423
- helper .triedb .Reference (
424
- common .BytesToHash (stRoot ),
425
- common .HexToHash ("0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2" ),
426
- )
427
- helper .triedb .Commit (root , false , nil )
423
+
424
+ root := helper .Commit ()
428
425
429
426
// Delete a storage trie root and ensure the generator chokes
430
427
helper .diskdb .Delete (stRoot )
@@ -458,18 +455,7 @@ func TestGenerateCorruptStorageTrie(t *testing.T) {
458
455
stRoot = helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
459
456
helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
460
457
461
- root , _ , _ := helper .accTrie .Commit (nil )
462
-
463
- // We can only corrupt the disk database, so flush the tries out
464
- helper .triedb .Reference (
465
- common .BytesToHash (stRoot ),
466
- common .HexToHash ("0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e" ),
467
- )
468
- helper .triedb .Reference (
469
- common .BytesToHash (stRoot ),
470
- common .HexToHash ("0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2" ),
471
- )
472
- helper .triedb .Commit (root , false , nil )
458
+ root := helper .Commit ()
473
459
474
460
// Delete a storage trie leaf and ensure the generator chokes
475
461
helper .diskdb .Delete (common .HexToHash ("0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371" ).Bytes ())
@@ -825,10 +811,12 @@ func populateDangling(disk ethdb.KeyValueStore) {
825
811
// This test will populate some dangling storages to see if they can be cleaned up.
826
812
func TestGenerateCompleteSnapshotWithDanglingStorage (t * testing.T ) {
827
813
var helper = newHelper ()
828
- stRoot := helper .makeStorageTrie (common.Hash {}, common.Hash {}, []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
829
814
815
+ stRoot := helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-1" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
830
816
helper .addAccount ("acc-1" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
831
817
helper .addAccount ("acc-2" , & Account {Balance : big .NewInt (1 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()})
818
+
819
+ helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
832
820
helper .addAccount ("acc-3" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
833
821
834
822
helper .addSnapStorage ("acc-1" , []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" })
@@ -858,10 +846,12 @@ func TestGenerateCompleteSnapshotWithDanglingStorage(t *testing.T) {
858
846
// This test will populate some dangling storages to see if they can be cleaned up.
859
847
func TestGenerateBrokenSnapshotWithDanglingStorage (t * testing.T ) {
860
848
var helper = newHelper ()
861
- stRoot := helper .makeStorageTrie (common.Hash {}, common.Hash {}, []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
862
849
850
+ stRoot := helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-1" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
863
851
helper .addTrieAccount ("acc-1" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
864
852
helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()})
853
+
854
+ helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
865
855
helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
866
856
867
857
populateDangling (helper .diskdb )
0 commit comments