|
| 1 | +package fetch |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "go-backfill/config" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +type BlockInfo struct { |
| 15 | + Header Header `json:"header"` |
| 16 | + Payload Payload `json:"payloadWithOutputs"` |
| 17 | +} |
| 18 | + |
| 19 | +type Adjacents map[string]string |
| 20 | + |
| 21 | +type Header struct { |
| 22 | + Nonce string `json:"nonce"` |
| 23 | + CreationTime int64 `json:"creationTime"` |
| 24 | + Parent string `json:"parent"` |
| 25 | + Adjacents Adjacents `json:"adjacents"` |
| 26 | + Target string `json:"target"` |
| 27 | + PayloadHash string `json:"payloadHash"` |
| 28 | + ChainId int `json:"chainId"` |
| 29 | + Weight string `json:"weight"` |
| 30 | + Height int `json:"height"` |
| 31 | + ChainwebVersion string `json:"chainwebVersion"` |
| 32 | + EpochStart int64 `json:"epochStart"` |
| 33 | + FeatureFlags uint64 `json:"featureFlags"` |
| 34 | + Hash string `json:"hash"` |
| 35 | +} |
| 36 | + |
| 37 | +type Payload struct { |
| 38 | + Transactions [][2]string `json:"transactions"` |
| 39 | + MinerData string `json:"minerData"` |
| 40 | + TransactionsHash string `json:"transactionsHash"` |
| 41 | + OutputsHash string `json:"outputsHash"` |
| 42 | + PayloadHash string `json:"payloadHash"` |
| 43 | + Coinbase string `json:"coinbase"` |
| 44 | +} |
| 45 | + |
| 46 | +func FetchPayloadsWithHeaders(network string, chainId int, Hash string, minHeight int, maxHeight int) ([]BlockInfo, error) { |
| 47 | + type FetchResponse struct { |
| 48 | + Items []BlockInfo `json:"items"` |
| 49 | + } |
| 50 | + |
| 51 | + startTime := time.Now() |
| 52 | + env := config.GetConfig() |
| 53 | + endpoint := fmt.Sprintf("%s/%s/chain/%d/block/branch?minheight=%d&maxheight=%d", env.SyncBaseUrl, network, chainId, minHeight, maxHeight) |
| 54 | + |
| 55 | + param := map[string]interface{}{ |
| 56 | + "upper": []string{Hash}, |
| 57 | + } |
| 58 | + |
| 59 | + paramJSON, err := json.Marshal(param) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to marshal payload hashes to JSON: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + attempt := 1 |
| 65 | + for attempt <= env.SyncAttemptsMaxRetry { |
| 66 | + req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(paramJSON)) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("failed to create request: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + req.Header.Set("Content-Type", "application/json") |
| 72 | + req.Header.Set("Accept", "application/json") |
| 73 | + |
| 74 | + client := &http.Client{} |
| 75 | + resp, err := client.Do(req) |
| 76 | + if err != nil { |
| 77 | + log.Printf("Attempt %d: Error making POST request for payloads: %v\n", attempt, err) |
| 78 | + if attempt == env.SyncAttemptsMaxRetry { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + attempt++ |
| 83 | + time.Sleep(time.Duration(env.SyncAttemptsIntervalInMs) * time.Millisecond) |
| 84 | + continue |
| 85 | + } |
| 86 | + defer resp.Body.Close() |
| 87 | + |
| 88 | + if resp.StatusCode != http.StatusOK { |
| 89 | + log.Printf("Attempt %d: Received non-OK HTTP status %d\n", attempt, resp.StatusCode) |
| 90 | + if attempt == env.SyncAttemptsMaxRetry { |
| 91 | + return nil, fmt.Errorf("received non-OK HTTP status: %d", resp.StatusCode) |
| 92 | + } |
| 93 | + |
| 94 | + attempt++ |
| 95 | + time.Sleep(time.Duration(env.SyncAttemptsIntervalInMs) * time.Millisecond) |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + body, err := io.ReadAll(resp.Body) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to read response body: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + var payload FetchResponse |
| 105 | + err = json.Unmarshal(body, &payload) |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("failed to unmarshal JSON response: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + if len(payload.Items) == 0 { |
| 111 | + log.Printf("Attempt %d: No payloads found, retrying...\n", attempt) |
| 112 | + if attempt == env.SyncAttemptsMaxRetry { |
| 113 | + return nil, fmt.Errorf("no payloads found after maximum attempts: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + attempt++ |
| 117 | + time.Sleep(time.Duration(env.SyncAttemptsIntervalInMs) * time.Millisecond) |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + log.Printf("Fetched payloads in %fs\n", time.Since(startTime).Seconds()) |
| 122 | + return payload.Items, nil |
| 123 | + } |
| 124 | + |
| 125 | + return nil, fmt.Errorf("failed to fetch payloads after maximum retry attempts: %v", err) |
| 126 | +} |
0 commit comments