2018-11-28 22:19:22 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
tmcrypto "github.com/tendermint/tendermint/crypto"
|
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-12-18 16:10:04 +00:00
|
|
|
"github.com/cosmos/ethermint/types"
|
2018-11-28 22:19:22 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func requireValidTx(
|
|
|
|
t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, sim bool,
|
|
|
|
) {
|
2019-11-04 20:45:02 +00:00
|
|
|
_, err := anteHandler(ctx, tx, sim)
|
|
|
|
require.True(t, err == nil)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func requireInvalidTx(
|
|
|
|
t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context,
|
|
|
|
tx sdk.Tx, sim bool, code sdk.CodeType,
|
|
|
|
) {
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
_, err := anteHandler(ctx, tx, sim)
|
|
|
|
// require.Equal(t, code, err, fmt.Sprintf("invalid result: %v", err))
|
|
|
|
require.Error(t, err)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
if code == sdk.CodeOutOfGas {
|
2019-11-04 20:45:02 +00:00
|
|
|
_, ok := tx.(auth.StdTx)
|
2018-11-28 22:19:22 +00:00
|
|
|
require.True(t, ok, "tx must be in form auth.StdTx")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
func TestValidEthTx(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
|
|
|
|
acc1 := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc1.SetCoins(newTestCoins())
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc1)
|
|
|
|
|
|
|
|
acc2 := input.accKeeper.NewAccountWithAddress(input.ctx, addr2)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc2.SetCoins(newTestCoins())
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc2)
|
|
|
|
|
|
|
|
// require a valid Ethereum tx to pass
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, 22000, gas, []byte("test"))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
requireValidTx(t, input.anteHandler, input.ctx, tx, false)
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
func TestValidTx(t *testing.T) {
|
2018-12-18 16:10:04 +00:00
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, priv2 := newTestAddrKey()
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
acc1 := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-11-28 22:19:22 +00:00
|
|
|
acc1.SetCoins(newTestCoins())
|
2018-12-18 16:10:04 +00:00
|
|
|
input.accKeeper.SetAccount(input.ctx, acc1)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
acc2 := input.accKeeper.NewAccountWithAddress(input.ctx, addr2)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-11-28 22:19:22 +00:00
|
|
|
acc2.SetCoins(newTestCoins())
|
2018-12-18 16:10:04 +00:00
|
|
|
input.accKeeper.SetAccount(input.ctx, acc2)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
// require a valid SDK tx to pass
|
|
|
|
fee := newTestStdFee()
|
|
|
|
msg1 := newTestMsg(addr1, addr2)
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
privKeys := []tmcrypto.PrivKey{priv1, priv2}
|
|
|
|
accNums := []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber()}
|
|
|
|
accSeqs := []uint64{acc1.GetSequence(), acc2.GetSequence()}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
tx := newTestSDKTx(input.ctx, msgs, privKeys, accNums, accSeqs, fee)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
requireValidTx(t, input.anteHandler, input.ctx, tx, false)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSDKInvalidSigs(t *testing.T) {
|
2018-12-18 16:10:04 +00:00
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, priv2 := newTestAddrKey()
|
|
|
|
addr3, priv3 := newTestAddrKey()
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
acc1 := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-11-28 22:19:22 +00:00
|
|
|
acc1.SetCoins(newTestCoins())
|
2018-12-18 16:10:04 +00:00
|
|
|
input.accKeeper.SetAccount(input.ctx, acc1)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
acc2 := input.accKeeper.NewAccountWithAddress(input.ctx, addr2)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-11-28 22:19:22 +00:00
|
|
|
acc2.SetCoins(newTestCoins())
|
2018-12-18 16:10:04 +00:00
|
|
|
input.accKeeper.SetAccount(input.ctx, acc2)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
fee := newTestStdFee()
|
|
|
|
msg1 := newTestMsg(addr1, addr2)
|
|
|
|
|
|
|
|
// require validation failure with no signers
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
privKeys := []tmcrypto.PrivKey{}
|
|
|
|
accNums := []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber()}
|
|
|
|
accSeqs := []uint64{acc1.GetSequence(), acc2.GetSequence()}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
tx := newTestSDKTx(input.ctx, msgs, privKeys, accNums, accSeqs, fee)
|
2019-07-04 19:46:54 +00:00
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeNoSignatures)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
// require validation failure with invalid number of signers
|
|
|
|
msgs = []sdk.Msg{msg1}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
privKeys = []tmcrypto.PrivKey{priv1}
|
|
|
|
accNums = []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber()}
|
|
|
|
accSeqs = []uint64{acc1.GetSequence(), acc2.GetSequence()}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
tx = newTestSDKTx(input.ctx, msgs, privKeys, accNums, accSeqs, fee)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeUnauthorized)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
// require validation failure with an invalid signer
|
|
|
|
msg2 := newTestMsg(addr1, addr3)
|
|
|
|
msgs = []sdk.Msg{msg1, msg2}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
privKeys = []tmcrypto.PrivKey{priv1, priv2, priv3}
|
|
|
|
accNums = []uint64{acc1.GetAccountNumber(), acc2.GetAccountNumber(), 0}
|
|
|
|
accSeqs = []uint64{acc1.GetSequence(), acc2.GetSequence(), 0}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
tx = newTestSDKTx(input.ctx, msgs, privKeys, accNums, accSeqs, fee)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeUnknownAddress)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSDKInvalidAcc(t *testing.T) {
|
2018-12-18 16:10:04 +00:00
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
acc1 := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-11-28 22:19:22 +00:00
|
|
|
acc1.SetCoins(newTestCoins())
|
2018-12-18 16:10:04 +00:00
|
|
|
input.accKeeper.SetAccount(input.ctx, acc1)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
fee := newTestStdFee()
|
|
|
|
msg1 := newTestMsg(addr1)
|
|
|
|
msgs := []sdk.Msg{msg1}
|
2018-12-18 16:10:04 +00:00
|
|
|
privKeys := []tmcrypto.PrivKey{priv1}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
// require validation failure with invalid account number
|
2018-12-18 16:10:04 +00:00
|
|
|
accNums := []uint64{1}
|
|
|
|
accSeqs := []uint64{acc1.GetSequence()}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
tx := newTestSDKTx(input.ctx, msgs, privKeys, accNums, accSeqs, fee)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeUnauthorized)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2019-11-05 16:50:55 +00:00
|
|
|
// require validation failure with invalid sequence (nonce)
|
|
|
|
accNums = []uint64{acc1.GetAccountNumber()}
|
|
|
|
accSeqs = []uint64{1}
|
2018-12-18 16:10:04 +00:00
|
|
|
|
2019-11-05 16:50:55 +00:00
|
|
|
tx = newTestSDKTx(input.ctx, msgs, privKeys, accNums, accSeqs, fee)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeUnauthorized)
|
2018-12-18 16:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEthInvalidSig(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
_, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, 22000, gas, []byte("test"))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
ctx := input.ctx.WithChainID("4")
|
|
|
|
requireInvalidTx(t, input.anteHandler, ctx, tx, false, sdk.CodeUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEthInvalidNonce(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
|
|
|
|
acc := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc.SetCoins(newTestCoins())
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc.SetSequence(10)
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc)
|
|
|
|
|
|
|
|
// require a valid Ethereum tx to pass
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, 22000, gas, []byte("test"))
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeInvalidSequence)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
func TestEthInsufficientBalance(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
|
|
|
|
acc := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc)
|
|
|
|
|
|
|
|
// require a valid Ethereum tx to pass
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, 22000, gas, []byte("test"))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeInsufficientFunds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEthInvalidIntrinsicGas(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
|
|
|
|
acc := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc.SetCoins(newTestCoins())
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc)
|
|
|
|
|
|
|
|
// require a valid Ethereum tx to pass
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
|
|
|
gasLimit := uint64(1000)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, gasLimit, gas, []byte("test"))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeInternal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEthInvalidMempoolFees(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
2019-07-04 19:46:54 +00:00
|
|
|
input.ctx = input.ctx.WithMinGasPrices(sdk.DecCoins{sdk.NewDecCoin(types.DenomDefault, sdk.NewInt(500000))})
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
|
|
|
|
acc := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc.SetCoins(newTestCoins())
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc)
|
|
|
|
|
|
|
|
// require a valid Ethereum tx to pass
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, 22000, gas, []byte("test"))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
requireInvalidTx(t, input.anteHandler, input.ctx, tx, false, sdk.CodeInsufficientFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEthInvalidChainID(t *testing.T) {
|
|
|
|
input := newTestSetup()
|
|
|
|
input.ctx = input.ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
addr1, priv1 := newTestAddrKey()
|
|
|
|
addr2, _ := newTestAddrKey()
|
|
|
|
|
|
|
|
acc := input.accKeeper.NewAccountWithAddress(input.ctx, addr1)
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-12-18 16:10:04 +00:00
|
|
|
acc.SetCoins(newTestCoins())
|
|
|
|
input.accKeeper.SetAccount(input.ctx, acc)
|
|
|
|
|
|
|
|
// require a valid Ethereum tx to pass
|
|
|
|
to := ethcmn.BytesToAddress(addr2.Bytes())
|
|
|
|
amt := big.NewInt(32)
|
|
|
|
gas := big.NewInt(20)
|
2019-10-30 18:30:24 +00:00
|
|
|
ethMsg := evmtypes.NewEthereumTxMsg(0, &to, amt, 22000, gas, []byte("test"))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
tx := newTestEthTx(input.ctx, ethMsg, priv1)
|
|
|
|
ctx := input.ctx.WithChainID("bad-chain-id")
|
|
|
|
requireInvalidTx(t, input.anteHandler, ctx, tx, false, types.CodeInvalidChainID)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|