2020-04-01 18:49:21 +00:00
|
|
|
package ante_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-06-01 17:14:33 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-05-31 09:05:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
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
|
|
|
|
|
|
|
"github.com/cosmos/ethermint/app"
|
|
|
|
ante "github.com/cosmos/ethermint/app/ante"
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
2021-06-04 13:45:37 +00:00
|
|
|
"github.com/cosmos/ethermint/encoding"
|
2021-05-12 13:08:31 +00:00
|
|
|
"github.com/cosmos/ethermint/tests"
|
2020-04-01 18:49:21 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
|
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-05-31 09:05:32 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
clientCtx client.Context
|
|
|
|
anteHandler sdk.AnteHandler
|
2021-06-01 17:14:33 +00:00
|
|
|
ethSigner ethtypes.Signer
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AnteTestSuite) SetupTest() {
|
|
|
|
checkTx := false
|
|
|
|
suite.app = app.Setup(checkTx)
|
2021-05-31 09:05:32 +00:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 2, ChainID: "ethermint-1", Time: time.Now().UTC()})
|
|
|
|
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-05-31 09:05:32 +00:00
|
|
|
suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.EvmKeeper, 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-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-06-21 07:59:45 +00:00
|
|
|
) client.TxBuilder {
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
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-05-31 09:05:32 +00:00
|
|
|
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-05-31 09:05:32 +00:00
|
|
|
fees := sdk.NewCoins(sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewIntFromBigInt(msg.Fee())))
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
Sequence: msg.Data.Nonce,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Sequence: msg.Data.Nonce,
|
|
|
|
}
|
|
|
|
sigV2, err = tx.SignWithPrivKey(
|
|
|
|
suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), signerData,
|
|
|
|
txBuilder, priv, suite.clientCtx.TxConfig, msg.Data.Nonce,
|
|
|
|
)
|
|
|
|
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
|
|
|
func newTestAddrKey() (common.Address, cryptotypes.PrivKey) {
|
|
|
|
privkey, _ := ethsecp256k1.GenerateKey()
|
|
|
|
addr := crypto.PubkeyToAddress(privkey.ToECDSA().PublicKey)
|
2020-04-23 15:49:25 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
return addr, privkey
|
2020-04-01 18:49:21 +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 }
|