Skip to content

Commit

Permalink
refactor(node discovery): implement options for tx Broadcast (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Jan 9, 2024
1 parent e08955a commit d60ce66
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
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

0 comments on commit d60ce66

Please sign in to comment.