generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathredis.go
852 lines (721 loc) · 32.3 KB
/
redis.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
package datastore
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"time"
builderApi "github.com/attestantio/go-builder-client/api"
builderApiDeneb "github.com/attestantio/go-builder-client/api/deneb"
builderSpec "github.com/attestantio/go-builder-client/spec"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/flashbots/go-utils/cli"
"github.com/flashbots/mev-boost-relay/common"
"github.com/redis/go-redis/v9"
)
var (
redisScheme = "redis://"
redisPrefix = "boost-relay"
expiryBidCache = 45 * time.Second
RedisConfigFieldPubkey = "pubkey"
RedisStatsFieldLatestSlot = "latest-slot"
RedisStatsFieldValidatorsTotal = "validators-total"
ErrFailedUpdatingTopBidNoBids = errors.New("failed to update top bid because no bids were found")
ErrAnotherPayloadAlreadyDeliveredForSlot = errors.New("another payload block hash for slot was already delivered")
ErrPastSlotAlreadyDelivered = errors.New("payload for past slot was already delivered")
// Docs about redis settings: https://redis.io/docs/reference/clients/
redisConnectionPoolSize = cli.GetEnvInt("REDIS_CONNECTION_POOL_SIZE", 0) // 0 means use default (10 per CPU)
redisMinIdleConnections = cli.GetEnvInt("REDIS_MIN_IDLE_CONNECTIONS", 0) // 0 means use default
redisReadTimeoutSec = cli.GetEnvInt("REDIS_READ_TIMEOUT_SEC", 0) // 0 means use default (3 sec)
redisPoolTimeoutSec = cli.GetEnvInt("REDIS_POOL_TIMEOUT_SEC", 0) // 0 means use default (ReadTimeout + 1 sec)
redisWriteTimeoutSec = cli.GetEnvInt("REDIS_WRITE_TIMEOUT_SEC", 0) // 0 means use default (3 seconds)
)
func connectRedis(redisURI string) (*redis.Client, error) {
// Handle both URIs and full URLs, assume unencrypted connections
if !strings.HasPrefix(redisURI, redisScheme) && !strings.HasPrefix(redisURI, "rediss://") {
redisURI = redisScheme + redisURI
}
redisOpts, err := redis.ParseURL(redisURI)
if err != nil {
return nil, err
}
if redisConnectionPoolSize > 0 {
redisOpts.PoolSize = redisConnectionPoolSize
}
if redisMinIdleConnections > 0 {
redisOpts.MinIdleConns = redisMinIdleConnections
}
if redisReadTimeoutSec > 0 {
redisOpts.ReadTimeout = time.Duration(redisReadTimeoutSec) * time.Second
}
if redisPoolTimeoutSec > 0 {
redisOpts.PoolTimeout = time.Duration(redisPoolTimeoutSec) * time.Second
}
if redisWriteTimeoutSec > 0 {
redisOpts.WriteTimeout = time.Duration(redisWriteTimeoutSec) * time.Second
}
redisClient := redis.NewClient(redisOpts)
if _, err := redisClient.Ping(context.Background()).Result(); err != nil {
// unable to connect to redis
return nil, err
}
return redisClient, nil
}
type RedisCache struct {
client *redis.Client
readonlyClient *redis.Client
// prefixes (keys generated with a function)
prefixGetHeaderResponse string
prefixExecPayloadCapella string
prefixPayloadContentsDeneb string
prefixPayloadContentsElectra string
prefixBidTrace string
prefixBlockBuilderLatestBids string // latest bid for a given slot
prefixBlockBuilderLatestBidsValue string // value of latest bid for a given slot
prefixBlockBuilderLatestBidsTime string // when the request was received, to avoid older requests overwriting newer ones after a slot validation
prefixTopBidValue string
prefixFloorBid string
prefixFloorBidValue string
// keys
keyValidatorRegistrationTimestamp string
keyRelayConfig string
keyStats string
keyProposerDuties string
keyBlockBuilderStatus string
keyLastSlotDelivered string
keyLastHashDelivered string
}
func NewRedisCache(prefix, redisURI, readonlyURI string) (*RedisCache, error) {
client, err := connectRedis(redisURI)
if err != nil {
return nil, err
}
roClient := client
if readonlyURI != "" {
roClient, err = connectRedis(readonlyURI)
if err != nil {
return nil, err
}
}
return &RedisCache{
client: client,
readonlyClient: roClient,
prefixGetHeaderResponse: fmt.Sprintf("%s/%s:cache-gethead-response", redisPrefix, prefix),
prefixExecPayloadCapella: fmt.Sprintf("%s/%s:cache-execpayload-capella", redisPrefix, prefix),
prefixPayloadContentsDeneb: fmt.Sprintf("%s/%s:cache-payloadcontents-deneb", redisPrefix, prefix),
prefixPayloadContentsElectra: fmt.Sprintf("%s/%s:cache-payloadcontents-electra", redisPrefix, prefix),
prefixBidTrace: fmt.Sprintf("%s/%s:cache-bid-trace", redisPrefix, prefix),
prefixBlockBuilderLatestBids: fmt.Sprintf("%s/%s:block-builder-latest-bid", redisPrefix, prefix), // hashmap for slot+parentHash+proposerPubkey with builderPubkey as field
prefixBlockBuilderLatestBidsValue: fmt.Sprintf("%s/%s:block-builder-latest-bid-value", redisPrefix, prefix), // hashmap for slot+parentHash+proposerPubkey with builderPubkey as field
prefixBlockBuilderLatestBidsTime: fmt.Sprintf("%s/%s:block-builder-latest-bid-time", redisPrefix, prefix), // hashmap for slot+parentHash+proposerPubkey with builderPubkey as field
prefixTopBidValue: fmt.Sprintf("%s/%s:top-bid-value", redisPrefix, prefix), // prefix:slot_parentHash_proposerPubkey
prefixFloorBid: fmt.Sprintf("%s/%s:bid-floor", redisPrefix, prefix), // prefix:slot_parentHash_proposerPubkey
prefixFloorBidValue: fmt.Sprintf("%s/%s:bid-floor-value", redisPrefix, prefix), // prefix:slot_parentHash_proposerPubkey
keyValidatorRegistrationTimestamp: fmt.Sprintf("%s/%s:validator-registration-timestamp", redisPrefix, prefix),
keyRelayConfig: fmt.Sprintf("%s/%s:relay-config", redisPrefix, prefix),
keyStats: fmt.Sprintf("%s/%s:stats", redisPrefix, prefix),
keyProposerDuties: fmt.Sprintf("%s/%s:proposer-duties", redisPrefix, prefix),
keyBlockBuilderStatus: fmt.Sprintf("%s/%s:block-builder-status", redisPrefix, prefix),
keyLastSlotDelivered: fmt.Sprintf("%s/%s:last-slot-delivered", redisPrefix, prefix),
keyLastHashDelivered: fmt.Sprintf("%s/%s:last-hash-delivered", redisPrefix, prefix),
}, nil
}
func (r *RedisCache) keyCacheGetHeaderResponse(slot uint64, parentHash, proposerPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixGetHeaderResponse, slot, parentHash, proposerPubkey)
}
func (r *RedisCache) keyExecPayloadCapella(slot uint64, proposerPubkey, blockHash string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixExecPayloadCapella, slot, proposerPubkey, blockHash)
}
func (r *RedisCache) keyPayloadContentsDeneb(slot uint64, proposerPubkey, blockHash string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixPayloadContentsDeneb, slot, proposerPubkey, blockHash)
}
func (r *RedisCache) keyPayloadContentsElectra(slot uint64, proposerPubkey, blockHash string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixPayloadContentsElectra, slot, proposerPubkey, blockHash)
}
func (r *RedisCache) keyCacheBidTrace(slot uint64, proposerPubkey, blockHash string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixBidTrace, slot, proposerPubkey, blockHash)
}
// keyLatestBidByBuilder returns the key for the getHeader response the latest bid by a specific builder
func (r *RedisCache) keyLatestBidByBuilder(slot uint64, parentHash, proposerPubkey, builderPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s/%s", r.prefixBlockBuilderLatestBids, slot, parentHash, proposerPubkey, builderPubkey)
}
// keyBlockBuilderLatestBidValue returns the hashmap key for the value of the latest bid by a specific builder
func (r *RedisCache) keyBlockBuilderLatestBidsValue(slot uint64, parentHash, proposerPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixBlockBuilderLatestBidsValue, slot, parentHash, proposerPubkey)
}
// keyBlockBuilderLatestBidValue returns the hashmap key for the time of the latest bid by a specific builder
func (r *RedisCache) keyBlockBuilderLatestBidsTime(slot uint64, parentHash, proposerPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixBlockBuilderLatestBidsTime, slot, parentHash, proposerPubkey)
}
// keyTopBidValue returns the hashmap key for the time of the latest bid by a specific builder
func (r *RedisCache) keyTopBidValue(slot uint64, parentHash, proposerPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixTopBidValue, slot, parentHash, proposerPubkey)
}
// keyFloorBid returns the key for the highest non-cancellable bid of a given slot+parentHash+proposerPubkey
func (r *RedisCache) keyFloorBid(slot uint64, parentHash, proposerPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixFloorBid, slot, parentHash, proposerPubkey)
}
// keyFloorBidValue returns the key for the highest non-cancellable value of a given slot+parentHash+proposerPubkey
func (r *RedisCache) keyFloorBidValue(slot uint64, parentHash, proposerPubkey string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixFloorBidValue, slot, parentHash, proposerPubkey)
}
func (r *RedisCache) GetObj(key string, obj any) (err error) {
value, err := r.client.Get(context.Background(), key).Result()
if err != nil {
return err
}
return json.Unmarshal([]byte(value), &obj)
}
func (r *RedisCache) SetObj(key string, value any, expiration time.Duration) (err error) {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
return r.client.Set(context.Background(), key, marshalledValue, expiration).Err()
}
// SetObjPipelined saves an object in the given Redis key on a Redis pipeline (JSON encoded)
func (r *RedisCache) SetObjPipelined(ctx context.Context, pipeliner redis.Pipeliner, key string, value any, expiration time.Duration) (err error) {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
return pipeliner.Set(ctx, key, marshalledValue, expiration).Err()
}
func (r *RedisCache) HSetObj(key, field string, value any, expiration time.Duration) (err error) {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
err = r.client.HSet(context.Background(), key, field, marshalledValue).Err()
if err != nil {
return err
}
return r.client.Expire(context.Background(), key, expiration).Err()
}
func (r *RedisCache) GetValidatorRegistrationTimestamp(proposerPubkey common.PubkeyHex) (uint64, error) {
timestamp, err := r.client.HGet(context.Background(), r.keyValidatorRegistrationTimestamp, strings.ToLower(proposerPubkey.String())).Uint64()
if errors.Is(err, redis.Nil) {
return 0, nil
}
return timestamp, err
}
func (r *RedisCache) SetValidatorRegistrationTimestampIfNewer(proposerPubkey common.PubkeyHex, timestamp uint64) error {
knownTimestamp, err := r.GetValidatorRegistrationTimestamp(proposerPubkey)
if err != nil {
return err
}
if knownTimestamp >= timestamp {
return nil
}
return r.SetValidatorRegistrationTimestamp(proposerPubkey, timestamp)
}
func (r *RedisCache) SetValidatorRegistrationTimestamp(proposerPubkey common.PubkeyHex, timestamp uint64) error {
return r.client.HSet(context.Background(), r.keyValidatorRegistrationTimestamp, proposerPubkey.String(), timestamp).Err()
}
func (r *RedisCache) CheckAndSetLastSlotAndHashDelivered(slot uint64, hash string) (err error) {
// More details about Redis optimistic locking:
// - https://redis.uptrace.dev/guide/go-redis-pipelines.html#transactions
// - https://github.com/redis/go-redis/blob/6ecbcf6c90919350c42181ce34c1cbdfbd5d1463/race_test.go#L183
txf := func(tx *redis.Tx) error {
lastSlotDelivered, err := tx.Get(context.Background(), r.keyLastSlotDelivered).Uint64()
if err != nil && !errors.Is(err, redis.Nil) {
return err
}
// slot in the past, reject request
if slot < lastSlotDelivered {
return ErrPastSlotAlreadyDelivered
}
// current slot, reject request if hash is different
if slot == lastSlotDelivered {
lastHashDelivered, err := tx.Get(context.Background(), r.keyLastHashDelivered).Result()
if err != nil && !errors.Is(err, redis.Nil) {
return err
}
if hash != lastHashDelivered {
return ErrAnotherPayloadAlreadyDeliveredForSlot
}
return nil
}
_, err = tx.TxPipelined(context.Background(), func(pipe redis.Pipeliner) error {
pipe.Set(context.Background(), r.keyLastSlotDelivered, slot, 0)
pipe.Set(context.Background(), r.keyLastHashDelivered, hash, 0)
return nil
})
return err
}
return r.client.Watch(context.Background(), txf, r.keyLastSlotDelivered, r.keyLastHashDelivered)
}
func (r *RedisCache) GetLastSlotDelivered(ctx context.Context, pipeliner redis.Pipeliner) (slot uint64, err error) {
c := pipeliner.Get(ctx, r.keyLastSlotDelivered)
_, err = pipeliner.Exec(ctx)
if err != nil {
return 0, err
}
return c.Uint64()
}
func (r *RedisCache) GetLastHashDelivered() (hash string, err error) {
return r.client.Get(context.Background(), r.keyLastHashDelivered).Result()
}
func (r *RedisCache) SetStats(field string, value any) (err error) {
return r.client.HSet(context.Background(), r.keyStats, field, value).Err()
}
func (r *RedisCache) GetStats(field string) (value string, err error) {
return r.client.HGet(context.Background(), r.keyStats, field).Result()
}
// GetStatsUint64 returns (valueUint64, nil), or (0, redis.Nil) if the field does not exist
func (r *RedisCache) GetStatsUint64(field string) (value uint64, err error) {
valStr, err := r.client.HGet(context.Background(), r.keyStats, field).Result()
if err != nil {
return 0, err
}
value, err = strconv.ParseUint(valStr, 10, 64)
return value, err
}
func (r *RedisCache) SetProposerDuties(proposerDuties []common.BuilderGetValidatorsResponseEntry) (err error) {
return r.SetObj(r.keyProposerDuties, proposerDuties, 0)
}
func (r *RedisCache) GetProposerDuties() (proposerDuties []common.BuilderGetValidatorsResponseEntry, err error) {
proposerDuties = make([]common.BuilderGetValidatorsResponseEntry, 0)
err = r.GetObj(r.keyProposerDuties, &proposerDuties)
if errors.Is(err, redis.Nil) {
return proposerDuties, nil
}
return proposerDuties, err
}
func (r *RedisCache) SetRelayConfig(field, value string) (err error) {
return r.client.HSet(context.Background(), r.keyRelayConfig, field, value).Err()
}
func (r *RedisCache) GetRelayConfig(field string) (string, error) {
res, err := r.client.HGet(context.Background(), r.keyRelayConfig, field).Result()
if errors.Is(err, redis.Nil) {
return res, nil
}
return res, err
}
func (r *RedisCache) GetBestBid(slot uint64, parentHash, proposerPubkey string) (*builderSpec.VersionedSignedBuilderBid, error) {
key := r.keyCacheGetHeaderResponse(slot, parentHash, proposerPubkey)
resp := new(builderSpec.VersionedSignedBuilderBid)
err := r.GetObj(key, resp)
if errors.Is(err, redis.Nil) {
return nil, nil
}
return resp, err
}
func (r *RedisCache) GetPayloadContents(slot uint64, proposerPubkey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) {
resp, err := r.GetPayloadContentsElectra(slot, proposerPubkey, blockHash)
if errors.Is(err, redis.Nil) {
resp, err = r.GetPayloadContentsDeneb(slot, proposerPubkey, blockHash)
if errors.Is(err, redis.Nil) {
return r.GetExecutionPayloadCapella(slot, proposerPubkey, blockHash)
}
}
return resp, err
}
func (r *RedisCache) SavePayloadContentsElectra(ctx context.Context, tx redis.Pipeliner, slot uint64, proposerPubkey, blockHash string, execPayload *builderApiDeneb.ExecutionPayloadAndBlobsBundle) (err error) {
key := r.keyPayloadContentsElectra(slot, proposerPubkey, blockHash)
b, err := execPayload.MarshalSSZ()
if err != nil {
return err
}
return tx.Set(ctx, key, b, expiryBidCache).Err()
}
func (r *RedisCache) GetPayloadContentsElectra(slot uint64, proposerPubkey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) {
electraPayloadContents := new(builderApiDeneb.ExecutionPayloadAndBlobsBundle)
key := r.keyPayloadContentsElectra(slot, proposerPubkey, blockHash)
val, err := r.client.Get(context.Background(), key).Result()
if err != nil {
return nil, err
}
err = electraPayloadContents.UnmarshalSSZ([]byte(val))
if err != nil {
return nil, err
}
return &builderApi.VersionedSubmitBlindedBlockResponse{
Version: spec.DataVersionElectra,
Electra: electraPayloadContents,
}, nil
}
func (r *RedisCache) SavePayloadContentsDeneb(ctx context.Context, tx redis.Pipeliner, slot uint64, proposerPubkey, blockHash string, execPayload *builderApiDeneb.ExecutionPayloadAndBlobsBundle) (err error) {
key := r.keyPayloadContentsDeneb(slot, proposerPubkey, blockHash)
b, err := execPayload.MarshalSSZ()
if err != nil {
return err
}
return tx.Set(ctx, key, b, expiryBidCache).Err()
}
func (r *RedisCache) GetPayloadContentsDeneb(slot uint64, proposerPubkey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) {
denebPayloadContents := new(builderApiDeneb.ExecutionPayloadAndBlobsBundle)
key := r.keyPayloadContentsDeneb(slot, proposerPubkey, blockHash)
val, err := r.client.Get(context.Background(), key).Result()
if err != nil {
return nil, err
}
err = denebPayloadContents.UnmarshalSSZ([]byte(val))
if err != nil {
return nil, err
}
return &builderApi.VersionedSubmitBlindedBlockResponse{
Version: spec.DataVersionDeneb,
Deneb: denebPayloadContents,
}, nil
}
func (r *RedisCache) SaveExecutionPayloadCapella(ctx context.Context, pipeliner redis.Pipeliner, slot uint64, proposerPubkey, blockHash string, execPayload *capella.ExecutionPayload) (err error) {
key := r.keyExecPayloadCapella(slot, proposerPubkey, blockHash)
b, err := execPayload.MarshalSSZ()
if err != nil {
return err
}
return pipeliner.Set(ctx, key, b, expiryBidCache).Err()
}
func (r *RedisCache) GetExecutionPayloadCapella(slot uint64, proposerPubkey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) {
capellaPayload := new(capella.ExecutionPayload)
key := r.keyExecPayloadCapella(slot, proposerPubkey, blockHash)
val, err := r.client.Get(context.Background(), key).Result()
if err != nil {
return nil, err
}
err = capellaPayload.UnmarshalSSZ([]byte(val))
if err != nil {
return nil, err
}
return &builderApi.VersionedSubmitBlindedBlockResponse{
Version: spec.DataVersionCapella,
Capella: capellaPayload,
}, nil
}
func (r *RedisCache) SaveBidTrace(ctx context.Context, pipeliner redis.Pipeliner, trace *common.BidTraceV2WithBlobFields) (err error) {
key := r.keyCacheBidTrace(trace.Slot, trace.ProposerPubkey.String(), trace.BlockHash.String())
return r.SetObjPipelined(ctx, pipeliner, key, trace, expiryBidCache)
}
// GetBidTrace returns (trace, nil), or (nil, redis.Nil) if the trace does not exist
func (r *RedisCache) GetBidTrace(slot uint64, proposerPubkey, blockHash string) (*common.BidTraceV2WithBlobFields, error) {
key := r.keyCacheBidTrace(slot, proposerPubkey, blockHash)
resp := new(common.BidTraceV2WithBlobFields)
err := r.GetObj(key, resp)
return resp, err
}
func (r *RedisCache) GetBuilderLatestPayloadReceivedAt(ctx context.Context, pipeliner redis.Pipeliner, slot uint64, builderPubkey, parentHash, proposerPubkey string) (int64, error) {
keyLatestBidsTime := r.keyBlockBuilderLatestBidsTime(slot, parentHash, proposerPubkey)
c := pipeliner.HGet(context.Background(), keyLatestBidsTime, builderPubkey)
_, err := pipeliner.Exec(ctx)
if errors.Is(err, redis.Nil) {
return 0, nil
} else if err != nil {
return 0, err
}
return c.Int64()
}
// SaveBuilderBid saves the latest bid by a specific builder. TODO: use transaction to make these writes atomic
func (r *RedisCache) SaveBuilderBid(ctx context.Context, pipeliner redis.Pipeliner, slot uint64, parentHash, proposerPubkey, builderPubkey string, receivedAt time.Time, headerResp *builderSpec.VersionedSignedBuilderBid) (err error) {
// save the actual bid
keyLatestBid := r.keyLatestBidByBuilder(slot, parentHash, proposerPubkey, builderPubkey)
err = r.SetObjPipelined(ctx, pipeliner, keyLatestBid, headerResp, expiryBidCache)
if err != nil {
return err
}
// set the time of the request
keyLatestBidsTime := r.keyBlockBuilderLatestBidsTime(slot, parentHash, proposerPubkey)
err = pipeliner.HSet(ctx, keyLatestBidsTime, builderPubkey, receivedAt.UnixMilli()).Err()
if err != nil {
return err
}
err = pipeliner.Expire(ctx, keyLatestBidsTime, expiryBidCache).Err()
if err != nil {
return err
}
// set the value last, because that's iterated over when updating the best bid, and the payload has to be available
keyLatestBidsValue := r.keyBlockBuilderLatestBidsValue(slot, parentHash, proposerPubkey)
value, err := headerResp.Value()
if err != nil {
return err
}
err = pipeliner.HSet(ctx, keyLatestBidsValue, builderPubkey, value.ToBig().String()).Err()
if err != nil {
return err
}
return pipeliner.Expire(ctx, keyLatestBidsValue, expiryBidCache).Err()
}
type SaveBidAndUpdateTopBidResponse struct {
WasBidSaved bool // Whether this bid was saved
WasTopBidUpdated bool // Whether the top bid was updated
IsNewTopBid bool // Whether the submitted bid became the new top bid
TopBidValue *big.Int
PrevTopBidValue *big.Int
TimePrep time.Duration
TimeSavePayload time.Duration
TimeSaveBid time.Duration
TimeSaveTrace time.Duration
TimeUpdateTopBid time.Duration
TimeUpdateFloor time.Duration
}
func (r *RedisCache) SaveBidAndUpdateTopBid(ctx context.Context, pipeliner redis.Pipeliner, trace *common.BidTraceV2WithBlobFields, payload *common.VersionedSubmitBlockRequest, getPayloadResponse *builderApi.VersionedSubmitBlindedBlockResponse, getHeaderResponse *builderSpec.VersionedSignedBuilderBid, reqReceivedAt time.Time, isCancellationEnabled bool, floorValue *big.Int) (state SaveBidAndUpdateTopBidResponse, err error) {
var prevTime, nextTime time.Time
prevTime = time.Now()
submission, err := common.GetBlockSubmissionInfo(payload)
if err != nil {
return state, err
}
// Load latest bids for a given slot+parent+proposer
builderBids, err := NewBuilderBidsFromRedis(ctx, r, pipeliner, submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String())
if err != nil {
return state, err
}
// Load floor value (if not passed in already)
if floorValue == nil {
floorValue, err = r.GetFloorBidValue(ctx, pipeliner, submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String())
if err != nil {
return state, err
}
}
// Get the reference top bid value
_, state.TopBidValue = builderBids.getTopBid()
if floorValue.Cmp(state.TopBidValue) == 1 {
state.TopBidValue = floorValue
}
state.PrevTopBidValue = state.TopBidValue
// Abort now if non-cancellation bid is lower than floor value
isBidAboveFloor := submission.BidTrace.Value.ToBig().Cmp(floorValue) == 1
if !isCancellationEnabled && !isBidAboveFloor {
return state, nil
}
// Record time needed
nextTime = time.Now().UTC()
state.TimePrep = nextTime.Sub(prevTime)
prevTime = nextTime
//
// Time to save things in Redis
//
// 1. Save the execution payload
switch payload.Version {
case spec.DataVersionCapella:
err = r.SaveExecutionPayloadCapella(ctx, pipeliner, submission.BidTrace.Slot, submission.BidTrace.ProposerPubkey.String(), submission.BidTrace.BlockHash.String(), getPayloadResponse.Capella)
if err != nil {
return state, err
}
case spec.DataVersionDeneb:
err = r.SavePayloadContentsDeneb(ctx, pipeliner, submission.BidTrace.Slot, submission.BidTrace.ProposerPubkey.String(), submission.BidTrace.BlockHash.String(), getPayloadResponse.Deneb)
if err != nil {
return state, err
}
case spec.DataVersionElectra:
err = r.SavePayloadContentsElectra(ctx, pipeliner, submission.BidTrace.Slot, submission.BidTrace.ProposerPubkey.String(), submission.BidTrace.BlockHash.String(), getPayloadResponse.Electra)
if err != nil {
return state, err
}
case spec.DataVersionUnknown, spec.DataVersionPhase0, spec.DataVersionAltair, spec.DataVersionBellatrix:
return state, fmt.Errorf("unsupported payload version: %s", payload.Version) //nolint:goerr113
}
// Record time needed to save payload
nextTime = time.Now().UTC()
state.TimeSavePayload = nextTime.Sub(prevTime)
prevTime = nextTime
// 2. Save latest bid for this builder
err = r.SaveBuilderBid(ctx, pipeliner, submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String(), submission.BidTrace.BuilderPubkey.String(), reqReceivedAt, getHeaderResponse)
if err != nil {
return state, err
}
builderBids.bidValues[submission.BidTrace.BuilderPubkey.String()] = submission.BidTrace.Value.ToBig()
// Record time needed to save bid
nextTime = time.Now().UTC()
state.TimeSaveBid = nextTime.Sub(prevTime)
prevTime = nextTime
// 3. Save the bid trace
err = r.SaveBidTrace(ctx, pipeliner, trace)
if err != nil {
return state, err
}
// Record time needed to save trace
nextTime = time.Now().UTC()
state.TimeSaveTrace = nextTime.Sub(prevTime)
prevTime = nextTime
// If top bid value hasn't changed, abort now
_, state.TopBidValue = builderBids.getTopBid()
if state.TopBidValue.Cmp(state.PrevTopBidValue) == 0 {
return state, nil
}
state, err = r._updateTopBid(ctx, pipeliner, state, builderBids, submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String(), floorValue)
if err != nil {
return state, err
}
state.IsNewTopBid = submission.BidTrace.Value.ToBig().Cmp(state.TopBidValue) == 0
// An Exec happens in _updateTopBid.
state.WasBidSaved = true
// Record time needed to update top bid
nextTime = time.Now().UTC()
state.TimeUpdateTopBid = nextTime.Sub(prevTime)
prevTime = nextTime
if isCancellationEnabled || !isBidAboveFloor {
return state, nil
}
// Non-cancellable bid above floor should set new floor
keyBidSource := r.keyLatestBidByBuilder(submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String(), submission.BidTrace.BuilderPubkey.String())
keyFloorBid := r.keyFloorBid(submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String())
c := pipeliner.Copy(ctx, keyBidSource, keyFloorBid, 0, true)
_, err = pipeliner.Exec(ctx)
if err != nil {
return state, err
}
wasCopied, copyErr := c.Result()
if copyErr != nil {
return state, copyErr
} else if wasCopied == 0 {
return state, fmt.Errorf("could not copy floor bid from %s to %s", keyBidSource, keyFloorBid) //nolint:goerr113
}
err = pipeliner.Expire(ctx, keyFloorBid, expiryBidCache).Err()
if err != nil {
return state, err
}
keyFloorBidValue := r.keyFloorBidValue(submission.BidTrace.Slot, submission.BidTrace.ParentHash.String(), submission.BidTrace.ProposerPubkey.String())
err = pipeliner.Set(ctx, keyFloorBidValue, submission.BidTrace.Value.Dec(), expiryBidCache).Err()
if err != nil {
return state, err
}
// Execute setting the floor bid
_, err = pipeliner.Exec(ctx)
// Record time needed to update floor
nextTime = time.Now().UTC()
state.TimeUpdateFloor = nextTime.Sub(prevTime)
return state, err
}
func (r *RedisCache) _updateTopBid(ctx context.Context, pipeliner redis.Pipeliner, state SaveBidAndUpdateTopBidResponse, builderBids *BuilderBids, slot uint64, parentHash, proposerPubkey string, floorValue *big.Int) (resp SaveBidAndUpdateTopBidResponse, err error) {
if builderBids == nil {
builderBids, err = NewBuilderBidsFromRedis(ctx, r, pipeliner, slot, parentHash, proposerPubkey)
if err != nil {
return state, err
}
}
if len(builderBids.bidValues) == 0 {
return state, nil
}
// Load floor value (if not passed in already)
if floorValue == nil {
floorValue, err = r.GetFloorBidValue(ctx, pipeliner, slot, parentHash, proposerPubkey)
if err != nil {
return state, err
}
}
topBidBuilder := ""
topBidBuilder, state.TopBidValue = builderBids.getTopBid()
keyBidSource := r.keyLatestBidByBuilder(slot, parentHash, proposerPubkey, topBidBuilder)
// If floor value is higher than this bid, use floor bid instead
if floorValue.Cmp(state.TopBidValue) == 1 {
state.TopBidValue = floorValue
keyBidSource = r.keyFloorBid(slot, parentHash, proposerPubkey)
}
// Copy winning bid to top bid cache
keyTopBid := r.keyCacheGetHeaderResponse(slot, parentHash, proposerPubkey)
c := pipeliner.Copy(context.Background(), keyBidSource, keyTopBid, 0, true)
_, err = pipeliner.Exec(ctx)
if err != nil {
return state, err
}
wasCopied, err := c.Result()
if err != nil {
return state, err
} else if wasCopied == 0 {
return state, fmt.Errorf("could not copy top bid from %s to %s", keyBidSource, keyTopBid) //nolint:goerr113
}
err = pipeliner.Expire(context.Background(), keyTopBid, expiryBidCache).Err()
if err != nil {
return state, err
}
state.WasTopBidUpdated = state.PrevTopBidValue == nil || state.PrevTopBidValue.Cmp(state.TopBidValue) != 0
// 6. Finally, update the global top bid value
keyTopBidValue := r.keyTopBidValue(slot, parentHash, proposerPubkey)
err = pipeliner.Set(context.Background(), keyTopBidValue, state.TopBidValue.String(), expiryBidCache).Err()
if err != nil {
return state, err
}
_, err = pipeliner.Exec(ctx)
return state, err
}
// GetTopBidValue gets the top bid value for a given slot+parent+proposer combination
func (r *RedisCache) GetTopBidValue(ctx context.Context, pipeliner redis.Pipeliner, slot uint64, parentHash, proposerPubkey string) (topBidValue *big.Int, err error) {
keyTopBidValue := r.keyTopBidValue(slot, parentHash, proposerPubkey)
c := pipeliner.Get(ctx, keyTopBidValue)
_, err = pipeliner.Exec(ctx)
if errors.Is(err, redis.Nil) {
return big.NewInt(0), nil
} else if err != nil {
return nil, err
}
topBidValueStr, err := c.Result()
if err != nil {
return nil, err
}
topBidValue = new(big.Int)
topBidValue, ok := topBidValue.SetString(topBidValueStr, 10)
if !ok {
return nil, fmt.Errorf("could not set top bid value from %s", topBidValueStr) //nolint:goerr113
}
return topBidValue, nil
}
// GetBuilderLatestValue gets the latest bid value for a given slot+parent+proposer combination for a specific builder pubkey.
func (r *RedisCache) GetBuilderLatestValue(slot uint64, parentHash, proposerPubkey, builderPubkey string) (topBidValue *big.Int, err error) {
keyLatestValue := r.keyBlockBuilderLatestBidsValue(slot, parentHash, proposerPubkey)
topBidValueStr, err := r.client.HGet(context.Background(), keyLatestValue, builderPubkey).Result()
if errors.Is(err, redis.Nil) {
return big.NewInt(0), nil
} else if err != nil {
return nil, err
}
topBidValue = new(big.Int)
topBidValue, ok := topBidValue.SetString(topBidValueStr, 10)
if !ok {
return nil, fmt.Errorf("could not set top bid value from %s", topBidValueStr) //nolint:goerr113
}
return topBidValue, nil
}
// DelBuilderBid removes a builders most recent bid
func (r *RedisCache) DelBuilderBid(ctx context.Context, pipeliner redis.Pipeliner, slot uint64, parentHash, proposerPubkey, builderPubkey string) (err error) {
// delete the value
keyLatestValue := r.keyBlockBuilderLatestBidsValue(slot, parentHash, proposerPubkey)
err = r.client.HDel(ctx, keyLatestValue, builderPubkey).Err()
if err != nil && !errors.Is(err, redis.Nil) {
return err
}
// delete the time
keyLatestBidsTime := r.keyBlockBuilderLatestBidsTime(slot, parentHash, proposerPubkey)
err = r.client.HDel(ctx, keyLatestBidsTime, builderPubkey).Err()
if err != nil {
return err
}
// update bids now to compute current top bid
state := SaveBidAndUpdateTopBidResponse{} //nolint:exhaustruct
_, err = r._updateTopBid(ctx, pipeliner, state, nil, slot, parentHash, proposerPubkey, nil)
return err
}
// GetFloorBidValue returns the value of the highest non-cancellable bid
func (r *RedisCache) GetFloorBidValue(ctx context.Context, pipeliner redis.Pipeliner, slot uint64, parentHash, proposerPubkey string) (floorValue *big.Int, err error) {
keyFloorBidValue := r.keyFloorBidValue(slot, parentHash, proposerPubkey)
c := pipeliner.Get(ctx, keyFloorBidValue)
_, err = pipeliner.Exec(ctx)
if errors.Is(err, redis.Nil) {
return big.NewInt(0), nil
} else if err != nil {
return nil, err
}
topBidValueStr, err := c.Result()
if err != nil {
return nil, err
}
floorValue = new(big.Int)
floorValue.SetString(topBidValueStr, 10)
return floorValue, nil
}
// SetFloorBidValue is used only for testing.
func (r *RedisCache) SetFloorBidValue(slot uint64, parentHash, proposerPubkey, value string) error {
keyFloorBidValue := r.keyFloorBidValue(slot, parentHash, proposerPubkey)
err := r.client.Set(context.Background(), keyFloorBidValue, value, 0).Err()
return err
}
func (r *RedisCache) NewPipeline() redis.Pipeliner { //nolint:ireturn,nolintlint
return r.client.Pipeline()
}
func (r *RedisCache) NewTxPipeline() redis.Pipeliner { //nolint:ireturn
return r.client.TxPipeline()
}