diff --git a/CHANGELOG.md b/CHANGELOG.md index 522beb7055d24..deb93141a538b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,6 +81,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (all) [#16497](https://github.com/cosmos/cosmos-sdk/pull/16497) Removed all exported vestiges of `sdk.MustSortJSON` and `sdk.SortJSON`. * (types) [#16076](https://github.com/cosmos/cosmos-sdk/pull/16076) Optimize `ChainAnteDecorators`/`ChainPostDecorators` to instantiate the functions once instead of on every invocation of the returned `AnteHandler`/`PostHandler`. * (server) [#16071](https://github.com/cosmos/cosmos-sdk/pull/16071) When `mempool.max-txs` is set to a negative value, use a no-op mempool (effectively disable the app mempool). * (simapp) [#15958](https://github.com/cosmos/cosmos-sdk/pull/15958) Refactor SimApp for removing the global basic manager. diff --git a/types/utils.go b/types/utils.go index 02f022d1d4023..da243d756bfd7 100644 --- a/types/utils.go +++ b/types/utils.go @@ -2,7 +2,6 @@ package types import ( "encoding/binary" - "encoding/json" "fmt" "time" @@ -11,34 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/kv" ) -// 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 -} - // Uint64ToBigEndian - marshals uint64 to a bigendian byte slice so it can be sorted func Uint64ToBigEndian(i uint64) []byte { b := make([]byte, 8) diff --git a/types/utils_test.go b/types/utils_test.go index 8bdba4082b350..22675a2c5990e 100644 --- a/types/utils_test.go +++ b/types/utils_test.go @@ -23,53 +23,6 @@ func (s *utilsTestSuite) SetupSuite() { s.T().Parallel() } -func (s *utilsTestSuite) TestSortJSON() { - 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":"stake","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":"stake"},"candidates":null,"bonds":null}}}`, - want: `{"app_hash":"","app_state":{"accounts":[{"address":"718C9C23F98C9642569742ADDD9F9AB9743FBD5D","coins":[{"amount":1000,"denom":"Token"},{"amount":50,"denom":"stake"}]}],"stake":{"bonds":null,"candidates":null,"params":{"bond_denom":"stake","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 tcIndex, tc := range cases { - tc := tc - got, err := sdk.SortJSON([]byte(tc.unsortedJSON)) - if tc.wantErr { - s.Require().NotNil(err, "tc #%d", tcIndex) - s.Require().Panics(func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) }) - } else { - s.Require().Nil(err, "tc #%d, err=%s", tcIndex, err) - s.Require().NotPanics(func() { sdk.MustSortJSON([]byte(tc.unsortedJSON)) }) - s.Require().Equal(got, sdk.MustSortJSON([]byte(tc.unsortedJSON))) - } - - s.Require().Equal(string(got), tc.want) - } -} - func (s *utilsTestSuite) TestTimeFormatAndParse() { cases := []struct { RFC3339NanoStr string diff --git a/x/auth/migrations/legacytx/stdsign.go b/x/auth/migrations/legacytx/stdsign.go index c4ef01b9d69fb..5d489837bc868 100644 --- a/x/auth/migrations/legacytx/stdsign.go +++ b/x/auth/migrations/legacytx/stdsign.go @@ -46,6 +46,20 @@ type StdSignDoc struct { var RegressionTestingAminoCodec *codec.LegacyAmino +// Deprecated: please delete this code eventually. +func mustSortJSON(bz []byte) []byte { + var c any + err := json.Unmarshal(bz, &c) + if err != nil { + panic(err) + } + js, err := json.Marshal(c) + if err != nil { + panic(err) + } + return js +} + // StdSignBytes returns the bytes to sign for a transaction. // Deprecated: Please use x/tx/signing/aminojson instead. func StdSignBytes(chainID string, accnum, sequence, timeout uint64, fee StdFee, msgs []sdk.Msg, memo string, tip *tx.Tip) []byte { @@ -55,7 +69,7 @@ func StdSignBytes(chainID string, accnum, sequence, timeout uint64, fee StdFee, msgsBytes := make([]json.RawMessage, 0, len(msgs)) for _, msg := range msgs { bz := RegressionTestingAminoCodec.MustMarshalJSON(msg) - msgsBytes = append(msgsBytes, sdk.MustSortJSON(bz)) + msgsBytes = append(msgsBytes, mustSortJSON(bz)) } var stdTip *StdTip @@ -81,7 +95,7 @@ func StdSignBytes(chainID string, accnum, sequence, timeout uint64, fee StdFee, panic(err) } - return sdk.MustSortJSON(bz) + return mustSortJSON(bz) } // Deprecated: StdSignature represents a sig diff --git a/x/genutil/client/cli/init.go b/x/genutil/client/cli/init.go index 37eda3cc719d4..02a2e64c7f486 100644 --- a/x/genutil/client/cli/init.go +++ b/x/genutil/client/cli/init.go @@ -60,7 +60,7 @@ func displayInfo(info printInfo) error { return err } - _, err = fmt.Fprintf(os.Stderr, "%s\n", sdk.MustSortJSON(out)) + _, err = fmt.Fprintf(os.Stderr, "%s\n", out) return err }