2018-08-09 01:06:34 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
2018-08-30 21:36:14 +00:00
|
|
|
"math/big"
|
2018-08-09 01:06:34 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2018-08-30 21:36:14 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-08-09 01:06:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
func TestTransactionRLPEncode(t *testing.T) {
|
|
|
|
txs := NewTestEthTxs(TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1})
|
|
|
|
gtxs := NewTestGethTxs(TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1})
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
txRLP, err := rlp.EncodeToBytes(txs[0])
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
gtxRLP, err := rlp.EncodeToBytes(gtxs[0])
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
require.Equal(t, gtxRLP, txRLP)
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
func TestTransactionRLPDecode(t *testing.T) {
|
|
|
|
txs := NewTestEthTxs(TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1})
|
|
|
|
gtxs := NewTestGethTxs(TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1})
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
txRLP, err := rlp.EncodeToBytes(txs[0])
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
gtxRLP, err := rlp.EncodeToBytes(gtxs[0])
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
var (
|
|
|
|
decodedTx Transaction
|
|
|
|
decodedGtx ethtypes.Transaction
|
|
|
|
)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
err = rlp.DecodeBytes(txRLP, &decodedTx)
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
err = rlp.DecodeBytes(gtxRLP, &decodedGtx)
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
require.Equal(t, decodedGtx.Hash(), decodedTx.Hash())
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidation(t *testing.T) {
|
2018-08-30 21:36:14 +00:00
|
|
|
ethTxs := NewTestEthTxs(
|
|
|
|
TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1},
|
2018-08-09 01:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
msg sdk.Msg
|
|
|
|
mutate func(sdk.Msg) sdk.Msg
|
|
|
|
expectedErr bool
|
|
|
|
}{
|
|
|
|
{ethTxs[0], func(msg sdk.Msg) sdk.Msg { return msg }, false},
|
|
|
|
{ethTxs[0], func(msg sdk.Msg) sdk.Msg {
|
2018-08-30 21:36:14 +00:00
|
|
|
tx := msg.(*Transaction)
|
|
|
|
tx.data.Price = big.NewInt(-1)
|
2018-08-09 01:06:34 +00:00
|
|
|
return tx
|
|
|
|
}, true},
|
|
|
|
{ethTxs[0], func(msg sdk.Msg) sdk.Msg {
|
2018-08-30 21:36:14 +00:00
|
|
|
tx := msg.(*Transaction)
|
|
|
|
tx.data.Amount = big.NewInt(-1)
|
2018-08-09 01:06:34 +00:00
|
|
|
return tx
|
|
|
|
}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
msg := tc.mutate(tc.msg)
|
|
|
|
err := msg.ValidateBasic()
|
|
|
|
|
|
|
|
if tc.expectedErr {
|
|
|
|
require.NotEqual(t, sdk.CodeOK, err.Code(), fmt.Sprintf("expected error: test case #%d", i))
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, fmt.Sprintf("unexpected error: test case #%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
func TestTransactionVerifySig(t *testing.T) {
|
|
|
|
txs := NewTestEthTxs(
|
|
|
|
TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1},
|
2018-08-09 01:06:34 +00:00
|
|
|
)
|
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
addr, err := txs[0].VerifySig(TestChainID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, TestAddr1, addr)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
addr, err = txs[0].VerifySig(big.NewInt(100))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.NotEqual(t, TestAddr1, addr)
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxDecoder(t *testing.T) {
|
2018-08-30 21:36:14 +00:00
|
|
|
testCodec := NewTestCodec()
|
|
|
|
txDecoder := TxDecoder(testCodec, TestSDKAddr)
|
2018-08-24 15:29:10 +00:00
|
|
|
msgs := []sdk.Msg{sdk.NewTestMsg()}
|
2018-08-09 01:06:34 +00:00
|
|
|
|
|
|
|
// create a non-SDK Ethereum transaction
|
2018-08-30 21:36:14 +00:00
|
|
|
txs := NewTestEthTxs(
|
|
|
|
TestChainID, []int64{0}, []ethcmn.Address{TestAddr1}, []*ecdsa.PrivateKey{TestPrivKey1},
|
2018-08-09 01:06:34 +00:00
|
|
|
)
|
2018-08-30 21:36:14 +00:00
|
|
|
|
|
|
|
txBytes, err := rlp.EncodeToBytes(txs[0])
|
|
|
|
require.NoError(t, err)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
|
|
|
// require the transaction to properly decode into a Transaction
|
2018-08-30 21:36:14 +00:00
|
|
|
decodedTx, err := txDecoder(txBytes)
|
2018-08-09 01:06:34 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-30 21:36:14 +00:00
|
|
|
require.IsType(t, Transaction{}, decodedTx)
|
|
|
|
require.Equal(t, txs[0].data, (decodedTx.(Transaction)).data)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-24 15:29:10 +00:00
|
|
|
// create a SDK (auth.StdTx) transaction and encode
|
2018-08-30 21:36:14 +00:00
|
|
|
txs = NewTestSDKTxs(
|
|
|
|
testCodec, TestChainID, TestSDKAddr, msgs, []int64{0}, []int64{0},
|
|
|
|
[]*ecdsa.PrivateKey{TestPrivKey1}, NewTestStdFee(),
|
2018-08-09 01:06:34 +00:00
|
|
|
)
|
|
|
|
|
2018-08-30 21:36:14 +00:00
|
|
|
txBytes, err = rlp.EncodeToBytes(txs[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-08-09 01:06:34 +00:00
|
|
|
// require the transaction to properly decode into a Transaction
|
2018-08-30 21:36:14 +00:00
|
|
|
stdTx := NewTestStdTx(TestChainID, msgs, []int64{0}, []int64{0}, []*ecdsa.PrivateKey{TestPrivKey1}, NewTestStdFee())
|
|
|
|
decodedTx, err = txDecoder(txBytes)
|
2018-08-09 01:06:34 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-30 21:36:14 +00:00
|
|
|
require.IsType(t, auth.StdTx{}, decodedTx)
|
|
|
|
require.Equal(t, stdTx, decodedTx)
|
2018-08-09 01:06:34 +00:00
|
|
|
|
|
|
|
// require the decoding to fail when no transaction bytes are given
|
2018-08-30 21:36:14 +00:00
|
|
|
decodedTx, err = txDecoder([]byte{})
|
2018-08-09 01:06:34 +00:00
|
|
|
require.Error(t, err)
|
2018-08-30 21:36:14 +00:00
|
|
|
require.Nil(t, decodedTx)
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|