From 5bcedac5e04f9bf09d5696c1182250fbaa9b6afc Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Thu, 24 Aug 2023 22:44:01 +0530 Subject: [PATCH 1/4] corrected upfront gas deduction for type-2 transactions --- state/executor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state/executor.go b/state/executor.go index 4d31ff5ed1..57d1cf5da7 100644 --- a/state/executor.go +++ b/state/executor.go @@ -450,7 +450,7 @@ func (t *Transition) subGasLimitPrice(msg *types.Transaction) error { factor := new(big.Int) if msg.GasFeeCap != nil && msg.GasFeeCap.BitLen() > 0 { // Apply EIP-1559 tx cost calculation factor - factor = factor.Set(msg.GasFeeCap) + factor = factor.Set(msg.GetGasPrice(t.ctx.BaseFee.Uint64())) } else { // Apply legacy tx cost calculation factor factor = factor.Set(msg.GasPrice) From d37eace010555c0befd6380aae9cfd663e2edd81 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Fri, 25 Aug 2023 01:28:43 +0530 Subject: [PATCH 2/4] solve the bug reported in EVM-804 --- state/executor.go | 16 +++++++++++----- txpool/txpool.go | 6 ++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/state/executor.go b/state/executor.go index 57d1cf5da7..1d939fc386 100644 --- a/state/executor.go +++ b/state/executor.go @@ -634,11 +634,17 @@ func (t *Transition) apply(msg *types.Transaction) (*runtime.ExecutionResult, er // We use EIP-1559 fields of the tx if the london hardfork is enabled. // Effective tip became to be either gas tip cap or (gas fee cap - current base fee) effectiveTip := new(big.Int).Set(gasPrice) - if t.config.London && msg.Type == types.DynamicFeeTx { - effectiveTip = common.BigMin( - new(big.Int).Sub(msg.GasFeeCap, t.ctx.BaseFee), - new(big.Int).Set(msg.GasTipCap), - ) + + if t.config.London { + if msg.Type == types.DynamicFeeTx { + effectiveTip = common.BigMin( + new(big.Int).Sub(msg.GasFeeCap, t.ctx.BaseFee), + new(big.Int).Set(msg.GasTipCap), + ) + } else { + // legacy tx + effectiveTip.Sub(gasPrice, t.ctx.BaseFee) + } } // Pay the coinbase fee as a miner reward using the calculated effective tip. diff --git a/txpool/txpool.go b/txpool/txpool.go index 03461dea92..9b856db6bb 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -661,6 +661,12 @@ func (p *TxPool) validateTx(tx *types.Transaction) error { } } else { // Legacy approach to check if the given tx is not underpriced + if forks.London && tx.GasPrice.Cmp(new(big.Int).SetUint64(baseFee)) < 0 { + metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) + + return ErrUnderpriced + } + if tx.GetGasPrice(baseFee).Cmp(big.NewInt(0).SetUint64(p.priceLimit)) < 0 { metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) From 779ccbdba81b24a812369cfa61b1dc333846c6c0 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Fri, 25 Aug 2023 21:57:12 +0530 Subject: [PATCH 3/4] code optimization --- state/executor.go | 25 ++----------------------- txpool/txpool.go | 6 +----- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/state/executor.go b/state/executor.go index 1d939fc386..5d6fbe79db 100644 --- a/state/executor.go +++ b/state/executor.go @@ -11,7 +11,6 @@ import ( "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/contracts" "github.com/0xPolygon/polygon-edge/crypto" - "github.com/0xPolygon/polygon-edge/helper/common" "github.com/0xPolygon/polygon-edge/state/runtime" "github.com/0xPolygon/polygon-edge/state/runtime/addresslist" "github.com/0xPolygon/polygon-edge/state/runtime/evm" @@ -445,18 +444,7 @@ func (t *Transition) ContextPtr() *runtime.TxContext { } func (t *Transition) subGasLimitPrice(msg *types.Transaction) error { - upfrontGasCost := new(big.Int).SetUint64(msg.Gas) - - factor := new(big.Int) - if msg.GasFeeCap != nil && msg.GasFeeCap.BitLen() > 0 { - // Apply EIP-1559 tx cost calculation factor - factor = factor.Set(msg.GetGasPrice(t.ctx.BaseFee.Uint64())) - } else { - // Apply legacy tx cost calculation factor - factor = factor.Set(msg.GasPrice) - } - - upfrontGasCost = upfrontGasCost.Mul(upfrontGasCost, factor) + upfrontGasCost := new(big.Int).Mul(new(big.Int).SetUint64(msg.Gas), msg.GetGasPrice(t.ctx.BaseFee.Uint64())) if err := t.state.SubBalance(msg.From, upfrontGasCost); err != nil { if errors.Is(err, runtime.ErrNotEnoughFunds) { @@ -634,17 +622,8 @@ func (t *Transition) apply(msg *types.Transaction) (*runtime.ExecutionResult, er // We use EIP-1559 fields of the tx if the london hardfork is enabled. // Effective tip became to be either gas tip cap or (gas fee cap - current base fee) effectiveTip := new(big.Int).Set(gasPrice) - if t.config.London { - if msg.Type == types.DynamicFeeTx { - effectiveTip = common.BigMin( - new(big.Int).Sub(msg.GasFeeCap, t.ctx.BaseFee), - new(big.Int).Set(msg.GasTipCap), - ) - } else { - // legacy tx - effectiveTip.Sub(gasPrice, t.ctx.BaseFee) - } + effectiveTip = msg.EffectiveGasTip(t.ctx.BaseFee) } // Pay the coinbase fee as a miner reward using the calculated effective tip. diff --git a/txpool/txpool.go b/txpool/txpool.go index 069a8b9b8d..90c96e6f6c 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -660,16 +660,12 @@ func (p *TxPool) validateTx(tx *types.Transaction) error { return ErrUnderpriced } } else { - // Legacy approach to check if the given tx is not underpriced + // Legacy approach to check if the given tx is not underpriced when london hardfork is enabled if forks.London && tx.GasPrice.Cmp(new(big.Int).SetUint64(baseFee)) < 0 { metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) return ErrUnderpriced } - - if tx.GetGasPrice(baseFee).Cmp(big.NewInt(0).SetUint64(p.priceLimit)) < 0 { - metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) - } } // Check if the given tx is not underpriced From 727e27764bf21d056aac76829ca93c4fe53d7173 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Fri, 25 Aug 2023 23:12:37 +0530 Subject: [PATCH 4/4] testCase fix --- state/transition_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/state/transition_test.go b/state/transition_test.go index de94c84cb0..d8e507c8be 100644 --- a/state/transition_test.go +++ b/state/transition_test.go @@ -18,6 +18,7 @@ func newTestTransition(preState map[types.Address]*PreState) *Transition { return &Transition{ logger: hclog.NewNullLogger(), state: newTestTxn(preState), + ctx: runtime.TxContext{BaseFee: big.NewInt(0)}, } }