2021-11-19 16:22:25 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-23 18:48:44 +00:00
|
|
|
"math/big"
|
2022-04-26 10:39:18 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-09-30 06:41:39 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2021-11-19 16:22:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestCalculateBaseFee() {
|
|
|
|
testCases := []struct {
|
2022-10-10 10:38:33 +00:00
|
|
|
name string
|
|
|
|
NoBaseFee bool
|
|
|
|
blockHeight int64
|
|
|
|
parentBlockGasWanted uint64
|
|
|
|
minGasPrice sdk.Dec
|
|
|
|
expFee *big.Int
|
2021-11-19 16:22:25 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"without BaseFee",
|
|
|
|
true,
|
2022-10-10 10:38:33 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
sdk.ZeroDec(),
|
2021-11-19 16:22:25 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with BaseFee - initial EIP-1559 block",
|
|
|
|
false,
|
2022-10-10 10:38:33 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
sdk.ZeroDec(),
|
2022-02-23 18:48:44 +00:00
|
|
|
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
|
2021-11-19 16:22:25 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-10 10:38:33 +00:00
|
|
|
"with BaseFee - parent block wanted the same gas as its target (ElasticityMultiplier = 2)",
|
2021-11-19 16:22:25 +00:00
|
|
|
false,
|
2022-10-10 10:38:33 +00:00
|
|
|
1,
|
|
|
|
50,
|
|
|
|
sdk.ZeroDec(),
|
2022-02-23 18:48:44 +00:00
|
|
|
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
|
2021-11-19 16:22:25 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-10 10:38:33 +00:00
|
|
|
"with BaseFee - parent block wanted the same gas as its target, with higher min gas price (ElasticityMultiplier = 2)",
|
2021-11-19 16:22:25 +00:00
|
|
|
false,
|
2022-10-10 10:38:33 +00:00
|
|
|
1,
|
|
|
|
50,
|
|
|
|
sdk.NewDec(1500000000),
|
|
|
|
suite.app.FeeMarketKeeper.GetParams(suite.ctx).BaseFee.BigInt(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with BaseFee - parent block wanted more gas than its target (ElasticityMultiplier = 2)",
|
|
|
|
false,
|
|
|
|
1,
|
|
|
|
100,
|
|
|
|
sdk.ZeroDec(),
|
2021-11-19 16:22:25 +00:00
|
|
|
big.NewInt(1125000000),
|
|
|
|
},
|
|
|
|
{
|
2022-10-10 10:38:33 +00:00
|
|
|
"with BaseFee - parent block wanted more gas than its target, with higher min gas price (ElasticityMultiplier = 2)",
|
2021-11-19 16:22:25 +00:00
|
|
|
false,
|
2022-10-10 10:38:33 +00:00
|
|
|
1,
|
|
|
|
100,
|
|
|
|
sdk.NewDec(1500000000),
|
|
|
|
big.NewInt(1125000000),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with BaseFee - Parent gas wanted smaller than parent gas target (ElasticityMultiplier = 2)",
|
|
|
|
false,
|
|
|
|
1,
|
|
|
|
25,
|
|
|
|
sdk.ZeroDec(),
|
2021-11-19 16:22:25 +00:00
|
|
|
big.NewInt(937500000),
|
|
|
|
},
|
2022-10-10 10:38:33 +00:00
|
|
|
{
|
|
|
|
"with BaseFee - Parent gas wanted smaller than parent gas target, with higher min gas price (ElasticityMultiplier = 2)",
|
|
|
|
false,
|
|
|
|
1,
|
|
|
|
25,
|
|
|
|
sdk.NewDec(1500000000),
|
|
|
|
big.NewInt(1500000000),
|
|
|
|
},
|
2021-11-19 16:22:25 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
|
|
|
|
suite.SetupTest() // reset
|
2022-10-10 10:38:33 +00:00
|
|
|
|
2021-11-19 16:22:25 +00:00
|
|
|
params := suite.app.FeeMarketKeeper.GetParams(suite.ctx)
|
|
|
|
params.NoBaseFee = tc.NoBaseFee
|
2022-10-10 10:38:33 +00:00
|
|
|
params.MinGasPrice = tc.minGasPrice
|
2021-11-19 16:22:25 +00:00
|
|
|
suite.app.FeeMarketKeeper.SetParams(suite.ctx, params)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// Set block height
|
|
|
|
suite.ctx = suite.ctx.WithBlockHeight(tc.blockHeight)
|
|
|
|
|
|
|
|
// Set parent block gas
|
|
|
|
suite.app.FeeMarketKeeper.SetBlockGasWanted(suite.ctx, tc.parentBlockGasWanted)
|
|
|
|
|
|
|
|
// Set next block target/gasLimit through Consensus Param MaxGas
|
|
|
|
blockParams := abci.BlockParams{
|
|
|
|
MaxGas: 100,
|
|
|
|
MaxBytes: 10,
|
|
|
|
}
|
|
|
|
consParams := abci.ConsensusParams{Block: &blockParams}
|
|
|
|
suite.ctx = suite.ctx.WithConsensusParams(&consParams)
|
2021-11-19 16:22:25 +00:00
|
|
|
|
|
|
|
fee := suite.app.FeeMarketKeeper.CalculateBaseFee(suite.ctx)
|
|
|
|
if tc.NoBaseFee {
|
|
|
|
suite.Require().Nil(fee, tc.name)
|
|
|
|
} else {
|
|
|
|
suite.Require().Equal(tc.expFee, fee, tc.name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|