2018-08-14 01:02:21 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2018-08-16 21:04:19 +00:00
|
|
|
"fmt"
|
2018-08-14 01:02:21 +00:00
|
|
|
"testing"
|
|
|
|
"math/big"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
"github.com/cosmos/ethermint/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-08-16 21:04:19 +00:00
|
|
|
stake "github.com/cosmos/cosmos-sdk/x/stake/types"
|
|
|
|
|
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
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-08-14 01:02:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
// Test EthTx Antehandler
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2018-08-14 01:02:21 +00:00
|
|
|
func TestBadSig(t *testing.T) {
|
|
|
|
tx := types.NewTestEthTxs(types.TestChainID, []*ecdsa.PrivateKey{types.TestPrivKey1}, []ethcmn.Address{types.TestAddr1})[0]
|
|
|
|
|
|
|
|
tx.Data.Signature = types.NewEthSignature(new(big.Int), new(big.Int), new(big.Int))
|
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
assert.True(t, abort, "Antehandler did not abort")
|
2018-08-16 21:04:19 +00:00
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, fmt.Sprintf("Result has wrong code on bad tx: %s", res.Log))
|
2018-08-14 01:02:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsufficientGas(t *testing.T) {
|
|
|
|
tx := types.NewTestEthTxs(types.TestChainID, []*ecdsa.PrivateKey{types.TestPrivKey1}, []ethcmn.Address{types.TestAddr1})[0]
|
|
|
|
|
|
|
|
tx.Data.GasLimit = 0
|
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
assert.True(t, abort, "Antehandler did not abort")
|
2018-08-16 21:04:19 +00:00
|
|
|
require.Equal(t, sdk.ABCICodeType(0x1000c), res.Code, fmt.Sprintf("Result has wrong code on bad tx: %s", res.Log))
|
2018-08-14 01:02:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-16 21:04:19 +00:00
|
|
|
func TestIncorrectNonce(t *testing.T) {
|
2018-08-16 22:37:10 +00:00
|
|
|
// Create transaction with wrong nonce 12
|
|
|
|
tx := types.NewTransaction(12, types.TestAddr2, sdk.NewInt(50), 1000, sdk.NewInt(1000), []byte("test_bytes"))
|
|
|
|
tx.Sign(types.TestChainID, types.TestPrivKey1)
|
2018-08-14 01:02:21 +00:00
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
2018-08-16 21:04:19 +00:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, log.NewNopLogger())
|
2018-08-14 01:02:21 +00:00
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 21:04:19 +00:00
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
accountMapper.SetAccount(ctx, acc)
|
|
|
|
|
2018-08-14 01:02:21 +00:00
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
assert.True(t, abort, "Antehandler did not abort")
|
2018-08-16 21:04:19 +00:00
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10003), res.Code, fmt.Sprintf("Result has wrong code on bad tx: %s", res.Log))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidTx(t *testing.T) {
|
|
|
|
tx := types.NewTestEthTxs(types.TestChainID, []*ecdsa.PrivateKey{types.TestPrivKey1}, []ethcmn.Address{types.TestAddr1})[0]
|
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
accountMapper.SetAccount(ctx, acc)
|
|
|
|
|
2018-08-16 21:04:19 +00:00
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
assert.False(t, abort, "Antehandler abort on valid tx")
|
|
|
|
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-16 22:37:10 +00:00
|
|
|
// Ensure account state updated correctly
|
|
|
|
seq, _ := accountMapper.GetSequence(ctx, types.TestAddr1[:])
|
|
|
|
require.Equal(t, int64(1), seq, "AccountNonce did not increment correctly")
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
// Test EmbeddedTx Antehandler
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func TestInvalidSigEmbeddedTx(t *testing.T) {
|
2018-08-16 21:04:19 +00:00
|
|
|
// Create msg to be embedded
|
|
|
|
msgs := []sdk.Msg{stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)})}
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
// Create transaction with incorrect signer
|
|
|
|
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey2},
|
|
|
|
[]int64{0}, []int64{0}, types.NewStdFee())
|
2018-08-14 01:02:21 +00:00
|
|
|
|
2018-08-16 21:04:19 +00:00
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
accountMapper.SetAccount(ctx, acc)
|
|
|
|
|
2018-08-16 21:04:19 +00:00
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidMultiMsgEmbeddedTx(t *testing.T) {
|
|
|
|
// Create msg to be embedded
|
|
|
|
msgs := []sdk.Msg{
|
|
|
|
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
|
|
|
stake.NewMsgDelegate(types.TestAddr2[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create transaction with only one signer
|
|
|
|
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1},
|
|
|
|
[]int64{0}, []int64{0}, types.NewStdFee())
|
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc1 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
accountMapper.SetAccount(ctx, acc1)
|
|
|
|
acc2 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
accountMapper.SetAccount(ctx, acc2)
|
|
|
|
|
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
|
|
|
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
func TestInvalidAccountNumberEmbeddedTx(t *testing.T) {
|
2018-08-16 21:04:19 +00:00
|
|
|
// Create msg to be embedded
|
2018-08-16 22:37:10 +00:00
|
|
|
msgs := []sdk.Msg{
|
|
|
|
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
|
|
|
}
|
2018-08-16 21:04:19 +00:00
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
// Create transaction with wrong account number
|
|
|
|
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1},
|
|
|
|
[]int64{3}, []int64{0}, types.NewStdFee())
|
2018-08-16 21:04:19 +00:00
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
2018-08-16 22:37:10 +00:00
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
acc.SetAccountNumber(4)
|
|
|
|
accountMapper.SetAccount(ctx, acc)
|
|
|
|
|
2018-08-16 21:04:19 +00:00
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
2018-08-16 22:37:10 +00:00
|
|
|
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
2018-08-16 21:04:19 +00:00
|
|
|
}
|
2018-08-16 22:37:10 +00:00
|
|
|
|
|
|
|
func TestInvalidSequenceEmbeddedTx(t *testing.T) {
|
|
|
|
// Create msg to be embedded
|
|
|
|
msgs := []sdk.Msg{
|
|
|
|
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create transaction with wrong account number
|
|
|
|
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1},
|
|
|
|
[]int64{4}, []int64{2}, types.NewStdFee())
|
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
acc.SetAccountNumber(4)
|
|
|
|
acc.SetSequence(3)
|
|
|
|
accountMapper.SetAccount(ctx, acc)
|
|
|
|
|
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
|
|
|
assert.True(t, abort, "Antehandler did not abort on invalid embedded tx")
|
|
|
|
require.Equal(t, sdk.ABCICodeType(0x10004), res.Code, "Result is OK on bad tx")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidEmbeddedTx(t *testing.T) {
|
|
|
|
// Create msg to be embedded
|
|
|
|
msgs := []sdk.Msg{
|
|
|
|
stake.NewMsgDelegate(types.TestAddr1[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
|
|
|
stake.NewMsgDelegate(types.TestAddr2[:], types.TestAddr2[:], sdk.Coin{"steak", sdk.NewInt(50)}),
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := types.NewTestEmbeddedTx(types.TestChainID, msgs, []*ecdsa.PrivateKey{types.TestPrivKey1, types.TestPrivKey2},
|
|
|
|
[]int64{4, 5}, []int64{3, 1}, types.NewStdFee())
|
|
|
|
|
|
|
|
ms, key := SetupMultiStore()
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: types.TestChainID.String()}, false, nil)
|
|
|
|
|
|
|
|
accountMapper := auth.NewAccountMapper(types.NewTestCodec(), key, auth.ProtoBaseAccount)
|
|
|
|
|
|
|
|
// Set account in accountMapper
|
|
|
|
acc1 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr1[:])
|
|
|
|
acc1.SetAccountNumber(4)
|
|
|
|
acc1.SetSequence(3)
|
|
|
|
accountMapper.SetAccount(ctx, acc1)
|
|
|
|
acc2 := accountMapper.NewAccountWithAddress(ctx, types.TestAddr2[:])
|
|
|
|
acc2.SetAccountNumber(5)
|
|
|
|
acc2.SetSequence(1)
|
|
|
|
accountMapper.SetAccount(ctx, acc2)
|
|
|
|
|
|
|
|
handler := AnteHandler(accountMapper)
|
|
|
|
|
|
|
|
_, res, abort := handler(ctx, tx)
|
|
|
|
|
|
|
|
require.False(t, abort, "Antehandler abort on valid embedded tx")
|
|
|
|
require.True(t, res.IsOK(), fmt.Sprintf("Result not OK on valid Tx: %s", res.Log))
|
|
|
|
|
|
|
|
// Ensure account state updated correctly
|
|
|
|
seq1, _ := accountMapper.GetSequence(ctx, types.TestAddr1[:])
|
|
|
|
seq2, _ := accountMapper.GetSequence(ctx, types.TestAddr2[:])
|
|
|
|
|
|
|
|
assert.Equal(t, int64(4), seq1, "Sequence1 did not increment correctly")
|
|
|
|
assert.Equal(t, int64(2), seq2, "Sequence2 did not increment correctly")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|