From 0f1ceb073e4cd2a1a39b377ca01def9f8a43b0e5 Mon Sep 17 00:00:00 2001 From: Liamsi Date: Fri, 6 Jul 2018 00:23:12 +0100 Subject: [PATCH 1/3] sort all relevant GetSignBytes: - call SortJSON before return JSON bytes to guarantee alphabetic ordering --- x/auth/stdtx.go | 5 +++++ x/bank/msgs.go | 17 +++++++++++++++++ x/bank/msgs_test.go | 4 ++-- x/gov/msgs.go | 13 +++++++++++++ x/stake/types/msg.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go index fd55eebb9..566940db7 100644 --- a/x/auth/stdtx.go +++ b/x/auth/stdtx.go @@ -3,6 +3,7 @@ package auth import ( "encoding/json" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/tendermint/crypto" ) @@ -141,6 +142,10 @@ func StdSignBytes(chainID string, accnum int64, sequence int64, fee StdFee, msgs if err != nil { panic(err) } + bz, err = server.SortJSON(bz) + if err != nil { + panic(err) + } return bz } diff --git a/x/bank/msgs.go b/x/bank/msgs.go index 1a871979e..069ace9ce 100644 --- a/x/bank/msgs.go +++ b/x/bank/msgs.go @@ -3,6 +3,7 @@ package bank import ( "encoding/json" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -72,6 +73,10 @@ func (msg MsgSend) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -133,6 +138,10 @@ func (msg MsgIssue) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -162,6 +171,10 @@ func (in Input) GetSignBytes() []byte { if err != nil { panic(err) } + bin, err = server.SortJSON(bin) + if err != nil { + panic(err) + } return bin } @@ -209,6 +222,10 @@ func (out Output) GetSignBytes() []byte { if err != nil { panic(err) } + bin, err = server.SortJSON(bin) + if err != nil { + panic(err) + } return bin } diff --git a/x/bank/msgs_test.go b/x/bank/msgs_test.go index 3754858ec..f4e7363d3 100644 --- a/x/bank/msgs_test.go +++ b/x/bank/msgs_test.go @@ -187,7 +187,7 @@ func TestMsgSendGetSignBytes(t *testing.T) { } res := msg.GetSignBytes() - expected := `{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"denom":"atom","amount":"10"}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"denom":"atom","amount":"10"}]}]}` + expected := `{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"amount":"10","denom":"atom"}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"amount":"10","denom":"atom"}]}]}` require.Equal(t, expected, string(res)) } @@ -257,7 +257,7 @@ func TestMsgIssueGetSignBytes(t *testing.T) { } res := msg.GetSignBytes() - expected := `{"banker":"cosmosaccaddr1d9h8qat5e4ehc5","outputs":[{"address":"cosmosaccaddr1d3hkzm3dveex7mfdvfsku6cwsauqd","coins":[{"denom":"atom","amount":"10"}]}]}` + expected := `{"banker":"cosmosaccaddr1d9h8qat5e4ehc5","outputs":[{"address":"cosmosaccaddr1d3hkzm3dveex7mfdvfsku6cwsauqd","coins":[{"amount":"10","denom":"atom"}]}]}` require.Equal(t, expected, string(res)) } diff --git a/x/gov/msgs.go b/x/gov/msgs.go index f436d024b..afe7084c4 100644 --- a/x/gov/msgs.go +++ b/x/gov/msgs.go @@ -3,6 +3,7 @@ package gov import ( "fmt" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -82,6 +83,10 @@ func (msg MsgSubmitProposal) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -149,6 +154,10 @@ func (msg MsgDeposit) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -213,6 +222,10 @@ func (msg MsgVote) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } diff --git a/x/stake/types/msg.go b/x/stake/types/msg.go index 878c1ba17..609935fb0 100644 --- a/x/stake/types/msg.go +++ b/x/stake/types/msg.go @@ -3,6 +3,7 @@ package types import ( "math" + "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/tendermint/crypto" ) @@ -63,6 +64,10 @@ func (msg MsgCreateValidator) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -114,6 +119,10 @@ func (msg MsgEditValidator) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -166,6 +175,10 @@ func (msg MsgDelegate) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -226,6 +239,10 @@ func (msg MsgBeginRedelegate) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -286,6 +303,10 @@ func (msg MsgCompleteRedelegate) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -338,6 +359,10 @@ func (msg MsgBeginUnbonding) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } @@ -387,6 +412,10 @@ func (msg MsgCompleteUnbonding) GetSignBytes() []byte { if err != nil { panic(err) } + b, err = server.SortJSON(b) + if err != nil { + panic(err) + } return b } From b7f902fba5fcb367d82a2dd9e59600e91d2dfc72 Mon Sep 17 00:00:00 2001 From: Liamsi Date: Fri, 6 Jul 2018 01:09:23 +0100 Subject: [PATCH 2/3] sort *all* GetSignBytes: - call MustSortJSON before return JSON bytes to guarantee alphabetic ordering - moved SortJSON and MustSortJSON to types package to avoid cyclic package dep --- baseapp/baseapp_test.go | 4 +-- docs/core/examples/app1.go | 2 +- docs/core/examples/app2.go | 2 +- examples/democoin/x/cool/types.go | 4 +-- examples/democoin/x/oracle/types.go | 2 +- examples/democoin/x/pow/types.go | 2 +- examples/democoin/x/pow/types_test.go | 2 +- examples/democoin/x/simplestake/msgs.go | 2 +- examples/kvstore/tx.go | 2 +- server/util.go | 18 ----------- server/util_test.go | 31 ------------------ types/tx_msg.go | 2 +- types/utils.go | 31 ++++++++++++++++++ types/utils_test.go | 39 ++++++++++++++++++++++ x/auth/stdtx.go | 7 +--- x/bank/msgs.go | 25 +++----------- x/gov/msgs.go | 19 ++--------- x/ibc/types.go | 4 +-- x/slashing/msg.go | 2 +- x/stake/types/msg.go | 43 ++++--------------------- 20 files changed, 100 insertions(+), 143 deletions(-) create mode 100644 types/utils.go create mode 100644 types/utils_test.go diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index e919375d9..b8c42c4d2 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -662,7 +662,7 @@ const msgType3 = "burn" func (msg testBurnMsg) Type() string { return msgType3 } func (msg testBurnMsg) GetSignBytes() []byte { bz, _ := json.Marshal(msg) - return bz + return sdk.MustSortJSON(bz) } func (msg testBurnMsg) ValidateBasic() sdk.Error { if msg.Addr == nil { @@ -685,7 +685,7 @@ const msgType4 = "send" func (msg testSendMsg) Type() string { return msgType4 } func (msg testSendMsg) GetSignBytes() []byte { bz, _ := json.Marshal(msg) - return bz + return sdk.MustSortJSON(bz) } func (msg testSendMsg) ValidateBasic() sdk.Error { if msg.Sender == nil || msg.Receiver == nil { diff --git a/docs/core/examples/app1.go b/docs/core/examples/app1.go index f9eceecdb..a9580fcc2 100644 --- a/docs/core/examples/app1.go +++ b/docs/core/examples/app1.go @@ -85,7 +85,7 @@ func (msg MsgSend) GetSignBytes() []byte { if err != nil { panic(err) } - return bz + return sdk.MustSortJSON(bz) } // Implements Msg. Return the signer. diff --git a/docs/core/examples/app2.go b/docs/core/examples/app2.go index b4d44dca7..c96a1022f 100644 --- a/docs/core/examples/app2.go +++ b/docs/core/examples/app2.go @@ -100,7 +100,7 @@ func (msg MsgIssue) GetSignBytes() []byte { if err != nil { panic(err) } - return bz + return sdk.MustSortJSON(bz) } // Implements Msg. Return the signer. diff --git a/examples/democoin/x/cool/types.go b/examples/democoin/x/cool/types.go index 77f49a0ba..82c9286c6 100644 --- a/examples/democoin/x/cool/types.go +++ b/examples/democoin/x/cool/types.go @@ -58,7 +58,7 @@ func (msg MsgSetTrend) GetSignBytes() []byte { if err != nil { panic(err) } - return b + return sdk.MustSortJSON(b) } //_______________________________________________________________________ @@ -102,5 +102,5 @@ func (msg MsgQuiz) GetSignBytes() []byte { if err != nil { panic(err) } - return b + return sdk.MustSortJSON(b) } diff --git a/examples/democoin/x/oracle/types.go b/examples/democoin/x/oracle/types.go index 041b9ab34..d06f47a4e 100644 --- a/examples/democoin/x/oracle/types.go +++ b/examples/democoin/x/oracle/types.go @@ -18,7 +18,7 @@ func (msg Msg) GetSignBytes() []byte { if err != nil { panic(err) } - return bz + return sdk.MustSortJSON(bz) } // GetSigners implements sdk.Msg diff --git a/examples/democoin/x/pow/types.go b/examples/democoin/x/pow/types.go index cd77a68af..3529917dc 100644 --- a/examples/democoin/x/pow/types.go +++ b/examples/democoin/x/pow/types.go @@ -76,5 +76,5 @@ func (msg MsgMine) GetSignBytes() []byte { if err != nil { panic(err) } - return b + return sdk.MustSortJSON(b) } diff --git a/examples/democoin/x/pow/types_test.go b/examples/democoin/x/pow/types_test.go index 9ed5dc102..1291cb96a 100644 --- a/examples/democoin/x/pow/types_test.go +++ b/examples/democoin/x/pow/types_test.go @@ -62,7 +62,7 @@ func TestMsgMineGetSignBytes(t *testing.T) { addr := sdk.Address([]byte("sender")) msg := MsgMine{addr, 1, 1, 1, []byte("abc")} res := msg.GetSignBytes() - require.Equal(t, string(res), `{"sender":"73656E646572","difficulty":1,"count":1,"nonce":1,"proof":"YWJj"}`) + require.Equal(t, string(res), `{"count":1,"difficulty":1,"nonce":1,"proof":"YWJj","sender":"73656E646572"}`) } func TestMsgMineGetSigners(t *testing.T) { diff --git a/examples/democoin/x/simplestake/msgs.go b/examples/democoin/x/simplestake/msgs.go index 564e0d979..91729e3ef 100644 --- a/examples/democoin/x/simplestake/msgs.go +++ b/examples/democoin/x/simplestake/msgs.go @@ -48,7 +48,7 @@ func (msg MsgBond) GetSignBytes() []byte { if err != nil { panic(err) } - return bz + return sdk.MustSortJSON(bz) } //_______________________________________________________________ diff --git a/examples/kvstore/tx.go b/examples/kvstore/tx.go index 0e94ace28..e1c5cab3a 100644 --- a/examples/kvstore/tx.go +++ b/examples/kvstore/tx.go @@ -27,7 +27,7 @@ func (tx kvstoreTx) GetMemo() string { } func (tx kvstoreTx) GetSignBytes() []byte { - return tx.bytes + return sdk.MustSortJSON(tx.bytes) } // Should the app be calling this? Or only handlers? diff --git a/server/util.go b/server/util.go index c77222bfe..d1ad001c5 100644 --- a/server/util.go +++ b/server/util.go @@ -135,24 +135,6 @@ func AppendJSON(cdc *wire.Codec, baseJSON []byte, key string, value json.RawMess return json.RawMessage(bz), err } -// SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces -// are removed. -// This method can be used to canonicalize JSON to be returned by GetSignBytes, -// e.g. for the ledger integration. -// If the passed JSON isn't valid it will return an error. -func SortJSON(toSortJSON []byte) ([]byte, error) { - var c interface{} - err := json.Unmarshal(toSortJSON, &c) - if err != nil { - return nil, err - } - js, err := json.Marshal(c) - if err != nil { - return nil, err - } - return js, nil -} - // https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go // TODO there must be a better way to get external IP func externalIP() (string, error) { diff --git a/server/util_test.go b/server/util_test.go index dc4ba75f2..8f1ab21db 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -38,34 +38,3 @@ func TestAppendJSON(t *testing.T) { require.Equal(t, bar, resBar, "appended: %v", appended) } - -func TestSortJSON(t *testing.T) { - cases := []struct { - unsortedJSON string - want string - wantErr bool - }{ - // simple case - {unsortedJSON: `{"cosmos":"foo", "atom":"bar", "tendermint":"foobar"}`, - want: `{"atom":"bar","cosmos":"foo","tendermint":"foobar"}`, wantErr: false}, - // failing case (invalid JSON): - {unsortedJSON: `"cosmos":"foo",,,, "atom":"bar", "tendermint":"foobar"}`, - want: "", wantErr: true}, - // genesis.json - {unsortedJSON: `{"consensus_params":{"block_size_params":{"max_bytes":22020096,"max_txs":100000,"max_gas":-1},"tx_size_params":{"max_bytes":10240,"max_gas":-1},"block_gossip_params":{"block_part_size_bytes":65536},"evidence_params":{"max_age":100000}},"validators":[{"pub_key":{"type":"AC26791624DE60","value":"c7UMMAbjFuc5GhGPy0E5q5tefy12p9Tq0imXqdrKXwo="},"power":100,"name":""}],"app_hash":"","genesis_time":"2018-05-11T15:52:25.424795506Z","chain_id":"test-chain-Q6VeoW","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"denom":"Token","amount":1000},{"denom":"steak","amount":50}]}],"stake":{"pool":{"total_supply":50,"bonded_shares":"0","unbonded_shares":"0","bonded_pool":0,"unbonded_pool":0,"inflation_last_time":0,"inflation":"7/100"},"params":{"inflation_rate_change":"13/100","inflation_max":"1/5","inflation_min":"7/100","goal_bonded":"67/100","max_validators":100,"bond_denom":"steak"},"candidates":null,"bonds":null}}}`, - want: `{"app_hash":"","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"amount":1000,"denom":"Token"},{"amount":50,"denom":"steak"}]}],"stake":{"bonds":null,"candidates":null,"params":{"bond_denom":"steak","goal_bonded":"67/100","inflation_max":"1/5","inflation_min":"7/100","inflation_rate_change":"13/100","max_validators":100},"pool":{"bonded_pool":0,"bonded_shares":"0","inflation":"7/100","inflation_last_time":0,"total_supply":50,"unbonded_pool":0,"unbonded_shares":"0"}}},"chain_id":"test-chain-Q6VeoW","consensus_params":{"block_gossip_params":{"block_part_size_bytes":65536},"block_size_params":{"max_bytes":22020096,"max_gas":-1,"max_txs":100000},"evidence_params":{"max_age":100000},"tx_size_params":{"max_bytes":10240,"max_gas":-1}},"genesis_time":"2018-05-11T15:52:25.424795506Z","validators":[{"name":"","power":100,"pub_key":{"type":"AC26791624DE60","value":"c7UMMAbjFuc5GhGPy0E5q5tefy12p9Tq0imXqdrKXwo="}}]}`, - wantErr: false}, - // from the TXSpec: - {unsortedJSON: `{"chain_id":"test-chain-1","sequence":1,"fee_bytes":{"amount":[{"amount":5,"denom":"photon"}],"gas":10000},"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":10,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":10,"denom":"atom"}]}]},"alt_bytes":null}`, - want: `{"alt_bytes":null,"chain_id":"test-chain-1","fee_bytes":{"amount":[{"amount":5,"denom":"photon"}],"gas":10000},"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":10,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":10,"denom":"atom"}]}]},"sequence":1}`, - wantErr: false}, - } - - for _, tc := range cases { - got, err := SortJSON([]byte(tc.unsortedJSON)) - if tc.wantErr != (err != nil) { - t.Fatalf("got %t, want: %t, err=%s", err != nil, tc.wantErr, err) - } - require.Equal(t, string(got), tc.want) - } -} diff --git a/types/tx_msg.go b/types/tx_msg.go index c1af91df8..fbadfb49d 100644 --- a/types/tx_msg.go +++ b/types/tx_msg.go @@ -60,7 +60,7 @@ func (msg *TestMsg) GetSignBytes() []byte { if err != nil { panic(err) } - return bz + return MustSortJSON(bz) } func (msg *TestMsg) ValidateBasic() Error { return nil } func (msg *TestMsg) GetSigners() []Address { diff --git a/types/utils.go b/types/utils.go new file mode 100644 index 000000000..2e027676a --- /dev/null +++ b/types/utils.go @@ -0,0 +1,31 @@ +package types + +import "encoding/json" + +// SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces +// are removed. +// This method can be used to canonicalize JSON to be returned by GetSignBytes, +// e.g. for the ledger integration. +// If the passed JSON isn't valid it will return an error. +func SortJSON(toSortJSON []byte) ([]byte, error) { + var c interface{} + err := json.Unmarshal(toSortJSON, &c) + if err != nil { + return nil, err + } + js, err := json.Marshal(c) + if err != nil { + return nil, err + } + return js, nil +} + +// MustSortJSON is like SortJSON but panic if an error occurs, e.g., if +// the passed JSON isn't valid. +func MustSortJSON(toSortJSON []byte) []byte { + js, err := SortJSON(toSortJSON) + if err != nil { + panic(err) + } + return js +} diff --git a/types/utils_test.go b/types/utils_test.go new file mode 100644 index 000000000..8c84e2ace --- /dev/null +++ b/types/utils_test.go @@ -0,0 +1,39 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSortJSON(t *testing.T) { + cases := []struct { + unsortedJSON string + want string + wantErr bool + }{ + // simple case + {unsortedJSON: `{"cosmos":"foo", "atom":"bar", "tendermint":"foobar"}`, + want: `{"atom":"bar","cosmos":"foo","tendermint":"foobar"}`, wantErr: false}, + // failing case (invalid JSON): + {unsortedJSON: `"cosmos":"foo",,,, "atom":"bar", "tendermint":"foobar"}`, + want: "", + wantErr: true}, + // genesis.json + {unsortedJSON: `{"consensus_params":{"block_size_params":{"max_bytes":22020096,"max_txs":100000,"max_gas":-1},"tx_size_params":{"max_bytes":10240,"max_gas":-1},"block_gossip_params":{"block_part_size_bytes":65536},"evidence_params":{"max_age":100000}},"validators":[{"pub_key":{"type":"AC26791624DE60","value":"c7UMMAbjFuc5GhGPy0E5q5tefy12p9Tq0imXqdrKXwo="},"power":100,"name":""}],"app_hash":"","genesis_time":"2018-05-11T15:52:25.424795506Z","chain_id":"test-chain-Q6VeoW","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"denom":"Token","amount":1000},{"denom":"steak","amount":50}]}],"stake":{"pool":{"total_supply":50,"bonded_shares":"0","unbonded_shares":"0","bonded_pool":0,"unbonded_pool":0,"inflation_last_time":0,"inflation":"7/100"},"params":{"inflation_rate_change":"13/100","inflation_max":"1/5","inflation_min":"7/100","goal_bonded":"67/100","max_validators":100,"bond_denom":"steak"},"candidates":null,"bonds":null}}}`, + want: `{"app_hash":"","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"amount":1000,"denom":"Token"},{"amount":50,"denom":"steak"}]}],"stake":{"bonds":null,"candidates":null,"params":{"bond_denom":"steak","goal_bonded":"67/100","inflation_max":"1/5","inflation_min":"7/100","inflation_rate_change":"13/100","max_validators":100},"pool":{"bonded_pool":0,"bonded_shares":"0","inflation":"7/100","inflation_last_time":0,"total_supply":50,"unbonded_pool":0,"unbonded_shares":"0"}}},"chain_id":"test-chain-Q6VeoW","consensus_params":{"block_gossip_params":{"block_part_size_bytes":65536},"block_size_params":{"max_bytes":22020096,"max_gas":-1,"max_txs":100000},"evidence_params":{"max_age":100000},"tx_size_params":{"max_bytes":10240,"max_gas":-1}},"genesis_time":"2018-05-11T15:52:25.424795506Z","validators":[{"name":"","power":100,"pub_key":{"type":"AC26791624DE60","value":"c7UMMAbjFuc5GhGPy0E5q5tefy12p9Tq0imXqdrKXwo="}}]}`, + wantErr: false}, + // from the TXSpec: + {unsortedJSON: `{"chain_id":"test-chain-1","sequence":1,"fee_bytes":{"amount":[{"amount":5,"denom":"photon"}],"gas":10000},"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":10,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":10,"denom":"atom"}]}]},"alt_bytes":null}`, + want: `{"alt_bytes":null,"chain_id":"test-chain-1","fee_bytes":{"amount":[{"amount":5,"denom":"photon"}],"gas":10000},"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":10,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":10,"denom":"atom"}]}]},"sequence":1}`, + wantErr: false}, + } + + for _, tc := range cases { + got, err := SortJSON([]byte(tc.unsortedJSON)) + if tc.wantErr != (err != nil) { + t.Fatalf("got %t, want: %t, err=%s", err != nil, tc.wantErr, err) + } + require.Equal(t, string(got), tc.want) + } +} diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go index 566940db7..960d9855e 100644 --- a/x/auth/stdtx.go +++ b/x/auth/stdtx.go @@ -3,7 +3,6 @@ package auth import ( "encoding/json" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/tendermint/crypto" ) @@ -142,11 +141,7 @@ func StdSignBytes(chainID string, accnum int64, sequence int64, fee StdFee, msgs if err != nil { panic(err) } - bz, err = server.SortJSON(bz) - if err != nil { - panic(err) - } - return bz + return sdk.MustSortJSON(bz) } // StdSignMsg is a convenience structure for passing along diff --git a/x/bank/msgs.go b/x/bank/msgs.go index 069ace9ce..46b199563 100644 --- a/x/bank/msgs.go +++ b/x/bank/msgs.go @@ -3,7 +3,6 @@ package bank import ( "encoding/json" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -73,11 +72,7 @@ func (msg MsgSend) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // Implements Msg. @@ -138,11 +133,7 @@ func (msg MsgIssue) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // Implements Msg. @@ -171,11 +162,7 @@ func (in Input) GetSignBytes() []byte { if err != nil { panic(err) } - bin, err = server.SortJSON(bin) - if err != nil { - panic(err) - } - return bin + return sdk.MustSortJSON(bin) } // ValidateBasic - validate transaction input @@ -222,11 +209,7 @@ func (out Output) GetSignBytes() []byte { if err != nil { panic(err) } - bin, err = server.SortJSON(bin) - if err != nil { - panic(err) - } - return bin + return sdk.MustSortJSON(bin) } // ValidateBasic - validate transaction output diff --git a/x/gov/msgs.go b/x/gov/msgs.go index afe7084c4..75a8d51e3 100644 --- a/x/gov/msgs.go +++ b/x/gov/msgs.go @@ -3,7 +3,6 @@ package gov import ( "fmt" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -83,11 +82,7 @@ func (msg MsgSubmitProposal) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // Implements Msg. @@ -154,11 +149,7 @@ func (msg MsgDeposit) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // Implements Msg. @@ -222,11 +213,7 @@ func (msg MsgVote) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // Implements Msg. diff --git a/x/ibc/types.go b/x/ibc/types.go index a311b9869..33f2a9a8d 100644 --- a/x/ibc/types.go +++ b/x/ibc/types.go @@ -59,7 +59,7 @@ func (p IBCPacket) GetSignBytes() []byte { if err != nil { panic(err) } - return b + return sdk.MustSortJSON(b) } // validator the ibc packey @@ -131,5 +131,5 @@ func (msg IBCReceiveMsg) GetSignBytes() []byte { if err != nil { panic(err) } - return b + return sdk.MustSortJSON(b) } diff --git a/x/slashing/msg.go b/x/slashing/msg.go index 561c92266..6060079a4 100644 --- a/x/slashing/msg.go +++ b/x/slashing/msg.go @@ -38,7 +38,7 @@ func (msg MsgUnrevoke) GetSignBytes() []byte { if err != nil { panic(err) } - return b + return sdk.MustSortJSON(b) } // quick validity check diff --git a/x/stake/types/msg.go b/x/stake/types/msg.go index 609935fb0..aa9eea8cb 100644 --- a/x/stake/types/msg.go +++ b/x/stake/types/msg.go @@ -3,7 +3,6 @@ package types import ( "math" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tendermint/tendermint/crypto" ) @@ -64,11 +63,7 @@ func (msg MsgCreateValidator) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check @@ -119,11 +114,7 @@ func (msg MsgEditValidator) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check @@ -175,11 +166,7 @@ func (msg MsgDelegate) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check @@ -239,11 +226,7 @@ func (msg MsgBeginRedelegate) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check @@ -303,11 +286,7 @@ func (msg MsgCompleteRedelegate) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check @@ -359,11 +338,7 @@ func (msg MsgBeginUnbonding) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check @@ -412,11 +387,7 @@ func (msg MsgCompleteUnbonding) GetSignBytes() []byte { if err != nil { panic(err) } - b, err = server.SortJSON(b) - if err != nil { - panic(err) - } - return b + return sdk.MustSortJSON(b) } // quick validity check From 1fabaee2dd0fe53c233c8929ffe94cc11a51726b Mon Sep 17 00:00:00 2001 From: Liamsi Date: Fri, 6 Jul 2018 02:37:55 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b35664140..7b5834035 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ *TBD* BREAKING CHANGES +* msg.GetSignBytes() returns sorted JSON (by key) * Update Tendermint to v0.22.0 * Default ports changed from 466xx to 266xx * Amino JSON uses type names instead of prefix bytes