Skip to content

Commit

Permalink
feat:: support has state and get version interface
Browse files Browse the repository at this point in the history
  • Loading branch information
joeylichang committed Aug 21, 2024
1 parent 7076bb1 commit 1a70548
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions core/state/caching_versa_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1a70548

Please sign in to comment.