1//! This contains the [`BenchContext`], which is information that all replay-based benchmarks need.
2//! The initialization code is also the same, so this can be shared across benchmark commands.
34use crate::{authenticated_transport::AuthenticatedTransportConnect, bench_mode::BenchMode};
5use alloy_eips::BlockNumberOrTag;
6use alloy_provider::{network::AnyNetwork, Provider, RootProvider};
7use alloy_rpc_client::ClientBuilder;
8use alloy_rpc_types_engine::JwtSecret;
9use reqwest::Url;
10use reth_node_core::args::BenchmarkArgs;
11use tracing::info;
1213/// This is intended to be used by benchmarks that replay blocks from an RPC.
14///
15/// It contains an authenticated provider for engine API queries, a block provider for block
16/// queries, a [`BenchMode`] to determine whether the benchmark should run for a closed or open
17/// range of blocks, and the next block to fetch.
18pub(crate) struct BenchContext {
19/// The auth provider is used for engine API queries.
20pub(crate) auth_provider: RootProvider<AnyNetwork>,
21/// The block provider is used for block queries.
22pub(crate) block_provider: RootProvider<AnyNetwork>,
23/// The benchmark mode, which defines whether the benchmark should run for a closed or open
24 /// range of blocks.
25pub(crate) benchmark_mode: BenchMode,
26/// The next block to fetch.
27pub(crate) next_block: u64,
28}
2930impl BenchContext {
31/// This is the initialization code for most benchmarks, taking in a [`BenchmarkArgs`] and
32 /// returning the providers needed to run a benchmark.
33pub(crate) async fn new(bench_args: &BenchmarkArgs, rpc_url: String) -> eyre::Result<Self> {
34info!("Running benchmark using data from RPC URL: {}", rpc_url);
3536// Ensure that output directory is a directory
37if let Some(output) = &bench_args.output {
38if output.is_file() {
39return Err(eyre::eyre!("Output path must be a directory"));
40 }
41 }
4243// set up alloy client for blocks
44let client = ClientBuilder::default().http(rpc_url.parse()?);
45let block_provider = RootProvider::<AnyNetwork>::new(client);
4647// If neither `--from` nor `--to` are provided, we will run the benchmark continuously,
48 // starting at the latest block.
49let mut benchmark_mode = BenchMode::new(bench_args.from, bench_args.to)?;
5051// construct the authenticated provider
52let auth_jwt = bench_args
53 .auth_jwtsecret
54 .clone()
55 .ok_or_else(|| eyre::eyre!("--jwtsecret must be provided for authenticated RPC"))?;
5657// fetch jwt from file
58 //
59 // the jwt is hex encoded so we will decode it after
60let jwt = std::fs::read_to_string(auth_jwt)?;
61let jwt = JwtSecret::from_hex(jwt)?;
6263// get engine url
64let auth_url = Url::parse(&bench_args.engine_rpc_url)?;
6566// construct the authed transport
67info!("Connecting to Engine RPC at {} for replay", auth_url);
68let auth_transport = AuthenticatedTransportConnect::new(auth_url, jwt);
69let client = ClientBuilder::default().connect_with(auth_transport).await?;
70let auth_provider = RootProvider::<AnyNetwork>::new(client);
7172let first_block = match benchmark_mode {
73 BenchMode::Continuous => {
74// fetch Latest block
75block_provider.get_block_by_number(BlockNumberOrTag::Latest).full().await?.unwrap()
76 }
77 BenchMode::Range(ref mut range) => {
78match range.next() {
79Some(block_number) => {
80// fetch first block in range
81block_provider
82 .get_block_by_number(block_number.into())
83 .full()
84 .await?
85.unwrap()
86 }
87None => {
88return Err(eyre::eyre!(
89"Benchmark mode range is empty, please provide a larger range"
90));
91 }
92 }
93 }
94 };
9596let next_block = first_block.header.number + 1;
97Ok(Self { auth_provider, block_provider, benchmark_mode, next_block })
98 }
99}