core, trie: flush preimages to db on database close #25533 (#1107)

This commit is contained in:
Daniel Liu 2025-09-09 17:32:22 +08:00 committed by GitHub
parent ad9003c41e
commit 4e234f231a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View file

@ -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")
}

View file

@ -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)
}