SendDataWithRetry doesn't work properly(Retries will not happen)
The SendDataWithRetry function in Allora's appchain code checks the error return value from BroadcastTx, but the underlying Cosmos SDK BroadcastTxAsync returns errors in the first return value (the response) and nil as the error. As a result, the retry loop never triggers because err is always nil, so failed transactions are not retried.
Source: https://github.com/sherlock-audit/2024-06-allora-judging/issues/52
Found by
volodya
Summary
Cosmos's BroadcastTx returns an error in the first argument instead of the second one.
Vulnerability Detail
Here is the current Allora implementation for .BroadcastTx( where its trying to handle retries
func (ap *AppChain) SendDataWithRetry(ctx context.Context, req sdktypes.Msg, MaxRetries, MinDelay, MaxDelay int, SuccessMsg string) (*cosmosclient.Response, error) {
var txResp *cosmosclient.Response
var err error
for retryCount := 0; retryCount <= MaxRetries; retryCount++ {
txResponse, err := ap.Client.BroadcastTx(ctx, ap.Account, req)
txResp = &txResponse
if err == nil {
ap.Logger.Info().Str("Tx Hash:", txResp.TxHash).Msg("Success: " + SuccessMsg)
break
}
// Log the error for each retry.
ap.Logger.Info().Err(err).Msgf("Failed: "+SuccessMsg+", retrying... (Retry %d/%d)", retryCount, MaxRetries)
// Generate a random number between MinDelay and MaxDelay
randomDelay := rand.Intn(MaxDelay-MinDelay+1) + MinDelay
// Apply exponential backoff to the random delay
backoffDelay := randomDelay << retryCount
// Wait for the calculated delay before retrying
time.Sleep(time.Duration(backoffDelay) * time.Second)
}
return txResp, err
}
Here is how BroadcastTx function works under the hood where it returns errors in the first argument and nill as the second variable which will pass in the current Allora implementation
func (ctx Context) BroadcastTxAsync(txBytes []byte) (*sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
res, err := node.BroadcastTxAsync(context.Background(), txBytes)
if errRes := CheckCometError(err, txBytes); errRes != nil {
return errRes, nil // @audit returns errors in first argument
}
return sdk.NewResponseFormatBroadcastTx(res), err
}
Impact
Retries will not happen, error will be passed
Code Snippet
Tool used
Manual Review
Recommendation
Copy how its done in sei
grpcRes, err := TX_CLIENT.BroadcastTx(
context.Background(),
&typestx.BroadcastTxRequest{
Mode: typestx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes,
},
)
if err != nil {
panic(err)
}
for grpcRes.TxResponse.Code == sdkerrors.ErrMempoolIsFull.ABCICode() {
// retry after a second until either succeed or fail for some other reason
fmt.Printf("Mempool full\n")
time.Sleep(1 * time.Second)
grpcRes, err = TX_CLIENT.BroadcastTx(
context.Background(),
&typestx.BroadcastTxRequest{
Mode: typestx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes,
},
)
if err != nil {
panic(err)
}
}
if grpcRes.TxResponse.Code != 0 {
fmt.Printf("Error: %d\n", grpcRes.TxResponse.Code)
} else {
mu.Lock()
defer mu.Unlock()
TX_HASH_FILE.WriteString(fmt.Sprintf("%s\n", grpcRes.TxResponse.TxHash))
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/allora-network/allora-chain/pull/494
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code assumes BroadcastTx returns errors in the second return value, but the Cosmos SDK function actually returns errors in the first return value (the response) and nil as the error.
Impact
Retries will not happen, and errors will be passed silently, potentially causing failed transactions to be ignored.
What the attacker needed
None
The fix
The issue was fixed by the protocol team in a pull request.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a program sends a transaction to a blockchain, it can fail. The code here tries to retry sending if it fails, but it checks the wrong variable to see if a failure happened. So it thinks everything succeeded and never retries.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.