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

fix(sdk-utils): return error if BroadcastTx fails #2124

Merged
merged 7 commits into from
Apr 4, 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
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
Loading