From 12185e40e06ec9d68a3780f56ec70a0a831b25bc Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 17 Aug 2022 13:12:10 +0200 Subject: [PATCH] core, trie: flush preimages to db on blockchain close (#25533) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * core, trie: flush preimages to db on database close Co-authored-by: rjl493456442 * rename Close to CommitPreimages for clarity * core, trie: nitpick fixes Co-authored-by: rjl493456442 Co-authored-by: Péter Szilágyi --- core/blockchain.go | 4 ++++ trie/database.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index f35de4046..ee95cfb6c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -893,6 +893,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 81f0477ae..8c9f47176 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) +}