reth_bench/bench/
context.rs

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.
3
4use 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;
12
13/// 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.
20    pub(crate) auth_provider: RootProvider<AnyNetwork>,
21    /// The block provider is used for block queries.
22    pub(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.
25    pub(crate) benchmark_mode: BenchMode,
26    /// The next block to fetch.
27    pub(crate) next_block: u64,
28}
29
30impl BenchContext {
31    /// This is the initialization code for most benchmarks, taking in a [`BenchmarkArgs`] and
32    /// returning the providers needed to run a benchmark.
33    pub(crate) async fn new(bench_args: &BenchmarkArgs, rpc_url: String) -> eyre::Result<Self> {
34        info!("Running benchmark using data from RPC URL: {}", rpc_url);
35
36        // Ensure that output directory is a directory
37        if let Some(output) = &bench_args.output {
38            if output.is_file() {
39                return Err(eyre::eyre!("Output path must be a directory"));
40            }
41        }
42
43        // set up alloy client for blocks
44        let client = ClientBuilder::default().http(rpc_url.parse()?);
45        let block_provider = RootProvider::<AnyNetwork>::new(client);
46
47        // If neither `--from` nor `--to` are provided, we will run the benchmark continuously,
48        // starting at the latest block.
49        let mut benchmark_mode = BenchMode::new(bench_args.from, bench_args.to)?;
50
51        // construct the authenticated provider
52        let auth_jwt = bench_args
53            .auth_jwtsecret
54            .clone()
55            .ok_or_else(|| eyre::eyre!("--jwtsecret must be provided for authenticated RPC"))?;
56
57        // fetch jwt from file
58        //
59        // the jwt is hex encoded so we will decode it after
60        let jwt = std::fs::read_to_string(auth_jwt)?;
61        let jwt = JwtSecret::from_hex(jwt)?;
62
63        // get engine url
64        let auth_url = Url::parse(&bench_args.engine_rpc_url)?;
65
66        // construct the authed transport
67        info!("Connecting to Engine RPC at {} for replay", auth_url);
68        let auth_transport = AuthenticatedTransportConnect::new(auth_url, jwt);
69        let client = ClientBuilder::default().connect_with(auth_transport).await?;
70        let auth_provider = RootProvider::<AnyNetwork>::new(client);
71
72        let first_block = match benchmark_mode {
73            BenchMode::Continuous => {
74                // fetch Latest block
75                block_provider.get_block_by_number(BlockNumberOrTag::Latest).full().await?.unwrap()
76            }
77            BenchMode::Range(ref mut range) => {
78                match range.next() {
79                    Some(block_number) => {
80                        // fetch first block in range
81                        block_provider
82                            .get_block_by_number(block_number.into())
83                            .full()
84                            .await?
85                            .unwrap()
86                    }
87                    None => {
88                        return Err(eyre::eyre!(
89                            "Benchmark mode range is empty, please provide a larger range"
90                        ));
91                    }
92                }
93            }
94        };
95
96        let next_block = first_block.header.number + 1;
97        Ok(Self { auth_provider, block_provider, benchmark_mode, next_block })
98    }
99}