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-12-15 02:10:52 +00:00
|
|
|
// block during BeginBlock. 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)
|
|
|
|
|
2022-04-26 14:24:32 +00:00
|
|
|
// Ignore the calculation if not enabled
|
2021-10-15 08:59:26 +00:00
|
|
|
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
|
|
|
|
2022-04-26 14:24:32 +00:00
|
|
|
// If the current block is the first EIP-1559 block, return the base fee
|
|
|
|
// defined in the parameters (DefaultBaseFee if it hasn't been changed by
|
|
|
|
// governance).
|
2021-10-15 08:59:26 +00:00
|
|
|
if ctx.BlockHeight() == params.EnableHeight {
|
2022-02-23 18:48:44 +00:00
|
|
|
return params.BaseFee.BigInt()
|
2021-08-25 14:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the block gas used and the base fee values for the parent block.
|
2022-04-26 14:24:32 +00:00
|
|
|
// NOTE: this is not the parent's base fee but the current block's base fee,
|
|
|
|
// as it is retrieved from the transient store, which is committed to the
|
|
|
|
// persistent KVStore after EndBlock (ABCI Commit).
|
2022-02-23 18:48:44 +00:00
|
|
|
parentBaseFee := params.BaseFee.BigInt()
|
2021-08-26 10:08:11 +00:00
|
|
|
if parentBaseFee == nil {
|
2022-02-23 18:48:44 +00:00
|
|
|
return nil
|
2021-08-26 10:08:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 09:22:33 +00:00
|
|
|
parentGasUsed := k.GetBlockGasWanted(ctx)
|
2021-08-25 14:45:51 +00:00
|
|
|
|
|
|
|
gasLimit := new(big.Int).SetUint64(math.MaxUint64)
|
2022-04-26 14:24:32 +00:00
|
|
|
|
|
|
|
// NOTE: a MaxGas equal to -1 means that block gas is unlimited
|
2021-08-25 14:45:51 +00:00
|
|
|
if consParams != nil && consParams.Block.MaxGas > -1 {
|
|
|
|
gasLimit = big.NewInt(consParams.Block.MaxGas)
|
|
|
|
}
|
|
|
|
|
2022-04-26 14:24:32 +00:00
|
|
|
// CONTRACT: ElasticityMultiplier cannot be 0 as it's checked in the params
|
|
|
|
// validation
|
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() {
|
2022-02-23 18:48:44 +00:00
|
|
|
return nil
|
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
|
|
|
|
2022-06-22 10:57:16 +00:00
|
|
|
// If the parent gasUsed is the same as the target, the baseFee remains
|
|
|
|
// unchanged.
|
2021-08-25 14:45:51 +00:00
|
|
|
if parentGasUsed == parentGasTarget {
|
|
|
|
return new(big.Int).Set(parentBaseFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentGasUsed > parentGasTarget {
|
2022-06-22 10:57:16 +00:00
|
|
|
// If the parent block used more gas than its target, the baseFee should
|
|
|
|
// increase.
|
2021-08-25 14:45:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:57:16 +00:00
|
|
|
// Otherwise if the parent block used less gas than its target, the baseFee
|
|
|
|
// should decrease.
|
2021-08-25 14:45:51 +00:00
|
|
|
gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parentGasUsed)
|
|
|
|
x := new(big.Int).Mul(parentBaseFee, gasUsedDelta)
|
|
|
|
y := x.Div(x, parentGasTargetBig)
|
|
|
|
baseFeeDelta := x.Div(y, baseFeeChangeDenominator)
|
|
|
|
|
2022-06-22 10:57:16 +00:00
|
|
|
// Set global min gas price as lower bound of the base fee, transactions below
|
|
|
|
// the min gas price don't even reach the mempool.
|
|
|
|
minGasPrice := params.MinGasPrice.TruncateInt().BigInt()
|
|
|
|
return math.BigMax(x.Sub(parentBaseFee, baseFeeDelta), minGasPrice)
|
2021-08-25 14:45:51 +00:00
|
|
|
}
|