-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathextension.rs
More file actions
79 lines (63 loc) · 2.56 KB
/
extension.rs
File metadata and controls
79 lines (63 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Contains the [`MeteringExtension`] which wires up the metering RPC surface
//! on the Base node builder.
use std::sync::Arc;
use base_client_node::{BaseBuilder, BaseNodeExtension, FromExtensionConfig};
use base_flashblocks::{FlashblocksConfig, FlashblocksState};
use tracing::info;
use crate::{MeteringApiImpl, MeteringApiServer};
/// Helper struct that wires the metering RPC into the node builder.
#[derive(Debug)]
pub struct MeteringExtension {
/// Whether metering is enabled.
pub enabled: bool,
/// Optional Flashblocks configuration (includes state).
pub flashblocks_config: Option<FlashblocksConfig>,
}
impl MeteringExtension {
/// Creates a new metering extension.
pub const fn new(enabled: bool, flashblocks_config: Option<FlashblocksConfig>) -> Self {
Self { enabled, flashblocks_config }
}
}
impl BaseNodeExtension for MeteringExtension {
/// Applies the extension to the supplied builder.
fn apply(self: Box<Self>, builder: BaseBuilder) -> BaseBuilder {
if !self.enabled {
return builder;
}
let flashblocks_config = self.flashblocks_config;
builder.add_rpc_module(move |ctx| {
info!(message = "Starting Metering RPC");
// Get flashblocks state from config, or create a default one if not configured
let fb_state: Arc<FlashblocksState> =
flashblocks_config.as_ref().map(|cfg| cfg.state.clone()).unwrap_or_default();
let metering_api = MeteringApiImpl::new(ctx.provider().clone(), fb_state);
ctx.modules.merge_configured(metering_api.into_rpc())?;
Ok(())
})
}
}
/// Configuration for building a [`MeteringExtension`].
#[derive(Debug)]
pub struct MeteringConfig {
/// Whether metering is enabled.
pub enabled: bool,
/// Optional Flashblocks configuration (includes state).
pub flashblocks_config: Option<FlashblocksConfig>,
}
impl MeteringConfig {
/// Creates a configuration with metering enabled and no flashblocks integration.
pub const fn enabled() -> Self {
Self { enabled: true, flashblocks_config: None }
}
/// Creates a configuration with metering enabled and flashblocks integration.
pub const fn with_flashblocks(flashblocks_config: FlashblocksConfig) -> Self {
Self { enabled: true, flashblocks_config: Some(flashblocks_config) }
}
}
impl FromExtensionConfig for MeteringExtension {
type Config = MeteringConfig;
fn from_config(config: Self::Config) -> Self {
Self::new(config.enabled, config.flashblocks_config)
}
}