Skip to content

Commit 515b671

Browse files
rjl493456442fjl
andcommitted
core: consolidate BlockChain constructor options (ethereum#31925)
In this pull request, the original `CacheConfig` has been renamed to `BlockChainConfig`. Over time, more fields have been added to `CacheConfig` to support blockchain configuration. Such as `ChainHistoryMode`, which clearly extends beyond just caching concerns. Additionally, adding new parameters to the blockchain constructor has become increasingly complicated, since it’s initialized across multiple places in the codebase. A natural solution is to consolidate these arguments into a dedicated configuration struct. As a result, the existing `CacheConfig` has been redefined as `BlockChainConfig`. Some parameters, such as `VmConfig`, `TxLookupLimit`, and `ChainOverrides` have been moved into `BlockChainConfig`. Besides, a few fields in `BlockChainConfig` were renamed, specifically: - `TrieCleanNoPrefetch` -> `NoPrefetch` - `TrieDirtyDisabled` -> `ArchiveMode` Notably, this change won't affect the command line flags or the toml configuration file. It's just an internal refactoring and fully backward-compatible. --------- Co-authored-by: Felix Lange <[email protected]>
1 parent 1f27d04 commit 515b671

36 files changed

+343
-308
lines changed

cmd/utils/flags.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,36 +2164,38 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
21642164
if err != nil {
21652165
Fatalf("%v", err)
21662166
}
2167-
cache := &core.CacheConfig{
2168-
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2169-
TrieCleanNoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2170-
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2171-
TrieDirtyDisabled: ctx.String(GCModeFlag.Name) == "archive",
2172-
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2173-
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2174-
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2175-
StateScheme: scheme,
2176-
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2177-
}
2178-
if cache.TrieDirtyDisabled && !cache.Preimages {
2179-
cache.Preimages = true
2167+
options := &core.BlockChainConfig{
2168+
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
2169+
NoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
2170+
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
2171+
ArchiveMode: ctx.String(GCModeFlag.Name) == "archive",
2172+
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
2173+
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
2174+
Preimages: ctx.Bool(CachePreimagesFlag.Name),
2175+
StateScheme: scheme,
2176+
StateHistory: ctx.Uint64(StateHistoryFlag.Name),
2177+
// Disable transaction indexing/unindexing.
2178+
TxLookupLimit: -1,
2179+
}
2180+
if options.ArchiveMode && !options.Preimages {
2181+
options.Preimages = true
21802182
log.Info("Enabling recording of key preimages since archive mode is used")
21812183
}
21822184
if !ctx.Bool(SnapshotFlag.Name) {
2183-
cache.SnapshotLimit = 0 // Disabled
2185+
options.SnapshotLimit = 0 // Disabled
21842186
} else if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheSnapshotFlag.Name) {
2185-
cache.SnapshotLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheSnapshotFlag.Name) / 100
2187+
options.SnapshotLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheSnapshotFlag.Name) / 100
21862188
}
21872189
// If we're in readonly, do not bother generating snapshot data.
21882190
if readonly {
2189-
cache.SnapshotNoBuild = true
2191+
options.SnapshotNoBuild = true
21902192
}
21912193

21922194
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
2193-
cache.TrieCleanLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
2195+
options.TrieCleanLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
21942196
}
21952197
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheGCFlag.Name) {
2196-
cache.TrieDirtyLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheGCFlag.Name) / 100
2198+
options.TrieDirtyLimit = ctx.Int(CacheFlag.Name) * ctx.Int(CacheGCFlag.Name) / 100
21972199
}
21982200
vmcfg := vm.Config{
21992201
EnablePreimageRecording: ctx.Bool(VMEnableDebugFlag.Name),
@@ -2208,8 +2210,9 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
22082210
vmcfg.Tracer = t
22092211
}
22102212
}
2211-
// Disable transaction indexing/unindexing by default.
2212-
chain, err := core.NewBlockChain(chainDb, cache, gspec, nil, engine, vmcfg, nil)
2213+
options.VmConfig = vmcfg
2214+
2215+
chain, err := core.NewBlockChain(chainDb, gspec, engine, options)
22132216
if err != nil {
22142217
Fatalf("Can't create BlockChain: %v", err)
22152218
}

cmd/utils/history_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/ethereum/go-ethereum/core"
3232
"github.com/ethereum/go-ethereum/core/rawdb"
3333
"github.com/ethereum/go-ethereum/core/types"
34-
"github.com/ethereum/go-ethereum/core/vm"
3534
"github.com/ethereum/go-ethereum/crypto"
3635
"github.com/ethereum/go-ethereum/internal/era"
3736
"github.com/ethereum/go-ethereum/params"
@@ -78,7 +77,7 @@ func TestHistoryImportAndExport(t *testing.T) {
7877
})
7978

8079
// Initialize BlockChain.
81-
chain, err := core.NewBlockChain(db, nil, genesis, nil, ethash.NewFaker(), vm.Config{}, nil)
80+
chain, err := core.NewBlockChain(db, genesis, ethash.NewFaker(), nil)
8281
if err != nil {
8382
t.Fatalf("unable to initialize chain: %v", err)
8483
}
@@ -167,7 +166,7 @@ func TestHistoryImportAndExport(t *testing.T) {
167166
})
168167

169168
genesis.MustCommit(db2, triedb.NewDatabase(db2, triedb.HashDefaults))
170-
imported, err := core.NewBlockChain(db2, nil, genesis, nil, ethash.NewFaker(), vm.Config{}, nil)
169+
imported, err := core.NewBlockChain(db2, genesis, ethash.NewFaker(), nil)
171170
if err != nil {
172171
t.Fatalf("unable to initialize chain: %v", err)
173172
}

consensus/clique/clique_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/ethereum/go-ethereum/core"
2525
"github.com/ethereum/go-ethereum/core/rawdb"
2626
"github.com/ethereum/go-ethereum/core/types"
27-
"github.com/ethereum/go-ethereum/core/vm"
2827
"github.com/ethereum/go-ethereum/crypto"
2928
"github.com/ethereum/go-ethereum/params"
3029
)
@@ -55,7 +54,7 @@ func TestReimportMirroredState(t *testing.T) {
5554
copy(genspec.ExtraData[extraVanity:], addr[:])
5655

5756
// Generate a batch of blocks, each properly signed
58-
chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil)
57+
chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), genspec, engine, nil)
5958
defer chain.Stop()
6059

6160
_, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 3, func(i int, block *core.BlockGen) {
@@ -87,7 +86,7 @@ func TestReimportMirroredState(t *testing.T) {
8786
}
8887
// Insert the first two blocks and make sure the chain is valid
8988
db = rawdb.NewMemoryDatabase()
90-
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil)
89+
chain, _ = core.NewBlockChain(db, genspec, engine, nil)
9190
defer chain.Stop()
9291

9392
if _, err := chain.InsertChain(blocks[:2]); err != nil {
@@ -100,7 +99,7 @@ func TestReimportMirroredState(t *testing.T) {
10099
// Simulate a crash by creating a new chain on top of the database, without
101100
// flushing the dirty states out. Insert the last block, triggering a sidechain
102101
// reimport.
103-
chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil)
102+
chain, _ = core.NewBlockChain(db, genspec, engine, nil)
104103
defer chain.Stop()
105104

106105
if _, err := chain.InsertChain(blocks[2:]); err != nil {

consensus/clique/snapshot_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/ethereum/go-ethereum/core"
2929
"github.com/ethereum/go-ethereum/core/rawdb"
3030
"github.com/ethereum/go-ethereum/core/types"
31-
"github.com/ethereum/go-ethereum/core/vm"
3231
"github.com/ethereum/go-ethereum/crypto"
3332
"github.com/ethereum/go-ethereum/params"
3433
)
@@ -458,7 +457,7 @@ func (tt *cliqueTest) run(t *testing.T) {
458457
batches[len(batches)-1] = append(batches[len(batches)-1], block)
459458
}
460459
// Pass all the headers through clique and ensure tallying succeeds
461-
chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genesis, nil, engine, vm.Config{}, nil)
460+
chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), genesis, engine, nil)
462461
if err != nil {
463462
t.Fatalf("failed to create test chain: %v", err)
464463
}

core/bench_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/ethereum/go-ethereum/consensus/ethash"
2727
"github.com/ethereum/go-ethereum/core/rawdb"
2828
"github.com/ethereum/go-ethereum/core/types"
29-
"github.com/ethereum/go-ethereum/core/vm"
3029
"github.com/ethereum/go-ethereum/crypto"
3130
"github.com/ethereum/go-ethereum/ethdb"
3231
"github.com/ethereum/go-ethereum/ethdb/pebble"
@@ -200,7 +199,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
200199

201200
// Time the insertion of the new chain.
202201
// State and blocks are stored in the same DB.
203-
chainman, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil)
202+
chainman, _ := NewBlockChain(db, gspec, ethash.NewFaker(), nil)
204203
defer chainman.Stop()
205204
b.ReportAllocs()
206205
b.ResetTimer()
@@ -325,9 +324,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
325324
genesis := &Genesis{Config: params.AllEthashProtocolChanges}
326325
makeChainForBench(db, genesis, full, count)
327326
db.Close()
328-
cacheConfig := *defaultCacheConfig
329-
cacheConfig.TrieDirtyDisabled = true
330-
327+
options := DefaultConfig().WithArchive(true)
331328
b.ReportAllocs()
332329
b.ResetTimer()
333330

@@ -338,7 +335,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
338335
}
339336
db = rawdb.NewDatabase(pdb)
340337

341-
chain, err := NewBlockChain(db, &cacheConfig, genesis, nil, ethash.NewFaker(), vm.Config{}, nil)
338+
chain, err := NewBlockChain(db, genesis, ethash.NewFaker(), options)
342339
if err != nil {
343340
b.Fatalf("error creating chain: %v", err)
344341
}

core/block_validator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/ethereum/go-ethereum/consensus/ethash"
2929
"github.com/ethereum/go-ethereum/core/rawdb"
3030
"github.com/ethereum/go-ethereum/core/types"
31-
"github.com/ethereum/go-ethereum/core/vm"
3231
"github.com/ethereum/go-ethereum/crypto"
3332
"github.com/ethereum/go-ethereum/params"
3433
)
@@ -50,7 +49,8 @@ func testHeaderVerification(t *testing.T, scheme string) {
5049
headers[i] = block.Header()
5150
}
5251
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
53-
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil)
52+
options := DefaultConfig().WithStateScheme(scheme)
53+
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), gspec, ethash.NewFaker(), options)
5454
defer chain.Stop()
5555
if err != nil {
5656
t.Fatal(err)
@@ -166,7 +166,7 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
166166
postHeaders[i] = block.Header()
167167
}
168168
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
169-
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil)
169+
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), gspec, engine, nil)
170170
defer chain.Stop()
171171
if err != nil {
172172
t.Fatal(err)

0 commit comments

Comments
 (0)