From df52eacea846c17ae19fdc2e03902e677b8499bd Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 16 Aug 2022 11:56:59 +0200 Subject: [PATCH 1/3] core, trie: flush preimages to db on database close Co-authored-by: rjl493456442 --- core/blockchain.go | 4 ++++ trie/database.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index f35de404619b..5c0854d6745a 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -899,6 +899,10 @@ func (bc *BlockChain) Stop() { triedb := bc.stateCache.TrieDB() triedb.SaveCache(bc.cacheConfig.TrieCleanJournal) } + // Flush the preimages to disk + if err := bc.stateCache.TrieDB().Close(); err != nil { + log.Error("Error closing Trie database", "err", err) + } log.Info("Blockchain stopped") } diff --git a/trie/database.go b/trie/database.go index 81f0477aeb86..72652540bd2b 100644 --- a/trie/database.go +++ b/trie/database.go @@ -852,3 +852,15 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st } } } + +// Close will flush the dangling preimages to disk. It is meant to be called when closing +// the blockchain object, so that all preimages are persisted to the db. +func (db *Database) Close() error { + db.lock.Lock() + defer db.lock.Unlock() + + if db.preimages != nil { + return db.preimages.commit(true) + } + return nil +} From 7e420b70372b9c4bd8d8eef086ef0f7320f541bd Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 16 Aug 2022 14:54:29 +0200 Subject: [PATCH 2/3] rename Close to CommitPreimages for clarity --- core/blockchain.go | 2 +- trie/database.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 5c0854d6745a..e3ddfe429dcb 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -900,7 +900,7 @@ func (bc *BlockChain) Stop() { triedb.SaveCache(bc.cacheConfig.TrieCleanJournal) } // Flush the preimages to disk - if err := bc.stateCache.TrieDB().Close(); err != nil { + if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil { log.Error("Error closing Trie database", "err", err) } log.Info("Blockchain stopped") diff --git a/trie/database.go b/trie/database.go index 72652540bd2b..1be79b0ba40a 100644 --- a/trie/database.go +++ b/trie/database.go @@ -853,9 +853,9 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st } } -// Close will flush the dangling preimages to disk. It is meant to be called when closing +// CommitPreimages will flush the dangling preimages to disk. It is meant to be called when closing // the blockchain object, so that all preimages are persisted to the db. -func (db *Database) Close() error { +func (db *Database) CommitPreimages() error { db.lock.Lock() defer db.lock.Unlock() From 3832294529c1d163929fde53b0e6ccf15a0c0615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 17 Aug 2022 14:10:51 +0300 Subject: [PATCH 3/3] core, trie: nitpick fixes --- core/blockchain.go | 8 ++++---- trie/database.go | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index e3ddfe429dcb..ee95cfb6cb66 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -893,16 +893,16 @@ 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 != "" { triedb := bc.stateCache.TrieDB() triedb.SaveCache(bc.cacheConfig.TrieCleanJournal) } - // Flush the preimages to disk - if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil { - log.Error("Error closing Trie database", "err", err) - } log.Info("Blockchain stopped") } diff --git a/trie/database.go b/trie/database.go index 1be79b0ba40a..8c9f47176845 100644 --- a/trie/database.go +++ b/trie/database.go @@ -853,14 +853,15 @@ func (db *Database) SaveCachePeriodically(dir string, interval time.Duration, st } } -// CommitPreimages will flush the dangling preimages to disk. It is meant to be called when closing -// the blockchain object, so that all preimages are persisted to the db. +// 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 db.preimages.commit(true) + if db.preimages == nil { + return nil } - return nil + return db.preimages.commit(true) }