generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 118
/
blocksim_ratelimiter.go
171 lines (148 loc) · 4.86 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
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
package api
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/attestantio/go-eth2-client/spec"
"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")
ErrNoDenebPayload = errors.New("deneb 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) (*common.BuilderBlockValidationResponse, 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,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
MaxConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
},
},
}
}
func (b *BlockSimulationRateLimiter) Send(
context context.Context,
payload *common.BuilderBlockValidationRequest,
isHighPrio,
fastTrack bool,
) (response *common.BuilderBlockValidationResponse, 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 nil, fmt.Errorf("%w, %w", ErrRequestClosed, err), nil
}
var simReq *jsonrpc.JSONRPCRequest
if payload.Version == spec.DataVersionCapella && payload.Capella == nil {
return nil, ErrNoCapellaPayload, nil
}
if payload.Version == spec.DataVersionDeneb && payload.Deneb == nil {
return nil, ErrNoDenebPayload, nil
}
submission, err := common.GetBlockSubmissionInfo(payload.VersionedSubmitBlockRequest)
if err != nil {
return nil, err, nil
}
// Prepare headers
headers := http.Header{}
headers.Add("X-Request-ID", fmt.Sprintf("%d/%s", submission.BidTrace.Slot, submission.BidTrace.BlockHash.String()))
if isHighPrio {
headers.Add("X-High-Priority", "true")
}
if fastTrack {
headers.Add("X-Fast-Track", "true")
}
// Create and fire off JSON-RPC request
if payload.Version == spec.DataVersionDeneb {
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV3", payload)
} else {
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV2", payload)
}
res, requestErr, validationErr := SendJSONRPCRequest(&b.client, *simReq, b.blockSimURL, headers)
response = new(common.BuilderBlockValidationResponse)
if res != nil {
if err := json.Unmarshal(res.Result, response); err != nil {
return nil, fmt.Errorf("unable to unmarshal response: %w", err), validationErr
}
}
return response, 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
}