2018-08-14 01:02:21 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2018-08-31 20:44:34 +00:00
|
|
|
"crypto/ecdsa"
|
2018-08-16 21:04:19 +00:00
|
|
|
"fmt"
|
2018-08-14 01:02:21 +00:00
|
|
|
"math/big"
|
2018-08-31 20:44:34 +00:00
|
|
|
"testing"
|
2018-08-14 01:02:21 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-08-31 20:44:34 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
|
|
|
"github.com/cosmos/ethermint/types"
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-14 01:02:21 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-08-16 21:04:19 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-08-14 01:02:21 +00:00
|
|
|
)
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEthTxBadSig(t *testing.T) {
|
|
|
|
tx := types.NewTransaction(uint64(0), types.TestAddr1, big.NewInt(10), 1000, big.NewInt(100), []byte{})
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// create bad signature
|
|
|
|
tx.Sign(big.NewInt(100), types.TestPrivKey2)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-14 01:02:21 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
|
|
|
_, res, abort := handler(ctx, *tx)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-14 01:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEthTxInsufficientGas(t *testing.T) {
|
|
|
|
tx := types.NewTransaction(uint64(0), types.TestAddr1, big.NewInt(0), 0, big.NewInt(0), []byte{})
|
|
|
|
tx.Sign(types.TestChainID, types.TestPrivKey1)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-14 01:02:21 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
|
|
|
_, res, abort := handler(ctx, *tx)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x1000c), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-14 01:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEthTxIncorrectNonce(t *testing.T) {
|
|
|
|
// create transaction with wrong nonce 12
|
|
|
|
tx := types.NewTransaction(12, types.TestAddr2, big.NewInt(50), 1000, big.NewInt(1000), []byte("test_bytes"))
|
2018-08-16 22:37:10 +00:00
|
|
|
tx.Sign(types.TestChainID, types.TestPrivKey1)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 21:04:19 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, log.NewNopLogger())
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set account in accountMapper
|
|
|
|
acc := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
|
|
|
accMapper.SetAccount(ctx, acc)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
|
|
|
_, res, abort := handler(ctx, *tx)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10003), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEthTxValidTx(t *testing.T) {
|
|
|
|
tx := types.NewTransaction(0, types.TestAddr1, big.NewInt(50), 1000, big.NewInt(1000), []byte{})
|
|
|
|
tx.Sign(types.TestChainID, types.TestPrivKey1)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 21:04:19 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set account in accountMapper
|
|
|
|
acc := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
|
|
|
accMapper.SetAccount(ctx, acc)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
|
|
|
_, res, abort := handler(ctx, *tx)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.False(t, abort, "expected ante handler to not abort")
|
|
|
|
require.True(t, res.IsOK(), fmt.Sprintf("result not OK on valid Tx: %s", res.Log))
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// ensure account state updated correctly
|
|
|
|
seq, _ := accMapper.GetSequence(ctx, types.TestAddr1[:])
|
|
|
|
require.Equal(t, int64(1), seq, "account nonce did not increment correctly")
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEmbeddedTxBadSig(t *testing.T) {
|
|
|
|
testCodec := types.NewTestCodec()
|
|
|
|
testFee := types.NewTestStdFee()
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
msgs := []sdk.Msg{sdk.NewTestMsg()}
|
|
|
|
tx := types.NewTestStdTx(
|
|
|
|
types.TestChainID, msgs, []int64{0}, []int64{0}, []*ecdsa.PrivateKey{types.TestPrivKey1}, testFee,
|
|
|
|
)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// create bad signature
|
|
|
|
signBytes := types.GetStdTxSignBytes(big.NewInt(100).String(), 1, 1, testFee, msgs, "")
|
|
|
|
sig, _ := ethcrypto.Sign(signBytes, types.TestPrivKey1)
|
|
|
|
(tx.(auth.StdTx)).Signatures[0].Signature = sig
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 21:04:19 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(testCodec, key, auth.ProtoBaseAccount)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set account in accountMapper
|
|
|
|
acc := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
|
|
|
accMapper.SetAccount(ctx, acc)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
2018-08-16 21:04:19 +00:00
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEmbeddedTxInvalidMultiMsg(t *testing.T) {
|
|
|
|
testCodec := types.NewTestCodec()
|
|
|
|
testCodec.RegisterConcrete(stake.MsgDelegate{}, "test/MsgDelegate", nil)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
msgs := []sdk.Msg{
|
2018-08-31 20:44:34 +00:00
|
|
|
stake.NewMsgDelegate(types.TestAddr1.Bytes(), types.TestAddr2.Bytes(), sdk.NewCoin("steak", sdk.NewInt(50))),
|
|
|
|
stake.NewMsgDelegate(types.TestAddr2.Bytes(), types.TestAddr2.Bytes(), sdk.NewCoin("steak", sdk.NewInt(50))),
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// create transaction with only one signer
|
|
|
|
tx := types.NewTestStdTx(
|
|
|
|
types.TestChainID, msgs, []int64{0}, []int64{0}, []*ecdsa.PrivateKey{types.TestPrivKey1}, types.NewTestStdFee(),
|
|
|
|
)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 22:37:10 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(testCodec, key, auth.ProtoBaseAccount)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set accounts in accountMapper
|
|
|
|
acc1 := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
|
|
|
accMapper.SetAccount(ctx, acc1)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
acc2 := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
|
|
|
accMapper.SetAccount(ctx, acc2)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
2018-08-16 22:37:10 +00:00
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEmbeddedTxInvalidAccountNumber(t *testing.T) {
|
|
|
|
testCodec := types.NewTestCodec()
|
|
|
|
testCodec.RegisterConcrete(stake.MsgDelegate{}, "test/MsgDelegate", nil)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
msgs := []sdk.Msg{
|
2018-08-31 20:44:34 +00:00
|
|
|
stake.NewMsgDelegate(types.TestAddr1.Bytes(), types.TestAddr2.Bytes(), sdk.NewCoin("steak", sdk.NewInt(50))),
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// create a transaction with an invalid account number
|
|
|
|
tx := types.NewTestStdTx(
|
|
|
|
types.TestChainID, msgs, []int64{3}, []int64{0}, []*ecdsa.PrivateKey{types.TestPrivKey1}, types.NewTestStdFee(),
|
|
|
|
)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 21:04:19 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(testCodec, key, auth.ProtoBaseAccount)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set account in accountMapper
|
|
|
|
acc := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
2018-08-16 22:37:10 +00:00
|
|
|
acc.SetAccountNumber(4)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper.SetAccount(ctx, acc)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
2018-08-16 21:04:19 +00:00
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10003), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEmbeddedTxInvalidSequence(t *testing.T) {
|
|
|
|
testCodec := types.NewTestCodec()
|
|
|
|
testCodec.RegisterConcrete(stake.MsgDelegate{}, "test/MsgDelegate", nil)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
msgs := []sdk.Msg{
|
2018-08-31 20:44:34 +00:00
|
|
|
stake.NewMsgDelegate(types.TestAddr1.Bytes(), types.TestAddr2.Bytes(), sdk.NewCoin("steak", sdk.NewInt(50))),
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// create transaction with an invalid sequence (nonce)
|
|
|
|
tx := types.NewTestStdTx(
|
|
|
|
types.TestChainID, msgs, []int64{4}, []int64{2}, []*ecdsa.PrivateKey{types.TestPrivKey1}, types.NewTestStdFee(),
|
|
|
|
)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 22:37:10 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set account in accountMapper
|
|
|
|
acc := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
2018-08-16 22:37:10 +00:00
|
|
|
acc.SetAccountNumber(4)
|
|
|
|
acc.SetSequence(3)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper.SetAccount(ctx, acc)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
2018-08-16 22:37:10 +00:00
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.True(t, abort, "expected ante handler to abort")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10003), res.Code, fmt.Sprintf("invalid code returned on bad tx: %s", res.Log))
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
func TestEmbeddedTxValid(t *testing.T) {
|
|
|
|
testCodec := types.NewTestCodec()
|
|
|
|
testCodec.RegisterConcrete(stake.MsgDelegate{}, "test/MsgDelegate", nil)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
msgs := []sdk.Msg{
|
2018-08-31 20:44:34 +00:00
|
|
|
stake.NewMsgDelegate(types.TestAddr1.Bytes(), types.TestAddr2.Bytes(), sdk.NewCoin("steak", sdk.NewInt(50))),
|
|
|
|
stake.NewMsgDelegate(types.TestAddr2.Bytes(), types.TestAddr2.Bytes(), sdk.NewCoin("steak", sdk.NewInt(50))),
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// create a valid transaction
|
|
|
|
tx := types.NewTestStdTx(
|
|
|
|
types.TestChainID, msgs, []int64{4, 5}, []int64{3, 1},
|
|
|
|
[]*ecdsa.PrivateKey{types.TestPrivKey1, types.TestPrivKey2}, types.NewTestStdFee(),
|
|
|
|
)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
ms, key := createTestMultiStore()
|
2018-08-16 22:37:10 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
// set accounts in the accountMapper
|
|
|
|
acc1 := accMapper.NewAccountWithAddress(ctx, types.TestAddr1.Bytes())
|
2018-08-16 22:37:10 +00:00
|
|
|
acc1.SetAccountNumber(4)
|
|
|
|
acc1.SetSequence(3)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper.SetAccount(ctx, acc1)
|
|
|
|
|
|
|
|
acc2 := accMapper.NewAccountWithAddress(ctx, types.TestAddr2.Bytes())
|
2018-08-16 22:37:10 +00:00
|
|
|
acc2.SetAccountNumber(5)
|
|
|
|
acc2.SetSequence(1)
|
2018-08-31 20:44:34 +00:00
|
|
|
accMapper.SetAccount(ctx, acc2)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
handler := AnteHandler(accMapper, auth.FeeCollectionKeeper{})
|
2018-08-16 22:37:10 +00:00
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.False(t, abort, "expected ante handler to not abort")
|
|
|
|
require.True(t, res.IsOK(), fmt.Sprintf("result not OK on valid Tx: %s", res.Log))
|
2018-08-16 22:37:10 +00:00
|
|
|
|
|
|
|
// Ensure account state updated correctly
|
2018-08-31 20:44:34 +00:00
|
|
|
seq1, _ := accMapper.GetSequence(ctx, types.TestAddr1.Bytes())
|
|
|
|
seq2, _ := accMapper.GetSequence(ctx, types.TestAddr2.Bytes())
|
2018-08-16 22:37:10 +00:00
|
|
|
|
2018-08-31 20:44:34 +00:00
|
|
|
require.Equal(t, int64(4), seq1, "account nonce did not increment correctly")
|
|
|
|
require.Equal(t, int64(2), seq2, "account nonce did not increment correctly")
|
2018-08-16 22:37:10 +00:00
|
|
|
}
|