From ea16396f71c21ed30edb8ce88872ee3b35672fae Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 13 Jun 2024 21:44:33 +0000 Subject: [PATCH] fix: Properly parse json in the wait-tx command. (backport #20631) (#20660) Co-authored-by: Daniel Wedul Co-authored-by: marbar3778 --- CHANGELOG.md | 1 + client/rpc/tx.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b86f7ba24904..292aa1524e13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Improvements * (x/authz,x/feegrant) [#20590](https://github.com/cosmos/cosmos-sdk/pull/20590) Provide updated keeper in depinject for authz and feegrant modules. +* [#20631](https://github.com/cosmos/cosmos-sdk/pull/20631) Fix json parsing in the wait-tx command. ## [v0.50.7](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.7) - 2024-06-04 diff --git a/client/rpc/tx.go b/client/rpc/tx.go index c22af90a8d8f..429dff59caa2 100644 --- a/client/rpc/tx.go +++ b/client/rpc/tx.go @@ -102,10 +102,10 @@ func WaitTxCmd() *cobra.Command { Short: "Wait for a transaction to be included in a block", Long: `Subscribes to a CometBFT WebSocket connection and waits for a transaction event with the given hash.`, Example: fmt.Sprintf(`By providing the transaction hash: -$ %[1]sd q wait-tx [hash] +$ %[1]s q wait-tx [hash] Or, by piping a "tx" command: -$ %[1]sd tx [flags] | %[1]sd q wait-tx +$ %[1]s tx [flags] | %[1]s q wait-tx `, version.AppName), Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { @@ -200,13 +200,21 @@ $ %[1]sd tx [flags] | %[1]sd q wait-tx } func parseHashFromInput(in []byte) ([]byte, error) { - var resultTx coretypes.ResultTx - if err := json.Unmarshal(in, &resultTx); err == nil { + // The content of in is expected to be the result of a tx command which should be using GenerateOrBroadcastTxCLI. + // That outputs a sdk.TxResponse as either the json or yaml. As json, we can't unmarshal it back into that struct, + // though, because the height field ends up quoted which confuses json.Unmarshal (because it's for an int64 field). + + // Try to find the txhash from json ouptut. + resultTx := make(map[string]json.RawMessage) + if err := json.Unmarshal(in, &resultTx); err == nil && len(resultTx["txhash"]) > 0 { // input was JSON, return the hash - return resultTx.Hash, nil + hash := strings.Trim(strings.TrimSpace(string(resultTx["txhash"])), `"`) + if len(hash) > 0 { + return hex.DecodeString(hash) + } } - // try to parse the hash from the output of a tx command + // Try to find the txhash from yaml output. lines := strings.Split(string(in), "\n") for _, line := range lines { if strings.HasPrefix(line, "txhash:") {