Skip to main content

reth_stages_types/
execution.rs

1use core::time::Duration;
2
3/// The thresholds at which the execution stage writes state changes to the database.
4///
5/// If any of the thresholds (`max_blocks`, `max_changes`, `max_cumulative_gas`, or `max_duration`)
6/// are hit, then the execution stage commits all pending changes to the database.
7#[derive(Debug, Clone)]
8pub struct ExecutionStageThresholds {
9    /// The maximum number of blocks to execute before the execution stage commits.
10    pub max_blocks: Option<u64>,
11    /// The maximum number of state changes to keep in memory before the execution stage commits.
12    pub max_changes: Option<u64>,
13    /// The maximum cumulative amount of gas to process before the execution stage commits.
14    pub max_cumulative_gas: Option<u64>,
15    /// The maximum spent on blocks processing before the execution stage commits.
16    pub max_duration: Option<Duration>,
17}
18
19impl Default for ExecutionStageThresholds {
20    fn default() -> Self {
21        Self {
22            max_blocks: Some(500_000),
23            max_changes: Some(5_000_000),
24            // 50k full blocks of 30M gas
25            max_cumulative_gas: Some(30_000_000 * 50_000),
26            // 10 minutes
27            max_duration: Some(Duration::from_secs(10 * 60)),
28        }
29    }
30}
31
32impl ExecutionStageThresholds {
33    /// Check if the batch thresholds have been hit.
34    #[inline]
35    pub fn is_end_of_batch(
36        &self,
37        blocks_processed: u64,
38        changes_processed: u64,
39        cumulative_gas_used: u64,
40        elapsed: Duration,
41    ) -> bool {
42        blocks_processed >= self.max_blocks.unwrap_or(u64::MAX) ||
43            changes_processed >= self.max_changes.unwrap_or(u64::MAX) ||
44            cumulative_gas_used >= self.max_cumulative_gas.unwrap_or(u64::MAX) ||
45            elapsed >= self.max_duration.unwrap_or(Duration::MAX)
46    }
47}