-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.rs
More file actions
721 lines (642 loc) · 27.6 KB
/
main.rs
File metadata and controls
721 lines (642 loc) · 27.6 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Debug/Trace RPC Server
//!
//! # Overview
//! A standalone RPC server for `debug_*` and `trace_*` methods using stateless execution.
//! Data can be fetched from upstream RPC endpoints or from a local database with chain sync.
//!
//! # Architecture
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ RPC Server │
//! │ Receives external requests, invokes executor, returns traces │
//! │ ┌─────────────────────────────────────────────────────────┐ │
//! │ │ HTTP Response Cache │ │
//! │ │ Caches pre-serialized JSON responses (quick_cache) │ │
//! │ └─────────────────────────────────────────────────────────┘ │
//! └─────────────────────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ Stateless Executor │
//! │ Replays blocks using witness data to generate transaction traces│
//! └─────────────────────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ DataProvider │
//! │ Multi-level lookup: Local DB → Remote RPC (with single-flight) │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Supported RPC Methods
//! - `debug_traceBlockByNumber` - Trace block execution by block number
//! - `debug_traceBlockByHash` - Trace block execution by block hash
//! - `debug_traceTransaction` - Trace a single transaction execution
//! - `trace_block` - Parity-style block tracing (flat call traces)
//! - `trace_transaction` - Parity-style transaction tracing
//! - `debug_getCacheStatus` - Query current response cache status
//!
//! # Operating Modes
//! - **Stateless mode**: Without `data_dir`, all data is fetched from remote RPC
//! - **Local cache mode**: With `data_dir`, enables chain sync to pre-fetch blocks into local DB
use std::{path::PathBuf, sync::Arc};
use alloy_genesis::Genesis;
use alloy_primitives::BlockHash;
use alloy_rpc_types_eth::BlockId;
use clap::Parser;
use eyre::Result;
use jsonrpsee::server::{Server, ServerConfig};
use stateless_common::{RpcClient, RpcClientConfig, logging::LogArgs};
use stateless_core::{
BlockStore, ChainStore, ContractStore, PipelineConfig, chain_spec::ChainSpec, db::BlockMeta,
pipeline::run_pipeline,
};
use stateless_db::ContractCache;
use tokio::task;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, instrument, warn};
mod chain_sync;
mod data_provider;
mod metrics;
mod response_cache;
mod response_size;
mod rpc_service;
mod server_db;
mod timing;
mod tracing_executor;
use data_provider::{DataProvider, NoopContractStore};
use response_cache::{DEFAULT_RESPONSE_CACHE_ESTIMATED_ITEMS, ResponseCache, ResponseCacheConfig};
use rpc_service::RpcContext;
use server_db::ServerDB;
use crate::chain_sync::{TraceFetcher, TraceHooks, TraceProcessor};
/// Command line arguments for the debug-trace-server.
#[derive(Parser, Debug)]
#[clap(name = "debug-trace-server", about = "Debug/Trace RPC Server")]
struct Args {
/// RPC server listen address.
#[clap(long, env = "DEBUG_TRACE_SERVER_ADDR", default_value = "0.0.0.0:8545")]
addr: String,
/// One or more upstream RPC endpoint URLs for fetching blockchain data (tried in order).
/// Accepts repeated flags (`--rpc-endpoint a --rpc-endpoint b`) or a comma-separated
/// list (`--rpc-endpoint a,b`, also via the env var).
#[clap(
long,
env = "DEBUG_TRACE_SERVER_RPC_ENDPOINT",
required = true,
value_delimiter = ',',
action = clap::ArgAction::Append,
)]
rpc_endpoint: Vec<String>,
/// One or more upstream witness endpoint URLs for fetching witness data (tried in order).
/// Accepts repeated flags (`--witness-endpoint a --witness-endpoint b`) or a comma-separated
/// list (`--witness-endpoint a,b`, also via the env var).
#[clap(
long,
env = "DEBUG_TRACE_SERVER_WITNESS_ENDPOINT",
required = true,
value_delimiter = ',',
action = clap::ArgAction::Append,
)]
witness_endpoint: Vec<String>,
/// Enable Prometheus metrics exporter.
#[clap(long, env = "DEBUG_TRACE_SERVER_METRICS_ENABLED")]
metrics_enabled: bool,
/// Port for Prometheus metrics HTTP endpoint.
#[clap(
long,
env = "DEBUG_TRACE_SERVER_METRICS_PORT",
default_value_t = metrics::DEFAULT_METRICS_PORT
)]
metrics_port: u16,
/// Path to genesis JSON file.
#[clap(long, env = "DEBUG_TRACE_SERVER_GENESIS_FILE")]
genesis_file: Option<String>,
/// Data directory path for local database and chain sync.
#[clap(long, env = "DEBUG_TRACE_SERVER_DATA_DIR")]
data_dir: Option<String>,
/// Trusted starting block hash for chain initialization.
#[clap(long, env = "DEBUG_TRACE_SERVER_START_BLOCK")]
start_block: Option<String>,
/// Witness fetch timeout in seconds.
#[clap(
long,
env = "DEBUG_TRACE_SERVER_WITNESS_TIMEOUT",
default_value_t = data_provider::DEFAULT_WITNESS_TIMEOUT_SECS
)]
witness_timeout: u64,
/// Total block-fetch timeout in seconds (header + witness + block + contracts).
/// Bounds `RpcClient`'s unbounded retry loop so deterministic upstream errors
/// (e.g. requesting a nonexistent block) surface instead of hanging the client.
#[clap(
long,
env = "DEBUG_TRACE_SERVER_BLOCK_FETCH_TIMEOUT_SECS",
default_value_t = data_provider::DEFAULT_BLOCK_FETCH_TIMEOUT_SECS
)]
block_fetch_timeout: u64,
/// Maximum memory for response cache (e.g., "1GB", "512MB", "1024").
#[clap(
long,
env = "DEBUG_TRACE_SERVER_RESPONSE_CACHE_MAX_SIZE",
default_value = "1GB",
value_parser = parse_size,
)]
response_cache_max_size: u64,
/// Estimated number of items in response cache (for initial capacity).
#[clap(
long,
env = "DEBUG_TRACE_SERVER_RESPONSE_CACHE_ESTIMATED_ITEMS",
default_value_t = DEFAULT_RESPONSE_CACHE_ESTIMATED_ITEMS
)]
response_cache_estimated_items: usize,
/// Number of recent blocks to retain in database (older blocks are pruned).
#[clap(
long,
env = "DEBUG_TRACE_SERVER_BLOCKS_TO_KEEP",
default_value_t = DEFAULT_BLOCKS_TO_KEEP
)]
blocks_to_keep: u64,
/// Maximum database file size before additional pruning triggers (e.g., "10GB", "512MB").
/// Set to "0" to disable size-based pruning (only block-count pruning applies).
#[clap(
long,
env = "DEBUG_TRACE_SERVER_DB_MAX_SIZE",
default_value = "0",
value_parser = parse_size,
)]
db_max_size: u64,
/// Interval between database pruning cycles in seconds.
#[clap(
long,
env = "DEBUG_TRACE_SERVER_PRUNER_INTERVAL_SECS",
default_value_t = DEFAULT_PRUNER_INTERVAL_SECS
)]
pruner_interval_secs: u64,
/// Maximum concurrent in-flight data-endpoint requests (blocks, headers, code, tx).
/// Omit for unlimited.
#[clap(long, env = "DEBUG_TRACE_SERVER_DATA_MAX_CONCURRENT_REQUESTS")]
data_max_concurrent_requests: Option<usize>,
/// Maximum concurrent in-flight witness fetches, independent of the data cap.
/// Omit for unlimited.
#[clap(long, env = "DEBUG_TRACE_SERVER_WITNESS_MAX_CONCURRENT_REQUESTS")]
witness_max_concurrent_requests: Option<usize>,
/// Per-attempt RPC timeout (milliseconds). Bounds every individual provider attempt
/// even when the caller passes no overall deadline (e.g. background chain sync), so
/// a provider that accepts the TCP connection but never replies is detected and
/// rotated past instead of wedging the retry loop.
#[clap(long, env = "DEBUG_TRACE_SERVER_RPC_PER_ATTEMPT_TIMEOUT_MS")]
rpc_per_attempt_timeout_ms: Option<u64>,
/// Logging configuration.
#[command(flatten)]
log: LogArgs,
}
/// Database filename for the trace server's local storage.
const TRACE_SERVER_DB_FILENAME: &str = "trace_server.redb";
/// Default number of blocks to keep in database.
const DEFAULT_BLOCKS_TO_KEEP: u64 = 1000;
/// Default pruner interval in seconds (5 minutes).
const DEFAULT_PRUNER_INTERVAL_SECS: u64 = 300;
/// Parses a human-readable size string into bytes.
///
/// Accepts suffixes: `KB` (1024), `MB` (1024²), `GB` (1024³). Case-insensitive.
/// Plain numbers are treated as raw bytes.
///
/// # Examples
/// ```text
/// "1GB" -> 1_073_741_824
/// "512MB" -> 536_870_912
/// "100KB" -> 102_400
/// "1024" -> 1_024
/// ```
fn parse_size(s: &str) -> Result<u64, String> {
let s = s.trim();
let upper = s.to_uppercase();
let (num_str, multiplier) = if let Some(n) = upper.strip_suffix("GB") {
(n, 1024u64 * 1024 * 1024)
} else if let Some(n) = upper.strip_suffix("MB") {
(n, 1024u64 * 1024)
} else if let Some(n) = upper.strip_suffix("KB") {
(n, 1024u64)
} else {
(upper.as_str(), 1u64)
};
let value: u64 = num_str.trim().parse().map_err(|e| format!("invalid size '{}': {}", s, e))?;
value.checked_mul(multiplier).ok_or_else(|| format!("size overflow: '{}'", s))
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
let _log_guard = args.log.init_tracing()?;
info!(
listen_addr = %args.addr,
"Debug-trace-server starting"
);
let response_cache_disabled = args.response_cache_estimated_items == 0;
debug!(
rpc_endpoints = ?args.rpc_endpoint,
witness_endpoints = ?args.witness_endpoint,
witness_timeout_secs = args.witness_timeout,
response_cache_disabled,
response_cache_max_size = args.response_cache_max_size,
response_cache_estimated_items = args.response_cache_estimated_items,
"Server configuration"
);
// Initialize metrics
if args.metrics_enabled {
let metrics_addr = std::net::SocketAddr::from(([0, 0, 0, 0], args.metrics_port));
match metrics::init_metrics(metrics_addr) {
Ok(_) => info!(metrics_port = args.metrics_port, "Metrics enabled"),
Err(e) => {
error!(error = %e, metrics_port = args.metrics_port, "Failed to initialize metrics");
return Err(e);
}
}
} else {
debug!("Metrics disabled");
}
// Initialize components
let data_apis: Vec<&str> = args.rpc_endpoint.iter().map(String::as_str).collect();
let witness_apis: Vec<&str> = args.witness_endpoint.iter().map(String::as_str).collect();
let rpc_defaults = RpcClientConfig::trace_server();
let per_attempt_timeout = args
.rpc_per_attempt_timeout_ms
.map(std::time::Duration::from_millis)
.unwrap_or(rpc_defaults.per_attempt_timeout);
let rpc_config = RpcClientConfig {
data_max_concurrent_requests: args.data_max_concurrent_requests,
witness_max_concurrent_requests: args.witness_max_concurrent_requests,
per_attempt_timeout,
..rpc_defaults
}
.with_metrics(Arc::new(metrics::TraceRpcMetrics));
let rpc_client =
Arc::new(RpcClient::new_with_config(&data_apis, &witness_apis, rpc_config, None)?);
let validator_db = init_validator_db(&args, &rpc_client).await?;
// Keep concrete ServerDB for pipeline (needs Sized), and dyn BlockStore for data_provider
let server_db: Option<Arc<ServerDB>> = validator_db;
let block_store: Option<Arc<dyn BlockStore>> =
server_db.as_ref().map(|db| Arc::clone(db) as Arc<dyn BlockStore>);
// Contract cache: local-cache mode writes through to ServerDB, stateless mode is
// memory-only via `NoopContractStore`. Either way every RPC-fetched contract is
// cached for the lifetime of the process, so repeated trace requests skip RPC.
let contract_store: Arc<dyn ContractStore> = match server_db.as_ref() {
Some(db) => Arc::clone(db) as Arc<dyn ContractStore>,
None => Arc::new(NoopContractStore),
};
let contract_cache = Arc::new(ContractCache::new(contract_store));
let data_provider = Arc::new(DataProvider::new(
rpc_client.clone(),
block_store.clone(),
contract_cache,
args.witness_timeout,
args.block_fetch_timeout,
));
let chain_spec = load_chain_spec(&args)?;
let response_cache = if response_cache_disabled {
info!("Response cache disabled (estimated_items = 0)");
None
} else {
let cache = ResponseCache::new(ResponseCacheConfig::new(
args.response_cache_max_size,
args.response_cache_estimated_items,
));
debug!(
max_bytes = args.response_cache_max_size,
estimated_items = args.response_cache_estimated_items,
"Response cache initialized"
);
Some(cache)
};
// Spawn background chain sync pipeline (if database is configured)
if let Some(db) = &server_db {
let shutdown = CancellationToken::new();
debug!("Starting chain sync pipeline");
// Spawn unified pipeline (fetch → process → advance with reorg restart).
// `#[non_exhaustive]` on `PipelineConfig` rules out struct-update syntax across
// the crate boundary; mutate a default instance instead.
let mut pipeline_cfg = PipelineConfig::default();
pipeline_cfg.concurrent_workers = 1;
pipeline_cfg.stale_reset_threshold = Some(args.blocks_to_keep);
let config = Arc::new(pipeline_cfg);
let processor = Arc::new(TraceProcessor);
let hooks = Arc::new(TraceHooks::new(
Arc::clone(db) as Arc<dyn BlockStore>,
response_cache.clone(),
));
let fetcher = Arc::new(TraceFetcher { rpc_client: Arc::clone(&rpc_client) });
task::spawn({
let db = Arc::clone(db);
let shutdown = shutdown.clone();
async move {
if let Err(e) = run_pipeline(fetcher, db, processor, hooks, config, shutdown).await
{
error!(error = %e, "Chain sync pipeline exited with error");
}
}
});
// Spawn history pruner to prevent unbounded database growth
let db_path =
PathBuf::from(args.data_dir.as_deref().unwrap()).join(TRACE_SERVER_DB_FILENAME);
let pruner_metrics = metrics::ChainSyncMetrics::create();
task::spawn({
let db = Arc::clone(db);
async move {
if let Err(e) = history_pruner(
db,
args.blocks_to_keep,
args.pruner_interval_secs,
args.db_max_size,
db_path,
pruner_metrics,
)
.await
{
error!(error = %e, "History pruner exited with error");
}
}
});
}
// Create RPC context and module
let ctx = RpcContext::new(data_provider, chain_spec, response_cache);
// Spawn watch dog checker to monitor long-running requests
let watch_dog = ctx.watch_dog().clone();
task::spawn(async move {
watch_dog
.run_checker(
std::time::Duration::from_secs(5), // check interval
std::time::Duration::from_secs(15), // warn threshold
)
.await;
});
let module = ctx.into_rpc_module()?;
// Start server
let config = ServerConfig::builder().max_response_body_size(u32::MAX).build();
let server = Server::builder()
.set_config(config)
.set_http_middleware(
tower::ServiceBuilder::new()
.layer(response_size::ResponseSizeLayer)
.layer(timing::TimingHeaderLayer),
)
.build(&args.addr)
.await?;
let addr = server.local_addr()?;
let handle = server.start(module);
info!(listen_addr = %addr, "Server started");
handle.stopped().await;
Ok(())
}
/// Initializes the validator database if data_dir is provided.
/// Returns the database if configured, None otherwise.
/// Note: Chain tracker is spawned separately in main() to allow passing the response cache
/// callback.
#[instrument(skip_all, name = "init_db")]
async fn init_validator_db(
args: &Args,
rpc_client: &Arc<RpcClient>,
) -> Result<Option<Arc<ServerDB>>> {
let Some(data_dir) = &args.data_dir else {
debug!("Running in stateless mode, no local database");
return Ok(None);
};
debug!(data_dir = %data_dir, "Initializing local database");
let work_dir = PathBuf::from(data_dir);
std::fs::create_dir_all(&work_dir)
.map_err(|e| eyre::eyre!("Failed to create data dir {}: {e}", work_dir.display()))?;
let db = Arc::new(ServerDB::new(work_dir.join(TRACE_SERVER_DB_FILENAME))?);
// Check if we already have a local tip
if db.get_local_tip()?.is_some() {
debug!("Continuing from existing canonical chain");
return Ok(Some(db));
}
// No local tip - need to initialize anchor block
// Use explicit start_block if provided, otherwise fetch latest.
//
// `get_header` retries transient failures forever at the RPC layer; the binary stays
// stuck here until the endpoint is reachable — a permanent misconfiguration surfaces via
// the "no forward progress" signal rather than a bounded-retry error.
let header = if let Some(start_block_str) = &args.start_block {
debug!(start_block = %start_block_str, "Initializing from specified start block");
let block_hash: BlockHash = start_block_str.parse()?;
rpc_client.get_header(BlockId::Hash(block_hash.into()), false).await
} else {
info!("No local tip found, fetching latest block as anchor");
rpc_client.get_header(BlockId::latest(), false).await
};
let anchor = BlockMeta {
block_number: header.number,
block_hash: header.hash,
post_state_root: header.state_root,
post_withdrawals_root: header
.withdrawals_root
.ok_or_else(|| eyre::eyre!("Block {} is missing withdrawals_root", header.hash))?,
};
ChainStore::reset_to_anchor(&*db, &anchor)
.map_err(|e| eyre::eyre!("Failed to reset anchor: {}", e))?;
info!(
block_hash = %header.hash,
block_number = header.number,
"Anchor block initialized"
);
Ok(Some(db))
}
/// Loads the chain specification from genesis file or uses default.
#[instrument(skip_all, name = "load_chain_spec")]
fn load_chain_spec(args: &Args) -> Result<Arc<ChainSpec>> {
if let Some(genesis_path) = &args.genesis_file {
debug!(genesis_file = %genesis_path, "Loading genesis from file");
let genesis_content = std::fs::read_to_string(genesis_path)?;
let genesis: Genesis = serde_json::from_str(&genesis_content)?;
Ok(Arc::new(ChainSpec::from_genesis(genesis)))
} else {
debug!("Using default chain spec");
Ok(Arc::new(ChainSpec::default()))
}
}
/// Background task that periodically prunes old block data to prevent unbounded database growth.
///
/// Runs in an infinite loop, removing blocks older than `blocks_to_keep` from the current tip.
/// If `db_max_size > 0`, also prunes additional blocks when the DB file exceeds that size.
#[instrument(skip_all, name = "history_pruner")]
async fn history_pruner(
validator_db: Arc<dyn BlockStore>,
blocks_to_keep: u64,
interval_secs: u64,
db_max_size: u64,
db_path: PathBuf,
chain_sync_metrics: metrics::ChainSyncMetrics,
) -> Result<()> {
let interval = std::time::Duration::from_secs(interval_secs);
info!(
blocks_to_keep = blocks_to_keep,
interval_secs = interval_secs,
db_max_size = db_max_size,
"Starting history pruner"
);
/// Number of extra blocks to prune per iteration when DB file is over size limit.
const SIZE_PRUNE_BATCH: u64 = 100;
loop {
if let Ok(Some(tip)) = validator_db.get_canonical_tip() {
let current_tip = tip.block_number;
let mut prune_before = current_tip.saturating_sub(blocks_to_keep);
match validator_db.prune_chain(prune_before) {
Ok(blocks_pruned) if blocks_pruned > 0 => {
debug!(
blocks_pruned = blocks_pruned,
prune_before = prune_before,
"Pruned old blocks from database"
);
}
Err(e) => warn!(error = %e, "Failed to prune old block data"),
_ => {}
}
// Size-based pruning: keep removing blocks until DB is under the limit
if db_max_size > 0 {
loop {
let file_size = match std::fs::metadata(&db_path) {
Ok(m) => m.len(),
Err(e) => {
warn!(error = %e, "Failed to read DB file size");
break;
}
};
if file_size <= db_max_size {
break;
}
prune_before = prune_before.saturating_add(SIZE_PRUNE_BATCH);
// Don't prune beyond the current tip
if prune_before >= current_tip {
info!(
file_size = file_size,
db_max_size = db_max_size,
"DB still over size limit but no more blocks to prune"
);
break;
}
info!(
file_size = file_size,
db_max_size = db_max_size,
prune_before = prune_before,
"DB over size limit, pruning additional blocks"
);
match validator_db.prune_chain(prune_before) {
Ok(blocks_pruned) if blocks_pruned > 0 => {
debug!(
blocks_pruned = blocks_pruned,
prune_before = prune_before,
"Size-based prune completed"
);
}
Ok(_) => break, // No more blocks to prune
Err(e) => {
warn!(error = %e, "Failed to prune during size-based pruning");
break;
}
}
}
}
// Update DB block range metrics
let earliest =
validator_db.get_earliest_block().ok().flatten().map(|(n, _)| n).unwrap_or(0);
chain_sync_metrics.set_db_block_range(earliest, current_tip);
// Update DB file size metric
if let Ok(m) = std::fs::metadata(&db_path) {
chain_sync_metrics.set_db_size(m.len());
}
}
tokio::time::sleep(interval).await;
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Verifies that an endpoint flag accepts repeated flags, CSV values, and env var —
/// ensuring container deployments configured purely via env are not silently limited
/// to one endpoint (clap's `value_delimiter` applies to env-var values too).
fn assert_endpoint_accepts_multiple_forms(
flag: &str,
env: &str,
base: &[&str],
extract: impl Fn(Args) -> Vec<String>,
) {
let guard = stateless_test_utils::env::env_lock();
let parse =
|extra: &[&str]| extract(Args::try_parse_from(base.iter().chain(extra)).unwrap());
assert_eq!(parse(&[flag, "http://a,http://b"]), ["http://a", "http://b"]);
assert_eq!(
parse(&[flag, "http://a,http://b", flag, "http://c"]),
["http://a", "http://b", "http://c"],
);
let from_env = stateless_test_utils::env::with_env_var(
&guard,
env,
"http://a,http://b",
|| parse(&[]),
);
assert_eq!(from_env, ["http://a", "http://b"]);
}
/// `--rpc-endpoint` accepts repeated flags and CSV values, both on the CLI and via env var,
/// mirroring `--witness-endpoint` behavior for multi-endpoint data RPC support.
#[test]
fn witness_endpoint_accepts_multiple_forms() {
assert_endpoint_accepts_multiple_forms(
"--witness-endpoint",
"DEBUG_TRACE_SERVER_WITNESS_ENDPOINT",
&["debug-trace-server", "--rpc-endpoint", "http://rpc"],
|a| a.witness_endpoint,
);
}
#[test]
fn rpc_endpoint_accepts_multiple_forms() {
assert_endpoint_accepts_multiple_forms(
"--rpc-endpoint",
"DEBUG_TRACE_SERVER_RPC_ENDPOINT",
&["debug-trace-server", "--witness-endpoint", "http://w"],
|a| a.rpc_endpoint,
);
}
/// Verifies a concurrency cap flag parses via CLI and env var, and defaults to `None`.
fn assert_concurrency_flag(
flag: &str,
env: &str,
base: &[&str],
extract: impl Fn(Args) -> Option<usize>,
) {
let guard = stateless_test_utils::env::env_lock();
let parse =
|extra: &[&str]| extract(Args::try_parse_from(base.iter().chain(extra)).unwrap());
assert_eq!(parse(&[]), None);
assert_eq!(parse(&[flag, "7"]), Some(7));
let from_env = stateless_test_utils::env::with_env_var(&guard, env, "12", || parse(&[]));
assert_eq!(from_env, Some(12));
}
#[test]
fn data_max_concurrent_requests_flag_and_env() {
assert_concurrency_flag(
"--data-max-concurrent-requests",
"DEBUG_TRACE_SERVER_DATA_MAX_CONCURRENT_REQUESTS",
&[
"debug-trace-server",
"--rpc-endpoint",
"http://rpc",
"--witness-endpoint",
"http://w",
],
|a| a.data_max_concurrent_requests,
);
}
#[test]
fn witness_max_concurrent_requests_flag_and_env() {
assert_concurrency_flag(
"--witness-max-concurrent-requests",
"DEBUG_TRACE_SERVER_WITNESS_MAX_CONCURRENT_REQUESTS",
&[
"debug-trace-server",
"--rpc-endpoint",
"http://rpc",
"--witness-endpoint",
"http://w",
],
|a| a.witness_max_concurrent_requests,
);
}
}