diff --git a/core/blockchain.go b/core/blockchain.go index aa241df87debf..1972e65aec385 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -897,6 +897,10 @@ func (bc *BlockChain) Stop() { log.Error("Dangling trie nodes after full cleanup") } } + // Flush the collected preimages to disk + if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil { + log.Error("Failed to commit trie preimages", "err", err) + } // Ensure all live cached entries be saved into disk, so that we can skip // cache warmup when node restarts. if bc.cacheConfig.TrieCleanJournal != "" { diff --git a/trie/database.go b/trie/database.go index 81f0477aeb86e..8c9f471768451 100644 --- a/trie/database.go +++ b/trie/database.go @@ -852,3 +852,16 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st } } } + +// CommitPreimages flushes the dangling preimages to disk. It is meant to be +// called when closing the blockchain object, so that preimages are persisted +// to the database. +func (db *Database) CommitPreimages() error { + db.lock.Lock() + defer db.lock.Unlock() + + if db.preimages == nil { + return nil + } + return db.preimages.commit(true) +}