From 1c12cf9b069f2b7c66a5575a331821ae682e23ab Mon Sep 17 00:00:00 2001 From: Martin HS Date: Mon, 8 Apr 2024 19:40:34 +0100 Subject: [PATCH] core/vm, params: ensure order of forks, prevent overflow (#29023) This PR fixes an overflow which can could happen if inconsistent blockchain rules were configured. Additionally, it tries to prevent such inconsistencies from occurring by making sure that merge cannot be enabled unless previous fork(s) are also enabled. --- core/vm/operations_acl.go | 7 ++++++- internal/ethapi/api_test.go | 2 +- params/config.go | 16 +++++++++------- params/config_test.go | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index bca6d1e83b..f420a24105 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -187,7 +187,12 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc { // outside of this function, as part of the dynamic gas, and that will make it // also become correctly reported to tracers. contract.Gas += coldCost - return gas + coldCost, nil + + var overflow bool + if gas, overflow = math.SafeAdd(gas, coldCost); overflow { + return 0, ErrGasUintOverflow + } + return gas, nil } } diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 80f14edcc4..37477253d1 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -1796,6 +1796,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha tx *types.Transaction err error ) + b.SetPoS() switch i { case 0: // transfer 1000wei @@ -1844,7 +1845,6 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha b.AddTx(tx) txHashes[i] = tx.Hash() } - b.SetPoS() }) return backend, txHashes } diff --git a/params/config.go b/params/config.go index 8d856b53a9..e7b3c0494b 100644 --- a/params/config.go +++ b/params/config.go @@ -1042,6 +1042,8 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules if chainID == nil { chainID = new(big.Int) } + // disallow setting Merge out of order + isMerge = isMerge && c.IsLondon(num) return Rules{ ChainID: new(big.Int).Set(chainID), IsHomestead: c.IsHomestead(num), @@ -1055,13 +1057,13 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules IsBerlin: c.IsBerlin(num), IsLondon: c.IsLondon(num), IsMerge: isMerge, - IsShanghai: c.IsShanghai(num, timestamp), - IsCancun: c.IsCancun(num, timestamp), - IsPrague: c.IsPrague(num, timestamp), - IsVerkle: c.IsVerkle(num, timestamp), + IsShanghai: isMerge && c.IsShanghai(num, timestamp), + IsCancun: isMerge && c.IsCancun(num, timestamp), + IsPrague: isMerge && c.IsPrague(num, timestamp), + IsVerkle: isMerge && c.IsVerkle(num, timestamp), // Optimism - IsOptimismBedrock: c.IsOptimismBedrock(num), - IsOptimismRegolith: c.IsOptimismRegolith(timestamp), - IsOptimismCanyon: c.IsOptimismCanyon(timestamp), + IsOptimismBedrock: isMerge && c.IsOptimismBedrock(num), + IsOptimismRegolith: isMerge && c.IsOptimismRegolith(timestamp), + IsOptimismCanyon: isMerge && c.IsOptimismCanyon(timestamp), } } diff --git a/params/config_test.go b/params/config_test.go index 14d7f833bb..b91befdf71 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -140,6 +140,7 @@ func TestConfigRules(t *testing.T) { func TestConfigRulesRegolith(t *testing.T) { c := &ChainConfig{ + LondonBlock: new(big.Int), RegolithTime: newUint64(500), Optimism: &OptimismConfig{}, }