Skip to content

Commit 1bf3792

Browse files
committed
refactor: sentry validator routing
1 parent b6ed1c9 commit 1bf3792

1 file changed

Lines changed: 35 additions & 77 deletions

File tree

service/sentry.go

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,8 @@ func (s *MevSentry) SendBid(ctx context.Context, args BidArgsWrapper) (bidHash c
8181
}
8282
log.Debugw("[BID RECEIVED]", "block", args.RawBid.BlockNumber, "builder", builder, "hash", args.RawBid.Hash().TerminalString())
8383

84-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
85-
if strings.Contains(hostname, ":") {
86-
hostname = hostname[:strings.Index(hostname, ":")]
87-
}
88-
89-
if args.ValidatorHostName != "" {
90-
log.Debugw("hostname override", "from", hostname, "to", args.ValidatorHostName)
91-
hostname = args.ValidatorHostName
92-
} else {
93-
log.Debugw("hostname from context", "hostname", hostname)
94-
}
95-
96-
validator, ok := s.validators[hostname]
97-
if !ok {
98-
log.Errorw("validator not found", "hostname", hostname)
99-
err = types.NewInvalidBidError("validator hostname not found")
84+
validator, err := s.validatorFromRequest(ctx, args.ValidatorHostName)
85+
if err != nil {
10086
return
10187
}
10288

@@ -166,22 +152,8 @@ func (s *MevSentry) SendBidBlock(ctx context.Context, args BidBlockArgsWrapper)
166152
}
167153
log.Debugw("[BID BLOCK RECEIVED]", "builder", builder)
168154

169-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
170-
if strings.Contains(hostname, ":") {
171-
hostname = hostname[:strings.Index(hostname, ":")]
172-
}
173-
174-
if args.ValidatorHostName != "" {
175-
log.Debugw("hostname override", "from", hostname, "to", args.ValidatorHostName)
176-
hostname = args.ValidatorHostName
177-
} else {
178-
log.Debugw("hostname from context", "hostname", hostname)
179-
}
180-
181-
validator, ok := s.validators[hostname]
182-
if !ok {
183-
log.Errorw("validator not found", "hostname", hostname)
184-
err = types.NewInvalidBidError("validator hostname not found")
155+
validator, err := s.validatorFromRequest(ctx, args.ValidatorHostName)
156+
if err != nil {
185157
return
186158
}
187159

@@ -202,15 +174,8 @@ func (s *MevSentry) GetBidBlockPermission(ctx context.Context, builder common.Ad
202174
}
203175
}()
204176

205-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
206-
if strings.Contains(hostname, ":") {
207-
hostname = hostname[:strings.Index(hostname, ":")]
208-
}
209-
210-
validator, ok := s.validators[hostname]
211-
if !ok {
212-
log.Errorw("validator not found", "hostname", hostname)
213-
err = types.NewInvalidBidError("validator hostname not found")
177+
validator, err := s.validatorFromRequest(ctx, "")
178+
if err != nil {
214179
return
215180
}
216181

@@ -230,15 +195,8 @@ func (s *MevSentry) BestBidGasFee(ctx context.Context, parentHash common.Hash) (
230195
}
231196
}()
232197

233-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
234-
if strings.Contains(hostname, ":") {
235-
hostname = hostname[:strings.Index(hostname, ":")]
236-
}
237-
238-
validator, ok := s.validators[hostname]
239-
if !ok {
240-
log.Errorw("validator not found", "hostname", hostname)
241-
err = types.NewInvalidBidError("validator hostname not found")
198+
validator, err := s.validatorFromRequest(ctx, "")
199+
if err != nil {
242200
return
243201
}
244202

@@ -259,15 +217,8 @@ func (s *MevSentry) Params(ctx context.Context) (param *types.MevParams, err err
259217
}
260218
}()
261219

262-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
263-
if strings.Contains(hostname, ":") {
264-
hostname = hostname[:strings.Index(hostname, ":")]
265-
}
266-
267-
validator, ok := s.validators[hostname]
268-
if !ok {
269-
log.Errorw("validator not found", "hostname", hostname)
270-
err = types.NewInvalidBidError("validator hostname not found")
220+
validator, err := s.validatorFromRequest(ctx, "")
221+
if err != nil {
271222
return
272223
}
273224

@@ -288,15 +239,8 @@ func (s *MevSentry) Running(ctx context.Context) (running bool, err error) {
288239
}
289240
}()
290241

291-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
292-
if strings.Contains(hostname, ":") {
293-
hostname = hostname[:strings.Index(hostname, ":")]
294-
}
295-
296-
validator, ok := s.validators[hostname]
297-
if !ok {
298-
log.Errorw("validator not found", "hostname", hostname)
299-
err = types.NewInvalidBidError("validator hostname not found")
242+
validator, err := s.validatorFromRequest(ctx, "")
243+
if err != nil {
300244
return
301245
}
302246

@@ -316,15 +260,8 @@ func (s *MevSentry) HasBuilder(ctx context.Context, builder common.Address) (has
316260
}
317261
}()
318262

319-
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
320-
if strings.Contains(hostname, ":") {
321-
hostname = hostname[:strings.Index(hostname, ":")]
322-
}
323-
324-
validator, ok := s.validators[hostname]
325-
if !ok {
326-
log.Errorw("validator not found", "hostname", hostname)
327-
err = types.NewInvalidBidError("validator hostname not found")
263+
validator, err := s.validatorFromRequest(ctx, "")
264+
if err != nil {
328265
return
329266
}
330267

@@ -364,6 +301,27 @@ func recordLatency(method string, start time.Time) {
364301
metrics.ApiLatencyHist.WithLabelValues(method).Observe(float64(time.Since(start).Milliseconds()))
365302
}
366303

304+
func (s *MevSentry) validatorFromRequest(ctx context.Context, override string) (node.Validator, error) {
305+
hostname := rpc.PeerInfoFromContext(ctx).HTTP.Host
306+
if strings.Contains(hostname, ":") {
307+
hostname = hostname[:strings.Index(hostname, ":")]
308+
}
309+
310+
if override != "" {
311+
log.Debugw("hostname override", "from", hostname, "to", override)
312+
hostname = override
313+
} else {
314+
log.Debugw("hostname from context", "hostname", hostname)
315+
}
316+
317+
validator, ok := s.validators[hostname]
318+
if !ok {
319+
log.Errorw("validator not found", "hostname", hostname)
320+
return nil, types.NewInvalidBidError("validator hostname not found")
321+
}
322+
return validator, nil
323+
}
324+
367325
func nilCancel() {
368326
}
369327

0 commit comments

Comments
 (0)