From 1199688a5ba6f79fd06903a3d55e6fb58effbabf Mon Sep 17 00:00:00 2001 From: Ilya Lukyanov Date: Fri, 5 Apr 2024 12:25:15 +1300 Subject: [PATCH] fix(sdk-utils): return error if BroadcastTx fails (#2124) --- sdk-utils/broadcast/broadcast.go | 15 ++++++++----- sdk-utils/broadcast/broadcast_test.go | 31 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/sdk-utils/broadcast/broadcast.go b/sdk-utils/broadcast/broadcast.go index 4c045f64b..1f4c244cb 100644 --- a/sdk-utils/broadcast/broadcast.go +++ b/sdk-utils/broadcast/broadcast.go @@ -77,7 +77,12 @@ func isSequenceMismatch(err error) bool { // Broadcast sends the given tx to the blockchain and blocks until it is added to a block (or timeout). func Broadcast(ctx sdkClient.Context, txBytes []byte, options ...BroadcasterOption) (*sdk.TxResponse, error) { res, err := ctx.BroadcastTx(txBytes) - if err == nil && ctx.BroadcastMode != flags.BroadcastBlock { + switch { + case err != nil: + return nil, err + case res.Code != abci.CodeTypeOK: + return nil, sdkerrors.ABCIError(res.Codespace, res.Code, res.RawLog) + case ctx.BroadcastMode != flags.BroadcastBlock: params := broadcastParams{ Timeout: config.DefaultRPCConfig().TimeoutBroadcastTxCommit, PollingInterval: 2 * time.Second, @@ -88,11 +93,11 @@ func Broadcast(ctx sdkClient.Context, txBytes []byte, options ...BroadcasterOpti res, err = waitForBlockInclusion(ctx, res.TxHash, params) } - if err != nil { - return nil, err - } - if res.Code != abci.CodeTypeOK { + switch { + case err != nil: + return nil, err + case res.Code != abci.CodeTypeOK: return nil, sdkerrors.ABCIError(res.Codespace, res.Code, res.RawLog) } diff --git a/sdk-utils/broadcast/broadcast_test.go b/sdk-utils/broadcast/broadcast_test.go index 58bfe779a..284dc3133 100644 --- a/sdk-utils/broadcast/broadcast_test.go +++ b/sdk-utils/broadcast/broadcast_test.go @@ -103,7 +103,7 @@ func TestStatefulBroadcaster(t *testing.T) { } }) - getAccountSequenceMismatch := When("get an account seuqence mismatch", func() { + getAccountSequenceMismatch := When("get an account sequence mismatch", func() { clientMock.ABCIQueryWithOptionsFunc = func(context.Context, string, bytes.HexBytes, rpcclient.ABCIQueryOptions) (*coretypes.ResultABCIQuery, error) { return nil, sdkerrors.ErrWrongSequence } @@ -115,6 +115,21 @@ func TestStatefulBroadcaster(t *testing.T) { } }) + broadcastSyncReturnsErrorCode := When("BroadcastTxSync returns error code", func() { + clientMock.BroadcastTxSyncFunc = func(context.Context, tm.Tx) (*coretypes.ResultBroadcastTx, error) { + return &coretypes.ResultBroadcastTx{ + Code: mathRand.Uint32(), + Log: "broadcast failed", + }, nil + } + }) + + broadcastSyncFails := When("BroadcastTxSync fails", func() { + clientMock.BroadcastTxSyncFunc = func(context.Context, tm.Tx) (*coretypes.ResultBroadcastTx, error) { + return nil, errors.New("broadcast failed") + } + }) + txsGetExecuted := When("txs get executed correctly", func() { clientMock.TxFunc = func(context.Context, []byte, bool) (*coretypes.ResultTx, error) { expectedResponse = &coretypes.ResultTx{TxResult: abci.ResponseDeliverTx{ @@ -222,6 +237,20 @@ func TestStatefulBroadcaster(t *testing.T) { givenSetup. When2(sendingNoMessages). Then2(returnError).Run(t) + + givenSetup. + When2(sendingMultipleMessages). + When2(accountExists). + When2(simulationSucceeds). + When2(broadcastSyncReturnsErrorCode). + Then2(returnErrorWithCode).Run(t) + + givenSetup. + When2(sendingMultipleMessages). + When2(accountExists). + When2(simulationSucceeds). + When2(broadcastSyncFails). + Then2(returnError).Run(t) } func TestWithRefund(t *testing.T) {