diff --git a/core/blockchain.go b/core/blockchain.go index 0e2ea4fd91..b0c67291cd 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1161,6 +1161,10 @@ func (bc *BlockChain) Stop() { bc.chainmu.Close() bc.wg.Wait() bc.saveData() + // Flush the collected preimages to disk + if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil { + log.Error("Failed to commit trie preimages", "err", err) + } log.Info("Blockchain manager stopped") } diff --git a/trie/database.go b/trie/database.go index fa80101d7d..671a29c0ec 100644 --- a/trie/database.go +++ b/trie/database.go @@ -824,3 +824,16 @@ func (db *Database) Size() (common.StorageSize, common.StorageSize) { } return db.dirtiesSize + db.childrenSize + metadataSize - metarootRefs, preimageSize } + +// 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) +}