@@ -5,25 +5,58 @@ import (
55 "fmt"
66 "io"
77 "net/http"
8+ "time"
89
910 "github.com/ugorji/go/codec"
1011
1112 "github.com/yetanotherco/aligned_layer/operator/merkle_tree"
1213)
1314
14- func (o * Operator ) getBatchFromS3 (ctx context.Context , batchURL string , expectedMerkleRoot [32 ]byte ) ([]VerificationData , error ) {
15- o .Logger .Infof ("Getting batch from S3..., batchURL: %s" , batchURL )
15+ func (o * Operator ) getBatchFromDataService (ctx context.Context , batchURL string , expectedMerkleRoot [32 ]byte , maxRetries int , retryDelay time.Duration ) ([]VerificationData , error ) {
16+ o .Logger .Infof ("Getting batch from data service, batchURL: %s" , batchURL )
17+
18+ var resp * http.Response
19+ var err error
20+ var req * http.Request
21+
22+ for attempt := 0 ; attempt < maxRetries ; attempt ++ {
23+ if attempt > 0 {
24+ o .Logger .Infof ("Waiting for %s before retrying data fetch (attempt %d of %d)" , retryDelay , attempt + 1 , maxRetries )
25+ select {
26+ case <- time .After (retryDelay ):
27+ // Wait before retrying
28+ case <- ctx .Done ():
29+ return nil , ctx .Err ()
30+ }
31+ retryDelay *= 2 // Exponential backoff. Ex: 5s, 10s, 20s
32+ }
1633
17- req , err := http .NewRequestWithContext (ctx , "GET" , batchURL , nil )
34+ req , err = http .NewRequestWithContext (ctx , "GET" , batchURL , nil )
35+ if err != nil {
36+ return nil , err
37+ }
1838
19- if err != nil {
20- return nil , err
39+ resp , err = http .DefaultClient .Do (req )
40+ if err == nil && resp .StatusCode == http .StatusOK {
41+ break // Successful request, exit retry loop
42+ }
43+
44+ if resp != nil {
45+ err := resp .Body .Close ()
46+ if err != nil {
47+ return nil , err
48+ }
49+ }
50+
51+ o .Logger .Warnf ("Error fetching batch from data service - (attempt %d): %v" , attempt + 1 , err )
2152 }
2253
23- resp , err := http .DefaultClient .Do (req )
2454 if err != nil {
2555 return nil , err
2656 }
57+
58+ // At this point, the HTTP request was successfull.
59+
2760 defer func (Body io.ReadCloser ) {
2861 err := Body .Close ()
2962 if err != nil {
@@ -33,7 +66,7 @@ func (o *Operator) getBatchFromS3(ctx context.Context, batchURL string, expected
3366
3467 // Check if the response is OK
3568 if resp .StatusCode != http .StatusOK {
36- return nil , fmt .Errorf ("error getting Proof Head from S3 : %s" , resp .Status )
69+ return nil , fmt .Errorf ("error getting batch from data service : %s" , resp .Status )
3770 }
3871
3972 contentLength := resp .ContentLength
@@ -58,8 +91,8 @@ func (o *Operator) getBatchFromS3(ctx context.Context, batchURL string, expected
5891
5992 // Checks if downloaded merkle root is the same as the expected one
6093 o .Logger .Infof ("Verifying batch merkle tree..." )
61- merkle_root_check := merkle_tree .VerifyMerkleTreeBatch (batchBytes , uint (len (batchBytes )), expectedMerkleRoot )
62- if ! merkle_root_check {
94+ merkleRootCheck := merkle_tree .VerifyMerkleTreeBatch (batchBytes , uint (len (batchBytes )), expectedMerkleRoot )
95+ if ! merkleRootCheck {
6396 return nil , fmt .Errorf ("merkle root check failed" )
6497 }
6598 o .Logger .Infof ("Batch merkle tree verified" )
0 commit comments