From b884ded52258c18eb4bcaf89b0d41fc3c91b30fe Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 29 Mar 2023 14:19:53 +0000 Subject: [PATCH 1/7] add skipping commiting states --- core/blockchain.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index b86c84b1df..6db888f4a1 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -139,6 +139,9 @@ type CacheConfig struct { TriesInMemory uint64 // Height difference before which a trie may not be garbage-collected TrieRetention time.Duration // Time limit before which a trie may not be garbage-collected + MaxNumberOfBlocksToSkipStateSaving uint32 + MaxAmountOfGasToSkipStateSaving uint64 + SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it } @@ -147,8 +150,10 @@ type CacheConfig struct { var defaultCacheConfig = &CacheConfig{ // Arbitrum Config Options - TriesInMemory: DefaultTriesInMemory, - TrieRetention: 30 * time.Minute, + TriesInMemory: DefaultTriesInMemory, + TrieRetention: 30 * time.Minute, + MaxNumberOfBlocksToSkipStateSaving: 128, + MaxAmountOfGasToSkipStateSaving: 15 * 1000 * 1000, TrieCleanLimit: 256, TrieDirtyLimit: 256, @@ -225,6 +230,9 @@ type BlockChain struct { processor Processor // Block transaction processor interface forker *ForkChoice vmConfig vm.Config + + numberOfBlocksToSkipStateSaving uint32 + amountOfGasInBlocksToSkipStateSaving uint64 } type trieGcEntry struct { @@ -1338,7 +1346,25 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. // If we're running an archive node, always flush if bc.cacheConfig.TrieDirtyDisabled { - return triedb.Commit(root, false, nil) + var skipCommitingState bool + if bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving != 0 && bc.numberOfBlocksToSkipStateSaving > 1 { + bc.numberOfBlocksToSkipStateSaving-- + skipCommitingState = true + } + if bc.cacheConfig.MaxAmountOfGasToSkipStateSaving != 0 && bc.amountOfGasInBlocksToSkipStateSaving > block.GasUsed() { + bc.amountOfGasInBlocksToSkipStateSaving -= block.GasUsed() + skipCommitingState = true + } + if !skipCommitingState { + bc.numberOfBlocksToSkipStateSaving = bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving + bc.amountOfGasInBlocksToSkipStateSaving = bc.cacheConfig.MaxAmountOfGasToSkipStateSaving + // TODO remove log + log.Debug("Saving state", "blockNumber", block.Number(), "blockHash", block.Hash(), "leftBlocks", bc.numberOfBlocksToSkipStateSaving, "leftGas", bc.amountOfGasInBlocksToSkipStateSaving) + return triedb.Commit(root, false, nil) + } else { + // TODO remove log + log.Debug("Skipping saving state", "blockNumber", block.Number(), "blockHash", block.Hash(), "leftBlocks", bc.numberOfBlocksToSkipStateSaving, "leftGas", bc.amountOfGasInBlocksToSkipStateSaving) + } } else { // Full but not archive node, do proper garbage collection triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive From c90d53074f5e57fd39a70994c4143bf3b2496946 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Tue, 11 Apr 2023 12:30:27 +0000 Subject: [PATCH 2/7] fix state commit skipping limits --- core/blockchain.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 6db888f4a1..c7e51c0298 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1346,16 +1346,24 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. // If we're running an archive node, always flush if bc.cacheConfig.TrieDirtyDisabled { - var skipCommitingState bool - if bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving != 0 && bc.numberOfBlocksToSkipStateSaving > 1 { - bc.numberOfBlocksToSkipStateSaving-- - skipCommitingState = true + var maySkipCommiting, blockLimitReached, gasLimitReached bool + if bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving != 0 { + maySkipCommiting = true + if bc.numberOfBlocksToSkipStateSaving > 0 { + bc.numberOfBlocksToSkipStateSaving-- + } else { + blockLimitReached = true + } } - if bc.cacheConfig.MaxAmountOfGasToSkipStateSaving != 0 && bc.amountOfGasInBlocksToSkipStateSaving > block.GasUsed() { - bc.amountOfGasInBlocksToSkipStateSaving -= block.GasUsed() - skipCommitingState = true + if bc.cacheConfig.MaxAmountOfGasToSkipStateSaving != 0 { + maySkipCommiting = true + if bc.amountOfGasInBlocksToSkipStateSaving >= block.GasUsed() { + bc.amountOfGasInBlocksToSkipStateSaving -= block.GasUsed() + } else { + gasLimitReached = true + } } - if !skipCommitingState { + if !maySkipCommiting || blockLimitReached || gasLimitReached { bc.numberOfBlocksToSkipStateSaving = bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving bc.amountOfGasInBlocksToSkipStateSaving = bc.cacheConfig.MaxAmountOfGasToSkipStateSaving // TODO remove log From 5d2eddd1b9c6884756589b0f32b3c6f23c40860f Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Tue, 11 Apr 2023 13:30:58 +0000 Subject: [PATCH 3/7] update MaxNumberOfBlocksToSkipStateSaving default value --- core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index c7e51c0298..c920de6ed6 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -152,7 +152,7 @@ var defaultCacheConfig = &CacheConfig{ // Arbitrum Config Options TriesInMemory: DefaultTriesInMemory, TrieRetention: 30 * time.Minute, - MaxNumberOfBlocksToSkipStateSaving: 128, + MaxNumberOfBlocksToSkipStateSaving: 127, MaxAmountOfGasToSkipStateSaving: 15 * 1000 * 1000, TrieCleanLimit: 256, From a47849f671f2f247bf5b3c02ab7b3bc2785b42fc Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Tue, 13 Jun 2023 16:22:08 +0000 Subject: [PATCH 4/7] fix merge --- core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index 863e6bd30f..578fd900f0 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1472,7 +1472,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.amountOfGasInBlocksToSkipStateSaving = bc.cacheConfig.MaxAmountOfGasToSkipStateSaving // TODO remove log log.Debug("Saving state", "blockNumber", block.Number(), "blockHash", block.Hash(), "leftBlocks", bc.numberOfBlocksToSkipStateSaving, "leftGas", bc.amountOfGasInBlocksToSkipStateSaving) - return triedb.Commit(root, false, nil) + return bc.triedb.Commit(root, false) } // TODO remove log log.Debug("Skipping saving state", "blockNumber", block.Number(), "blockHash", block.Hash(), "leftBlocks", bc.numberOfBlocksToSkipStateSaving, "leftGas", bc.amountOfGasInBlocksToSkipStateSaving) From eb96086a95859470cfedd3539a9c8bb3132f015f Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Thu, 15 Jun 2023 01:35:13 +0000 Subject: [PATCH 5/7] disable state save skipping in default geth cache config --- core/blockchain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 578fd900f0..6922670192 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -158,8 +158,8 @@ var defaultCacheConfig = &CacheConfig{ // Arbitrum Config Options TriesInMemory: DefaultTriesInMemory, TrieRetention: 30 * time.Minute, - MaxNumberOfBlocksToSkipStateSaving: 127, - MaxAmountOfGasToSkipStateSaving: 15 * 1000 * 1000, + MaxNumberOfBlocksToSkipStateSaving: 0, + MaxAmountOfGasToSkipStateSaving: 0, TrieCleanLimit: 256, TrieDirtyLimit: 256, From c86303cf0b7c10f55a19b767a326a8bd06ce8108 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Thu, 15 Jun 2023 01:54:26 +0000 Subject: [PATCH 6/7] remove debug logs --- core/blockchain.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 6922670192..a9439e0981 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1470,12 +1470,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. if !maySkipCommiting || blockLimitReached || gasLimitReached { bc.numberOfBlocksToSkipStateSaving = bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving bc.amountOfGasInBlocksToSkipStateSaving = bc.cacheConfig.MaxAmountOfGasToSkipStateSaving - // TODO remove log - log.Debug("Saving state", "blockNumber", block.Number(), "blockHash", block.Hash(), "leftBlocks", bc.numberOfBlocksToSkipStateSaving, "leftGas", bc.amountOfGasInBlocksToSkipStateSaving) return bc.triedb.Commit(root, false) } - // TODO remove log - log.Debug("Skipping saving state", "blockNumber", block.Number(), "blockHash", block.Hash(), "leftBlocks", bc.numberOfBlocksToSkipStateSaving, "leftGas", bc.amountOfGasInBlocksToSkipStateSaving) return nil } From 89f53b035d7a8b9b1ff8599958bf0c55efcdf718 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Tue, 26 Sep 2023 22:12:59 +0000 Subject: [PATCH 7/7] update flushing comment --- core/blockchain.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index ac91e46d44..79d97d9d0d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1447,7 +1447,10 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. if err != nil { return err } - // If we're running an archive node, always flush + // If we're running an archive node, flush + // If MaxNumberOfBlocksToSkipStateSaving or MaxAmountOfGasToSkipStateSaving is not zero, then flushing of some blocks will be skipped: + // * at most MaxNumberOfBlocksToSkipStateSaving block state commits will be skipped + // * sum of gas used in skipped blocks will be at most MaxAmountOfGasToSkipStateSaving if bc.cacheConfig.TrieDirtyDisabled { var maySkipCommiting, blockLimitReached, gasLimitReached bool if bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving != 0 {