From 82f9ed49fa6a6831c997582821dd1c5ba4f14116 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 8 May 2020 20:52:20 +0200 Subject: [PATCH] core/state: avoid statedb.dbErr due to emptyCode (#21051) * core/state: more verbose stateb errors * core/state: fix flaw * core/state: fixed lint Co-authored-by: Marius van der Wijden --- core/state/statedb.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 6e9796e704..8b7c7ee22e 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -18,6 +18,7 @@ package state import ( + "bytes" "errors" "fmt" "math/big" @@ -294,9 +295,12 @@ func (s *StateDB) GetCodeSize(addr common.Address) int { if stateObject.code != nil { return len(stateObject.code) } + if bytes.Equal(stateObject.CodeHash(), emptyCode[:]) { + return 0 + } size, err := s.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash())) if err != nil { - s.setError(err) + s.setError(fmt.Errorf("GetCodeSize (%x) error: %v", addr[:], err)) } return size } @@ -465,7 +469,9 @@ func (s *StateDB) updateStateObject(obj *stateObject) { if err != nil { panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) } - s.setError(s.trie.TryUpdate(addr[:], data)) + if err = s.trie.TryUpdate(addr[:], data); err != nil { + s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) + } // If state snapshotting is active, cache the data til commit. Note, this // update mechanism is not symmetric to the deletion, because whereas it is @@ -484,7 +490,9 @@ func (s *StateDB) deleteStateObject(obj *stateObject) { } // Delete the account from the trie addr := obj.Address() - s.setError(s.trie.TryDelete(addr[:])) + if err := s.trie.TryDelete(addr[:]); err != nil { + s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err)) + } } // getStateObject retrieves a state object given by the address, returning nil if @@ -536,8 +544,11 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now()) } enc, err := s.trie.TryGet(addr[:]) + if err != nil { + s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr[:], err)) + return nil + } if len(enc) == 0 { - s.setError(err) return nil } if err := rlp.DecodeBytes(enc, &data); err != nil {