@@ -143,6 +143,7 @@ type testHelper struct {
143143 diskdb ethdb.Database
144144 triedb * trie.Database
145145 accTrie * trie.SecureTrie
146+ nodes * trie.MergedNodeSet
146147}
147148
148149func newHelper () * testHelper {
@@ -153,6 +154,7 @@ func newHelper() *testHelper {
153154 diskdb : diskdb ,
154155 triedb : triedb ,
155156 accTrie : accTrie ,
157+ nodes : trie .NewMergedNodeSet (),
156158 }
157159}
158160
@@ -184,17 +186,22 @@ func (t *testHelper) makeStorageTrie(stateRoot, owner common.Hash, keys []string
184186 for i , k := range keys {
185187 stTrie .Update ([]byte (k ), []byte (vals [i ]))
186188 }
187- var root common.Hash
188189 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 )
192195 }
193196 return root .Bytes ()
194197}
195198
196199func (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 )
198205 t .triedb .Commit (root , false , nil )
199206 return root
200207}
@@ -378,7 +385,7 @@ func TestGenerateCorruptAccountTrie(t *testing.T) {
378385 helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
379386 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4
380387
381- root , _ , _ := helper .accTrie . Commit (nil ) // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
388+ root := helper .Commit () // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
382389
383390 // Delete an account trie leaf and ensure the generator chokes
384391 helper .triedb .Commit (root , false , nil )
@@ -413,18 +420,8 @@ func TestGenerateMissingStorageTrie(t *testing.T) {
413420 helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
414421 stRoot = helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
415422 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 ()
428425
429426 // Delete a storage trie root and ensure the generator chokes
430427 helper .diskdb .Delete (stRoot )
@@ -458,18 +455,7 @@ func TestGenerateCorruptStorageTrie(t *testing.T) {
458455 stRoot = helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
459456 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
460457
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 ()
473459
474460 // Delete a storage trie leaf and ensure the generator chokes
475461 helper .diskdb .Delete (common .HexToHash ("0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371" ).Bytes ())
@@ -825,10 +811,12 @@ func populateDangling(disk ethdb.KeyValueStore) {
825811// This test will populate some dangling storages to see if they can be cleaned up.
826812func TestGenerateCompleteSnapshotWithDanglingStorage (t * testing.T ) {
827813 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 )
829814
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 )
830816 helper .addAccount ("acc-1" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
831817 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 )
832820 helper .addAccount ("acc-3" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
833821
834822 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) {
858846// This test will populate some dangling storages to see if they can be cleaned up.
859847func TestGenerateBrokenSnapshotWithDanglingStorage (t * testing.T ) {
860848 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 )
862849
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 )
863851 helper .addTrieAccount ("acc-1" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
864852 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 )
865855 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
866856
867857 populateDangling (helper .diskdb )
0 commit comments