generated from flashbots/flashbots-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 221
/
service.go
401 lines (330 loc) · 11.6 KB
/
service.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-utils/httplogger"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
var (
errInvalidSlot = errors.New("invalid slot")
errInvalidHash = errors.New("invalid hash")
errInvalidPubkey = errors.New("invalid pubkey")
errInvalidSignature = errors.New("invalid signature")
errNoSuccessfulRelayResponse = errors.New("no successful relay response")
errServerAlreadyRunning = errors.New("server already running")
)
var nilHash = types.Hash{}
var nilResponse = struct{}{}
type httpErrorResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
// BoostService TODO
type BoostService struct {
listenAddr string
relays []RelayEntry
log *logrus.Entry
srv *http.Server
builderSigningDomain types.Domain
httpClient http.Client
}
// NewBoostService created a new BoostService
func NewBoostService(listenAddr string, relays []RelayEntry, log *logrus.Entry, genesisForkVersionHex string, relayRequestTimeout time.Duration) (*BoostService, error) {
if len(relays) == 0 {
return nil, errors.New("no relays")
}
builderSigningDomain, err := ComputeDomain(types.DomainTypeAppBuilder, genesisForkVersionHex, types.Root{}.String())
if err != nil {
return nil, err
}
return &BoostService{
listenAddr: listenAddr,
relays: relays,
log: log.WithField("module", "service"),
builderSigningDomain: builderSigningDomain,
httpClient: http.Client{Timeout: relayRequestTimeout},
}, nil
}
func (m *BoostService) respondError(w http.ResponseWriter, code int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
resp := httpErrorResp{code, message}
if err := json.NewEncoder(w).Encode(resp); err != nil {
m.log.WithField("response", resp).WithError(err).Error("Couldn't write error response")
http.Error(w, "", http.StatusInternalServerError)
}
}
func (m *BoostService) respondOK(w http.ResponseWriter, response any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
m.log.WithField("response", response).WithError(err).Error("Couldn't write OK response")
http.Error(w, "", http.StatusInternalServerError)
}
}
func (m *BoostService) getRouter() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/", m.handleRoot)
r.HandleFunc(pathStatus, m.handleStatus).Methods(http.MethodGet)
r.HandleFunc(pathRegisterValidator, m.handleRegisterValidator).Methods(http.MethodPost)
r.HandleFunc(pathGetHeader, m.handleGetHeader).Methods(http.MethodGet)
r.HandleFunc(pathGetPayload, m.handleGetPayload).Methods(http.MethodPost)
r.Use(mux.CORSMethodMiddleware(r))
loggedRouter := httplogger.LoggingMiddlewareLogrus(m.log, r)
return loggedRouter
}
// StartHTTPServer starts the HTTP server for this boost service instance
func (m *BoostService) StartHTTPServer() error {
if m.srv != nil {
return errServerAlreadyRunning
}
m.srv = &http.Server{
Addr: m.listenAddr,
Handler: m.getRouter(),
ReadTimeout: 0,
ReadHeaderTimeout: 0,
WriteTimeout: 0,
IdleTimeout: 0,
}
err := m.srv.ListenAndServe()
if err == http.ErrServerClosed {
return nil
}
return err
}
func (m *BoostService) handleRoot(w http.ResponseWriter, req *http.Request) {
m.respondOK(w, nilResponse)
}
// handleStatus sends calls to the status endpoint of every relay.
// It returns OK if at least one returned OK, and returns KO otherwise.
func (m *BoostService) handleStatus(w http.ResponseWriter, req *http.Request) {
var wg sync.WaitGroup
var numSuccessRequestsToRelay uint32
for _, r := range m.relays {
wg.Add(1)
go func(relay RelayEntry) {
defer wg.Done()
log := m.log.WithField("relay", relay.Address)
log.Debug("Checking relay status")
url := relay.Address + pathStatus
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, url, nil, nil)
if err != nil {
log.WithError(err).Error("failed to retrieve relay status")
return
}
atomic.AddUint32(&numSuccessRequestsToRelay, 1)
}(r)
}
// At the end, we wait for every routine and return status according to relay's ones.
wg.Wait()
if numSuccessRequestsToRelay == 0 {
m.respondError(w, http.StatusServiceUnavailable, "all relays are unavailable")
} else {
m.respondOK(w, nilResponse)
}
}
// RegisterValidatorV1 - returns 200 if at least one relay returns 200
func (m *BoostService) handleRegisterValidator(w http.ResponseWriter, req *http.Request) {
log := m.log.WithField("method", "registerValidator")
log.Info("registerValidator")
payload := []types.SignedValidatorRegistration{}
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
m.respondError(w, http.StatusBadRequest, err.Error())
return
}
numSuccessRequestsToRelay := 0
var mu sync.Mutex
// Call the relays
var wg sync.WaitGroup
for _, relay := range m.relays {
wg.Add(1)
go func(relayAddr string) {
defer wg.Done()
url := relayAddr + pathRegisterValidator
log := log.WithField("url", url)
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodPost, url, payload, nil)
if err != nil {
log.WithError(err).Warn("error in registerValidator to relay")
return
}
mu.Lock()
defer mu.Unlock()
numSuccessRequestsToRelay++
}(relay.Address)
}
// Wait for all requests to complete...
wg.Wait()
if numSuccessRequestsToRelay > 0 {
m.respondOK(w, nilResponse)
} else {
m.respondError(w, http.StatusBadGateway, errNoSuccessfulRelayResponse.Error())
}
}
// GetHeaderV1 TODO
func (m *BoostService) handleGetHeader(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
slot := vars["slot"]
parentHashHex := vars["parent_hash"]
pubkey := vars["pubkey"]
log := m.log.WithFields(logrus.Fields{
"method": "getHeader",
"slot": slot,
"parentHash": parentHashHex,
"pubkey": pubkey,
})
log.Info("getHeader")
if _, err := strconv.ParseUint(slot, 10, 64); err != nil {
m.respondError(w, http.StatusBadRequest, errInvalidSlot.Error())
return
}
if len(pubkey) != 98 {
m.respondError(w, http.StatusBadRequest, errInvalidPubkey.Error())
return
}
if len(parentHashHex) != 66 {
m.respondError(w, http.StatusBadRequest, errInvalidHash.Error())
return
}
result := new(types.GetHeaderResponse)
var mu sync.Mutex
// Call the relays
var wg sync.WaitGroup
for _, relay := range m.relays {
wg.Add(1)
go func(relayAddr string, relayPubKey types.PublicKey) {
defer wg.Done()
url := fmt.Sprintf("%s/eth/v1/builder/header/%s/%s/%s", relayAddr, slot, parentHashHex, pubkey)
log := log.WithField("url", url)
responsePayload := new(types.GetHeaderResponse)
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, url, nil, responsePayload)
if err != nil {
log.WithError(err).Warn("error making request to relay")
return
}
// Skip if invalid payload
if responsePayload.Data == nil || responsePayload.Data.Message == nil || responsePayload.Data.Message.Header == nil || responsePayload.Data.Message.Header.BlockHash == nilHash {
return
}
log = log.WithFields(logrus.Fields{
"blockNumber": responsePayload.Data.Message.Header.BlockNumber,
"blockHash": responsePayload.Data.Message.Header.BlockHash,
"txRoot": responsePayload.Data.Message.Header.TransactionsRoot.String(),
"value": responsePayload.Data.Message.Value.String(),
})
// Verify the relay signature in the relay response
ok, err := types.VerifySignature(responsePayload.Data.Message, m.builderSigningDomain, relayPubKey[:], responsePayload.Data.Signature[:])
if err != nil {
log.WithError(err).Error("error verifying relay signature")
return
}
if !ok {
log.WithError(errInvalidSignature).Error("failed to verify relay signature")
return
}
// Compare value of header, skip processing this result if lower fee than current
mu.Lock()
defer mu.Unlock()
// Skip if not a higher value
if result.Data != nil && responsePayload.Data.Message.Value.Cmp(&result.Data.Message.Value) < 1 {
return
}
// Use this relay's response as mev-boost response because it's most profitable
*result = *responsePayload
log.Info("successfully got more valuable payload header")
}(relay.Address, relay.PublicKey)
}
// Wait for all requests to complete...
wg.Wait()
if result.Data == nil || result.Data.Message == nil || result.Data.Message.Header == nil || result.Data.Message.Header.BlockHash == nilHash {
log.Warn("no successful relay response")
m.respondError(w, http.StatusBadGateway, errNoSuccessfulRelayResponse.Error())
return
}
m.respondOK(w, result)
}
func (m *BoostService) handleGetPayload(w http.ResponseWriter, req *http.Request) {
log := m.log.WithField("method", "getPayload")
log.Info("getPayload")
payload := new(types.SignedBlindedBeaconBlock)
if err := json.NewDecoder(req.Body).Decode(payload); err != nil {
m.respondError(w, http.StatusBadRequest, err.Error())
return
}
if len(payload.Signature) != 96 {
m.respondError(w, http.StatusBadRequest, errInvalidSignature.Error())
return
}
result := new(types.GetPayloadResponse)
requestCtx, requestCtxCancel := context.WithCancel(context.Background())
defer requestCtxCancel()
var wg sync.WaitGroup
var mu sync.Mutex
for _, relay := range m.relays {
wg.Add(1)
go func(relayAddr string) {
defer wg.Done()
url := fmt.Sprintf("%s%s", relayAddr, pathGetPayload)
log := log.WithField("url", url)
responsePayload := new(types.GetPayloadResponse)
err := SendHTTPRequest(requestCtx, m.httpClient, http.MethodPost, url, payload, responsePayload)
if err != nil {
log.WithError(err).Warn("error making request to relay")
return
}
if responsePayload.Data == nil || responsePayload.Data.BlockHash == nilHash {
log.Warn("invalid response")
return
}
// Lock before accessing the shared payload
mu.Lock()
defer mu.Unlock()
if requestCtx.Err() != nil { // request has been cancelled (or deadline exceeded)
return
}
// Ensure the response blockhash matches the request
if payload.Message.Body.ExecutionPayloadHeader.BlockHash != responsePayload.Data.BlockHash {
log.WithFields(logrus.Fields{
"payloadBlockHash": payload.Message.Body.ExecutionPayloadHeader.BlockHash,
"responseBlockHash": responsePayload.Data.BlockHash,
}).Warn("requestBlockHash does not equal responseBlockHash")
return
}
// Received successful response. Now cancel other requests and return immediately
requestCtxCancel()
*result = *responsePayload
log.WithFields(logrus.Fields{
"blockHash": responsePayload.Data.BlockHash,
"blockNumber": responsePayload.Data.BlockNumber,
}).Info("getPayload: received payload from relay")
}(relay.Address)
}
// Wait for all requests to complete...
wg.Wait()
if result.Data == nil || result.Data.BlockHash == nilHash {
log.Warn("getPayload: no valid response from relay")
m.respondError(w, http.StatusBadGateway, errNoSuccessfulRelayResponse.Error())
return
}
m.respondOK(w, result)
}
// CheckRelays sends a request to each one of the relays previously registered to get their status
func (m *BoostService) CheckRelays() bool {
for _, relay := range m.relays {
m.log.WithField("relay", relay).Info("Checking relay")
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, relay.Address+pathStatus, nil, nil)
if err != nil {
m.log.WithError(err).WithField("relay", relay).Error("relay check failed")
return false
}
}
return true
}