Skip to content

Commit

Permalink
fix(sdk-utils): return error if BroadcastTx fails (#2124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalukyanov authored Apr 4, 2024
1 parent 8aba6e5 commit 1199688
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
15 changes: 10 additions & 5 deletions sdk-utils/broadcast/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down
31 changes: 30 additions & 1 deletion sdk-utils/broadcast/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1199688

Please sign in to comment.