From 6e534a43e0cd998512481290b849f01c0a130432 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 7 May 2020 15:13:34 +0200 Subject: [PATCH] core/state: abort commit if read errors have occurred #21039 This finally adds the error check that the documentation of StateDB.dbErr promises to do. dbErr was added in 9e5f03b6c (June 2017), and the check was already missing in that commit. We somehow survived without it for three years. --- core/state/statedb.go | 5 +++++ core/state/statedb_test.go | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/core/state/statedb.go b/core/state/statedb.go index a06e016a2289..da252c7dd9a9 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -802,6 +802,11 @@ func (s *StateDB) clearJournalAndRefund() { // Commit writes the state to the underlying in-memory trie database. func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { + // TODO(daniel): need test, ref: #489 + // Short circuit in case any database failure occurred earlier. + if s.dbErr != nil { + return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr) + } // Finalize any pending changes and merge everything into the tries s.IntermediateRoot(deleteEmptyObjects) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index dd47c7618035..4d894b8e1ebc 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -912,3 +912,49 @@ func TestCopyCopyCommitCopy(t *testing.T) { t.Fatalf("third copy committed storage slot mismatch: have %x, want %x", val, sval) } } + +// TestMissingTrieNodes tests that if the statedb fails to load parts of the trie, +// the Commit operation fails with an error +// If we are missing trie nodes, we should not continue writing to the trie +func TestMissingTrieNodes(t *testing.T) { + // Create an initial state with a few accounts + memDb := rawdb.NewMemoryDatabase() + db := NewDatabase(memDb) + var root common.Hash + state, _ := New(common.Hash{}, db) + addr := common.BytesToAddress([]byte("so")) + { + state.SetBalance(addr, big.NewInt(1)) + state.SetCode(addr, []byte{1, 2, 3}) + a2 := common.BytesToAddress([]byte("another")) + state.SetBalance(a2, big.NewInt(100)) + state.SetCode(a2, []byte{1, 2, 4}) + root, _ = state.Commit(false) + t.Logf("root: %x", root) + // force-flush + state.Database().TrieDB().Cap(0) + } + // Create a new state on the old root + state, _ = New(root, db) + // Now we clear out the memdb + it := memDb.NewIterator(nil, nil) + for it.Next() { + k := it.Key() + // Leave the root intact + if !bytes.Equal(k, root[:]) { + t.Logf("key: %x", k) + memDb.Delete(k) + } + } + balance := state.GetBalance(addr) + // The removed elem should lead to it returning zero balance + if exp, got := uint64(0), balance.Uint64(); got != exp { + t.Errorf("expected %d, got %d", exp, got) + } + // Modify the state + state.SetBalance(addr, big.NewInt(2)) + root, err := state.Commit(false) + if err == nil { + t.Fatalf("expected error, got root :%x", root) + } +}