Skip to content

Commit

Permalink
test(evm): evmante fee checker coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev committed Jun 10, 2024
1 parent f807e6f commit 93ca583
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 5 deletions.
28 changes: 23 additions & 5 deletions app/evmante_fee_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
// - Tx priority is set to `effectiveGasPrice / DefaultPriorityReduction`.
func NewDynamicFeeChecker(k evmkeeper.Keeper) ante.TxFeeChecker {
return func(ctx sdk.Context, feeTx sdk.FeeTx) (sdk.Coins, int64, error) {
// TODO: in the e2e test, if the fee in the genesis transaction meet the baseFee and minGasPrice in the feemarket, we can remove this code
// TODO: in the e2e test,
// if the fee in the genesis transaction meet the baseFee and minGasPrice in the feemarket,
// we can remove this code
if ctx.BlockHeight() == 0 {
// genesis transactions: fallback to min-gas-price logic
return checkTxFeeWithValidatorMinGasPrices(ctx, feeTx)
Expand All @@ -51,7 +53,10 @@ func NewDynamicFeeChecker(k evmkeeper.Keeper) ante.TxFeeChecker {

// priority fee cannot be negative
if maxPriorityPrice.IsNegative() {
return nil, 0, errors.Wrapf(errortypes.ErrInsufficientFee, "max priority price cannot be negative")
return nil, 0, errors.Wrapf(
errortypes.ErrInsufficientFee,
"max priority price cannot be negative",
)
}

gas := feeTx.GetGas()
Expand All @@ -62,11 +67,21 @@ func NewDynamicFeeChecker(k evmkeeper.Keeper) ante.TxFeeChecker {
baseFeeInt := sdkmath.NewIntFromBigInt(baseFee)

if feeCap.LT(baseFeeInt) {
return nil, 0, errors.Wrapf(errortypes.ErrInsufficientFee, "gas prices too low, got: %s%s required: %s%s. Please retry using a higher gas price or a higher fee", feeCap, denom, baseFeeInt, denom)
return nil, 0, errors.Wrapf(
errortypes.ErrInsufficientFee,
"gas prices too low, got: %s%s required: %s%s. "+
"Please retry using a higher gas price or a higher fee",
feeCap,
denom,
baseFeeInt,
denom,
)
}

// calculate the effective gas price using the EIP-1559 logic.
effectivePrice := sdkmath.NewIntFromBigInt(evm.EffectiveGasPrice(baseFeeInt.BigInt(), feeCap.BigInt(), maxPriorityPrice.BigInt()))
effectivePrice := sdkmath.NewIntFromBigInt(
evm.EffectiveGasPrice(baseFeeInt.BigInt(), feeCap.BigInt(), maxPriorityPrice.BigInt()),
)

// NOTE: create a new coins slice without having to validate the denom
effectiveFee := sdk.Coins{
Expand Down Expand Up @@ -109,7 +124,10 @@ func checkTxFeeWithValidatorMinGasPrices(ctx sdk.Context, tx sdk.FeeTx) (sdk.Coi
}

if !feeCoins.IsAnyGTE(requiredFees) {
return nil, 0, errors.Wrapf(errortypes.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeCoins, requiredFees)
return nil, 0, errors.Wrapf(
errortypes.ErrInsufficientFee,
"insufficient fees; got: %s required: %s", feeCoins, requiredFees,
)
}
}

Expand Down
97 changes: 97 additions & 0 deletions app/evmante_fee_checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package app_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
)

func (s *TestSuite) TestNewDynamicFeeChecker() {
testCases := []struct {
name string
txSetup func(deps *evmtest.TestDeps) sdk.FeeTx
ctxSetup func(deps *evmtest.TestDeps)
wantErr string
wantFee int64
wantPriority int64
}{
{
name: "happy: genesis tx with sufficient fee",
ctxSetup: func(deps *evmtest.TestDeps) {
gasPrice := sdk.NewInt64Coin("unibi", 1)
deps.Ctx = deps.Ctx.
WithBlockHeight(0).
WithMinGasPrices(
sdk.NewDecCoins(sdk.NewDecCoinFromCoin(gasPrice)),
).
WithIsCheckTx(true)
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()
tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)
return tx
},
wantErr: "",
wantFee: gasLimitCreateContract().Int64(),
wantPriority: 0,
},
{
name: "sad: genesis tx insufficient fee",
ctxSetup: func(deps *evmtest.TestDeps) {
gasPrice := sdk.NewInt64Coin("unibi", 2)
deps.Ctx = deps.Ctx.
WithBlockHeight(0).
WithMinGasPrices(
sdk.NewDecCoins(sdk.NewDecCoinFromCoin(gasPrice)),
).
WithIsCheckTx(true)
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()
tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)
return tx
},
wantErr: "insufficient fee",
},
{
name: "happy: tx with sufficient fee",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()
tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)
return tx
},
wantErr: "",
wantFee: gasLimitCreateContract().Int64(),
wantPriority: 0,
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
checker := app.NewDynamicFeeChecker(deps.K)

if tc.ctxSetup != nil {
tc.ctxSetup(&deps)
}

fee, priority, err := checker(deps.Ctx, tc.txSetup(&deps))

if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
s.Require().Equal(tc.wantFee, fee.AmountOf("unibi").Int64())
s.Require().Equal(tc.wantPriority, priority)
})
}
}

0 comments on commit 93ca583

Please sign in to comment.