generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 118
/
service.go
1439 lines (1227 loc) · 46.4 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package api contains the API webserver for the proposer and block-builder APIs
package api
import (
"compress/gzip"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/NYTimes/gziphandler"
"github.com/buger/jsonparser"
"github.com/flashbots/go-boost-utils/bls"
"github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-utils/cli"
"github.com/flashbots/go-utils/httplogger"
"github.com/flashbots/mev-boost-relay/beaconclient"
"github.com/flashbots/mev-boost-relay/common"
"github.com/flashbots/mev-boost-relay/database"
"github.com/flashbots/mev-boost-relay/datastore"
"github.com/go-redis/redis/v9"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
uberatomic "go.uber.org/atomic"
)
var (
ErrMissingLogOpt = errors.New("log parameter is nil")
ErrMissingBeaconClientOpt = errors.New("beacon-client is nil")
ErrMissingDatastoreOpt = errors.New("proposer datastore is nil")
ErrRelayPubkeyMismatch = errors.New("relay pubkey does not match existing one")
ErrServerAlreadyStarted = errors.New("server was already started")
ErrBuilderAPIWithoutSecretKey = errors.New("cannot start builder API without secret key")
)
var (
// Proposer API (builder-specs)
pathStatus = "/eth/v1/builder/status"
pathRegisterValidator = "/eth/v1/builder/validators"
pathGetHeader = "/eth/v1/builder/header/{slot:[0-9]+}/{parent_hash:0x[a-fA-F0-9]+}/{pubkey:0x[a-fA-F0-9]+}"
pathGetPayload = "/eth/v1/builder/blinded_blocks"
// Block builder API
pathBuilderGetValidators = "/relay/v1/builder/validators"
pathSubmitNewBlock = "/relay/v1/builder/blocks"
// Data API
pathDataProposerPayloadDelivered = "/relay/v1/data/bidtraces/proposer_payload_delivered"
pathDataBuilderBidsReceived = "/relay/v1/data/bidtraces/builder_blocks_received"
pathDataValidatorRegistration = "/relay/v1/data/validator_registration"
// Internal API
pathInternalBuilderStatus = "/internal/v1/builder/{pubkey:0x[a-fA-F0-9]+}"
// number of goroutines to save active validator
numActiveValidatorProcessors = cli.GetEnvInt("NUM_ACTIVE_VALIDATOR_PROCESSORS", 10)
numValidatorRegProcessors = cli.GetEnvInt("NUM_VALIDATOR_REG_PROCESSORS", 10)
timeoutGetPayloadRetryMs = cli.GetEnvInt("GETPAYLOAD_RETRY_TIMEOUT_MS", 100)
)
// RelayAPIOpts contains the options for a relay
type RelayAPIOpts struct {
Log *logrus.Entry
ListenAddr string
BlockSimURL string
BeaconClient beaconclient.IMultiBeaconClient
Datastore *datastore.Datastore
Redis *datastore.RedisCache
DB database.IDatabaseService
SecretKey *bls.SecretKey // used to sign bids (getHeader responses)
// Network specific variables
EthNetDetails common.EthNetworkDetails
// APIs to enable
ProposerAPI bool
BlockBuilderAPI bool
DataAPI bool
PprofAPI bool
InternalAPI bool
}
type randaoHelper struct {
slot uint64
prevRandao string
}
// RelayAPI represents a single Relay instance
type RelayAPI struct {
opts RelayAPIOpts
log *logrus.Entry
blsSk *bls.SecretKey
publicKey *types.PublicKey
srv *http.Server
srvStarted uberatomic.Bool
beaconClient beaconclient.IMultiBeaconClient
datastore *datastore.Datastore
redis *datastore.RedisCache
db database.IDatabaseService
headSlot uberatomic.Uint64
genesisInfo *beaconclient.GetGenesisResponse
proposerDutiesLock sync.RWMutex
proposerDutiesResponse []types.BuilderGetValidatorsResponseEntry
proposerDutiesMap map[uint64]*types.RegisterValidatorRequestMessage
proposerDutiesSlot uint64
isUpdatingProposerDuties uberatomic.Bool
blockSimRateLimiter *BlockSimulationRateLimiter
activeValidatorC chan types.PubkeyHex
validatorRegC chan types.SignedValidatorRegistration
// used to wait on any active getPayload calls on shutdown
getPayloadCallsInFlight sync.WaitGroup
// Feature flags
ffForceGetHeader204 bool
ffDisableBlockPublishing bool
ffDisableLowPrioBuilders bool
expectedPrevRandao randaoHelper
expectedPrevRandaoLock sync.RWMutex
expectedPrevRandaoUpdating uint64
}
// NewRelayAPI creates a new service. if builders is nil, allow any builder
func NewRelayAPI(opts RelayAPIOpts) (api *RelayAPI, err error) {
if opts.Log == nil {
return nil, ErrMissingLogOpt
}
if opts.BeaconClient == nil {
return nil, ErrMissingBeaconClientOpt
}
if opts.Datastore == nil {
return nil, ErrMissingDatastoreOpt
}
// If block-builder API is enabled, then ensure secret key is all set
var publicKey types.PublicKey
if opts.BlockBuilderAPI {
if opts.SecretKey == nil {
return nil, ErrBuilderAPIWithoutSecretKey
}
// If using a secret key, ensure it's the correct one
publicKey, err = types.BlsPublicKeyToPublicKey(bls.PublicKeyFromSecretKey(opts.SecretKey))
if err != nil {
return nil, err
}
opts.Log.Infof("Using BLS key: %s", publicKey.String())
// ensure pubkey is same across all relay instances
_pubkey, err := opts.Redis.GetRelayConfig(datastore.RedisConfigFieldPubkey)
if err != nil {
return nil, err
} else if _pubkey == "" {
err := opts.Redis.SetRelayConfig(datastore.RedisConfigFieldPubkey, publicKey.String())
if err != nil {
return nil, err
}
} else if _pubkey != publicKey.String() {
return nil, fmt.Errorf("%w: new=%s old=%s", ErrRelayPubkeyMismatch, publicKey.String(), _pubkey)
}
}
api = &RelayAPI{
opts: opts,
log: opts.Log,
blsSk: opts.SecretKey,
publicKey: &publicKey,
datastore: opts.Datastore,
beaconClient: opts.BeaconClient,
redis: opts.Redis,
db: opts.DB,
proposerDutiesResponse: []types.BuilderGetValidatorsResponseEntry{},
blockSimRateLimiter: NewBlockSimulationRateLimiter(opts.BlockSimURL),
activeValidatorC: make(chan types.PubkeyHex, 450_000),
validatorRegC: make(chan types.SignedValidatorRegistration, 450_000),
}
if os.Getenv("FORCE_GET_HEADER_204") == "1" {
api.log.Warn("env: FORCE_GET_HEADER_204 - forcing getHeader to always return 204")
api.ffForceGetHeader204 = true
}
if os.Getenv("DISABLE_BLOCK_PUBLISHING") == "1" {
api.log.Warn("env: DISABLE_BLOCK_PUBLISHING - disabling publishing blocks on getPayload")
api.ffDisableBlockPublishing = true
}
if os.Getenv("DISABLE_LOWPRIO_BUILDERS") == "1" {
api.log.Warn("env: DISABLE_LOWPRIO_BUILDERS - allowing only high-level builders")
api.ffDisableLowPrioBuilders = true
}
return api, nil
}
func (api *RelayAPI) getRouter() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/", api.handleRoot).Methods(http.MethodGet)
// Proposer API
if api.opts.ProposerAPI {
api.log.Info("proposer API enabled")
r.HandleFunc(pathStatus, api.handleStatus).Methods(http.MethodGet)
r.HandleFunc(pathRegisterValidator, api.handleRegisterValidator).Methods(http.MethodPost)
r.HandleFunc(pathGetHeader, api.handleGetHeader).Methods(http.MethodGet)
r.HandleFunc(pathGetPayload, api.handleGetPayload).Methods(http.MethodPost)
}
// Builder API
if api.opts.BlockBuilderAPI {
api.log.Info("block builder API enabled")
r.HandleFunc(pathBuilderGetValidators, api.handleBuilderGetValidators).Methods(http.MethodGet)
r.HandleFunc(pathSubmitNewBlock, api.handleSubmitNewBlock).Methods(http.MethodPost)
}
// Data API
if api.opts.DataAPI {
api.log.Info("data API enabled")
r.HandleFunc(pathDataProposerPayloadDelivered, api.handleDataProposerPayloadDelivered).Methods(http.MethodGet)
r.HandleFunc(pathDataBuilderBidsReceived, api.handleDataBuilderBidsReceived).Methods(http.MethodGet)
r.HandleFunc(pathDataValidatorRegistration, api.handleDataValidatorRegistration).Methods(http.MethodGet)
}
// Pprof
if api.opts.PprofAPI {
api.log.Info("pprof API enabled")
r.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
}
// /internal/...
if api.opts.InternalAPI {
api.log.Info("internal API enabled")
r.HandleFunc(pathInternalBuilderStatus, api.handleInternalBuilderStatus).Methods(http.MethodGet, http.MethodPost, http.MethodPut)
}
// r.Use(mux.CORSMethodMiddleware(r))
loggedRouter := httplogger.LoggingMiddlewareLogrus(api.log, r)
withGz := gziphandler.GzipHandler(loggedRouter)
return withGz
}
// StartServer starts the HTTP server for this instance
func (api *RelayAPI) StartServer() (err error) {
if api.srvStarted.Swap(true) {
return ErrServerAlreadyStarted
}
// Get best beacon-node status by head slot, process current slot and start slot updates
bestSyncStatus, err := api.beaconClient.BestSyncStatus()
if err != nil {
return err
}
api.genesisInfo, err = api.beaconClient.GetGenesis()
if err != nil {
return err
}
api.log.Infof("genesis info: %d", api.genesisInfo.Data.GenesisTime)
// start things for the block-builder API
if api.opts.BlockBuilderAPI {
// Get current proposer duties blocking before starting, to have them ready
api.updateProposerDuties(bestSyncStatus.HeadSlot)
}
// start things specific for the proposer API
if api.opts.ProposerAPI {
// Update list of known validators, and start refresh loop
go api.startKnownValidatorUpdates()
// Start the worker pool to process active validators
api.log.Infof("starting %d active validator processors", numActiveValidatorProcessors)
for i := 0; i < numActiveValidatorProcessors; i++ {
go api.startActiveValidatorProcessor()
}
// Start the validator registration db-save processor
api.log.Infof("starting %d validator registration processors", numValidatorRegProcessors)
for i := 0; i < numValidatorRegProcessors; i++ {
go api.startValidatorRegistrationDBProcessor()
}
}
// Process current slot
api.processNewSlot(bestSyncStatus.HeadSlot)
// Start regular slot updates
go func() {
c := make(chan beaconclient.HeadEventData)
api.beaconClient.SubscribeToHeadEvents(c)
for {
headEvent := <-c
api.processNewSlot(headEvent.Slot)
}
}()
api.srv = &http.Server{
Addr: api.opts.ListenAddr,
Handler: api.getRouter(),
ReadTimeout: 1500 * time.Millisecond,
ReadHeaderTimeout: 600 * time.Millisecond,
WriteTimeout: 3 * time.Second,
IdleTimeout: 3 * time.Second,
}
err = api.srv.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
// StopServer disables sending any bids on getHeader calls, waits a few seconds to catch any remaining getPayload call, and then shuts down the webserver
func (api *RelayAPI) StopServer() (err error) {
api.log.Info("Stopping server...")
if api.opts.ProposerAPI {
// stop sending bids
api.ffForceGetHeader204 = true
api.log.Info("Disabled sending bids, waiting a few seconds...")
// wait a few seconds, for any pending getPayload call to complete
time.Sleep(5 * time.Second)
// wait for any active getPayload call to finish
api.getPayloadCallsInFlight.Wait()
}
// shutdown
return api.srv.Shutdown(context.Background())
}
// startActiveValidatorProcessor keeps listening on the channel and saving active validators to redis
func (api *RelayAPI) startActiveValidatorProcessor() {
for pubkey := range api.activeValidatorC {
err := api.redis.SetActiveValidator(pubkey)
if err != nil {
api.log.WithError(err).Infof("error setting active validator")
}
}
}
// startActiveValidatorProcessor keeps listening on the channel and saving active validators to redis
func (api *RelayAPI) startValidatorRegistrationDBProcessor() {
for valReg := range api.validatorRegC {
err := api.datastore.SaveValidatorRegistration(valReg)
if err != nil {
api.log.WithError(err).WithFields(logrus.Fields{
"reg_pubkey": valReg.Message.Pubkey,
"reg_feeRecipient": valReg.Message.FeeRecipient,
"reg_gasLimit": valReg.Message.GasLimit,
"reg_timestamp": valReg.Message.Timestamp,
}).Error("error saving validator registration")
}
}
}
func (api *RelayAPI) processNewSlot(headSlot uint64) {
_apiHeadSlot := api.headSlot.Load()
if headSlot <= _apiHeadSlot {
return
}
if _apiHeadSlot > 0 {
for s := _apiHeadSlot + 1; s < headSlot; s++ {
api.log.WithField("missedSlot", s).Warnf("missed slot: %d", s)
}
}
// store the head slot
api.headSlot.Store(headSlot)
// only for builder-api
if api.opts.BlockBuilderAPI {
// query the expected prev_randao field
go api.updatedExpectedRandao(headSlot)
// update proposer duties in the background
go api.updateProposerDuties(headSlot)
}
// log
epoch := headSlot / uint64(common.SlotsPerEpoch)
api.log.WithFields(logrus.Fields{
"epoch": epoch,
"slotHead": headSlot,
"slotStartNextEpoch": (epoch + 1) * uint64(common.SlotsPerEpoch),
}).Infof("updated headSlot to %d", headSlot)
}
func (api *RelayAPI) updateProposerDuties(headSlot uint64) {
// Ensure only one updating is running at a time
if api.isUpdatingProposerDuties.Swap(true) {
return
}
defer api.isUpdatingProposerDuties.Store(false)
// Update once every 8 slots (or more, if a slot was missed)
if headSlot%8 != 0 && headSlot-api.proposerDutiesSlot < 8 {
return
}
// Get duties from mem
duties, err := api.redis.GetProposerDuties()
dutiesMap := make(map[uint64]*types.RegisterValidatorRequestMessage)
for _, duty := range duties {
dutiesMap[duty.Slot] = duty.Entry.Message
}
if err == nil {
api.proposerDutiesLock.Lock()
api.proposerDutiesResponse = duties
api.proposerDutiesMap = dutiesMap
api.proposerDutiesSlot = headSlot
api.proposerDutiesLock.Unlock()
// pretty-print
_duties := make([]string, len(duties))
for i, duty := range duties {
_duties[i] = fmt.Sprint(duty.Slot)
}
sort.Strings(_duties)
api.log.Infof("proposer duties updated: %s", strings.Join(_duties, ", "))
} else {
api.log.WithError(err).Error("failed to update proposer duties")
}
}
func (api *RelayAPI) startKnownValidatorUpdates() {
for {
// Refresh known validators
cnt, err := api.datastore.RefreshKnownValidators()
if err != nil {
api.log.WithError(err).Error("error getting known validators")
} else {
api.log.WithField("cnt", cnt).Info("updated known validators")
}
// Wait for one epoch (at the beginning, because initially the validators have already been queried)
time.Sleep(common.DurationPerEpoch / 2)
}
}
func (api *RelayAPI) 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 {
api.log.WithField("response", resp).WithError(err).Error("Couldn't write error response")
http.Error(w, "", http.StatusInternalServerError)
}
}
func (api *RelayAPI) 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 {
api.log.WithField("response", response).WithError(err).Error("Couldn't write OK response")
http.Error(w, "", http.StatusInternalServerError)
}
}
func (api *RelayAPI) handleStatus(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
}
// ---------------
// PROPOSER APIS
// ---------------
func (api *RelayAPI) handleRoot(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "MEV-Boost Relay API")
}
func (api *RelayAPI) handleRegisterValidator(w http.ResponseWriter, req *http.Request) {
ua := req.UserAgent()
log := api.log.WithFields(logrus.Fields{
"method": "registerValidator",
"ua": ua,
"mevBoostV": common.GetMevBoostVersionFromUserAgent(ua),
})
start := time.Now()
registrationTimeUpperBound := start.Add(10 * time.Second)
numRegTotal := 0
numRegProcessed := 0
numRegActive := 0
numRegNew := 0
processingStoppedByError := false
respondError := func(code int, msg string) {
processingStoppedByError = true
log.Warnf("error: %s", msg)
api.RespondError(w, code, msg)
}
if req.ContentLength == 0 {
respondError(http.StatusBadRequest, "empty request")
return
}
body, err := io.ReadAll(req.Body)
if err != nil {
log.WithError(err).WithField("contentLength", req.ContentLength).Warn("failed to read request body")
api.RespondError(w, http.StatusBadRequest, "failed to read request body")
return
}
req.Body.Close()
parseRegistration := func(value []byte) (pkHex types.PubkeyHex, timestampInt int64, err error) {
pubkey, err := jsonparser.GetUnsafeString(value, "message", "pubkey")
if err != nil {
return pkHex, timestampInt, fmt.Errorf("registration message error (pubkey): %w", err)
}
timestamp, err := jsonparser.GetUnsafeString(value, "message", "timestamp")
if err != nil {
return pkHex, timestampInt, fmt.Errorf("registration message error (timestamp): %w", err)
}
timestampInt, err = strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return pkHex, timestampInt, fmt.Errorf("invalid timestamp: %w", err)
}
return types.PubkeyHex(pubkey), timestampInt, nil
}
// Iterate over the registrations
_, err = jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, _err error) {
numRegTotal += 1
if processingStoppedByError {
return
}
numRegProcessed += 1
// Extract immediately necessary registration fields
pkHex, timestampInt, err := parseRegistration(value)
if err != nil {
respondError(http.StatusBadRequest, err.Error())
return
}
// Add validator pubkey to logs
regLog := api.log.WithField("pubkey", pkHex.String())
// Ensure registration is not too far in the future
registrationTime := time.Unix(timestampInt, 0)
if registrationTime.After(registrationTimeUpperBound) {
respondError(http.StatusBadRequest, "timestamp too far in the future")
return
}
// Check if a real validator
isKnownValidator := api.datastore.IsKnownValidator(pkHex)
if !isKnownValidator {
respondError(http.StatusBadRequest, fmt.Sprintf("not a known validator: %s", pkHex.String()))
return
}
// Track active validators here
numRegActive += 1
select {
case api.activeValidatorC <- pkHex:
default:
regLog.Error("active validator channel full")
}
// Check for a previous registration timestamp
prevTimestamp, err := api.redis.GetValidatorRegistrationTimestamp(pkHex)
if err != nil {
regLog.WithError(err).Error("error getting last registration timestamp")
} else if prevTimestamp >= uint64(timestampInt) {
// abort if the current registration timestamp is older or equal to the last known one
return
}
// Now we have a new registration to process
numRegNew += 1
// JSON-decode the registration now (needed for signature verification)
signedValidatorRegistration := new(types.SignedValidatorRegistration)
err = json.Unmarshal(value, signedValidatorRegistration)
if err != nil {
regLog.WithError(err).Error("error unmarshalling signed validator registration")
respondError(http.StatusBadRequest, fmt.Sprintf("error unmarshalling signed validator registration: %s", err.Error()))
return
}
// Verify the signature
ok, err := types.VerifySignature(signedValidatorRegistration.Message, api.opts.EthNetDetails.DomainBuilder, signedValidatorRegistration.Message.Pubkey[:], signedValidatorRegistration.Signature[:])
if err != nil {
regLog.WithError(err).Error("error verifying registerValidator signature")
respondError(http.StatusBadRequest, fmt.Sprintf("error verifying registerValidator signature: %s", err.Error()))
return
} else if !ok {
api.RespondError(w, http.StatusBadRequest, fmt.Sprintf("failed to verify validator signature for %s", signedValidatorRegistration.Message.Pubkey.String()))
return
}
// Save to database
select {
case api.validatorRegC <- *signedValidatorRegistration:
default:
regLog.Error("validator registration channel full")
}
})
if err != nil {
respondError(http.StatusBadRequest, "error in traversing json")
return
}
log = log.WithFields(logrus.Fields{
"timeNeededSec": time.Since(start).Seconds(),
"numRegistrations": numRegTotal,
"numRegistrationsActive": numRegActive,
"numRegistrationsProcessed": numRegProcessed,
"numRegistrationsNew": numRegNew,
"processingStoppedByError": processingStoppedByError,
})
log.Info("validator registrations call processed")
w.WriteHeader(http.StatusOK)
}
func (api *RelayAPI) handleGetHeader(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
slotStr := vars["slot"]
parentHashHex := vars["parent_hash"]
proposerPubkeyHex := vars["pubkey"]
ua := req.UserAgent()
log := api.log.WithFields(logrus.Fields{
"method": "getHeader",
"slot": slotStr,
"parentHash": parentHashHex,
"pubkey": proposerPubkeyHex,
"ua": ua,
"mevBoostV": common.GetMevBoostVersionFromUserAgent(ua),
})
slot, err := strconv.ParseUint(slotStr, 10, 64)
if err != nil {
api.RespondError(w, http.StatusBadRequest, common.ErrInvalidSlot.Error())
return
}
if len(proposerPubkeyHex) != 98 {
api.RespondError(w, http.StatusBadRequest, common.ErrInvalidPubkey.Error())
return
}
if len(parentHashHex) != 66 {
api.RespondError(w, http.StatusBadRequest, common.ErrInvalidHash.Error())
return
}
if slot < api.headSlot.Load() {
api.RespondError(w, http.StatusBadRequest, "slot is too old")
return
}
log.Debug("getHeader request received")
if api.ffForceGetHeader204 {
log.Info("forced getHeader 204 response")
w.WriteHeader(http.StatusNoContent)
return
}
bid, err := api.redis.GetBestBid(slot, parentHashHex, proposerPubkeyHex)
if err != nil {
log.WithError(err).Error("could not get bid")
api.RespondError(w, http.StatusBadRequest, err.Error())
return
}
if bid == nil || bid.Data == nil || bid.Data.Message == nil {
w.WriteHeader(http.StatusNoContent)
return
}
// Error on bid without value
if bid.Data.Message.Value.Cmp(&ZeroU256) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
log.WithFields(logrus.Fields{
"value": bid.Data.Message.Value.String(),
"blockHash": bid.Data.Message.Header.BlockHash.String(),
}).Info("bid delivered")
api.RespondOK(w, bid)
}
func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request) {
api.getPayloadCallsInFlight.Add(1)
defer api.getPayloadCallsInFlight.Done()
ua := req.UserAgent()
log := api.log.WithFields(logrus.Fields{
"method": "getPayload",
"ua": ua,
"mevBoostV": common.GetMevBoostVersionFromUserAgent(ua),
"contentLength": req.ContentLength,
})
payload := new(types.SignedBlindedBeaconBlock)
if err := json.NewDecoder(req.Body).Decode(payload); err != nil {
if strings.Contains(err.Error(), "i/o timeout") {
log.WithError(err).Error("getPayload request failed to decode (i/o timeout)")
} else {
log.WithError(err).Warn("getPayload request failed to decode")
}
api.RespondError(w, http.StatusBadRequest, err.Error())
return
}
slot := payload.Message.Slot
blockHash := payload.Message.Body.ExecutionPayloadHeader.BlockHash
log = log.WithFields(logrus.Fields{
"slot": slot,
"blockHash": blockHash.String(),
"idArg": req.URL.Query().Get("id"),
})
log.Debug("getPayload request received")
proposerPubkey, found := api.datastore.GetKnownValidatorPubkeyByIndex(payload.Message.ProposerIndex)
if !found {
log.Errorf("could not find proposer pubkey for index %d", payload.Message.ProposerIndex)
api.RespondError(w, http.StatusBadRequest, "could not match proposer index to pubkey")
return
}
log = log.WithField("pubkeyFromIndex", proposerPubkey)
// Get the proposer pubkey based on the validator index from the payload
pk, err := types.HexToPubkey(proposerPubkey.String())
if err != nil {
log.WithError(err).Warn("could not convert pubkey to types.PublicKey")
api.RespondError(w, http.StatusBadRequest, "could not convert pubkey to types.PublicKey")
return
}
// Verify the signature
ok, err := types.VerifySignature(payload.Message, api.opts.EthNetDetails.DomainBeaconProposer, pk[:], payload.Signature[:])
if !ok || err != nil {
log.WithError(err).Warn("could not verify payload signature")
api.RespondError(w, http.StatusBadRequest, "could not verify payload signature")
return
}
// Get the response - from memory, Redis or DB
// note that mev-boost might send getPayload for bids of other relays, thus this code wouldn't find anything
getPayloadResp, err := api.datastore.GetGetPayloadResponse(slot, proposerPubkey.String(), blockHash.String())
if err != nil || getPayloadResp == nil {
log.WithError(err).Warn("failed getting execution payload (1/2)")
time.Sleep(time.Duration(timeoutGetPayloadRetryMs) * time.Millisecond)
// Try again
getPayloadResp, err = api.datastore.GetGetPayloadResponse(slot, proposerPubkey.String(), blockHash.String())
if err != nil {
log.WithError(err).Error("failed getting execution payload (2/2) - due to error")
api.RespondError(w, http.StatusBadRequest, err.Error())
return
} else if getPayloadResp == nil {
log.Warn("failed getting execution payload (2/2)")
api.RespondError(w, http.StatusBadRequest, "no execution payload for this request")
return
}
}
api.RespondOK(w, getPayloadResp)
log = log.WithFields(logrus.Fields{
"numTx": len(getPayloadResp.Data.Transactions),
"blockNumber": payload.Message.Body.ExecutionPayloadHeader.BlockNumber,
})
log.Info("execution payload delivered")
// Save information about delivered payload
go func() {
err = api.redis.SetStats(datastore.RedisStatsFieldSlotLastPayloadDelivered, slot)
if err != nil {
log.WithError(err).Error("failed to save delivered payload slot to redis")
}
bidTrace, err := api.redis.GetBidTrace(slot, proposerPubkey.String(), blockHash.String())
if err != nil {
log.WithError(err).Error("failed to get bidTrace for delivered payload from redis")
}
err = api.db.SaveDeliveredPayload(bidTrace, payload)
if err != nil {
log.WithError(err).WithFields(logrus.Fields{
"bidTrace": bidTrace,
"payload": payload,
}).Error("failed to save delivered payload")
}
// Increment builder stats
err = api.db.IncBlockBuilderStatsAfterGetPayload(bidTrace.BuilderPubkey.String())
if err != nil {
log.WithError(err).Error("failed to increment builder-stats after getPayload")
}
}()
// Publish the signed beacon block via beacon-node
go func() {
if api.ffDisableBlockPublishing {
log.Info("publishing the block is disabled")
return
}
signedBeaconBlock := SignedBlindedBeaconBlockToBeaconBlock(payload, getPayloadResp.Data)
_, _ = api.beaconClient.PublishBlock(signedBeaconBlock) // errors are logged inside
}()
}
// --------------------
// BLOCK BUILDER APIS
// --------------------
// updatedExpectedRandao updates the prev_randao field we expect from builder block submissions
func (api *RelayAPI) updatedExpectedRandao(slot uint64) {
api.log.Infof("updating randao for %d ...", slot)
api.expectedPrevRandaoLock.Lock()
latestKnownSlot := api.expectedPrevRandao.slot
if slot < latestKnownSlot || slot <= api.expectedPrevRandaoUpdating { // do nothing slot is already known or currently being updated
api.log.Debugf("- abort updating randao - slot %d, latest: %d, updating: %d", slot, latestKnownSlot, api.expectedPrevRandaoUpdating)
api.expectedPrevRandaoLock.Unlock()
return
}
api.expectedPrevRandaoUpdating = slot
api.expectedPrevRandaoLock.Unlock()
// get randao from BN
api.log.Debugf("- querying BN for randao for slot %d", slot)
randao, err := api.beaconClient.GetRandao(slot)
if err != nil {
api.log.WithField("slot", slot).WithError(err).Warn("failed to get randao from beacon node")
api.expectedPrevRandaoLock.Lock()
api.expectedPrevRandaoUpdating = 0
api.expectedPrevRandaoLock.Unlock()
return
}
// after request, check if still the latest, then update
api.expectedPrevRandaoLock.Lock()
defer api.expectedPrevRandaoLock.Unlock()
targetSlot := slot + 1
api.log.Debugf("- after BN randao: slot %d, targetSlot: %d latest: %d", slot, targetSlot, api.expectedPrevRandao.slot)
// update if still the latest
if targetSlot >= api.expectedPrevRandao.slot {
api.expectedPrevRandao = randaoHelper{
slot: targetSlot, // the retrieved prev_randao is for the next slot
prevRandao: randao.Data.Randao,
}
api.log.WithField("slot", slot).Infof("updated expected prev_randao to %s for slot %d", randao.Data.Randao, targetSlot)
}
}
func (api *RelayAPI) handleBuilderGetValidators(w http.ResponseWriter, req *http.Request) {
api.proposerDutiesLock.RLock()
defer api.proposerDutiesLock.RUnlock()
api.RespondOK(w, api.proposerDutiesResponse)
}
func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Request) {
receivedAt := time.Now().UTC()
log := api.log.WithFields(logrus.Fields{
"method": "submitNewBlock",
"contentLength": req.ContentLength,
})
var err error
var r io.Reader = req.Body
if req.Header.Get("Content-Encoding") == "gzip" {
r, err = gzip.NewReader(req.Body)
if err != nil {
log.WithError(err).Warn("could not create gzip reader")
api.RespondError(w, http.StatusBadRequest, err.Error())
return
}
log = log.WithField("gzip-req", true)
}
payload := new(types.BuilderSubmitBlockRequest)
if err := json.NewDecoder(r).Decode(payload); err != nil {
log.WithError(err).Warn("could not decode payload")
api.RespondError(w, http.StatusBadRequest, err.Error())
return
}
if payload.Message == nil || payload.ExecutionPayload == nil {
api.RespondError(w, http.StatusBadRequest, "missing parts of the payload")
return
}
log = log.WithFields(logrus.Fields{
"slot": payload.Message.Slot,
"builderPubkey": payload.Message.BuilderPubkey.String(),
"blockHash": payload.Message.BlockHash.String(),
})
// Reject new submissions once the payload for this slot was delivered
slotStr, err := api.redis.GetStats(datastore.RedisStatsFieldSlotLastPayloadDelivered)
if err != nil && !errors.Is(err, redis.Nil) {
log.WithError(err).Error("failed to get delivered payload slot from redis")
} else {
slotLastPayloadDelivered, err := strconv.ParseUint(slotStr, 10, 64)
if err != nil {
log.WithError(err).Errorf("failed to parse delivered payload slot from redis: %s", slotStr)
} else if payload.Message.Slot <= slotLastPayloadDelivered {
log.Info("rejecting submission because payload for this slot was already delivered")
api.RespondError(w, http.StatusBadRequest, "payload for this slot was already delivered")
return
}
}
builderIsHighPrio, builderIsBlacklisted, err := api.redis.GetBlockBuilderStatus(payload.Message.BuilderPubkey.String())
log = log.WithFields(logrus.Fields{
"builderIsHighPrio": builderIsHighPrio,
"builderIsBlacklisted": builderIsBlacklisted,
})
if err != nil {
log.WithError(err).Error("could not get block builder status")
}
// Timestamp check
expectedTimestamp := api.genesisInfo.Data.GenesisTime + (payload.Message.Slot * 12)
if payload.ExecutionPayload.Timestamp != expectedTimestamp {
log.Warnf("incorrect timestamp. got %d, expected %d", payload.ExecutionPayload.Timestamp, expectedTimestamp)
api.RespondError(w, http.StatusBadRequest, fmt.Sprintf("incorrect timestamp. got %d, expected %d", payload.ExecutionPayload.Timestamp, expectedTimestamp))
return
}
// randao check 1:
// - querying the randao from the BN if payload has a newer slot (might be faster than headSlot event)
// - check for validity happens later, again after validation (to use some time for BN request to finish...)
api.expectedPrevRandaoLock.RLock()
if payload.Message.Slot > api.expectedPrevRandao.slot {
go api.updatedExpectedRandao(payload.Message.Slot - 1)
}
api.expectedPrevRandaoLock.RUnlock()
// ensure correct feeRecipient is used
api.proposerDutiesLock.RLock()
slotDuty := api.proposerDutiesMap[payload.Message.Slot]
api.proposerDutiesLock.RUnlock()
if slotDuty == nil {
log.Warn("could not find slot duty")
api.RespondError(w, http.StatusBadRequest, "could not find slot duty")
return
} else if slotDuty.FeeRecipient != payload.Message.ProposerFeeRecipient {
log.Info("fee recipient does not match")
api.RespondError(w, http.StatusBadRequest, "fee recipient does not match")
return
}
if builderIsBlacklisted {
log.Info("builder is blacklisted")
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
return
}
// In case only high-prio requests are accepted, fail others
if api.ffDisableLowPrioBuilders && !builderIsHighPrio {
log.Info("rejecting low-prio builder (ff-disable-low-prio-builders)")
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
return