From a1f8141d58187fa51ec45b7d867f28466fbe704e Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Thu, 21 Sep 2023 14:17:35 -0500 Subject: [PATCH 01/10] Make contract size limit configurable --- core/state_transition.go | 4 ++-- core/txpool/txpool.go | 4 ++-- core/vm/evm.go | 2 +- core/vm/gas_table.go | 8 ++++---- params/config.go | 22 ++++++++++++++++++++++ params/config_test.go | 31 +++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index a70ce4eb06..5dd8de1542 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -408,8 +408,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { } // Check whether the init code size has been exceeded. - if rules.IsShanghai && contractCreation && len(msg.Data) > params.MaxInitCodeSize { - return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize) + if rules.IsShanghai && contractCreation && len(msg.Data) > int(*st.evm.ChainConfig().MaxInitCodeSize) { + return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(*st.evm.ChainConfig().MaxInitCodeSize)) } // Execute the preparatory steps for state transition which includes: diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index 745568d437..a7f6d43518 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -615,8 +615,8 @@ func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error { return ErrOversizedData } // Check whether the init code size has been exceeded. - if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize { - return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize) + if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(*pool.chainconfig.MaxInitCodeSize) { + return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(*pool.chainconfig.MaxInitCodeSize)) } // Transactions can't be negative. This may never happen using RLP decoded // transactions but may occur if you create a transaction using the RPC. diff --git a/core/vm/evm.go b/core/vm/evm.go index b604d3389c..dbea4747b1 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -504,7 +504,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, ret, err := evm.interpreter.Run(contract, nil, false) // Check whether the max code size has been exceeded, assign err if the case. - if err == nil && evm.chainRules.IsEIP158 && len(ret) > params.MaxCodeSize { + if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(*evm.chainConfig.MaxCodeSize) { err = ErrMaxCodeSizeExceeded } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 4f961ef4db..7409d2c6cc 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -308,10 +308,10 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > params.MaxInitCodeSize { + if overflow || size > *evm.chainConfig.MaxInitCodeSize { return 0, ErrGasUintOverflow } - // Since size <= params.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= *evm.chainConfig.MaxInitCodeSize, these multiplication cannot overflow moreGas := params.InitCodeWordGas * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow @@ -324,10 +324,10 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > params.MaxInitCodeSize { + if overflow || size > *evm.chainConfig.MaxInitCodeSize { return 0, ErrGasUintOverflow } - // Since size <= params.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= *evm.chainConfig.MaxInitCodeSize, these multiplication cannot overflow moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow diff --git a/params/config.go b/params/config.go index a5c518aa8d..527a2fb925 100644 --- a/params/config.go +++ b/params/config.go @@ -17,6 +17,7 @@ package params import ( + "encoding/json" "fmt" "math/big" @@ -307,6 +308,27 @@ type ChainConfig struct { Clique *CliqueConfig `json:"clique,omitempty"` ArbitrumChainParams ArbitrumChainParams `json:"arbitrum,omitempty"` + + MaxCodeSize *uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract + MaxInitCodeSize *uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions +} + +// UnmarshalJSON implements the json.Unmarshaler interface by decoding the json +// string values into the config fields +func (c *ChainConfig) UnmarshalJSON(data []byte) error { + type chainConfigJSON ChainConfig + var cfgJSON chainConfigJSON + if err := json.Unmarshal(data, &cfgJSON); err != nil { + return err + } + if cfgJSON.MaxCodeSize == nil { + cfgJSON.MaxCodeSize = newUint64(MaxCodeSize) + } + if cfgJSON.MaxInitCodeSize == nil { + cfgJSON.MaxInitCodeSize = newUint64(MaxInitCodeSize) + } + *c = ChainConfig(cfgJSON) + return nil } // EthashConfig is the consensus engine configs for proof-of-work based sealing. diff --git a/params/config_test.go b/params/config_test.go index 1d03d96739..f2ca219cf2 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -17,6 +17,7 @@ package params import ( + "encoding/json" "math/big" "reflect" "testing" @@ -140,3 +141,33 @@ func TestConfigRules(t *testing.T) { t.Errorf("expected %v to be shanghai", currentArbosVersion) } } + +type marshalUnMarshalTest struct { + input interface{} + want interface{} +} + +var unmarshalChainConfigTests = []marshalUnMarshalTest{ + {input: `{"maxCodeSize": 10, "maxInitCodeSize": 10}`, + want: [2]uint64{10, 10}}, + {input: `{"maxCodeSize": 10}`, + want: [2]uint64{10, MaxInitCodeSize}}, + {input: `{"maxInitCodeSize": 10}`, + want: [2]uint64{MaxCodeSize, 10}}, + {input: `{}`, + want: [2]uint64{MaxCodeSize, MaxInitCodeSize}}, +} + +func TestUnmarshalChainConfig(t *testing.T) { + var c ChainConfig + for _, test := range unmarshalChainConfigTests { + if err := json.Unmarshal([]byte(test.input.(string)), &c); err != nil { + t.Errorf("failed to unmarshal. Error: %q", err) + } + expected := test.want.([2]uint64) + if *c.MaxCodeSize != expected[0] || *c.MaxInitCodeSize != expected[1] { + t.Errorf("failed to unmarshal MaxCodeSize and MaxInitCodeSize correctly. unmarshalled as (%d %d) want (%d %d))", + *c.MaxCodeSize, *c.MaxInitCodeSize, expected[0], expected[1]) + } + } +} From fe56a0fda32f0da5acfda5d3c3d1ca90939d61bd Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Thu, 21 Sep 2023 15:57:15 -0500 Subject: [PATCH 02/10] fix current failing tests --- core/genesis_test.go | 6 ++++-- core/state_processor_test.go | 6 ++++++ core/vm/runtime/runtime_test.go | 8 +++++--- params/config.go | 6 ++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index e069eb448c..2223bfa0d6 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -41,18 +41,20 @@ func TestInvalidCliqueConfig(t *testing.T) { } } +func newUint64(val uint64) *uint64 { return &val } + func TestSetupGenesis(t *testing.T) { var ( customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") customg = Genesis{ - Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, + Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), MaxCodeSize: newUint64(params.MaxCodeSize), MaxInitCodeSize: newUint64(params.MaxInitCodeSize)}, Alloc: GenesisAlloc{ {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, }, } oldcustomg = customg ) - oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} + oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2), MaxCodeSize: newUint64(params.MaxCodeSize), MaxInitCodeSize: newUint64(params.MaxInitCodeSize)} tests := []struct { name string fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 53675c93e7..ab3ca5d9d9 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -58,6 +58,8 @@ func TestStateProcessorErrors(t *testing.T) { BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), Ethash: new(params.EthashConfig), + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize), } signer = types.LatestSigner(config) key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") @@ -237,6 +239,8 @@ func TestStateProcessorErrors(t *testing.T) { PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize), }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ @@ -333,6 +337,8 @@ func TestStateProcessorErrors(t *testing.T) { TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, ShanghaiTime: u64(0), + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize), }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index 7b521a2ead..b22cfce7a0 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -831,8 +831,9 @@ func TestRuntimeJSTracer(t *testing.T) { t.Fatal(err) } _, _, err = Call(main, nil, &Config{ - GasLimit: 1000000, - State: statedb, + ChainConfig: params.TestChainConfig, + GasLimit: 1000000, + State: statedb, EVMConfig: vm.Config{ Tracer: tracer, }}) @@ -866,7 +867,8 @@ func TestJSTracerCreateTx(t *testing.T) { t.Fatal(err) } _, _, _, err = Create(code, &Config{ - State: statedb, + ChainConfig: params.TestChainConfig, + State: statedb, EVMConfig: vm.Config{ Tracer: tracer, }}) diff --git a/params/config.go b/params/config.go index 527a2fb925..3eb43529ae 100644 --- a/params/config.go +++ b/params/config.go @@ -157,6 +157,8 @@ var ( Ethash: new(EthashConfig), Clique: nil, ArbitrumChainParams: DisableArbitrumParams(), + MaxCodeSize: newUint64(MaxCodeSize), + MaxInitCodeSize: newUint64(MaxInitCodeSize), } // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced @@ -187,6 +189,8 @@ var ( Ethash: nil, Clique: &CliqueConfig{Period: 0, Epoch: 30000}, ArbitrumChainParams: DisableArbitrumParams(), + MaxCodeSize: newUint64(MaxCodeSize), + MaxInitCodeSize: newUint64(MaxInitCodeSize), } // TestChainConfig contains every protocol change (EIPs) introduced @@ -217,6 +221,8 @@ var ( Ethash: new(EthashConfig), Clique: nil, ArbitrumChainParams: DisableArbitrumParams(), + MaxCodeSize: newUint64(MaxCodeSize), + MaxInitCodeSize: newUint64(MaxInitCodeSize), } // NonActivatedConfig defines the chain configuration without activating From b5d5c2b0cb748e251c2f2dccffdca2df85a21f36 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Tue, 26 Sep 2023 16:18:43 -0500 Subject: [PATCH 03/10] move limits inside ArbitrumChainParams --- core/genesis_test.go | 8 ++++++-- core/state_processor_test.go | 18 ++++++++++++------ core/state_transition.go | 4 ++-- core/txpool/txpool.go | 4 ++-- core/vm/evm.go | 2 +- core/vm/gas_table.go | 8 ++++---- params/config.go | 17 ++++------------- params/config_arbitrum.go | 4 ++++ params/config_test.go | 12 ++++++------ 9 files changed, 41 insertions(+), 36 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index 2223bfa0d6..10f8267781 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -47,14 +47,18 @@ func TestSetupGenesis(t *testing.T) { var ( customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") customg = Genesis{ - Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), MaxCodeSize: newUint64(params.MaxCodeSize), MaxInitCodeSize: newUint64(params.MaxInitCodeSize)}, + Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), ArbitrumChainParams: params.ArbitrumChainParams{ + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize)}}, Alloc: GenesisAlloc{ {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, }, } oldcustomg = customg ) - oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2), MaxCodeSize: newUint64(params.MaxCodeSize), MaxInitCodeSize: newUint64(params.MaxInitCodeSize)} + oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2), ArbitrumChainParams: params.ArbitrumChainParams{ + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize)}} tests := []struct { name string fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index ab3ca5d9d9..8e496a9330 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -58,8 +58,10 @@ func TestStateProcessorErrors(t *testing.T) { BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), Ethash: new(params.EthashConfig), - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + ArbitrumChainParams: params.ArbitrumChainParams{ + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + }, } signer = types.LatestSigner(config) key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") @@ -239,8 +241,10 @@ func TestStateProcessorErrors(t *testing.T) { PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + ArbitrumChainParams: params.ArbitrumChainParams{ + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + }, }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ @@ -337,8 +341,10 @@ func TestStateProcessorErrors(t *testing.T) { TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, ShanghaiTime: u64(0), - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + ArbitrumChainParams: params.ArbitrumChainParams{ + MaxCodeSize: newUint64(params.MaxCodeSize), + MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + }, }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ diff --git a/core/state_transition.go b/core/state_transition.go index 5dd8de1542..599a82dc6a 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -408,8 +408,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { } // Check whether the init code size has been exceeded. - if rules.IsShanghai && contractCreation && len(msg.Data) > int(*st.evm.ChainConfig().MaxInitCodeSize) { - return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(*st.evm.ChainConfig().MaxInitCodeSize)) + if rules.IsShanghai && contractCreation && len(msg.Data) > int(*st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize) { + return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(*st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize)) } // Execute the preparatory steps for state transition which includes: diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index a7f6d43518..ba6d45afc3 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -615,8 +615,8 @@ func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error { return ErrOversizedData } // Check whether the init code size has been exceeded. - if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(*pool.chainconfig.MaxInitCodeSize) { - return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(*pool.chainconfig.MaxInitCodeSize)) + if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(*pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize) { + return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(*pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize)) } // Transactions can't be negative. This may never happen using RLP decoded // transactions but may occur if you create a transaction using the RPC. diff --git a/core/vm/evm.go b/core/vm/evm.go index dbea4747b1..e4927e7fb5 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -504,7 +504,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, ret, err := evm.interpreter.Run(contract, nil, false) // Check whether the max code size has been exceeded, assign err if the case. - if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(*evm.chainConfig.MaxCodeSize) { + if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(*evm.chainConfig.ArbitrumChainParams.MaxCodeSize) { err = ErrMaxCodeSizeExceeded } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 7409d2c6cc..f49dcb5bce 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -308,10 +308,10 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > *evm.chainConfig.MaxInitCodeSize { + if overflow || size > *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { return 0, ErrGasUintOverflow } - // Since size <= *evm.chainConfig.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow moreGas := params.InitCodeWordGas * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow @@ -324,10 +324,10 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > *evm.chainConfig.MaxInitCodeSize { + if overflow || size > *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { return 0, ErrGasUintOverflow } - // Since size <= *evm.chainConfig.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow diff --git a/params/config.go b/params/config.go index 3eb43529ae..605d286c3c 100644 --- a/params/config.go +++ b/params/config.go @@ -157,8 +157,6 @@ var ( Ethash: new(EthashConfig), Clique: nil, ArbitrumChainParams: DisableArbitrumParams(), - MaxCodeSize: newUint64(MaxCodeSize), - MaxInitCodeSize: newUint64(MaxInitCodeSize), } // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced @@ -189,8 +187,6 @@ var ( Ethash: nil, Clique: &CliqueConfig{Period: 0, Epoch: 30000}, ArbitrumChainParams: DisableArbitrumParams(), - MaxCodeSize: newUint64(MaxCodeSize), - MaxInitCodeSize: newUint64(MaxInitCodeSize), } // TestChainConfig contains every protocol change (EIPs) introduced @@ -221,8 +217,6 @@ var ( Ethash: new(EthashConfig), Clique: nil, ArbitrumChainParams: DisableArbitrumParams(), - MaxCodeSize: newUint64(MaxCodeSize), - MaxInitCodeSize: newUint64(MaxInitCodeSize), } // NonActivatedConfig defines the chain configuration without activating @@ -314,9 +308,6 @@ type ChainConfig struct { Clique *CliqueConfig `json:"clique,omitempty"` ArbitrumChainParams ArbitrumChainParams `json:"arbitrum,omitempty"` - - MaxCodeSize *uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract - MaxInitCodeSize *uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions } // UnmarshalJSON implements the json.Unmarshaler interface by decoding the json @@ -327,11 +318,11 @@ func (c *ChainConfig) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &cfgJSON); err != nil { return err } - if cfgJSON.MaxCodeSize == nil { - cfgJSON.MaxCodeSize = newUint64(MaxCodeSize) + if cfgJSON.ArbitrumChainParams.MaxCodeSize == nil { + cfgJSON.ArbitrumChainParams.MaxCodeSize = newUint64(MaxCodeSize) } - if cfgJSON.MaxInitCodeSize == nil { - cfgJSON.MaxInitCodeSize = newUint64(MaxInitCodeSize) + if cfgJSON.ArbitrumChainParams.MaxInitCodeSize == nil { + cfgJSON.ArbitrumChainParams.MaxInitCodeSize = newUint64(MaxInitCodeSize) } *c = ChainConfig(cfgJSON) return nil diff --git a/params/config_arbitrum.go b/params/config_arbitrum.go index 516fb085af..b8823dd65f 100644 --- a/params/config_arbitrum.go +++ b/params/config_arbitrum.go @@ -29,6 +29,8 @@ type ArbitrumChainParams struct { InitialArbOSVersion uint64 InitialChainOwner common.Address GenesisBlockNum uint64 + MaxCodeSize *uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract + MaxInitCodeSize *uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions } func (c *ChainConfig) IsArbitrum() bool { @@ -136,6 +138,8 @@ func DisableArbitrumParams() ArbitrumChainParams { DataAvailabilityCommittee: false, InitialArbOSVersion: 0, InitialChainOwner: common.Address{}, + MaxCodeSize: newUint64(MaxCodeSize), + MaxInitCodeSize: newUint64(MaxInitCodeSize), } } diff --git a/params/config_test.go b/params/config_test.go index f2ca219cf2..576f191063 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -148,13 +148,13 @@ type marshalUnMarshalTest struct { } var unmarshalChainConfigTests = []marshalUnMarshalTest{ - {input: `{"maxCodeSize": 10, "maxInitCodeSize": 10}`, + {input: `{"arbitrum": {"maxCodeSize": 10, "maxInitCodeSize": 10} }`, want: [2]uint64{10, 10}}, - {input: `{"maxCodeSize": 10}`, + {input: `{"arbitrum": {"maxCodeSize": 10} }`, want: [2]uint64{10, MaxInitCodeSize}}, - {input: `{"maxInitCodeSize": 10}`, + {input: `{"arbitrum": {"maxInitCodeSize": 10} }`, want: [2]uint64{MaxCodeSize, 10}}, - {input: `{}`, + {input: `{"arbitrum": {} }`, want: [2]uint64{MaxCodeSize, MaxInitCodeSize}}, } @@ -165,9 +165,9 @@ func TestUnmarshalChainConfig(t *testing.T) { t.Errorf("failed to unmarshal. Error: %q", err) } expected := test.want.([2]uint64) - if *c.MaxCodeSize != expected[0] || *c.MaxInitCodeSize != expected[1] { + if *c.ArbitrumChainParams.MaxCodeSize != expected[0] || *c.ArbitrumChainParams.MaxInitCodeSize != expected[1] { t.Errorf("failed to unmarshal MaxCodeSize and MaxInitCodeSize correctly. unmarshalled as (%d %d) want (%d %d))", - *c.MaxCodeSize, *c.MaxInitCodeSize, expected[0], expected[1]) + *c.ArbitrumChainParams.MaxCodeSize, *c.ArbitrumChainParams.MaxInitCodeSize, expected[0], expected[1]) } } } From 76a50083f4619ec47d128672ef6a854813a698d6 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Thu, 28 Sep 2023 11:03:17 -0500 Subject: [PATCH 04/10] address PR comments --- core/genesis_test.go | 10 ++++------ core/state_processor_test.go | 12 ++++++------ core/state_transition.go | 4 ++-- core/txpool/txpool.go | 4 ++-- core/vm/evm.go | 2 +- core/vm/gas_table.go | 4 ++-- params/config.go | 8 ++++---- params/config_arbitrum.go | 8 ++++---- params/config_test.go | 6 ++++-- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index 10f8267781..1fda52514d 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -41,15 +41,13 @@ func TestInvalidCliqueConfig(t *testing.T) { } } -func newUint64(val uint64) *uint64 { return &val } - func TestSetupGenesis(t *testing.T) { var ( customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") customg = Genesis{ Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize)}}, + MaxCodeSize: params.MaxCodeSize, + MaxInitCodeSize: params.MaxInitCodeSize}}, Alloc: GenesisAlloc{ {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, }, @@ -57,8 +55,8 @@ func TestSetupGenesis(t *testing.T) { oldcustomg = customg ) oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize)}} + MaxCodeSize: params.MaxCodeSize, + MaxInitCodeSize: params.MaxInitCodeSize}} tests := []struct { name string fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 8e496a9330..31110db3f4 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -59,8 +59,8 @@ func TestStateProcessorErrors(t *testing.T) { LondonBlock: big.NewInt(0), Ethash: new(params.EthashConfig), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + MaxCodeSize: params.MaxCodeSize, + MaxInitCodeSize: params.MaxInitCodeSize, }, } signer = types.LatestSigner(config) @@ -242,8 +242,8 @@ func TestStateProcessorErrors(t *testing.T) { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + MaxCodeSize: params.MaxCodeSize, + MaxInitCodeSize: params.MaxInitCodeSize, }, }, Alloc: GenesisAlloc{ @@ -342,8 +342,8 @@ func TestStateProcessorErrors(t *testing.T) { TerminalTotalDifficultyPassed: true, ShanghaiTime: u64(0), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: newUint64(params.MaxCodeSize), - MaxInitCodeSize: newUint64(params.MaxInitCodeSize), + MaxCodeSize: params.MaxCodeSize, + MaxInitCodeSize: params.MaxInitCodeSize, }, }, Alloc: GenesisAlloc{ diff --git a/core/state_transition.go b/core/state_transition.go index 599a82dc6a..bdd0eaba96 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -408,8 +408,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { } // Check whether the init code size has been exceeded. - if rules.IsShanghai && contractCreation && len(msg.Data) > int(*st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize) { - return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(*st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize)) + if rules.IsShanghai && contractCreation && len(msg.Data) > int(st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize) { + return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize)) } // Execute the preparatory steps for state transition which includes: diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index ba6d45afc3..9f2406cccc 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -615,8 +615,8 @@ func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error { return ErrOversizedData } // Check whether the init code size has been exceeded. - if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(*pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize) { - return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(*pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize)) + if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize) { + return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize)) } // Transactions can't be negative. This may never happen using RLP decoded // transactions but may occur if you create a transaction using the RPC. diff --git a/core/vm/evm.go b/core/vm/evm.go index e4927e7fb5..4a7eb2a1ca 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -504,7 +504,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, ret, err := evm.interpreter.Run(contract, nil, false) // Check whether the max code size has been exceeded, assign err if the case. - if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(*evm.chainConfig.ArbitrumChainParams.MaxCodeSize) { + if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(evm.chainConfig.ArbitrumChainParams.MaxCodeSize) { err = ErrMaxCodeSizeExceeded } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index f49dcb5bce..7a83fbc360 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -308,7 +308,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { + if overflow || size > evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { return 0, ErrGasUintOverflow } // Since size <= *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow @@ -324,7 +324,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { + if overflow || size > evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { return 0, ErrGasUintOverflow } // Since size <= *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow diff --git a/params/config.go b/params/config.go index 605d286c3c..e734b66467 100644 --- a/params/config.go +++ b/params/config.go @@ -318,11 +318,11 @@ func (c *ChainConfig) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &cfgJSON); err != nil { return err } - if cfgJSON.ArbitrumChainParams.MaxCodeSize == nil { - cfgJSON.ArbitrumChainParams.MaxCodeSize = newUint64(MaxCodeSize) + if cfgJSON.ArbitrumChainParams.MaxCodeSize == 0 { + cfgJSON.ArbitrumChainParams.MaxCodeSize = MaxCodeSize } - if cfgJSON.ArbitrumChainParams.MaxInitCodeSize == nil { - cfgJSON.ArbitrumChainParams.MaxInitCodeSize = newUint64(MaxInitCodeSize) + if cfgJSON.ArbitrumChainParams.MaxInitCodeSize == 0 { + cfgJSON.ArbitrumChainParams.MaxInitCodeSize = MaxInitCodeSize } *c = ChainConfig(cfgJSON) return nil diff --git a/params/config_arbitrum.go b/params/config_arbitrum.go index b8823dd65f..0483c9db94 100644 --- a/params/config_arbitrum.go +++ b/params/config_arbitrum.go @@ -29,8 +29,8 @@ type ArbitrumChainParams struct { InitialArbOSVersion uint64 InitialChainOwner common.Address GenesisBlockNum uint64 - MaxCodeSize *uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract - MaxInitCodeSize *uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions + MaxCodeSize uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize + MaxInitCodeSize uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions. o vale implies params.MaxInitCodeSize } func (c *ChainConfig) IsArbitrum() bool { @@ -138,8 +138,8 @@ func DisableArbitrumParams() ArbitrumChainParams { DataAvailabilityCommittee: false, InitialArbOSVersion: 0, InitialChainOwner: common.Address{}, - MaxCodeSize: newUint64(MaxCodeSize), - MaxInitCodeSize: newUint64(MaxInitCodeSize), + MaxCodeSize: MaxCodeSize, + MaxInitCodeSize: MaxInitCodeSize, } } diff --git a/params/config_test.go b/params/config_test.go index 576f191063..bf0766af82 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -156,6 +156,8 @@ var unmarshalChainConfigTests = []marshalUnMarshalTest{ want: [2]uint64{MaxCodeSize, 10}}, {input: `{"arbitrum": {} }`, want: [2]uint64{MaxCodeSize, MaxInitCodeSize}}, + {input: `{"arbitrum": {"maxCodeSize": 0, "maxInitCodeSize": 0} }`, + want: [2]uint64{MaxCodeSize, MaxInitCodeSize}}, } func TestUnmarshalChainConfig(t *testing.T) { @@ -165,9 +167,9 @@ func TestUnmarshalChainConfig(t *testing.T) { t.Errorf("failed to unmarshal. Error: %q", err) } expected := test.want.([2]uint64) - if *c.ArbitrumChainParams.MaxCodeSize != expected[0] || *c.ArbitrumChainParams.MaxInitCodeSize != expected[1] { + if c.ArbitrumChainParams.MaxCodeSize != expected[0] || c.ArbitrumChainParams.MaxInitCodeSize != expected[1] { t.Errorf("failed to unmarshal MaxCodeSize and MaxInitCodeSize correctly. unmarshalled as (%d %d) want (%d %d))", - *c.ArbitrumChainParams.MaxCodeSize, *c.ArbitrumChainParams.MaxInitCodeSize, expected[0], expected[1]) + c.ArbitrumChainParams.MaxCodeSize, c.ArbitrumChainParams.MaxInitCodeSize, expected[0], expected[1]) } } } From 40834490cbd9e92e5b08bc18951e2794bb45ed33 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Thu, 28 Sep 2023 11:04:23 -0500 Subject: [PATCH 05/10] fix comments --- core/vm/gas_table.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 7a83fbc360..f9aa3bef55 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -311,7 +311,7 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m if overflow || size > evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { return 0, ErrGasUintOverflow } - // Since size <= *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow moreGas := params.InitCodeWordGas * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow @@ -327,7 +327,7 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, if overflow || size > evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { return 0, ErrGasUintOverflow } - // Since size <= *evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow From 3353e501cce63030302b0f9f68b6f61d0e2e1582 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Fri, 29 Sep 2023 10:17:19 -0500 Subject: [PATCH 06/10] change impl --- core/genesis_test.go | 8 ++------ core/state_processor_test.go | 12 ------------ core/state_transition.go | 4 ++-- core/txpool/txpool.go | 4 ++-- core/vm/evm.go | 2 +- core/vm/gas_table.go | 8 ++++---- core/vm/runtime/runtime_test.go | 8 +++----- params/config.go | 19 ------------------- params/config_arbitrum.go | 20 ++++++++++++++++---- params/config_test.go | 33 --------------------------------- 10 files changed, 30 insertions(+), 88 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index 1fda52514d..e069eb448c 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -45,18 +45,14 @@ func TestSetupGenesis(t *testing.T) { var ( customghash = common.HexToHash("0x89c99d90b79719238d2645c7642f2c9295246e80775b38cfd162b696817fbd50") customg = Genesis{ - Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: params.MaxCodeSize, - MaxInitCodeSize: params.MaxInitCodeSize}}, + Config: ¶ms.ChainConfig{HomesteadBlock: big.NewInt(3)}, Alloc: GenesisAlloc{ {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, }, } oldcustomg = customg ) - oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2), ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: params.MaxCodeSize, - MaxInitCodeSize: params.MaxInitCodeSize}} + oldcustomg.Config = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(2)} tests := []struct { name string fn func(ethdb.Database) (*params.ChainConfig, common.Hash, error) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 31110db3f4..53675c93e7 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -58,10 +58,6 @@ func TestStateProcessorErrors(t *testing.T) { BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), Ethash: new(params.EthashConfig), - ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: params.MaxCodeSize, - MaxInitCodeSize: params.MaxInitCodeSize, - }, } signer = types.LatestSigner(config) key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") @@ -241,10 +237,6 @@ func TestStateProcessorErrors(t *testing.T) { PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), - ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: params.MaxCodeSize, - MaxInitCodeSize: params.MaxInitCodeSize, - }, }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ @@ -341,10 +333,6 @@ func TestStateProcessorErrors(t *testing.T) { TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, ShanghaiTime: u64(0), - ArbitrumChainParams: params.ArbitrumChainParams{ - MaxCodeSize: params.MaxCodeSize, - MaxInitCodeSize: params.MaxInitCodeSize, - }, }, Alloc: GenesisAlloc{ common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7"): GenesisAccount{ diff --git a/core/state_transition.go b/core/state_transition.go index bdd0eaba96..b995ee4887 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -408,8 +408,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { } // Check whether the init code size has been exceeded. - if rules.IsShanghai && contractCreation && len(msg.Data) > int(st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize) { - return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(st.evm.ChainConfig().ArbitrumChainParams.MaxInitCodeSize)) + if rules.IsShanghai && contractCreation && len(msg.Data) > int(st.evm.ChainConfig().MaxInitCodeSize()) { + return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), int(st.evm.ChainConfig().MaxInitCodeSize())) } // Execute the preparatory steps for state transition which includes: diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index 9f2406cccc..8c5915d7d8 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -615,8 +615,8 @@ func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error { return ErrOversizedData } // Check whether the init code size has been exceeded. - if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize) { - return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(pool.chainconfig.ArbitrumChainParams.MaxInitCodeSize)) + if pool.shanghai.Load() && tx.To() == nil && len(tx.Data()) > int(pool.chainconfig.MaxInitCodeSize()) { + return fmt.Errorf("%w: code size %v limit %v", core.ErrMaxInitCodeSizeExceeded, len(tx.Data()), int(pool.chainconfig.MaxInitCodeSize())) } // Transactions can't be negative. This may never happen using RLP decoded // transactions but may occur if you create a transaction using the RPC. diff --git a/core/vm/evm.go b/core/vm/evm.go index 4a7eb2a1ca..585503732f 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -504,7 +504,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, ret, err := evm.interpreter.Run(contract, nil, false) // Check whether the max code size has been exceeded, assign err if the case. - if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(evm.chainConfig.ArbitrumChainParams.MaxCodeSize) { + if err == nil && evm.chainRules.IsEIP158 && len(ret) > int(evm.chainConfig.MaxCodeSize()) { err = ErrMaxCodeSizeExceeded } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index f9aa3bef55..7775db2951 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -308,10 +308,10 @@ func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { + if overflow || size > evm.chainConfig.MaxInitCodeSize() { return 0, ErrGasUintOverflow } - // Since size <= evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= evm.chainConfig.MaxInitCodeSize(), these multiplication cannot overflow moreGas := params.InitCodeWordGas * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow @@ -324,10 +324,10 @@ func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, return 0, err } size, overflow := stack.Back(2).Uint64WithOverflow() - if overflow || size > evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize { + if overflow || size > evm.chainConfig.MaxInitCodeSize() { return 0, ErrGasUintOverflow } - // Since size <= evm.chainConfig.ArbitrumChainParams.MaxInitCodeSize, these multiplication cannot overflow + // Since size <= evm.chainConfig.MaxInitCodeSize(), these multiplication cannot overflow moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { return 0, ErrGasUintOverflow diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index b22cfce7a0..7b521a2ead 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -831,9 +831,8 @@ func TestRuntimeJSTracer(t *testing.T) { t.Fatal(err) } _, _, err = Call(main, nil, &Config{ - ChainConfig: params.TestChainConfig, - GasLimit: 1000000, - State: statedb, + GasLimit: 1000000, + State: statedb, EVMConfig: vm.Config{ Tracer: tracer, }}) @@ -867,8 +866,7 @@ func TestJSTracerCreateTx(t *testing.T) { t.Fatal(err) } _, _, _, err = Create(code, &Config{ - ChainConfig: params.TestChainConfig, - State: statedb, + State: statedb, EVMConfig: vm.Config{ Tracer: tracer, }}) diff --git a/params/config.go b/params/config.go index e734b66467..a5c518aa8d 100644 --- a/params/config.go +++ b/params/config.go @@ -17,7 +17,6 @@ package params import ( - "encoding/json" "fmt" "math/big" @@ -310,24 +309,6 @@ type ChainConfig struct { ArbitrumChainParams ArbitrumChainParams `json:"arbitrum,omitempty"` } -// UnmarshalJSON implements the json.Unmarshaler interface by decoding the json -// string values into the config fields -func (c *ChainConfig) UnmarshalJSON(data []byte) error { - type chainConfigJSON ChainConfig - var cfgJSON chainConfigJSON - if err := json.Unmarshal(data, &cfgJSON); err != nil { - return err - } - if cfgJSON.ArbitrumChainParams.MaxCodeSize == 0 { - cfgJSON.ArbitrumChainParams.MaxCodeSize = MaxCodeSize - } - if cfgJSON.ArbitrumChainParams.MaxInitCodeSize == 0 { - cfgJSON.ArbitrumChainParams.MaxInitCodeSize = MaxInitCodeSize - } - *c = ChainConfig(cfgJSON) - return nil -} - // EthashConfig is the consensus engine configs for proof-of-work based sealing. type EthashConfig struct{} diff --git a/params/config_arbitrum.go b/params/config_arbitrum.go index 0483c9db94..48053e2e4a 100644 --- a/params/config_arbitrum.go +++ b/params/config_arbitrum.go @@ -29,8 +29,8 @@ type ArbitrumChainParams struct { InitialArbOSVersion uint64 InitialChainOwner common.Address GenesisBlockNum uint64 - MaxCodeSize uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize - MaxInitCodeSize uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions. o vale implies params.MaxInitCodeSize + MaxCodeSize uint64 // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize + MaxInitCodeSize uint64 // Maximum initcode to permit in a creation transaction and create instructions. o vale implies params.MaxInitCodeSize } func (c *ChainConfig) IsArbitrum() bool { @@ -41,6 +41,20 @@ func (c *ChainConfig) IsArbitrumNitro(num *big.Int) bool { return c.IsArbitrum() && isBlockForked(new(big.Int).SetUint64(c.ArbitrumChainParams.GenesisBlockNum), num) } +func (c *ChainConfig) MaxCodeSize() uint64 { + if c.ArbitrumChainParams.MaxCodeSize == 0 { + return MaxCodeSize + } + return c.ArbitrumChainParams.MaxCodeSize +} + +func (c *ChainConfig) MaxInitCodeSize() uint64 { + if c.ArbitrumChainParams.MaxInitCodeSize == 0 { + return MaxInitCodeSize + } + return c.ArbitrumChainParams.MaxInitCodeSize +} + func (c *ChainConfig) DebugMode() bool { return c.ArbitrumChainParams.AllowDebugPrecompiles } @@ -138,8 +152,6 @@ func DisableArbitrumParams() ArbitrumChainParams { DataAvailabilityCommittee: false, InitialArbOSVersion: 0, InitialChainOwner: common.Address{}, - MaxCodeSize: MaxCodeSize, - MaxInitCodeSize: MaxInitCodeSize, } } diff --git a/params/config_test.go b/params/config_test.go index bf0766af82..1d03d96739 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -17,7 +17,6 @@ package params import ( - "encoding/json" "math/big" "reflect" "testing" @@ -141,35 +140,3 @@ func TestConfigRules(t *testing.T) { t.Errorf("expected %v to be shanghai", currentArbosVersion) } } - -type marshalUnMarshalTest struct { - input interface{} - want interface{} -} - -var unmarshalChainConfigTests = []marshalUnMarshalTest{ - {input: `{"arbitrum": {"maxCodeSize": 10, "maxInitCodeSize": 10} }`, - want: [2]uint64{10, 10}}, - {input: `{"arbitrum": {"maxCodeSize": 10} }`, - want: [2]uint64{10, MaxInitCodeSize}}, - {input: `{"arbitrum": {"maxInitCodeSize": 10} }`, - want: [2]uint64{MaxCodeSize, 10}}, - {input: `{"arbitrum": {} }`, - want: [2]uint64{MaxCodeSize, MaxInitCodeSize}}, - {input: `{"arbitrum": {"maxCodeSize": 0, "maxInitCodeSize": 0} }`, - want: [2]uint64{MaxCodeSize, MaxInitCodeSize}}, -} - -func TestUnmarshalChainConfig(t *testing.T) { - var c ChainConfig - for _, test := range unmarshalChainConfigTests { - if err := json.Unmarshal([]byte(test.input.(string)), &c); err != nil { - t.Errorf("failed to unmarshal. Error: %q", err) - } - expected := test.want.([2]uint64) - if c.ArbitrumChainParams.MaxCodeSize != expected[0] || c.ArbitrumChainParams.MaxInitCodeSize != expected[1] { - t.Errorf("failed to unmarshal MaxCodeSize and MaxInitCodeSize correctly. unmarshalled as (%d %d) want (%d %d))", - c.ArbitrumChainParams.MaxCodeSize, c.ArbitrumChainParams.MaxInitCodeSize, expected[0], expected[1]) - } - } -} From e0f0fa7258f16d92716641e2936d2deb45ccf574 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Mon, 2 Oct 2023 16:46:00 -0500 Subject: [PATCH 07/10] missed change --- cmd/evm/internal/t8ntool/transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index a4dc0e9854..c839f3b3a4 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -172,7 +172,7 @@ func Transaction(ctx *cli.Context) error { r.Error = errors.New("gas * maxFeePerGas exceeds 256 bits") } // Check whether the init code size has been exceeded. - if chainConfig.IsShanghai(new(big.Int), 0, 0) && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize { + if chainConfig.IsShanghai(new(big.Int), 0, 0) && tx.To() == nil && len(tx.Data()) > int(chainConfig.MaxInitCodeSize()) { r.Error = errors.New("max initcode size exceeded") } results = append(results, r) From 83eab2068adeb1fa06222b64841841d76f603133 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Fri, 13 Oct 2023 12:20:10 -0500 Subject: [PATCH 08/10] fix typo --- params/config_arbitrum.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params/config_arbitrum.go b/params/config_arbitrum.go index 2adbea0436..950f1358af 100644 --- a/params/config_arbitrum.go +++ b/params/config_arbitrum.go @@ -30,7 +30,7 @@ type ArbitrumChainParams struct { InitialChainOwner common.Address GenesisBlockNum uint64 MaxCodeSize uint64 // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize - MaxInitCodeSize uint64 // Maximum initcode to permit in a creation transaction and create instructions. o vale implies params.MaxInitCodeSize + MaxInitCodeSize uint64 // Maximum initcode to permit in a creation transaction and create instructions. o value implies params.MaxInitCodeSize } func (c *ChainConfig) IsArbitrum() bool { From 8dd6cfd5c1ce6f5276aad3a31f6b5a910c722fc7 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Tue, 24 Oct 2023 11:29:10 -0500 Subject: [PATCH 09/10] address PR comments --- params/config_arbitrum.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/params/config_arbitrum.go b/params/config_arbitrum.go index 950f1358af..0f54935e3f 100644 --- a/params/config_arbitrum.go +++ b/params/config_arbitrum.go @@ -29,8 +29,8 @@ type ArbitrumChainParams struct { InitialArbOSVersion uint64 InitialChainOwner common.Address GenesisBlockNum uint64 - MaxCodeSize uint64 // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize - MaxInitCodeSize uint64 // Maximum initcode to permit in a creation transaction and create instructions. o value implies params.MaxInitCodeSize + MaxCodeSize uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize + MaxInitCodeSize uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions. 0 value implies params.MaxInitCodeSize } func (c *ChainConfig) IsArbitrum() bool { @@ -50,7 +50,7 @@ func (c *ChainConfig) MaxCodeSize() uint64 { func (c *ChainConfig) MaxInitCodeSize() uint64 { if c.ArbitrumChainParams.MaxInitCodeSize == 0 { - return MaxInitCodeSize + return c.MaxCodeSize() * 2 } return c.ArbitrumChainParams.MaxInitCodeSize } From a60b14e7b919e26e8cadad3fa50268a0f72b5db1 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Tue, 24 Oct 2023 17:45:41 -0500 Subject: [PATCH 10/10] fix typo --- params/config_arbitrum.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/params/config_arbitrum.go b/params/config_arbitrum.go index 0f54935e3f..6d93a685e1 100644 --- a/params/config_arbitrum.go +++ b/params/config_arbitrum.go @@ -29,8 +29,8 @@ type ArbitrumChainParams struct { InitialArbOSVersion uint64 InitialChainOwner common.Address GenesisBlockNum uint64 - MaxCodeSize uint64 `json:"maxCodeSize,omitempty,"` // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize - MaxInitCodeSize uint64 `json:"maxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions. 0 value implies params.MaxInitCodeSize + MaxCodeSize uint64 `json:"MaxCodeSize,omitempty"` // Maximum bytecode to permit for a contract. 0 value implies params.MaxCodeSize + MaxInitCodeSize uint64 `json:"MaxInitCodeSize,omitempty"` // Maximum initcode to permit in a creation transaction and create instructions. 0 value implies params.MaxInitCodeSize } func (c *ChainConfig) IsArbitrum() bool {