Skip to content

Commit 535b154

Browse files
gballetrjl493456442karalabe
authored andcommitted
core, trie: flush preimages to db on blockchain close (ethereum#25533)
* core, trie: flush preimages to db on database close Co-authored-by: rjl493456442 <[email protected]> * rename Close to CommitPreimages for clarity * core, trie: nitpick fixes Co-authored-by: rjl493456442 <[email protected]> Co-authored-by: Péter Szilágyi <[email protected]>
1 parent 8781584 commit 535b154

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

core/blockchain.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,10 @@ func (bc *BlockChain) Stop() {
893893
log.Error("Dangling trie nodes after full cleanup")
894894
}
895895
}
896+
// Flush the collected preimages to disk
897+
if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil {
898+
log.Error("Failed to commit trie preimages", "err", err)
899+
}
896900
// Ensure all live cached entries be saved into disk, so that we can skip
897901
// cache warmup when node restarts.
898902
if bc.cacheConfig.TrieCleanJournal != "" {

trie/database.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,16 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st
852852
}
853853
}
854854
}
855+
856+
// CommitPreimages flushes the dangling preimages to disk. It is meant to be
857+
// called when closing the blockchain object, so that preimages are persisted
858+
// to the database.
859+
func (db *Database) CommitPreimages() error {
860+
db.lock.Lock()
861+
defer db.lock.Unlock()
862+
863+
if db.preimages == nil {
864+
return nil
865+
}
866+
return db.preimages.commit(true)
867+
}

0 commit comments

Comments
 (0)