2019-06-05 16:26:17 -07:00
|
|
|
package types
|
2018-01-06 20:54:04 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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) {
|
2020-08-17 07:07:27 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte("from________________"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("to__________________"))
|
|
|
|
addrEmpty := sdk.AccAddress([]byte(""))
|
2021-02-01 05:17:44 -08:00
|
|
|
addrLong := sdk.AccAddress([]byte("Purposefully long address"))
|
2020-08-17 07:07:27 -07:00
|
|
|
|
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)}
|
|
|
|
|
|
|
|
cases := []struct {
|
2020-08-17 07:07:27 -07:00
|
|
|
expectedErr string // empty means no error expected
|
|
|
|
msg *MsgSend
|
2019-02-04 15:58:02 -08:00
|
|
|
}{
|
2020-08-17 07:07:27 -07:00
|
|
|
{"", NewMsgSend(addr1, addr2, atom123)}, // valid send
|
|
|
|
{"", NewMsgSend(addr1, addr2, atom123eth123)}, // valid send with multiple coins
|
2021-02-01 05:17:44 -08:00
|
|
|
{"", NewMsgSend(addrLong, addr2, atom123)}, // valid send with long addr sender
|
|
|
|
{"", NewMsgSend(addr1, addrLong, atom123)}, // valid send with long addr recipient
|
2020-08-17 07:07:27 -07:00
|
|
|
{": invalid coins", NewMsgSend(addr1, addr2, atom0)}, // non positive coin
|
|
|
|
{"123atom,0eth: invalid coins", NewMsgSend(addr1, addr2, atom123eth0)}, // non positive coin in multicoins
|
2021-08-13 08:34:00 -07:00
|
|
|
{"invalid from address: empty address string is not allowed: invalid address", NewMsgSend(addrEmpty, addr2, atom123)},
|
|
|
|
{"invalid to address: empty address string is not allowed: invalid address", NewMsgSend(addr1, addrEmpty, atom123)},
|
2019-02-04 15:58:02 -08:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:55:08 -08:00
|
|
|
for _, tc := range cases {
|
2020-08-17 07:07:27 -07:00
|
|
|
err := tc.msg.ValidateBasic()
|
|
|
|
if tc.expectedErr == "" {
|
2019-03-07 16:55:08 -08:00
|
|
|
require.Nil(t, err)
|
2019-02-04 15:58:02 -08:00
|
|
|
} else {
|
2020-08-17 07:07:27 -07:00
|
|
|
require.EqualError(t, err, tc.expectedErr)
|
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 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) {
|
2020-08-17 07:07:27 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte("_______alice________"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("________bob_________"))
|
|
|
|
addrEmpty := sdk.AccAddress([]byte(""))
|
2021-02-01 05:17:44 -08:00
|
|
|
addrLong := sdk.AccAddress([]byte("Purposefully long address"))
|
2020-08-17 07:07:27 -07:00
|
|
|
|
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
|
|
|
|
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 {
|
2020-08-17 07:07:27 -07:00
|
|
|
expectedErr string // empty means no error expected
|
|
|
|
txIn Input
|
2018-01-06 20:54:04 -08:00
|
|
|
}{
|
|
|
|
// auth works with different apps
|
2020-08-17 07:07:27 -07:00
|
|
|
{"", NewInput(addr1, someCoins)},
|
|
|
|
{"", NewInput(addr2, someCoins)},
|
|
|
|
{"", NewInput(addr2, multiCoins)},
|
2021-02-01 05:17:44 -08:00
|
|
|
{"", NewInput(addrLong, someCoins)},
|
2020-08-17 07:07:27 -07:00
|
|
|
|
2021-08-13 08:34:00 -07:00
|
|
|
{"invalid input address: empty address string is not allowed: invalid address", NewInput(addrEmpty, someCoins)},
|
2020-08-17 07:07:27 -07:00
|
|
|
{": invalid coins", NewInput(addr1, emptyCoins)}, // invalid coins
|
|
|
|
{": invalid coins", NewInput(addr1, emptyCoins2)}, // invalid coins
|
|
|
|
{"10eth,0atom: invalid coins", NewInput(addr1, someEmptyCoins)}, // invalid coins
|
|
|
|
{"1eth,1atom: invalid coins", NewInput(addr1, unsortedCoins)}, // unsorted coins
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.txIn.ValidateBasic()
|
2020-08-17 07:07:27 -07:00
|
|
|
if tc.expectedErr == "" {
|
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 {
|
2020-08-17 07:07:27 -07:00
|
|
|
require.EqualError(t, err, tc.expectedErr, "%d", i)
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 17:21:33 -08:00
|
|
|
func TestOutputValidation(t *testing.T) {
|
2020-08-17 07:07:27 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte("_______alice________"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("________bob_________"))
|
|
|
|
addrEmpty := sdk.AccAddress([]byte(""))
|
2021-02-01 05:17:44 -08:00
|
|
|
addrLong := sdk.AccAddress([]byte("Purposefully long address"))
|
2020-08-17 07:07:27 -07:00
|
|
|
|
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
|
|
|
|
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 {
|
2020-08-17 07:07:27 -07:00
|
|
|
expectedErr string // empty means no error expected
|
|
|
|
txOut Output
|
2018-01-06 20:54:04 -08:00
|
|
|
}{
|
|
|
|
// auth works with different apps
|
2020-08-17 07:07:27 -07:00
|
|
|
{"", NewOutput(addr1, someCoins)},
|
|
|
|
{"", NewOutput(addr2, someCoins)},
|
|
|
|
{"", NewOutput(addr2, multiCoins)},
|
2021-02-01 05:17:44 -08:00
|
|
|
{"", NewOutput(addrLong, someCoins)},
|
2020-08-17 07:07:27 -07:00
|
|
|
|
2021-08-13 08:34:00 -07:00
|
|
|
{"invalid output address: empty address string is not allowed: invalid address", NewOutput(addrEmpty, someCoins)},
|
2020-08-17 07:07:27 -07:00
|
|
|
{": invalid coins", NewOutput(addr1, emptyCoins)}, // invalid coins
|
|
|
|
{": invalid coins", NewOutput(addr1, emptyCoins2)}, // invalid coins
|
|
|
|
{"10eth,0atom: invalid coins", NewOutput(addr1, someEmptyCoins)}, // invalid coins
|
|
|
|
{"1eth,1atom: invalid coins", NewOutput(addr1, unsortedCoins)}, // unsorted coins
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
err := tc.txOut.ValidateBasic()
|
2020-08-17 07:07:27 -07:00
|
|
|
if tc.expectedErr == "" {
|
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 {
|
2020-08-17 07:07:27 -07:00
|
|
|
require.EqualError(t, err, tc.expectedErr, "%d", i)
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 15:58:02 -08:00
|
|
|
func TestMsgMultiSendValidation(t *testing.T) {
|
2020-08-17 07:07:27 -07:00
|
|
|
addr1 := sdk.AccAddress([]byte("_______alice________"))
|
|
|
|
addr2 := sdk.AccAddress([]byte("________bob_________"))
|
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},
|
2020-09-25 03:25:37 -07:00
|
|
|
Outputs: []Output{{emptyAddr.String(), 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) {
|
2021-07-07 03:18:00 -07:00
|
|
|
addrs := make([]string, 3)
|
|
|
|
inputs := make([]Input, 3)
|
|
|
|
for i, v := range []string{"input111111111111111", "input222222222222222", "input333333333333333"} {
|
|
|
|
addr := sdk.AccAddress([]byte(v))
|
|
|
|
inputs[i] = NewInput(addr, nil)
|
|
|
|
addrs[i] = addr.String()
|
2018-02-20 17:15:57 -08:00
|
|
|
}
|
2021-07-07 03:18:00 -07:00
|
|
|
var msg = NewMsgMultiSend(inputs, nil)
|
2020-09-25 03:25:37 -07:00
|
|
|
|
2018-02-20 17:15:57 -08:00
|
|
|
res := msg.GetSigners()
|
2021-08-13 08:34:00 -07:00
|
|
|
for i, signer := range res {
|
|
|
|
require.Equal(t, signer.String(), addrs[i])
|
|
|
|
}
|
2018-02-20 15:57:24 -08:00
|
|
|
}
|
|
|
|
|
2021-07-07 03:18:00 -07:00
|
|
|
func TestMsgSendGetSigners(t *testing.T) {
|
|
|
|
from := sdk.AccAddress([]byte("input111111111111111"))
|
|
|
|
msg := NewMsgSend(from, sdk.AccAddress{}, sdk.NewCoins())
|
|
|
|
res := msg.GetSigners()
|
2021-08-13 08:34:00 -07:00
|
|
|
require.Equal(t, 1, len(res))
|
|
|
|
require.True(t, from.Equals(res[0]))
|
2018-01-06 20:54:04 -08:00
|
|
|
}
|