-
Notifications
You must be signed in to change notification settings - Fork 12
/
data-api-backfill.go
216 lines (186 loc) · 6.16 KB
/
data-api-backfill.go
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
package core
import (
"context"
"database/sql"
"errors"
"fmt"
"net/http"
"time"
relaycommon "github.com/flashbots/mev-boost-relay/common"
"github.com/flashbots/relayscan/common"
"github.com/flashbots/relayscan/database"
"github.com/flashbots/relayscan/vars"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
cliRelay string
minSlot int64
initCursor uint64
pageLimit = 100 // 100 is max on bloxroute
)
func init() {
backfillDataAPICmd.Flags().StringVar(&cliRelay, "relay", "", "specific relay only")
backfillDataAPICmd.Flags().Uint64Var(&initCursor, "cursor", 0, "initial cursor")
backfillDataAPICmd.Flags().Int64Var(&minSlot, "min-slot", 0, "minimum slot (if unset, backfill until the merge, negative number for that number of slots before latest)")
}
var backfillDataAPICmd = &cobra.Command{
Use: "data-api-backfill",
Short: "Backfill all relays data API",
Run: func(cmd *cobra.Command, args []string) {
var err error
var relays []common.RelayEntry
startTime := time.Now().UTC()
if cliRelay != "" {
var relayEntry common.RelayEntry
if cliRelay == "fb" {
relayEntry, err = common.NewRelayEntry(vars.RelayURLs[0], false)
} else {
relayEntry, err = common.NewRelayEntry(cliRelay, false)
}
if err != nil {
log.WithField("relay", cliRelay).WithError(err).Fatal("failed to decode relay")
}
relays = []common.RelayEntry{relayEntry}
} else {
relays, err = common.GetRelays()
if err != nil {
log.WithError(err).Fatal("failed to get relays")
}
}
log.Infof("Relayscan %s", vars.Version)
log.Infof("Using %d relays", len(relays))
for index, relay := range relays {
log.Infof("- relay #%d: %s", index+1, relay.Hostname())
}
// Connect to Postgres
db := database.MustConnectPostgres(log, vars.DefaultPostgresDSN)
// If needed, get latest slot (i.e. if min-slot is negative)
if minSlot < 0 {
log.Infof("Getting latest slot from beaconcha.in for offset %d", minSlot)
latestSlotOnBeaconChain := common.MustGetLatestSlot()
log.Infof("Latest slot from beaconcha.in: %d", latestSlotOnBeaconChain)
minSlot = int64(latestSlotOnBeaconChain) + minSlot
}
if minSlot != 0 {
log.Infof("Using min slot: %d", minSlot)
}
for _, relay := range relays {
log.Infof("Starting backfilling for relay %s ...", relay.Hostname())
backfiller := newBackfiller(db, relay, initCursor, uint64(minSlot))
err = backfiller.backfillPayloadsDelivered()
if err != nil {
log.WithError(err).WithField("relay", relay).Error("backfill failed")
}
}
timeNeeded := time.Since(startTime)
log.WithField("timeNeeded", timeNeeded).Info("All done!")
},
}
type backfiller struct {
relay common.RelayEntry
db *database.DatabaseService
cursorSlot uint64
minSlot uint64
}
func newBackfiller(db *database.DatabaseService, relay common.RelayEntry, cursorSlot, minSlot uint64) *backfiller {
return &backfiller{
relay: relay,
db: db,
cursorSlot: cursorSlot,
minSlot: minSlot,
}
}
func (bf *backfiller) backfillPayloadsDelivered() error {
_log := log.WithField("relay", bf.relay.Hostname())
// _log.Info("backfilling payloads from relay data-api ...")
// 1. get latest entry from DB
latestEntry, err := bf.db.GetDataAPILatestPayloadDelivered(bf.relay.Hostname())
latestSlotInDB := uint64(0)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
_log.WithError(err).Fatal("failed to get latest entry")
return err
} else {
latestSlotInDB = latestEntry.Slot
}
_log.Infof("Latest payload in DB for slot: %d", latestSlotInDB)
// 2. backfill until latest DB entry is reached
baseURL := bf.relay.GetURI("/relay/v1/data/bidtraces/proposer_payload_delivered")
cursorSlot := bf.cursorSlot
slotsReceived := make(map[uint64]bool)
builders := make(map[string]bool)
for {
payloadsNew := 0
url := fmt.Sprintf("%s?limit=%d", baseURL, pageLimit)
if cursorSlot > 0 {
url = fmt.Sprintf("%s&cursor=%d", url, cursorSlot)
}
_log.WithField("url: ", url).Info("Fetching payloads...")
var data []relaycommon.BidTraceV2JSON
_, err = common.SendHTTPRequest(context.Background(), *http.DefaultClient, http.MethodGet, url, nil, &data)
if err != nil {
return err
}
_log.Infof("Response contains %d delivered payloads", len(data))
// build a list of entries for batch DB update
entries := make([]*database.DataAPIPayloadDeliveredEntry, len(data))
slotFirst := uint64(0)
slotLast := uint64(0)
for index, payload := range data {
_log.Debugf("saving entry for slot %d", payload.Slot)
dbEntry := database.BidTraceV2JSONToPayloadDeliveredEntry(bf.relay.Hostname(), payload)
entries[index] = &dbEntry
// Set first and last slot
if slotFirst == 0 || payload.Slot < slotFirst {
slotFirst = payload.Slot
}
if slotLast == 0 || payload.Slot > slotLast {
slotLast = payload.Slot
}
// Count number of slots with payloads
if !slotsReceived[payload.Slot] {
slotsReceived[payload.Slot] = true
payloadsNew += 1
}
// Set cursor for next request
if cursorSlot == 0 || cursorSlot > payload.Slot {
cursorSlot = payload.Slot
}
// Remember the builder
builders[payload.BuilderPubkey] = true
}
// Save entries
newEntries, err := bf.db.SaveDataAPIPayloadDeliveredBatch(entries)
if err != nil {
_log.WithError(err).Fatal("failed to save entries")
return err
}
_log.WithFields(logrus.Fields{
"newEntries": newEntries,
"slotFirst": slotFirst,
"slotLast": slotLast,
}).Info("Batch of payloads saved to database")
// Save builders
for builderPubkey := range builders {
err = bf.db.SaveBuilder(&database.BlockBuilderEntry{BuilderPubkey: builderPubkey})
if err != nil {
_log.WithError(err).Error("failed to save builder")
}
}
// Stop as soon as no new payloads are received
if payloadsNew == 0 {
_log.Infof("No new payloads, all done. Earliest payload for slot: %d", cursorSlot)
return nil
}
// Stop if at the latest slot in DB
if cursorSlot < latestSlotInDB {
_log.Infof("Payloads backfilled until latest slot in DB: %d", latestSlotInDB)
return nil
}
// Stop if at min slot
if cursorSlot < bf.minSlot {
_log.Infof("Payloads backfilled until min slot: %d", bf.minSlot)
return nil
}
}
}