Sitelet https://github.com/init4tech/node-components/commit/d3592a5f5e8a6e7bb46a44b2a0ec41d18d776baa
Skip to content

Commit d3592a5

Browse files
prestwichclaude
andcommitted
refactor: DRY up EVM setup, fix glob import, remove dead code
- Extract `resolve_evm_block` method on `StorageRpcCtx` to deduplicate the block resolution + header fetch + revm db creation shared by `call()` and `estimate_gas()`. Resolves headers directly (by hash or by tag→number) to avoid redundant cold storage lookups. - Replace glob import `use endpoints::*` with explicit imports. - Remove unused `revm_state()` method from `StorageRpcCtx`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ad055f0 commit d3592a5

3 files changed

Lines changed: 47 additions & 32 deletions

File tree

‎crates/rpc-storage/src/ctx.rs‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! RPC context wrapping [`UnifiedStorage`].
22
3-
use crate::resolve::BlockTags;
3+
use crate::{
4+
EthError,
5+
resolve::{BlockTags, resolve_block_number_or_tag},
6+
};
7+
use alloy::eips::BlockId;
48
use signet_cold::ColdStorageReadHandle;
59
use signet_hot::HotKv;
610
use signet_hot::model::{HotKvRead, RevmRead};
@@ -11,6 +15,18 @@ use std::sync::Arc;
1115
use trevm::revm::database::DBErrorMarker;
1216
use trevm::revm::database::StateBuilder;
1317

18+
/// Resolved block context for EVM execution.
19+
///
20+
/// Contains the header and a revm-compatible database snapshot at the
21+
/// resolved block height, ready for use with `signet_evm`.
22+
#[derive(Debug)]
23+
pub(crate) struct EvmBlockContext<Db> {
24+
/// The resolved block header.
25+
pub header: alloy::consensus::Header,
26+
/// The revm database at the resolved height.
27+
pub db: trevm::revm::database::State<Db>,
28+
}
29+
1430
/// RPC context backed by [`UnifiedStorage`].
1531
///
1632
/// Provides access to hot storage (state), cold storage (blocks/txs/receipts),
@@ -111,15 +127,31 @@ impl<H: HotKv> StorageRpcCtx<H> {
111127
Ok(StateBuilder::new_with_database(revm_read).build())
112128
}
113129

114-
/// Create a revm-compatible database at the current tip.
115-
pub fn revm_state(
130+
/// Resolve a [`BlockId`] to a header and revm database in one pass.
131+
///
132+
/// For hash-based IDs, fetches the header directly by hash. For
133+
/// tag/number-based IDs, resolves the tag then fetches the header by
134+
/// number. This avoids a redundant header lookup that would occur if
135+
/// resolving to a block number first.
136+
pub(crate) async fn resolve_evm_block(
116137
&self,
117-
) -> signet_storage::StorageResult<trevm::revm::database::State<RevmRead<H::RoTx>>>
138+
id: BlockId,
139+
) -> Result<EvmBlockContext<RevmRead<H::RoTx>>, EthError>
118140
where
119141
H::RoTx: Send + Sync,
120142
<H::RoTx as HotKvRead>::Error: DBErrorMarker,
121143
{
122-
let revm_read = self.inner.storage.revm_reader()?;
123-
Ok(StateBuilder::new_with_database(revm_read).build())
144+
let cold = self.cold();
145+
let header = match id {
146+
BlockId::Hash(h) => cold.get_header_by_hash(h.block_hash).await?,
147+
BlockId::Number(tag) => {
148+
let height = resolve_block_number_or_tag(tag, self.tags())?;
149+
cold.get_header_by_number(height).await?
150+
}
151+
}
152+
.ok_or(EthError::BlockNotFound(id))?;
153+
154+
let db = self.revm_state_at_height(header.number)?;
155+
Ok(EvmBlockContext { header, db })
124156
}
125157
}

‎crates/rpc-storage/src/eth/endpoints.rs‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! ETH namespace RPC endpoint implementations.
22
33
use crate::{
4-
ctx::StorageRpcCtx,
4+
ctx::{EvmBlockContext, StorageRpcCtx},
55
eth::helpers::{
66
AddrWithBlock, BlockParams, BlockRangeInclusiveIter, CfgFiller, StorageAtArgs, TxParams,
77
await_handler, build_receipt, build_receipt_from_parts, build_rpc_transaction,
@@ -507,18 +507,7 @@ where
507507
let span = trace_span!("eth_call", block_id = %id);
508508

509509
let task = async move {
510-
let cold = ctx.cold();
511-
let height = response_tri!(resolve_block_id(id, ctx.tags(), &cold).await);
512-
513-
let header = response_tri!(cold.get_header_by_number(height).await);
514-
let Some(header) = header else {
515-
return ResponsePayload::internal_error_with_message_and_obj(
516-
"block not found".into(),
517-
id.to_string().into(),
518-
);
519-
};
520-
521-
let db = response_tri!(ctx.revm_state_at_height(height));
510+
let EvmBlockContext { header, db } = response_tri!(ctx.resolve_evm_block(id).await);
522511

523512
let trevm = signet_evm::signet_evm(db, ctx.constants().clone())
524513
.fill_cfg(&CfgFiller(ctx.chain_id()))
@@ -576,18 +565,7 @@ where
576565
let span = trace_span!("eth_estimateGas", block_id = %id);
577566

578567
let task = async move {
579-
let cold = ctx.cold();
580-
let height = response_tri!(resolve_block_id(id, ctx.tags(), &cold).await);
581-
582-
let header = response_tri!(cold.get_header_by_number(height).await);
583-
let Some(header) = header else {
584-
return ResponsePayload::internal_error_with_message_and_obj(
585-
"block not found".into(),
586-
id.to_string().into(),
587-
);
588-
};
589-
590-
let db = response_tri!(ctx.revm_state_at_height(height));
568+
let EvmBlockContext { header, db } = response_tri!(ctx.resolve_evm_block(id).await);
591569

592570
let trevm = signet_evm::signet_evm(db, ctx.constants().clone())
593571
.fill_cfg(&CfgFiller(ctx.chain_id()))

‎crates/rpc-storage/src/eth/mod.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
//! ETH namespace RPC router backed by storage.
22
33
mod endpoints;
4-
use endpoints::*;
4+
use endpoints::{
5+
addr_tx_count, balance, block, block_number, block_receipts, block_tx_count, call, chain_id,
6+
code_at, estimate_gas, get_logs, header_by, not_supported, raw_transaction_by_hash,
7+
raw_tx_by_block_and_index, send_raw_transaction, storage_at, transaction_by_hash,
8+
transaction_receipt, tx_by_block_and_index,
9+
};
510

611
mod error;
712
pub use error::EthError;

0 commit comments

Comments
 (0)