Skip to content

Commit 54119d5

Browse files
committed
feat: remove signet system txns before sending to fee cache
1 parent bf1a8c0 commit 54119d5

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

crates/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tokio-util = "0.7.13"
4040
tower-http = { version = "0.6.2", features = ["cors"] }
4141
tracing.workspace = true
4242
serde_json.workspace = true
43+
futures-util = "0.3.31"
4344

4445
[dev-dependencies]
4546
signet-zenith.workspace = true

crates/rpc/src/ctx/fee_hist.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1+
use reth::{
2+
core::primitives::SealedBlock,
3+
primitives::{Block, RecoveredBlock},
4+
providers::{CanonStateNotification, Chain},
5+
};
6+
use signet_types::MagicSig;
7+
use std::sync::Arc;
18

9+
/// Removes Signet system transactions from the block.
10+
fn strip_block(block: RecoveredBlock<Block>) -> RecoveredBlock<Block> {
11+
let (sealed, senders) = block.split_sealed();
12+
let (header, mut body) = sealed.split_sealed_header_body();
13+
14+
// This is the index of the first transaction that has a system magic
15+
// signature.
16+
let sys_index = body
17+
.transactions
18+
.partition_point(|tx| MagicSig::try_from_signature(tx.signature()).is_some());
19+
20+
body.transactions.truncate(sys_index);
21+
22+
let sealed = SealedBlock::from_sealed_parts(header, body);
23+
RecoveredBlock::new_sealed(sealed, senders)
24+
}
25+
26+
/// Removes Signet system transactions from the chain. This function uses
27+
/// `Arc::make_mut` to clone the contents of the Arc and modify the new
28+
/// instance.
29+
fn strip_chain(chain: &mut Arc<Chain>) {
30+
let chain = Arc::make_mut(chain);
31+
32+
let (blocks, outcome, trie) = std::mem::take(chain).into_inner();
33+
let blocks = blocks.into_blocks().map(strip_block);
34+
35+
*chain = Chain::new(blocks, outcome, trie);
36+
}
37+
38+
/// Strips Signet system transactions from the `CanonStateNotification`.
39+
pub(crate) fn strip_signet_system_txns(notif: CanonStateNotification) -> CanonStateNotification {
40+
// Cloning here ensures that the `make_mut` invocations in
41+
// `strip_chain` do not ever modify the original `notif` object.
42+
let _c = notif.clone();
43+
44+
match notif {
45+
CanonStateNotification::Commit { mut new } => {
46+
strip_chain(&mut new);
47+
CanonStateNotification::Commit { new }
48+
}
49+
CanonStateNotification::Reorg { mut old, mut new } => {
50+
strip_chain(&mut old);
51+
strip_chain(&mut new);
52+
53+
CanonStateNotification::Reorg { old, new }
54+
}
55+
}
56+
}

crates/rpc/src/ctx/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod full;
55
pub use full::RpcCtx;
66

77
mod fee_hist;
8+
pub(crate) use fee_hist::strip_signet_system_txns;
89

910
/// Type alias for EVMs using a [`StateProviderBox`] as the `DB` type for
1011
/// trevm.

crates/rpc/src/ctx/signet.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
RuRevmState,
3+
ctx::strip_signet_system_txns,
34
eth::EthError,
45
interest::{ActiveFilter, FilterManager, FilterOutput, SubscriptionManager},
56
receipts::build_signet_receipt,
@@ -12,6 +13,7 @@ use alloy::{
1213
primitives::{B256, U64},
1314
rpc::types::{FeeHistory, Filter, Log},
1415
};
16+
use futures_util::StreamExt;
1517
use reth::{
1618
core::primitives::SignerRecoverable,
1719
primitives::{Block, Receipt, Recovered, RecoveredBlock, TransactionSigned},
@@ -106,11 +108,14 @@ where
106108
let cache = EthStateCache::spawn_with(provider.clone(), eth_config.cache, spawner.clone());
107109
let gas_oracle =
108110
GasPriceOracle::new(provider.clone(), eth_config.gas_oracle, cache.clone());
111+
109112
let fee_history = FeeHistoryCache::new(eth_config.fee_history_cache);
110113

114+
// The fee task pre-calculates and caches common percentiles for the
115+
// `eth_feeHistory` RPC method.
111116
let fee_task = fee_history_cache_new_blocks_task(
112117
fee_history.clone(),
113-
provider.canonical_state_stream(),
118+
provider.canonical_state_stream().map(strip_signet_system_txns),
114119
provider.clone(),
115120
cache.clone(),
116121
);

0 commit comments

Comments
 (0)