2020-04-01 18:49:21 +00:00
|
|
|
package ante_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2021-06-21 07:59:45 +00:00
|
|
|
"strings"
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2020-04-01 18:49:21 +00:00
|
|
|
)
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
func (suite AnteTestSuite) TestAnteHandler() {
|
|
|
|
addr, privKey := newTestAddrKey()
|
|
|
|
to, _ := newTestAddrKey()
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
|
|
|
|
suite.Require().NoError(acc.SetSequence(1))
|
2020-04-01 18:49:21 +00:00
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
err := suite.app.BankKeeper.SetBalance(suite.ctx, addr.Bytes(), sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(10000000000)))
|
2020-04-23 15:49:25 +00:00
|
|
|
suite.Require().NoError(err)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2021-06-01 17:14:33 +00:00
|
|
|
txFn func() sdk.Tx
|
2021-05-31 09:05:32 +00:00
|
|
|
checkTx bool
|
|
|
|
reCheckTx bool
|
|
|
|
expPass bool
|
|
|
|
}{
|
2021-06-01 17:14:33 +00:00
|
|
|
{
|
|
|
|
"success - DeliverTx (contract)",
|
|
|
|
func() sdk.Tx {
|
|
|
|
signedContractTx := evmtypes.NewMsgEthereumTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
|
|
|
signedContractTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
tx := suite.CreateTestTx(signedContractTx, privKey, 1, true)
|
2021-06-01 17:14:33 +00:00
|
|
|
return tx
|
|
|
|
},
|
|
|
|
false, false, true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success - CheckTx (contract)",
|
|
|
|
func() sdk.Tx {
|
2021-06-02 08:52:53 +00:00
|
|
|
signedContractTx := evmtypes.NewMsgEthereumTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
2021-06-01 17:14:33 +00:00
|
|
|
signedContractTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
tx := suite.CreateTestTx(signedContractTx, privKey, 1, true)
|
2021-06-01 17:14:33 +00:00
|
|
|
return tx
|
|
|
|
},
|
|
|
|
true, false, true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success - ReCheckTx (contract)",
|
|
|
|
func() sdk.Tx {
|
2021-06-02 08:52:53 +00:00
|
|
|
signedContractTx := evmtypes.NewMsgEthereumTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
2021-06-01 17:14:33 +00:00
|
|
|
signedContractTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
tx := suite.CreateTestTx(signedContractTx, privKey, 1, true)
|
2021-06-01 17:14:33 +00:00
|
|
|
return tx
|
|
|
|
},
|
|
|
|
false, true, true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success - DeliverTx",
|
|
|
|
func() sdk.Tx {
|
2021-06-02 08:52:53 +00:00
|
|
|
signedTx := evmtypes.NewMsgEthereumTx(suite.app.EvmKeeper.ChainID(), 1, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
2021-06-01 17:14:33 +00:00
|
|
|
signedTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
tx := suite.CreateTestTx(signedTx, privKey, 1, true)
|
2021-06-01 17:14:33 +00:00
|
|
|
return tx
|
|
|
|
},
|
|
|
|
false, false, true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success - CheckTx",
|
|
|
|
func() sdk.Tx {
|
2021-06-02 08:52:53 +00:00
|
|
|
signedTx := evmtypes.NewMsgEthereumTx(suite.app.EvmKeeper.ChainID(), 2, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
2021-06-01 17:14:33 +00:00
|
|
|
signedTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
tx := suite.CreateTestTx(signedTx, privKey, 1, true)
|
2021-06-01 17:14:33 +00:00
|
|
|
return tx
|
|
|
|
},
|
|
|
|
true, false, true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"success - ReCheckTx",
|
|
|
|
func() sdk.Tx {
|
2021-06-02 08:52:53 +00:00
|
|
|
signedTx := evmtypes.NewMsgEthereumTx(suite.app.EvmKeeper.ChainID(), 3, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
2021-06-01 17:14:33 +00:00
|
|
|
signedTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
tx := suite.CreateTestTx(signedTx, privKey, 1, true)
|
2021-06-01 17:14:33 +00:00
|
|
|
return tx
|
|
|
|
}, false, true, true,
|
|
|
|
},
|
2021-06-21 10:44:37 +00:00
|
|
|
{
|
|
|
|
"success - CheckTx (cosmos tx not signed)",
|
|
|
|
func() sdk.Tx {
|
|
|
|
signedTx := evmtypes.NewMsgEthereumTx(suite.app.EvmKeeper.ChainID(), 4, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
|
|
|
signedTx.From = addr.Hex()
|
|
|
|
|
|
|
|
tx := suite.CreateTestTx(signedTx, privKey, 1, false)
|
|
|
|
return tx
|
|
|
|
}, false, true, true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail - CheckTx (cosmos tx is not valid)",
|
|
|
|
func() sdk.Tx {
|
|
|
|
signedTx := evmtypes.NewMsgEthereumTx(suite.app.EvmKeeper.ChainID(), 4, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
|
|
|
signedTx.From = addr.Hex()
|
|
|
|
|
|
|
|
txBuilder := suite.CreateTestTxBuilder(signedTx, privKey, 1, false)
|
|
|
|
// bigger than MaxGasWanted
|
|
|
|
txBuilder.SetGasLimit(uint64(1 << 63))
|
|
|
|
return txBuilder.GetTx()
|
|
|
|
}, false, true, false,
|
|
|
|
},
|
2021-06-21 07:59:45 +00:00
|
|
|
{
|
|
|
|
"fail - CheckTx (memo too long)",
|
|
|
|
func() sdk.Tx {
|
2021-06-21 10:44:37 +00:00
|
|
|
signedTx := evmtypes.NewMsgEthereumTx(suite.app.EvmKeeper.ChainID(), 5, &to, big.NewInt(10), 100000, big.NewInt(1), nil, nil)
|
2021-06-21 07:59:45 +00:00
|
|
|
signedTx.From = addr.Hex()
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
txBuilder := suite.CreateTestTxBuilder(signedTx, privKey, 1, true)
|
2021-06-21 07:59:45 +00:00
|
|
|
txBuilder.SetMemo(strings.Repeat("*", 257))
|
|
|
|
return txBuilder.GetTx()
|
|
|
|
}, true, false, false,
|
|
|
|
},
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
suite.ctx = suite.ctx.WithIsCheckTx(tc.reCheckTx).WithIsReCheckTx(tc.reCheckTx)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-06-01 17:14:33 +00:00
|
|
|
// expConsumed := params.TxGasContractCreation + params.TxGas
|
|
|
|
_, err := suite.anteHandler(suite.ctx, tc.txFn(), false)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
// suite.Require().Equal(consumed, ctx.GasMeter().GasConsumed())
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
if tc.expPass {
|
|
|
|
suite.Require().NoError(err)
|
2021-06-01 17:14:33 +00:00
|
|
|
// suite.Require().Equal(int(expConsumed), int(suite.ctx.GasMeter().GasConsumed()))
|
2021-05-31 09:05:32 +00:00
|
|
|
} else {
|
|
|
|
suite.Require().Error(err)
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-23 15:49:25 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|