1
- use alloy_json_rpc:: RpcError ;
2
1
use alloy_primitives:: { BlockHash , U256 } ;
3
- use alloy_provider:: Provider ;
2
+ use jsonrpsee:: {
3
+ core:: { client:: ClientT , traits:: ToRpcParams } ,
4
+ http_client:: { HttpClient , HttpClientBuilder } ,
5
+ } ;
4
6
use rbuilder:: {
5
7
building:: BuiltBlockTrace ,
6
8
live_builder:: block_output:: bid_observer:: BidObserver ,
@@ -9,10 +11,11 @@ use rbuilder::{
9
11
serialize:: { RawBundle , RawShareBundle } ,
10
12
Order ,
11
13
} ,
12
- utils:: { error_storage:: store_error_event, http_provider , BoxedProvider } ,
14
+ utils:: error_storage:: store_error_event,
13
15
} ;
14
16
use reth:: primitives:: SealedBlock ;
15
17
use serde:: { Deserialize , Serialize } ;
18
+ use serde_json:: value:: RawValue ;
16
19
use serde_with:: { serde_as, DisplayFromStr } ;
17
20
use std:: sync:: Arc ;
18
21
use time:: format_description:: well_known;
@@ -21,11 +24,8 @@ use tracing::{debug, error, warn, Span};
21
24
use crate :: metrics:: inc_blocks_api_errors;
22
25
23
26
const BLOCK_PROCESSOR_ERROR_CATEGORY : & str = "block_processor" ;
24
-
25
- #[ derive( Debug , Clone ) ]
26
- pub struct BlocksProcessorClient {
27
- client : Arc < BoxedProvider > ,
28
- }
27
+ const DEFAULT_BLOCK_CONSUME_BUILT_BLOCK_METHOD : & str = "block_consumeBuiltBlockV2" ;
28
+ pub const SIGNED_BLOCK_CONSUME_BUILT_BLOCK_METHOD : & str = "flashbots_consumeBuiltBlockV2" ;
29
29
30
30
#[ derive( Debug , Serialize , Deserialize ) ]
31
31
#[ serde( rename_all = "camelCase" ) ]
@@ -64,13 +64,68 @@ struct BlocksProcessorHeader {
64
64
pub number : Option < U256 > ,
65
65
}
66
66
67
- impl BlocksProcessorClient {
67
+ type ConsumeBuiltBlockRequest = (
68
+ BlocksProcessorHeader ,
69
+ String ,
70
+ String ,
71
+ Vec < UsedBundle > ,
72
+ Vec < UsedBundle > ,
73
+ Vec < UsedSbundle > ,
74
+ reth:: rpc:: types:: beacon:: relay:: BidTrace ,
75
+ String ,
76
+ U256 ,
77
+ U256 ,
78
+ ) ;
79
+
80
+ /// Struct to avoid copying ConsumeBuiltBlockRequest since HttpClient::request eats the parameter.
81
+ #[ derive( Clone ) ]
82
+ struct ConsumeBuiltBlockRequestArc {
83
+ inner : Arc < ConsumeBuiltBlockRequest > ,
84
+ }
85
+
86
+ impl ConsumeBuiltBlockRequestArc {
87
+ fn new ( request : ConsumeBuiltBlockRequest ) -> Self {
88
+ Self {
89
+ inner : Arc :: new ( request) ,
90
+ }
91
+ }
92
+ fn as_ref ( & self ) -> & ConsumeBuiltBlockRequest {
93
+ self . inner . as_ref ( )
94
+ }
95
+ }
96
+
97
+ impl ToRpcParams for ConsumeBuiltBlockRequestArc {
98
+ fn to_rpc_params ( self ) -> Result < Option < Box < RawValue > > , jsonrpsee:: core:: Error > {
99
+ let json = serde_json:: to_string ( self . inner . as_ref ( ) )
100
+ . map_err ( jsonrpsee:: core:: Error :: ParseError ) ?;
101
+ RawValue :: from_string ( json)
102
+ . map ( Some )
103
+ . map_err ( jsonrpsee:: core:: Error :: ParseError )
104
+ }
105
+ }
106
+
107
+ #[ derive( Debug , Clone ) ]
108
+ pub struct BlocksProcessorClient < HttpClientType > {
109
+ client : HttpClientType ,
110
+ consume_built_block_method : & ' static str ,
111
+ }
112
+
113
+ impl BlocksProcessorClient < HttpClient > {
68
114
pub fn try_from ( url : & str ) -> eyre:: Result < Self > {
69
115
Ok ( Self {
70
- client : Arc :: new ( http_provider ( url. parse ( ) ?) ) ,
116
+ client : HttpClientBuilder :: default ( ) . build ( url) ?,
117
+ consume_built_block_method : DEFAULT_BLOCK_CONSUME_BUILT_BLOCK_METHOD ,
71
118
} )
72
119
}
120
+ }
73
121
122
+ impl < HttpClientType : ClientT > BlocksProcessorClient < HttpClientType > {
123
+ pub fn new ( client : HttpClientType , consume_built_block_method : & ' static str ) -> Self {
124
+ Self {
125
+ client,
126
+ consume_built_block_method,
127
+ }
128
+ }
74
129
pub async fn submit_built_block (
75
130
& self ,
76
131
sealed_block : & SealedBlock ,
@@ -115,7 +170,7 @@ impl BlocksProcessorClient {
115
170
116
171
let used_share_bundles = Self :: get_used_sbundles ( built_block_trace) ;
117
172
118
- let params = (
173
+ let params: ConsumeBuiltBlockRequest = (
119
174
header,
120
175
closed_at,
121
176
sealed_at,
@@ -127,58 +182,47 @@ impl BlocksProcessorClient {
127
182
built_block_trace. true_bid_value ,
128
183
best_bid_value,
129
184
) ;
130
-
185
+ let request = ConsumeBuiltBlockRequestArc :: new ( params ) ;
131
186
match self
132
187
. client
133
- . raw_request ( "block_consumeBuiltBlockV2" . into ( ) , & params )
188
+ . request ( self . consume_built_block_method , request . clone ( ) )
134
189
. await
135
190
{
136
191
Ok ( ( ) ) => { }
137
192
Err ( err) => {
138
- match & err {
139
- RpcError :: ErrorResp ( err) => {
140
- error ! ( err = ?err, "Block processor returned error" ) ;
141
- store_error_event (
142
- BLOCK_PROCESSOR_ERROR_CATEGORY ,
143
- & err. to_string ( ) ,
144
- & params,
145
- ) ;
146
- }
147
- RpcError :: SerError ( err) => {
148
- error ! ( err = ?err, "Failed to serialize block processor request" ) ;
149
- }
150
- RpcError :: DeserError { err, text } => {
151
- if !( text. contains ( "504 Gateway Time-out" )
152
- || text. contains ( "502 Bad Gateway" ) )
153
- {
154
- error ! ( err = ?err, "Failed to deserialize block processor response" ) ;
155
- store_error_event (
156
- BLOCK_PROCESSOR_ERROR_CATEGORY ,
157
- & err. to_string ( ) ,
158
- & params,
159
- ) ;
160
- }
161
- }
162
- RpcError :: Transport ( err) => {
163
- debug ! ( err = ?err, "Failed to send block processor request" ) ;
164
- }
165
- RpcError :: NullResp => {
166
- error ! ( "Block processor returned null response" ) ;
167
- }
168
- RpcError :: UnsupportedFeature ( err) => {
169
- error ! ( err = ?err, "Unsupported feature" ) ;
170
- }
171
- RpcError :: LocalUsageError ( err) => {
172
- error ! ( err = ?err, "Local usage error" ) ;
173
- }
174
- }
193
+ Self :: handle_rpc_error ( & err, request. as_ref ( ) ) ;
175
194
return Err ( err. into ( ) ) ;
176
195
}
177
196
}
178
197
179
198
Ok ( ( ) )
180
199
}
181
200
201
+ /// T is parametric just because it too BIG to type
202
+ fn handle_rpc_error ( err : & jsonrpsee:: core:: Error , request : & ConsumeBuiltBlockRequest ) {
203
+ match err {
204
+ jsonrpsee:: core:: Error :: Call ( error_object) => {
205
+ error ! ( err = ?error_object, "Block processor returned error" ) ;
206
+ store_error_event ( BLOCK_PROCESSOR_ERROR_CATEGORY , & err. to_string ( ) , request) ;
207
+ }
208
+ jsonrpsee:: core:: Error :: Transport ( _) => {
209
+ debug ! ( err = ?err, "Failed to send block processor request" ) ;
210
+ }
211
+ jsonrpsee:: core:: Error :: ParseError ( error) => {
212
+ error ! ( err = ?err, "Failed to deserialize block processor response" ) ;
213
+ let error_txt = error. to_string ( ) ;
214
+ if !( error_txt. contains ( "504 Gateway Time-out" )
215
+ || error_txt. contains ( "502 Bad Gateway" ) )
216
+ {
217
+ store_error_event ( BLOCK_PROCESSOR_ERROR_CATEGORY , & err. to_string ( ) , request) ;
218
+ }
219
+ }
220
+ _ => {
221
+ error ! ( err = ?err, "Block processor error" ) ;
222
+ }
223
+ }
224
+ }
225
+
182
226
/// Gets the UsedSbundle carefully considering virtual orders formed by other original orders.
183
227
fn get_used_sbundles ( built_block_trace : & BuiltBlockTrace ) -> Vec < UsedSbundle > {
184
228
built_block_trace
@@ -229,17 +273,19 @@ impl BlocksProcessorClient {
229
273
230
274
/// BidObserver sending all data to a BlocksProcessorClient
231
275
#[ derive( Debug ) ]
232
- pub struct BlocksProcessorClientBidObserver {
233
- client : BlocksProcessorClient ,
276
+ pub struct BlocksProcessorClientBidObserver < HttpClientType > {
277
+ client : BlocksProcessorClient < HttpClientType > ,
234
278
}
235
279
236
- impl BlocksProcessorClientBidObserver {
237
- pub fn new ( client : BlocksProcessorClient ) -> Self {
280
+ impl < HttpClientType > BlocksProcessorClientBidObserver < HttpClientType > {
281
+ pub fn new ( client : BlocksProcessorClient < HttpClientType > ) -> Self {
238
282
Self { client }
239
283
}
240
284
}
241
285
242
- impl BidObserver for BlocksProcessorClientBidObserver {
286
+ impl < HttpClientType : ClientT + Clone + Send + Sync + std:: fmt:: Debug + ' static > BidObserver
287
+ for BlocksProcessorClientBidObserver < HttpClientType >
288
+ {
243
289
fn block_submitted (
244
290
& self ,
245
291
sealed_block : SealedBlock ,
0 commit comments