2021-08-25 14:45:51 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CalculateBaseFee calculates the base fee for the current block. This is only calculated once per
|
2021-10-15 08:59:26 +00:00
|
|
|
// block during EndBlock. If the NoBaseFee parameter is enabled or below activation height, this function returns nil.
|
2021-08-25 14:45:51 +00:00
|
|
|
// NOTE: This code is inspired from the go-ethereum EIP1559 implementation and adapted to Cosmos SDK-based
|
|
|
|
// chains. For the canonical code refer to: https://github.com/ethereum/go-ethereum/blob/master/consensus/misc/eip1559.go
|
|
|
|
func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
2021-10-15 08:59:26 +00:00
|
|
|
// Ignore the calculation if not enable
|
|
|
|
if !params.IsBaseFeeEnabled(ctx.BlockHeight()) {
|
2021-08-25 14:45:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:08:11 +00:00
|
|
|
consParams := ctx.ConsensusParams()
|
2021-08-25 14:45:51 +00:00
|
|
|
|
|
|
|
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
|
2021-10-15 08:59:26 +00:00
|
|
|
if ctx.BlockHeight() == params.EnableHeight {
|
2021-08-26 10:08:11 +00:00
|
|
|
return new(big.Int).SetInt64(params.InitialBaseFee)
|
2021-08-25 14:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the block gas used and the base fee values for the parent block.
|
|
|
|
parentBaseFee := k.GetBaseFee(ctx)
|
2021-08-26 10:08:11 +00:00
|
|
|
if parentBaseFee == nil {
|
|
|
|
parentBaseFee = new(big.Int).SetInt64(params.InitialBaseFee)
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:45:51 +00:00
|
|
|
parentGasUsed := k.GetBlockGasUsed(ctx)
|
|
|
|
|
|
|
|
gasLimit := new(big.Int).SetUint64(math.MaxUint64)
|
|
|
|
if consParams != nil && consParams.Block.MaxGas > -1 {
|
|
|
|
gasLimit = big.NewInt(consParams.Block.MaxGas)
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:08:11 +00:00
|
|
|
parentGasTargetBig := new(big.Int).Div(gasLimit, new(big.Int).SetUint64(uint64(params.ElasticityMultiplier)))
|
2021-08-25 14:45:51 +00:00
|
|
|
if !parentGasTargetBig.IsUint64() {
|
2021-08-26 10:08:11 +00:00
|
|
|
return new(big.Int).SetInt64(params.InitialBaseFee)
|
2021-08-25 14:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parentGasTarget := parentGasTargetBig.Uint64()
|
2021-08-26 10:08:11 +00:00
|
|
|
baseFeeChangeDenominator := new(big.Int).SetUint64(uint64(params.BaseFeeChangeDenominator))
|
2021-08-25 14:45:51 +00:00
|
|
|
|
|
|
|
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
|
|
|
|
if parentGasUsed == parentGasTarget {
|
|
|
|
return new(big.Int).Set(parentBaseFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentGasUsed > parentGasTarget {
|
|
|
|
// If the parent block used more gas than its target, the baseFee should increase.
|
|
|
|
gasUsedDelta := new(big.Int).SetUint64(parentGasUsed - parentGasTarget)
|
|
|
|
x := new(big.Int).Mul(parentBaseFee, gasUsedDelta)
|
|
|
|
y := x.Div(x, parentGasTargetBig)
|
|
|
|
baseFeeDelta := math.BigMax(
|
|
|
|
x.Div(y, baseFeeChangeDenominator),
|
|
|
|
common.Big1,
|
|
|
|
)
|
|
|
|
|
|
|
|
return x.Add(parentBaseFee, baseFeeDelta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
|
|
|
|
gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parentGasUsed)
|
|
|
|
x := new(big.Int).Mul(parentBaseFee, gasUsedDelta)
|
|
|
|
y := x.Div(x, parentGasTargetBig)
|
|
|
|
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)
|
|
|
|
|
|
|
|
return math.BigMax(
|
|
|
|
x.Sub(parentBaseFee, baseFeeDelta),
|
|
|
|
common.Big0,
|
|
|
|
)
|
|
|
|
}
|