Skip to content

Commit 3946306

Browse files
committed
Fix nightly clippy warnings in metering pipeline
- Add const fn annotations where possible (cache, estimator, runner) - Add Debug impls for ResourceAnnotator and KafkaBundleConsumer - Remove redundant clones in meter_rpc and runner - Change pub mod to pub(crate) mod for internal modules - Replace redundant closure with function reference - Remove unused cache field from MeteringRuntime
1 parent e4633df commit 3946306

File tree

7 files changed

+51
-45
lines changed

7 files changed

+51
-45
lines changed

crates/rpc/src/base/annotator.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Resource annotator that correlates Kafka metering data with flashblock inclusions.
22
3-
use std::sync::Arc;
3+
use std::{fmt, sync::Arc};
44

55
use alloy_primitives::TxHash;
66
use parking_lot::RwLock;
@@ -40,6 +40,14 @@ pub struct ResourceAnnotator {
4040
pending_transactions: indexmap::IndexMap<TxHash, MeteredTransaction>,
4141
}
4242

43+
impl fmt::Debug for ResourceAnnotator {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45+
f.debug_struct("ResourceAnnotator")
46+
.field("pending_transactions", &self.pending_transactions.len())
47+
.finish_non_exhaustive()
48+
}
49+
}
50+
4351
impl ResourceAnnotator {
4452
/// Creates a new resource annotator.
4553
pub fn new(

crates/rpc/src/base/cache.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct MeteredTransaction {
2626

2727
impl MeteredTransaction {
2828
/// Creates a zeroed transaction (placeholder with no resource usage).
29-
pub fn zeroed(tx_hash: B256) -> Self {
29+
pub const fn zeroed(tx_hash: B256) -> Self {
3030
Self {
3131
tx_hash,
3232
priority_fee_per_gas: U256::ZERO,
@@ -52,7 +52,7 @@ pub struct ResourceTotals {
5252
}
5353

5454
impl ResourceTotals {
55-
fn accumulate(&mut self, tx: &MeteredTransaction) {
55+
const fn accumulate(&mut self, tx: &MeteredTransaction) {
5656
self.gas_used = self.gas_used.saturating_add(tx.gas_used);
5757
self.execution_time_us = self.execution_time_us.saturating_add(tx.execution_time_us);
5858
self.state_root_time_us = self.state_root_time_us.saturating_add(tx.state_root_time_us);
@@ -93,7 +93,7 @@ impl FlashblockMetrics {
9393
}
9494

9595
/// Returns the resource totals for this flashblock.
96-
pub fn totals(&self) -> ResourceTotals {
96+
pub const fn totals(&self) -> ResourceTotals {
9797
self.totals
9898
}
9999

@@ -103,12 +103,12 @@ impl FlashblockMetrics {
103103
}
104104

105105
/// Returns the number of transactions.
106-
pub fn len(&self) -> usize {
106+
pub const fn len(&self) -> usize {
107107
self.transactions.len()
108108
}
109109

110110
/// Returns true if empty.
111-
pub fn is_empty(&self) -> bool {
111+
pub const fn is_empty(&self) -> bool {
112112
self.transactions.is_empty()
113113
}
114114
}
@@ -150,7 +150,7 @@ impl BlockMetrics {
150150
}
151151

152152
/// Returns the resource totals for this block.
153-
pub fn totals(&self) -> ResourceTotals {
153+
pub const fn totals(&self) -> ResourceTotals {
154154
self.totals
155155
}
156156

@@ -184,7 +184,7 @@ impl MeteringCache {
184184
}
185185

186186
/// Returns the maximum number of blocks retained.
187-
pub fn max_blocks(&self) -> usize {
187+
pub const fn max_blocks(&self) -> usize {
188188
self.max_blocks
189189
}
190190

crates/rpc/src/base/estimator.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub enum ResourceKind {
8484

8585
impl ResourceKind {
8686
/// Returns all resource kinds in a fixed order.
87-
pub fn all() -> [Self; 4] {
87+
pub const fn all() -> [Self; 4] {
8888
[Self::GasUsed, Self::ExecutionTime, Self::StateRootTime, Self::DataAvailability]
8989
}
9090

@@ -98,7 +98,7 @@ impl ResourceKind {
9898
///
9999
/// Other resources like gas and DA bytes are bounded per-block but are
100100
/// evaluated per-flashblock since their limits apply independently.
101-
fn use_it_or_lose_it(self) -> bool {
101+
const fn use_it_or_lose_it(self) -> bool {
102102
matches!(self, Self::ExecutionTime)
103103
}
104104

@@ -185,7 +185,7 @@ pub struct ResourceEstimates {
185185

186186
impl ResourceEstimates {
187187
/// Returns the estimate for the given resource kind.
188-
pub fn get(&self, kind: ResourceKind) -> Option<&ResourceEstimate> {
188+
pub const fn get(&self, kind: ResourceKind) -> Option<&ResourceEstimate> {
189189
match kind {
190190
ResourceKind::GasUsed => self.gas_used.as_ref(),
191191
ResourceKind::ExecutionTime => self.execution_time.as_ref(),
@@ -195,7 +195,7 @@ impl ResourceEstimates {
195195
}
196196

197197
/// Sets the estimate for the given resource kind.
198-
pub fn set(&mut self, kind: ResourceKind, estimate: ResourceEstimate) {
198+
pub const fn set(&mut self, kind: ResourceKind, estimate: ResourceEstimate) {
199199
match kind {
200200
ResourceKind::GasUsed => self.gas_used = Some(estimate),
201201
ResourceKind::ExecutionTime => self.execution_time = Some(estimate),
@@ -278,7 +278,7 @@ impl PriorityFeeEstimator {
278278
/// - `limits`: Configured resource capacity limits.
279279
/// - `default_priority_fee`: Fee to return when a resource is not congested.
280280
/// - `da_config`: Optional shared DA config for dynamic DA limit updates.
281-
pub fn new(
281+
pub const fn new(
282282
cache: Arc<RwLock<MeteringCache>>,
283283
percentile: f64,
284284
limits: ResourceLimits,
@@ -318,10 +318,8 @@ impl PriorityFeeEstimator {
318318
demand: ResourceDemand,
319319
) -> Result<Option<BlockPriorityEstimates>, EstimateError> {
320320
let cache_guard = self.cache.read();
321-
let block_metrics = match block_number {
322-
Some(target) => cache_guard.block(target),
323-
None => cache_guard.blocks_desc().next(),
324-
};
321+
let block_metrics = block_number
322+
.map_or_else(|| cache_guard.blocks_desc().next(), |target| cache_guard.block(target));
325323
let Some(block_metrics) = block_metrics else {
326324
return Ok(None);
327325
};
@@ -555,8 +553,15 @@ fn compute_estimate(
555553
});
556554
}
557555

558-
let (supporting_count, threshold_fee, recommended_fee) = match last_included_idx {
559-
Some(idx) => {
556+
let (supporting_count, threshold_fee, recommended_fee) = last_included_idx.map_or_else(
557+
|| {
558+
// No transactions fit - even the first transaction would crowd out
559+
// the bundle. The bundle must beat the highest fee to be included.
560+
// Report 0 supporting transactions since none were actually included.
561+
let threshold_fee = transactions[0].priority_fee_per_gas;
562+
(0, threshold_fee, threshold_fee)
563+
},
564+
|idx| {
560565
// At least one transaction fits alongside the bundle.
561566
// The threshold is the fee of the last included transaction.
562567
let threshold_fee = transactions[idx].priority_fee_per_gas;
@@ -574,15 +579,8 @@ fn compute_estimate(
574579
};
575580

576581
(idx + 1, threshold_fee, recommended_fee)
577-
}
578-
None => {
579-
// No transactions fit - even the first transaction would crowd out
580-
// the bundle. The bundle must beat the highest fee to be included.
581-
// Report 0 supporting transactions since none were actually included.
582-
let threshold_fee = transactions[0].priority_fee_per_gas;
583-
(0, threshold_fee, threshold_fee)
584-
}
585-
};
582+
},
583+
);
586584

587585
Ok(ResourceEstimate {
588586
threshold_priority_fee: threshold_fee,

crates/rpc/src/base/kafka.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Kafka consumer for accepted bundle events.
22
3-
use std::time::Duration;
3+
use std::{fmt, time::Duration};
44

55
use alloy_consensus::{Transaction, transaction::Recovered};
66
use alloy_eips::Encodable2718;
@@ -38,6 +38,12 @@ pub struct KafkaBundleConsumer {
3838
topic: String,
3939
}
4040

41+
impl fmt::Debug for KafkaBundleConsumer {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
f.debug_struct("KafkaBundleConsumer").field("topic", &self.topic).finish_non_exhaustive()
44+
}
45+
}
46+
4147
impl KafkaBundleConsumer {
4248
/// Creates a new Kafka bundle consumer.
4349
pub fn new(

crates/rpc/src/base/meter_rpc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ where
4242
}
4343

4444
/// Creates a new instance of MeteringApi with priority fee estimation enabled.
45-
pub fn with_estimator(
45+
pub const fn with_estimator(
4646
provider: Provider,
4747
priority_fee_estimator: Arc<PriorityFeeEstimator>,
4848
) -> Self {
@@ -100,7 +100,7 @@ where
100100
)
101101
})?;
102102

103-
let chain_spec = self.provider.chain_spec().clone();
103+
let chain_spec = self.provider.chain_spec();
104104

105105
let (results, total_gas_used, total_gas_fees, bundle_hash, total_execution_time) =
106106
meter_bundle(state_provider, chain_spec, parsed_bundle, &header).map_err(|e| {

crates/rpc/src/base/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
pub mod annotator;
2-
pub mod cache;
3-
pub mod estimator;
4-
pub mod kafka;
1+
pub(crate) mod annotator;
2+
pub(crate) mod cache;
3+
pub(crate) mod estimator;
4+
pub(crate) mod kafka;
55
pub(crate) mod meter;
66
pub(crate) mod meter_rpc;
77
pub(crate) mod pubsub;

crates/runner/src/extensions/rpc.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ use crate::{
2626
/// Runtime state for the metering pipeline.
2727
#[derive(Clone)]
2828
struct MeteringRuntime {
29-
/// Shared cache for metered transactions.
30-
cache: Arc<RwLock<MeteringCache>>,
3129
/// Priority fee estimator.
3230
estimator: Arc<PriorityFeeEstimator>,
3331
/// Sender for metered transactions from Kafka.
@@ -45,7 +43,7 @@ struct CompositeFlashblocksReceiver<Client> {
4543
}
4644

4745
impl<Client> CompositeFlashblocksReceiver<Client> {
48-
fn new(
46+
const fn new(
4947
state: Arc<FlashblocksState<Client>>,
5048
metering_sender: Option<mpsc::UnboundedSender<FlashblockInclusion>>,
5149
) -> Self {
@@ -84,8 +82,7 @@ fn flashblock_inclusion_from_flashblock(flashblock: &Flashblock) -> Option<Flash
8482
return None;
8583
}
8684

87-
let ordered_tx_hashes: Vec<B256> =
88-
flashblock.diff.transactions.iter().map(|tx_bytes| keccak256(tx_bytes)).collect();
85+
let ordered_tx_hashes: Vec<B256> = flashblock.diff.transactions.iter().map(keccak256).collect();
8986

9087
Some(FlashblockInclusion {
9188
block_number: flashblock.metadata.block_number,
@@ -187,14 +184,11 @@ impl BaseNodeExtension for BaseRpcExtension {
187184
mpsc::unbounded_channel::<FlashblockInclusion>();
188185

189186
// Spawn the resource annotator
190-
let annotator_cache = cache.clone();
191187
tokio::spawn(async move {
192-
ResourceAnnotator::new(annotator_cache, tx_receiver, flashblock_receiver)
193-
.run()
194-
.await;
188+
ResourceAnnotator::new(cache, tx_receiver, flashblock_receiver).run().await;
195189
});
196190

197-
Some(MeteringRuntime { cache, estimator, tx_sender, flashblock_sender })
191+
Some(MeteringRuntime { estimator, tx_sender, flashblock_sender })
198192
} else {
199193
None
200194
};

0 commit comments

Comments
 (0)