Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

corrected upfront gas deduction for type-2 transactions #1849

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.GasFeeCap)
} 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) {
Expand Down Expand Up @@ -634,11 +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 && 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 {
effectiveTip = msg.EffectiveGasTip(t.ctx.BaseFee)
}

// Pay the coinbase fee as a miner reward using the calculated effective tip.
Expand Down
1 change: 1 addition & 0 deletions state/transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
}
}

Expand Down
7 changes: 7 additions & 0 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,13 @@ func (p *TxPool) validateTx(tx *types.Transaction) error {
if tx.GasFeeCap.Cmp(new(big.Int).SetUint64(baseFee)) < 0 {
metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1)

return ErrUnderpriced
}
} else {
// 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 {
begmaroman marked this conversation as resolved.
Show resolved Hide resolved
metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1)

return ErrUnderpriced
}
}
Expand Down
Loading