Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update test case for faucet
  • Loading branch information
Anmol1696 committed Mar 25, 2025
commit 84e52751a674598476922045b62ce61ee6cf739a
36 changes: 35 additions & 1 deletion starship/faucet/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"strings"
"sync"
"time"

"go.uber.org/zap"

Expand Down Expand Up @@ -293,6 +294,39 @@ func (a *Account) queryTx(txhash string) (map[string]interface{}, error) {
return txMap, nil
}

// queryTxWithRetry wraps queryTx with retry logic on tx "not found" error
func (a *Account) queryTxWithRetry(txhash string, maxRetries int) (map[string]interface{}, error) {
var (
txMap map[string]interface{}
err error
)

for attempt := 1; attempt <= maxRetries; attempt++ {
txMap, err = a.queryTx(txhash)
if err == nil {
return txMap, nil
}

// Check explicitly for the "not found" error condition
if strings.Contains(err.Error(), "not found") {
a.logger.Warn("transaction not found, retrying",
zap.String("txhash", txhash),
zap.Int("attempt", attempt),
zap.Int("maxRetries", maxRetries),
)

// Sleep 2-3 seconds before retry
time.Sleep(2*time.Second + time.Duration(rand.Intn(1000))*time.Millisecond)
continue
}

// If any other error occurs, return immediately
return nil, err
}

return nil, fmt.Errorf("transaction not found after retries: txhash: %s", txhash)
}

// sendTokens performs chain binary send txn from account
func (a *Account) sendTokens(address string, denom string, amount string) error {
ok := a.mu.TryLock()
Expand Down Expand Up @@ -320,7 +354,7 @@ func (a *Account) sendTokens(address string, denom string, amount string) error
a.logger.Debug("send tokens txhash", zap.String("txhash", txhash))

// query tx to check if the tx was successful
txMap, err := a.queryTx(txhash)
txMap, err := a.queryTxWithRetry(txhash, 3)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion starship/tests/e2e/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
urlpkg "net/url"
"strconv"
Expand Down Expand Up @@ -131,9 +132,17 @@ func (s *TestSuite) creditAccount(chain *Chain, addr, denom string) error {
if err != nil {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unexpected status code: %d, and failed to read response body: %w", resp.StatusCode, err)
}
defer resp.Body.Close()

return fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, string(bodyBytes))
}

return nil
}

Expand Down
Loading