Skip to content

Commit

Permalink
feat: NodePrimitivesProvider (#12855)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Nov 26, 2024
1 parent b34fb78 commit 26fc701
Show file tree
Hide file tree
Showing 40 changed files with 239 additions and 148 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

24 changes: 4 additions & 20 deletions crates/blockchain-tree/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use alloy_primitives::{BlockHash, BlockNumber};
use reth_consensus::Consensus;
use reth_db::{static_file::BlockHashMask, tables};
use reth_db_api::{cursor::DbCursorRO, transaction::DbTx};
use reth_node_types::{FullNodePrimitives, NodeTypesWithDB};
use reth_primitives::StaticFileSegment;
use reth_node_types::NodeTypesWithDB;
use reth_primitives::{EthPrimitives, StaticFileSegment};
use reth_provider::{
providers::{NodeTypesForProvider, ProviderNodeTypes},
ChainStateBlockReader, ChainStateBlockWriter, ProviderFactory, StaticFileProviderFactory,
Expand All @@ -15,25 +15,9 @@ use reth_storage_errors::provider::ProviderResult;
use std::{collections::BTreeMap, sync::Arc};

/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within [`TreeExternals`].
pub trait NodeTypesForTree:
NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
{
}
pub trait NodeTypesForTree: NodeTypesForProvider<Primitives = EthPrimitives> {}

impl<T> NodeTypesForTree for T where
T: NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
{
}
impl<T> NodeTypesForTree for T where T: NodeTypesForProvider<Primitives = EthPrimitives> {}

/// A helper trait with requirements for [`ProviderNodeTypes`] to be used within [`TreeExternals`].
pub trait TreeNodeTypes: ProviderNodeTypes + NodeTypesForTree {}
Expand Down
8 changes: 6 additions & 2 deletions crates/blockchain-tree/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use reth_blockchain_tree_api::{
BlockValidationKind, BlockchainTreeEngine, BlockchainTreeViewer, CanonicalOutcome,
InsertPayloadOk,
};
use reth_primitives::{Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader};
use reth_primitives::{EthPrimitives, Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader};
use reth_provider::{
BlockchainTreePendingStateProvider, CanonStateNotificationSender, CanonStateNotifications,
CanonStateSubscriptions, FullExecutionDataProvider,
CanonStateSubscriptions, FullExecutionDataProvider, NodePrimitivesProvider,
};
use reth_storage_errors::provider::ProviderResult;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -126,6 +126,10 @@ impl BlockchainTreePendingStateProvider for NoopBlockchainTree {
}
}

impl NodePrimitivesProvider for NoopBlockchainTree {
type Primitives = EthPrimitives;
}

impl CanonStateSubscriptions for NoopBlockchainTree {
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications {
self.canon_state_notification_sender
Expand Down
12 changes: 10 additions & 2 deletions crates/blockchain-tree/src/shareable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use reth_node_types::NodeTypesWithDB;
use reth_primitives::{Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader};
use reth_provider::{
providers::ProviderNodeTypes, BlockchainTreePendingStateProvider, CanonStateNotifications,
CanonStateSubscriptions, FullExecutionDataProvider, ProviderError,
CanonStateSubscriptions, FullExecutionDataProvider, NodePrimitivesProvider, ProviderError,
};
use reth_storage_errors::provider::ProviderResult;
use std::{collections::BTreeMap, sync::Arc};
Expand Down Expand Up @@ -185,10 +185,18 @@ where
}
}

impl<N, E> CanonStateSubscriptions for ShareableBlockchainTree<N, E>
impl<N, E> NodePrimitivesProvider for ShareableBlockchainTree<N, E>
where
N: ProviderNodeTypes,
E: Send + Sync,
{
type Primitives = N::Primitives;
}

impl<N, E> CanonStateSubscriptions for ShareableBlockchainTree<N, E>
where
N: TreeNodeTypes,
E: Send + Sync,
{
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications {
trace!(target: "blockchain_tree", "Registered subscriber for canonical state");
Expand Down
1 change: 0 additions & 1 deletion crates/chain-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ tokio-stream = { workspace = true, features = ["sync"] }
tracing.workspace = true

# misc
auto_impl.workspace = true
derive_more.workspace = true
metrics.workspace = true
parking_lot.workspace = true
Expand Down
19 changes: 14 additions & 5 deletions crates/chain-state/src/notifications.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Canonical chain state notification trait and types.
use auto_impl::auto_impl;
use derive_more::{Deref, DerefMut};
use reth_execution_types::{BlockReceipts, Chain};
use reth_primitives::{NodePrimitives, SealedBlockWithSenders, SealedHeader};
use reth_storage_api::NodePrimitivesProvider;
use std::{
pin::Pin,
sync::Arc,
Expand All @@ -25,21 +25,30 @@ pub type CanonStateNotificationSender<N = reth_primitives::EthPrimitives> =
broadcast::Sender<CanonStateNotification<N>>;

/// A type that allows to register chain related event subscriptions.
#[auto_impl(&, Arc)]
pub trait CanonStateSubscriptions: Send + Sync {
pub trait CanonStateSubscriptions: NodePrimitivesProvider + Send + Sync {
/// Get notified when a new canonical chain was imported.
///
/// A canonical chain be one or more blocks, a reorg or a revert.
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications;
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications<Self::Primitives>;

/// Convenience method to get a stream of [`CanonStateNotification`].
fn canonical_state_stream(&self) -> CanonStateNotificationStream {
fn canonical_state_stream(&self) -> CanonStateNotificationStream<Self::Primitives> {
CanonStateNotificationStream {
st: BroadcastStream::new(self.subscribe_to_canonical_state()),
}
}
}

impl<T: CanonStateSubscriptions> CanonStateSubscriptions for &T {
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications<Self::Primitives> {
(*self).subscribe_to_canonical_state()
}

fn canonical_state_stream(&self) -> CanonStateNotificationStream<Self::Primitives> {
(*self).canonical_state_stream()
}
}

/// A Stream of [`CanonStateNotification`].
#[derive(Debug)]
#[pin_project::pin_project]
Expand Down
10 changes: 8 additions & 2 deletions crates/chain-state/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ use reth_chainspec::{ChainSpec, EthereumHardfork, MIN_TRANSACTION_GAS};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
proofs::{calculate_receipt_root, calculate_transaction_root, calculate_withdrawals_root},
BlockBody, NodePrimitives, Receipt, Receipts, SealedBlock, SealedBlockWithSenders,
SealedHeader, Transaction, TransactionSigned, TransactionSignedEcRecovered,
BlockBody, EthPrimitives, NodePrimitives, Receipt, Receipts, SealedBlock,
SealedBlockWithSenders, SealedHeader, Transaction, TransactionSigned,
TransactionSignedEcRecovered,
};
use reth_storage_api::NodePrimitivesProvider;
use reth_trie::{root::state_root_unhashed, updates::TrieUpdates, HashedPostState};
use revm::{db::BundleState, primitives::AccountInfo};
use std::{
Expand Down Expand Up @@ -314,6 +316,10 @@ impl TestCanonStateSubscriptions {
}
}

impl NodePrimitivesProvider for TestCanonStateSubscriptions {
type Primitives = EthPrimitives;
}

impl CanonStateSubscriptions for TestCanonStateSubscriptions {
/// Sets up a broadcast channel with a buffer size of 100.
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications {
Expand Down
18 changes: 3 additions & 15 deletions crates/cli/commands/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use reth_db::{init_db, open_db_read_only, DatabaseEnv};
use reth_db_common::init::init_genesis;
use reth_downloaders::{bodies::noop::NoopBodiesDownloader, headers::noop::NoopHeaderDownloader};
use reth_evm::noop::NoopBlockExecutorProvider;
use reth_node_api::FullNodePrimitives;
use reth_node_builder::{NodeTypesWithDBAdapter, NodeTypesWithEngine};
use reth_node_core::{
args::{DatabaseArgs, DatadirArgs},
dirs::{ChainPath, DataDirPath},
};
use reth_primitives::EthPrimitives;
use reth_provider::{
providers::{NodeTypesForProvider, StaticFileProvider},
ProviderFactory, StaticFileProviderFactory,
Expand Down Expand Up @@ -196,22 +196,10 @@ impl AccessRights {
/// Helper trait with a common set of requirements for the
/// [`NodeTypes`](reth_node_builder::NodeTypes) in CLI.
pub trait CliNodeTypes:
NodeTypesWithEngine
+ NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
NodeTypesWithEngine + NodeTypesForProvider<Primitives = EthPrimitives>
{
}
impl<N> CliNodeTypes for N where
N: NodeTypesWithEngine
+ NodeTypesForProvider<
Primitives: FullNodePrimitives<
Block = reth_primitives::Block,
BlockBody = reth_primitives::BlockBody,
>,
>
N: NodeTypesWithEngine + NodeTypesForProvider<Primitives = EthPrimitives>
{
}
7 changes: 3 additions & 4 deletions crates/e2e-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use node::NodeTestContext;
use reth::{
args::{DiscoveryArgs, NetworkArgs, RpcServerArgs},
blockchain_tree::externals::NodeTypesForTree,
builder::{FullNodePrimitives, NodeBuilder, NodeConfig, NodeHandle},
builder::{NodeBuilder, NodeConfig, NodeHandle},
network::PeersHandleProvider,
primitives::EthPrimitives,
rpc::server_types::RpcModuleSelection,
tasks::TaskManager,
};
Expand Down Expand Up @@ -121,7 +122,7 @@ pub async fn setup_engine<N>(
where
N: Default
+ Node<TmpNodeAdapter<N, BlockchainProvider2<NodeTypesWithDBAdapter<N, TmpDB>>>>
+ NodeTypesWithEngine
+ NodeTypesWithEngine<Primitives = EthPrimitives>
+ NodeTypesForProvider,
N::ComponentsBuilder: NodeComponentsBuilder<
TmpNodeAdapter<N, BlockchainProvider2<NodeTypesWithDBAdapter<N, TmpDB>>>,
Expand All @@ -134,8 +135,6 @@ where
LocalPayloadAttributesBuilder<N::ChainSpec>: PayloadAttributesBuilder<
<<N as NodeTypesWithEngine>::Engine as PayloadTypes>::PayloadAttributes,
>,
N::Primitives:
FullNodePrimitives<Block = reth_primitives::Block, BlockBody = reth_primitives::BlockBody>,
{
let tasks = TaskManager::current();
let exec = tasks.executor();
Expand Down
7 changes: 4 additions & 3 deletions crates/e2e-test-utils/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{marker::PhantomData, pin::Pin};

use alloy_consensus::{BlockHeader, Sealable};
use alloy_consensus::BlockHeader;
use alloy_primitives::{BlockHash, BlockNumber, Bytes, B256};
use alloy_rpc_types_eth::BlockNumberOrTag;
use eyre::Ok;
Expand All @@ -16,8 +16,9 @@ use reth::{
},
};
use reth_chainspec::EthereumHardforks;
use reth_node_api::{Block, FullBlock, NodePrimitives};
use reth_node_api::Block;
use reth_node_builder::{rpc::RethRpcAddOns, NodeTypes, NodeTypesWithEngine};
use reth_primitives::EthPrimitives;
use reth_stages_types::StageId;
use tokio_stream::StreamExt;
use url::Url;
Expand Down Expand Up @@ -56,7 +57,7 @@ where
Node::Types: NodeTypesWithEngine<
ChainSpec: EthereumHardforks,
Engine = Engine,
Primitives: NodePrimitives<Block: FullBlock>,
Primitives = EthPrimitives,
>,
Node::Network: PeersHandleProvider,
AddOns: RethRpcAddOns<Node>,
Expand Down
6 changes: 3 additions & 3 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub struct EthereumPoolBuilder {

impl<Types, Node> PoolBuilder<Node> for EthereumPoolBuilder
where
Types: NodeTypesWithEngine<ChainSpec = ChainSpec>,
Types: NodeTypesWithEngine<ChainSpec = ChainSpec, Primitives = EthPrimitives>,
Node: FullNodeTypes<Types = Types>,
{
type Pool = EthTransactionPool<Node::Provider, DiskFileBlobStore>;
Expand Down Expand Up @@ -240,7 +240,7 @@ impl EthereumPayloadBuilder {
pool: Pool,
) -> eyre::Result<PayloadBuilderHandle<Types::Engine>>
where
Types: NodeTypesWithEngine<ChainSpec = ChainSpec>,
Types: NodeTypesWithEngine<ChainSpec = ChainSpec, Primitives = EthPrimitives>,
Node: FullNodeTypes<Types = Types>,
Evm: ConfigureEvm<Header = Header>,
Pool: TransactionPool + Unpin + 'static,
Expand Down Expand Up @@ -278,7 +278,7 @@ impl EthereumPayloadBuilder {

impl<Types, Node, Pool> PayloadServiceBuilder<Node, Pool> for EthereumPayloadBuilder
where
Types: NodeTypesWithEngine<ChainSpec = ChainSpec>,
Types: NodeTypesWithEngine<ChainSpec = ChainSpec, Primitives = EthPrimitives>,
Node: FullNodeTypes<Types = Types>,
Pool: TransactionPool + Unpin + 'static,
Types::Engine: PayloadTypes<
Expand Down
5 changes: 3 additions & 2 deletions crates/node/builder/src/launch/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use reth_node_core::{
primitives::Head,
};
use reth_node_events::{cl::ConsensusLayerHealthEvents, node};
use reth_primitives::EthereumHardforks;
use reth_primitives::{EthPrimitives, EthereumHardforks};
use reth_provider::providers::{BlockchainProvider2, ProviderNodeTypes};
use reth_tasks::TaskExecutor;
use reth_tokio_util::EventSender;
Expand Down Expand Up @@ -70,7 +70,8 @@ impl EngineNodeLauncher {

impl<Types, T, CB, AO> LaunchNode<NodeBuilderWithComponents<T, CB, AO>> for EngineNodeLauncher
where
Types: ProviderNodeTypes + NodeTypesWithEngine + PersistenceNodeTypes,
Types:
ProviderNodeTypes<Primitives = EthPrimitives> + NodeTypesWithEngine + PersistenceNodeTypes,
T: FullNodeTypes<Types = Types, Provider = BlockchainProvider2<Types>>,
CB: NodeComponentsBuilder<T>,
AO: RethRpcAddOns<NodeAdapter<T, CB::Components>>,
Expand Down
6 changes: 4 additions & 2 deletions crates/node/builder/src/launch/exex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reth_exex::{
DEFAULT_EXEX_MANAGER_CAPACITY,
};
use reth_node_api::{FullNodeComponents, NodeTypes};
use reth_primitives::Head;
use reth_primitives::{EthPrimitives, Head};
use reth_provider::CanonStateSubscriptions;
use reth_tracing::tracing::{debug, info};
use tracing::Instrument;
Expand All @@ -25,7 +25,9 @@ pub struct ExExLauncher<Node: FullNodeComponents> {
config_container: WithConfigs<<Node::Types as NodeTypes>::ChainSpec>,
}

impl<Node: FullNodeComponents + Clone> ExExLauncher<Node> {
impl<Node: FullNodeComponents<Types: NodeTypes<Primitives = EthPrimitives>> + Clone>
ExExLauncher<Node>
{
/// Create a new `ExExLauncher` with the given extensions.
pub const fn new(
head: Head,
Expand Down
9 changes: 6 additions & 3 deletions crates/node/builder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use reth_node_core::{
version::{CARGO_PKG_VERSION, CLIENT_CODE, NAME_CLIENT, VERGEN_GIT_SHA},
};
use reth_payload_builder::PayloadStore;
use reth_primitives::EthPrimitives;
use reth_provider::providers::ProviderNodeTypes;
use reth_rpc::{
eth::{EthApiTypes, FullEthApiServer},
Expand Down Expand Up @@ -402,7 +403,7 @@ where
impl<N, EthApi, EV> RpcAddOns<N, EthApi, EV>
where
N: FullNodeComponents<
Types: ProviderNodeTypes,
Types: ProviderNodeTypes<Primitives = EthPrimitives>,
PayloadBuilder: PayloadBuilder<PayloadType = <N::Types as NodeTypesWithEngine>::Engine>,
>,
EthApi: EthApiTypes + FullEthApiServer + AddDevSigners + Unpin + 'static,
Expand Down Expand Up @@ -524,7 +525,7 @@ where
impl<N, EthApi, EV> NodeAddOns<N> for RpcAddOns<N, EthApi, EV>
where
N: FullNodeComponents<
Types: ProviderNodeTypes,
Types: ProviderNodeTypes<Primitives = EthPrimitives>,
PayloadBuilder: PayloadBuilder<PayloadType = <N::Types as NodeTypesWithEngine>::Engine>,
>,
EthApi: EthApiTypes + FullEthApiServer + AddDevSigners + Unpin + 'static,
Expand Down Expand Up @@ -566,7 +567,9 @@ pub trait EthApiBuilder<N: FullNodeComponents>: 'static {
fn build(ctx: &EthApiBuilderCtx<N>) -> Self;
}

impl<N: FullNodeComponents> EthApiBuilder<N> for EthApi<N::Provider, N::Pool, N::Network, N::Evm> {
impl<N: FullNodeComponents<Types: NodeTypes<Primitives = EthPrimitives>>> EthApiBuilder<N>
for EthApi<N::Provider, N::Pool, N::Network, N::Evm>
{
fn build(ctx: &EthApiBuilderCtx<N>) -> Self {
Self::with_spawner(ctx)
}
Expand Down
Loading

0 comments on commit 26fc701

Please sign in to comment.