Skip to content

Commit 42e777c

Browse files
committed
core: Use TriesInMemory from config
The config setting got mistakenly removed in this PR https://github.com/maticnetwork/bor/pull/1300/files
1 parent 3c7256e commit 42e777c

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

core/blockchain.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func (c *CacheConfig) triedbConfig(isVerkle bool) *triedb.Config {
197197
StateHistory: c.StateHistory,
198198
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
199199
WriteBufferSize: c.TrieDirtyLimit * 1024 * 1024,
200+
MaxDiffLayers: c.TriesInMemory,
200201
}
201202
}
202203
return config
@@ -577,7 +578,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
577578
// NewParallelBlockChain , similar to NewBlockChain, creates a new blockchain object, but with a parallel state processor
578579
func NewParallelBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(header *types.Header) bool, txLookupLimit *uint64, checker ethereum.ChainValidator, numprocs int, enforce bool) (*BlockChain, error) {
579580
bc, err := NewBlockChain(db, cacheConfig, genesis, overrides, engine, vmConfig, shouldPreserve, txLookupLimit, checker)
580-
581581
if err != nil {
582582
return nil, err
583583
}
@@ -1475,7 +1475,7 @@ func (bc *BlockChain) Stop() {
14751475
if !bc.cacheConfig.TrieDirtyDisabled {
14761476
triedb := bc.triedb
14771477

1478-
for _, offset := range []uint64{0, 1, state.TriesInMemory - 1} {
1478+
for _, offset := range []uint64{0, 1, bc.cacheConfig.TriesInMemory - 1} {
14791479
if number := bc.CurrentBlock().Number.Uint64(); number > offset {
14801480
recent := bc.GetBlockByNumber(number - offset)
14811481

@@ -1710,9 +1710,10 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
17101710
}
17111711

17121712
// Delete block data from the main database.
1713-
var (
1714-
canonHashes = make(map[common.Hash]struct{}, len(blockChain))
1715-
)
1713+
var (
1714+
canonHashes = make(map[common.Hash]struct{}, len(blockChain))
1715+
)
1716+
17161717
batch = bc.db.NewBatch()
17171718
for _, block := range blockChain {
17181719
canonHashes[block.Hash()] = struct{}{}
@@ -1954,7 +1955,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
19541955

19551956
// Flush limits are not considered for the first TriesInMemory blocks.
19561957
current := block.NumberU64()
1957-
if current <= state.TriesInMemory {
1958+
if current <= bc.cacheConfig.TriesInMemory {
19581959
return []*types.Log{}, nil
19591960
}
19601961
// If we exceeded our memory allowance, flush matured singleton nodes to disk
@@ -1967,7 +1968,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
19671968
_ = bc.triedb.Cap(limit - ethdb.IdealBatchSize)
19681969
}
19691970
// Find the next state trie we need to commit
1970-
chosen := current - state.TriesInMemory
1971+
chosen := current - bc.cacheConfig.TriesInMemory
19711972
flushInterval := time.Duration(bc.flushInterval.Load())
19721973
// If we exceeded time allowance, flush an entire trie to disk
19731974
if bc.gcproc > flushInterval {
@@ -1979,8 +1980,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
19791980
} else {
19801981
// If we're exceeding limits but haven't reached a large enough memory gap,
19811982
// warn the user that the system is becoming unstable.
1982-
if chosen < bc.lastWrite+state.TriesInMemory && bc.gcproc >= 2*flushInterval {
1983-
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", flushInterval, "optimum", float64(chosen-bc.lastWrite)/state.TriesInMemory)
1983+
if chosen < bc.lastWrite+bc.cacheConfig.TriesInMemory && bc.gcproc >= 2*flushInterval {
1984+
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", flushInterval, "optimum", float64(chosen-bc.lastWrite)/float64(bc.cacheConfig.TriesInMemory))
19841985
}
19851986
// Flush an entire trie and restart the counters
19861987
_ = bc.triedb.Commit(header.Root, true)

triedb/pathdb/database.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const (
5353
)
5454

5555
var (
56-
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
57-
maxDiffLayers = 128
56+
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
57+
maxDiffLayers = 128
5858
)
5959

6060
// layer is the interface implemented by all state layers which includes some
@@ -114,6 +114,7 @@ type Config struct {
114114
CleanCacheSize int // Maximum memory allowance (in bytes) for caching clean nodes
115115
WriteBufferSize int // Maximum memory allowance (in bytes) for write buffer
116116
ReadOnly bool // Flag whether the database is opened in read only mode.
117+
MaxDiffLayers uint64 // Maximum diff layers allowed in the layer tree.
117118
}
118119

119120
// sanitize checks the provided user configurations and changes anything that's
@@ -144,6 +145,7 @@ var Defaults = &Config{
144145
StateHistory: params.FullImmutabilityThreshold,
145146
CleanCacheSize: defaultCleanSize,
146147
WriteBufferSize: defaultBufferSize,
148+
MaxDiffLayers: uint64(maxDiffLayers),
147149
}
148150

149151
// ReadOnly is the config in order to open database in read only mode.
@@ -323,7 +325,11 @@ func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint6
323325
// - head-1 layer is paired with HEAD-1 state
324326
// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
325327
// - head-128 layer(disk layer) is paired with HEAD-128 state
326-
return db.tree.cap(root, maxDiffLayers)
328+
layers := maxDiffLayers
329+
if db.config.MaxDiffLayers > 0 {
330+
layers = int(db.config.MaxDiffLayers)
331+
}
332+
return db.tree.cap(root, layers)
327333
}
328334

329335
// Commit traverses downwards the layer tree from a specified layer with the

0 commit comments

Comments
 (0)