-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfetcher_api.go
More file actions
105 lines (86 loc) · 2.57 KB
/
Copy pathfetcher_api.go
File metadata and controls
105 lines (86 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package dasmon
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
var _ Fetcher = &FetcherBeaconAPI{}
type FetcherBeaconAPI struct {
baseURL string
client *http.Client
}
func NewFetcherBeaconAPI(baseURL string) *FetcherBeaconAPI {
return &FetcherBeaconAPI{
baseURL: baseURL,
client: &http.Client{
Timeout: 10 * time.Second,
},
}
}
func (f *FetcherBeaconAPI) MaxSlotsPerFetch() uint64 {
return 1
}
func (f *FetcherBeaconAPI) FetchCommitments(ctx context.Context, fetchRange Range[uint64]) ([]*BlockCommitmentInfo, error) {
if fetchRange.End-fetchRange.Start != 0 {
return nil, fmt.Errorf("beacon API fetcher only supports single slot fetches")
}
slot := fetchRange.Start
url := fmt.Sprintf("%s/eth/v2/beacon/blocks/%d", f.baseURL, slot)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
resp, err := f.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch block from API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to fetch block at slot %d: %s", slot, body)
}
// Parse JSON response
var apiResp struct {
Data struct {
Message struct {
Slot string `json:"slot"`
Body struct {
BlobKZGCommitments []string `json:"blob_kzg_commitments"`
} `json:"body"`
} `json:"message"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return nil, fmt.Errorf("failed to decode API response: %w", err)
}
// Convert hex-encoded commitments to byte slices
commitments := make([][]byte, len(apiResp.Data.Message.Body.BlobKZGCommitments))
for i, comm := range apiResp.Data.Message.Body.BlobKZGCommitments {
comm, err := hex.DecodeString(strings.TrimPrefix(comm, "0x"))
if err != nil {
return nil, fmt.Errorf("failed to decode commitment hex string: %w", err)
}
commitments[i] = comm
}
// TODO
// For now, we don't have the block root from this API response
// In a real implementation, you might need to compute it or fetch it separately
// For our purposes, we'll leave it as zero and rely on slot-based lookups
var blockRoot [32]byte
comm := &BlockCommitmentInfo{
Slot: fetchRange.Start,
BlockRoot: blockRoot,
BlobCommitments: commitments,
}
return []*BlockCommitmentInfo{comm}, nil
}
func (f *FetcherBeaconAPI) Close() error {
f.client.CloseIdleConnections()
return nil
}