2018-05-23 22:09:01 -07:00
|
|
|
package auth
|
2018-03-17 13:54:21 -07:00
|
|
|
|
|
|
|
import (
|
2018-07-06 16:26:22 -07:00
|
|
|
"fmt"
|
2018-12-12 13:29:42 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-11-21 02:16:56 -08:00
|
|
|
"strings"
|
2018-03-17 13:54:21 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-11-21 02:16:56 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2018-07-25 13:43:37 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2018-11-21 02:16:56 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-09-26 06:39:05 -07:00
|
|
|
)
|
2018-05-23 22:09:01 -07:00
|
|
|
|
2018-09-26 06:39:05 -07:00
|
|
|
var (
|
|
|
|
priv = ed25519.GenPrivKey()
|
|
|
|
addr = sdk.AccAddress(priv.PubKey().Address())
|
2018-03-17 13:54:21 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStdTx(t *testing.T) {
|
2018-06-21 15:05:25 -07:00
|
|
|
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
|
2018-03-17 13:54:21 -07:00
|
|
|
fee := newStdFee()
|
|
|
|
sigs := []StdSignature{}
|
|
|
|
|
2018-06-21 15:05:25 -07:00
|
|
|
tx := NewStdTx(msgs, fee, sigs, "")
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, msgs, tx.GetMsgs())
|
|
|
|
require.Equal(t, sigs, tx.GetSignatures())
|
2018-03-17 13:54:21 -07:00
|
|
|
|
2018-09-26 11:34:01 -07:00
|
|
|
feePayer := tx.GetSigners()[0]
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, addr, feePayer)
|
2018-03-17 13:54:21 -07:00
|
|
|
}
|
2018-07-06 16:26:22 -07:00
|
|
|
|
|
|
|
func TestStdSignBytes(t *testing.T) {
|
2018-09-26 06:39:05 -07:00
|
|
|
type args struct {
|
|
|
|
chainID string
|
2018-11-26 03:29:21 -08:00
|
|
|
accnum uint64
|
|
|
|
sequence uint64
|
2018-09-26 06:39:05 -07:00
|
|
|
fee StdFee
|
|
|
|
msgs []sdk.Msg
|
|
|
|
memo string
|
|
|
|
}
|
|
|
|
defaultFee := newStdFee()
|
|
|
|
tests := []struct {
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args{"1234", 3, 6, defaultFee, []sdk.Msg{sdk.NewTestMsg(addr)}, "memo"},
|
2018-12-10 02:48:19 -08:00
|
|
|
fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"50000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr),
|
2018-09-26 06:39:05 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tc := range tests {
|
|
|
|
got := string(StdSignBytes(tc.args.chainID, tc.args.accnum, tc.args.sequence, tc.args.fee, tc.args.msgs, tc.args.memo))
|
|
|
|
require.Equal(t, tc.want, got, "Got unexpected result on test case i: %d", i)
|
2018-07-06 16:26:22 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 02:16:56 -08:00
|
|
|
|
|
|
|
func TestTxValidateBasic(t *testing.T) {
|
|
|
|
ctx := sdk.NewContext(nil, abci.Header{ChainID: "mychainid"}, false, log.NewNopLogger())
|
|
|
|
|
|
|
|
// keys and addresses
|
|
|
|
priv1, addr1 := privAndAddr()
|
|
|
|
priv2, addr2 := privAndAddr()
|
|
|
|
priv3, addr3 := privAndAddr()
|
|
|
|
priv4, addr4 := privAndAddr()
|
|
|
|
priv5, addr5 := privAndAddr()
|
|
|
|
priv6, addr6 := privAndAddr()
|
|
|
|
priv7, addr7 := privAndAddr()
|
|
|
|
priv8, addr8 := privAndAddr()
|
|
|
|
|
|
|
|
// msg and signatures
|
|
|
|
msg1 := newTestMsg(addr1, addr2)
|
|
|
|
fee := newStdFee()
|
|
|
|
|
|
|
|
msgs := []sdk.Msg{msg1}
|
|
|
|
|
|
|
|
// require to fail validation upon invalid fee
|
|
|
|
badFee := newStdFee()
|
|
|
|
badFee.Amount[0].Amount = sdk.NewInt(-5)
|
|
|
|
tx := newTestTx(ctx, nil, nil, nil, nil, badFee)
|
|
|
|
|
|
|
|
err := tx.ValidateBasic()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, sdk.CodeInsufficientFee, err.Result().Code)
|
|
|
|
|
|
|
|
// require to fail validation when no signatures exist
|
2018-11-26 03:29:21 -08:00
|
|
|
privs, accNums, seqs := []crypto.PrivKey{}, []uint64{}, []uint64{}
|
2018-11-21 02:16:56 -08:00
|
|
|
tx = newTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
|
|
|
|
|
|
|
err = tx.ValidateBasic()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, sdk.CodeUnauthorized, err.Result().Code)
|
|
|
|
|
|
|
|
// require to fail validation when signatures do not match expected signers
|
2018-11-26 03:29:21 -08:00
|
|
|
privs, accNums, seqs = []crypto.PrivKey{priv1}, []uint64{0, 1}, []uint64{0, 0}
|
2018-11-21 02:16:56 -08:00
|
|
|
tx = newTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
|
|
|
|
|
|
|
err = tx.ValidateBasic()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, sdk.CodeUnauthorized, err.Result().Code)
|
|
|
|
|
|
|
|
// require to fail validation when memo is too large
|
|
|
|
badMemo := strings.Repeat("bad memo", 50)
|
2018-11-26 03:29:21 -08:00
|
|
|
privs, accNums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{0, 0}
|
2018-11-21 02:16:56 -08:00
|
|
|
tx = newTestTxWithMemo(ctx, msgs, privs, accNums, seqs, fee, badMemo)
|
|
|
|
|
|
|
|
err = tx.ValidateBasic()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, sdk.CodeMemoTooLarge, err.Result().Code)
|
|
|
|
|
|
|
|
// require to fail validation when there are too many signatures
|
|
|
|
privs = []crypto.PrivKey{priv1, priv2, priv3, priv4, priv5, priv6, priv7, priv8}
|
2018-11-26 03:29:21 -08:00
|
|
|
accNums, seqs = []uint64{0, 0, 0, 0, 0, 0, 0, 0}, []uint64{0, 0, 0, 0, 0, 0, 0, 0}
|
2018-11-21 02:16:56 -08:00
|
|
|
badMsg := newTestMsg(addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8)
|
|
|
|
badMsgs := []sdk.Msg{badMsg}
|
|
|
|
tx = newTestTx(ctx, badMsgs, privs, accNums, seqs, fee)
|
|
|
|
|
|
|
|
err = tx.ValidateBasic()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, sdk.CodeTooManySignatures, err.Result().Code)
|
|
|
|
|
2018-12-10 11:24:57 -08:00
|
|
|
// require to fail with invalid gas supplied
|
|
|
|
badFee = newStdFee()
|
|
|
|
badFee.Gas = 9223372036854775808
|
|
|
|
tx = newTestTx(ctx, nil, nil, nil, nil, badFee)
|
|
|
|
|
|
|
|
err = tx.ValidateBasic()
|
|
|
|
require.Error(t, err)
|
2018-12-10 12:39:29 -08:00
|
|
|
require.Equal(t, sdk.CodeGasOverflow, err.Result().Code)
|
2018-12-10 11:24:57 -08:00
|
|
|
|
2018-11-21 02:16:56 -08:00
|
|
|
// require to pass when above criteria are matched
|
2018-11-26 03:29:21 -08:00
|
|
|
privs, accNums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{0, 0}
|
2018-11-21 02:16:56 -08:00
|
|
|
tx = newTestTx(ctx, msgs, privs, accNums, seqs, fee)
|
|
|
|
|
|
|
|
err = tx.ValidateBasic()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2018-12-12 13:29:42 -08:00
|
|
|
|
|
|
|
func TestDefaultTxEncoder(t *testing.T) {
|
|
|
|
cdc := codec.New()
|
|
|
|
sdk.RegisterCodec(cdc)
|
|
|
|
RegisterCodec(cdc)
|
|
|
|
cdc.RegisterConcrete(sdk.TestMsg{}, "cosmos-sdk/Test", nil)
|
|
|
|
encoder := DefaultTxEncoder(cdc)
|
|
|
|
|
|
|
|
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
|
|
|
|
fee := newStdFee()
|
|
|
|
sigs := []StdSignature{}
|
|
|
|
|
|
|
|
tx := NewStdTx(msgs, fee, sigs, "")
|
|
|
|
|
|
|
|
cdcBytes, err := cdc.MarshalBinaryLengthPrefixed(tx)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
encoderBytes, err := encoder(tx)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, cdcBytes, encoderBytes)
|
|
|
|
}
|