Skip to content

Commit 1a70548

Browse files
committed
feat:: support has state and get version interface
1 parent 7076bb1 commit 1a70548

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

core/blockchain_reader.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,7 @@ func (bc *BlockChain) HasState(hash common.Hash) bool {
348348
return true
349349
}
350350
}
351-
block := bc.GetBlockByHash(hash)
352-
if block == nil {
353-
return false
354-
}
355-
return bc.stateCache.HasState(block.NumberU64(), hash)
351+
return bc.stateCache.HasState(hash)
356352
}
357353

358354
// HasBlockAndState checks if a block and associated state trie is fully present
@@ -400,7 +396,19 @@ func (bc *BlockChain) State() (*state.StateDB, error) {
400396
// StateAt returns a new mutable state based on a particular point in time.
401397
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
402398
// new state db with no need commit mode
403-
stateDb, err := state.New(root, state.NewDatabaseWithNodeDB(bc.db, bc.triedb, false), bc.snaps)
399+
var (
400+
blockNumber int64
401+
err error
402+
)
403+
if bc.triedb.Scheme() == rawdb.VersionScheme {
404+
blockNumber, err = bc.triedb.VersaDB().GetVersionByRootHash(root)
405+
if err != nil {
406+
return nil, err
407+
}
408+
}
409+
stateCache := state.NewDatabaseWithNodeDB(bc.db, bc.triedb, false)
410+
stateCache.SetVersion(blockNumber + 1)
411+
stateDb, err := state.New(root, stateCache, bc.snaps)
404412
if err != nil {
405413
return nil, err
406414
}

core/state/caching_versa_db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ func (cv *cachingVersaDB) Reset() {
249249
cv.root = common.Hash{}
250250
}
251251

252-
func (cv *cachingVersaDB) HasState(version uint64, root common.Hash) bool {
253-
return cv.versionDB.HasState(int64(version), root)
252+
func (cv *cachingVersaDB) HasState(root common.Hash) bool {
253+
return cv.versionDB.HasState(root)
254254
}
255255

256256
func (cv *cachingVersaDB) HasTreeExpired(tr Trie) bool {

core/state/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type Database interface {
8989
// HasTreeExpired used for caching versa db, whether the state where the opened tree resides has been closed
9090
HasTreeExpired(tr Trie) bool
9191

92-
HasState(version uint64, root common.Hash) bool
92+
HasState(root common.Hash) bool
9393

9494
// NoTries returns whether the database has tries storage.
9595
NoTries() bool
@@ -365,7 +365,7 @@ func (db *cachingDB) TrieDB() *triedb.Database {
365365
return db.triedb
366366
}
367367

368-
func (db *cachingDB) HasState(_ uint64, root common.Hash) bool {
368+
func (db *cachingDB) HasState(root common.Hash) bool {
369369
_, err := db.OpenTrie(root)
370370
return err == nil
371371
}

0 commit comments

Comments
 (0)