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;
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    metrics: OpMinerMetrics,
16}
17
18impl OpMinerExtApi {
19    /// Instantiate the miner API extension with the given, sharable data availability
20    /// configuration.
21    pub fn new(da_config: OpDAConfig) -> Self {
22        Self { da_config, metrics: OpMinerMetrics::default() }
23    }
24}
25
26#[async_trait]
27impl MinerApiExtServer for OpMinerExtApi {
28    /// Handler for `miner_setMaxDASize` RPC method.
29    async fn set_max_da_size(&self, max_tx_size: U64, max_block_size: U64) -> RpcResult<bool> {
30        debug!(target: "rpc", "Setting max DA size: tx={}, block={}", max_tx_size, max_block_size);
31        self.da_config.set_max_da_size(max_tx_size.to(), max_block_size.to());
32
33        self.metrics.set_max_da_tx_size(max_tx_size.to());
34        self.metrics.set_max_da_block_size(max_block_size.to());
35
36        Ok(true)
37    }
38}
39
40/// Optimism miner metrics
41#[derive(Metrics, Clone)]
42#[metrics(scope = "optimism_rpc.miner")]
43pub struct OpMinerMetrics {
44    /// Max DA tx size set on the miner
45    max_da_tx_size: Gauge,
46    /// Max DA block size set on the miner
47    max_da_block_size: Gauge,
48}
49
50impl OpMinerMetrics {
51    /// Sets the max DA tx size gauge value
52    #[inline]
53    pub fn set_max_da_tx_size(&self, size: u64) {
54        self.max_da_tx_size.set(size as f64);
55    }
56
57    /// Sets the max DA block size gauge value
58    #[inline]
59    pub fn set_max_da_block_size(&self, size: u64) {
60        self.max_da_block_size.set(size as f64);
61    }
62}