From a1f8141d58187fa51ec45b7d867f28466fbe704e Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Thu, 21 Sep 2023 14:17:35 -0500 Subject: [PATCH 01/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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 1607adcaaba04c561d3f75a7d8d550d1cc21561c Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Thu, 5 Oct 2023 16:48:17 +0000 Subject: [PATCH 08/20] garbage collect not commited tries --- core/blockchain.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 79d97d9d0d..85b342cc20 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1451,7 +1451,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. // If MaxNumberOfBlocksToSkipStateSaving or MaxAmountOfGasToSkipStateSaving is not zero, then flushing of some blocks will be skipped: // * at most MaxNumberOfBlocksToSkipStateSaving block state commits will be skipped // * sum of gas used in skipped blocks will be at most MaxAmountOfGasToSkipStateSaving - if bc.cacheConfig.TrieDirtyDisabled { + archiveNode := bc.cacheConfig.TrieDirtyDisabled + if archiveNode { var maySkipCommiting, blockLimitReached, gasLimitReached bool if bc.cacheConfig.MaxNumberOfBlocksToSkipStateSaving != 0 { maySkipCommiting = true @@ -1474,10 +1475,10 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.amountOfGasInBlocksToSkipStateSaving = bc.cacheConfig.MaxAmountOfGasToSkipStateSaving return bc.triedb.Commit(root, false) } - return nil + // we are skipping saving the trie to diskdb, so we need to keep the trie in memory and garbage collect it later } - // Full but not archive node, do proper garbage collection + // Full node or archive node that's not keeping all states, do proper garbage collection bc.triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive bc.triegc.Push(trieGcEntry{root, block.Header().Time}, -int64(block.NumberU64())) @@ -1510,7 +1511,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. } flushInterval := time.Duration(bc.flushInterval.Load()) // If we exceeded out time allowance, flush an entire trie to disk - if bc.gcproc > flushInterval && prevEntry != nil { + // In case of archive node that skips some trie commits we don't flush tries here + if bc.gcproc > flushInterval && prevEntry != nil && !archiveNode { // If the header is missing (canonical chain behind), we're reorging a low // diff sidechain. Suspend committing until this operation is completed. header := bc.GetHeaderByNumber(prevNum) From 2b1eb7e3bcecba01b99d7d0cee8ba869105f7705 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Mon, 9 Oct 2023 17:34:49 +0000 Subject: [PATCH 09/20] lock db lock when accessing dirties map in commit method --- trie/triedb/hashdb/database.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/trie/triedb/hashdb/database.go b/trie/triedb/hashdb/database.go index 8ebcff9f1f..60f18e8151 100644 --- a/trie/triedb/hashdb/database.go +++ b/trie/triedb/hashdb/database.go @@ -460,7 +460,9 @@ func (db *Database) Commit(node common.Hash, report bool) error { // commit is the private locked version of Commit. func (db *Database) commit(hash common.Hash, batch ethdb.Batch, uncacher *cleaner) error { // If the node does not exist, it's a previously committed node + db.lock.RLock() node, ok := db.dirties[hash] + db.lock.RUnlock() if !ok { return nil } From f5e52cb1e34bd0bf76d17477202c2848a27edbd7 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Tue, 10 Oct 2023 17:55:39 +0200 Subject: [PATCH 10/20] add safty checks to BlockChain.Stop --- core/blockchain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 85b342cc20..f6cf6814d4 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1041,13 +1041,13 @@ func (bc *BlockChain) Stop() { for _, offset := range []uint64{0, 1, bc.cacheConfig.TriesInMemory - 1, math.MaxUint64} { if number := bc.CurrentBlock().Number.Uint64(); number > offset { var recent *types.Block - if offset == math.MaxUint { + if offset == math.MaxUint && !bc.triegc.Empty() { _, latest := bc.triegc.Peek() recent = bc.GetBlockByNumber(uint64(-latest)) } else { recent = bc.GetBlockByNumber(number - offset) } - if recent.Root() == (common.Hash{}) { + if recent == nil || recent.Root() == (common.Hash{}) { continue } From f0840474b8f2c629fd1c7b79a891c1a5e0a13c79 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 11 Oct 2023 01:02:47 +0200 Subject: [PATCH 11/20] fix commiting oldest node from triegc in BlockChain.Stop --- core/blockchain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index f6cf6814d4..2ccc178de8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1039,9 +1039,9 @@ func (bc *BlockChain) Stop() { triedb := bc.triedb for _, offset := range []uint64{0, 1, bc.cacheConfig.TriesInMemory - 1, math.MaxUint64} { - if number := bc.CurrentBlock().Number.Uint64(); number > offset { + if number := bc.CurrentBlock().Number.Uint64(); number > offset || offset == math.MaxUint64 { var recent *types.Block - if offset == math.MaxUint && !bc.triegc.Empty() { + if offset == math.MaxUint64 && !bc.triegc.Empty() { _, latest := bc.triegc.Peek() recent = bc.GetBlockByNumber(uint64(-latest)) } else { From 89540b1440179a851e5e29672a700851c4237d83 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 11 Oct 2023 18:57:42 +0200 Subject: [PATCH 12/20] add locking aroung db.dirties access --- trie/triedb/hashdb/database.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/trie/triedb/hashdb/database.go b/trie/triedb/hashdb/database.go index 60f18e8151..b93764ae9e 100644 --- a/trie/triedb/hashdb/database.go +++ b/trie/triedb/hashdb/database.go @@ -329,6 +329,7 @@ func (db *Database) Cap(limit common.StorageSize) error { // outside code doesn't see an inconsistent state (referenced data removed from // memory cache during commit but not yet in persistent storage). This is ensured // by only uncaching existing data when the database write finalizes. + db.lock.RLock() nodes, storage, start := len(db.dirties), db.dirtiesSize, time.Now() batch := db.diskdb.NewBatch() @@ -337,12 +338,15 @@ func (db *Database) Cap(limit common.StorageSize) error { // counted. size := db.dirtiesSize + common.StorageSize(len(db.dirties)*cachedNodeSize) size += db.childrenSize + db.lock.RUnlock() // Keep committing nodes from the flush-list until we're below allowance oldest := db.oldest for size > limit && oldest != (common.Hash{}) { // Fetch the oldest referenced node and push into the batch + db.lock.RLock() node := db.dirties[oldest] + db.lock.RUnlock() rawdb.WriteLegacyTrieNode(batch, oldest, node.node) // If we exceeded the ideal batch size, commit and reset @@ -418,7 +422,9 @@ func (db *Database) Commit(node common.Hash, report bool) error { batch := db.diskdb.NewBatch() // Move the trie itself into the batch, flushing if enough data is accumulated + db.lock.RLock() nodes, storage := len(db.dirties), db.dirtiesSize + db.lock.RUnlock() uncacher := &cleaner{db} if err := db.commit(node, batch, uncacher); err != nil { From 3573250adec48511a0bc45647061ed6d25c4ae35 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 11 Oct 2023 19:09:30 +0200 Subject: [PATCH 13/20] move time.Now outside critical section --- trie/triedb/hashdb/database.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/trie/triedb/hashdb/database.go b/trie/triedb/hashdb/database.go index b93764ae9e..2f4e40111a 100644 --- a/trie/triedb/hashdb/database.go +++ b/trie/triedb/hashdb/database.go @@ -329,8 +329,9 @@ func (db *Database) Cap(limit common.StorageSize) error { // outside code doesn't see an inconsistent state (referenced data removed from // memory cache during commit but not yet in persistent storage). This is ensured // by only uncaching existing data when the database write finalizes. + start := time.Now() db.lock.RLock() - nodes, storage, start := len(db.dirties), db.dirtiesSize, time.Now() + nodes, storage := len(db.dirties), db.dirtiesSize batch := db.diskdb.NewBatch() // db.dirtiesSize only contains the useful data in the cache, but when reporting From 202caf5bf944835ef78b4d7c4ba8b48bdc54d65d Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 11 Oct 2023 22:34:39 +0200 Subject: [PATCH 14/20] move NewBatch() call outside of critical section --- trie/triedb/hashdb/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trie/triedb/hashdb/database.go b/trie/triedb/hashdb/database.go index 2f4e40111a..da1339e07f 100644 --- a/trie/triedb/hashdb/database.go +++ b/trie/triedb/hashdb/database.go @@ -330,9 +330,9 @@ func (db *Database) Cap(limit common.StorageSize) error { // memory cache during commit but not yet in persistent storage). This is ensured // by only uncaching existing data when the database write finalizes. start := time.Now() + batch := db.diskdb.NewBatch() db.lock.RLock() nodes, storage := len(db.dirties), db.dirtiesSize - batch := db.diskdb.NewBatch() // db.dirtiesSize only contains the useful data in the cache, but when reporting // the total memory consumption, the maintenance metadata is also needed to be From 83eab2068adeb1fa06222b64841841d76f603133 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Fri, 13 Oct 2023 12:20:10 -0500 Subject: [PATCH 15/20] 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 4fc6ae82ec9e6f9825935245f5cd6bbff0cd3125 Mon Sep 17 00:00:00 2001 From: Tsahi Zidenberg Date: Wed, 18 Oct 2023 12:08:17 -0600 Subject: [PATCH 16/20] arbitrum apiBackend: GetEVM handles nil blokContext --- arbitrum/apibackend.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arbitrum/apibackend.go b/arbitrum/apibackend.go index 4d5c308dc8..ce4dea0951 100644 --- a/arbitrum/apibackend.go +++ b/arbitrum/apibackend.go @@ -504,7 +504,13 @@ func (a *APIBackend) GetEVM(ctx context.Context, msg *core.Message, state *state vmConfig = a.BlockChain().GetVMConfig() } txContext := core.NewEVMTxContext(msg) - return vm.NewEVM(*blockCtx, txContext, state, a.BlockChain().Config(), *vmConfig), vmError + var context vm.BlockContext + if blockCtx != nil { + context = *blockCtx + } else { + context = core.NewEVMBlockContext(header, a.BlockChain(), nil) + } + return vm.NewEVM(context, txContext, state, a.BlockChain().Config(), *vmConfig), vmError } func (a *APIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { From e8c8827c0b9e22e60829da1945cba9c451cda85a Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Thu, 19 Oct 2023 12:08:08 -0600 Subject: [PATCH 17/20] Set TxRunMode for scheduled txs in DoCall --- internal/ethapi/api.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 0007451991..e370f249a7 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1068,6 +1068,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash if err != nil { return nil, err } + msg.TxRunMode = runMode // make a new EVM for the scheduled Tx (an EVM must never be reused) evm, vmError := b.GetEVM(ctx, msg, state, header, &vm.Config{NoBaseFee: true}, &blockCtx) go func() { From e9e463e9a8dcecc078f93711abeb8f3c77974015 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Mon, 23 Oct 2023 19:19:12 +0000 Subject: [PATCH 18/20] add cleans cache finalizer, trigger the cleaning early when recreating state --- eth/state_accessor.go | 2 ++ trie/database_wrap.go | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 1b0ab0c545..4eab76ba69 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -85,6 +85,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe // Create an ephemeral trie.Database for isolating the live one. Otherwise // the internal junks created by tracing will be persisted into the disk. database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16}) + defer database.TrieDB().ResetCleans() if statedb, err = state.New(block.Root(), database, nil); err == nil { log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number()) return statedb, noopReleaser, nil @@ -100,6 +101,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe // Create an ephemeral trie.Database for isolating the live one. Otherwise // the internal junks created by tracing will be persisted into the disk. database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16}) + defer database.TrieDB().ResetCleans() // If we didn't check the live database, do check state over ephemeral database, // otherwise we would rewind past a persisted block (specific corner case is diff --git a/trie/database_wrap.go b/trie/database_wrap.go index b05f56847a..2ca83d8f68 100644 --- a/trie/database_wrap.go +++ b/trie/database_wrap.go @@ -84,6 +84,7 @@ func prepare(diskdb ethdb.Database, config *Config) *Database { } else { cleans = fastcache.LoadFromFileOrNew(config.Journal, config.Cache*1024*1024) } + runtime.SetFinalizer(cleans, func(c *fastcache.Cache) { c.Reset() }) } var preimages *preimageStore if config != nil && config.Preimages { @@ -97,6 +98,13 @@ func prepare(diskdb ethdb.Database, config *Config) *Database { } } +// resets fastcache to return memory chunks to pool of free chunks +func (db *Database) ResetCleans() { + if db.cleans != nil { + db.cleans.Reset() + } +} + // NewDatabase initializes the trie database with default settings, namely // the legacy hash-based scheme is used by default. func NewDatabase(diskdb ethdb.Database) *Database { From 8dd6cfd5c1ce6f5276aad3a31f6b5a910c722fc7 Mon Sep 17 00:00:00 2001 From: ganeshvanahalli Date: Tue, 24 Oct 2023 11:29:10 -0500 Subject: [PATCH 19/20] 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 20/20] 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 {