From b57e7819d5cab99573337412b9a1969dee6927db Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Tue, 10 Oct 2023 12:19:44 +0530 Subject: [PATCH] removed london fork fix --- chain/params.go | 6 +- server/server.go | 5 -- state/executor.go | 45 +++++++++-- state/londonFix_fork.go | 164 ---------------------------------------- 4 files changed, 41 insertions(+), 179 deletions(-) delete mode 100644 state/londonFix_fork.go diff --git a/chain/params.go b/chain/params.go index 1630362aa0..b794e6ae43 100644 --- a/chain/params.go +++ b/chain/params.go @@ -89,7 +89,6 @@ const ( EIP155 = "EIP155" QuorumCalcAlignment = "quorumcalcalignment" TxHashWithType = "txHashWithType" - LondonFix = "londonfix" ) // Forks is map which contains all forks and their starting blocks from genesis @@ -125,7 +124,6 @@ func (f *Forks) At(block uint64) ForksInTime { EIP155: f.IsActive(EIP155, block), QuorumCalcAlignment: f.IsActive(QuorumCalcAlignment, block), TxHashWithType: f.IsActive(TxHashWithType, block), - LondonFix: f.IsActive(LondonFix, block), } } @@ -177,8 +175,7 @@ type ForksInTime struct { EIP158, EIP155, QuorumCalcAlignment, - TxHashWithType, - LondonFix bool + TxHashWithType bool } // AllForksEnabled should contain all supported forks by current edge version @@ -194,5 +191,4 @@ var AllForksEnabled = &Forks{ London: NewFork(0), QuorumCalcAlignment: NewFork(0), TxHashWithType: NewFork(0), - LondonFix: NewFork(0), } diff --git a/server/server.go b/server/server.go index 394ed93226..c8d46146f4 100644 --- a/server/server.go +++ b/server/server.go @@ -1062,11 +1062,6 @@ func initForkManager(engineName string, config *chain.Chain) error { return err } - // Register Handler for London fork fix - if err := state.RegisterLondonFixFork(chain.LondonFix); err != nil { - return err - } - if factory := forkManagerFactory[ConsensusType(engineName)]; factory != nil { if err := factory(config.Params.Forks); err != nil { return err diff --git a/state/executor.go b/state/executor.go index 0bcf46510a..d42befd5b7 100644 --- a/state/executor.go +++ b/state/executor.go @@ -444,7 +444,7 @@ func (t *Transition) ContextPtr() *runtime.TxContext { } func (t *Transition) subGasLimitPrice(msg *types.Transaction) error { - upfrontGasCost := GetLondonFixHandler(uint64(t.ctx.Number)).getUpfrontGasCost(msg, t.ctx.BaseFee) + 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) { @@ -470,7 +470,39 @@ func (t *Transition) nonceCheck(msg *types.Transaction) error { // checkDynamicFees checks correctness of the EIP-1559 feature-related fields. // Basically, makes sure gas tip cap and gas fee cap are good for dynamic and legacy transactions func (t *Transition) checkDynamicFees(msg *types.Transaction) error { - return GetLondonFixHandler(uint64(t.ctx.Number)).checkDynamicFees(msg, t) + if !t.config.London { + return nil + } + + if msg.Type == types.DynamicFeeTx { + if msg.GasFeeCap.BitLen() == 0 && msg.GasTipCap.BitLen() == 0 { + return nil + } + + if l := msg.GasFeeCap.BitLen(); l > 256 { + return fmt.Errorf("%w: address %v, GasFeeCap bit length: %d", ErrFeeCapVeryHigh, + msg.From.String(), l) + } + + if l := msg.GasTipCap.BitLen(); l > 256 { + return fmt.Errorf("%w: address %v, GasTipCap bit length: %d", ErrTipVeryHigh, + msg.From.String(), l) + } + + if msg.GasFeeCap.Cmp(msg.GasTipCap) < 0 { + return fmt.Errorf("%w: address %v, GasTipCap: %s, GasFeeCap: %s", ErrTipAboveFeeCap, + msg.From.String(), msg.GasTipCap, msg.GasFeeCap) + } + } + + // This will panic if baseFee is nil, but basefee presence is verified + // as part of header validation. + if gasFeeCap := msg.GetGasFeeCap(); gasFeeCap.Cmp(t.ctx.BaseFee) < 0 { + return fmt.Errorf("%w: address %v, GasFeeCap/GasPrice: %s, BaseFee: %s", ErrFeeCapTooLow, + msg.From.String(), gasFeeCap, t.ctx.BaseFee) + } + + return nil } // errors that can originate in the consensus rules checks of the apply method below @@ -596,9 +628,12 @@ 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 := GetLondonFixHandler(uint64(t.ctx.Number)).getEffectiveTip( - msg, gasPrice, t.ctx.BaseFee, t.config.London, - ) + var effectiveTip *big.Int + if t.config.London { + effectiveTip = msg.EffectiveGasTip(t.ctx.BaseFee) + } else { + effectiveTip = new(big.Int).Set(gasPrice) + } // 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 deleted file mode 100644 index ca8c6dd900..0000000000 --- a/state/londonFix_fork.go +++ /dev/null @@ -1,164 +0,0 @@ -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" -) - -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{} - -// checkDynamicFees checks correctness of the EIP-1559 feature-related fields. -// Basically, makes sure gas tip cap and gas fee cap are good for dynamic and legacy transactions -// and that GasFeeCap/GasPrice cap is not lower than base fee when London fork is active. -func (l *LondonFixForkV1) checkDynamicFees(msg *types.Transaction, t *Transition) error { - if msg.Type != types.DynamicFeeTx { - return nil - } - - if msg.GasFeeCap.BitLen() == 0 && msg.GasTipCap.BitLen() == 0 { - return nil - } - - if l := msg.GasFeeCap.BitLen(); l > 256 { - return fmt.Errorf("%w: address %v, GasFeeCap bit length: %d", ErrFeeCapVeryHigh, - msg.From.String(), l) - } - - if l := msg.GasTipCap.BitLen(); l > 256 { - return fmt.Errorf("%w: address %v, GasTipCap bit length: %d", ErrTipVeryHigh, - msg.From.String(), l) - } - - if msg.GasFeeCap.Cmp(msg.GasTipCap) < 0 { - return fmt.Errorf("%w: address %v, GasTipCap: %s, GasFeeCap: %s", ErrTipAboveFeeCap, - msg.From.String(), msg.GasTipCap, msg.GasFeeCap) - } - - // This will panic if baseFee is nil, but basefee presence is verified - // as part of header validation. - if msg.GasFeeCap.Cmp(t.ctx.BaseFee) < 0 { - return fmt.Errorf("%w: address %v, GasFeeCap: %s, BaseFee: %s", ErrFeeCapTooLow, - msg.From.String(), msg.GasFeeCap, t.ctx.BaseFee) - } - - 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 { - if !t.config.London { - return nil - } - - if msg.Type == types.DynamicFeeTx { - if msg.GasFeeCap.BitLen() == 0 && msg.GasTipCap.BitLen() == 0 { - return nil - } - - if l := msg.GasFeeCap.BitLen(); l > 256 { - return fmt.Errorf("%w: address %v, GasFeeCap bit length: %d", ErrFeeCapVeryHigh, - msg.From.String(), l) - } - - if l := msg.GasTipCap.BitLen(); l > 256 { - return fmt.Errorf("%w: address %v, GasTipCap bit length: %d", ErrTipVeryHigh, - msg.From.String(), l) - } - - if msg.GasFeeCap.Cmp(msg.GasTipCap) < 0 { - return fmt.Errorf("%w: address %v, GasTipCap: %s, GasFeeCap: %s", ErrTipAboveFeeCap, - msg.From.String(), msg.GasTipCap, msg.GasFeeCap) - } - } - - // This will panic if baseFee is nil, but basefee presence is verified - // as part of header validation. - if gasFeeCap := msg.GetGasFeeCap(); gasFeeCap.Cmp(t.ctx.BaseFee) < 0 { - return fmt.Errorf("%w: address %v, GasFeeCap/GasPrice: %s, BaseFee: %s", ErrFeeCapTooLow, - msg.From.String(), gasFeeCap, t.ctx.BaseFee) - } - - 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() - - if err := fh.RegisterHandler( - forkmanager.InitialFork, LondonFixHandler, &LondonFixForkV1{}); err != nil { - return err - } - - if fh.IsForkRegistered(londonFixFork) { - if err := fh.RegisterHandler( - londonFixFork, LondonFixHandler, &LondonFixForkV2{}); err != nil { - return err - } - } - - return nil -} - -func GetLondonFixHandler(blockNumber uint64) LondonFixFork { - if h := forkmanager.GetInstance().GetHandler(LondonFixHandler, blockNumber); h != nil { - //nolint:forcetypeassert - return h.(LondonFixFork) - } - - // for tests - return &LondonFixForkV2{} -}