|
| 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. |
0 commit comments