Skip to content

Commit 34e94f6

Browse files
committed
refactor: break ctx into modules
1 parent 9f2c264 commit 34e94f6

File tree

5 files changed

+183
-134
lines changed

5 files changed

+183
-134
lines changed

crates/rpc/src/ctx/fee_hist.rs

Whitespace-only changes.

crates/rpc/src/ctx/full.rs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
use crate::{RuRevmState, SignetCtx};
2+
use alloy::{
3+
consensus::{BlockHeader, Header},
4+
eips::BlockId,
5+
};
6+
use reth::{
7+
providers::{ProviderFactory, ProviderResult},
8+
rpc::server_types::eth::{EthApiError, EthConfig},
9+
tasks::{TaskExecutor, TaskSpawner},
10+
};
11+
use reth_node_api::FullNodeComponents;
12+
use signet_evm::EvmNeedsTx;
13+
use signet_node_types::Pnt;
14+
use signet_tx_cache::client::TxCache;
15+
use signet_types::constants::SignetSystemConstants;
16+
use std::sync::Arc;
17+
18+
/// RPC context. Contains all necessary host and signet components for serving
19+
/// RPC requests.
20+
#[derive(Debug)]
21+
pub struct RpcCtx<Host, Signet>
22+
where
23+
Host: FullNodeComponents,
24+
Signet: Pnt,
25+
{
26+
inner: Arc<RpcCtxInner<Host, Signet>>,
27+
}
28+
29+
impl<Host, Signet> RpcCtx<Host, Signet>
30+
where
31+
Host: FullNodeComponents,
32+
Signet: Pnt,
33+
{
34+
/// Create a new `RpcCtx`.
35+
pub fn new<Tasks>(
36+
host: Host,
37+
constants: SignetSystemConstants,
38+
factory: ProviderFactory<Signet>,
39+
eth_config: EthConfig,
40+
tx_cache: Option<TxCache>,
41+
spawner: Tasks,
42+
) -> ProviderResult<Self>
43+
where
44+
Tasks: TaskSpawner + Clone + 'static,
45+
{
46+
RpcCtxInner::new(host, constants, factory, eth_config, tx_cache, spawner)
47+
.map(|inner| Self { inner: Arc::new(inner) })
48+
}
49+
}
50+
51+
impl<Host, Signet> Clone for RpcCtx<Host, Signet>
52+
where
53+
Host: FullNodeComponents,
54+
Signet: Pnt,
55+
{
56+
fn clone(&self) -> Self {
57+
Self { inner: self.inner.clone() }
58+
}
59+
}
60+
61+
impl<Host, Signet> core::ops::Deref for RpcCtx<Host, Signet>
62+
where
63+
Host: FullNodeComponents,
64+
Signet: Pnt,
65+
{
66+
type Target = RpcCtxInner<Host, Signet>;
67+
68+
fn deref(&self) -> &Self::Target {
69+
&self.inner
70+
}
71+
}
72+
73+
/// Inner context for [`RpcCtx`].
74+
#[derive(Debug)]
75+
pub struct RpcCtxInner<Host, Signet>
76+
where
77+
Host: FullNodeComponents,
78+
Signet: Pnt,
79+
{
80+
host: Host,
81+
signet: SignetCtx<Signet>,
82+
}
83+
84+
impl<Host, Signet> RpcCtxInner<Host, Signet>
85+
where
86+
Host: FullNodeComponents,
87+
Signet: Pnt,
88+
{
89+
/// Create a new `RpcCtxInner`.
90+
pub fn new<Tasks>(
91+
host: Host,
92+
constants: SignetSystemConstants,
93+
factory: ProviderFactory<Signet>,
94+
eth_config: EthConfig,
95+
tx_cache: Option<TxCache>,
96+
spawner: Tasks,
97+
) -> ProviderResult<Self>
98+
where
99+
Tasks: TaskSpawner + Clone + 'static,
100+
{
101+
SignetCtx::new(constants, factory, eth_config, tx_cache, spawner)
102+
.map(|signet| Self { host, signet })
103+
}
104+
105+
pub const fn host(&self) -> &Host {
106+
&self.host
107+
}
108+
109+
pub const fn signet(&self) -> &SignetCtx<Signet> {
110+
&self.signet
111+
}
112+
113+
pub fn task_executor(&self) -> &TaskExecutor {
114+
self.host.task_executor()
115+
}
116+
117+
/// Create a trevm instance.
118+
pub fn trevm(
119+
&self,
120+
block_id: BlockId,
121+
block: &Header,
122+
) -> Result<EvmNeedsTx<RuRevmState>, EthApiError> {
123+
// decrement if the id is pending, so that the state is on the latest block
124+
let height = block.number() - block_id.is_pending() as u64;
125+
let spec_id = self.signet.evm_spec_id(block);
126+
127+
let db = self.signet.state_provider_database(height)?;
128+
129+
let mut trevm = signet_evm::signet_evm(db, self.signet.constants().clone())
130+
.fill_cfg(&self.signet)
131+
.fill_block(block);
132+
133+
trevm.set_spec_id(spec_id);
134+
135+
Ok(trevm)
136+
}
137+
}
138+
139+
// Some code in this file has been copied and modified from reth
140+
// <https://github.com/paradigmxyz/reth>
141+
// The original license is included below:
142+
//
143+
// The MIT License (MIT)
144+
//
145+
// Copyright (c) 2022-2025 Reth Contributors
146+
//
147+
// Permission is hereby granted, free of charge, to any person obtaining a copy
148+
// of this software and associated documentation files (the "Software"), to deal
149+
// in the Software without restriction, including without limitation the rights
150+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
151+
// copies of the Software, and to permit persons to whom the Software is
152+
// furnished to do so, subject to the following conditions:
153+
//.
154+
// The above copyright notice and this permission notice shall be included in
155+
// all copies or substantial portions of the Software.
156+
//
157+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
159+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
162+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
163+
// THE SOFTWARE.

crates/rpc/src/ctx/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mod signet;
2+
pub use signet::SignetCtx;
3+
4+
mod full;
5+
pub use full::RpcCtx;
6+
7+
mod fee_hist;
8+
9+
/// Type alias for EVMs using a [`StateProviderBox`] as the `DB` type for
10+
/// trevm.
11+
///
12+
/// [`StateProviderBox`]: reth::providers::StateProviderBox
13+
pub type RuRevmState = trevm::revm::database::State<
14+
reth::revm::database::StateProviderDatabase<reth::providers::StateProviderBox>,
15+
>;

crates/rpc/src/ctx.rs renamed to crates/rpc/src/ctx/signet.rs

Lines changed: 4 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
RuRevmState,
23
eth::EthError,
34
interest::{ActiveFilter, FilterManager, FilterOutput, SubscriptionManager},
45
receipts::build_signet_receipt,
@@ -32,12 +33,11 @@ use reth::{
3233
},
3334
types::{FilterBlockOption, FilteredParams},
3435
},
35-
tasks::{TaskExecutor, TaskSpawner},
36+
tasks::TaskSpawner,
3637
};
3738
use reth_chainspec::{BaseFeeParams, ChainSpec, ChainSpecProvider};
38-
use reth_node_api::{BlockBody, FullNodeComponents};
39+
use reth_node_api::BlockBody;
3940
use reth_rpc_eth_api::{RpcBlock, RpcConvert, RpcReceipt, RpcTransaction};
40-
use signet_evm::EvmNeedsTx;
4141
use signet_node_types::Pnt;
4242
use signet_tx_cache::client::TxCache;
4343
use signet_types::{MagicSig, constants::SignetSystemConstants};
@@ -48,138 +48,9 @@ use trevm::{
4848
revm::{context::CfgEnv, database::StateBuilder},
4949
};
5050

51-
/// Type alias for EVMs using a [`StateProviderBox`] as the `DB` type for
52-
/// trevm.
53-
///
54-
/// [`StateProviderBox`]: reth::providers::StateProviderBox
55-
pub type RuRevmState = trevm::revm::database::State<
56-
reth::revm::database::StateProviderDatabase<reth::providers::StateProviderBox>,
57-
>;
58-
5951
/// The maximum number of headers we read at once when handling a range filter.
6052
const MAX_HEADERS_RANGE: u64 = 1_000; // with ~530bytes per header this is ~500kb
6153

62-
/// RPC context. Contains all necessary host and signet components for serving
63-
/// RPC requests.
64-
#[derive(Debug)]
65-
pub struct RpcCtx<Host, Signet>
66-
where
67-
Host: FullNodeComponents,
68-
Signet: Pnt,
69-
{
70-
inner: Arc<RpcCtxInner<Host, Signet>>,
71-
}
72-
73-
impl<Host, Signet> RpcCtx<Host, Signet>
74-
where
75-
Host: FullNodeComponents,
76-
Signet: Pnt,
77-
{
78-
/// Create a new `RpcCtx`.
79-
pub fn new<Tasks>(
80-
host: Host,
81-
constants: SignetSystemConstants,
82-
factory: ProviderFactory<Signet>,
83-
eth_config: EthConfig,
84-
tx_cache: Option<TxCache>,
85-
spawner: Tasks,
86-
) -> ProviderResult<Self>
87-
where
88-
Tasks: TaskSpawner + Clone + 'static,
89-
{
90-
RpcCtxInner::new(host, constants, factory, eth_config, tx_cache, spawner)
91-
.map(|inner| Self { inner: Arc::new(inner) })
92-
}
93-
}
94-
95-
impl<Host, Signet> Clone for RpcCtx<Host, Signet>
96-
where
97-
Host: FullNodeComponents,
98-
Signet: Pnt,
99-
{
100-
fn clone(&self) -> Self {
101-
Self { inner: self.inner.clone() }
102-
}
103-
}
104-
105-
impl<Host, Signet> core::ops::Deref for RpcCtx<Host, Signet>
106-
where
107-
Host: FullNodeComponents,
108-
Signet: Pnt,
109-
{
110-
type Target = RpcCtxInner<Host, Signet>;
111-
112-
fn deref(&self) -> &Self::Target {
113-
&self.inner
114-
}
115-
}
116-
117-
/// Inner context for [`RpcCtx`].
118-
#[derive(Debug)]
119-
pub struct RpcCtxInner<Host, Signet>
120-
where
121-
Host: FullNodeComponents,
122-
Signet: Pnt,
123-
{
124-
host: Host,
125-
signet: SignetCtx<Signet>,
126-
}
127-
128-
impl<Host, Signet> RpcCtxInner<Host, Signet>
129-
where
130-
Host: FullNodeComponents,
131-
Signet: Pnt,
132-
{
133-
/// Create a new `RpcCtxInner`.
134-
pub fn new<Tasks>(
135-
host: Host,
136-
constants: SignetSystemConstants,
137-
factory: ProviderFactory<Signet>,
138-
eth_config: EthConfig,
139-
tx_cache: Option<TxCache>,
140-
spawner: Tasks,
141-
) -> ProviderResult<Self>
142-
where
143-
Tasks: TaskSpawner + Clone + 'static,
144-
{
145-
SignetCtx::new(constants, factory, eth_config, tx_cache, spawner)
146-
.map(|signet| Self { host, signet })
147-
}
148-
149-
pub const fn host(&self) -> &Host {
150-
&self.host
151-
}
152-
153-
pub const fn signet(&self) -> &SignetCtx<Signet> {
154-
&self.signet
155-
}
156-
157-
pub fn task_executor(&self) -> &TaskExecutor {
158-
self.host.task_executor()
159-
}
160-
161-
/// Create a trevm instance.
162-
pub fn trevm(
163-
&self,
164-
block_id: BlockId,
165-
block: &Header,
166-
) -> Result<EvmNeedsTx<RuRevmState>, EthApiError> {
167-
// decrement if the id is pending, so that the state is on the latest block
168-
let height = block.number() - block_id.is_pending() as u64;
169-
let spec_id = self.signet.evm_spec_id(block);
170-
171-
let db = self.signet.state_provider_database(height)?;
172-
173-
let mut trevm = signet_evm::signet_evm(db, self.signet.constants.clone())
174-
.fill_cfg(&self.signet)
175-
.fill_block(block);
176-
177-
trevm.set_spec_id(spec_id);
178-
179-
Ok(trevm)
180-
}
181-
}
182-
18354
/// Signet context. This struct contains all the necessary components for
18455
/// accessing Signet node state, and serving RPC requests.
18556
#[derive(Debug)]
@@ -307,7 +178,7 @@ where
307178

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

crates/rpc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod config;
5353
pub use config::{RpcServerGuard, ServeConfig};
5454

5555
mod ctx;
56-
pub use ctx::{RpcCtx, RuRevmState};
56+
pub use ctx::{RpcCtx, RuRevmState, SignetCtx};
5757

5858
mod eth;
5959
pub use eth::{CallErrorData, EthError, eth};

0 commit comments

Comments
 (0)