8000 state: abort commit if errors have occurred by ehnuje · Pull Request #766 · klaytn/klaytn · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 19, 2024. It is now read-only.

state: abort commit if errors have occurred #766

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions blockchain/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,10 @@ func (s *StateDB) clearJournalAndRefund() {

// Commit writes the state to the underlying in-memory trie database.
func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) {
if s.dbErr != nil {
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
}

defer s.clearJournalAndRefund()

for addr := range s.journal.dirties {
Expand Down
46 changes: 46 additions & 0 deletions blockchain/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,49 @@ func TestZeroHashNode(t *testing.T) {
t.Fatalf("node should return nil value for zero hash")
}
}

// 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 := database.NewMemoryDBManager()
db := NewDatabase(memDb)
var root common.Hash
state, _ := New(common.Hash{}, db, nil)
addr := toAddr([]byte("so"))
{
state.SetBalance(addr, big.NewInt(1))
state.SetCode(addr, []byte{1, 2, 3})
a2 := toAddr([]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, nil)
// Now we clear out the memdb
it := memDb.GetMemDB().NewIterator(nil, nil)
for it.Next() {
k := it.Key()
// Leave the root intact
if !bytes.Equal(k, root[:]) {
t.Logf("key: %x", k)
memDb.GetMemDB().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)
}
}
0