Skip to content

Commit

Permalink
chore(sdk): add helper trait to node API to simplify type definition (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane authored Oct 26, 2024
1 parent ac329bf commit 44e4c47
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 61 deletions.
5 changes: 1 addition & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/consensus/debug-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ workspace = true
[dependencies]
# reth
reth-node-api.workspace = true
reth-rpc-api.workspace = true
reth-rpc-api = { workspace = true, features = ["client"] }
reth-rpc-builder.workspace = true
reth-tracing.workspace = true

Expand Down
7 changes: 5 additions & 2 deletions crates/e2e-test-utils/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use alloy_primitives::{Bytes, B256};
use reth::{
builder::{rpc::RpcRegistry, FullNodeComponents},
rpc::api::{
eth::helpers::{EthApiSpec, EthTransactions, TraceExt},
eth::{
helpers::{EthApiSpec, EthTransactions, TraceExt},
EthApiTypes,
},
DebugApiServer,
},
};
use reth_chainspec::EthereumHardforks;
use reth_node_builder::{EthApiTypes, NodeTypes};
use reth_node_builder::NodeTypes;

#[allow(missing_debug_implementations)]
pub struct RpcTestContext<Node: FullNodeComponents, EthApi: EthApiTypes> {
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/node/tests/e2e/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::utils::eth_payload_attributes;
use alloy_genesis::Genesis;
use alloy_primitives::{b256, hex};
use futures::StreamExt;
use reth::{args::DevArgs, core::rpc::eth::helpers::EthTransactions};
use reth::{args::DevArgs, rpc::api::eth::helpers::EthTransactions};
use reth_chainspec::ChainSpec;
use reth_e2e_test_utils::setup;
use reth_node_api::FullNodeComponents;
Expand Down
1 change: 0 additions & 1 deletion crates/node/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ reth-transaction-pool.workspace = true
reth-payload-builder.workspace = true
reth-payload-primitives.workspace = true
reth-tasks.workspace = true
reth-rpc-eth-api.workspace = true
reth-network-api.workspace = true
reth-node-types.workspace = true
reth-primitives.workspace = true
Expand Down
2 changes: 0 additions & 2 deletions crates/node/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ pub use node::*;
// re-export for convenience
pub use reth_node_types::*;
pub use reth_provider::FullProvider;

pub use reth_rpc_eth_api::EthApiTypes;
17 changes: 8 additions & 9 deletions crates/node/builder/src/launch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ use reth_consensus_debug_client::{DebugConsensusClient, EtherscanBlockProvider,
use reth_engine_util::EngineMessageStreamExt;
use reth_exex::ExExManagerHandle;
use reth_network::{BlockDownloaderProvider, NetworkEventListenerProvider};
use reth_node_api::{
AddOnsContext, FullNodeComponents, FullNodeTypes, NodeTypesWithDB, NodeTypesWithEngine,
};
use reth_node_api::{AddOnsContext, FullNodeTypes, NodeTypesWithDB, NodeTypesWithEngine};
use reth_node_core::{
dirs::{ChainPath, DataDirPath},
exit::NodeExitFuture,
};
use reth_node_events::{cl::ConsensusLayerHealthEvents, node};
use reth_provider::providers::BlockchainProvider;
use reth_rpc::eth::RpcNodeCore;
use reth_tasks::TaskExecutor;
use reth_tracing::tracing::{debug, info};
use reth_transaction_pool::TransactionPool;
Expand All @@ -47,14 +46,14 @@ use crate::{
AddOns, NodeBuilderWithComponents, NodeHandle,
};

/// Alias for [`reth_rpc_eth_types::EthApiBuilderCtx`], adapter for [`FullNodeComponents`].
/// Alias for [`reth_rpc_eth_types::EthApiBuilderCtx`], adapter for [`RpcNodeCore`].
pub type EthApiBuilderCtx<N> = reth_rpc_eth_types::EthApiBuilderCtx<
<N as FullNodeTypes>::Provider,
<N as FullNodeComponents>::Pool,
<N as FullNodeComponents>::Evm,
<N as FullNodeComponents>::Network,
<N as RpcNodeCore>::Provider,
<N as RpcNodeCore>::Pool,
<N as RpcNodeCore>::Evm,
<N as RpcNodeCore>::Network,
TaskExecutor,
<N as FullNodeTypes>::Provider,
<N as RpcNodeCore>::Provider,
>;

/// A general purpose trait that launches a new node of any kind.
Expand Down
2 changes: 1 addition & 1 deletion crates/node/builder/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use reth_node_api::{EngineTypes, FullNodeComponents};
use reth_node_core::{
dirs::{ChainPath, DataDirPath},
node_config::NodeConfig,
rpc::api::EngineApiClient,
};
use reth_payload_builder::PayloadBuilderHandle;
use reth_provider::ChainSpecProvider;
use reth_rpc_api::EngineApiClient;
use reth_rpc_builder::{auth::AuthServerHandle, RpcServerHandle};
use reth_tasks::TaskExecutor;

Expand Down
6 changes: 4 additions & 2 deletions crates/node/builder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use reth_node_api::{
};
use reth_node_core::{
node_config::NodeConfig,
rpc::eth::{EthApiTypes, FullEthApiServer},
version::{CARGO_PKG_VERSION, CLIENT_CODE, NAME_CLIENT, VERGEN_GIT_SHA},
};
use reth_payload_builder::PayloadBuilderHandle;
use reth_provider::providers::ProviderNodeTypes;
use reth_rpc::EthApi;
use reth_rpc::{
eth::{EthApiTypes, FullEthApiServer},
EthApi,
};
use reth_rpc_api::eth::helpers::AddDevSigners;
use reth_rpc_builder::{
auth::{AuthRpcModule, AuthServerHandle},
Expand Down
4 changes: 1 addition & 3 deletions crates/node/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ reth-network-p2p.workspace = true
reth-rpc-eth-types.workspace = true
reth-rpc-server-types.workspace = true
reth-rpc-types-compat.workspace = true
reth-rpc-api = { workspace = true, features = ["client"] }
reth-rpc-eth-api = { workspace = true, features = ["client"] }
reth-transaction-pool.workspace = true
reth-tracing.workspace = true
reth-config.workspace = true
Expand All @@ -38,7 +36,7 @@ reth-stages-types.workspace = true

# ethereum
alloy-primitives.workspace = true
alloy-rpc-types-engine = { workspace = true, features = ["jwt"] }
alloy-rpc-types-engine = { workspace = true, features = ["std", "jwt"] }
alloy-consensus.workspace = true
alloy-eips.workspace = true

Expand Down
9 changes: 0 additions & 9 deletions crates/node/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ pub mod primitives {

/// Re-export of `reth_rpc_*` crates.
pub mod rpc {
/// Re-exported from `reth_rpc_api`.
pub mod api {
pub use reth_rpc_api::*;
}
/// Re-exported from `reth_rpc::eth`.
pub mod eth {
pub use reth_rpc_eth_api::*;
}

/// Re-exported from `reth_rpc::rpc`.
pub mod result {
pub use reth_rpc_server_types::result::*;
Expand Down
40 changes: 18 additions & 22 deletions crates/optimism/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ use op_alloy_network::Optimism;
use reth_chainspec::EthereumHardforks;
use reth_evm::ConfigureEvm;
use reth_network_api::NetworkInfo;
use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeTypes};
use reth_node_api::{FullNodeComponents, NodeTypes};
use reth_node_builder::EthApiBuilderCtx;
use reth_primitives::Header;
use reth_provider::{
BlockIdReader, BlockNumReader, BlockReaderIdExt, ChainSpecProvider, HeaderProvider,
StageCheckpointReader, StateProviderFactory,
BlockIdReader, BlockNumReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider,
HeaderProvider, StageCheckpointReader, StateProviderFactory,
};
use reth_rpc::eth::{core::EthApiInner, DevSigner};
use reth_rpc_eth_api::{
helpers::{
AddDevSigners, EthApiSpec, EthFees, EthSigner, EthState, LoadBlock, LoadFee, LoadState,
SpawnBlocking, Trace,
},
EthApiTypes,
EthApiTypes, RpcNodeCore,
};
use reth_rpc_eth_types::{EthStateCache, FeeHistoryCache, GasPriceOracle};
use reth_tasks::{
Expand All @@ -43,10 +43,10 @@ use crate::{OpEthApiError, SequencerClient};

/// Adapter for [`EthApiInner`], which holds all the data required to serve core `eth_` API.
pub type EthApiNodeBackend<N> = EthApiInner<
<N as FullNodeTypes>::Provider,
<N as FullNodeComponents>::Pool,
<N as FullNodeComponents>::Network,
<N as FullNodeComponents>::Evm,
<N as RpcNodeCore>::Provider,
<N as RpcNodeCore>::Pool,
<N as RpcNodeCore>::Network,
<N as RpcNodeCore>::Evm,
>;

/// OP-Reth `Eth` API implementation.
Expand All @@ -59,8 +59,8 @@ pub type EthApiNodeBackend<N> = EthApiInner<
///
/// This type implements the [`FullEthApi`](reth_rpc_eth_api::helpers::FullEthApi) by implemented
/// all the `Eth` helper traits and prerequisite traits.
#[derive(Deref)]
pub struct OpEthApi<N: FullNodeComponents> {
#[derive(Deref, Clone)]
pub struct OpEthApi<N: RpcNodeCore> {
/// Gateway to node's core components.
#[deref]
inner: Arc<EthApiNodeBackend<N>>,
Expand All @@ -69,7 +69,12 @@ pub struct OpEthApi<N: FullNodeComponents> {
sequencer_client: Option<SequencerClient>,
}

impl<N: FullNodeComponents> OpEthApi<N> {
impl<N> OpEthApi<N>
where
N: RpcNodeCore<
Provider: BlockReaderIdExt + ChainSpecProvider + CanonStateSubscriptions + Clone + 'static,
>,
{
/// Creates a new instance for given context.
pub fn new(ctx: &EthApiBuilderCtx<N>, sequencer_http: Option<String>) -> Self {
let blocking_task_pool =
Expand Down Expand Up @@ -98,7 +103,7 @@ impl<N: FullNodeComponents> OpEthApi<N> {
impl<N> EthApiTypes for OpEthApi<N>
where
Self: Send + Sync,
N: FullNodeComponents,
N: RpcNodeCore,
{
type Error = OpEthApiError;
type NetworkTypes = Optimism;
Expand Down Expand Up @@ -248,17 +253,8 @@ where
}
}

impl<N: FullNodeComponents> fmt::Debug for OpEthApi<N> {
impl<N: RpcNodeCore> fmt::Debug for OpEthApi<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OpEthApi").finish_non_exhaustive()
}
}

impl<N> Clone for OpEthApi<N>
where
N: FullNodeComponents,
{
fn clone(&self) -> Self {
Self { inner: self.inner.clone(), sequencer_client: self.sequencer_client.clone() }
}
}
1 change: 1 addition & 0 deletions crates/rpc/rpc-eth-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ reth-rpc-eth-types.workspace = true
reth-rpc-server-types.workspace = true
reth-network-api.workspace = true
reth-trie.workspace = true
reth-node-api.workspace = true

# ethereum
alloy-eips.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/rpc/rpc-eth-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod bundle;
pub mod core;
pub mod filter;
pub mod helpers;
pub mod node;
pub mod pubsub;
pub mod types;

Expand All @@ -25,6 +26,7 @@ pub use bundle::{EthBundleApiServer, EthCallBundleApiServer};
pub use core::{EthApiServer, FullEthApiServer};
pub use filter::EthFilterApiServer;
pub use helpers::error::{AsEthApiError, FromEthApiError, FromEvmError, IntoEthApiError};
pub use node::RpcNodeCore;
pub use pubsub::EthPubSubApiServer;
pub use types::{EthApiTypes, FullEthApiTypes, RpcBlock, RpcReceipt, RpcTransaction};

Expand Down
58 changes: 58 additions & 0 deletions crates/rpc/rpc-eth-api/src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Helper trait for interfacing with [`FullNodeComponents`].

use reth_node_api::FullNodeComponents;

/// Helper trait to relax trait bounds on [`FullNodeComponents`].
///
/// Helpful when defining types that would otherwise have a generic `N: FullNodeComponents`. Using
/// `N: RpcNodeCore` instead, allows access to all the associated types on [`FullNodeComponents`]
/// that are used in RPC, but with more flexibility since they have no trait bounds (asides auto
/// traits).
pub trait RpcNodeCore: Clone {
/// The provider type used to interact with the node.
type Provider: Send + Sync + Clone + Unpin;
/// The transaction pool of the node.
type Pool: Send + Sync + Clone + Unpin;
/// The node's EVM configuration, defining settings for the Ethereum Virtual Machine.
type Evm: Send + Sync + Clone + Unpin;
/// Network API.
type Network: Send + Sync + Clone;

/// Returns the transaction pool of the node.
fn pool(&self) -> &Self::Pool;

/// Returns the node's evm config.
fn evm_config(&self) -> &Self::Evm;

/// Returns the handle to the network
fn network(&self) -> &Self::Network;

/// Returns the provider of the node.
fn provider(&self) -> &Self::Provider;
}

impl<T> RpcNodeCore for T
where
T: FullNodeComponents,
{
type Provider = T::Provider;
type Pool = T::Pool;
type Network = <T as FullNodeComponents>::Network;
type Evm = <T as FullNodeComponents>::Evm;

fn pool(&self) -> &Self::Pool {
FullNodeComponents::pool(self)
}

fn evm_config(&self) -> &Self::Evm {
FullNodeComponents::evm_config(self)
}

fn network(&self) -> &Self::Network {
FullNodeComponents::network(self)
}

fn provider(&self) -> &Self::Provider {
FullNodeComponents::provider(self)
}
}
1 change: 0 additions & 1 deletion crates/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ reth-network-peers = { workspace = true, features = ["secp256k1"] }
reth-evm.workspace = true
reth-rpc-eth-types.workspace = true
reth-rpc-server-types.workspace = true
reth-node-api.workspace = true
reth-network-types.workspace = true
reth-trie.workspace = true

Expand Down
5 changes: 3 additions & 2 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ use alloy_rpc_types::{
use async_trait::async_trait;
use jsonrpsee::{core::RpcResult, server::IdProvider};
use reth_chainspec::ChainInfo;
use reth_node_api::EthApiTypes;
use reth_primitives::{Receipt, SealedBlockWithSenders, TransactionSignedEcRecovered};
use reth_provider::{BlockIdReader, BlockReader, EvmEnvProvider, ProviderError};
use reth_rpc_eth_api::{EthFilterApiServer, FullEthApiTypes, RpcTransaction, TransactionCompat};
use reth_rpc_eth_api::{
EthApiTypes, EthFilterApiServer, FullEthApiTypes, RpcTransaction, TransactionCompat,
};
use reth_rpc_eth_types::{
logs_utils::{self, append_matching_block_logs, ProviderOrBlock},
EthApiError, EthFilterConfig, EthStateCache, EthSubscriptionIdProvider,
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pub use pubsub::EthPubSub;

pub use helpers::{signer::DevSigner, types::EthTxBuilder};

pub use reth_rpc_eth_api::EthApiServer;
pub use reth_rpc_eth_api::{EthApiServer, EthApiTypes, FullEthApiServer, RpcNodeCore};

0 comments on commit 44e4c47

Please sign in to comment.