@@ -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}
@@ -384,7 +391,7 @@ func TestGenerateCorruptAccountTrie(t *testing.T) {
384391 helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
385392 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x19ead688e907b0fab07176120dceec244a72aff2f0aa51e8b827584e378772f4
386393
387- root , _ , _ := helper .accTrie . Commit (nil ) // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
394+ root := helper .Commit () // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978
388395
389396 // Delete an account trie leaf and ensure the generator chokes
390397 helper .triedb .Commit (root , false , nil )
@@ -423,18 +430,8 @@ func TestGenerateMissingStorageTrie(t *testing.T) {
423430 helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()}) // 0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7
424431 stRoot = helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
425432 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
426- root , _ , _ := helper .accTrie .Commit (nil )
427-
428- // We can only corrupt the disk database, so flush the tries out
429- helper .triedb .Reference (
430- common .BytesToHash (stRoot ),
431- common .HexToHash ("0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e" ),
432- )
433- helper .triedb .Reference (
434- common .BytesToHash (stRoot ),
435- common .HexToHash ("0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2" ),
436- )
437- helper .triedb .Commit (root , false , nil )
433+
434+ root := helper .Commit ()
438435
439436 // Delete a storage trie root and ensure the generator chokes
440437 helper .diskdb .Delete (stRoot )
@@ -472,18 +469,7 @@ func TestGenerateCorruptStorageTrie(t *testing.T) {
472469 stRoot = helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
473470 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()}) // 0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2
474471
475- root , _ , _ := helper .accTrie .Commit (nil )
476-
477- // We can only corrupt the disk database, so flush the tries out
478- helper .triedb .Reference (
479- common .BytesToHash (stRoot ),
480- common .HexToHash ("0x9250573b9c18c664139f3b6a7a8081b7d8f8916a8fcc5d94feec6c29f5fd4e9e" ),
481- )
482- helper .triedb .Reference (
483- common .BytesToHash (stRoot ),
484- common .HexToHash ("0x50815097425d000edfc8b3a4a13e175fc2bdcfee8bdfbf2d1ff61041d3c235b2" ),
485- )
486- helper .triedb .Commit (root , false , nil )
472+ root := helper .Commit ()
487473
488474 // Delete a storage trie leaf and ensure the generator chokes
489475 helper .diskdb .Delete (common .HexToHash ("0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371" ).Bytes ())
@@ -839,10 +825,12 @@ func populateDangling(disk ethdb.KeyValueStore) {
839825// This test will populate some dangling storages to see if they can be cleaned up.
840826func TestGenerateCompleteSnapshotWithDanglingStorage (t * testing.T ) {
841827 var helper = newHelper ()
842- stRoot := helper .makeStorageTrie (common.Hash {}, common.Hash {}, []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
843828
829+ stRoot := helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-1" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
844830 helper .addAccount ("acc-1" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
845831 helper .addAccount ("acc-2" , & Account {Balance : big .NewInt (1 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()})
832+
833+ helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
846834 helper .addAccount ("acc-3" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
847835
848836 helper .addSnapStorage ("acc-1" , []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" })
@@ -872,10 +860,12 @@ func TestGenerateCompleteSnapshotWithDanglingStorage(t *testing.T) {
872860// This test will populate some dangling storages to see if they can be cleaned up.
873861func TestGenerateBrokenSnapshotWithDanglingStorage (t * testing.T ) {
874862 var helper = newHelper ()
875- stRoot := helper .makeStorageTrie (common.Hash {}, common.Hash {}, []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
876863
864+ stRoot := helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-1" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
877865 helper .addTrieAccount ("acc-1" , & Account {Balance : big .NewInt (1 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
878866 helper .addTrieAccount ("acc-2" , & Account {Balance : big .NewInt (2 ), Root : emptyRoot .Bytes (), CodeHash : emptyCode .Bytes ()})
867+
868+ helper .makeStorageTrie (common.Hash {}, hashData ([]byte ("acc-3" )), []string {"key-1" , "key-2" , "key-3" }, []string {"val-1" , "val-2" , "val-3" }, true )
879869 helper .addTrieAccount ("acc-3" , & Account {Balance : big .NewInt (3 ), Root : stRoot , CodeHash : emptyCode .Bytes ()})
880870
881871 populateDangling (helper .diskdb )
0 commit comments