2020-04-01 18:49:21 +00:00
|
|
|
package ante_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-01 17:14:33 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-05-31 09:05:32 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2021-04-21 13:41:30 +00:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2020-04-01 18:49:21 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-05-31 09:05:32 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/app"
|
|
|
|
ante "github.com/tharsis/ethermint/app/ante"
|
|
|
|
"github.com/tharsis/ethermint/encoding"
|
|
|
|
"github.com/tharsis/ethermint/tests"
|
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2021-10-19 08:49:29 +00:00
|
|
|
feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types"
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-04-01 18:49:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AnteTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
clientCtx client.Context
|
|
|
|
anteHandler sdk.AnteHandler
|
|
|
|
ethSigner ethtypes.Signer
|
|
|
|
dynamicTxFee bool
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AnteTestSuite) SetupTest() {
|
|
|
|
checkTx := false
|
2021-10-19 08:49:29 +00:00
|
|
|
|
|
|
|
if suite.dynamicTxFee {
|
|
|
|
// setup feemarketGenesis params
|
|
|
|
feemarketGenesis := feemarkettypes.DefaultGenesisState()
|
|
|
|
feemarketGenesis.Params.EnableHeight = 1
|
|
|
|
feemarketGenesis.Params.NoBaseFee = false
|
|
|
|
feemarketGenesis.BaseFee = sdk.NewInt(feemarketGenesis.Params.InitialBaseFee)
|
|
|
|
suite.app = app.Setup(checkTx, feemarketGenesis)
|
|
|
|
} else {
|
|
|
|
suite.app = app.Setup(checkTx, nil)
|
|
|
|
}
|
|
|
|
|
2021-08-17 14:11:26 +00:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 2, ChainID: "ethermint_9000-1", Time: time.Now().UTC()})
|
2021-05-31 09:05:32 +00:00
|
|
|
suite.ctx = suite.ctx.WithMinGasPrices(sdk.NewDecCoins(sdk.NewDecCoin(evmtypes.DefaultEVMDenom, sdk.OneInt())))
|
|
|
|
suite.ctx = suite.ctx.WithBlockGasMeter(sdk.NewGasMeter(1000000000000000000))
|
|
|
|
suite.app.EvmKeeper.WithChainID(suite.ctx)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
infCtx := suite.ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
suite.app.AccountKeeper.SetParams(infCtx, authtypes.DefaultParams())
|
|
|
|
suite.app.EvmKeeper.SetParams(infCtx, evmtypes.DefaultParams())
|
2020-09-02 19:41:05 +00:00
|
|
|
|
2021-06-04 13:45:37 +00:00
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
2021-04-17 10:00:07 +00:00
|
|
|
// We're using TestMsg amino encoding in some tests, so register it here.
|
2021-05-31 09:05:32 +00:00
|
|
|
encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
|
|
|
|
|
|
|
|
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-10-04 14:58:06 +00:00
|
|
|
suite.anteHandler = ante.NewAnteHandler(
|
|
|
|
suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.EvmKeeper, suite.app.FeeGrantKeeper,
|
|
|
|
suite.app.IBCKeeper.ChannelKeeper, suite.app.FeeMarketKeeper,
|
|
|
|
encodingConfig.TxConfig.SignModeHandler(),
|
|
|
|
)
|
2021-06-01 17:14:33 +00:00
|
|
|
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnteTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AnteTestSuite))
|
|
|
|
}
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
// CreateTestTx is a helper function to create a tx given multiple inputs.
|
|
|
|
func (suite *AnteTestSuite) CreateTestTx(
|
2021-06-21 10:44:37 +00:00
|
|
|
msg *evmtypes.MsgEthereumTx, priv cryptotypes.PrivKey, accNum uint64, signCosmosTx bool,
|
2021-12-23 16:07:23 +00:00
|
|
|
unsetExtensionOptions ...bool,
|
2021-05-31 09:05:32 +00:00
|
|
|
) authsigning.Tx {
|
2021-06-21 10:44:37 +00:00
|
|
|
return suite.CreateTestTxBuilder(msg, priv, accNum, signCosmosTx).GetTx()
|
2021-06-21 07:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTestTxBuilder is a helper function to create a tx builder given multiple inputs.
|
|
|
|
func (suite *AnteTestSuite) CreateTestTxBuilder(
|
2021-06-21 10:44:37 +00:00
|
|
|
msg *evmtypes.MsgEthereumTx, priv cryptotypes.PrivKey, accNum uint64, signCosmosTx bool,
|
2021-12-23 16:07:23 +00:00
|
|
|
unsetExtensionOptions ...bool,
|
2021-06-21 07:59:45 +00:00
|
|
|
) client.TxBuilder {
|
2021-12-23 16:07:23 +00:00
|
|
|
var option *codectypes.Any
|
|
|
|
var err error
|
|
|
|
if len(unsetExtensionOptions) == 0 {
|
|
|
|
option, err = codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
txBuilder := suite.clientCtx.TxConfig.NewTxBuilder()
|
|
|
|
builder, ok := txBuilder.(authtx.ExtensionOptionsTxBuilder)
|
2021-05-31 09:05:32 +00:00
|
|
|
suite.Require().True(ok)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-12-23 16:07:23 +00:00
|
|
|
if len(unsetExtensionOptions) == 0 {
|
|
|
|
builder.SetExtensionOptions(option)
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-06-01 17:14:33 +00:00
|
|
|
err = msg.Sign(suite.ethSigner, tests.NewSigner(priv))
|
2021-05-31 09:05:32 +00:00
|
|
|
suite.Require().NoError(err)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
err = builder.SetMsgs(msg)
|
2021-06-15 17:39:41 +00:00
|
|
|
suite.Require().NoError(err)
|
2021-07-05 16:39:08 +00:00
|
|
|
|
|
|
|
txData, err := evmtypes.UnpackTxData(msg.Data)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
fees := sdk.NewCoins(sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewIntFromBigInt(txData.Fee())))
|
2021-05-31 09:05:32 +00:00
|
|
|
builder.SetFeeAmount(fees)
|
|
|
|
builder.SetGasLimit(msg.GetGas())
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
if signCosmosTx {
|
|
|
|
// First round: we gather all the signer infos. We use the "set empty
|
|
|
|
// signature" hack to do that.
|
|
|
|
sigV2 := signing.SignatureV2{
|
|
|
|
PubKey: priv.PubKey(),
|
|
|
|
Data: &signing.SingleSignatureData{
|
|
|
|
SignMode: suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(),
|
|
|
|
Signature: nil,
|
|
|
|
},
|
2021-07-05 16:39:08 +00:00
|
|
|
Sequence: txData.GetNonce(),
|
2021-06-21 10:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sigsV2 := []signing.SignatureV2{sigV2}
|
|
|
|
|
|
|
|
err = txBuilder.SetSignatures(sigsV2...)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Second round: all signer infos are set, so each signer can sign.
|
|
|
|
|
|
|
|
signerData := authsigning.SignerData{
|
|
|
|
ChainID: suite.ctx.ChainID(),
|
|
|
|
AccountNumber: accNum,
|
2021-07-05 16:39:08 +00:00
|
|
|
Sequence: txData.GetNonce(),
|
2021-06-21 10:44:37 +00:00
|
|
|
}
|
|
|
|
sigV2, err = tx.SignWithPrivKey(
|
|
|
|
suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), signerData,
|
2021-07-05 16:39:08 +00:00
|
|
|
txBuilder, priv, suite.clientCtx.TxConfig, txData.GetNonce(),
|
2021-06-21 10:44:37 +00:00
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
sigsV2 = []signing.SignatureV2{sigV2}
|
|
|
|
|
|
|
|
err = txBuilder.SetSignatures(sigsV2...)
|
|
|
|
suite.Require().NoError(err)
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
return txBuilder
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2021-05-12 13:08:31 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
var _ sdk.Tx = &invalidTx{}
|
|
|
|
|
|
|
|
type invalidTx struct{}
|
|
|
|
|
|
|
|
func (invalidTx) GetMsgs() []sdk.Msg { return []sdk.Msg{nil} }
|
|
|
|
func (invalidTx) ValidateBasic() error { return nil }
|