diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 2faf274dbd..b071e4c25d 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -84,7 +84,12 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis GasLimit: gasLimit, Alloc: alloc, } - blockchain, _ := core.NewBlockChain(database, nil, &genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + config := core.NewBlockChainConfig( + core.WithGenesis(&genesis), + core.WithVmConfig(&vm.Config{}), + ) + blockchain, _ := core.NewBlockChain(database, ethash.NewFaker(), config) backend := &SimulatedBackend{ database: database, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 159c47ca01..2b43465d72 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -2121,8 +2121,14 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh } vmcfg := vm.Config{EnablePreimageRecording: ctx.Bool(VMEnableDebugFlag.Name)} + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(cache), + core.WithGenesis(gspec), + core.WithVmConfig(&vmcfg), + ) + // Disable transaction indexing/unindexing by default. - chain, err := core.NewBlockChain(chainDb, cache, gspec, nil, engine, vmcfg, nil, nil) + chain, err := core.NewBlockChain(chainDb, engine, bcConfig) if err != nil { Fatalf("Can't create BlockChain: %v", err) } diff --git a/consensus/clique/clique_test.go b/consensus/clique/clique_test.go index 7cd5919c5e..76dfe2e308 100644 --- a/consensus/clique/clique_test.go +++ b/consensus/clique/clique_test.go @@ -54,8 +54,13 @@ func TestReimportMirroredState(t *testing.T) { } copy(genspec.ExtraData[extraVanity:], addr[:]) + config := core.NewBlockChainConfig( + core.WithGenesis(genspec), + core.WithVmConfig(&vm.Config{}), + ) + // Generate a batch of blocks, each properly signed - chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil, nil) + chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) defer chain.Stop() _, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 3, func(i int, block *core.BlockGen) { @@ -87,7 +92,7 @@ func TestReimportMirroredState(t *testing.T) { } // Insert the first two blocks and make sure the chain is valid db = rawdb.NewMemoryDatabase() - chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil) + chain, _ = core.NewBlockChain(db, engine, config) defer chain.Stop() if _, err := chain.InsertChain(blocks[:2]); err != nil { @@ -100,7 +105,7 @@ func TestReimportMirroredState(t *testing.T) { // Simulate a crash by creating a new chain on top of the database, without // flushing the dirty states out. Insert the last block, triggering a sidechain // reimport. - chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil) + chain, _ = core.NewBlockChain(db, engine, config) defer chain.Stop() if _, err := chain.InsertChain(blocks[2:]); err != nil { diff --git a/consensus/clique/snapshot_test.go b/consensus/clique/snapshot_test.go index 26cebe008a..68a3d35d39 100644 --- a/consensus/clique/snapshot_test.go +++ b/consensus/clique/snapshot_test.go @@ -457,8 +457,14 @@ func (tt *cliqueTest) run(t *testing.T) { } batches[len(batches)-1] = append(batches[len(batches)-1], block) } + + bcConfig := core.NewBlockChainConfig( + core.WithGenesis(genesis), + core.WithVmConfig(&vm.Config{}), + ) + // Pass all the headers through clique and ensure tallying succeeds - chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genesis, nil, engine, vm.Config{}, nil, nil) + chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), engine, bcConfig) if err != nil { t.Fatalf("failed to create test chain: %v", err) } diff --git a/core/bench_test.go b/core/bench_test.go index c5991f10e8..baf7f47b7c 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -195,7 +195,9 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) { // Time the insertion of the new chain. // State and blocks are stored in the same DB. - chainman, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + config := NewBlockChainConfig(WithGenesis(gspec), WithVmConfig(&vm.Config{})) + + chainman, _ := NewBlockChain(db, ethash.NewFaker(), config) defer chainman.Stop() b.ReportAllocs() b.ResetTimer() @@ -298,6 +300,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) { db.Close() cacheConfig := *defaultCacheConfig cacheConfig.TrieDirtyDisabled = true + config := NewBlockChainConfig(WithCacheConfig(&cacheConfig), WithVmConfig(&vm.Config{})) b.ReportAllocs() b.ResetTimer() @@ -307,7 +310,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) { if err != nil { b.Fatalf("error opening database at %v: %v", dir, err) } - chain, err := NewBlockChain(db, &cacheConfig, nil, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + chain, err := NewBlockChain(db, ethash.NewFaker(), config) if err != nil { b.Fatalf("error creating chain: %v", err) } diff --git a/core/block_validator_test.go b/core/block_validator_test.go index 48bdceff62..d5f4f7f1f1 100644 --- a/core/block_validator_test.go +++ b/core/block_validator_test.go @@ -50,7 +50,14 @@ func testHeaderVerification(t *testing.T, scheme string) { headers[i] = block.Header() } // Run the header checker for blocks one-by-one, checking for both valid and invalid nonces - chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + config := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + ) + + chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), config) defer chain.Stop() for i := 0; i < len(blocks); i++ { @@ -163,7 +170,8 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) { t.Logf("Post-merge header: %d", block.NumberU64()) } // Run the header checker for blocks one-by-one, checking for both valid and invalid nonces - chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig(WithGenesis(gspec), WithVmConfig(&vm.Config{})) + chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) defer chain.Stop() // Verify the blocks before the merging diff --git a/core/blockchain.go b/core/blockchain.go index f458da8257..43b345b356 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -260,20 +260,98 @@ type BlockChain struct { vmConfig vm.Config } +// blockChainConfig stores configuration options for the BlockChain. +type blockChainConfig struct { + cacheConfig *CacheConfig + genesis *Genesis + overrides *ChainOverrides + shouldPreserve func(header *types.Header) bool + txLookupLimit *uint64 + vmConfig *vm.Config +} + +// A BlockChainOption is a function that takes in and modifies a *blockChainConfig +type BlockChainOption func(config *blockChainConfig) + +// NewBlockChainConfig creates a new blockChainConfig with optional configurations. +func NewBlockChainConfig(options ...BlockChainOption) *blockChainConfig { + var config blockChainConfig + + for _, f := range options { + f(&config) + } + + return &config +} + +// WithCacheConfig returns a BlockChainOption that sets the CacheConfig. +func WithCacheConfig(cacheConfig *CacheConfig) BlockChainOption { + changeConfig := func(bcConfig *blockChainConfig) { + bcConfig.cacheConfig = cacheConfig + } + + return changeConfig +} + +// WithGenesis returns a BlockChainOption that sets the genesis block. +func WithGenesis(genesis *Genesis) BlockChainOption { + changeGenesis := func(bcConfig *blockChainConfig) { + bcConfig.genesis = genesis + } + + return changeGenesis +} + +// WithOverrides returns a BlockChainOption that sets the overrides for the BlockChain. +func WithOverrides(overrides *ChainOverrides) BlockChainOption { + changeOverrides := func(config *blockChainConfig) { + config.overrides = overrides + } + + return changeOverrides +} + +// WithShouldPreserve returns a BlockChainOption that sets the shouldPreserve function. +func WithShouldPreserve(shouldPreserve func(header *types.Header) bool) BlockChainOption { + changeShouldPreserve := func(config *blockChainConfig) { + config.shouldPreserve = shouldPreserve + } + + return changeShouldPreserve +} + +// WithTxLookupLimit returns a BlockChainOption that sets the transaction lookup limit. +func WithTxLookupLimit(limit *uint64) BlockChainOption { + changeTxLookupLimit := func(config *blockChainConfig) { + config.txLookupLimit = limit + } + + return changeTxLookupLimit +} + +// WithVmConfig returns a BlockChainOption that sets the vm.Config. +func WithVmConfig(vmConfig *vm.Config) BlockChainOption { + changeVmConfig := func(config *blockChainConfig) { + config.vmConfig = vmConfig + } + + return changeVmConfig +} + // NewBlockChain returns a fully initialised block chain using information // available in the database. It initialises the default Ethereum Validator // and Processor. -func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis, overrides *ChainOverrides, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(header *types.Header) bool, txLookupLimit *uint64) (*BlockChain, error) { - if cacheConfig == nil { - cacheConfig = defaultCacheConfig +func NewBlockChain(db ethdb.Database, engine consensus.Engine, bcConfig *blockChainConfig) (*BlockChain, error) { + if bcConfig.cacheConfig == nil { + bcConfig.cacheConfig = defaultCacheConfig } // Open trie database with provided config - triedb := trie.NewDatabase(db, cacheConfig.triedbConfig()) + triedb := trie.NewDatabase(db, bcConfig.cacheConfig.triedbConfig()) // Setup the genesis block, commit the provided genesis specification // to database if the genesis block is not present yet, or load the // stored one from database. - chainConfig, genesisHash, genesisErr := SetupGenesisBlockWithOverride(db, triedb, genesis, overrides) + chainConfig, genesisHash, genesisErr := SetupGenesisBlockWithOverride(db, triedb, bcConfig.genesis, bcConfig.overrides) if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok { return nil, genesisErr } @@ -287,7 +365,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis bc := &BlockChain{ chainConfig: chainConfig, - cacheConfig: cacheConfig, + cacheConfig: bcConfig.cacheConfig, db: db, triedb: triedb, triegc: prque.New[int64, common.Hash](nil), @@ -300,10 +378,10 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis txLookupCache: lru.NewCache[common.Hash, *rawdb.LegacyTxLookupEntry](txLookupCacheLimit), futureBlocks: lru.NewCache[common.Hash, *types.Block](maxFutureBlocks), engine: engine, - vmConfig: vmConfig, + vmConfig: *bcConfig.vmConfig, } - bc.flushInterval.Store(int64(cacheConfig.TrieTimeLimit)) - bc.forker = NewForkChoice(bc, shouldPreserve) + bc.flushInterval.Store(int64(bcConfig.cacheConfig.TrieTimeLimit)) + bc.forker = NewForkChoice(bc, bcConfig.shouldPreserve) bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb) bc.validator = NewBlockValidator(chainConfig, bc, engine) bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine) @@ -464,8 +542,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis rawdb.WriteChainConfig(db, genesisHash, chainConfig) } // Start tx indexer/unindexer if required. - if txLookupLimit != nil { - bc.txLookupLimit = *txLookupLimit + if bcConfig.txLookupLimit != nil { + bc.txLookupLimit = *bcConfig.txLookupLimit bc.wg.Add(1) go bc.maintainTxIndex() diff --git a/core/blockchain_repair_test.go b/core/blockchain_repair_test.go index b2df39d17b..7fe5f51853 100644 --- a/core/blockchain_repair_test.go +++ b/core/blockchain_repair_test.go @@ -1794,7 +1794,10 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s config.SnapshotLimit = 256 config.SnapshotWait = true } - chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil) + + baseConfig := NewBlockChainConfig(WithCacheConfig(config), WithGenesis(gspec), WithVmConfig(&vm.Config{})) + + chain, err := NewBlockChain(db, engine, baseConfig) if err != nil { t.Fatalf("Failed to create chain: %v", err) } @@ -1855,7 +1858,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s } defer db.Close() - newChain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil) + newChain, err := NewBlockChain(db, engine, baseConfig) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -1927,7 +1930,14 @@ func testIssue23496(t *testing.T, scheme string) { } engine = ethash.NewFullFaker() ) - chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + + baseConfig := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(db, engine, baseConfig) if err != nil { t.Fatalf("Failed to create chain: %v", err) } @@ -1977,7 +1987,7 @@ func testIssue23496(t *testing.T, scheme string) { } defer db.Close() - chain, err = NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + chain, err = NewBlockChain(db, engine, baseConfig) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } diff --git a/core/blockchain_sethead_test.go b/core/blockchain_sethead_test.go index fa739f924f..b68e6adb05 100644 --- a/core/blockchain_sethead_test.go +++ b/core/blockchain_sethead_test.go @@ -1997,7 +1997,14 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme config.SnapshotLimit = 256 config.SnapshotWait = true } - chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil) + + bcConfig := NewBlockChainConfig( + WithGenesis(gspec), + WithCacheConfig(config), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(db, engine, bcConfig) if err != nil { t.Fatalf("Failed to create chain: %v", err) } diff --git a/core/blockchain_snapshot_test.go b/core/blockchain_snapshot_test.go index dd012c430c..1c35418db2 100644 --- a/core/blockchain_snapshot_test.go +++ b/core/blockchain_snapshot_test.go @@ -80,8 +80,13 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo Config: params.AllEthashProtocolChanges, } engine = ethash.NewFullFaker() + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(basic.scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) - chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(basic.scheme), gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(db, engine, config) if err != nil { t.Fatalf("Failed to create chain: %v", err) } @@ -228,7 +233,14 @@ func (snaptest *snapshotTest) test(t *testing.T) { // Restart the chain normally chain.Stop() - newchain, err := NewBlockChain(snaptest.db, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(snaptest.scheme)), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + newchain, err := NewBlockChain(snaptest.db, snaptest.engine, config) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -270,13 +282,19 @@ func (snaptest *crashSnapshotTest) test(t *testing.T) { // the crash, we do restart twice here: one after the crash and one // after the normal stop. It's used to ensure the broken snapshot // can be detected all the time. - newchain, err := NewBlockChain(newdb, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(snaptest.scheme)), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + newchain, err := NewBlockChain(newdb, snaptest.engine, config) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } newchain.Stop() - newchain, err = NewBlockChain(newdb, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + newchain, err = NewBlockChain(newdb, snaptest.engine, config) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -313,15 +331,28 @@ func (snaptest *gappedSnapshotTest) test(t *testing.T) { SnapshotLimit: 0, StateScheme: snaptest.scheme, } - newchain, err := NewBlockChain(snaptest.db, cacheConfig, snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + + config := NewBlockChainConfig( + WithCacheConfig(cacheConfig), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + newchain, err := NewBlockChain(snaptest.db, snaptest.engine, config) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } newchain.InsertChain(gappedBlocks) newchain.Stop() + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(snaptest.scheme)), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + // Restart the chain with enabling the snapshot - newchain, err = NewBlockChain(snaptest.db, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + newchain, err = NewBlockChain(snaptest.db, snaptest.engine, config) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -349,7 +380,13 @@ func (snaptest *setHeadSnapshotTest) test(t *testing.T) { chain.SetHead(snaptest.setHead) chain.Stop() - newchain, err := NewBlockChain(snaptest.db, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(snaptest.scheme)), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + newchain, err := NewBlockChain(snaptest.db, snaptest.engine, config) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -385,7 +422,14 @@ func (snaptest *wipeCrashSnapshotTest) test(t *testing.T) { SnapshotLimit: 0, StateScheme: snaptest.scheme, } - newchain, err := NewBlockChain(snaptest.db, config, snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + + bcConfig := NewBlockChainConfig( + WithCacheConfig(config), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + newchain, err := NewBlockChain(snaptest.db, snaptest.engine, bcConfig) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -402,7 +446,14 @@ func (snaptest *wipeCrashSnapshotTest) test(t *testing.T) { SnapshotWait: false, // Don't wait rebuild StateScheme: snaptest.scheme, } - tmp, err := NewBlockChain(snaptest.db, config, snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + + bcConfig = NewBlockChainConfig( + WithCacheConfig(config), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + tmp, err := NewBlockChain(snaptest.db, snaptest.engine, bcConfig) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } @@ -411,7 +462,13 @@ func (snaptest *wipeCrashSnapshotTest) test(t *testing.T) { tmp.triedb.Close() tmp.stopWithoutSaving() - newchain, err = NewBlockChain(snaptest.db, DefaultCacheConfigWithScheme(snaptest.scheme), snaptest.gspec, nil, snaptest.engine, vm.Config{}, nil, nil) + bcConfig = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(snaptest.scheme)), + WithGenesis(snaptest.gspec), + WithVmConfig(&vm.Config{}), + ) + + newchain, err = NewBlockChain(snaptest.db, snaptest.engine, bcConfig) if err != nil { t.Fatalf("Failed to recreate chain: %v", err) } diff --git a/core/blockchain_test.go b/core/blockchain_test.go index bc6f8112f0..6e001081af 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -53,14 +53,19 @@ var ( // header only chain. The database and genesis specification for block generation // are also returned in case more test blocks are needed later. func newCanonical(engine consensus.Engine, n int, full bool, scheme string) (ethdb.Database, *Genesis, *BlockChain, error) { - var ( - genesis = &Genesis{ - BaseFee: big.NewInt(params.InitialBaseFee), - Config: params.AllEthashProtocolChanges, - } - ) // Initialize a fresh chain with only a genesis block - blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + genesis := &Genesis{ + BaseFee: big.NewInt(params.InitialBaseFee), + Config: params.AllEthashProtocolChanges, + } + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + + blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) // Create and inject the requested chain if n == 0 { @@ -739,7 +744,13 @@ func testReorgBadHashes(t *testing.T, full bool, scheme string) { blockchain.Stop() // Create a new BlockChain and check that it rolled back the state. - ncm, err := NewBlockChain(blockchain.db, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + ncm, err := NewBlockChain(blockchain.db, ethash.NewFaker(), config) if err != nil { t.Fatalf("failed to create new chain manager: %v", err) } @@ -842,6 +853,12 @@ func testFastVsFullChains(t *testing.T, scheme string) { BaseFee: big.NewInt(params.InitialBaseFee), } signer = types.LatestSigner(gspec.Config) + + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) _, blocks, receipts := GenerateChainWithGenesis(gspec, ethash.NewFaker(), 1024, func(i int, block *BlockGen) { block.SetCoinbase(common.Address{0x00}) @@ -863,7 +880,7 @@ func testFastVsFullChains(t *testing.T, scheme string) { }) // Import the chain as an archive node for the comparison baseline archiveDb := rawdb.NewMemoryDatabase() - archive, _ := NewBlockChain(archiveDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + archive, _ := NewBlockChain(archiveDb, ethash.NewFaker(), config) defer archive.Stop() if n, err := archive.InsertChain(blocks); err != nil { @@ -871,7 +888,7 @@ func testFastVsFullChains(t *testing.T, scheme string) { } // Fast import the chain as a non-archive node to test fastDb := rawdb.NewMemoryDatabase() - fast, _ := NewBlockChain(fastDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + fast, _ := NewBlockChain(fastDb, ethash.NewFaker(), config) defer fast.Stop() headers := make([]*types.Header, len(blocks)) @@ -891,7 +908,7 @@ func testFastVsFullChains(t *testing.T, scheme string) { } defer ancientDb.Close() - ancient, _ := NewBlockChain(ancientDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + ancient, _ := NewBlockChain(ancientDb, ethash.NewFaker(), config) defer ancient.Stop() if n, err := ancient.InsertHeaderChain(headers); err != nil { @@ -1011,7 +1028,13 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) { archiveCaching.TrieDirtyDisabled = true archiveCaching.StateScheme = scheme - archive, _ := NewBlockChain(archiveDb, &archiveCaching, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(&archiveCaching), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + archive, _ := NewBlockChain(archiveDb, ethash.NewFaker(), config) if n, err := archive.InsertChain(blocks); err != nil { t.Fatalf("failed to process block %d: %v", n, err) } @@ -1024,7 +1047,14 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) { // Import the chain as a non-archive node and ensure all pointers are updated fastDb := makeDb() defer fastDb.Close() - fast, _ := NewBlockChain(fastDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + fast, _ := NewBlockChain(fastDb, ethash.NewFaker(), config) defer fast.Stop() headers := make([]*types.Header, len(blocks)) @@ -1044,7 +1074,7 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) { // Import the chain as a ancient-first node and ensure all pointers are updated ancientDb := makeDb() defer ancientDb.Close() - ancient, _ := NewBlockChain(ancientDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + ancient, _ := NewBlockChain(ancientDb, ethash.NewFaker(), config) defer ancient.Stop() if n, err := ancient.InsertHeaderChain(headers); err != nil { @@ -1063,7 +1093,7 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) { // Import the chain as a light node and ensure all pointers are updated lightDb := makeDb() defer lightDb.Close() - light, _ := NewBlockChain(lightDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + light, _ := NewBlockChain(lightDb, ethash.NewFaker(), config) if n, err := light.InsertHeaderChain(headers); err != nil { t.Fatalf("failed to insert header %d: %v", n, err) } @@ -1136,7 +1166,14 @@ func testChainTxReorgs(t *testing.T, scheme string) { }) // Import the chain. This runs all block validation rules. db := rawdb.NewMemoryDatabase() - blockchain, _ := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + blockchain, _ := NewBlockChain(db, ethash.NewFaker(), config) if i, err := blockchain.InsertChain(chain); err != nil { t.Fatalf("failed to insert original chain[%d]: %v", i, err) } @@ -1210,7 +1247,13 @@ func testLogReorgs(t *testing.T, scheme string) { signer = types.LatestSigner(gspec.Config) ) - blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), config) defer blockchain.Stop() rmLogsCh := make(chan RemovedLogsEvent) @@ -1261,12 +1304,17 @@ func TestLogRebirth(t *testing.T) { func testLogRebirth(t *testing.T, scheme string) { var ( - key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") - addr1 = crypto.PubkeyToAddress(key1.PublicKey) - gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000000)}}} - signer = types.LatestSigner(gspec.Config) - engine = ethash.NewFaker() - blockchain, _ = NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + addr1 = crypto.PubkeyToAddress(key1.PublicKey) + gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000000)}}} + signer = types.LatestSigner(gspec.Config) + engine = ethash.NewFaker() + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + blockchain, _ = NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) ) defer blockchain.Stop() @@ -1343,11 +1391,16 @@ func TestSideLogRebirth(t *testing.T) { func testSideLogRebirth(t *testing.T, scheme string) { var ( - key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") - addr1 = crypto.PubkeyToAddress(key1.PublicKey) - gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000000)}}} - signer = types.LatestSigner(gspec.Config) - blockchain, _ = NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + addr1 = crypto.PubkeyToAddress(key1.PublicKey) + gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000000)}}} + signer = types.LatestSigner(gspec.Config) + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + blockchain, _ = NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), config) ) defer blockchain.Stop() @@ -1445,8 +1498,13 @@ func testReorgSideEvent(t *testing.T, scheme string) { Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000000)}}, } signer = types.LatestSigner(gspec.Config) + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) - blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), config) defer blockchain.Stop() _, chain, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), 3, func(i int, gen *BlockGen) {}) @@ -1587,6 +1645,11 @@ func testEIP155Transition(t *testing.T, scheme string) { }, Alloc: GenesisAlloc{address: {Balance: funds}, deleteAddr: {Balance: new(big.Int)}}, } + bcConfig = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) genDb, blocks, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), 4, func(i int, block *BlockGen) { var ( @@ -1630,7 +1693,7 @@ func testEIP155Transition(t *testing.T, scheme string) { } }) - blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), bcConfig) defer blockchain.Stop() if _, err := blockchain.InsertChain(blocks); err != nil { @@ -1702,6 +1765,11 @@ func testEIP161AccountRemoval(t *testing.T, scheme string) { }, Alloc: GenesisAlloc{address: {Balance: funds}}, } + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) _, blocks, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), 3, func(i int, block *BlockGen) { var ( @@ -1723,7 +1791,7 @@ func testEIP161AccountRemoval(t *testing.T, scheme string) { block.AddTx(tx) }) // account must exist pre eip 161 - blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + blockchain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), config) defer blockchain.Stop() if _, err := blockchain.InsertChain(types.Blocks{blocks[0]}); err != nil { @@ -1767,6 +1835,11 @@ func testBlockchainHeaderchainReorgConsistency(t *testing.T, scheme string) { Config: params.TestChainConfig, BaseFee: big.NewInt(params.InitialBaseFee), } + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) genDb, blocks, _ := GenerateChainWithGenesis(genesis, engine, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) // Generate a bunch of fork blocks, each side forking from the canonical chain @@ -1781,7 +1854,7 @@ func testBlockchainHeaderchainReorgConsistency(t *testing.T, scheme string) { } // Import the canonical and fork chain side by side, verifying the current block // and current header consistency - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -1812,6 +1885,10 @@ func TestTrieForkGC(t *testing.T) { Config: params.TestChainConfig, BaseFee: big.NewInt(params.InitialBaseFee), } + config := NewBlockChainConfig( + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) genDb, blocks, _ := GenerateChainWithGenesis(genesis, engine, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) // Generate a bunch of fork blocks, each side forking from the canonical chain @@ -1825,7 +1902,7 @@ func TestTrieForkGC(t *testing.T) { forks[i] = fork[0] } // Import the canonical and fork chain side by side, forcing the trie cache to cache both - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, genesis, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -1863,6 +1940,12 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) { Config: params.TestChainConfig, BaseFee: big.NewInt(params.InitialBaseFee), } + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + genDb, shared, _ := GenerateChainWithGenesis(genesis, engine, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) original, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }) competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*TriesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) }) @@ -1871,7 +1954,7 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) { db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false) defer db.Close() - chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(db, engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -1932,6 +2015,11 @@ func testBlockchainRecovery(t *testing.T, scheme string) { address = crypto.PubkeyToAddress(key.PublicKey) funds = big.NewInt(1000000000) gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{address: {Balance: funds}}} + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) height := uint64(1024) _, blocks, receipts := GenerateChainWithGenesis(gspec, ethash.NewFaker(), int(height), nil) @@ -1942,7 +2030,7 @@ func testBlockchainRecovery(t *testing.T, scheme string) { t.Fatalf("failed to create temp freezer db: %v", err) } defer ancientDb.Close() - ancient, _ := NewBlockChain(ancientDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + ancient, _ := NewBlockChain(ancientDb, ethash.NewFaker(), config) headers := make([]*types.Header, len(blocks)) for i, block := range blocks { @@ -1962,7 +2050,7 @@ func testBlockchainRecovery(t *testing.T, scheme string) { rawdb.WriteHeadFastBlockHash(ancientDb, midBlock.Hash()) // Reopen broken blockchain again - ancient, _ = NewBlockChain(ancientDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + ancient, _ = NewBlockChain(ancientDb, ethash.NewFaker(), config) defer ancient.Stop() if num := ancient.CurrentBlock().Number.Uint64(); num != 0 { t.Errorf("head block mismatch: have #%v, want #%v", num, 0) @@ -2014,7 +2102,12 @@ func testInsertReceiptChainRollback(t *testing.T, scheme string) { } defer ancientDb.Close() - ancientChain, _ := NewBlockChain(ancientDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + ancientChain, _ := NewBlockChain(ancientDb, ethash.NewFaker(), config) defer ancientChain.Stop() // Import the canonical header chain. @@ -2081,7 +2174,13 @@ func testLowDiffLongChain(t *testing.T, scheme string) { diskdb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false) defer diskdb.Close() - chain, err := NewBlockChain(diskdb, DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(diskdb, engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2141,9 +2240,13 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon } signer = types.LatestSigner(gspec.Config) mergeBlock = math.MaxInt32 + config = NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) // Generate and import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2301,7 +2404,13 @@ func testInsertKnownChainData(t *testing.T, typ string, scheme string) { } defer chaindb.Close() - chain, err := NewBlockChain(chaindb, DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(chaindb, engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2472,7 +2581,12 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i } defer chaindb.Close() - chain, err := NewBlockChain(chaindb, nil, genesis, nil, engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(chaindb, engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2586,7 +2700,14 @@ func getLongAndShortChains(scheme string) (*BlockChain, []*types.Block, []*types genDb, longChain, _ := GenerateChainWithGenesis(genesis, engine, 80, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { return nil, nil, nil, nil, fmt.Errorf("failed to create tester chain: %v", err) } @@ -2784,7 +2905,14 @@ func TestTransactionIndices(t *testing.T) { rawdb.WriteAncientBlocks(ancientDb, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), big.NewInt(0)) l := l - chain, err := NewBlockChain(ancientDb, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, &l) + + config := NewBlockChainConfig( + WithGenesis(gspec), + WithTxLookupLimit(&l), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(ancientDb, ethash.NewFaker(), config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2808,7 +2936,14 @@ func TestTransactionIndices(t *testing.T) { limit = []uint64{0, 64 /* drop stale */, 32 /* shorten history */, 64 /* extend history */, 0 /* restore all */} for _, l := range limit { l := l - chain, err := NewBlockChain(ancientDb, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, &l) + + config := NewBlockChainConfig( + WithGenesis(gspec), + WithTxLookupLimit(&l), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(ancientDb, ethash.NewFaker(), config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2886,7 +3021,15 @@ func testSkipStaleTxIndicesInSnapSync(t *testing.T, scheme string) { // Import all blocks into ancient db, only HEAD-32 indices are kept. l := uint64(32) - chain, err := NewBlockChain(ancientDb, DefaultCacheConfigWithScheme(scheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, &l) + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithTxLookupLimit(&l), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(ancientDb, ethash.NewFaker(), config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -2943,11 +3086,16 @@ func benchmarkLargeNumberOfValueToNonexisting(b *testing.B, numTxs, numBlocks in } _, shared, _ := GenerateChainWithGenesis(gspec, engine, numBlocks, blockGenerator) + config := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + b.StopTimer() b.ResetTimer() for i := 0; i < b.N; i++ { // Import the shared chain and the original canonical one - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { b.Fatalf("failed to create tester chain: %v", err) } @@ -3034,7 +3182,13 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) { // Generate and import the canonical chain _, blocks, _ := GenerateChainWithGenesis(genesis, engine, 2*TriesInMemory, nil) - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(genesis), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3120,6 +3274,11 @@ func testDeleteCreateRevert(t *testing.T, scheme string) { }, }, } + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) _, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) { @@ -3134,7 +3293,7 @@ func testDeleteCreateRevert(t *testing.T, scheme string) { b.AddTx(tx) }) // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3246,10 +3405,17 @@ func testDeleteRecreateSlots(t *testing.T, scheme string) { big.NewInt(0), 100000, b.header.BaseFee, nil), types.HomesteadSigner{}, key) b.AddTx(tx) }) + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{ + Tracer: logger.NewJSONLogger(nil, os.Stdout), + }), + ) + // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{ - Tracer: logger.NewJSONLogger(nil, os.Stdout), - }, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3328,10 +3494,17 @@ func testDeleteRecreateAccount(t *testing.T, scheme string) { big.NewInt(1), 100000, b.header.BaseFee, nil), types.HomesteadSigner{}, key) b.AddTx(tx) }) + + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{ + Tracer: logger.NewJSONLogger(nil, os.Stdout), + }), + ) + // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{ - Tracer: logger.NewJSONLogger(nil, os.Stdout), - }, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3504,10 +3677,13 @@ func testDeleteRecreateSlotsAcrossManyBlocks(t *testing.T, scheme string) { current = exp }) // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{ - //Debug: true, - //Tracer: vm.NewJSONLogger(nil, os.Stdout), - }, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3642,10 +3818,13 @@ func testInitThenFailCreateContract(t *testing.T, scheme string) { }) // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{ - //Debug: true, - //Tracer: vm.NewJSONLogger(nil, os.Stdout), - }, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3732,7 +3911,13 @@ func testEIP2718Transition(t *testing.T, scheme string) { }) // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3796,6 +3981,11 @@ func testEIP1559Transition(t *testing.T, scheme string) { }, }, } + bcConfig = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) gspec.Config.BerlinBlock = common.Big0 @@ -3826,7 +4016,7 @@ func testEIP1559Transition(t *testing.T, scheme string) { b.AddTx(tx) }) - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, bcConfig) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -3926,6 +4116,11 @@ func testSetCanonical(t *testing.T, scheme string) { } signer = types.LatestSigner(gspec.Config) engine = ethash.NewFaker() + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) // Generate and import the canonical chain _, canon, _ := GenerateChainWithGenesis(gspec, engine, 2*TriesInMemory, func(i int, gen *BlockGen) { @@ -3938,7 +4133,7 @@ func testSetCanonical(t *testing.T, scheme string) { diskdb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false) defer diskdb.Close() - chain, err := NewBlockChain(diskdb, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(diskdb, engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -4042,12 +4237,17 @@ func testCanonicalHashMarker(t *testing.T, scheme string) { BaseFee: big.NewInt(params.InitialBaseFee), } engine = ethash.NewFaker() + config = NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) ) _, forkA, _ := GenerateChainWithGenesis(gspec, engine, c.forkA, func(i int, gen *BlockGen) {}) _, forkB, _ := GenerateChainWithGenesis(gspec, engine, c.forkB, func(i int, gen *BlockGen) {}) // Initialize test chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -4286,7 +4486,13 @@ func TestTxIndexer(t *testing.T) { rawdb.WriteAncientBlocks(db, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), big.NewInt(0)) // Index the initial blocks from ancient store - chain, _ := NewBlockChain(db, nil, gspec, nil, engine, vm.Config{}, nil, &c.limitA) + config := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + WithTxLookupLimit(&c.limitA), + ) + + chain, _ := NewBlockChain(db, engine, config) chain.indexBlocks(nil, 128, make(chan struct{})) verify(db, c.tailA) @@ -4386,11 +4592,14 @@ func testCreateThenDelete(t *testing.T, config *params.ChainConfig) { b.AddTx(tx) nonce++ }) + + bcConfig := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{ - //Debug: true, - //Tracer: logger.NewJSONLogger(nil, os.Stdout), - }, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, bcConfig) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -4501,8 +4710,14 @@ func TestDeleteThenCreate(t *testing.T) { nonce++ } }) + + config := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + // Import the canonical chain - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -4586,8 +4801,13 @@ func TestTransientStorageReset(t *testing.T) { nonce++ }) + config := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vmConfig), + ) + // Initialize the blockchain with 1153 enabled. - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vmConfig, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, config) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } @@ -4655,6 +4875,10 @@ func TestEIP3651(t *testing.T) { }, }, } + bcConfig = NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{Tracer: logger.NewMarkdownLogger(&logger.Config{}, os.Stderr)}), + ) ) gspec.Config.BerlinBlock = common.Big0 @@ -4682,7 +4906,7 @@ func TestEIP3651(t *testing.T) { b.AddTx(tx) }) - chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{Tracer: logger.NewMarkdownLogger(&logger.Config{}, os.Stderr)}, nil, nil) + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), engine, bcConfig) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } diff --git a/core/chain_makers_test.go b/core/chain_makers_test.go index 84148841f5..f13bea5d74 100644 --- a/core/chain_makers_test.go +++ b/core/chain_makers_test.go @@ -123,8 +123,13 @@ func TestGeneratePOSChain(t *testing.T) { } }) + bcConfig := NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + // Import the chain. This runs all block validation rules. - blockchain, _ := NewBlockChain(db, nil, gspec, nil, beacon.NewFaker(), vm.Config{}, nil, nil) + blockchain, _ := NewBlockChain(db, beacon.NewFaker(), bcConfig) defer blockchain.Stop() if i, err := blockchain.InsertChain(genchain); err != nil { @@ -238,8 +243,14 @@ func ExampleGenerateChain() { } }) + config := NewBlockChainConfig( + WithCacheConfig(DefaultCacheConfigWithScheme(rawdb.HashScheme)), + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + // Import the chain. This runs all block validation rules. - blockchain, _ := NewBlockChain(db, DefaultCacheConfigWithScheme(rawdb.HashScheme), gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + blockchain, _ := NewBlockChain(db, ethash.NewFaker(), config) defer blockchain.Stop() if i, err := blockchain.InsertChain(chain); err != nil { diff --git a/core/dao_test.go b/core/dao_test.go index b9a899ef2f..4493a6923e 100644 --- a/core/dao_test.go +++ b/core/dao_test.go @@ -50,7 +50,13 @@ func TestDAOForkRangeExtradata(t *testing.T) { BaseFee: big.NewInt(params.InitialBaseFee), Config: &proConf, } - proBc, _ := NewBlockChain(proDb, nil, progspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + progspecConfig := NewBlockChainConfig( + WithGenesis(progspec), + WithVmConfig(&vm.Config{}), + ) + + proBc, _ := NewBlockChain(proDb, ethash.NewFaker(), progspecConfig) defer proBc.Stop() conDb := rawdb.NewMemoryDatabase() @@ -62,7 +68,13 @@ func TestDAOForkRangeExtradata(t *testing.T) { BaseFee: big.NewInt(params.InitialBaseFee), Config: &conConf, } - conBc, _ := NewBlockChain(conDb, nil, congspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + congspecConfig := NewBlockChainConfig( + WithGenesis(congspec), + WithVmConfig(&vm.Config{}), + ) + + conBc, _ := NewBlockChain(conDb, ethash.NewFaker(), congspecConfig) defer conBc.Stop() if _, err := proBc.InsertChain(prefix); err != nil { @@ -74,7 +86,7 @@ func TestDAOForkRangeExtradata(t *testing.T) { // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ { // Create a pro-fork block, and try to feed into the no-fork chain - bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, congspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), congspecConfig) blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().Number.Uint64())) for j := 0; j < len(blocks)/2; j++ { @@ -97,7 +109,7 @@ func TestDAOForkRangeExtradata(t *testing.T) { t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err) } // Create a no-fork block, and try to feed into the pro-fork chain - bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), nil, progspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), progspecConfig) blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().Number.Uint64())) for j := 0; j < len(blocks)/2; j++ { @@ -121,7 +133,7 @@ func TestDAOForkRangeExtradata(t *testing.T) { } } // Verify that contra-forkers accept pro-fork extra-datas after forking finishes - bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, congspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), congspecConfig) defer bc.Stop() blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().Number.Uint64())) @@ -139,7 +151,7 @@ func TestDAOForkRangeExtradata(t *testing.T) { t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err) } // Verify that pro-forkers accept contra-fork extra-datas after forking finishes - bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), nil, progspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), progspecConfig) defer bc.Stop() blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().Number.Uint64())) diff --git a/core/genesis_test.go b/core/genesis_test.go index 1d85b510ca..70bec52331 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -131,8 +131,12 @@ func testSetupGenesis(t *testing.T, scheme string) { // Advance to block #4, past the homestead transition block of customg. tdb := trie.NewDatabase(db, newDbConfig(scheme)) oldcustomg.Commit(db, tdb) - - bc, _ := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), &oldcustomg, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil) + config := NewBlockChainConfig( + WithGenesis(&oldcustomg), + WithCacheConfig(DefaultCacheConfigWithScheme(scheme)), + WithVmConfig(&vm.Config{}), + ) + bc, _ := NewBlockChain(db, ethash.NewFullFaker(), config) defer bc.Stop() _, blocks, _ := GenerateChainWithGenesis(&oldcustomg, ethash.NewFaker(), 4, nil) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 5ff9353bd9..26e6297239 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -128,7 +128,11 @@ func TestStateProcessorErrors(t *testing.T) { }, }, } - blockchain, _ = NewBlockChain(db, nil, gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil, nil) + config = NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + blockchain, _ = NewBlockChain(db, beacon.New(ethash.NewFaker()), config) tooBigInitCode = [params.MaxInitCodeSize + 1]byte{} ) @@ -288,7 +292,11 @@ func TestStateProcessorErrors(t *testing.T) { }, }, } - blockchain, _ = NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + config = NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + blockchain, _ = NewBlockChain(db, ethash.NewFaker(), config) ) defer blockchain.Stop() for i, tt := range []struct { @@ -327,7 +335,11 @@ func TestStateProcessorErrors(t *testing.T) { }, }, } - blockchain, _ = NewBlockChain(db, nil, gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil, nil) + config = NewBlockChainConfig( + WithGenesis(gspec), + WithVmConfig(&vm.Config{}), + ) + blockchain, _ = NewBlockChain(db, beacon.New(ethash.NewFaker()), config) ) defer blockchain.Stop() for i, tt := range []struct { diff --git a/eth/backend.go b/eth/backend.go index 774ffaf248..131c797563 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -213,7 +213,17 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.OverrideVerkle != nil { overrides.OverrideVerkle = config.OverrideVerkle } - eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TransactionHistory) + + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(cacheConfig), + core.WithGenesis(config.Genesis), + core.WithOverrides(&overrides), + core.WithShouldPreserve(eth.shouldPreserve), + core.WithTxLookupLimit(&config.TransactionHistory), + core.WithVmConfig(&vmConfig), + ) + + eth.blockchain, err = core.NewBlockChain(chainDb, eth.engine, bcConfig) if err != nil { return nil, err } diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index e4875b959a..f8a3eb1e33 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -72,7 +72,13 @@ func newTesterWithNotification(t *testing.T, success func()) *downloadTester { Alloc: core.GenesisAlloc{testAddress: {Balance: big.NewInt(1000000000000000)}}, BaseFee: big.NewInt(params.InitialBaseFee), } - chain, err := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + config := core.NewBlockChainConfig( + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + + chain, err := core.NewBlockChain(db, ethash.NewFaker(), config) if err != nil { panic(err) } diff --git a/eth/downloader/testchain_test.go b/eth/downloader/testchain_test.go index 1bf03411d1..99e8f8c5cf 100644 --- a/eth/downloader/testchain_test.go +++ b/eth/downloader/testchain_test.go @@ -218,7 +218,13 @@ func newTestBlockchain(blocks []*types.Block) *core.BlockChain { if pregenerated { panic("Requested chain generation outside of init") } - chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, testGspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + bcConfig := core.NewBlockChainConfig( + core.WithGenesis(testGspec), + core.WithVmConfig(&vm.Config{}), + ) + + chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), bcConfig) if err != nil { panic(err) } diff --git a/eth/filters/filter_test.go b/eth/filters/filter_test.go index 1db917c960..c548694959 100644 --- a/eth/filters/filter_test.go +++ b/eth/filters/filter_test.go @@ -249,7 +249,14 @@ func TestFilters(t *testing.T) { } }) var l uint64 - bc, err := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, &l) + + config := core.NewBlockChainConfig( + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + core.WithTxLookupLimit(&l), + ) + + bc, err := core.NewBlockChain(db, ethash.NewFaker(), config) if err != nil { t.Fatal(err) } diff --git a/eth/gasprice/gasprice_test.go b/eth/gasprice/gasprice_test.go index 4ee5a0d1b2..585d960ffa 100644 --- a/eth/gasprice/gasprice_test.go +++ b/eth/gasprice/gasprice_test.go @@ -163,8 +163,15 @@ func newTestBackend(t *testing.T, londonBlock *big.Int, pending bool) *testBacke } b.AddTx(types.MustSignNewTx(key, signer, txdata)) }) + + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(&core.CacheConfig{TrieCleanNoPrefetch: true}), + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + // Construct testing chain - chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), &core.CacheConfig{TrieCleanNoPrefetch: true}, gspec, nil, engine, vm.Config{}, nil, nil) + chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), engine, bcConfig) if err != nil { t.Fatalf("Failed to create local chain, %v", err) } diff --git a/eth/handler_eth_test.go b/eth/handler_eth_test.go index bb342acc18..20e88dcf7e 100644 --- a/eth/handler_eth_test.go +++ b/eth/handler_eth_test.go @@ -104,8 +104,17 @@ func testForkIDSplit(t *testing.T, protocol uint) { gspecNoFork = &core.Genesis{Config: configNoFork} gspecProFork = &core.Genesis{Config: configProFork} - chainNoFork, _ = core.NewBlockChain(dbNoFork, nil, gspecNoFork, nil, engine, vm.Config{}, nil, nil) - chainProFork, _ = core.NewBlockChain(dbProFork, nil, gspecProFork, nil, engine, vm.Config{}, nil, nil) + noForkConfig = core.NewBlockChainConfig( + core.WithGenesis(gspecNoFork), + core.WithVmConfig(&vm.Config{}), + ) + chainNoFork, _ = core.NewBlockChain(dbNoFork, engine, noForkConfig) + + proForkConfig = core.NewBlockChainConfig( + core.WithGenesis(gspecProFork), + core.WithVmConfig(&vm.Config{}), + ) + chainProFork, _ = core.NewBlockChain(dbProFork, engine, proForkConfig) _, blocksNoFork, _ = core.GenerateChainWithGenesis(gspecNoFork, engine, 2, nil) _, blocksProFork, _ = core.GenerateChainWithGenesis(gspecProFork, engine, 2, nil) diff --git a/eth/handler_test.go b/eth/handler_test.go index 6d6132ee4c..a82f88e9b4 100644 --- a/eth/handler_test.go +++ b/eth/handler_test.go @@ -151,7 +151,13 @@ func newTestHandlerWithBlocks(blocks int) *testHandler { Config: params.TestChainConfig, Alloc: core.GenesisAlloc{testAddr: {Balance: big.NewInt(1000000)}}, } - chain, _ := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + config := core.NewBlockChainConfig( + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + + chain, _ := core.NewBlockChain(db, ethash.NewFaker(), config) _, bs, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), blocks, nil) if _, err := chain.InsertChain(bs); err != nil { diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go index 41e18bfb3e..0c3586fa7b 100644 --- a/eth/protocols/eth/handler_test.go +++ b/eth/protocols/eth/handler_test.go @@ -104,7 +104,12 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int, Config: config, Alloc: core.GenesisAlloc{testAddr: {Balance: big.NewInt(100_000_000_000_000_000)}}, } - chain, _ := core.NewBlockChain(db, nil, gspec, nil, engine, vm.Config{}, nil, nil) + + bcConfig := core.NewBlockChainConfig( + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + chain, _ := core.NewBlockChain(db, engine, bcConfig) _, bs, _ := core.GenerateChainWithGenesis(gspec, engine, blocks, generator) if _, err := chain.InsertChain(bs); err != nil { diff --git a/eth/protocols/snap/handler_fuzzing_test.go b/eth/protocols/snap/handler_fuzzing_test.go index daed7ed44a..66f5b6a2a0 100644 --- a/eth/protocols/snap/handler_fuzzing_test.go +++ b/eth/protocols/snap/handler_fuzzing_test.go @@ -125,7 +125,13 @@ func getChain() *core.BlockChain { SnapshotWait: true, } trieRoot = blocks[len(blocks)-1].Root() - bc, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), cacheConf, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) + + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(cacheConf), + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + bc, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), ethash.NewFaker(), bcConfig) if _, err := bc.InsertChain(blocks); err != nil { panic(err) } diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 49c3ebb67d..012c3bf257 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -80,7 +80,14 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i i SnapshotLimit: 0, TrieDirtyDisabled: true, // Archive mode } - chain, err := core.NewBlockChain(backend.chaindb, cacheConfig, gspec, nil, backend.engine, vm.Config{}, nil, nil) + + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(cacheConfig), + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + + chain, err := core.NewBlockChain(backend.chaindb, backend.engine, bcConfig) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index c2490ac703..142911d18c 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -422,7 +422,15 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, engine consensus.E // Generate blocks for testing db, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, n, generator) txlookupLimit := uint64(0) - chain, err := core.NewBlockChain(db, cacheConfig, gspec, nil, engine, vm.Config{}, nil, &txlookupLimit) + + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(cacheConfig), + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + core.WithTxLookupLimit(&txlookupLimit), + ) + + chain, err := core.NewBlockChain(db, engine, bcConfig) if err != nil { t.Fatalf("failed to create tester chain: %v", err) } diff --git a/miner/miner_test.go b/miner/miner_test.go index 411d6026ce..efc0b01272 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -309,7 +309,12 @@ func createMiner(t *testing.T) (*Miner, *event.TypeMux, func(skipMiner bool)) { // Create consensus engine engine := clique.New(chainConfig.Clique, chainDB) // Create Ethereum backend - bc, err := core.NewBlockChain(chainDB, nil, genesis, nil, engine, vm.Config{}, nil, nil) + bcConfig := core.NewBlockChainConfig( + core.WithGenesis(genesis), + core.WithVmConfig(&vm.Config{}), + ) + + bc, err := core.NewBlockChain(chainDB, engine, bcConfig) if err != nil { t.Fatalf("can't create new chain %v", err) } diff --git a/miner/worker_test.go b/miner/worker_test.go index 59fbbbcdca..e282f06984 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -129,7 +129,14 @@ func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine default: t.Fatalf("unexpected consensus engine type: %T", engine) } - chain, err := core.NewBlockChain(db, &core.CacheConfig{TrieDirtyDisabled: true}, gspec, nil, engine, vm.Config{}, nil, nil) + + config := core.NewBlockChainConfig( + core.WithCacheConfig(&core.CacheConfig{TrieDirtyDisabled: true}), + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{}), + ) + + chain, err := core.NewBlockChain(db, engine, config) if err != nil { t.Fatalf("core.NewBlockChain failed: %v", err) } @@ -179,7 +186,12 @@ func TestGenerateAndImportBlock(t *testing.T) { defer w.close() // This test chain imports the mined blocks. - chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, b.genesis, nil, engine, vm.Config{}, nil, nil) + bcConfig := core.NewBlockChainConfig( + core.WithGenesis(b.genesis), + core.WithVmConfig(&vm.Config{}), + ) + + chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), engine, bcConfig) defer chain.Stop() // Ignore empty commit here for less noise. diff --git a/tests/block_test_util.go b/tests/block_test_util.go index e0130be48a..413c6a7417 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -148,9 +148,14 @@ func (t *BlockTest) Run(snapshotter bool, scheme string, tracer vm.EVMLogger, po cache.SnapshotLimit = 1 cache.SnapshotWait = true } - chain, err := core.NewBlockChain(db, cache, gspec, nil, engine, vm.Config{ - Tracer: tracer, - }, nil, nil) + + bcConfig := core.NewBlockChainConfig( + core.WithCacheConfig(cache), + core.WithGenesis(gspec), + core.WithVmConfig(&vm.Config{Tracer: tracer}), + ) + + chain, err := core.NewBlockChain(db, engine, bcConfig) if err != nil { return err }