2021-09-03 15:55:37 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2023-01-04 11:04:38 +00:00
|
|
|
"github.com/cerc-io/laconicd/x/evm/keeper"
|
2022-09-07 06:36:11 +00:00
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2021-09-03 15:55:37 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-10-19 08:49:29 +00:00
|
|
|
ethparams "github.com/ethereum/go-ethereum/params"
|
2021-09-03 15:55:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestCheckSenderBalance() {
|
2022-10-10 10:38:33 +00:00
|
|
|
hundredInt := sdkmath.NewInt(100)
|
2021-09-03 15:55:37 +00:00
|
|
|
zeroInt := sdk.ZeroInt()
|
2021-09-21 10:29:59 +00:00
|
|
|
oneInt := sdk.OneInt()
|
2022-10-10 10:38:33 +00:00
|
|
|
fiveInt := sdkmath.NewInt(5)
|
|
|
|
fiftyInt := sdkmath.NewInt(50)
|
|
|
|
negInt := sdkmath.NewInt(-10)
|
2021-09-03 15:55:37 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-12-28 07:59:28 +00:00
|
|
|
name string
|
|
|
|
to string
|
|
|
|
gasLimit uint64
|
2022-10-10 10:38:33 +00:00
|
|
|
gasPrice *sdkmath.Int
|
2021-12-28 07:59:28 +00:00
|
|
|
gasFeeCap *big.Int
|
|
|
|
gasTipCap *big.Int
|
2022-10-10 10:38:33 +00:00
|
|
|
cost *sdkmath.Int
|
2021-12-28 07:59:28 +00:00
|
|
|
from string
|
|
|
|
accessList *ethtypes.AccessList
|
|
|
|
expectPass bool
|
|
|
|
enableFeemarket bool
|
2021-09-03 15:55:37 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 99,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
},
|
2021-09-21 10:29:59 +00:00
|
|
|
{
|
|
|
|
name: "negative cost",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 1,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &negInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
},
|
2021-09-03 15:55:37 +00:00
|
|
|
{
|
|
|
|
name: "Higher gas limit, not enough balance",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 100,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Higher gas price, enough balance",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasPrice: &fiveInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Higher gas price, not enough balance",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 20,
|
|
|
|
gasPrice: &fiveInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Higher cost, enough balance",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasPrice: &fiveInt,
|
|
|
|
cost: &fiftyInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Higher cost, not enough balance",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasPrice: &fiveInt,
|
|
|
|
cost: &hundredInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
},
|
2021-10-19 08:49:29 +00:00
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Enough balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasFeeCap: big.NewInt(1),
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Equal balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 99,
|
|
|
|
gasFeeCap: big.NewInt(1),
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "negative cost w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 1,
|
|
|
|
gasFeeCap: big.NewInt(1),
|
|
|
|
cost: &negInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Higher gas limit, not enough balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 100,
|
|
|
|
gasFeeCap: big.NewInt(1),
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Higher gas price, enough balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasFeeCap: big.NewInt(5),
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Higher gas price, not enough balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 20,
|
|
|
|
gasFeeCap: big.NewInt(5),
|
|
|
|
cost: &oneInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Higher cost, enough balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasFeeCap: big.NewInt(5),
|
|
|
|
cost: &fiftyInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: true,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 07:59:28 +00:00
|
|
|
name: "Higher cost, not enough balance w/ enableFeemarket",
|
|
|
|
to: suite.address.String(),
|
|
|
|
gasLimit: 10,
|
|
|
|
gasFeeCap: big.NewInt(5),
|
|
|
|
cost: &hundredInt,
|
|
|
|
from: suite.address.String(),
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPass: false,
|
|
|
|
enableFeemarket: true,
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
2021-09-03 15:55:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
|
|
|
vmdb.AddBalance(suite.address, hundredInt.BigInt())
|
|
|
|
balance := vmdb.GetBalance(suite.address)
|
2021-09-03 15:55:37 +00:00
|
|
|
suite.Require().Equal(balance, hundredInt.BigInt())
|
2022-11-30 17:00:19 +00:00
|
|
|
err := vmdb.Commit()
|
|
|
|
suite.Require().NoError(err, "Unexpected error while committing to vmdb: %d", err)
|
2021-09-03 15:55:37 +00:00
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
to := common.HexToAddress(tc.from)
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
var amount, gasPrice, gasFeeCap, gasTipCap *big.Int
|
2021-09-03 15:55:37 +00:00
|
|
|
if tc.cost != nil {
|
|
|
|
amount = tc.cost.BigInt()
|
|
|
|
}
|
2021-10-19 08:49:29 +00:00
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
if tc.enableFeemarket {
|
2021-10-19 08:49:29 +00:00
|
|
|
gasFeeCap = tc.gasFeeCap
|
|
|
|
if tc.gasTipCap == nil {
|
|
|
|
gasTipCap = oneInt.BigInt()
|
|
|
|
} else {
|
|
|
|
gasTipCap = tc.gasTipCap
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if tc.gasPrice != nil {
|
|
|
|
gasPrice = tc.gasPrice.BigInt()
|
|
|
|
}
|
2021-09-03 15:55:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
tx := evmtypes.NewTx(zeroInt.BigInt(), 1, &to, amount, tc.gasLimit, gasPrice, gasFeeCap, gasTipCap, nil, tc.accessList)
|
2021-09-03 15:55:37 +00:00
|
|
|
tx.From = tc.from
|
|
|
|
|
|
|
|
txData, _ := evmtypes.UnpackTxData(tx.Data)
|
|
|
|
|
2022-01-05 18:18:02 +00:00
|
|
|
acct := suite.app.EvmKeeper.GetAccountOrEmpty(suite.ctx, suite.address)
|
2022-11-30 17:00:19 +00:00
|
|
|
err := keeper.CheckSenderBalance(
|
2022-10-10 10:38:33 +00:00
|
|
|
sdkmath.NewIntFromBigInt(acct.Balance),
|
2021-09-03 15:55:37 +00:00
|
|
|
txData,
|
|
|
|
)
|
|
|
|
|
|
|
|
if tc.expectPass {
|
|
|
|
suite.Require().NoError(err, "valid test %d failed", i)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err, "invalid test %d passed", i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:00:19 +00:00
|
|
|
// 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() {
|
2022-10-10 10:38:33 +00:00
|
|
|
hundredInt := sdkmath.NewInt(100)
|
2021-09-03 15:55:37 +00:00
|
|
|
zeroInt := sdk.ZeroInt()
|
2022-10-10 10:38:33 +00:00
|
|
|
oneInt := sdkmath.NewInt(1)
|
|
|
|
fiveInt := sdkmath.NewInt(5)
|
|
|
|
fiftyInt := sdkmath.NewInt(50)
|
2021-12-15 02:17:03 +00:00
|
|
|
|
|
|
|
// should be enough to cover all test cases
|
2022-10-10 10:38:33 +00:00
|
|
|
initBalance := sdkmath.NewInt((ethparams.InitialBaseFee + 10) * 105)
|
2021-09-03 15:55:37 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2022-11-30 17:00:19 +00:00
|
|
|
name string
|
|
|
|
gasLimit uint64
|
|
|
|
gasPrice *sdkmath.Int
|
|
|
|
gasFeeCap *big.Int
|
|
|
|
gasTipCap *big.Int
|
|
|
|
cost *sdkmath.Int
|
|
|
|
accessList *ethtypes.AccessList
|
|
|
|
expectPassVerify bool
|
|
|
|
expectPassDeduct bool
|
|
|
|
enableFeemarket bool
|
|
|
|
from string
|
|
|
|
malleate func()
|
2021-09-03 15:55:37 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Enough balance",
|
|
|
|
gasLimit: 10,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
from: suite.address.String(),
|
2021-09-03 15:55:37 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Equal balance",
|
|
|
|
gasLimit: 100,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
from: suite.address.String(),
|
2021-09-03 15:55:37 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Higher gas limit, not enough balance",
|
|
|
|
gasLimit: 105,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: false,
|
|
|
|
from: suite.address.String(),
|
2021-09-03 15:55:37 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Higher gas price, enough balance",
|
|
|
|
gasLimit: 20,
|
|
|
|
gasPrice: &fiveInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
from: suite.address.String(),
|
2021-09-03 15:55:37 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Higher gas price, not enough balance",
|
|
|
|
gasLimit: 20,
|
|
|
|
gasPrice: &fiftyInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: false,
|
|
|
|
from: suite.address.String(),
|
2021-09-03 15:55:37 +00:00
|
|
|
},
|
|
|
|
// 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
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Higher cost, enough balance",
|
|
|
|
gasLimit: 100,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &fiftyInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
from: suite.address.String(),
|
2021-09-03 15:55:37 +00:00
|
|
|
},
|
2021-12-28 07:59:28 +00:00
|
|
|
// testcases with enableFeemarket enabled.
|
2021-10-19 08:49:29 +00:00
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Invalid gasFeeCap w/ enableFeemarket",
|
|
|
|
gasLimit: 10,
|
|
|
|
gasFeeCap: big.NewInt(1),
|
|
|
|
gasTipCap: big.NewInt(1),
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: false,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
enableFeemarket: true,
|
|
|
|
from: suite.address.String(),
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "empty tip fee is valid to deduct",
|
|
|
|
gasLimit: 10,
|
|
|
|
gasFeeCap: big.NewInt(ethparams.InitialBaseFee),
|
|
|
|
gasTipCap: big.NewInt(1),
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
enableFeemarket: true,
|
|
|
|
from: suite.address.String(),
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "effectiveTip equal to gasTipCap",
|
|
|
|
gasLimit: 100,
|
|
|
|
gasFeeCap: big.NewInt(ethparams.InitialBaseFee + 2),
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
enableFeemarket: true,
|
|
|
|
from: suite.address.String(),
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "effectiveTip equal to (gasFeeCap - baseFee)",
|
|
|
|
gasLimit: 105,
|
|
|
|
gasFeeCap: big.NewInt(ethparams.InitialBaseFee + 1),
|
|
|
|
gasTipCap: big.NewInt(2),
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
enableFeemarket: true,
|
|
|
|
from: suite.address.String(),
|
2022-10-21 16:59:03 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "Invalid from address",
|
|
|
|
gasLimit: 10,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: false,
|
|
|
|
from: "abcdef",
|
2022-10-21 16:59:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Enough balance - with access list",
|
|
|
|
gasLimit: 10,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{
|
|
|
|
ethtypes.AccessTuple{
|
|
|
|
Address: suite.address,
|
|
|
|
StorageKeys: []common.Hash{},
|
|
|
|
},
|
|
|
|
},
|
2022-11-30 17:00:19 +00:00
|
|
|
expectPassVerify: true,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
from: suite.address.String(),
|
2022-10-21 16:59:03 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-30 17:00:19 +00:00
|
|
|
name: "gasLimit < intrinsicGas during IsCheckTx",
|
|
|
|
gasLimit: 1,
|
|
|
|
gasPrice: &oneInt,
|
|
|
|
cost: &oneInt,
|
|
|
|
accessList: ðtypes.AccessList{},
|
|
|
|
expectPassVerify: false,
|
|
|
|
expectPassDeduct: true,
|
|
|
|
from: suite.address.String(),
|
2022-10-21 16:59:03 +00:00
|
|
|
malleate: func() {
|
|
|
|
suite.ctx = suite.ctx.WithIsCheckTx(true)
|
|
|
|
},
|
2021-10-19 08:49:29 +00:00
|
|
|
},
|
2021-09-03 15:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite.enableFeemarket = tc.enableFeemarket
|
2021-09-03 15:55:37 +00:00
|
|
|
suite.SetupTest()
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := suite.StateDB()
|
2021-09-03 15:55:37 +00:00
|
|
|
|
2022-10-21 16:59:03 +00:00
|
|
|
if tc.malleate != nil {
|
|
|
|
tc.malleate()
|
|
|
|
}
|
2021-10-19 08:49:29 +00:00
|
|
|
var amount, gasPrice, gasFeeCap, gasTipCap *big.Int
|
2021-09-03 15:55:37 +00:00
|
|
|
if tc.cost != nil {
|
|
|
|
amount = tc.cost.BigInt()
|
|
|
|
}
|
2021-10-19 08:49:29 +00:00
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
if suite.enableFeemarket {
|
2021-10-19 08:49:29 +00:00
|
|
|
if tc.gasFeeCap != nil {
|
|
|
|
gasFeeCap = tc.gasFeeCap
|
|
|
|
}
|
|
|
|
if tc.gasTipCap == nil {
|
|
|
|
gasTipCap = oneInt.BigInt()
|
|
|
|
} else {
|
|
|
|
gasTipCap = tc.gasTipCap
|
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.AddBalance(suite.address, initBalance.BigInt())
|
|
|
|
balance := vmdb.GetBalance(suite.address)
|
2021-12-15 02:17:03 +00:00
|
|
|
suite.Require().Equal(balance, initBalance.BigInt())
|
2021-10-19 08:49:29 +00:00
|
|
|
} else {
|
|
|
|
if tc.gasPrice != nil {
|
|
|
|
gasPrice = tc.gasPrice.BigInt()
|
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb.AddBalance(suite.address, hundredInt.BigInt())
|
|
|
|
balance := vmdb.GetBalance(suite.address)
|
2021-10-19 08:49:29 +00:00
|
|
|
suite.Require().Equal(balance, hundredInt.BigInt())
|
2021-09-03 15:55:37 +00:00
|
|
|
}
|
2022-11-30 17:00:19 +00:00
|
|
|
err := vmdb.Commit()
|
|
|
|
suite.Require().NoError(err, "Unexpected error while committing to vmdb: %d", err)
|
2021-09-03 15:55:37 +00:00
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
tx := evmtypes.NewTx(zeroInt.BigInt(), 1, &suite.address, amount, tc.gasLimit, gasPrice, gasFeeCap, gasTipCap, nil, tc.accessList)
|
2022-10-21 16:59:03 +00:00
|
|
|
tx.From = tc.from
|
2021-09-03 15:55:37 +00:00
|
|
|
|
|
|
|
txData, _ := evmtypes.UnpackTxData(tx.Data)
|
|
|
|
|
2023-01-23 19:01:28 +00:00
|
|
|
evmParams := suite.app.EvmKeeper.GetParams(suite.ctx)
|
|
|
|
ethCfg := evmParams.GetChainConfig().EthereumConfig(nil)
|
2022-11-30 17:00:19 +00:00
|
|
|
baseFee := suite.app.EvmKeeper.GetBaseFee(suite.ctx, ethCfg)
|
|
|
|
priority := evmtypes.GetTxPriority(txData, baseFee)
|
2021-09-03 15:55:37 +00:00
|
|
|
|
2022-11-30 17:00:19 +00:00
|
|
|
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)
|
2021-12-28 07:59:28 +00:00
|
|
|
if tc.enableFeemarket {
|
2021-10-19 08:49:29 +00:00
|
|
|
baseFee := suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx)
|
|
|
|
suite.Require().Equal(
|
|
|
|
fees,
|
|
|
|
sdk.NewCoins(
|
2022-10-10 10:38:33 +00:00
|
|
|
sdk.NewCoin(evmtypes.DefaultEVMDenom, sdkmath.NewIntFromBigInt(txData.EffectiveFee(baseFee))),
|
2021-10-19 08:49:29 +00:00
|
|
|
),
|
2022-11-30 17:00:19 +00:00
|
|
|
"valid test %d failed, fee value is wrong - '%s'", i, tc.name,
|
2021-10-19 08:49:29 +00:00
|
|
|
)
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.Require().Equal(int64(0), priority)
|
2021-10-19 08:49:29 +00:00
|
|
|
} else {
|
|
|
|
suite.Require().Equal(
|
|
|
|
fees,
|
|
|
|
sdk.NewCoins(
|
2022-10-10 10:38:33 +00:00
|
|
|
sdk.NewCoin(evmtypes.DefaultEVMDenom, tc.gasPrice.Mul(sdkmath.NewIntFromUint64(tc.gasLimit))),
|
2021-10-19 08:49:29 +00:00
|
|
|
),
|
2022-11-30 17:00:19 +00:00
|
|
|
"valid test %d failed, fee value is wrong - '%s'", i, tc.name,
|
2021-10-19 08:49:29 +00:00
|
|
|
)
|
|
|
|
}
|
2021-09-03 15:55:37 +00:00
|
|
|
} else {
|
2022-11-30 17:00:19 +00:00
|
|
|
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.Require().NoError(err, "valid test %d failed - '%s'", i, tc.name)
|
|
|
|
} else {
|
|
|
|
suite.Require().Error(err, "invalid test %d passed - '%s'", i, tc.name)
|
2021-09-03 15:55:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-28 07:59:28 +00:00
|
|
|
suite.enableFeemarket = false // reset flag
|
2021-09-03 15:55:37 +00:00
|
|
|
}
|