Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add docs for instantiating EthFilter #14224

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use alloc::{sync::Arc, vec::Vec};
use alloy_consensus::{BlockHeader, Header};
use alloy_primitives::{Address, U256};
use core::{convert::Infallible, fmt::Debug};
use reth_chainspec::{ChainSpec, EthChainSpec};
use reth_chainspec::{ChainSpec, EthChainSpec, MAINNET};
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, Database, Evm, NextBlockEnvAttributes};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::transaction::execute::FillTxEnv;
Expand Down Expand Up @@ -130,6 +130,11 @@ impl EthEvmConfig {
Self { chain_spec }
}

/// Creates a new Ethereum EVM configuration for the ethereum mainnet.
pub fn mainnet() -> Self {
Self::new(MAINNET.clone())
}

/// Returns the chain spec associated with this configuration.
pub const fn chain_spec(&self) -> &Arc<ChainSpec> {
&self.chain_spec
Expand Down
19 changes: 12 additions & 7 deletions crates/rpc/rpc-eth-api/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
//! Trait for specifying `eth` network dependent API types.

use std::{
error::Error,
fmt::{self},
};

use crate::{AsEthApiError, FromEthApiError, RpcNodeCore};
use alloy_network::Network;
use alloy_rpc_types_eth::Block;
use reth_provider::{ProviderTx, ReceiptProvider, TransactionsProvider};
use reth_rpc_types_compat::TransactionCompat;
use reth_transaction_pool::{PoolTransaction, TransactionPool};

use crate::{AsEthApiError, FromEthApiError, RpcNodeCore};
use std::{
error::Error,
fmt::{self},
};

/// Network specific `eth` API types.
///
/// This trait defines the network specific rpc types and helpers required for the `eth_` and
/// adjacent endpoints. `NetworkTypes` is [`Network`] as defined by the alloy crate, see also
/// [`alloy_network::Ethereum`].
///
/// This type is stateful so that it can provide additional context if necessary, e.g. populating
/// receipts with additional data.
pub trait EthApiTypes: Send + Sync + Clone {
/// Extension of [`FromEthApiError`], with network specific errors.
type Error: Into<jsonrpsee_types::error::ErrorObject<'static>>
Expand Down
31 changes: 30 additions & 1 deletion crates/rpc/rpc/src/eth/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const DEFAULT_BROADCAST_CAPACITY: usize = 2000;
/// separately in submodules. The rpc handler implementation can then delegate to the main impls.
/// This way [`EthApi`] is not limited to [`jsonrpsee`] and can be used standalone or in other
/// network handlers (for example ipc).
///
/// ## Trait requirements
///
/// While this type requires various unrestricted generic components, trait bounds are enforced when
/// additional traits are implemented for this type.
#[derive(Deref)]
pub struct EthApi<Provider: BlockReader, Pool, Network, EvmConfig> {
/// All nested fields bundled together.
Expand All @@ -62,7 +67,31 @@ impl<Provider, Pool, Network, EvmConfig> EthApi<Provider, Pool, Network, EvmConf
where
Provider: BlockReaderIdExt,
{
/// Convenience fn to obtain a new [`EthApiBuilder`] instance with mandatory components
/// Convenience fn to obtain a new [`EthApiBuilder`] instance with mandatory components.
///
/// Creating an [`EthApi`] requires a few mandatory compontents:
/// - provider: The type responsible for fetching requested data from disk.
/// - transaction pool: To interact with the pool, submitting new transactions (e.g.
/// `eth_sendRawTransactions`).
/// - network: required to handle requests related to network state (e.g. `eth_syncing`).
/// - evm config: Knows how create a new EVM instance to transact,estimate,call,trace.
///
/// # Create an instance with noop ethereum implementations
///
/// ```no_run
/// use reth_evm_ethereum::EthEvmConfig;
/// use reth_network_api::noop::NoopNetwork;
/// use reth_provider::noop::NoopProvider;
/// use reth_rpc::EthApi;
/// use reth_transaction_pool::noop::NoopTransactionPool;
/// let eth_api = EthApi::builder(
/// NoopProvider::default(),
/// NoopTransactionPool::default(),
/// NoopNetwork::default(),
/// EthEvmConfig::mainnet(),
/// )
/// .build();
/// ```
pub fn builder(
provider: Provider,
pool: Pool,
Expand Down
21 changes: 21 additions & 0 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use tracing::{error, trace};
const MAX_HEADERS_RANGE: u64 = 1_000; // with ~530bytes per header this is ~500kb

/// `Eth` filter RPC implementation.
///
/// This type handles `eth_` rpc requests related to filters (`eth_getLogs`).
pub struct EthFilter<Eth: EthApiTypes> {
/// All nested fields bundled together
inner: Arc<EthFilterInner<Eth>>,
Expand All @@ -69,6 +71,25 @@ where
/// See also [`EthFilterConfig`].
///
/// This also spawns a task that periodically clears stale filters.
///
/// # Create a new instance with [`EthApi`](crate::EthApi)
///
/// ```no_run
/// use reth_evm_ethereum::EthEvmConfig;
/// use reth_network_api::noop::NoopNetwork;
/// use reth_provider::noop::NoopProvider;
/// use reth_rpc::{EthApi, EthFilter};
/// use reth_tasks::TokioTaskExecutor;
/// use reth_transaction_pool::noop::NoopTransactionPool;
/// let eth_api = EthApi::builder(
/// NoopProvider::default(),
/// NoopTransactionPool::default(),
/// NoopNetwork::default(),
/// EthEvmConfig::mainnet(),
/// )
/// .build();
/// let filter = EthFilter::new(eth_api, Default::default(), TokioTaskExecutor::default().boxed());
/// ```
pub fn new(eth_api: Eth, config: EthFilterConfig, task_spawner: Box<dyn TaskSpawner>) -> Self {
let EthFilterConfig { max_blocks_per_filter, max_logs_per_response, stale_filter_ttl } =
config;
Expand Down
Loading