From 9d01ed67f96ee27caaeb7f7cd33219915fcb1e59 Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Mon, 24 Jul 2023 20:27:05 +0300 Subject: [PATCH 1/7] move genesis verify to vm --- core/genesis.go | 32 +++++++++++++++++--------------- plugin/evm/vm.go | 4 ++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index 65b9aa8aad..e345953c69 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -183,21 +183,6 @@ func SetupGenesisBlock( if genesis.Config == nil { return nil, common.Hash{}, errGenesisNoConfig } - // Make sure genesis gas limit is consistent in SubnetEVM fork - if genesis.Config.IsSubnetEVM(genesis.Timestamp) { - gasLimitConfig := genesis.Config.FeeConfig.GasLimit.Uint64() - if gasLimitConfig != genesis.GasLimit { - return nil, common.Hash{}, fmt.Errorf( - "gas limit in fee config (%d) does not match gas limit in header (%d)", - gasLimitConfig, - genesis.GasLimit, - ) - } - // Verify config - if err := genesis.Config.Verify(); err != nil { - return nil, common.Hash{}, err - } - } // Just commit the new block if there is no stored genesis block. stored := rawdb.ReadCanonicalHash(db, 0) @@ -396,6 +381,23 @@ func (g *Genesis) MustCommit(db ethdb.Database) *types.Block { return block } +func (g *Genesis) Verify() error { + // Make sure genesis gas limit is consistent in SubnetEVM fork + gasLimitConfig := g.Config.FeeConfig.GasLimit.Uint64() + if gasLimitConfig != g.GasLimit { + return fmt.Errorf( + "gas limit in fee config (%d) does not match gas limit in header (%d)", + gasLimitConfig, + g.GasLimit, + ) + } + // Verify config + if err := g.Config.Verify(); err != nil { + return err + } + return nil +} + // GenesisBlockForTesting creates and writes a block in which addr has the given wei balance. func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block { g := Genesis{ diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index b55907f594..f5afc1f13f 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -314,6 +314,10 @@ func (vm *VM) Initialize( g.Config.FeeConfig = params.DefaultFeeConfig } + if err := g.Verify(); err != nil { + return fmt.Errorf("failed to verify genesis: %w", err) + } + vm.ethConfig = ethconfig.NewDefaultConfig() vm.ethConfig.Genesis = g // NetworkID here is different than Avalanche's NetworkID. From 4f2265bc73e3fb4ed04ce8af528cacfe606a8be9 Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Mon, 24 Jul 2023 20:28:53 +0300 Subject: [PATCH 2/7] fix comment --- core/genesis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/genesis.go b/core/genesis.go index e345953c69..db52a157d7 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -382,7 +382,7 @@ func (g *Genesis) MustCommit(db ethdb.Database) *types.Block { } func (g *Genesis) Verify() error { - // Make sure genesis gas limit is consistent in SubnetEVM fork + // Make sure genesis gas limit is consistent gasLimitConfig := g.Config.FeeConfig.GasLimit.Uint64() if gasLimitConfig != g.GasLimit { return fmt.Errorf( From 62721ab5d3d7e0716fe574e69be54d070946a546 Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Mon, 24 Jul 2023 22:04:24 +0300 Subject: [PATCH 3/7] set a correct default from genesis --- plugin/evm/vm.go | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index f5afc1f13f..28acc7088e 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -312,6 +312,7 @@ func (vm *VM) Initialize( if g.Config.FeeConfig == commontype.EmptyFeeConfig { log.Info("No fee config given in genesis, setting default fee config", "DefaultFeeConfig", params.DefaultFeeConfig) g.Config.FeeConfig = params.DefaultFeeConfig + g.Config.FeeConfig.GasLimit = new(big.Int).SetUint64(g.GasLimit) } if err := g.Verify(); err != nil { From 74f81db03c9b0dd4741c6369f9b6ce3b3a9adb22 Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Mon, 24 Jul 2023 22:33:25 +0300 Subject: [PATCH 4/7] fix log --- plugin/evm/vm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 28acc7088e..a66b5a8ed6 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -310,9 +310,9 @@ func (vm *VM) Initialize( vm.syntacticBlockValidator = NewBlockValidator() if g.Config.FeeConfig == commontype.EmptyFeeConfig { - log.Info("No fee config given in genesis, setting default fee config", "DefaultFeeConfig", params.DefaultFeeConfig) g.Config.FeeConfig = params.DefaultFeeConfig g.Config.FeeConfig.GasLimit = new(big.Int).SetUint64(g.GasLimit) + log.Info("No fee config given in genesis, setting default fee config", "DefaultConfig", g.Config.FeeConfig) } if err := g.Verify(); err != nil { From db90a22c5fbd93dbd02c542b883a4a16bb3cbbfa Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Mon, 24 Jul 2023 15:54:31 -0700 Subject: [PATCH 5/7] revert change to vm.go --- plugin/evm/vm.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index a66b5a8ed6..f5afc1f13f 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -310,9 +310,8 @@ func (vm *VM) Initialize( vm.syntacticBlockValidator = NewBlockValidator() if g.Config.FeeConfig == commontype.EmptyFeeConfig { + log.Info("No fee config given in genesis, setting default fee config", "DefaultFeeConfig", params.DefaultFeeConfig) g.Config.FeeConfig = params.DefaultFeeConfig - g.Config.FeeConfig.GasLimit = new(big.Int).SetUint64(g.GasLimit) - log.Info("No fee config given in genesis, setting default fee config", "DefaultConfig", g.Config.FeeConfig) } if err := g.Verify(); err != nil { From f3579f981901a832be014d03710b068001d2e8e2 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Mon, 24 Jul 2023 17:07:36 -0700 Subject: [PATCH 6/7] avoid adding/removing GasLimits in tests --- accounts/abi/bind/backends/simulated.go | 1 - core/bench_test.go | 5 ++--- core/blockchain_log_test.go | 7 +++---- core/blockchain_repair_test.go | 5 ++--- core/blockchain_snapshot_test.go | 5 ++--- core/blockchain_test.go | 20 +++++++------------- core/headerchain_test.go | 5 ++--- core/state_processor_test.go | 1 + core/test_blockchain.go | 20 ++++++++------------ eth/gasprice/gasprice_test.go | 10 ++++------ eth/tracers/api_test.go | 5 ----- 11 files changed, 31 insertions(+), 53 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 2ab5b86e2b..34de771fb8 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -109,7 +109,6 @@ type SimulatedBackend struct { func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { copyConfig := *params.TestChainConfig copyConfig.ChainID = big.NewInt(1337) - copyConfig.FeeConfig.GasLimit = big.NewInt(int64(gasLimit)) genesis := core.Genesis{ Config: ©Config, GasLimit: gasLimit, diff --git a/core/bench_test.go b/core/bench_test.go index f4d73ed29c..4f3cc2e97f 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -166,9 +166,8 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) { // Generate a chain of b.N blocks using the supplied block // generator function. gspec := &Genesis{ - Config: params.TestChainConfig, - Alloc: GenesisAlloc{benchRootAddr: {Balance: benchRootFunds}}, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + Config: params.TestChainConfig, + Alloc: GenesisAlloc{benchRootAddr: {Balance: benchRootFunds}}, } _, chain, _, _ := GenerateChainWithGenesis(gspec, dummy.NewCoinbaseFaker(), b.N, 10, gen) diff --git a/core/blockchain_log_test.go b/core/blockchain_log_test.go index d915fdfc47..d5bfe2a19c 100644 --- a/core/blockchain_log_test.go +++ b/core/blockchain_log_test.go @@ -40,10 +40,9 @@ func TestEmitLogsCorrectness(t *testing.T) { addr1 = crypto.PubkeyToAddress(key1.PublicKey) funds = new(big.Int).Mul(big.NewInt(100), big.NewInt(params.Ether)) gspec = &Genesis{ - Config: params.TestChainConfig, - Alloc: GenesisAlloc{addr1: {Balance: funds}}, - BaseFee: big.NewInt(params.TestInitialBaseFee), - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + Config: params.TestChainConfig, + Alloc: GenesisAlloc{addr1: {Balance: funds}}, + BaseFee: big.NewInt(params.TestInitialBaseFee), } contractAddress = crypto.CreateAddress(addr1, 0) signer = types.LatestSigner(gspec.Config) diff --git a/core/blockchain_repair_test.go b/core/blockchain_repair_test.go index 7f7c3052cc..c1b72856db 100644 --- a/core/blockchain_repair_test.go +++ b/core/blockchain_repair_test.go @@ -519,9 +519,8 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) { // Initialize a fresh chain var ( gspec = &Genesis{ - BaseFee: big.NewInt(params.TestInitialBaseFee), - Config: params.TestChainConfig, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + BaseFee: big.NewInt(params.TestInitialBaseFee), + Config: params.TestChainConfig, } engine = dummy.NewFullFaker() config = &CacheConfig{ diff --git a/core/blockchain_snapshot_test.go b/core/blockchain_snapshot_test.go index 6ca5a5610d..b4deac33a2 100644 --- a/core/blockchain_snapshot_test.go +++ b/core/blockchain_snapshot_test.go @@ -79,9 +79,8 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo // Initialize a fresh chain var ( gspec = &Genesis{ - BaseFee: big.NewInt(params.TestInitialBaseFee), - Config: params.TestChainConfig, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + BaseFee: big.NewInt(params.TestInitialBaseFee), + Config: params.TestChainConfig, } engine = dummy.NewFullFaker() diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 2f2d1391c8..3937501451 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -403,9 +403,8 @@ func testRepopulateMissingTriesParallel(t *testing.T, parallelism int) { // Ensure that key1 has some funds in the genesis block. genesisBalance := big.NewInt(1000000) gspec := &Genesis{ - Config: ¶ms.ChainConfig{HomesteadBlock: new(big.Int), FeeConfig: params.DefaultFeeConfig}, - Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, - GasLimit: params.DefaultFeeConfig.GasLimit.Uint64(), + Config: ¶ms.ChainConfig{HomesteadBlock: new(big.Int), FeeConfig: params.DefaultFeeConfig}, + Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, } blockchain, err := createBlockChain(chainDB, pruningConfig, gspec, common.Hash{}) @@ -514,9 +513,8 @@ func TestUngracefulAsyncShutdown(t *testing.T) { // Ensure that key1 has some funds in the genesis block. genesisBalance := big.NewInt(1000000) gspec := &Genesis{ - Config: ¶ms.ChainConfig{HomesteadBlock: new(big.Int), FeeConfig: params.DefaultFeeConfig}, - Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, - GasLimit: params.DefaultFeeConfig.GasLimit.Uint64(), + Config: ¶ms.ChainConfig{HomesteadBlock: new(big.Int), FeeConfig: params.DefaultFeeConfig}, + Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, } blockchain, err := create(chainDB, gspec, common.Hash{}) @@ -780,10 +778,9 @@ func TestCanonicalHashMarker(t *testing.T) { for _, c := range cases { var ( gspec = &Genesis{ - Config: params.TestChainConfig, - Alloc: GenesisAlloc{}, - BaseFee: big.NewInt(params.TestInitialBaseFee), - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + Config: params.TestChainConfig, + Alloc: GenesisAlloc{}, + BaseFee: big.NewInt(params.TestInitialBaseFee), } engine = dummy.NewCoinbaseFaker() ) @@ -924,7 +921,6 @@ func testCreateThenDelete(t *testing.T, config *params.ChainConfig) { Alloc: GenesisAlloc{ address: {Balance: funds}, }, - GasLimit: config.FeeConfig.GasLimit.Uint64(), } nonce := uint64(0) signer := types.HomesteadSigner{} @@ -1012,7 +1008,6 @@ func TestTransientStorageReset(t *testing.T) { Alloc: GenesisAlloc{ address: {Balance: funds}, }, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), } nonce := uint64(0) signer := types.HomesteadSigner{} @@ -1107,7 +1102,6 @@ func TestEIP3651(t *testing.T) { Balance: big.NewInt(0), }, }, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), } ) diff --git a/core/headerchain_test.go b/core/headerchain_test.go index 72721c3f79..8cdcf18447 100644 --- a/core/headerchain_test.go +++ b/core/headerchain_test.go @@ -75,9 +75,8 @@ func TestHeaderInsertion(t *testing.T) { var ( db = rawdb.NewMemoryDatabase() gspec = &Genesis{ - BaseFee: big.NewInt(params.TestInitialBaseFee), - Config: params.TestChainConfig, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + BaseFee: big.NewInt(params.TestInitialBaseFee), + Config: params.TestChainConfig, } ) genesis := gspec.ToBlock() diff --git a/core/state_processor_test.go b/core/state_processor_test.go index a206be7315..9eca088b42 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -235,6 +235,7 @@ func TestStateProcessorErrors(t *testing.T) { Nonce: 0, }, }, + GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), } blockchain, _ = NewBlockChain(db, DefaultCacheConfig, gspec, dummy.NewCoinbaseFaker(), vm.Config{}, common.Hash{}, false) ) diff --git a/core/test_blockchain.go b/core/test_blockchain.go index 69ac86b579..9f3c7cd5d8 100644 --- a/core/test_blockchain.go +++ b/core/test_blockchain.go @@ -1291,9 +1291,8 @@ func TestGenerateChainInvalidBlockFee(t *testing.T, create func(db ethdb.Databas // Ensure that key1 has some funds in the genesis block. genesisBalance := new(big.Int).Mul(big.NewInt(1000000), big.NewInt(params.Ether)) gspec := &Genesis{ - Config: params.TestChainConfig, - Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + Config: params.TestChainConfig, + Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, } blockchain, err := create(chainDB, gspec, common.Hash{}) @@ -1341,9 +1340,8 @@ func TestInsertChainInvalidBlockFee(t *testing.T, create func(db ethdb.Database, // Ensure that key1 has some funds in the genesis block. genesisBalance := new(big.Int).Mul(big.NewInt(1000000), big.NewInt(params.Ether)) gspec := &Genesis{ - Config: params.TestChainConfig, - Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + Config: params.TestChainConfig, + Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, } blockchain, err := create(chainDB, gspec, common.Hash{}) @@ -1398,9 +1396,8 @@ func TestInsertChainValidBlockFee(t *testing.T, create func(db ethdb.Database, g // Ensure that key1 has some funds in the genesis block. genesisBalance := new(big.Int).Mul(big.NewInt(1000000), big.NewInt(params.Ether)) gspec := &Genesis{ - Config: params.TestChainConfig, - Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), + Config: params.TestChainConfig, + Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, } blockchain, err := create(chainDB, gspec, common.Hash{}) @@ -1495,9 +1492,8 @@ func TestStatefulPrecompiles(t *testing.T, create func(db ethdb.Database, gspec feemanager.ConfigKey: feemanager.NewConfig(utils.NewUint64(0), []common.Address{addr1}, nil, nil), } gspec := &Genesis{ - Config: &config, - Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, - GasLimit: config.FeeConfig.GasLimit.Uint64(), + Config: &config, + Alloc: GenesisAlloc{addr1: {Balance: genesisBalance}}, } blockchain, err := create(chainDB, gspec, common.Hash{}) diff --git a/eth/gasprice/gasprice_test.go b/eth/gasprice/gasprice_test.go index e66de42906..cd5279a76b 100644 --- a/eth/gasprice/gasprice_test.go +++ b/eth/gasprice/gasprice_test.go @@ -100,9 +100,8 @@ func (b *testBackend) teardown() { func newTestBackendFakerEngine(t *testing.T, config *params.ChainConfig, numBlocks int, genBlocks func(i int, b *core.BlockGen)) *testBackend { var gspec = &core.Genesis{ - Config: config, - Alloc: core.GenesisAlloc{addr: core.GenesisAccount{Balance: bal}}, - GasLimit: config.FeeConfig.GasLimit.Uint64(), + Config: config, + Alloc: core.GenesisAlloc{addr: core.GenesisAccount{Balance: bal}}, } engine := dummy.NewETHFaker() @@ -128,9 +127,8 @@ func newTestBackendFakerEngine(t *testing.T, config *params.ChainConfig, numBloc // after use, otherwise the blockchain instance will mem-leak via goroutines. func newTestBackend(t *testing.T, config *params.ChainConfig, numBlocks int, genBlocks func(i int, b *core.BlockGen)) *testBackend { var gspec = &core.Genesis{ - Config: config, - Alloc: core.GenesisAlloc{addr: core.GenesisAccount{Balance: bal}}, - GasLimit: config.FeeConfig.GasLimit.Uint64(), + Config: config, + Alloc: core.GenesisAlloc{addr: core.GenesisAccount{Balance: bal}}, } engine := dummy.NewFaker() diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 2b99cacfc0..856e9236f1 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -217,7 +217,6 @@ func TestTraceCall(t *testing.T) { accounts[1].addr: {Balance: big.NewInt(params.Ether)}, accounts[2].addr: {Balance: big.NewInt(params.Ether)}, }, - GasLimit: params.TestSubnetEVMConfig.FeeConfig.GasLimit.Uint64(), } genBlocks := 10 signer := types.HomesteadSigner{} @@ -352,7 +351,6 @@ func TestTraceTransaction(t *testing.T) { accounts[0].addr: {Balance: big.NewInt(params.Ether)}, accounts[1].addr: {Balance: big.NewInt(params.Ether)}, }, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), } target := common.Hash{} signer := types.HomesteadSigner{} @@ -403,7 +401,6 @@ func TestTraceBlock(t *testing.T) { accounts[1].addr: {Balance: big.NewInt(params.Ether)}, accounts[2].addr: {Balance: big.NewInt(params.Ether)}, }, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), } genBlocks := 10 signer := types.HomesteadSigner{} @@ -493,7 +490,6 @@ func TestTracingWithOverrides(t *testing.T) { }, }, }, - GasLimit: params.TestSubnetEVMConfig.FeeConfig.GasLimit.Uint64(), } genBlocks := 10 signer := types.HomesteadSigner{} @@ -855,7 +851,6 @@ func TestTraceChain(t *testing.T) { accounts[1].addr: {Balance: big.NewInt(5 * params.Ether)}, accounts[2].addr: {Balance: big.NewInt(5 * params.Ether)}, }, - GasLimit: params.TestChainConfig.FeeConfig.GasLimit.Uint64(), } genBlocks := 50 signer := types.HomesteadSigner{} From a3da2b5cf2af1f09f3ee5771cbe1c2c0a42b88df Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Mon, 24 Jul 2023 17:16:02 -0700 Subject: [PATCH 7/7] nit --- core/state_processor_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 9eca088b42..48a17618ee 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -218,6 +218,7 @@ func TestStateProcessorErrors(t *testing.T) { gspec = &Genesis{ Config: ¶ms.ChainConfig{ ChainID: big.NewInt(1), + FeeConfig: params.DefaultFeeConfig, HomesteadBlock: big.NewInt(0), EIP150Block: big.NewInt(0), EIP150Hash: common.Hash{},