cosmos-sdk/x/bank/msgs_test.go

263 lines
8.0 KiB
Go
Raw Normal View History

package bank
import (
2018-02-20 17:15:57 -08:00
"fmt"
"testing"
"github.com/stretchr/testify/require"
2018-01-12 12:03:23 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestMsgSendRoute(t *testing.T) {
addr1 := sdk.AccAddress([]byte("from"))
addr2 := sdk.AccAddress([]byte("to"))
coins := sdk.Coins{sdk.NewInt64Coin("atom", 10)}
var msg = NewMsgSend(addr1, addr2, coins)
require.Equal(t, msg.Route(), "bank")
require.Equal(t, msg.Type(), "send")
}
func TestMsgSendValidation(t *testing.T) {
addr1 := sdk.AccAddress([]byte("from"))
addr2 := sdk.AccAddress([]byte("to"))
atom123 := sdk.Coins{sdk.NewInt64Coin("atom", 123)}
atom0 := sdk.Coins{sdk.NewInt64Coin("atom", 0)}
atom123eth123 := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 123)}
atom123eth0 := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 0)}
var emptyAddr sdk.AccAddress
cases := []struct {
valid bool
tx MsgSend
}{
{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
}
for i, tc := range cases {
err := tc.tx.ValidateBasic()
if tc.valid {
require.Nil(t, err, "%d: %+v", i, err)
} else {
require.NotNil(t, err, "%d", i)
}
}
}
func TestMsgSendGetSignBytes(t *testing.T) {
addr1 := sdk.AccAddress([]byte("input"))
addr2 := sdk.AccAddress([]byte("output"))
coins := sdk.Coins{sdk.NewInt64Coin("atom", 10)}
var msg = NewMsgSend(addr1, addr2, coins)
res := msg.GetSignBytes()
expected := `{"type":"cosmos-sdk/Send","value":{"amount":[{"amount":"10","denom":"atom"}],"from_address":"cosmos1d9h8qat57ljhcm","to_address":"cosmos1da6hgur4wsmpnjyg"}}`
require.Equal(t, expected, string(res))
}
func TestMsgSendGetSigners(t *testing.T) {
var msg = NewMsgSend(sdk.AccAddress([]byte("input1")), sdk.AccAddress{}, sdk.Coins{})
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"))
2018-07-30 17:09:50 -07:00
coins := sdk.Coins{sdk.NewInt64Coin("atom", 10)}
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
require.Equal(t, msg.Route(), "bank")
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})
2018-07-30 17:09:50 -07:00
someCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123)}
multiCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 20)}
2018-07-06 00:06:53 -07:00
var emptyAddr sdk.AccAddress
2018-01-12 12:03:23 -08:00
emptyCoins := sdk.Coins{}
2018-07-30 17:09:50 -07:00
emptyCoins2 := sdk.Coins{sdk.NewInt64Coin("eth", 0)}
someEmptyCoins := sdk.Coins{sdk.NewInt64Coin("eth", 10), sdk.NewInt64Coin("atom", 0)}
unsortedCoins := sdk.Coins{sdk.NewInt64Coin("eth", 1), sdk.NewInt64Coin("atom", 1)}
cases := []struct {
valid bool
2018-01-15 17:21:33 -08:00
txIn Input
}{
// 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
}
for i, tc := range cases {
err := tc.txIn.ValidateBasic()
if tc.valid {
require.Nil(t, err, "%d: %+v", i, err)
} else {
require.NotNil(t, err, "%d", i)
}
}
}
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})
2018-07-30 17:09:50 -07:00
someCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123)}
multiCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 20)}
2018-07-06 00:06:53 -07:00
var emptyAddr sdk.AccAddress
2018-01-12 12:03:23 -08:00
emptyCoins := sdk.Coins{}
2018-07-30 17:09:50 -07:00
emptyCoins2 := sdk.Coins{sdk.NewInt64Coin("eth", 0)}
someEmptyCoins := sdk.Coins{sdk.NewInt64Coin("eth", 10), sdk.NewInt64Coin("atom", 0)}
unsortedCoins := sdk.Coins{sdk.NewInt64Coin("eth", 1), sdk.NewInt64Coin("atom", 1)}
cases := []struct {
valid bool
2018-01-15 17:21:33 -08:00
txOut Output
}{
// 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
}
for i, tc := range cases {
err := tc.txOut.ValidateBasic()
if tc.valid {
require.Nil(t, err, "%d: %+v", i, err)
} else {
require.NotNil(t, err, "%d", i)
}
}
}
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})
2018-07-30 17:09:50 -07:00
atom123 := sdk.Coins{sdk.NewInt64Coin("atom", 123)}
atom124 := sdk.Coins{sdk.NewInt64Coin("atom", 124)}
eth123 := sdk.Coins{sdk.NewInt64Coin("eth", 123)}
atom123eth123 := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 123)}
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-07-06 00:06:53 -07:00
var emptyAddr sdk.AccAddress
cases := []struct {
valid bool
tx MsgMultiSend
}{
{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}}},
{false, MsgMultiSend{
2018-01-15 17:21:33 -08:00
Inputs: []Input{input1},
Outputs: []Output{{emptyAddr, atom123}}}, // invalid output
},
{false, MsgMultiSend{
2018-01-15 17:21:33 -08:00
Inputs: []Input{input1},
Outputs: []Output{output2}}, // amounts dont match
},
{true, MsgMultiSend{
2018-01-15 17:21:33 -08:00
Inputs: []Input{input1},
Outputs: []Output{output1}},
},
{true, MsgMultiSend{
2018-01-15 17:21:33 -08:00
Inputs: []Input{input1, input2},
Outputs: []Output{outputMulti}},
},
}
for i, tc := range cases {
err := tc.tx.ValidateBasic()
if tc.valid {
require.Nil(t, err, "%d: %+v", i, err)
} else {
require.NotNil(t, err, "%d", i)
}
}
}
func TestMsgMultiSendGetSignBytes(t *testing.T) {
2018-07-06 00:06:53 -07:00
addr1 := sdk.AccAddress([]byte("input"))
addr2 := sdk.AccAddress([]byte("output"))
2018-07-30 17:09:50 -07:00
coins := sdk.Coins{sdk.NewInt64Coin("atom", 10)}
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()
expected := `{"type":"cosmos-sdk/MultiSend","value":{"inputs":[{"address":"cosmos1d9h8qat57ljhcm","coins":[{"amount":"10","denom":"atom"}]}],"outputs":[{"address":"cosmos1da6hgur4wsmpnjyg","coins":[{"amount":"10","denom":"atom"}]}]}}`
require.Equal(t, expected, string(res))
2018-02-20 17:15:57 -08:00
}
2018-02-20 15:57:24 -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 !
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{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
2018-07-30 17:09:50 -07:00
someCoins := sdk.Coins{sdk.NewInt64Coin("atom", 123)}
2018-01-15 17:21:33 -08:00
inputs := make([]Input, len(signers))
for i, signer := range signers {
2018-01-15 17:21:33 -08:00
inputs[i] = NewInput(signer, someCoins)
}
2018-04-18 21:49:24 -07:00
tx := NewMsgSend(inputs, nil)
require.Equal(t, signers, tx.Signers())
}
2018-01-15 17:21:33 -08:00
*/