From d109de1342425807725c56e7af78d9dff3563d79 Mon Sep 17 00:00:00 2001 From: welkin22 <136572398+welkin22@users.noreply.github.com> Date: Tue, 7 May 2024 14:27:59 +0800 Subject: [PATCH] fix: the TrieCommitInterval not taking effect on restart (#106) --- core/blockchain.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index a2453536d3..6e9a32059d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1046,7 +1046,15 @@ func (bc *BlockChain) Stop() { if !bc.cacheConfig.TrieDirtyDisabled { triedb := bc.triedb - for _, offset := range []uint64{0, 1, TriesInMemory - 1} { + blockOffsets := []uint64{0, 1, TriesInMemory - 1} + if bc.cacheConfig.TrieCommitInterval != 0 { + current := bc.CurrentBlock().Number.Uint64() + blockShouldCommitOffset := current - current/bc.cacheConfig.TrieCommitInterval*bc.cacheConfig.TrieCommitInterval + if blockShouldCommitOffset > 1 && blockShouldCommitOffset < TriesInMemory-1 { + blockOffsets = append(blockOffsets, blockShouldCommitOffset) + } + } + for _, offset := range blockOffsets { if number := bc.CurrentBlock().Number.Uint64(); number > offset { recent := bc.GetBlockByNumber(number - offset)