Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly parse json in the wait-tx command. #20631

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (server) [#18994](https://github.com/cosmos/cosmos-sdk/pull/18994) Update server context directly rather than a reference to a sub-object
* [#19833](https://github.com/cosmos/cosmos-sdk/pull/19833) Fix some places in which we call Remove inside a Walk.
* [#19851](https://github.com/cosmos/cosmos-sdk/pull/19851) Fix some places in which we call Remove inside a Walk (x/staking and x/gov).
* [#20631](https://github.com/cosmos/cosmos-sdk/pull/20631) Fix json parsing in the wait-tx command.

### API Breaking Changes

Expand Down
20 changes: 14 additions & 6 deletions client/rpc/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

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 {
Expand Down Expand Up @@ -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)
}
SpicyLemon marked this conversation as resolved.
Show resolved Hide resolved
}

// try to parse the hash from the output of a tx command
// Try to find the txhash from yaml output.
SpicyLemon marked this conversation as resolved.
Show resolved Hide resolved
lines := strings.Split(string(in), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "txhash:") {
Expand Down
Loading