2018-11-28 22:19:22 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
func TestMsgEthereumTx(t *testing.T) {
|
|
|
|
addr := GenerateEthAddress()
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
msg := NewMsgEthereumTx(nil, 0, &addr, nil, 100000, nil, []byte("test"), nil)
|
2020-04-23 15:49:25 +00:00
|
|
|
require.NotNil(t, msg)
|
2021-05-10 16:34:00 +00:00
|
|
|
require.EqualValues(t, msg.Data.To, addr.Bytes())
|
2020-04-23 15:49:25 +00:00
|
|
|
require.Equal(t, msg.Route(), RouterKey)
|
|
|
|
require.Equal(t, msg.Type(), TypeMsgEthereumTx)
|
|
|
|
require.NotNil(t, msg.To())
|
|
|
|
require.Equal(t, msg.GetMsgs(), []sdk.Msg{msg})
|
|
|
|
require.Panics(t, func() { msg.GetSigners() })
|
|
|
|
require.Panics(t, func() { msg.GetSignBytes() })
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
msg = NewMsgEthereumTxContract(nil, 0, nil, 100000, nil, []byte("test"), nil)
|
2020-04-23 15:49:25 +00:00
|
|
|
require.NotNil(t, msg)
|
2021-05-10 16:34:00 +00:00
|
|
|
require.Nil(t, msg.Data.To)
|
2020-04-23 15:49:25 +00:00
|
|
|
require.Nil(t, msg.To())
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgEthereumTxValidation(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2020-04-23 15:49:25 +00:00
|
|
|
msg string
|
2018-11-28 22:19:22 +00:00
|
|
|
amount *big.Int
|
|
|
|
gasPrice *big.Int
|
|
|
|
expectPass bool
|
|
|
|
}{
|
2020-04-23 15:49:25 +00:00
|
|
|
{msg: "pass", amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true},
|
|
|
|
{msg: "invalid amount", amount: big.NewInt(-1), gasPrice: big.NewInt(100000), expectPass: false},
|
|
|
|
{msg: "invalid gas price", amount: big.NewInt(100), gasPrice: big.NewInt(-1), expectPass: false},
|
2020-09-23 15:21:21 +00:00
|
|
|
{msg: "invalid gas price", amount: big.NewInt(100), gasPrice: big.NewInt(0), expectPass: false},
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
2021-05-10 16:34:00 +00:00
|
|
|
msg := NewMsgEthereumTx(nil, 0, nil, tc.amount, 0, tc.gasPrice, nil, nil)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
if tc.expectPass {
|
2021-04-18 15:54:18 +00:00
|
|
|
require.Nil(t, msg.ValidateBasic(), "valid test %d failed: %s", i, tc.msg)
|
2018-11-28 22:19:22 +00:00
|
|
|
} else {
|
2021-04-18 15:54:18 +00:00
|
|
|
require.NotNil(t, msg.ValidateBasic(), "invalid test %d passed: %s", i, tc.msg)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgEthereumTxRLPSignBytes(t *testing.T) {
|
|
|
|
addr := ethcmn.BytesToAddress([]byte("test_address"))
|
|
|
|
chainID := big.NewInt(3)
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
msg := NewMsgEthereumTx(chainID, 0, &addr, nil, 100000, nil, []byte("test"), nil)
|
2018-11-28 22:19:22 +00:00
|
|
|
hash := msg.RLPSignBytes(chainID)
|
|
|
|
require.Equal(t, "5BD30E35AD27449390B14C91E6BCFDCAADF8FE44EF33680E3BC200FC0DC083C7", fmt.Sprintf("%X", hash))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgEthereumTxRLPEncode(t *testing.T) {
|
|
|
|
addr := ethcmn.BytesToAddress([]byte("test_address"))
|
2021-05-10 16:34:00 +00:00
|
|
|
expMsg := NewMsgEthereumTx(big.NewInt(1), 0, &addr, nil, 100000, nil, []byte("test"), nil)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
raw, err := rlp.EncodeToBytes(&expMsg)
|
2018-11-28 22:19:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
msg := &MsgEthereumTx{}
|
|
|
|
err = rlp.Decode(bytes.NewReader(raw), &msg)
|
2018-11-28 22:19:22 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-10 16:34:00 +00:00
|
|
|
require.Equal(t, expMsg.Data, msg.Data)
|
2018-11-28 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
// func TestMsgEthereumTxSig(t *testing.T) {
|
|
|
|
// chainID := big.NewInt(3)
|
|
|
|
|
|
|
|
// priv1, _ := ethsecp256k1.GenerateKey()
|
|
|
|
// priv2, _ := ethsecp256k1.GenerateKey()
|
|
|
|
// addr1 := ethcmn.BytesToAddress(priv1.PubKey().Address().Bytes())
|
|
|
|
// addr2 := ethcmn.BytesToAddress(priv2.PubKey().Address().Bytes())
|
|
|
|
|
|
|
|
// // require valid signature passes validation
|
|
|
|
// msg := NewMsgEthereumTx(nil, 0, &addr1, nil, 100000, nil, []byte("test"), nil)
|
|
|
|
// err := msg.Sign(chainID, priv1.ToECDSA())
|
|
|
|
// require.Nil(t, err)
|
|
|
|
|
|
|
|
// signer, err := msg.VerifySig(chainID)
|
|
|
|
// require.NoError(t, err)
|
|
|
|
// require.Equal(t, addr1, signer)
|
|
|
|
// require.NotEqual(t, addr2, signer)
|
|
|
|
|
|
|
|
// // require invalid chain ID fail validation
|
|
|
|
// msg = NewMsgEthereumTx(nil, 0, &addr1, nil, 100000, nil, []byte("test"), nil)
|
|
|
|
// err = msg.Sign(chainID, priv1.ToECDSA())
|
|
|
|
// require.Nil(t, err)
|
|
|
|
|
|
|
|
// signer, err = msg.VerifySig(big.NewInt(4))
|
|
|
|
// require.Error(t, err)
|
|
|
|
// require.Equal(t, ethcmn.Address{}, signer)
|
|
|
|
// }
|