2020-04-01 18:49:21 +00:00
|
|
|
package ante_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
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/simapp/params"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2020-04-01 18:49:21 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-21 13:41:30 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
|
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-04-18 17:23:26 +00:00
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
2020-04-01 18:49:21 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
|
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-04-17 10:00:07 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
encodingConfig params.EncodingConfig
|
|
|
|
anteHandler sdk.AnteHandler
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AnteTestSuite) SetupTest() {
|
|
|
|
checkTx := false
|
|
|
|
|
|
|
|
suite.app = app.Setup(checkTx)
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 2, ChainID: "injective-3", Time: time.Now().UTC()})
|
2021-04-17 10:00:07 +00:00
|
|
|
suite.app.AccountKeeper.SetParams(suite.ctx, authtypes.DefaultParams())
|
2020-09-02 19:41:05 +00:00
|
|
|
suite.app.EvmKeeper.SetParams(suite.ctx, evmtypes.DefaultParams())
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
suite.encodingConfig = app.MakeEncodingConfig()
|
2021-04-17 10:00:07 +00:00
|
|
|
// We're using TestMsg amino encoding in some tests, so register it here.
|
|
|
|
suite.encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
|
|
|
|
|
|
|
|
suite.anteHandler = ante.NewAnteHandler(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.EvmKeeper, suite.encodingConfig.TxConfig.SignModeHandler())
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnteTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AnteTestSuite))
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
func newTestMsg(addrs ...sdk.AccAddress) *testdata.TestMsg {
|
|
|
|
return testdata.NewTestMsg(addrs...)
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestCoins() sdk.Coins {
|
2021-04-18 17:23:26 +00:00
|
|
|
return sdk.NewCoins(ethermint.NewPhotonCoinInt64(500000000))
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
func newTestStdFee() legacytx.StdFee {
|
|
|
|
return legacytx.NewStdFee(220000, sdk.NewCoins(ethermint.NewPhotonCoinInt64(150)))
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateAddress generates an Ethereum address.
|
2021-04-21 13:41:30 +00:00
|
|
|
func newTestAddrKey() (sdk.AccAddress, cryptotypes.PrivKey) {
|
2020-10-06 18:57:55 +00:00
|
|
|
privkey, _ := ethsecp256k1.GenerateKey()
|
2020-04-01 18:49:21 +00:00
|
|
|
addr := ethcrypto.PubkeyToAddress(privkey.ToECDSA().PublicKey)
|
|
|
|
|
|
|
|
return sdk.AccAddress(addr.Bytes()), privkey
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestSDKTx(
|
2021-04-21 13:41:30 +00:00
|
|
|
ctx sdk.Context, msgs []sdk.Msg, privs []cryptotypes.PrivKey,
|
|
|
|
accNums []uint64, seqs []uint64, fee legacytx.StdFee,
|
2020-04-01 18:49:21 +00:00
|
|
|
) sdk.Tx {
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
sigs := make([]legacytx.StdSignature, len(privs))
|
2020-04-01 18:49:21 +00:00
|
|
|
for i, priv := range privs {
|
2021-04-21 13:41:30 +00:00
|
|
|
signBytes := legacytx.StdSignBytes(ctx.ChainID(), accNums[i], seqs[i], 0, fee, msgs, "")
|
2020-04-01 18:49:21 +00:00
|
|
|
|
|
|
|
sig, err := priv.Sign(signBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
sigs[i] = legacytx.StdSignature{
|
2020-08-23 21:41:54 +00:00
|
|
|
PubKey: priv.PubKey(),
|
2020-04-01 18:49:21 +00:00
|
|
|
Signature: sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
return legacytx.NewStdTx(msgs, fee, sigs, "")
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 13:41:30 +00:00
|
|
|
func newTestEthTx(ctx sdk.Context, msg *evmtypes.MsgEthereumTx, priv cryptotypes.PrivKey) (sdk.Tx, error) {
|
2021-04-18 17:23:26 +00:00
|
|
|
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
|
2020-09-24 17:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
privkey := ðsecp256k1.PrivKey{Key: priv.Bytes()}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2020-09-24 17:50:47 +00:00
|
|
|
if err := msg.Sign(chainIDEpoch, privkey.ToECDSA()); err != nil {
|
2020-04-23 15:49:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|