Skip to content

Commit

Permalink
eth: prefer disk-backed db over ephemeral
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Sep 24, 2021
1 parent be80588 commit fcd22b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 14 additions & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state
database state.Database
report = true
origin = block.NumberU64()
root = block.Root()
)
// Check the live database first if we have the state fully available, use that.
if checkLive {
Expand All @@ -49,8 +50,20 @@ func (eth *Ethereum) stateAtBlock(block *types.Block, reexec uint64, base *state
return statedb, nil
}
}
// Even if the base is provided, if the state that's being asked for
// already exists on disk, we can clean out the ephemeral database and
// start from scratch.
if ok, _ := eth.chainDb.Has(root[:]); ok {
log.Info("State found in diskdb, resetting ephem db", "block", origin, "root", root)
// Create a new ephemeral trie.Database for isolating the live one. Otherwise
// the internal junks created by tracing will be persisted into the disk.
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
if statedb, err = state.New(block.Root(), database, nil); err == nil {
return statedb, nil
}
}
if base != nil {
// The optional base statedb is given, mark the start point as parent block
// Use the optional base statedb.
statedb, database, report = base, base.Database(), false
current = eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)
} else {
Expand Down
9 changes: 7 additions & 2 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type Database struct {

preimages map[common.Hash][]byte // Preimages of nodes from the secure trie

lastLog time.Time // for peridocal logging

derefs uint64 // counter on Dereference operations (accessed atomically)
gctime time.Duration // Time spent on garbage collection since last commit
gcnodes uint64 // Nodes garbage collected since last commit
Expand Down Expand Up @@ -537,8 +539,11 @@ func (db *Database) Dereference(root common.Hash) {
memcacheGCSizeMeter.Mark(int64(storage - db.dirtiesSize))
memcacheGCNodesMeter.Mark(int64(nodes - len(db.dirties)))

log.Debug("Dereferenced trie from memory database", "nodes", nodes-len(db.dirties), "size", storage-db.dirtiesSize, "time", time.Since(start),
"gcnodes", db.gcnodes, "gcsize", db.gcsize, "gctime", db.gctime, "livenodes", len(db.dirties), "livesize", db.dirtiesSize)
if time.Since(db.lastLog) > time.Second * 8{
log.Info("Dereferenced trie from memory database", "nodes", nodes-len(db.dirties), "size", storage-db.dirtiesSize, "time", time.Since(start),
"gcnodes", db.gcnodes, "gcsize", db.gcsize, "gctime", db.gctime, "livenodes", len(db.dirties), "livesize", db.dirtiesSize)
db.lastLog = time.Now()
}
}

// dereference is the private locked version of Dereference.
Expand Down

0 comments on commit fcd22b0

Please sign in to comment.