Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/rpc/src/ctx/fee_hist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Copy link
Member

@Evalir Evalir Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this file intended to be here

yeah i see it on #15 just looks odd being empty here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

place holder for the #15 work

163 changes: 163 additions & 0 deletions crates/rpc/src/ctx/full.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use crate::{RuRevmState, SignetCtx};
use alloy::{
consensus::{BlockHeader, Header},
eips::BlockId,
};
use reth::{
providers::{ProviderFactory, ProviderResult},
rpc::server_types::eth::{EthApiError, EthConfig},
tasks::{TaskExecutor, TaskSpawner},
};
use reth_node_api::FullNodeComponents;
use signet_evm::EvmNeedsTx;
use signet_node_types::Pnt;
use signet_tx_cache::client::TxCache;
use signet_types::constants::SignetSystemConstants;
use std::sync::Arc;

/// RPC context. Contains all necessary host and signet components for serving
/// RPC requests.
#[derive(Debug)]
pub struct RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
inner: Arc<RpcCtxInner<Host, Signet>>,
}

impl<Host, Signet> RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
/// Create a new `RpcCtx`.
pub fn new<Tasks>(
host: Host,
constants: SignetSystemConstants,
factory: ProviderFactory<Signet>,
eth_config: EthConfig,
tx_cache: Option<TxCache>,
spawner: Tasks,
) -> ProviderResult<Self>
where
Tasks: TaskSpawner + Clone + 'static,
{
RpcCtxInner::new(host, constants, factory, eth_config, tx_cache, spawner)
.map(|inner| Self { inner: Arc::new(inner) })
}
}

impl<Host, Signet> Clone for RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}

impl<Host, Signet> core::ops::Deref for RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
type Target = RpcCtxInner<Host, Signet>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

/// Inner context for [`RpcCtx`].
#[derive(Debug)]
pub struct RpcCtxInner<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
host: Host,
signet: SignetCtx<Signet>,
}

impl<Host, Signet> RpcCtxInner<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
/// Create a new `RpcCtxInner`.
pub fn new<Tasks>(
host: Host,
constants: SignetSystemConstants,
factory: ProviderFactory<Signet>,
eth_config: EthConfig,
tx_cache: Option<TxCache>,
spawner: Tasks,
) -> ProviderResult<Self>
where
Tasks: TaskSpawner + Clone + 'static,
{
SignetCtx::new(constants, factory, eth_config, tx_cache, spawner)
.map(|signet| Self { host, signet })
}

pub const fn host(&self) -> &Host {
&self.host
}

pub const fn signet(&self) -> &SignetCtx<Signet> {
&self.signet
}

pub fn task_executor(&self) -> &TaskExecutor {
self.host.task_executor()
}

/// Create a trevm instance.
pub fn trevm(
&self,
block_id: BlockId,
block: &Header,
) -> Result<EvmNeedsTx<RuRevmState>, EthApiError> {
// decrement if the id is pending, so that the state is on the latest block
let height = block.number() - block_id.is_pending() as u64;
let spec_id = self.signet.evm_spec_id(block);

let db = self.signet.state_provider_database(height)?;

let mut trevm = signet_evm::signet_evm(db, self.signet.constants().clone())
.fill_cfg(&self.signet)
.fill_block(block);

trevm.set_spec_id(spec_id);

Ok(trevm)
}
}

// Some code in this file has been copied and modified from reth
// <https://github.com/paradigmxyz/reth>
// The original license is included below:
//
// The MIT License (MIT)
//
// Copyright (c) 2022-2025 Reth Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//.
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
15 changes: 15 additions & 0 deletions crates/rpc/src/ctx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod signet;
pub use signet::SignetCtx;

mod full;
pub use full::RpcCtx;

mod fee_hist;

/// Type alias for EVMs using a [`StateProviderBox`] as the `DB` type for
/// trevm.
///
/// [`StateProviderBox`]: reth::providers::StateProviderBox
pub type RuRevmState = trevm::revm::database::State<
reth::revm::database::StateProviderDatabase<reth::providers::StateProviderBox>,
>;
137 changes: 4 additions & 133 deletions crates/rpc/src/ctx.rs → crates/rpc/src/ctx/signet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
RuRevmState,
eth::EthError,
interest::{ActiveFilter, FilterManager, FilterOutput, SubscriptionManager},
receipts::build_signet_receipt,
Expand Down Expand Up @@ -32,12 +33,11 @@ use reth::{
},
types::{FilterBlockOption, FilteredParams},
},
tasks::{TaskExecutor, TaskSpawner},
tasks::TaskSpawner,
};
use reth_chainspec::{BaseFeeParams, ChainSpec, ChainSpecProvider};
use reth_node_api::{BlockBody, FullNodeComponents};
use reth_node_api::BlockBody;
use reth_rpc_eth_api::{RpcBlock, RpcConvert, RpcReceipt, RpcTransaction};
use signet_evm::EvmNeedsTx;
use signet_node_types::Pnt;
use signet_tx_cache::client::TxCache;
use signet_types::{MagicSig, constants::SignetSystemConstants};
Expand All @@ -48,138 +48,9 @@ use trevm::{
revm::{context::CfgEnv, database::StateBuilder},
};

/// Type alias for EVMs using a [`StateProviderBox`] as the `DB` type for
/// trevm.
///
/// [`StateProviderBox`]: reth::providers::StateProviderBox
pub type RuRevmState = trevm::revm::database::State<
reth::revm::database::StateProviderDatabase<reth::providers::StateProviderBox>,
>;

/// The maximum number of headers we read at once when handling a range filter.
const MAX_HEADERS_RANGE: u64 = 1_000; // with ~530bytes per header this is ~500kb

/// RPC context. Contains all necessary host and signet components for serving
/// RPC requests.
#[derive(Debug)]
pub struct RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
inner: Arc<RpcCtxInner<Host, Signet>>,
}

impl<Host, Signet> RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
/// Create a new `RpcCtx`.
pub fn new<Tasks>(
host: Host,
constants: SignetSystemConstants,
factory: ProviderFactory<Signet>,
eth_config: EthConfig,
tx_cache: Option<TxCache>,
spawner: Tasks,
) -> ProviderResult<Self>
where
Tasks: TaskSpawner + Clone + 'static,
{
RpcCtxInner::new(host, constants, factory, eth_config, tx_cache, spawner)
.map(|inner| Self { inner: Arc::new(inner) })
}
}

impl<Host, Signet> Clone for RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}

impl<Host, Signet> core::ops::Deref for RpcCtx<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
type Target = RpcCtxInner<Host, Signet>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

/// Inner context for [`RpcCtx`].
#[derive(Debug)]
pub struct RpcCtxInner<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
host: Host,
signet: SignetCtx<Signet>,
}

impl<Host, Signet> RpcCtxInner<Host, Signet>
where
Host: FullNodeComponents,
Signet: Pnt,
{
/// Create a new `RpcCtxInner`.
pub fn new<Tasks>(
host: Host,
constants: SignetSystemConstants,
factory: ProviderFactory<Signet>,
eth_config: EthConfig,
tx_cache: Option<TxCache>,
spawner: Tasks,
) -> ProviderResult<Self>
where
Tasks: TaskSpawner + Clone + 'static,
{
SignetCtx::new(constants, factory, eth_config, tx_cache, spawner)
.map(|signet| Self { host, signet })
}

pub const fn host(&self) -> &Host {
&self.host
}

pub const fn signet(&self) -> &SignetCtx<Signet> {
&self.signet
}

pub fn task_executor(&self) -> &TaskExecutor {
self.host.task_executor()
}

/// Create a trevm instance.
pub fn trevm(
&self,
block_id: BlockId,
block: &Header,
) -> Result<EvmNeedsTx<RuRevmState>, EthApiError> {
// decrement if the id is pending, so that the state is on the latest block
let height = block.number() - block_id.is_pending() as u64;
let spec_id = self.signet.evm_spec_id(block);

let db = self.signet.state_provider_database(height)?;

let mut trevm = signet_evm::signet_evm(db, self.signet.constants.clone())
.fill_cfg(&self.signet)
.fill_block(block);

trevm.set_spec_id(spec_id);

Ok(trevm)
}
}

/// Signet context. This struct contains all the necessary components for
/// accessing Signet node state, and serving RPC requests.
#[derive(Debug)]
Expand Down Expand Up @@ -307,7 +178,7 @@ where

/// Make a [`StateProviderDatabase`] from the read-write provider, suitable
/// for use with Trevm.
fn state_provider_database(&self, height: u64) -> Result<RuRevmState, EthApiError> {
pub fn state_provider_database(&self, height: u64) -> Result<RuRevmState, EthApiError> {
// Get the state provider for the block number
let sp = self.provider.history_by_block_number(height)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod config;
pub use config::{RpcServerGuard, ServeConfig};

mod ctx;
pub use ctx::{RpcCtx, RuRevmState};
pub use ctx::{RpcCtx, RuRevmState, SignetCtx};

mod eth;
pub use eth::{CallErrorData, EthError, eth};
Expand Down
Loading