@@ -914,3 +914,43 @@ func TestStateDBAccessList(t *testing.T) {
914914 t .Fatalf ("expected empty, got %d" , got )
915915 }
916916}
917+
918+ // Tests that account and storage tries are flushed in the correct order and that
919+ // no data loss occurs.
920+ func TestFlushOrderDataLoss (t * testing.T ) {
921+ // Create a state trie with many accounts and slots
922+ var (
923+ memdb = rawdb .NewMemoryDatabase ()
924+ statedb = NewDatabase (memdb )
925+ state , _ = New (common.Hash {}, statedb , nil )
926+ )
927+ for a := byte (0 ); a < 10 ; a ++ {
928+ state .CreateAccount (common.Address {a })
929+ for s := byte (0 ); s < 10 ; s ++ {
930+ state .SetState (common.Address {a }, common.Hash {a , s }, common.Hash {a , s })
931+ }
932+ }
933+ root , err := state .Commit (false )
934+ if err != nil {
935+ t .Fatalf ("failed to commit state trie: %v" , err )
936+ }
937+ statedb .TrieDB ().Reference (root , common.Hash {})
938+ if err := statedb .TrieDB ().Cap (1024 ); err != nil {
939+ t .Fatalf ("failed to cap trie dirty cache: %v" , err )
940+ }
941+ if err := statedb .TrieDB ().Commit (root , false , nil ); err != nil {
942+ t .Fatalf ("failed to commit state trie: %v" , err )
943+ }
944+ // Reopen the state trie from flushed disk and verify it
945+ state , err = New (root , NewDatabase (memdb ), nil )
946+ if err != nil {
947+ t .Fatalf ("failed to reopen state trie: %v" , err )
948+ }
949+ for a := byte (0 ); a < 10 ; a ++ {
950+ for s := byte (0 ); s < 10 ; s ++ {
951+ if have := state .GetState (common.Address {a }, common.Hash {a , s }); have != (common.Hash {a , s }) {
952+ t .Errorf ("account %d: slot %d: state mismatch: have %x, want %x" , a , s , have , common.Hash {a , s })
953+ }
954+ }
955+ }
956+ }
0 commit comments