From fcf5e341b308d29e48116c333cdc14052f9bb8e7 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 1 Sep 2021 06:50:20 -0700 Subject: [PATCH] fix: remove legacy REST endpoints for broadcast & encode (v0.44.x) (#10041) * fix: remove legacy REST endpoints for broadcast & encode * add changelog * update changelog * fix amino tx marshaling test * try to fix x/auth/client/rest tests * changing tx broadcast request type * remove auth client/rest_test.go Co-authored-by: Robert Zaremba --- CHANGELOG.md | 5 ++- codec/amino_codec_test.go | 24 +++++++++++++ x/auth/client/cli/tx_multisign.go | 59 +++++++++++++++++++++++++++---- x/auth/client/cli/tx_sign.go | 23 ++++++++++-- 4 files changed, 100 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 844481c09bbf..6bfd9be4014f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,7 +57,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes * (client/tx) [\#9421](https://github.com/cosmos/cosmos-sdk/pull/9421/) `BuildUnsignedTx`, `BuildSimTx`, `PrintUnsignedStdTx` functions are moved to the Tx Factory as methods. - + +### Client Breaking Changes +* [\#10041](https://github.com/cosmos/cosmos-sdk/pull/10041) Remove broadcast & encode legacy REST endpoints. Please see the [REST Endpoints Migration guide](https://docs.cosmos.network/master/migrations/rest.html) to migrate to the new REST endpoints. + ## [v0.43.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0) - 2021-08-10 ### Features diff --git a/codec/amino_codec_test.go b/codec/amino_codec_test.go index f15da6a1456b..97673b1d9a44 100644 --- a/codec/amino_codec_test.go +++ b/codec/amino_codec_test.go @@ -10,6 +10,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/testutil/testdata" + "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" ) func createTestCodec() *codec.LegacyAmino { @@ -112,3 +114,25 @@ func TestAminoCodecUnpackAnyFails(t *testing.T) { require.Error(t, err) require.Equal(t, err, errors.New("AminoCodec can't handle unpack protobuf Any's")) } + +func TestAminoCodecFullDecodeAndEncode(t *testing.T) { + // This tx comes from https://github.com/cosmos/cosmos-sdk/issues/8117. + txSigned := `{"type":"cosmos-sdk/StdTx","value":{"msg":[{"type":"cosmos-sdk/MsgCreateValidator","value":{"description":{"moniker":"fulltest","identity":"satoshi","website":"example.com","details":"example inc"},"commission":{"rate":"0.500000000000000000","max_rate":"1.000000000000000000","max_change_rate":"0.200000000000000000"},"min_self_delegation":"1000000","delegator_address":"cosmos14pt0q5cwf38zt08uu0n6yrstf3rndzr5057jys","validator_address":"cosmosvaloper14pt0q5cwf38zt08uu0n6yrstf3rndzr52q28gr","pubkey":{"type":"tendermint/PubKeyEd25519","value":"CYrOiM3HtS7uv1B1OAkknZnFYSRpQYSYII8AtMMtev0="},"value":{"denom":"umuon","amount":"700000000"}}}],"fee":{"amount":[{"denom":"umuon","amount":"6000"}],"gas":"160000"},"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AwAOXeWgNf1FjMaayrSnrOOKz+Fivr6DiI/i0x0sZCHw"},"signature":"RcnfS/u2yl7uIShTrSUlDWvsXo2p2dYu6WJC8VDVHMBLEQZWc8bsINSCjOnlsIVkUNNe1q/WCA9n3Gy1+0zhYA=="}],"memo":"","timeout_height":"0"}}` + var legacyCdc = simapp.MakeTestEncodingConfig().Amino + var tx legacytx.StdTx + err := legacyCdc.UnmarshalJSON([]byte(txSigned), &tx) + require.NoError(t, err) + + // Marshalling/unmarshalling the tx should work. + marshaledTx, err := legacyCdc.MarshalJSON(tx) + require.NoError(t, err) + require.Equal(t, string(marshaledTx), txSigned) + + // Marshalling/unmarshalling the tx wrapped in a struct should work. + txRequest := &cli.BroadcastReq{ + Mode: "block", + Tx: tx, + } + _, err = legacyCdc.MarshalJSON(txRequest) + require.NoError(t, err) +} diff --git a/x/auth/client/cli/tx_multisign.go b/x/auth/client/cli/tx_multisign.go index ec6fcb4db448..07fd7802ce50 100644 --- a/x/auth/client/cli/tx_multisign.go +++ b/x/auth/client/cli/tx_multisign.go @@ -23,10 +23,17 @@ import ( signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/version" authclient "github.com/cosmos/cosmos-sdk/x/auth/client" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" "github.com/cosmos/cosmos-sdk/x/auth/signing" ) -// GetMultiSignCommand returns the multi-sign command +// BroadcastReq defines a tx broadcasting request. +type BroadcastReq struct { + Tx legacytx.StdTx `json:"tx" yaml:"tx"` + Mode string `json:"mode" yaml:"mode"` +} + +// GetSignCommand returns the sign command func GetMultiSignCommand() *cobra.Command { cmd := &cobra.Command{ Use: "multi-sign [...]", @@ -198,9 +205,31 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) (err error) { sigOnly, _ := cmd.Flags().GetBool(flagSigOnly) var json []byte - json, err = marshalSignatureJSON(txCfg, txBuilder.GetTx(), sigOnly) - if err != nil { - return err + + if aminoJSON { + stdTx, err := tx.ConvertTxToStdTx(clientCtx.LegacyAmino, txBuilder.GetTx()) + if err != nil { + return err + } + + req := BroadcastReq{ + Tx: stdTx, + Mode: "block|sync|async", + } + + json, _ = clientCtx.LegacyAmino.MarshalJSON(req) + + } else { + json, err = marshalSignatureJSON(txCfg, txBuilder, sigOnly) + if err != nil { + return err + } + } + + outputDoc, _ := cmd.Flags().GetString(flags.FlagOutputDocument) + if outputDoc == "" { + cmd.Printf("%s\n", json) + return } closeFunc, err := setOutputFile(cmd) @@ -375,9 +404,25 @@ func makeBatchMultisignCmd() func(cmd *cobra.Command, args []string) error { sigOnly, _ := cmd.Flags().GetBool(flagSigOnly) var json []byte - json, err = marshalSignatureJSON(txCfg, txBuilder.GetTx(), sigOnly) - if err != nil { - return err + + if aminoJSON { + stdTx, err := tx.ConvertTxToStdTx(clientCtx.LegacyAmino, txBldr.GetTx()) + if err != nil { + return err + } + + req := BroadcastReq{ + Tx: stdTx, + Mode: "block|sync|async", + } + + json, _ = clientCtx.LegacyAmino.MarshalJSON(req) + + } else { + json, err = marshalSignatureJSON(txCfg, txBldr, sigOnly) + if err != nil { + return err + } } err = clientCtx.PrintString(fmt.Sprintf("%s\n", json)) diff --git a/x/auth/client/cli/tx_sign.go b/x/auth/client/cli/tx_sign.go index 07d8f5330f41..094860a55c37 100644 --- a/x/auth/client/cli/tx_sign.go +++ b/x/auth/client/cli/tx_sign.go @@ -16,7 +16,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" authclient "github.com/cosmos/cosmos-sdk/x/auth/client" - "github.com/cosmos/cosmos-sdk/x/auth/signing" ) const ( @@ -449,8 +448,26 @@ func signTx(cmd *cobra.Command, clientCtx client.Context, txFactory tx.Factory, if err != nil { return err } - if !isSigner { - return errors.New("signing key is not a part of multisig key") + + var json []byte + if aminoJSON { + stdTx, err := tx.ConvertTxToStdTx(clientCtx.LegacyAmino, txBuilder.GetTx()) + if err != nil { + return err + } + req := BroadcastReq{ + Tx: stdTx, + Mode: "block|sync|async", + } + json, err = clientCtx.LegacyAmino.MarshalJSON(req) + if err != nil { + return err + } + } else { + json, err = marshalSignatureJSON(txCfg, txBuilder, printSignatureOnly) + if err != nil { + return err + } } err = authclient.SignTxWithSignerAddress(