|
| 1 | +package evmante_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + gethcommon "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/NibiruChain/nibiru/v2/x/evm" |
| 11 | + "github.com/NibiruChain/nibiru/v2/x/evm/evmante" |
| 12 | + "github.com/NibiruChain/nibiru/v2/x/evm/evmtest" |
| 13 | + "github.com/NibiruChain/nibiru/v2/x/sudo" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAnteStepZeroGasBlockQuota_PerBlockTxCount(t *testing.T) { |
| 17 | + deps := evmtest.NewTestDeps() |
| 18 | + |
| 19 | + // Configure ZeroGasActors with an always_zero_gas_contracts entry. |
| 20 | + targetAddr := gethcommon.HexToAddress("0x2222222222222222222222222222222222222222") |
| 21 | + deps.App.SudoKeeper.ZeroGasActors.Set(deps.Ctx(), sudo.ZeroGasActors{ |
| 22 | + AlwaysZeroGasContracts: []string{targetAddr.Hex()}, |
| 23 | + }) |
| 24 | + |
| 25 | + // Create a tx that targets the allowlisted contract with zero value. |
| 26 | + to := targetAddr |
| 27 | + tx := evm.NewTx(&evm.EvmTxArgs{ |
| 28 | + ChainID: deps.App.EvmKeeper.EthChainID(deps.Ctx()), |
| 29 | + Nonce: 0, |
| 30 | + GasLimit: 50_000, |
| 31 | + GasPrice: big.NewInt(0), |
| 32 | + To: &to, |
| 33 | + Amount: big.NewInt(0), |
| 34 | + }) |
| 35 | + tx.From = deps.Sender.EthAddr.Hex() |
| 36 | + |
| 37 | + // Fill the quota within the same block. |
| 38 | + for i := uint64(0); i < 100; i++ { |
| 39 | + sdb := deps.NewStateDB() |
| 40 | + err := evmante.AnteStepDetectZeroGas( |
| 41 | + sdb, sdb.Keeper(), tx, false, ANTE_OPTIONS_UNUSED, |
| 42 | + ) |
| 43 | + require.NoError(t, err) |
| 44 | + require.True(t, evm.IsZeroGasEthTx(sdb.Ctx())) |
| 45 | + |
| 46 | + err = evmante.AnteStepZeroGasBlockQuota( |
| 47 | + sdb, sdb.Keeper(), tx, false, ANTE_OPTIONS_UNUSED, |
| 48 | + ) |
| 49 | + require.NoError(t, err) |
| 50 | + deps.Commit() |
| 51 | + } |
| 52 | + |
| 53 | + // The next zero-gas tx in the same block should fail. |
| 54 | + { |
| 55 | + sdb := deps.NewStateDB() |
| 56 | + err := evmante.AnteStepDetectZeroGas( |
| 57 | + sdb, sdb.Keeper(), tx, false, ANTE_OPTIONS_UNUSED, |
| 58 | + ) |
| 59 | + require.NoError(t, err) |
| 60 | + require.True(t, evm.IsZeroGasEthTx(sdb.Ctx())) |
| 61 | + |
| 62 | + err = evmante.AnteStepZeroGasBlockQuota( |
| 63 | + sdb, sdb.Keeper(), tx, false, ANTE_OPTIONS_UNUSED, |
| 64 | + ) |
| 65 | + require.Error(t, err) |
| 66 | + require.ErrorContains(t, err, "zero-gas block quota exceeded") |
| 67 | + } |
| 68 | +} |
0 commit comments