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

refactor(node discovery): implement options for tx Broadcast #108

Merged
merged 1 commit into from
Jan 9, 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
2 changes: 1 addition & 1 deletion go/node/client/v1beta2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type QueryClient interface {

//go:generate mockery --name TxClient --output ./mocks
type TxClient interface {
Broadcast(context.Context, ...sdk.Msg) (proto.Message, error)
Broadcast(context.Context, []sdk.Msg, ...BroadcastOption) (proto.Message, error)
}

//go:generate mockery --name NodeClient --output ./mocks
Expand Down
63 changes: 40 additions & 23 deletions go/node/client/v1beta2/mocks/tx_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions go/node/client/v1beta2/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ const (
notFoundErrorMessageSuffix = ") not found"
)

type BroadcastOptions struct {
resultAsError bool
}

type BroadcastOption func(*BroadcastOptions) *BroadcastOptions

func WithResultCodeAsError() BroadcastOption {
return func(opts *BroadcastOptions) *BroadcastOptions {
opts.resultAsError = true
return opts
}
}

type broadcastResp struct {
resp proto.Message
err error
Expand Down Expand Up @@ -127,7 +140,7 @@ func newSerialTx(ctx context.Context, cctx sdkclient.Context, flags *pflag.FlagS
return client, nil
}

func (c *serialBroadcaster) Broadcast(ctx context.Context, msgs ...sdk.Msg) (proto.Message, error) {
func (c *serialBroadcaster) Broadcast(ctx context.Context, msgs []sdk.Msg, opts ...BroadcastOption) (proto.Message, error) {
responsech := make(chan broadcastResp, 1)
request := broadcastReq{
responsech: responsech,
Expand All @@ -136,6 +149,12 @@ func (c *serialBroadcaster) Broadcast(ctx context.Context, msgs ...sdk.Msg) (pro

request.id = uintptr(unsafe.Pointer(&request))

ropts := &BroadcastOptions{}

for _, opt := range opts {
_ = opt(ropts)
}

select {
case c.reqch <- request:
case <-ctx.Done():
Expand All @@ -148,7 +167,7 @@ func (c *serialBroadcaster) Broadcast(ctx context.Context, msgs ...sdk.Msg) (pro
case resp := <-responsech:
// if returned error is sdk error, it is likely to be wrapped response so discard it
// as clients supposed to check Tx code, unless resp is nil, which is error during Tx preparation
if !errors.As(resp.err, &sdkerrors.Error{}) || resp.resp == nil {
if !errors.As(resp.err, &sdkerrors.Error{}) || resp.resp == nil || ropts.resultAsError {
return resp.resp, resp.err
}
return resp.resp, nil
Expand Down