package keeper_test
import (
"math/big"
sdkmath "cosmossdk.io/math"
"github.com/cerc-io/laconicd/x/evm/keeper"
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethparams "github.com/ethereum/go-ethereum/params"
)
func (suite *KeeperTestSuite) TestCheckSenderBalance() {
hundredInt := sdkmath.NewInt(100)
zeroInt := sdk.ZeroInt()
oneInt := sdk.OneInt()
fiveInt := sdkmath.NewInt(5)
fiftyInt := sdkmath.NewInt(50)
negInt := sdkmath.NewInt(-10)
testCases := []struct {
name string
to string
gasLimit uint64
gasPrice *sdkmath.Int
gasFeeCap *big.Int
gasTipCap *big.Int
cost *sdkmath.Int
from string
accessList *ethtypes.AccessList
expectPass bool
enableFeemarket bool
}{
{
name: "Enough balance",
to: suite.address.String(),
gasLimit: 10,
gasPrice: &oneInt,
cost: &oneInt,
from: suite.address.String(),
accessList: ðtypes.AccessList{},
expectPass: true,
},
name: "Equal balance",
gasLimit: 99,
name: "negative cost",
gasLimit: 1,
cost: &negInt,
expectPass: false,
name: "Higher gas limit, not enough balance",
gasLimit: 100,
name: "Higher gas price, enough balance",
gasPrice: &fiveInt,
name: "Higher gas price, not enough balance",
gasLimit: 20,
name: "Higher cost, enough balance",
cost: &fiftyInt,
name: "Higher cost, not enough balance",
cost: &hundredInt,
name: "Enough balance w/ enableFeemarket",
gasFeeCap: big.NewInt(1),
enableFeemarket: true,
name: "Equal balance w/ enableFeemarket",
name: "negative cost w/ enableFeemarket",
name: "Higher gas limit, not enough balance w/ enableFeemarket",
name: "Higher gas price, enough balance w/ enableFeemarket",
gasFeeCap: big.NewInt(5),
name: "Higher gas price, not enough balance w/ enableFeemarket",
name: "Higher cost, enough balance w/ enableFeemarket",
name: "Higher cost, not enough balance w/ enableFeemarket",
}
vmdb := suite.StateDB()
vmdb.AddBalance(suite.address, hundredInt.BigInt())
balance := vmdb.GetBalance(suite.address)
suite.Require().Equal(balance, hundredInt.BigInt())
err := vmdb.Commit()
suite.Require().NoError(err, "Unexpected error while committing to vmdb: %d", err)
for i, tc := range testCases {
suite.Run(tc.name, func() {
to := common.HexToAddress(tc.from)
var amount, gasPrice, gasFeeCap, gasTipCap *big.Int
if tc.cost != nil {
amount = tc.cost.BigInt()
if tc.enableFeemarket {
gasFeeCap = tc.gasFeeCap
if tc.gasTipCap == nil {
gasTipCap = oneInt.BigInt()
} else {
gasTipCap = tc.gasTipCap
if tc.gasPrice != nil {
gasPrice = tc.gasPrice.BigInt()
tx := evmtypes.NewTx(zeroInt.BigInt(), 1, &to, amount, tc.gasLimit, gasPrice, gasFeeCap, gasTipCap, nil, tc.accessList)
tx.From = tc.from
txData, _ := evmtypes.UnpackTxData(tx.Data)
acct := suite.app.EvmKeeper.GetAccountOrEmpty(suite.ctx, suite.address)
err := keeper.CheckSenderBalance(
sdkmath.NewIntFromBigInt(acct.Balance),
txData,
if tc.expectPass {
suite.Require().NoError(err, "valid test %d failed", i)
suite.Require().Error(err, "invalid test %d passed", i)
})
// TestVerifyFeeAndDeductTxCostsFromUserBalance is a test method for both the VerifyFee
// function and the DeductTxCostsFromUserBalance method.
//
// NOTE: This method combines testing for both functions, because these used to be
// in one function and share a lot of the same setup.
// In practice, the two tested functions will also be sequentially executed.
func (suite *KeeperTestSuite) TestVerifyFeeAndDeductTxCostsFromUserBalance() {
oneInt := sdkmath.NewInt(1)
// should be enough to cover all test cases
initBalance := sdkmath.NewInt((ethparams.InitialBaseFee + 10) * 105)
expectPassVerify bool
expectPassDeduct bool
malleate func()
expectPassVerify: true,
expectPassDeduct: true,
gasLimit: 105,
expectPassDeduct: false,
gasPrice: &fiftyInt,
// This case is expected to be true because the fees can be deducted, but the tx
// execution is going to fail because there is no more balance to pay the cost
// testcases with enableFeemarket enabled.
name: "Invalid gasFeeCap w/ enableFeemarket",
gasTipCap: big.NewInt(1),
expectPassVerify: false,
name: "empty tip fee is valid to deduct",
gasFeeCap: big.NewInt(ethparams.InitialBaseFee),
name: "effectiveTip equal to gasTipCap",
gasFeeCap: big.NewInt(ethparams.InitialBaseFee + 2),
name: "effectiveTip equal to (gasFeeCap - baseFee)",
gasFeeCap: big.NewInt(ethparams.InitialBaseFee + 1),
gasTipCap: big.NewInt(2),
name: "Invalid from address",
from: "abcdef",
name: "Enough balance - with access list",
accessList: ðtypes.AccessList{
ethtypes.AccessTuple{
Address: suite.address,
StorageKeys: []common.Hash{},
name: "gasLimit < intrinsicGas during IsCheckTx",
malleate: func() {
suite.ctx = suite.ctx.WithIsCheckTx(true)
suite.enableFeemarket = tc.enableFeemarket
suite.SetupTest()
if tc.malleate != nil {
tc.malleate()
if suite.enableFeemarket {
if tc.gasFeeCap != nil {
vmdb.AddBalance(suite.address, initBalance.BigInt())
suite.Require().Equal(balance, initBalance.BigInt())
tx := evmtypes.NewTx(zeroInt.BigInt(), 1, &suite.address, amount, tc.gasLimit, gasPrice, gasFeeCap, gasTipCap, nil, tc.accessList)
ethCfg := suite.app.EvmKeeper.GetChainConfig(suite.ctx).EthereumConfig(nil)
baseFee := suite.app.EvmKeeper.GetBaseFee(suite.ctx, ethCfg)
priority := evmtypes.GetTxPriority(txData, baseFee)
fees, err := keeper.VerifyFee(txData, evmtypes.DefaultEVMDenom, baseFee, false, false, suite.ctx.IsCheckTx())
if tc.expectPassVerify {
suite.Require().NoError(err, "valid test %d failed - '%s'", i, tc.name)
baseFee := suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx)
suite.Require().Equal(
fees,
sdk.NewCoins(
sdk.NewCoin(evmtypes.DefaultEVMDenom, sdkmath.NewIntFromBigInt(txData.EffectiveFee(baseFee))),
),
"valid test %d failed, fee value is wrong - '%s'", i, tc.name,
suite.Require().Equal(int64(0), priority)
sdk.NewCoin(evmtypes.DefaultEVMDenom, tc.gasPrice.Mul(sdkmath.NewIntFromUint64(tc.gasLimit))),
suite.Require().Error(err, "invalid test %d passed - '%s'", i, tc.name)
suite.Require().Nil(fees, "invalid test %d passed. fees value must be nil - '%s'", i, tc.name)
err = suite.app.EvmKeeper.DeductTxCostsFromUserBalance(suite.ctx, fees, common.HexToAddress(tx.From))
if tc.expectPassDeduct {
suite.enableFeemarket = false // reset flag