This repository was archived by the owner on Aug 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathrpc.rs
More file actions
462 lines (405 loc) · 17 KB
/
Copy pathrpc.rs
File metadata and controls
462 lines (405 loc) · 17 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use alloy_primitives::{Address, Bytes, StorageKey, Uint, U256};
use alloy_provider::{ProviderBuilder, ReqwestProvider, RootProvider};
use alloy_rpc_client::{ClientBuilder, RpcClient};
use alloy_rpc_types::{Block, BlockId, BlockNumberOrTag, EIP1186AccountProofResponse};
use alloy_transport_http::Http;
use raiko_lib::clear_line;
use reqwest_alloy::Client;
use reth_primitives::revm_primitives::{AccountInfo, Bytecode};
use std::{collections::HashMap, env, time::Duration};
use tracing::debug;
use crate::{
interfaces::{RaikoError, RaikoResult},
provider::BlockDataProvider,
MerkleProof,
};
/// Env: total per-request timeout for L1/L2 JSON-RPC (connect + send + response body).
/// Default 300s so large `eth_getProof` / batch calls can finish on slow nodes.
const ENV_RPC_HTTP_TIMEOUT_SECS: &str = "RAIKO_RPC_HTTP_TIMEOUT_SECS";
/// Env: TCP connect timeout only. Default 30s.
const ENV_RPC_HTTP_CONNECT_TIMEOUT_SECS: &str = "RAIKO_RPC_HTTP_CONNECT_TIMEOUT_SECS";
fn build_rpc_reqwest_client() -> RaikoResult<Client> {
let timeout_secs: u64 = env::var(ENV_RPC_HTTP_TIMEOUT_SECS)
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(300);
let connect_secs: u64 = env::var(ENV_RPC_HTTP_CONNECT_TIMEOUT_SECS)
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(30);
Client::builder()
.timeout(Duration::from_secs(timeout_secs))
.connect_timeout(Duration::from_secs(connect_secs))
.build()
.map_err(|e| RaikoError::RPC(format!("Failed to build RPC HTTP client: {e}")))
}
fn rpc_http_transport(url: reqwest::Url) -> RaikoResult<(Http<Client>, bool)> {
let client = build_rpc_reqwest_client()?;
let http = Http::with_client(client, url);
let is_local = http.guess_local();
Ok((http, is_local))
}
#[derive(Clone)]
pub struct RpcBlockDataProvider {
pub provider: ReqwestProvider,
pub client: RpcClient<Http<Client>>,
block_numbers: Vec<u64>,
}
impl RpcBlockDataProvider {
/// async will be used for future preflight optimization
pub async fn new(url: &str, block_number: u64) -> RaikoResult<Self> {
let url =
reqwest::Url::parse(url).map_err(|_| RaikoError::RPC("Invalid RPC URL".to_owned()))?;
debug!(
"provider rpc url: {:?} for block_number {}",
url, block_number
);
let (http, is_local) = rpc_http_transport(url)?;
let rpc_client = RpcClient::new(http.clone(), is_local);
Ok(Self {
provider: ProviderBuilder::new().on_provider(RootProvider::new(rpc_client)),
client: ClientBuilder::default().transport(http, is_local),
block_numbers: vec![block_number, block_number + 1],
})
}
pub async fn new_batch(url: &str, block_numbers: Vec<u64>) -> RaikoResult<Self> {
assert!(
!block_numbers.is_empty() && block_numbers.len() > 1,
"batch block_numbers should have at least 2 elements"
);
let url =
reqwest::Url::parse(url).map_err(|_| RaikoError::RPC("Invalid RPC URL".to_owned()))?;
debug!(
"Batch provider rpc: {:?} for block_number {}",
url, block_numbers[0]
);
let (http, is_local) = rpc_http_transport(url)?;
let rpc_client = RpcClient::new(http.clone(), is_local);
Ok(Self {
provider: ProviderBuilder::new().on_provider(RootProvider::new(rpc_client)),
client: ClientBuilder::default().transport(http, is_local),
block_numbers,
})
}
pub fn provider(&self) -> &ReqwestProvider {
&self.provider
}
}
impl BlockDataProvider for RpcBlockDataProvider {
async fn get_blocks(&self, blocks_to_fetch: &[(u64, bool)]) -> RaikoResult<Vec<Block>> {
let mut all_blocks = Vec::with_capacity(blocks_to_fetch.len());
let max_batch_size = 32;
for blocks_to_fetch in blocks_to_fetch.chunks(max_batch_size) {
let mut batch = self.client.new_batch();
let mut requests = Vec::with_capacity(max_batch_size);
for (block_number, full) in blocks_to_fetch {
requests.push(Box::pin(
batch
.add_call(
"eth_getBlockByNumber",
&(BlockNumberOrTag::from(*block_number), full),
)
.map_err(|_| {
RaikoError::RPC(
"Failed adding eth_getBlockByNumber call to batch".to_owned(),
)
})?,
));
}
batch.send().await.map_err(|e| {
RaikoError::RPC(format!(
"Error sending batch request for block {blocks_to_fetch:?}: {e}"
))
})?;
let mut blocks = Vec::with_capacity(max_batch_size);
// Collect the data from the batch
for request in requests {
blocks.push(
request.await.map_err(|e| {
RaikoError::RPC(format!("Error collecting request data: {e}"))
})?,
);
}
all_blocks.append(&mut blocks);
}
Ok(all_blocks)
}
async fn get_accounts(
&self,
block_number: u64,
accounts: &[Address],
) -> RaikoResult<Vec<AccountInfo>> {
assert!(
self.block_numbers.contains(&block_number),
"Block number {} not found in {:?}",
block_number,
self.block_numbers
);
let mut all_accounts = Vec::with_capacity(accounts.len());
let max_batch_size = 250;
for accounts in accounts.chunks(max_batch_size) {
let mut batch = self.client.new_batch();
let mut nonce_requests = Vec::with_capacity(max_batch_size);
let mut balance_requests = Vec::with_capacity(max_batch_size);
let mut code_requests = Vec::with_capacity(max_batch_size);
for address in accounts {
nonce_requests.push(Box::pin(
batch
.add_call::<_, Uint<64, 1>>(
"eth_getTransactionCount",
&(address, Some(BlockId::from(block_number))),
)
.map_err(|_| {
RaikoError::RPC(
"Failed adding eth_getTransactionCount call to batch".to_owned(),
)
})?,
));
balance_requests.push(Box::pin(
batch
.add_call::<_, Uint<256, 4>>(
"eth_getBalance",
&(address, Some(BlockId::from(block_number))),
)
.map_err(|_| {
RaikoError::RPC("Failed adding eth_getBalance call to batch".to_owned())
})?,
));
code_requests.push(Box::pin(
batch
.add_call::<_, Bytes>(
"eth_getCode",
&(address, Some(BlockId::from(block_number))),
)
.map_err(|_| {
RaikoError::RPC("Failed adding eth_getCode call to batch".to_owned())
})?,
));
}
batch
.send()
.await
.map_err(|e| RaikoError::RPC(format!("Error sending batch request {e}")))?;
let mut accounts = vec![];
// Collect the data from the batch
for ((nonce_request, balance_request), code_request) in nonce_requests
.into_iter()
.zip(balance_requests.into_iter())
.zip(code_requests.into_iter())
{
let (nonce, balance, code) = (
nonce_request.await.map_err(|e| {
RaikoError::RPC(format!("Failed to collect nonce request: {e}"))
})?,
balance_request.await.map_err(|e| {
RaikoError::RPC(format!("Failed to collect balance request: {e}"))
})?,
code_request.await.map_err(|e| {
RaikoError::RPC(format!("Failed to collect code request: {e}"))
})?,
);
let nonce = nonce.try_into().map_err(|_| {
RaikoError::Conversion("Failed to convert nonce to u64".to_owned())
})?;
let bytecode = Bytecode::new_raw(code);
let account_info = AccountInfo::new(balance, nonce, bytecode.hash_slow(), bytecode);
accounts.push(account_info);
}
all_accounts.append(&mut accounts);
}
Ok(all_accounts)
}
async fn get_storage_values(
&self,
block_number: u64,
accounts: &[(Address, U256)],
) -> RaikoResult<Vec<U256>> {
assert!(
self.block_numbers.contains(&block_number),
"Block number {} not found in {:?}",
block_number,
self.block_numbers
);
let mut all_values = Vec::with_capacity(accounts.len());
let max_batch_size = 1000;
for accounts in accounts.chunks(max_batch_size) {
let mut batch = self.client.new_batch();
let mut requests = Vec::with_capacity(max_batch_size);
for (address, key) in accounts {
requests.push(Box::pin(
batch
.add_call::<_, U256>(
"eth_getStorageAt",
&(address, key, Some(BlockId::from(block_number))),
)
.map_err(|_| {
RaikoError::RPC(
"Failed adding eth_getStorageAt call to batch".to_owned(),
)
})?,
));
}
batch
.send()
.await
.map_err(|e| RaikoError::RPC(format!("Error sending batch request {e}")))?;
let mut values = Vec::with_capacity(max_batch_size);
// Collect the data from the batch
for request in requests {
values.push(
request.await.map_err(|e| {
RaikoError::RPC(format!("Error collecting request data: {e}"))
})?,
);
}
all_values.append(&mut values);
}
Ok(all_values)
}
async fn get_merkle_proofs(
&self,
block_number: u64,
accounts: HashMap<Address, Vec<U256>>,
offset: usize,
num_storage_proofs: usize,
) -> RaikoResult<MerkleProof> {
assert!(
self.block_numbers.contains(&block_number),
"Block number {} not found in {:?}",
block_number,
self.block_numbers
);
let mut storage_proofs: MerkleProof = HashMap::new();
let mut idx = offset;
let mut accounts = accounts.clone();
let batch_limit = 1000;
while !accounts.is_empty() {
#[cfg(debug_assertions)]
raiko_lib::inplace_print(&format!(
"fetching storage proof {idx}/{num_storage_proofs}..."
));
#[cfg(not(debug_assertions))]
tracing::trace!("Fetching storage proof {idx}/{num_storage_proofs}...");
// Create a batch for all storage proofs
let mut batch = self.client.new_batch();
// Collect all requests
let mut requests = Vec::new();
let mut batch_size = 0;
while !accounts.is_empty() && batch_size < batch_limit {
let mut address_to_remove = None;
if let Some((address, keys)) = accounts.iter_mut().next() {
// Calculate how many keys we can still process
let num_keys_to_process = if batch_size + keys.len() < batch_limit {
keys.len()
} else {
batch_limit - batch_size
};
// If we can process all keys, remove the address from the map after the loop
if num_keys_to_process == keys.len() {
address_to_remove = Some(*address);
}
// Extract the keys to process
let keys_to_process = keys
.drain(0..num_keys_to_process)
.map(StorageKey::from)
.collect::<Vec<_>>();
// Add the request
requests.push(Box::pin(
batch
.add_call::<_, EIP1186AccountProofResponse>(
"eth_getProof",
&(
*address,
keys_to_process.clone(),
BlockId::from(block_number),
),
)
.map_err(|_| {
RaikoError::RPC(
"Failed adding eth_getProof call to batch".to_owned(),
)
})?,
));
// Keep track of how many keys were processed
// Add an additional 1 for the account proof itself
batch_size += 1 + keys_to_process.len();
}
// Remove the address if all keys were processed for this account
if let Some(address) = address_to_remove {
accounts.remove(&address);
}
}
// Send the batch
batch
.send()
.await
.map_err(|e| RaikoError::RPC(format!("Error sending batch request {e}")))?;
// Collect the data from the batch
for request in requests {
let mut proof = request
.await
.map_err(|e| RaikoError::RPC(format!("Error collecting request data: {e}")))?;
idx += proof.storage_proof.len();
if let Some(map_proof) = storage_proofs.get_mut(&proof.address) {
map_proof.storage_proof.append(&mut proof.storage_proof);
} else {
storage_proofs.insert(proof.address, proof);
}
}
}
clear_line();
Ok(storage_proofs)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use tokio::io::AsyncReadExt;
use tokio::net::TcpListener;
fn set_short_rpc_timeouts_for_test() {
std::env::set_var(ENV_RPC_HTTP_TIMEOUT_SECS, "1");
std::env::set_var(ENV_RPC_HTTP_CONNECT_TIMEOUT_SECS, "1");
}
fn clear_rpc_timeout_env() {
std::env::remove_var(ENV_RPC_HTTP_TIMEOUT_SECS);
std::env::remove_var(ENV_RPC_HTTP_CONNECT_TIMEOUT_SECS);
}
/// Local TCP server: read the JSON-RPC POST then stall so the client hits `reqwest` timeout.
async fn spawn_stall_after_accept_json_rpc() -> String {
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind test listener");
let addr = listener.local_addr().expect("listener addr");
tokio::spawn(async move {
if let Ok((mut stream, _)) = listener.accept().await {
let mut buf = [0u8; 8192];
let _ = stream.read(&mut buf).await;
tokio::time::sleep(Duration::from_secs(600)).await;
}
});
format!("http://{addr}")
}
#[tokio::test]
#[serial]
async fn get_blocks_returns_rpc_error_when_http_times_out() {
set_short_rpc_timeouts_for_test();
let url = spawn_stall_after_accept_json_rpc().await;
let provider = RpcBlockDataProvider::new(&url, 1)
.await
.expect("provider new with short timeout");
let result = provider.get_blocks(&[(1, false)]).await;
clear_rpc_timeout_env();
let err = result.expect_err("expected RPC failure when server does not respond in time");
let RaikoError::RPC(payload) = &err else {
panic!("expected RaikoError::RPC, got {err:?}");
};
let lower = payload.to_lowercase();
// Reqwest may report `operation timed out` or a generic `error sending request for url (...)`
// when the overall request timeout fires.
assert!(
lower.contains("timeout")
|| lower.contains("timed out")
|| payload.contains("error sending request for url"),
"expected timeout or stalled-request error, got: {err}"
);
}
}