-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeacon.go
More file actions
218 lines (196 loc) · 5.51 KB
/
beacon.go
File metadata and controls
218 lines (196 loc) · 5.51 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
)
// resolveHeadRoot queries the consensus REST API to resolve the head beacon block root.
func resolveHeadRoot(ctx context.Context, client *http.Client, consensusAPI string) (string, error) {
base := strings.TrimRight(consensusAPI, "/")
url := base + "/eth/v1/beacon/headers/head"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// try v2 blocks endpoint as a fallback
return resolveHeadRootFallback(ctx, client, base)
}
var payload struct {
Data struct {
Root string `json:"root"`
} `json:"data"`
}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&payload); err != nil {
return "", err
}
if payload.Data.Root == "" {
return resolveHeadRootFallback(ctx, client, base)
}
return payload.Data.Root, nil
}
func resolveHeadRootFallback(ctx context.Context, client *http.Client, base string) (string, error) {
url := base + "/eth/v2/beacon/blocks/head"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// best-effort parse: check top-level root, or data.root
var m map[string]interface{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&m); err != nil {
return "", err
}
if v, ok := m["root"].(string); ok && v != "" {
return v, nil
}
if data, ok := m["data"].(map[string]interface{}); ok {
if v, ok := data["root"].(string); ok && v != "" {
return v, nil
}
}
return "", io.EOF
}
// enrichSlotConsensus fetches the beacon block from the consensus REST API and fills
// missing execution/eth1 fields in the provided slot data map.
func enrichSlotConsensus(ctx context.Context, client *http.Client, consensusAPI string, blockID string, slotData map[string]interface{}) {
base := strings.TrimRight(consensusAPI, "/")
url := base + "/eth/v2/beacon/blocks/" + blockID
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return
}
var payload map[string]interface{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&payload); err != nil {
return
}
data, _ := payload["data"].(map[string]interface{})
if data == nil {
return
}
message, _ := data["message"].(map[string]interface{})
if message == nil {
return
}
body, _ := message["body"].(map[string]interface{})
if body == nil {
return
}
// Add dora missing fields: signature
if sig, ok := data["signature"].(string); ok && sig != "" {
setStringIfEmpty(slotData, "signature", sig)
}
// Eth1 data
if eth1, ok := body["eth1_data"].(map[string]interface{}); ok {
if v, ok := eth1["deposit_count"]; ok {
if n, ok2 := parseUint64FromInterface(v); ok2 {
setUintIfZero(slotData, "eth1data_depositcount", n)
}
}
if v, ok := eth1["deposit_root"].(string); ok {
setStringIfEmpty(slotData, "eth1data_depositroot", v)
}
if v, ok := eth1["block_hash"].(string); ok {
setStringIfEmpty(slotData, "eth1data_blockhash", v)
}
}
// Sync aggregate (body.sync_aggregate)
if sa, ok := body["sync_aggregate"].(map[string]interface{}); ok {
if v, ok := sa["sync_committee_bits"].(string); ok {
setStringIfEmpty(slotData, "syncaggregate_bits", v)
}
if v, ok := sa["sync_committee_signature"].(string); ok {
setStringIfEmpty(slotData, "syncaggregate_signature", v)
}
}
// Randao reveal (body.randao_reveal)
if rr, ok := body["randao_reveal"].(string); ok {
setStringIfEmpty(slotData, "randaoreveal", rr)
}
// Execution payload(exec_logs_bloom, exec_parent_hash, exec_random, exec_receipts_root, exec_state_root, exec_timestamp)
if exec, ok := body["execution_payload"].(map[string]interface{}); ok {
if v, ok := exec["logs_bloom"].(string); ok {
setStringIfEmpty(slotData, "exec_logs_bloom", v)
}
if v, ok := exec["parent_hash"].(string); ok {
setStringIfEmpty(slotData, "exec_parent_hash", v)
}
if v, ok := exec["prev_randao"].(string); ok {
setStringIfEmpty(slotData, "exec_random", v)
}
if v, ok := exec["receipts_root"].(string); ok { // some impls use receipt_root
setStringIfEmpty(slotData, "exec_receipts_root", v)
}
if v, ok := exec["state_root"].(string); ok {
setStringIfEmpty(slotData, "exec_state_root", v)
}
if v, ok := exec["timestamp"].(string); ok {
setStringIfEmpty(slotData, "exec_timestamp", v)
}
}
}
func parseUint64FromInterface(v interface{}) (uint64, bool) {
switch t := v.(type) {
case string:
if t == "" {
return 0, false
}
n, err := strconv.ParseUint(t, 10, 64)
if err != nil {
return 0, false
}
return n, true
case float64:
return uint64(t), true
default:
return 0, false
}
}
func setStringIfEmpty(m map[string]interface{}, key string, value string) {
if m == nil || value == "" {
return
}
if cur, ok := m[key]; ok {
if s, ok := cur.(string); ok && s != "" {
return
}
}
m[key] = value
}
func setUintIfZero(m map[string]interface{}, key string, value uint64) {
if m == nil || value == 0 {
return
}
if cur, ok := m[key]; ok {
if f, ok := cur.(float64); ok && f != 0 {
return
}
}
m[key] = value
}