1- // Copyright 2014 The go-ethereum Authors
1+ // Copyright 2022 The go-ethereum Authors
22// This file is part of the go-ethereum library.
33//
44// The go-ethereum library is free software: you can redistribute it and/or modify
8181
8282 errInsertionInterrupted = errors .New ("insertion is interrupted" )
8383 errChainStopped = errors .New ("blockchain is stopped" )
84-
85- lastWrite uint64
8684)
8785
8886const (
@@ -161,14 +159,16 @@ var defaultCacheConfig = &CacheConfig{
161159// included in the canonical one where as GetBlockByNumber always represents the
162160// canonical chain.
163161type BlockChain struct {
164- chainConfig * params.ChainConfig // Chain & network configuration
165- cacheConfig * CacheConfig // Cache configuration for pruning
162+ // trieFlushFreq is accessed atomically and needs to be 64-bit aligned.
163+ trieFlushFreq uint64 // # blocks after which to flush the current in-memory trie to disk (0 = only consider time limit, 1 = archive mode)
164+ chainConfig * params.ChainConfig // Chain & network configuration
165+ cacheConfig * CacheConfig // Cache configuration for pruning
166166
167- db ethdb.Database // Low level persistent database to store final content in
168- snaps * snapshot.Tree // Snapshot tree for fast trie leaf access
169- triegc * prque.Prque // Priority queue mapping block numbers to tries to gc
170- gcproc time.Duration // Accumulates canonical block processing for trie dumping
171- trieFlushFreq uint64 // # blocks after which to flush the current in-memory trie to disk (0 = only consider time limit, 1 = archive mode)
167+ db ethdb.Database // Low level persistent database to store final content in
168+ snaps * snapshot.Tree // Snapshot tree for fast trie leaf access
169+ triegc * prque.Prque // Priority queue mapping block numbers to tries to gc
170+ gcproc time.Duration // Accumulates canonical block processing for trie dumping
171+ lastWrite uint64 // Last block when the state was flushed
172172
173173 // txLookupLimit is the maximum number of blocks from head whose tx indices
174174 // are reserved:
@@ -1268,7 +1268,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12681268 // Find the next state trie we need to commit
12691269 var (
12701270 chosen = current - TriesInMemory
1271- sinceFlush = chosen - lastWrite
1271+ sinceFlush = chosen - bc . lastWrite
12721272 doFlush = bc .gcproc > bc .cacheConfig .TrieTimeLimit
12731273 )
12741274 if freq > 1 {
@@ -1285,12 +1285,12 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12851285 } else {
12861286 // If we're exceeding limits but haven't reached a large enough memory gap,
12871287 // warn the user that the system is becoming unstable.
1288- if chosen < lastWrite + TriesInMemory && bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit {
1289- log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - lastWrite )/ TriesInMemory )
1288+ if chosen < bc . lastWrite + TriesInMemory && bc .gcproc >= 2 * bc .cacheConfig .TrieTimeLimit {
1289+ log .Info ("State in memory for too long, committing" , "time" , bc .gcproc , "allowance" , bc .cacheConfig .TrieTimeLimit , "optimum" , float64 (chosen - bc . lastWrite )/ TriesInMemory )
12901290 }
12911291 // Flush an entire trie and restart the counters
12921292 triedb .Commit (header .Root , true , nil )
1293- lastWrite = chosen
1293+ bc . lastWrite = chosen
12941294 bc .gcproc = 0
12951295 }
12961296 }
0 commit comments