2019-06-05 16:26:17 -07:00
|
|
|
package types
|
2018-01-06 20:54:04 -08:00
|
|
|
|
|
|
|
import (
|
2018-02-20 17:15:57 -08:00
|
|
|
"fmt"
|
2018-01-06 20:54:04 -08:00
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-06 20:54:04 -08:00
|
|
|
|
2018-01-12 12:03:23 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-01-06 20:54:04 -08:00
|
|
|
)
|
|
|
|
|
2018-10-23 12:23:55 -07:00
|
|
|
func TestMsgSendRoute(t *testing.T) {
|
2019-02-04 15:58:02 -08:00
|
|
|
addr1 := sdk.AccAddress([]byte("from"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("to"))
|
2019-03-07 16:55:08 -08:00
|
|
|
coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10))
|
2019-02-04 15:58:02 -08:00
|
|
|
var msg = NewMsgSend(addr1, addr2, coins)
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
require.Equal(t, msg.Route(), RouterKey)
|
2019-02-04 15:58:02 -08:00
|
|
|
require.Equal(t, msg.Type(), "send")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgSendValidation(t *testing.T) {
|
|
|
|
addr1 := sdk.AccAddress([]byte("from"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("to"))
|
2019-03-07 16:55:08 -08:00
|
|
|
atom123 := sdk.NewCoins(sdk.NewInt64Coin("atom", 123))
|
|
|
|
atom0 := sdk.NewCoins(sdk.NewInt64Coin("atom", 0))
|
|
|
|
atom123eth123 := sdk.NewCoins(sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 123))
|
2019-02-04 15:58:02 -08:00
|
|
|
atom123eth0 := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 0)}
|
|
|
|
|
|
|
|
var emptyAddr sdk.AccAddress
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
valid bool
|
2020-06-03 12:52:05 -07:00
|
|
|
tx *MsgSend
|
2019-02-04 15:58:02 -08:00
|
|
|
}{
|
|
|
|
{true, NewMsgSend(addr1, addr2, atom123)}, // valid send
|
|
|
|
{true, NewMsgSend(addr1, addr2, atom123eth123)}, // valid send with multiple coins
|
|
|
|
{false, NewMsgSend(addr1, addr2, atom0)}, // non positive coin
|
|
|
|
{false, NewMsgSend(addr1, addr2, atom123eth0)}, // non positive coin in multicoins
|
|
|
|
{false, NewMsgSend(emptyAddr, addr2, atom123)}, // empty from addr
|
|
|
|
{false, NewMsgSend(addr1, emptyAddr, atom123)}, // empty to addr
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:55:08 -08:00
|
|
|
for _, tc := range cases {
|
2019-02-04 15:58:02 -08:00
|
|
|
err := tc.tx.ValidateBasic()
|
|
|
|
if tc.valid {
|
2019-03-07 16:55:08 -08:00
|
|
|
require.Nil(t, err)
|
2019-02-04 15:58:02 -08:00
|
|
|
} else {
|
2019-03-07 16:55:08 -08:00
|
|
|
require.NotNil(t, err)
|
2019-02-04 15:58:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgSendGetSignBytes(t *testing.T) {
|
|
|
|
addr1 := sdk.AccAddress([]byte("input"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("output"))
|
2019-03-07 16:55:08 -08:00
|
|
|
coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10))
|
2019-02-04 15:58:02 -08:00
|
|
|
var msg = NewMsgSend(addr1, addr2, coins)
|
|
|
|
res := msg.GetSignBytes()
|
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
expected := `{"type":"cosmos-sdk/MsgSend","value":{"amount":[{"amount":"10","denom":"atom"}],"from_address":"cosmos1d9h8qat57ljhcm","to_address":"cosmos1da6hgur4wsmpnjyg"}}`
|
2019-02-04 15:58:02 -08:00
|
|
|
require.Equal(t, expected, string(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgSendGetSigners(t *testing.T) {
|
2019-03-07 16:55:08 -08:00
|
|
|
var msg = NewMsgSend(sdk.AccAddress([]byte("input1")), sdk.AccAddress{}, sdk.NewCoins())
|
2019-02-04 15:58:02 -08:00
|
|
|
res := msg.GetSigners()
|
|
|
|
// TODO: fix this !
|
|
|
|
require.Equal(t, fmt.Sprintf("%v", res), "[696E70757431]")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgMultiSendRoute(t *testing.T) {
|
2018-04-18 21:49:24 -07:00
|
|
|
// Construct a MsgSend
|
2018-07-06 00:06:53 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte("input"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("output"))
|
2019-03-07 16:55:08 -08:00
|
|
|
coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10))
|
2019-02-04 15:58:02 -08:00
|
|
|
var msg = MsgMultiSend{
|
2018-03-03 23:36:10 -08:00
|
|
|
Inputs: []Input{NewInput(addr1, coins)},
|
|
|
|
Outputs: []Output{NewOutput(addr2, coins)},
|
2018-02-20 15:57:24 -08:00
|
|
|
}
|
|
|
|
|
2018-02-20 17:15:57 -08:00
|
|
|
// TODO some failures for bad result
|
2019-05-16 08:25:32 -07:00
|
|
|
require.Equal(t, msg.Route(), RouterKey)
|
2019-02-04 15:58:02 -08:00
|
|
|
require.Equal(t, msg.Type(), "multisend")
|
2018-02-20 15:57:24 -08:00
|
|
|
}
|
|
|
|
|
2018-01-15 17:21:33 -08:00
|
|
|
func TestInputValidation(t *testing.T) {
|
2018-07-06 00:06:53 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte{1, 2})
|
|
|
|
addr2 := sdk.AccAddress([]byte{7, 8})
|
2019-03-07 16:55:08 -08:00
|
|
|
someCoins := sdk.NewCoins(sdk.NewInt64Coin("atom", 123))
|
|
|
|
multiCoins := sdk.NewCoins(sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 20))
|
2018-01-06 20:54:04 -08:00
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
var emptyAddr sdk.AccAddress
|
2019-03-07 16:55:08 -08:00
|
|
|
emptyCoins := sdk.NewCoins()
|
|
|
|
emptyCoins2 := sdk.NewCoins(sdk.NewInt64Coin("eth", 0))
|
2018-07-30 17:09:50 -07:00
|
|
|
someEmptyCoins := sdk.Coins{sdk.NewInt64Coin("eth", 10), sdk.NewInt64Coin("atom", 0)}
|
|
|
|
unsortedCoins := sdk.Coins{sdk.NewInt64Coin("eth", 1), sdk.NewInt64Coin("atom", 1)}
|
2018-01-06 20:54:04 -08:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
valid bool
|
2018-01-15 17:21:33 -08:00
|
|
|
txIn Input
|
2018-01-06 20:54:04 -08:00
|
|
|
}{
|
|
|
|
// auth works with different apps
|
2018-01-15 17:21:33 -08:00
|
|
|
{true, NewInput(addr1, someCoins)},
|
2018-03-03 23:36:10 -08:00
|
|
|
{true, NewInput(addr2, someCoins)},
|
|
|
|
{true, NewInput(addr2, multiCoins)},
|
|
|
|
|
|
|
|
{false, NewInput(emptyAddr, someCoins)}, // empty address
|
|
|
|
{false, NewInput(addr1, emptyCoins)}, // invalid coins
|
|
|
|
{false, NewInput(addr1, emptyCoins2)}, // invalid coins
|
|
|
|
{false, NewInput(addr1, someEmptyCoins)}, // invalid coins
|
|
|
|
{false, NewInput(addr1, unsortedCoins)}, // unsorted coins
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.txIn.ValidateBasic()
|
|
|
|
if tc.valid {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err, "%d: %+v", i, err)
|
2018-01-06 20:54:04 -08:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, err, "%d", i)
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 17:21:33 -08:00
|
|
|
func TestOutputValidation(t *testing.T) {
|
2018-07-06 00:06:53 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte{1, 2})
|
|
|
|
addr2 := sdk.AccAddress([]byte{7, 8})
|
2019-03-07 16:55:08 -08:00
|
|
|
someCoins := sdk.NewCoins(sdk.NewInt64Coin("atom", 123))
|
|
|
|
multiCoins := sdk.NewCoins(sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 20))
|
2018-01-06 20:54:04 -08:00
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
var emptyAddr sdk.AccAddress
|
2019-03-07 16:55:08 -08:00
|
|
|
emptyCoins := sdk.NewCoins()
|
|
|
|
emptyCoins2 := sdk.NewCoins(sdk.NewInt64Coin("eth", 0))
|
2018-07-30 17:09:50 -07:00
|
|
|
someEmptyCoins := sdk.Coins{sdk.NewInt64Coin("eth", 10), sdk.NewInt64Coin("atom", 0)}
|
|
|
|
unsortedCoins := sdk.Coins{sdk.NewInt64Coin("eth", 1), sdk.NewInt64Coin("atom", 1)}
|
2018-01-06 20:54:04 -08:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
valid bool
|
2018-01-15 17:21:33 -08:00
|
|
|
txOut Output
|
2018-01-06 20:54:04 -08:00
|
|
|
}{
|
|
|
|
// auth works with different apps
|
2018-01-15 17:21:33 -08:00
|
|
|
{true, NewOutput(addr1, someCoins)},
|
|
|
|
{true, NewOutput(addr2, someCoins)},
|
|
|
|
{true, NewOutput(addr2, multiCoins)},
|
|
|
|
|
|
|
|
{false, NewOutput(emptyAddr, someCoins)}, // empty address
|
|
|
|
{false, NewOutput(addr1, emptyCoins)}, // invalid coins
|
|
|
|
{false, NewOutput(addr1, emptyCoins2)}, // invalid coins
|
|
|
|
{false, NewOutput(addr1, someEmptyCoins)}, // invalid coins
|
|
|
|
{false, NewOutput(addr1, unsortedCoins)}, // unsorted coins
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.txOut.ValidateBasic()
|
|
|
|
if tc.valid {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err, "%d: %+v", i, err)
|
2018-01-06 20:54:04 -08:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, err, "%d", i)
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 15:58:02 -08:00
|
|
|
func TestMsgMultiSendValidation(t *testing.T) {
|
2018-07-06 00:06:53 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte{1, 2})
|
|
|
|
addr2 := sdk.AccAddress([]byte{7, 8})
|
2019-03-07 16:55:08 -08:00
|
|
|
atom123 := sdk.NewCoins(sdk.NewInt64Coin("atom", 123))
|
|
|
|
atom124 := sdk.NewCoins(sdk.NewInt64Coin("atom", 124))
|
|
|
|
eth123 := sdk.NewCoins(sdk.NewInt64Coin("eth", 123))
|
|
|
|
atom123eth123 := sdk.NewCoins(sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 123))
|
2018-01-06 20:54:04 -08:00
|
|
|
|
2018-01-15 17:21:33 -08:00
|
|
|
input1 := NewInput(addr1, atom123)
|
|
|
|
input2 := NewInput(addr1, eth123)
|
|
|
|
output1 := NewOutput(addr2, atom123)
|
|
|
|
output2 := NewOutput(addr2, atom124)
|
|
|
|
outputMulti := NewOutput(addr2, atom123eth123)
|
2018-01-06 20:54:04 -08:00
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
var emptyAddr sdk.AccAddress
|
2018-01-06 20:54:04 -08:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
valid bool
|
2019-02-04 15:58:02 -08:00
|
|
|
tx MsgMultiSend
|
2018-01-06 20:54:04 -08:00
|
|
|
}{
|
2019-02-04 15:58:02 -08:00
|
|
|
{false, MsgMultiSend{}}, // no input or output
|
|
|
|
{false, MsgMultiSend{Inputs: []Input{input1}}}, // just input
|
|
|
|
{false, MsgMultiSend{Outputs: []Output{output1}}}, // just output
|
|
|
|
{false, MsgMultiSend{
|
2018-03-03 23:36:10 -08:00
|
|
|
Inputs: []Input{NewInput(emptyAddr, atom123)}, // invalid input
|
2018-01-15 17:21:33 -08:00
|
|
|
Outputs: []Output{output1}}},
|
2019-02-04 15:58:02 -08:00
|
|
|
{false, MsgMultiSend{
|
2018-01-15 17:21:33 -08:00
|
|
|
Inputs: []Input{input1},
|
2018-06-25 11:23:17 -07:00
|
|
|
Outputs: []Output{{emptyAddr, atom123}}}, // invalid output
|
2018-01-06 20:54:04 -08:00
|
|
|
},
|
2019-02-04 15:58:02 -08:00
|
|
|
{false, MsgMultiSend{
|
2018-01-15 17:21:33 -08:00
|
|
|
Inputs: []Input{input1},
|
|
|
|
Outputs: []Output{output2}}, // amounts dont match
|
2018-01-06 20:54:04 -08:00
|
|
|
},
|
2019-02-04 15:58:02 -08:00
|
|
|
{true, MsgMultiSend{
|
2018-01-15 17:21:33 -08:00
|
|
|
Inputs: []Input{input1},
|
|
|
|
Outputs: []Output{output1}},
|
2018-01-06 20:54:04 -08:00
|
|
|
},
|
2019-02-04 15:58:02 -08:00
|
|
|
{true, MsgMultiSend{
|
2018-01-15 17:21:33 -08:00
|
|
|
Inputs: []Input{input1, input2},
|
|
|
|
Outputs: []Output{outputMulti}},
|
2018-01-06 20:54:04 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.tx.ValidateBasic()
|
|
|
|
if tc.valid {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, err, "%d: %+v", i, err)
|
2018-01-06 20:54:04 -08:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, err, "%d", i)
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 15:58:02 -08:00
|
|
|
func TestMsgMultiSendGetSignBytes(t *testing.T) {
|
2018-07-06 00:06:53 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte("input"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("output"))
|
2019-03-07 16:55:08 -08:00
|
|
|
coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10))
|
2019-02-04 15:58:02 -08:00
|
|
|
var msg = MsgMultiSend{
|
2018-03-03 23:50:57 -08:00
|
|
|
Inputs: []Input{NewInput(addr1, coins)},
|
|
|
|
Outputs: []Output{NewOutput(addr2, coins)},
|
2018-02-20 15:57:24 -08:00
|
|
|
}
|
2018-02-20 17:15:57 -08:00
|
|
|
res := msg.GetSignBytes()
|
2018-05-28 11:26:43 -07:00
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
expected := `{"type":"cosmos-sdk/MsgMultiSend","value":{"inputs":[{"address":"cosmos1d9h8qat57ljhcm","coins":[{"amount":"10","denom":"atom"}]}],"outputs":[{"address":"cosmos1da6hgur4wsmpnjyg","coins":[{"amount":"10","denom":"atom"}]}]}}`
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, expected, string(res))
|
2018-02-20 17:15:57 -08:00
|
|
|
}
|
2018-02-20 15:57:24 -08:00
|
|
|
|
2019-02-04 15:58:02 -08:00
|
|
|
func TestMsgMultiSendGetSigners(t *testing.T) {
|
|
|
|
var msg = MsgMultiSend{
|
2018-02-20 17:15:57 -08:00
|
|
|
Inputs: []Input{
|
2018-07-06 00:06:53 -07:00
|
|
|
NewInput(sdk.AccAddress([]byte("input1")), nil),
|
|
|
|
NewInput(sdk.AccAddress([]byte("input2")), nil),
|
|
|
|
NewInput(sdk.AccAddress([]byte("input3")), nil),
|
2018-02-20 17:15:57 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
res := msg.GetSigners()
|
2018-03-03 23:50:57 -08:00
|
|
|
// TODO: fix this !
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, fmt.Sprintf("%v", res), "[696E70757431 696E70757432 696E70757433]")
|
2018-02-20 15:57:24 -08:00
|
|
|
}
|
|
|
|
|
2018-01-15 17:21:33 -08:00
|
|
|
/*
|
2018-02-20 17:15:57 -08:00
|
|
|
// what to do w/ this test?
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgSendSigners(t *testing.T) {
|
2018-07-06 00:06:53 -07:00
|
|
|
signers := []sdk.AccAddress{
|
2018-01-06 20:54:04 -08:00
|
|
|
{1, 2, 3},
|
|
|
|
{4, 5, 6},
|
|
|
|
{7, 8, 9},
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:55:08 -08:00
|
|
|
someCoins := sdk.NewCoins(sdk.NewInt64Coin("atom", 123))
|
2018-01-15 17:21:33 -08:00
|
|
|
inputs := make([]Input, len(signers))
|
2018-01-06 20:54:04 -08:00
|
|
|
for i, signer := range signers {
|
2018-01-15 17:21:33 -08:00
|
|
|
inputs[i] = NewInput(signer, someCoins)
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
2018-04-18 21:49:24 -07:00
|
|
|
tx := NewMsgSend(inputs, nil)
|
2018-01-06 20:54:04 -08:00
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, signers, tx.Signers())
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
2018-01-15 17:21:33 -08:00
|
|
|
*/
|