From e6a8b2278e84d0ff2bc0362e1462d1292184ab7e Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Wed, 26 Sep 2018 06:39:05 -0700 Subject: [PATCH] Merge PR #2377: auth: Move stdsignmsg to txbuilder, add tests --- PENDING.md | 1 + x/auth/client/txbuilder/stdsignmsg.go | 23 +++++++ x/auth/client/txbuilder/txbuilder.go | 14 ++-- x/auth/client/txbuilder/txbuilder_test.go | 81 +++++++++++++++++++++++ x/auth/stdtx.go | 17 ----- x/auth/stdtx_test.go | 41 +++++++----- 6 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 x/auth/client/txbuilder/stdsignmsg.go create mode 100644 x/auth/client/txbuilder/txbuilder_test.go diff --git a/PENDING.md b/PENDING.md index aa328d969..bf9fc1d4a 100644 --- a/PENDING.md +++ b/PENDING.md @@ -56,6 +56,7 @@ BREAKING CHANGES * [codec] \#2324 All referrences to wire have been renamed to codec. Additionally, wire.NewCodec is now codec.New(). * [types] \#2343 Make sdk.Msg have a names field, to facilitate automatic tagging. * [baseapp] \#2366 Automatically add action tags to all messages + * [x/auth] \#2377 auth.StdSignMsg -> txbuilder.StdSignMsg * [x/staking] \#2244 staking now holds a consensus-address-index instead of a consensus-pubkey-index * [x/staking] \#2236 more distribution hooks for distribution diff --git a/x/auth/client/txbuilder/stdsignmsg.go b/x/auth/client/txbuilder/stdsignmsg.go new file mode 100644 index 000000000..050a370ff --- /dev/null +++ b/x/auth/client/txbuilder/stdsignmsg.go @@ -0,0 +1,23 @@ +package context + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" +) + +// StdSignMsg is a convenience structure for passing along +// a Msg with the other requirements for a StdSignDoc before +// it is signed. For use in the CLI. +type StdSignMsg struct { + ChainID string `json:"chain_id"` + AccountNumber int64 `json:"account_number"` + Sequence int64 `json:"sequence"` + Fee auth.StdFee `json:"fee"` + Msgs []sdk.Msg `json:"msgs"` + Memo string `json:"memo"` +} + +// get message bytes +func (msg StdSignMsg) Bytes() []byte { + return auth.StdSignBytes(msg.ChainID, msg.AccountNumber, msg.Sequence, msg.Fee, msg.Msgs, msg.Memo) +} diff --git a/x/auth/client/txbuilder/txbuilder.go b/x/auth/client/txbuilder/txbuilder.go index 642893b52..b488ef359 100644 --- a/x/auth/client/txbuilder/txbuilder.go +++ b/x/auth/client/txbuilder/txbuilder.go @@ -92,23 +92,23 @@ func (bldr TxBuilder) WithAccountNumber(accnum int64) TxBuilder { // Build builds a single message to be signed from a TxBuilder given a set of // messages. It returns an error if a fee is supplied but cannot be parsed. -func (bldr TxBuilder) Build(msgs []sdk.Msg) (auth.StdSignMsg, error) { +func (bldr TxBuilder) Build(msgs []sdk.Msg) (StdSignMsg, error) { chainID := bldr.ChainID if chainID == "" { - return auth.StdSignMsg{}, errors.Errorf("chain ID required but not specified") + return StdSignMsg{}, errors.Errorf("chain ID required but not specified") } fee := sdk.Coin{} if bldr.Fee != "" { parsedFee, err := sdk.ParseCoin(bldr.Fee) if err != nil { - return auth.StdSignMsg{}, err + return StdSignMsg{}, err } fee = parsedFee } - return auth.StdSignMsg{ + return StdSignMsg{ ChainID: bldr.ChainID, AccountNumber: bldr.AccountNumber, Sequence: bldr.Sequence, @@ -120,7 +120,7 @@ func (bldr TxBuilder) Build(msgs []sdk.Msg) (auth.StdSignMsg, error) { // Sign signs a transaction given a name, passphrase, and a single message to // signed. An error is returned if signing fails. -func (bldr TxBuilder) Sign(name, passphrase string, msg auth.StdSignMsg) ([]byte, error) { +func (bldr TxBuilder) Sign(name, passphrase string, msg StdSignMsg) ([]byte, error) { sig, err := MakeSignature(name, passphrase, msg) if err != nil { return nil, err @@ -172,7 +172,7 @@ func (bldr TxBuilder) BuildWithPubKey(name string, msgs []sdk.Msg) ([]byte, erro // SignStdTx appends a signature to a StdTx and returns a copy of a it. If append // is false, it replaces the signatures already attached with the new signature. func (bldr TxBuilder) SignStdTx(name, passphrase string, stdTx auth.StdTx, appendSig bool) (signedStdTx auth.StdTx, err error) { - stdSignature, err := MakeSignature(name, passphrase, auth.StdSignMsg{ + stdSignature, err := MakeSignature(name, passphrase, StdSignMsg{ ChainID: bldr.ChainID, AccountNumber: bldr.AccountNumber, Sequence: bldr.Sequence, @@ -195,7 +195,7 @@ func (bldr TxBuilder) SignStdTx(name, passphrase string, stdTx auth.StdTx, appen } // MakeSignature builds a StdSignature given key name, passphrase, and a StdSignMsg. -func MakeSignature(name, passphrase string, msg auth.StdSignMsg) (sig auth.StdSignature, err error) { +func MakeSignature(name, passphrase string, msg StdSignMsg) (sig auth.StdSignature, err error) { keybase, err := keys.GetKeyBase() if err != nil { return diff --git a/x/auth/client/txbuilder/txbuilder_test.go b/x/auth/client/txbuilder/txbuilder_test.go new file mode 100644 index 000000000..996d9b8b1 --- /dev/null +++ b/x/auth/client/txbuilder/txbuilder_test.go @@ -0,0 +1,81 @@ +package context + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/tendermint/tendermint/crypto/ed25519" +) + +var ( + priv = ed25519.GenPrivKey() + addr = sdk.AccAddress(priv.PubKey().Address()) +) + +func TestTxBuilderBuild(t *testing.T) { + type fields struct { + Codec *codec.Codec + AccountNumber int64 + Sequence int64 + Gas int64 + GasAdjustment float64 + SimulateGas bool + ChainID string + Memo string + Fee string + } + defaultMsg := []sdk.Msg{sdk.NewTestMsg(addr)} + tests := []struct { + fields fields + msgs []sdk.Msg + want StdSignMsg + wantErr bool + }{ + { + fields{ + Codec: codec.New(), + AccountNumber: 1, + Sequence: 1, + Gas: 100, + GasAdjustment: 1.1, + SimulateGas: false, + ChainID: "test-chain", + Memo: "hello", + Fee: "1steak", + }, + defaultMsg, + StdSignMsg{ + ChainID: "test-chain", + AccountNumber: 1, + Sequence: 1, + Memo: "hello", + Msgs: defaultMsg, + Fee: auth.NewStdFee(100, sdk.NewCoin("steak", sdk.NewInt(1))), + }, + false, + }, + } + for i, tc := range tests { + bldr := TxBuilder{ + Codec: tc.fields.Codec, + AccountNumber: tc.fields.AccountNumber, + Sequence: tc.fields.Sequence, + Gas: tc.fields.Gas, + GasAdjustment: tc.fields.GasAdjustment, + SimulateGas: tc.fields.SimulateGas, + ChainID: tc.fields.ChainID, + Memo: tc.fields.Memo, + Fee: tc.fields.Fee, + } + got, err := bldr.Build(tc.msgs) + require.Equal(t, tc.wantErr, (err != nil), "TxBuilder.Build() error = %v, wantErr %v, tc %d", err, tc.wantErr, i) + if !reflect.DeepEqual(got, tc.want) { + t.Errorf("TxBuilder.Build() = %v, want %v", got, tc.want) + } + } +} diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go index e38dc0c7e..0c0a133ed 100644 --- a/x/auth/stdtx.go +++ b/x/auth/stdtx.go @@ -139,23 +139,6 @@ func StdSignBytes(chainID string, accnum int64, sequence int64, fee StdFee, msgs return sdk.MustSortJSON(bz) } -// StdSignMsg is a convenience structure for passing along -// a Msg with the other requirements for a StdSignDoc before -// it is signed. For use in the CLI. -type StdSignMsg struct { - ChainID string `json:"chain_id"` - AccountNumber int64 `json:"account_number"` - Sequence int64 `json:"sequence"` - Fee StdFee `json:"fee"` - Msgs []sdk.Msg `json:"msgs"` - Memo string `json:"memo"` -} - -// get message bytes -func (msg StdSignMsg) Bytes() []byte { - return StdSignBytes(msg.ChainID, msg.AccountNumber, msg.Sequence, msg.Fee, msg.Msgs, msg.Memo) -} - // Standard Signature type StdSignature struct { crypto.PubKey `json:"pub_key"` // optional diff --git a/x/auth/stdtx_test.go b/x/auth/stdtx_test.go index f6e3f8fc2..8d9172a92 100644 --- a/x/auth/stdtx_test.go +++ b/x/auth/stdtx_test.go @@ -4,15 +4,17 @@ import ( "fmt" "testing" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" +) - sdk "github.com/cosmos/cosmos-sdk/types" +var ( + priv = ed25519.GenPrivKey() + addr = sdk.AccAddress(priv.PubKey().Address()) ) func TestStdTx(t *testing.T) { - priv := ed25519.GenPrivKey() - addr := sdk.AccAddress(priv.PubKey().Address()) msgs := []sdk.Msg{sdk.NewTestMsg(addr)} fee := newStdFee() sigs := []StdSignature{} @@ -26,17 +28,26 @@ func TestStdTx(t *testing.T) { } func TestStdSignBytes(t *testing.T) { - priv := ed25519.GenPrivKey() - addr := sdk.AccAddress(priv.PubKey().Address()) - msgs := []sdk.Msg{sdk.NewTestMsg(addr)} - fee := newStdFee() - signMsg := StdSignMsg{ - "1234", - 3, - 6, - fee, - msgs, - "memo", + type args struct { + chainID string + accnum int64 + sequence int64 + 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"}, + fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"5000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr), + }, + } + 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) } - require.Equal(t, fmt.Sprintf("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"5000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}", addr), string(signMsg.Bytes())) }