-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
253 lines (201 loc) · 8.43 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
package datastore
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/attestantio/go-builder-client/api"
consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/bloXroute-Labs/mev-relay/common"
"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
)
var (
redisPrefix = "boost-relay"
expiryBidCache = 1 * time.Minute
expiryBlockSubmissionStatus = 24 * time.Second
expiryActiveValidators = 3
)
type BlockSimulationStatus string
const (
BlockSimulationSubmitted = "1"
BlockSimulationPassed = "2"
BlockSimulationFailed = "3"
)
type redisCache struct {
client *redis.Client
prefixGetHeaderResponse string
prefixGetPayloadResponse string
prefixBlockSimulationStatus string
prefixBlockBuilderLatestBids string // latest bid for a given slot
prefixBlockBuilderLatestBidsValue string // value of the latest bid for a given slot
prefixBlockBuilderLatestBidsValueTime string // value of the latest bid for a given slot
prefixBlockBuilderLatestBidsTime string // when the request was received, to avoid older requests overwriting newer ones after a slot validation
prefixDeliveredPayload string
prefixBlockBuilderSubmission string
prefixBidTrace string
keyDemotedBuilders string
keyKnownValidators string
keyValidatorRegistration string
prefixActiveValidators string
prefixActiveValidatorsInt string
keyValidatorRegistrationTimestamp string
keyRelayConfig string
keyStats string
keyProposerDuties string
getHeaderList sync.Mutex
}
func newRedisCache(redisURI, prefix string, connectionPoolLimit int) (*redisCache, error) {
client, err := connectRedis(redisURI, connectionPoolLimit)
if err != nil {
return nil, err
}
// Enable tracing instrumentation.
if err := redisotel.InstrumentTracing(client); err != nil {
return nil, err
}
// Enable metrics instrumentation.
if err := redisotel.InstrumentMetrics(client); err != nil {
return nil, err
}
return &redisCache{
client: client,
prefixGetHeaderResponse: fmt.Sprintf("%s/%s:cache-gethead-response", redisPrefix, prefix),
prefixGetPayloadResponse: fmt.Sprintf("%s/%s:cache-getpayload-response", redisPrefix, prefix),
prefixBlockSimulationStatus: fmt.Sprintf("%s/%s:cache-block-simulation-status", 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
prefixBlockBuilderLatestBidsValueTime: fmt.Sprintf("%s/%s:block-builder-latest-bid-value-time", redisPrefix, prefix), // hashmap for slot+parentHash+proposerPubkey with builderPubkey as field
prefixDeliveredPayload: fmt.Sprintf("%s/%s:relay-delivered-payload", redisPrefix, prefix),
prefixBlockBuilderSubmission: fmt.Sprintf("%s/%s:builder-block-submission", redisPrefix, prefix),
prefixBidTrace: fmt.Sprintf("%s/%s:bid-trace", redisPrefix, prefix),
keyDemotedBuilders: fmt.Sprintf("%s/%s:builder-demoted", redisPrefix, prefix),
keyKnownValidators: fmt.Sprintf("%s/%s:known-validators", redisPrefix, prefix),
keyValidatorRegistration: fmt.Sprintf("%s/%s:validators-registration-timestamp", redisPrefix, prefix),
keyValidatorRegistrationTimestamp: fmt.Sprintf("%s/%s:validators-registration", redisPrefix, prefix),
prefixActiveValidators: fmt.Sprintf("%s/%s:active-validators", redisPrefix, prefix),
prefixActiveValidatorsInt: fmt.Sprintf("%s/%s:active-validators-int", 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),
}, nil
}
func connectRedis(redisURI string, connectionPoolLimit int) (*redis.Client, error) {
redisClient := redis.NewClient(&redis.Options{
PoolSize: connectionPoolLimit,
Addr: redisURI,
MinIdleConns: connectionPoolLimit / 2,
PoolTimeout: 20 * time.Second,
ReadTimeout: 20 * time.Second,
WriteTimeout: 15 * time.Second,
})
if _, err := redisClient.Ping(context.Background()).Result(); err != nil {
// unable to connect to redis
return nil, err
}
return redisClient, nil
}
func (r *redisCache) getObj(ctx context.Context, key string, obj any) (err error) {
value, err := r.client.Get(ctx, key).Result()
if err != nil {
return err
}
return json.Unmarshal([]byte(value), &obj)
}
func (r *redisCache) getObjSSZ(ctx context.Context, key string) (*common.GetPayloadResponse, error) {
res := new(common.GetPayloadResponse)
capData := new(capella.ExecutionPayload)
value, err := r.client.Get(ctx, key).Result()
if err != nil {
return nil, err
}
if err := capData.UnmarshalSSZ([]byte(value)); err != nil {
return nil, err
}
res.Capella = new(api.VersionedExecutionPayload)
res.Capella.Version = consensusspec.DataVersionCapella
res.Capella.Capella = capData
return res, nil
}
func (r *redisCache) getString(ctx context.Context, key string) (res string, err error) {
return r.client.Get(ctx, key).Result()
}
func (r *redisCache) setString(ctx context.Context, key string, value string, expiration time.Duration) (err error) {
return r.client.Set(ctx, key, value, expiration).Err()
}
func (r *redisCache) setObj(ctx context.Context, key string, value any, expiration time.Duration) (err error) {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
return r.client.Set(ctx, key, marshalledValue, expiration).Err()
}
// setObjTx is a transactional version of setObj
func setObjTx(ctx context.Context, tx redis.Pipeliner, key string, value any, expiration time.Duration) (err error) {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
return tx.Set(ctx, key, marshalledValue, expiration).Err()
}
// setObjTx is a transactional version of setObj
func setObjTxWithReturn(ctx context.Context, tx redis.Pipeliner, key string, value any, expiration time.Duration) (strVal string, err error) {
marshalledValue, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(marshalledValue), tx.Set(ctx, key, marshalledValue, expiration).Err()
}
// setObjTxSSZ is a transactional version of setObj
func setObjTxSSZ(ctx context.Context, tx redis.Pipeliner, key string, value *common.GetPayloadResponse, expiration time.Duration) (err error) {
b, err := value.Capella.Capella.MarshalSSZ()
if err != nil {
return err
}
return tx.Set(ctx, key, b, expiration).Err()
}
// hSetObjTx is a transactional version of hSetObj
func hSetObjTx(ctx context.Context, tx redis.Pipeliner, key, field string, value any, expiration time.Duration) error {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
err = tx.HSet(ctx, key, field, marshalledValue).Err()
if err != nil {
return err
}
return tx.Expire(ctx, key, expiration).Err()
}
// hSetObjTxNoExpire is a transactional version of hSetObj without calling expire
func hSetObjTxNoExpire(ctx context.Context, tx redis.Pipeliner, key, field string, value any, expiration time.Duration) error {
marshalledValue, err := json.Marshal(value)
if err != nil {
return err
}
err = tx.HSet(ctx, key, field, marshalledValue).Err()
if err != nil {
return err
}
return nil
}
func (r *redisCache) UpdateActiveValidatorCount(ctx context.Context) error {
registration, err := r.client.HGetAll(ctx, r.keyValidatorRegistration).Result()
if err != nil && err != redis.Nil {
return err
}
total := 0
threeHoursAgo := time.Now().Add(-3 * time.Hour).UnixNano()
for _, regString := range registration {
reg := new(ValidatorLatency)
if err := json.Unmarshal([]byte(regString), reg); err != nil {
return err
}
if reg.LastRegistered > threeHoursAgo {
total += 1
}
}
return r.client.Set(ctx, r.prefixActiveValidatorsInt, total, 2*time.Hour).Err()
}