reth_optimism_rpc/
miner.rs

1//! Miner API extension for OP.
2
3use alloy_primitives::U64;
4use jsonrpsee_core::{async_trait, RpcResult};
5pub use op_alloy_rpc_jsonrpsee::traits::MinerApiExtServer;
6use reth_metrics::{metrics::Gauge, Metrics};
7use reth_optimism_payload_builder::config::{OpDAConfig, OpGasLimitConfig};
8use tracing::debug;
9
10/// Miner API extension for OP, exposes settings for the data availability configuration via the
11/// `miner_` API.
12#[derive(Debug, Clone)]
13pub struct OpMinerExtApi {
14    da_config: OpDAConfig,
15    gas_limit_config: OpGasLimitConfig,
16    metrics: OpMinerMetrics,
17}
18
19impl OpMinerExtApi {
20    /// Instantiate the miner API extension with the given, sharable data availability
21    /// configuration.
22    pub fn new(da_config: OpDAConfig, gas_limit_config: OpGasLimitConfig) -> Self {
23        Self { da_config, gas_limit_config, metrics: OpMinerMetrics::default() }
24    }
25}
26
27#[async_trait]
28impl MinerApiExtServer for OpMinerExtApi {
29    /// Handler for `miner_setMaxDASize` RPC method.
30    async fn set_max_da_size(&self, max_tx_size: U64, max_block_size: U64) -> RpcResult<bool> {
31        debug!(target: "rpc", "Setting max DA size: tx={}, block={}", max_tx_size, max_block_size);
32        self.da_config.set_max_da_size(max_tx_size.to(), max_block_size.to());
33
34        self.metrics.set_max_da_tx_size(max_tx_size.to());
35        self.metrics.set_max_da_block_size(max_block_size.to());
36
37        Ok(true)
38    }
39
40    async fn set_gas_limit(&self, gas_limit: U64) -> RpcResult<bool> {
41        debug!(target: "rpc", "Setting gas limit: {}", gas_limit);
42        self.gas_limit_config.set_gas_limit(gas_limit.to());
43        self.metrics.set_gas_limit(gas_limit.to());
44        Ok(true)
45    }
46}
47
48/// Optimism miner metrics
49#[derive(Metrics, Clone)]
50#[metrics(scope = "optimism_rpc.miner")]
51pub struct OpMinerMetrics {
52    /// Max DA tx size set on the miner
53    max_da_tx_size: Gauge,
54    /// Max DA block size set on the miner
55    max_da_block_size: Gauge,
56    /// Gas limit set on the miner
57    gas_limit: Gauge,
58}
59
60impl OpMinerMetrics {
61    /// Sets the max DA tx size gauge value
62    #[inline]
63    pub fn set_max_da_tx_size(&self, size: u64) {
64        self.max_da_tx_size.set(size as f64);
65    }
66
67    /// Sets the max DA block size gauge value
68    #[inline]
69    pub fn set_max_da_block_size(&self, size: u64) {
70        self.max_da_block_size.set(size as f64);
71    }
72
73    /// Sets the gas limit gauge value
74    #[inline]
75    pub fn set_gas_limit(&self, gas_limit: u64) {
76        self.gas_limit.set(gas_limit as f64);
77    }
78}