diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 0904be805e..cc2d8175bf 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -348,11 +348,7 @@ func (bc *BlockChain) HasState(hash common.Hash) bool { return true } } - block := bc.GetBlockByHash(hash) - if block == nil { - return false - } - return bc.stateCache.HasState(block.NumberU64(), hash) + return bc.stateCache.HasState(hash) } // HasBlockAndState checks if a block and associated state trie is fully present @@ -400,7 +396,19 @@ 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) { // new state db with no need commit mode - stateDb, err := state.New(root, state.NewDatabaseWithNodeDB(bc.db, bc.triedb, false), bc.snaps) + var ( + blockNumber int64 + err error + ) + if bc.triedb.Scheme() == rawdb.VersionScheme { + blockNumber, err = bc.triedb.VersaDB().GetVersionByRootHash(root) + if err != nil { + return nil, err + } + } + stateCache := state.NewDatabaseWithNodeDB(bc.db, bc.triedb, false) + stateCache.SetVersion(blockNumber + 1) + stateDb, err := state.New(root, stateCache, bc.snaps) if err != nil { return nil, err } diff --git a/core/state/caching_versa_db.go b/core/state/caching_versa_db.go index 27c5d75517..e5683fbcdb 100644 --- a/core/state/caching_versa_db.go +++ b/core/state/caching_versa_db.go @@ -249,8 +249,8 @@ func (cv *cachingVersaDB) Reset() { cv.root = common.Hash{} } -func (cv *cachingVersaDB) HasState(version uint64, root common.Hash) bool { - return cv.versionDB.HasState(int64(version), root) +func (cv *cachingVersaDB) HasState(root common.Hash) bool { + return cv.versionDB.HasState(root) } func (cv *cachingVersaDB) HasTreeExpired(tr Trie) bool { diff --git a/core/state/database.go b/core/state/database.go index 045c1c8ce4..2f39a7d94e 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -89,7 +89,7 @@ type Database interface { // HasTreeExpired used for caching versa db, whether the state where the opened tree resides has been closed HasTreeExpired(tr Trie) bool - HasState(version uint64, root common.Hash) bool + HasState(root common.Hash) bool // NoTries returns whether the database has tries storage. NoTries() bool @@ -365,7 +365,7 @@ func (db *cachingDB) TrieDB() *triedb.Database { return db.triedb } -func (db *cachingDB) HasState(_ uint64, root common.Hash) bool { +func (db *cachingDB) HasState(root common.Hash) bool { _, err := db.OpenTrie(root) return err == nil }