forked from flashbots/mev-boost-relay
-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocksim_ratelimiter.go
140 lines (119 loc) · 3.83 KB
/
blocksim_ratelimiter.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
package api
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/flashbots/go-utils/cli"
"github.com/flashbots/go-utils/jsonrpc"
"github.com/flashbots/mev-boost-relay/common"
)
var (
ErrRequestClosed = errors.New("request context closed")
ErrSimulationFailed = errors.New("simulation failed")
ErrJSONDecodeFailed = errors.New("json error")
ErrNoCapellaPayload = errors.New("capella payload is nil")
maxConcurrentBlocks = int64(cli.GetEnvInt("BLOCKSIM_MAX_CONCURRENT", 4)) // 0 for no maximum
simRequestTimeout = time.Duration(cli.GetEnvInt("BLOCKSIM_TIMEOUT_MS", 10000)) * time.Millisecond
)
type IBlockSimRateLimiter interface {
Send(context context.Context, payload *common.BuilderBlockValidationRequest, isHighPrio, fastTrack bool) (error, error)
CurrentCounter() int64
}
type BlockSimulationRateLimiter struct {
cv *sync.Cond
counter int64
blockSimURL string
client http.Client
}
func NewBlockSimulationRateLimiter(blockSimURL string) *BlockSimulationRateLimiter {
return &BlockSimulationRateLimiter{
cv: sync.NewCond(&sync.Mutex{}),
counter: 0,
blockSimURL: blockSimURL,
client: http.Client{ //nolint:exhaustruct
Timeout: simRequestTimeout,
},
}
}
func (b *BlockSimulationRateLimiter) Send(context context.Context, payload *common.BuilderBlockValidationRequest, isHighPrio, fastTrack bool) (requestErr, validationErr error) {
b.cv.L.Lock()
cnt := atomic.AddInt64(&b.counter, 1)
if maxConcurrentBlocks > 0 && cnt > maxConcurrentBlocks {
b.cv.Wait()
}
b.cv.L.Unlock()
defer func() {
b.cv.L.Lock()
atomic.AddInt64(&b.counter, -1)
b.cv.Signal()
b.cv.L.Unlock()
}()
if err := context.Err(); err != nil {
return fmt.Errorf("%w, %w", ErrRequestClosed, err), nil
}
var simReq *jsonrpc.JSONRPCRequest
if payload.Capella == nil {
return ErrNoCapellaPayload, nil
}
// TODO: add deneb support.
// Prepare headers
headers := http.Header{}
headers.Add("X-Request-ID", fmt.Sprintf("%d/%s", payload.Slot(), payload.BlockHash()))
if isHighPrio {
headers.Add("X-High-Priority", "true")
}
if fastTrack {
headers.Add("X-Fast-Track", "true")
}
// Create and fire off JSON-RPC request
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV2", payload)
_, requestErr, validationErr = SendJSONRPCRequest(&b.client, *simReq, b.blockSimURL, headers)
return requestErr, validationErr
}
// CurrentCounter returns the number of waiting and active requests
func (b *BlockSimulationRateLimiter) CurrentCounter() int64 {
return atomic.LoadInt64(&b.counter)
}
// SendJSONRPCRequest sends the request to URL and returns the general JsonRpcResponse, or an error (note: not the JSONRPCError)
func SendJSONRPCRequest(client *http.Client, req jsonrpc.JSONRPCRequest, url string, headers http.Header) (res *jsonrpc.JSONRPCResponse, requestErr, validationErr error) {
buf, err := json.Marshal(req)
if err != nil {
return nil, err, nil
}
httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(buf))
if err != nil {
return nil, err, nil
}
// set request headers
httpReq.Header.Add("Content-Type", "application/json")
for k, v := range headers {
httpReq.Header.Add(k, v[0])
}
// execute request
resp, err := client.Do(httpReq)
if err != nil {
return nil, err, nil
}
defer resp.Body.Close()
// read all resp bytes
rawResp, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response bytes: %w", err), nil
}
// try json parsing
res = new(jsonrpc.JSONRPCResponse)
if err := json.NewDecoder(bytes.NewReader(rawResp)).Decode(res); err != nil {
return nil, fmt.Errorf("%w: %v", ErrJSONDecodeFailed, string(rawResp[:])), nil
}
if res.Error != nil {
return res, nil, fmt.Errorf("%w: %s", ErrSimulationFailed, res.Error.Message)
}
return res, nil, nil
}