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. (backport #20631) #20661

Closed
wants to merge 2 commits into from
Closed
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 @@ -42,6 +42,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.

### Bug Fixes

Expand Down
43 changes: 43 additions & 0 deletions client/rpc/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,31 @@
// QueryEventForTxCmd returns a CLI command that subscribes to a WebSocket connection and waits for a transaction event with the given hash.
func QueryEventForTxCmd() *cobra.Command {
cmd := &cobra.Command{
<<<<<<< HEAD

Check failure on line 90 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected <<, expecting expression

Check failure on line 90 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected operand, found '<<'
Use: "event-query-tx-for [hash]",
Short: "Query for a transaction by hash",
Long: `Subscribes to a CometBFT WebSocket connection and waits for a transaction event with the given hash.`,
Args: cobra.ExactArgs(1),

Check failure on line 94 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected ) in composite literal; possibly missing comma or }
=======
Use: "wait-tx [hash]",
Aliases: []string{"event-query-tx-for"},

Check failure on line 97 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected comma after top level declaration
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]s q wait-tx [hash]

Or, by piping a "tx" command:
$ %[1]s tx [flags] | %[1]s q wait-tx
`, version.AppName),
Args: cobra.MaximumNArgs(1),
>>>>>>> 55b00938b (fix: Properly parse json in the wait-tx command. (#20631))

Check failure on line 107 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

invalid character U+0023 '#'

Check failure on line 107 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'
RunE: func(cmd *cobra.Command, args []string) error {

Check failure on line 108 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

method has multiple receivers

Check failure on line 108 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected {, expecting (
clientCtx, err := client.GetClientTxContext(cmd)

Check failure on line 109 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected := in parameter list; possibly missing comma or )
if err != nil {

Check failure on line 110 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: non-declaration statement outside function body

Check failure on line 110 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing ',' in composite literal
return err

Check failure on line 111 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected operand, found 'return'

Check failure on line 111 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing ',' in composite literal

Check failure on line 111 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing ',' before newline in composite literal
}

Check failure on line 112 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing ',' before newline in composite literal
c, err := rpchttp.New(clientCtx.NodeURI, "/websocket")

Check failure on line 113 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing ',' in composite literal

Check failure on line 113 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing ',' before newline in composite literal
if err != nil {

Check failure on line 114 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected operand, found 'if'
return err
}
if err := c.Start(); err != nil {
Expand All @@ -111,7 +125,7 @@
hash := args[0]
query := fmt.Sprintf("%s='%s' AND %s='%s'", tmtypes.EventTypeKey, tmtypes.EventTx, tmtypes.TxHashKey, hash)
const subscriber = "subscriber"
eventCh, err := c.Subscribe(ctx, subscriber, query)

Check failure on line 128 in client/rpc/tx.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: non-declaration statement outside function body
if err != nil {
return fmt.Errorf("failed to subscribe to tx: %w", err)
}
Expand All @@ -138,3 +152,32 @@

return cmd
}
<<<<<<< HEAD
=======

func parseHashFromInput(in []byte) ([]byte, error) {
// 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
hash := strings.Trim(strings.TrimSpace(string(resultTx["txhash"])), `"`)
if len(hash) > 0 {
return hex.DecodeString(hash)
}
}

// Try to find the txhash from yaml output.
lines := strings.Split(string(in), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "txhash:") {
hash := strings.TrimSpace(line[len("txhash:"):])
return hex.DecodeString(hash)
}
}
return nil, fmt.Errorf("txhash not found")
}
>>>>>>> 55b00938b (fix: Properly parse json in the wait-tx command. (#20631))
Loading