forked from cerc-io/laconicd-deprecated
update fork
This commit is contained in:
@@ -16,31 +16,40 @@ import (
|
||||
func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {
|
||||
params := k.GetParams(ctx)
|
||||
|
||||
// Ignore the calculation if not enable
|
||||
// Ignore the calculation if not enabled
|
||||
if !params.IsBaseFeeEnabled(ctx.BlockHeight()) {
|
||||
return nil
|
||||
}
|
||||
|
||||
consParams := ctx.ConsensusParams()
|
||||
|
||||
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
|
||||
// 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).
|
||||
if ctx.BlockHeight() == params.EnableHeight {
|
||||
return params.BaseFee.BigInt()
|
||||
}
|
||||
|
||||
// get the block gas used and the base fee values for the parent block.
|
||||
// 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).
|
||||
parentBaseFee := params.BaseFee.BigInt()
|
||||
if parentBaseFee == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
parentGasUsed := k.GetBlockGasUsed(ctx)
|
||||
parentGasUsed := k.GetBlockGasWanted(ctx)
|
||||
|
||||
gasLimit := new(big.Int).SetUint64(math.MaxUint64)
|
||||
|
||||
// NOTE: a MaxGas equal to -1 means that block gas is unlimited
|
||||
if consParams != nil && consParams.Block.MaxGas > -1 {
|
||||
gasLimit = big.NewInt(consParams.Block.MaxGas)
|
||||
}
|
||||
|
||||
// CONTRACT: ElasticityMultiplier cannot be 0 as it's checked in the params
|
||||
// validation
|
||||
parentGasTargetBig := new(big.Int).Div(gasLimit, new(big.Int).SetUint64(uint64(params.ElasticityMultiplier)))
|
||||
if !parentGasTargetBig.IsUint64() {
|
||||
return nil
|
||||
@@ -49,13 +58,15 @@ func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {
|
||||
parentGasTarget := parentGasTargetBig.Uint64()
|
||||
baseFeeChangeDenominator := new(big.Int).SetUint64(uint64(params.BaseFeeChangeDenominator))
|
||||
|
||||
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
|
||||
// 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.
|
||||
// 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)
|
||||
@@ -67,14 +78,15 @@ func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int {
|
||||
return x.Add(parentBaseFee, baseFeeDelta)
|
||||
}
|
||||
|
||||
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
|
||||
// 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,
|
||||
)
|
||||
// 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user