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

Commit

Permalink
added hard fork logic for PR #1849
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit77 committed Sep 3, 2023
1 parent 4d009a1 commit 8b1d992
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
9 changes: 4 additions & 5 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions state/londonFix_fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ 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{}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 8b1d992

Please sign in to comment.