From 8b1d992b259869f5ca95618f32be3b75ae95cf9f Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Mon, 4 Sep 2023 02:11:07 +0530 Subject: [PATCH] added hard fork logic for PR #1849 --- state/executor.go | 9 ++++----- state/londonFix_fork.go | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/state/executor.go b/state/executor.go index aa2c97af0b..539ee64443 100644 --- a/state/executor.go +++ b/state/executor.go @@ -453,7 +453,7 @@ func (t *Transition) ContextPtr() *runtime.TxContext { } func (t *Transition) subGasLimitPrice(msg *types.Transaction) error { - upfrontGasCost := new(big.Int).Mul(new(big.Int).SetUint64(msg.Gas), msg.GetGasPrice(t.ctx.BaseFee.Uint64())) + upfrontGasCost := GetLondonFixHandler(uint64(t.ctx.Number)).getUpfrontGasCost(msg, t.ctx.BaseFee) if err := t.state.SubBalance(msg.From, upfrontGasCost); err != nil { if errors.Is(err, runtime.ErrNotEnoughFunds) { @@ -600,10 +600,9 @@ func (t *Transition) apply(msg *types.Transaction) (*runtime.ExecutionResult, er // Define effective tip based on tx type. // 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 { - effectiveTip = msg.EffectiveGasTip(t.ctx.BaseFee) - } + effectiveTip := GetLondonFixHandler(uint64(t.ctx.Number)).getEffectiveTip( + msg, gasPrice, t.ctx.BaseFee, t.config.London, + ) // Pay the coinbase fee as a miner reward using the calculated effective tip. coinbaseFee := new(big.Int).Mul(new(big.Int).SetUint64(result.GasUsed), effectiveTip) diff --git a/state/londonFix_fork.go b/state/londonFix_fork.go index 1f6772d3d3..e896a2fe39 100644 --- a/state/londonFix_fork.go +++ b/state/londonFix_fork.go @@ -2,8 +2,10 @@ package state import ( "fmt" + "math/big" "github.com/0xPolygon/polygon-edge/forkmanager" + "github.com/0xPolygon/polygon-edge/helper/common" "github.com/0xPolygon/polygon-edge/types" ) @@ -11,6 +13,9 @@ const LondonFixHandler forkmanager.HandlerDesc = "LondonFixHandler" type LondonFixFork interface { checkDynamicFees(*types.Transaction, *Transition) error + getUpfrontGasCost(msg *types.Transaction, baseFee *big.Int) *big.Int + getEffectiveTip(msg *types.Transaction, gasPrice *big.Int, + baseFee *big.Int, isLondonForkEnabled bool) *big.Int } type LondonFixForkV1 struct{} @@ -52,6 +57,33 @@ func (l *LondonFixForkV1) checkDynamicFees(msg *types.Transaction, t *Transition return nil } +func (l *LondonFixForkV1) getUpfrontGasCost(msg *types.Transaction, baseFee *big.Int) *big.Int { + 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) + } + + return upfrontGasCost.Mul(upfrontGasCost, factor) +} + +func (l *LondonFixForkV1) getEffectiveTip(msg *types.Transaction, gasPrice *big.Int, + baseFee *big.Int, isLondonForkEnabled bool) *big.Int { + if isLondonForkEnabled && msg.Type == types.DynamicFeeTx { + return common.BigMin( + new(big.Int).Sub(msg.GasFeeCap, baseFee), + new(big.Int).Set(msg.GasTipCap), + ) + } + + return new(big.Int).Set(gasPrice) +} + type LondonFixForkV2 struct{} func (l *LondonFixForkV2) checkDynamicFees(msg *types.Transaction, t *Transition) error { @@ -90,6 +122,19 @@ func (l *LondonFixForkV2) checkDynamicFees(msg *types.Transaction, t *Transition return nil } +func (l *LondonFixForkV2) getUpfrontGasCost(msg *types.Transaction, baseFee *big.Int) *big.Int { + return new(big.Int).Mul(new(big.Int).SetUint64(msg.Gas), msg.GetGasPrice(baseFee.Uint64())) +} + +func (l *LondonFixForkV2) getEffectiveTip(msg *types.Transaction, gasPrice *big.Int, + baseFee *big.Int, isLondonForkEnabled bool) *big.Int { + if isLondonForkEnabled { + return msg.EffectiveGasTip(baseFee) + } + + return new(big.Int).Set(gasPrice) +} + func RegisterLondonFixFork(londonFixFork string) error { fh := forkmanager.GetInstance()