From d60ce66786ce213bebd7d62aa4698f25de3bae12 Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Tue, 9 Jan 2024 06:58:55 -0500 Subject: [PATCH] refactor(node discovery): implement options for tx Broadcast (#108) Signed-off-by: Artur Troian --- go/node/client/v1beta2/client.go | 2 +- go/node/client/v1beta2/mocks/tx_client.go | 63 ++++++++++++++--------- go/node/client/v1beta2/tx.go | 23 ++++++++- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/go/node/client/v1beta2/client.go b/go/node/client/v1beta2/client.go index 7b412496..aef6364d 100644 --- a/go/node/client/v1beta2/client.go +++ b/go/node/client/v1beta2/client.go @@ -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 diff --git a/go/node/client/v1beta2/mocks/tx_client.go b/go/node/client/v1beta2/mocks/tx_client.go index d53b98b2..e0d2343c 100644 --- a/go/node/client/v1beta2/mocks/tx_client.go +++ b/go/node/client/v1beta2/mocks/tx_client.go @@ -5,8 +5,12 @@ package mocks import ( context "context" - types "github.com/cosmos/cosmos-sdk/types" + proto "github.com/gogo/protobuf/proto" mock "github.com/stretchr/testify/mock" + + types "github.com/cosmos/cosmos-sdk/types" + + v1beta2 "github.com/akash-network/akash-api/go/node/client/v1beta2" ) // TxClient is an autogenerated mock type for the TxClient type @@ -22,25 +26,37 @@ func (_m *TxClient) EXPECT() *TxClient_Expecter { return &TxClient_Expecter{mock: &_m.Mock} } -// Broadcast provides a mock function with given fields: _a0, _a1 -func (_m *TxClient) Broadcast(_a0 context.Context, _a1 ...types.Msg) error { - _va := make([]interface{}, len(_a1)) - for _i := range _a1 { - _va[_i] = _a1[_i] +// Broadcast provides a mock function with given fields: _a0, _a1, _a2 +func (_m *TxClient) Broadcast(_a0 context.Context, _a1 []types.Msg, _a2 ...v1beta2.BroadcastOption) (proto.Message, error) { + _va := make([]interface{}, len(_a2)) + for _i := range _a2 { + _va[_i] = _a2[_i] } var _ca []interface{} - _ca = append(_ca, _a0) + _ca = append(_ca, _a0, _a1) _ca = append(_ca, _va...) ret := _m.Called(_ca...) - var r0 error - if rf, ok := ret.Get(0).(func(context.Context, ...types.Msg) error); ok { - r0 = rf(_a0, _a1...) + var r0 proto.Message + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, []types.Msg, ...v1beta2.BroadcastOption) (proto.Message, error)); ok { + return rf(_a0, _a1, _a2...) + } + if rf, ok := ret.Get(0).(func(context.Context, []types.Msg, ...v1beta2.BroadcastOption) proto.Message); ok { + r0 = rf(_a0, _a1, _a2...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(proto.Message) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, []types.Msg, ...v1beta2.BroadcastOption) error); ok { + r1 = rf(_a0, _a1, _a2...) } else { - r0 = ret.Error(0) + r1 = ret.Error(1) } - return r0 + return r0, r1 } // TxClient_Broadcast_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Broadcast' @@ -50,31 +66,32 @@ type TxClient_Broadcast_Call struct { // Broadcast is a helper method to define mock.On call // - _a0 context.Context -// - _a1 ...types.Msg -func (_e *TxClient_Expecter) Broadcast(_a0 interface{}, _a1 ...interface{}) *TxClient_Broadcast_Call { +// - _a1 []types.Msg +// - _a2 ...v1beta2.BroadcastOption +func (_e *TxClient_Expecter) Broadcast(_a0 interface{}, _a1 interface{}, _a2 ...interface{}) *TxClient_Broadcast_Call { return &TxClient_Broadcast_Call{Call: _e.mock.On("Broadcast", - append([]interface{}{_a0}, _a1...)...)} + append([]interface{}{_a0, _a1}, _a2...)...)} } -func (_c *TxClient_Broadcast_Call) Run(run func(_a0 context.Context, _a1 ...types.Msg)) *TxClient_Broadcast_Call { +func (_c *TxClient_Broadcast_Call) Run(run func(_a0 context.Context, _a1 []types.Msg, _a2 ...v1beta2.BroadcastOption)) *TxClient_Broadcast_Call { _c.Call.Run(func(args mock.Arguments) { - variadicArgs := make([]types.Msg, len(args)-1) - for i, a := range args[1:] { + variadicArgs := make([]v1beta2.BroadcastOption, len(args)-2) + for i, a := range args[2:] { if a != nil { - variadicArgs[i] = a.(types.Msg) + variadicArgs[i] = a.(v1beta2.BroadcastOption) } } - run(args[0].(context.Context), variadicArgs...) + run(args[0].(context.Context), args[1].([]types.Msg), variadicArgs...) }) return _c } -func (_c *TxClient_Broadcast_Call) Return(_a0 error) *TxClient_Broadcast_Call { - _c.Call.Return(_a0) +func (_c *TxClient_Broadcast_Call) Return(_a0 proto.Message, _a1 error) *TxClient_Broadcast_Call { + _c.Call.Return(_a0, _a1) return _c } -func (_c *TxClient_Broadcast_Call) RunAndReturn(run func(context.Context, ...types.Msg) error) *TxClient_Broadcast_Call { +func (_c *TxClient_Broadcast_Call) RunAndReturn(run func(context.Context, []types.Msg, ...v1beta2.BroadcastOption) (proto.Message, error)) *TxClient_Broadcast_Call { _c.Call.Return(run) return _c } diff --git a/go/node/client/v1beta2/tx.go b/go/node/client/v1beta2/tx.go index 39b8d2bd..8070dd9d 100644 --- a/go/node/client/v1beta2/tx.go +++ b/go/node/client/v1beta2/tx.go @@ -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 @@ -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, @@ -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(): @@ -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