Skip to content

Commit

Permalink
Revert "core/state: abort commit if errors have occurred (#484)" (#489)
Browse files Browse the repository at this point in the history
This reverts commit 0114eca.
  • Loading branch information
wanwiset25 committed Mar 13, 2024
1 parent 106af4d commit 8edb76f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 54 deletions.
4 changes: 0 additions & 4 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,6 @@ 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()

// Commit objects to the trie.
Expand Down
51 changes: 1 addition & 50 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"math"
"math/big"
"math/rand"
Expand All @@ -28,8 +29,6 @@ import (
"testing"
"testing/quick"

"github.com/XinFinOrg/XDPoSChain/core/rawdb"

check "gopkg.in/check.v1"

"github.com/XinFinOrg/XDPoSChain/common"
Expand Down Expand Up @@ -428,51 +427,3 @@ func (s *StateSuite) TestTouchDelete(c *check.C) {
c.Fatal("expected no dirty state object")
}
}

// 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 := 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)
//state, _ = New(root, db, nil)
// 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)
}
}

0 comments on commit 8edb76f

Please sign in to comment.