generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 118
/
redis.go
639 lines (542 loc) · 25.2 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
package datastore
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"time"
boostTypes "github.com/flashbots/go-boost-utils/types"
"github.com/flashbots/go-utils/cli"
"github.com/flashbots/mev-boost-relay/common"
"github.com/go-redis/redis/v9"
)
var (
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")
activeValidatorsHours = cli.GetEnvInt("ACTIVE_VALIDATOR_HOURS", 3)
expiryActiveValidators = time.Duration(activeValidatorsHours) * time.Hour // careful with this setting - for each hour a hash set is created with each active proposer as field. for a lot of hours this can take a lot of space in redis.
// 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 PubkeyHexToLowerStr(pk boostTypes.PubkeyHex) string {
return strings.ToLower(string(pk))
}
func connectRedis(redisURI string) (*redis.Client, error) {
// Handle both URIs and full URLs, assume unencrypted connections
if !strings.HasPrefix(redisURI, "redis://") && !strings.HasPrefix(redisURI, "rediss://") {
redisURI = "redis://" + 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
prefixGetPayloadResponse string
prefixBidTrace string
prefixActiveValidators 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
keyKnownValidators string
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),
prefixGetPayloadResponse: fmt.Sprintf("%s/%s:cache-getpayload-response", redisPrefix, prefix),
prefixBidTrace: fmt.Sprintf("%s/%s:cache-bid-trace", redisPrefix, prefix),
prefixActiveValidators: fmt.Sprintf("%s/%s:active-validators", redisPrefix, prefix), // one entry per hour
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
keyKnownValidators: fmt.Sprintf("%s/%s:known-validators", redisPrefix, prefix),
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) keyCacheGetPayloadResponse(slot uint64, proposerPubkey, blockHash string) string {
return fmt.Sprintf("%s:%d_%s_%s", r.prefixGetPayloadResponse, 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)
}
// keyActiveValidators returns the key for the date + hour of the given time
func (r *RedisCache) keyActiveValidators(t time.Time) string {
return fmt.Sprintf("%s:%s", r.prefixActiveValidators, t.UTC().Format("2006-01-02T15"))
}
// 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()
}
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) GetKnownValidators() (map[uint64]boostTypes.PubkeyHex, error) {
validators := make(map[uint64]boostTypes.PubkeyHex)
entries, err := r.readonlyClient.HGetAll(context.Background(), r.keyKnownValidators).Result()
if err != nil {
return nil, err
}
for proposerIndexStr, pubkey := range entries {
if strings.HasPrefix(proposerIndexStr, "0x") {
// remove -- it's an artifact of the previous storage by pubkey
r.client.HDel(context.Background(), r.keyKnownValidators, proposerIndexStr)
continue
}
proposerIndex, err := strconv.ParseUint(proposerIndexStr, 10, 64)
if err == nil {
validators[proposerIndex] = boostTypes.PubkeyHex(pubkey)
}
}
return validators, nil
}
func (r *RedisCache) SetMultiKnownValidator(indexPkMap map[uint64]boostTypes.PubkeyHex) error {
values := []string{}
for proposerIndex, publickeyHex := range indexPkMap {
values = append(values, strconv.FormatUint(proposerIndex, 10), PubkeyHexToLowerStr(publickeyHex))
}
return r.client.HMSet(context.Background(), r.keyKnownValidators, values).Err()
}
func (r *RedisCache) SetKnownValidator(pubkeyHex boostTypes.PubkeyHex, proposerIndex uint64) error {
return r.SetMultiKnownValidator(map[uint64]boostTypes.PubkeyHex{proposerIndex: pubkeyHex})
}
func (r *RedisCache) GetValidatorRegistrationTimestamp(proposerPubkey boostTypes.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 boostTypes.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 boostTypes.PubkeyHex, timestamp uint64) error {
return r.client.HSet(context.Background(), r.keyValidatorRegistrationTimestamp, proposerPubkey.String(), timestamp).Err()
}
func (r *RedisCache) SetActiveValidator(pubkeyHex boostTypes.PubkeyHex) error {
key := r.keyActiveValidators(time.Now())
err := r.client.HSet(context.Background(), key, PubkeyHexToLowerStr(pubkeyHex), "1").Err()
if err != nil {
return err
}
// set expiry
return r.client.Expire(context.Background(), key, expiryActiveValidators).Err()
}
func (r *RedisCache) GetActiveValidators() (map[boostTypes.PubkeyHex]bool, error) {
hours := activeValidatorsHours
now := time.Now()
validators := make(map[boostTypes.PubkeyHex]bool)
for i := 0; i < hours; i++ {
key := r.keyActiveValidators(now.Add(time.Duration(-i) * time.Hour))
entries, err := r.readonlyClient.HGetAll(context.Background(), key).Result()
if err != nil {
return nil, err
}
for pubkey := range entries {
validators[boostTypes.PubkeyHex(pubkey)] = true
}
}
return validators, nil
}
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() (slot uint64, err error) {
return r.client.Get(context.Background(), r.keyLastSlotDelivered).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) (*common.GetHeaderResponse, error) {
key := r.keyCacheGetHeaderResponse(slot, parentHash, proposerPubkey)
resp := new(common.GetHeaderResponse)
err := r.GetObj(key, resp)
if errors.Is(err, redis.Nil) {
return nil, nil
}
return resp, err
}
func (r *RedisCache) SaveExecutionPayload(slot uint64, proposerPubkey, blockHash string, resp *common.GetPayloadResponse) (err error) {
key := r.keyCacheGetPayloadResponse(slot, proposerPubkey, blockHash)
return r.SetObj(key, resp, expiryBidCache)
}
func (r *RedisCache) GetExecutionPayload(slot uint64, proposerPubkey, blockHash string) (*common.VersionedExecutionPayload, error) {
key := r.keyCacheGetPayloadResponse(slot, proposerPubkey, blockHash)
resp := new(common.VersionedExecutionPayload)
err := r.GetObj(key, resp)
if errors.Is(err, redis.Nil) {
return nil, nil
}
return resp, err
}
func (r *RedisCache) SaveBidTrace(trace *common.BidTraceV2) (err error) {
key := r.keyCacheBidTrace(trace.Slot, trace.ProposerPubkey.String(), trace.BlockHash.String())
return r.SetObj(key, trace, expiryBidCache)
}
func (r *RedisCache) GetBidTrace(slot uint64, proposerPubkey, blockHash string) (*common.BidTraceV2, error) {
key := r.keyCacheBidTrace(slot, proposerPubkey, blockHash)
resp := new(common.BidTraceV2)
err := r.GetObj(key, resp)
if errors.Is(err, redis.Nil) {
return nil, nil
}
return resp, err
}
func (r *RedisCache) GetBuilderLatestPayloadReceivedAt(slot uint64, builderPubkey, parentHash, proposerPubkey string) (int64, error) {
keyLatestBidsTime := r.keyBlockBuilderLatestBidsTime(slot, parentHash, proposerPubkey)
timestamp, err := r.client.HGet(context.Background(), keyLatestBidsTime, builderPubkey).Int64()
if errors.Is(err, redis.Nil) {
return 0, nil
}
return timestamp, err
}
// SaveBuilderBid saves the latest bid by a specific builder. TODO: use transaction to make these writes atomic
func (r *RedisCache) SaveBuilderBid(slot uint64, parentHash, proposerPubkey, builderPubkey string, receivedAt time.Time, headerResp *common.GetHeaderResponse) (err error) {
// save the actual bid
keyLatestBid := r.keyLatestBidByBuilder(slot, parentHash, proposerPubkey, builderPubkey)
err = r.SetObj(keyLatestBid, headerResp, expiryBidCache)
if err != nil {
return err
}
// set the time of the request
keyLatestBidsTime := r.keyBlockBuilderLatestBidsTime(slot, parentHash, proposerPubkey)
err = r.client.HSet(context.Background(), keyLatestBidsTime, builderPubkey, receivedAt.UnixMilli()).Err()
if err != nil {
return err
}
err = r.client.Expire(context.Background(), 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)
err = r.client.HSet(context.Background(), keyLatestBidsValue, builderPubkey, headerResp.Value().String()).Err()
if err != nil {
return err
}
return r.client.Expire(context.Background(), 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
}
func (r *RedisCache) SaveBidAndUpdateTopBid(payload *common.BuilderSubmitBlockRequest, getPayloadResponse *common.GetPayloadResponse, getHeaderResponse *common.GetHeaderResponse, reqReceivedAt time.Time, isCancellationEnabled bool, floorValue *big.Int) (state SaveBidAndUpdateTopBidResponse, err error) {
// 1. Load latest bids for a given slot+parent+proposer
keyBidValues := r.keyBlockBuilderLatestBidsValue(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
bidValueMap, err := r.client.HGetAll(context.Background(), keyBidValues).Result()
if err != nil {
return state, err
}
if floorValue == nil {
floorValue, err = r.GetFloorBidValue(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
if err != nil {
return state, err
}
}
builderBids := NewBuilderBids(bidValueMap)
_, state.PrevTopBidValue = builderBids.getTopBid()
state.TopBidValue = state.PrevTopBidValue
// 2. Do we even need to continue / save the new payload and update the top bid?
// - In cancellation mode: always continue to saving latest bid
// - In non-cancellation mode: only save if current bid is higher value than floor value
if !isCancellationEnabled && payload.Value().Cmp(floorValue) < 1 {
return state, nil
}
// Time to save things in Redis
// 1. Save the execution payload
err = r.SaveExecutionPayload(payload.Slot(), payload.ProposerPubkey(), payload.BlockHash(), getPayloadResponse)
if err != nil {
return state, err
}
// 2. Save latest bid for this builder
err = r.SaveBuilderBid(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey(), payload.BuilderPubkey().String(), reqReceivedAt, getHeaderResponse)
if err != nil {
return state, err
}
state.WasBidSaved = true
// 3. Update this builders latest bid in local cache
builderBids.bidValues[payload.BuilderPubkey().String()] = payload.Value()
topBidBuilder := ""
topBidBuilder, state.TopBidValue = builderBids.getTopBid()
keyBidSource := r.keyLatestBidByBuilder(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey(), topBidBuilder)
// 4. Only proceed to update top bid in redis if it changed in local cache
if state.TopBidValue.Cmp(state.PrevTopBidValue) == 0 {
return state, nil
}
// If floor value is higher than this bid, use floor bid instead
if floorValue.Cmp(state.TopBidValue) == 1 {
state.TopBidValue = floorValue
keyBidSource = r.keyFloorBid(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
}
// 5. Copy winning bid to top bid cache
keyTopBid := r.keyCacheGetHeaderResponse(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
wasCopied, err := r.client.Copy(context.Background(), keyBidSource, keyTopBid, 0, true).Result()
if err != nil {
return state, err
} else if wasCopied == 0 {
return state, fmt.Errorf("could not copy %s to %s", keyBidSource, keyTopBid) //nolint:goerr113
}
err = r.client.Expire(context.Background(), keyTopBid, expiryBidCache).Err()
if err != nil {
return state, err
}
state.WasTopBidUpdated = state.PrevTopBidValue.Cmp(state.TopBidValue) != 0
state.IsNewTopBid = payload.Value().Cmp(state.TopBidValue) == 0
// 6. Finally, update the global top bid value
keyTopBidValue := r.keyTopBidValue(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
err = r.client.Set(context.Background(), keyTopBidValue, state.TopBidValue.String(), expiryBidCache).Err()
if err != nil {
return state, err
}
// 7. If non-cancelling, perhaps set a new bid floor
if !isCancellationEnabled && payload.Value().Cmp(floorValue) == 1 {
keyBidSource := r.keyLatestBidByBuilder(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey(), payload.BuilderPubkey().String())
keyFloorBid := r.keyFloorBid(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
wasCopied, copyErr := r.client.Copy(context.Background(), keyBidSource, keyFloorBid, 0, true).Result()
if copyErr != nil {
return state, copyErr
} else if wasCopied == 0 {
return state, fmt.Errorf("could not copy %s to %s", keyBidSource, keyTopBid) //nolint:goerr113
}
err = r.client.Expire(context.Background(), keyFloorBid, expiryBidCache).Err()
if err != nil {
return state, err
}
keyFloorBidValue := r.keyFloorBidValue(payload.Slot(), payload.ParentHash(), payload.ProposerPubkey())
err = r.client.Set(context.Background(), keyFloorBidValue, payload.Value().String(), expiryBidCache).Err()
}
return state, err
}
// GetTopBidValue gets the top bid value for a given slot+parent+proposer combination
func (r *RedisCache) GetTopBidValue(slot uint64, parentHash, proposerPubkey string) (topBidValue *big.Int, err error) {
keyTopBidValue := r.keyTopBidValue(slot, parentHash, proposerPubkey)
topBidValueStr, err := r.client.Get(context.Background(), keyTopBidValue).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return big.NewInt(0), nil
}
return nil, err
}
topBidValue = new(big.Int)
topBidValue.SetString(topBidValueStr, 10)
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 err != nil {
if errors.Is(err, redis.Nil) {
return big.NewInt(0), nil
}
return nil, err
}
topBidValue = new(big.Int)
topBidValue.SetString(topBidValueStr, 10)
return topBidValue, nil
}
// GetFloorBidValue returns the value of the highest non-cancellable bid
func (r *RedisCache) GetFloorBidValue(slot uint64, parentHash, proposerPubkey string) (floorValue *big.Int, err error) {
keyFloorBidValue := r.keyFloorBidValue(slot, parentHash, proposerPubkey)
topBidValueStr, err := r.client.Get(context.Background(), keyFloorBidValue).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return big.NewInt(0), nil
}
return nil, err
}
floorValue = new(big.Int)
floorValue.SetString(topBidValueStr, 10)
return floorValue, nil
}