From 14cbdda39803003863e5a2345083243efbfb55a4 Mon Sep 17 00:00:00 2001 From: weiihann Date: Wed, 3 Apr 2024 10:58:08 +0800 Subject: [PATCH] core: stateDb has no trie and no snap return err --- core/blockchain_reader.go | 15 ++++++++++++++- core/state/statedb.go | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 2023bcf4d6..5d5d3e0aaa 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -396,7 +396,20 @@ func (bc *BlockChain) State() (*state.StateDB, error) { // StateAt returns a new mutable state based on a particular point in time. func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { - return state.New(root, bc.stateCache, bc.snaps) + stateDb, err := state.New(root, bc.stateCache, bc.snaps) + if err != nil { + return nil, err + } + + // If there's no trie and the specified snapshot is not available, getting + // any state will by default return nil. + // Instead of that, it will be more useful to return an error to indicate + // the state is not available. + if stateDb.NoTrie() && stateDb.GetSnap() == nil { + return nil, errors.New("state is not available") + } + + return stateDb, err } // Config retrieves the chain's fork configuration. diff --git a/core/state/statedb.go b/core/state/statedb.go index ae0c20c076..95406ffba8 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1896,6 +1896,10 @@ func (s *StateDB) convertAccountSet(set map[common.Address]*types.StateAccount) return ret } +func (s *StateDB) GetSnap() snapshot.Snapshot { + return s.snap +} + // copySet returns a deep-copied set. func copySet[k comparable](set map[k][]byte) map[k][]byte { copied := make(map[k][]byte, len(set))