-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat: add event-query-tx-for cmd to subscribe and wait for transaction #17274
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e98849e
add event-query-tx-for cmd to subscribe and wait for transaction
mmsqe 6c1c8d4
update doc
mmsqe 0d4bcaa
fix lint
mmsqe 72d8a2b
Apply suggestions from code review
mmsqe ffec98a
Merge remote-tracking branch 'origin/main' into event-query-tx-for
mmsqe 66aee26
Merge branch 'main' into event-query-tx-for
mmsqe 9cf9548
Apply suggestions from code review
mmsqe c5bb8b8
Merge branch 'main' into event-query-tx-for
mmsqe e0e1e43
update nix
mmsqe 9125d04
Apply suggestions from code review
mmsqe 2eadad4
Apply suggestions from code review
mmsqe 041a4c1
Merge remote-tracking branch 'origin/main' into event-query-tx-for
mmsqe 20a7372
Apply suggestions from code review
mmsqe 4ac6bc5
Merge branch 'main' into event-query-tx-for
mmsqe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
rpchttp "github.com/cometbft/cometbft/rpc/client/http" | ||
coretypes "github.com/cometbft/cometbft/rpc/core/types" | ||
tmtypes "github.com/cometbft/cometbft/types" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
func newTxResponseCheckTx(res *coretypes.ResultBroadcastTxCommit) *sdk.TxResponse { | ||
if res == nil { | ||
return nil | ||
} | ||
|
||
var txHash string | ||
if res.Hash != nil { | ||
txHash = res.Hash.String() | ||
} | ||
|
||
parsedLogs, _ := sdk.ParseABCILogs(res.CheckTx.Log) | ||
|
||
return &sdk.TxResponse{ | ||
Height: res.Height, | ||
TxHash: txHash, | ||
Codespace: res.CheckTx.Codespace, | ||
Code: res.CheckTx.Code, | ||
Data: strings.ToUpper(hex.EncodeToString(res.CheckTx.Data)), | ||
RawLog: res.CheckTx.Log, | ||
Logs: parsedLogs, | ||
Info: res.CheckTx.Info, | ||
GasWanted: res.CheckTx.GasWanted, | ||
GasUsed: res.CheckTx.GasUsed, | ||
Events: res.CheckTx.Events, | ||
} | ||
} | ||
|
||
func newTxResponseDeliverTx(res *coretypes.ResultBroadcastTxCommit) *sdk.TxResponse { | ||
if res == nil { | ||
return nil | ||
} | ||
|
||
var txHash string | ||
if res.Hash != nil { | ||
txHash = res.Hash.String() | ||
} | ||
|
||
parsedLogs, _ := sdk.ParseABCILogs(res.TxResult.Log) | ||
|
||
return &sdk.TxResponse{ | ||
Height: res.Height, | ||
TxHash: txHash, | ||
Codespace: res.TxResult.Codespace, | ||
Code: res.TxResult.Code, | ||
Data: strings.ToUpper(hex.EncodeToString(res.TxResult.Data)), | ||
RawLog: res.TxResult.Log, | ||
Logs: parsedLogs, | ||
Info: res.TxResult.Info, | ||
GasWanted: res.TxResult.GasWanted, | ||
GasUsed: res.TxResult.GasUsed, | ||
Events: res.TxResult.Events, | ||
} | ||
} | ||
|
||
func newResponseFormatBroadcastTxCommit(res *coretypes.ResultBroadcastTxCommit) *sdk.TxResponse { | ||
if res == nil { | ||
return nil | ||
} | ||
|
||
if !res.CheckTx.IsOK() { | ||
return newTxResponseCheckTx(res) | ||
} | ||
|
||
return newTxResponseDeliverTx(res) | ||
} | ||
|
||
// 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{ | ||
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), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
c, err := rpchttp.New(clientCtx.NodeURI, "/websocket") | ||
if err != nil { | ||
return err | ||
} | ||
if err := c.Start(); err != nil { | ||
return err | ||
} | ||
defer c.Stop() //nolint:errcheck // ignore stop error | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) | ||
defer cancel() | ||
|
||
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) | ||
if err != nil { | ||
return fmt.Errorf("failed to subscribe to tx: %w", err) | ||
} | ||
defer c.UnsubscribeAll(context.Background(), subscriber) //nolint:errcheck // ignore unsubscribe error | ||
|
||
select { | ||
case evt := <-eventCh: | ||
if txe, ok := evt.Data.(tmtypes.EventDataTx); ok { | ||
res := &coretypes.ResultBroadcastTxCommit{ | ||
TxResult: txe.Result, | ||
Hash: tmtypes.Tx(txe.Tx).Hash(), | ||
Height: txe.Height, | ||
} | ||
return clientCtx.PrintProto(newResponseFormatBroadcastTxCommit(res)) | ||
} | ||
case <-ctx.Done(): | ||
return errors.ErrLogic.Wrapf("timed out waiting for event") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be more clear here, the transaction could have already been included or wasn't yet included.