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-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
|
|
|
|
txBuilder client.TxBuilder
|
|
|
|
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-05-31 09:05:32 +00:00
|
|
|
encodingConfig := app.MakeEncodingConfig()
|
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)
|
|
|
|
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
|
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(
|
|
|
|
msg *evmtypes.MsgEthereumTx, priv cryptotypes.PrivKey, accNum uint64,
|
|
|
|
) authsigning.Tx {
|
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-05-31 09:05:32 +00:00
|
|
|
builder, ok := suite.txBuilder.(authtx.ExtensionOptionsTxBuilder)
|
|
|
|
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)
|
|
|
|
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-05-31 09:05:32 +00:00
|
|
|
// 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 = suite.txBuilder.SetSignatures(sigsV2...)
|
|
|
|
suite.Require().NoError(err)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
// Second round: all signer infos are set, so each signer can sign.
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
signerData := authsigning.SignerData{
|
|
|
|
ChainID: suite.ctx.ChainID(),
|
|
|
|
AccountNumber: accNum,
|
|
|
|
Sequence: msg.Data.Nonce,
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2021-05-31 09:05:32 +00:00
|
|
|
sigV2, err = tx.SignWithPrivKey(
|
|
|
|
suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), signerData,
|
|
|
|
suite.txBuilder, priv, suite.clientCtx.TxConfig, msg.Data.Nonce,
|
|
|
|
)
|
|
|
|
suite.Require().NoError(err)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
sigsV2 = []signing.SignatureV2{sigV2}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
err = suite.txBuilder.SetSignatures(sigsV2...)
|
|
|
|
suite.Require().NoError(err)
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
return suite.txBuilder.GetTx()
|
|
|
|
}
|
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 }
|