Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't save every state on archive node #210

Merged
merged 11 commits into from
Sep 27, 2023
36 changes: 33 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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

SnapshotNoBuild bool // Whether the background generation is allowed
SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it
}
Expand All @@ -153,8 +156,10 @@ type CacheConfig struct {
var defaultCacheConfig = &CacheConfig{

// Arbitrum Config Options
TriesInMemory: DefaultTriesInMemory,
TrieRetention: 30 * time.Minute,
TriesInMemory: DefaultTriesInMemory,
TrieRetention: 30 * time.Minute,
MaxNumberOfBlocksToSkipStateSaving: 0,
MaxAmountOfGasToSkipStateSaving: 0,

TrieCleanLimit: 256,
TrieDirtyLimit: 256,
Expand Down Expand Up @@ -236,6 +241,9 @@ type BlockChain struct {
processor Processor // Block transaction processor interface
forker *ForkChoice
vmConfig vm.Config

numberOfBlocksToSkipStateSaving uint32
amountOfGasInBlocksToSkipStateSaving uint64
}

type trieGcEntry struct {
Expand Down Expand Up @@ -1442,7 +1450,29 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}
// If we're running an archive node, always flush
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably good to update comment to reflect changed code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment, I am open for any suggestions for improvement

if bc.cacheConfig.TrieDirtyDisabled {
return bc.triedb.Commit(root, false)
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 {
maySkipCommiting = true
if bc.amountOfGasInBlocksToSkipStateSaving >= block.GasUsed() {
bc.amountOfGasInBlocksToSkipStateSaving -= block.GasUsed()
} else {
gasLimitReached = true
}
}
if !maySkipCommiting || blockLimitReached || gasLimitReached {
bc.numberOfBlocksToSkipStateSaving = bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving
bc.amountOfGasInBlocksToSkipStateSaving = bc.cacheConfig.MaxAmountOfGasToSkipStateSaving
return bc.triedb.Commit(root, false)
}
return nil
}

// Full but not archive node, do proper garbage collection
Expand Down