Skip to content

Commit

Permalink
chore(rpc): expose ethapi in node builder for op customisation (#9444)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
emhane and mattsse authored Jul 16, 2024
1 parent fcc6307 commit 2aa94e9
Show file tree
Hide file tree
Showing 53 changed files with 1,110 additions and 657 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/e2e-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ reth-node-builder = { workspace = true, features = ["test-utils"] }
reth-tokio-util.workspace = true
reth-stages-types.workspace = true
reth-network-peers.workspace = true
reth-node-ethereum.workspace = true

jsonrpsee.workspace = true

Expand Down
15 changes: 10 additions & 5 deletions crates/e2e-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::sync::Arc;

use node::NodeTestContext;
use reth::{
args::{DiscoveryArgs, NetworkArgs, RpcServerArgs},
builder::{NodeBuilder, NodeConfig, NodeHandle},
rpc::api::eth::{helpers::AddDevSigners, FullEthApiServer},
tasks::TaskManager,
};
use reth_chainspec::ChainSpec;
use reth_db::{test_utils::TempDatabase, DatabaseEnv};
use reth_node_builder::{
components::NodeComponentsBuilder, FullNodeTypesAdapter, Node, NodeAdapter, RethFullAdapter,
components::NodeComponentsBuilder, rpc::EthApiBuilderProvider, FullNodeTypesAdapter, Node,
NodeAdapter, NodeAddOns, RethFullAdapter,
};
use reth_provider::providers::BlockchainProvider;
use std::sync::Arc;
use tracing::{span, Level};
use wallet::Wallet;

Expand Down Expand Up @@ -42,9 +45,11 @@ pub async fn setup<N>(
num_nodes: usize,
chain_spec: Arc<ChainSpec>,
is_dev: bool,
) -> eyre::Result<(Vec<NodeHelperType<N>>, TaskManager, Wallet)>
) -> eyre::Result<(Vec<NodeHelperType<N, N::AddOns>>, TaskManager, Wallet)>
where
N: Default + Node<TmpNodeAdapter<N>>,
<N::AddOns as NodeAddOns<Adapter<N>>>::EthApi:
FullEthApiServer + AddDevSigners + EthApiBuilderProvider<Adapter<N>>,
{
let tasks = TaskManager::current();
let exec = tasks.executor();
Expand All @@ -55,7 +60,7 @@ where
};

// Create nodes and peer them
let mut nodes: Vec<NodeTestContext<_>> = Vec::with_capacity(num_nodes);
let mut nodes: Vec<NodeTestContext<_, _>> = Vec::with_capacity(num_nodes);

for idx in 0..num_nodes {
let node_config = NodeConfig::test()
Expand Down Expand Up @@ -106,4 +111,4 @@ type Adapter<N> = NodeAdapter<
>;

/// Type alias for a type of NodeHelper
pub type NodeHelperType<N> = NodeTestContext<Adapter<N>>;
pub type NodeHelperType<N, AO> = NodeTestContext<Adapter<N>, AO>;
33 changes: 20 additions & 13 deletions crates/e2e-test-utils/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
engine_api::EngineApiTestContext, network::NetworkTestContext, payload::PayloadTestContext,
rpc::RpcTestContext, traits::PayloadEnvelopeExt,
};
use std::{marker::PhantomData, pin::Pin};

use alloy_rpc_types::BlockNumberOrTag;
use eyre::Ok;
Expand All @@ -11,32 +8,41 @@ use reth::{
builder::FullNode,
payload::PayloadTypes,
providers::{BlockReader, BlockReaderIdExt, CanonStateSubscriptions, StageCheckpointReader},
rpc::types::engine::PayloadStatusEnum,
rpc::{
api::eth::helpers::{EthApiSpec, EthTransactions, TraceExt},
types::engine::PayloadStatusEnum,
},
};
use reth_node_builder::NodeTypes;
use reth_node_builder::{NodeAddOns, NodeTypes};
use reth_primitives::{BlockHash, BlockNumber, Bytes, B256};
use reth_stages_types::StageId;
use std::{marker::PhantomData, pin::Pin};
use tokio_stream::StreamExt;

use crate::{
engine_api::EngineApiTestContext, network::NetworkTestContext, payload::PayloadTestContext,
rpc::RpcTestContext, traits::PayloadEnvelopeExt,
};

/// An helper struct to handle node actions
pub struct NodeTestContext<Node>
pub struct NodeTestContext<Node, AddOns>
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
pub inner: FullNode<Node>,
pub inner: FullNode<Node, AddOns>,
pub payload: PayloadTestContext<Node::Engine>,
pub network: NetworkTestContext,
pub engine_api: EngineApiTestContext<Node::Engine>,
pub rpc: RpcTestContext<Node>,
pub rpc: RpcTestContext<Node, AddOns::EthApi>,
}

impl<Node> NodeTestContext<Node>
impl<Node, AddOns> NodeTestContext<Node, AddOns>
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
/// Creates a new test node
pub async fn new(node: FullNode<Node>) -> eyre::Result<Self> {
pub async fn new(node: FullNode<Node, AddOns>) -> eyre::Result<Self> {
let builder = node.payload_builder.clone();

Ok(Self {
Expand All @@ -53,7 +59,7 @@ where
}

/// Establish a connection to the node
pub async fn connect(&mut self, node: &mut NodeTestContext<Node>) {
pub async fn connect(&mut self, node: &mut NodeTestContext<Node, AddOns>) {
self.network.add_peer(node.network.record()).await;
node.network.next_session_established().await;
self.network.next_session_established().await;
Expand All @@ -77,6 +83,7 @@ where
where
<Node::Engine as EngineTypes>::ExecutionPayloadV3:
From<<Node::Engine as PayloadTypes>::BuiltPayload> + PayloadEnvelopeExt,
AddOns::EthApi: EthApiSpec + EthTransactions + TraceExt,
{
let mut chain = Vec::with_capacity(length as usize);
for i in 0..length {
Expand Down
14 changes: 10 additions & 4 deletions crates/e2e-test-utils/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ use alloy_network::eip2718::Decodable2718;
use reth::{
builder::{rpc::RpcRegistry, FullNodeComponents},
rpc::{
api::{eth::helpers::EthTransactions, DebugApiServer},
api::{
eth::helpers::{EthApiSpec, EthTransactions, TraceExt},
DebugApiServer,
},
server_types::eth::EthResult,
},
};
use reth_primitives::{Bytes, B256};

pub struct RpcTestContext<Node: FullNodeComponents> {
pub inner: RpcRegistry<Node>,
pub struct RpcTestContext<Node: FullNodeComponents, EthApi> {
pub inner: RpcRegistry<Node, EthApi>,
}

impl<Node: FullNodeComponents> RpcTestContext<Node> {
impl<Node: FullNodeComponents, EthApi> RpcTestContext<Node, EthApi>
where
EthApi: EthApiSpec + EthTransactions + TraceExt,
{
/// Injects a raw transaction into the node tx pool via RPC server
pub async fn inject_tx(&mut self, raw_tx: Bytes) -> EthResult<B256> {
let eth_api = self.inner.eth_api();
Expand Down
2 changes: 2 additions & 0 deletions crates/ethereum/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ reth-evm-ethereum.workspace = true
reth-consensus.workspace = true
reth-auto-seal-consensus.workspace = true
reth-beacon-consensus.workspace = true
reth-rpc.workspace = true
reth-node-api.workspace = true

# misc
eyre.workspace = true
Expand Down
20 changes: 17 additions & 3 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Ethereum Node types config.
use crate::{EthEngineTypes, EthEvmConfig};
use std::sync::Arc;

use reth_auto_seal_consensus::AutoSealConsensus;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
use reth_beacon_consensus::EthBeaconConsensus;
Expand All @@ -9,6 +10,7 @@ use reth_ethereum_engine_primitives::{
};
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_network::NetworkHandle;
use reth_node_api::{FullNodeComponents, NodeAddOns};
use reth_node_builder::{
components::{
ComponentsBuilder, ConsensusBuilder, ExecutorBuilder, NetworkBuilder,
Expand All @@ -19,12 +21,14 @@ use reth_node_builder::{
};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_provider::CanonStateSubscriptions;
use reth_rpc::EthApi;
use reth_tracing::tracing::{debug, info};
use reth_transaction_pool::{
blobstore::DiskFileBlobStore, EthTransactionPool, TransactionPool,
TransactionValidationTaskExecutor,
};
use std::sync::Arc;

use crate::{EthEngineTypes, EthEvmConfig};

/// Type configuration for a regular Ethereum node.
#[derive(Debug, Default, Clone, Copy)]
Expand Down Expand Up @@ -64,6 +68,14 @@ impl NodeTypes for EthereumNode {
type Engine = EthEngineTypes;
}

/// Add-ons w.r.t. l1 ethereum.
#[derive(Debug, Clone)]
pub struct EthereumAddOns;

impl<N: FullNodeComponents> NodeAddOns<N> for EthereumAddOns {
type EthApi = EthApi<N::Provider, N::Pool, NetworkHandle, N::Evm>;
}

impl<N> Node<N> for EthereumNode
where
N: FullNodeTypes<Engine = EthEngineTypes>,
Expand All @@ -77,7 +89,9 @@ where
EthereumConsensusBuilder,
>;

fn components_builder(self) -> Self::ComponentsBuilder {
type AddOns = EthereumAddOns;

fn components_builder(&self) -> Self::ComponentsBuilder {
Self::components()
}
}
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 @@ -3,7 +3,7 @@ use std::sync::Arc;
use alloy_genesis::Genesis;
use alloy_primitives::{b256, hex};
use futures::StreamExt;
use reth::rpc::api::eth::helpers::EthTransactions;
use reth::core::rpc::eth::helpers::EthTransactions;
use reth_chainspec::ChainSpec;
use reth_e2e_test_utils::setup;
use reth_provider::CanonStateSubscriptions;
Expand Down
4 changes: 2 additions & 2 deletions crates/ethereum/node/tests/e2e/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use alloy_primitives::{Address, B256};
use reth::rpc::types::engine::PayloadAttributes;
use reth_e2e_test_utils::NodeHelperType;
use reth_node_ethereum::EthereumNode;
use reth_node_ethereum::{node::EthereumAddOns, EthereumNode};
use reth_payload_builder::EthPayloadBuilderAttributes;

/// Ethereum Node Helper type
pub(crate) type EthNode = NodeHelperType<EthereumNode>;
pub(crate) type EthNode = NodeHelperType<EthereumNode, EthereumAddOns>;

/// Helper function to create a new eth payload attributes
pub(crate) fn eth_payload_attributes(timestamp: u64) -> EthPayloadBuilderAttributes {
Expand Down
6 changes: 3 additions & 3 deletions crates/ethereum/node/tests/it/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Node builder setup tests.
use reth_db::test_utils::create_test_rw_db;
use reth_node_api::FullNodeComponents;
use reth_node_builder::{NodeBuilder, NodeConfig};
use reth_node_ethereum::node::EthereumNode;
use reth_node_builder::{FullNodeComponents, NodeBuilder, NodeConfig};
use reth_node_ethereum::node::{EthereumAddOns, EthereumNode};

#[test]
fn test_basic_setup() {
Expand All @@ -15,6 +14,7 @@ fn test_basic_setup() {
.with_database(db)
.with_types::<EthereumNode>()
.with_components(EthereumNode::components())
.with_add_ons::<EthereumAddOns>()
.on_component_initialized(move |ctx| {
let _provider = ctx.provider();
println!("{msg}");
Expand Down
3 changes: 2 additions & 1 deletion crates/ethereum/node/tests/it/exex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use reth_db::test_utils::create_test_rw_db;
use reth_exex::ExExContext;
use reth_node_api::FullNodeComponents;
use reth_node_builder::{NodeBuilder, NodeConfig};
use reth_node_ethereum::EthereumNode;
use reth_node_ethereum::{node::EthereumAddOns, EthereumNode};
use std::{
future::Future,
pin::Pin,
Expand Down Expand Up @@ -33,6 +33,7 @@ fn basic_exex() {
.with_database(db)
.with_types::<EthereumNode>()
.with_components(EthereumNode::components())
.with_add_ons::<EthereumAddOns>()
.install_exex("dummy", move |ctx| future::ok(DummyExEx { _ctx: ctx }))
.check_launch();
}
5 changes: 3 additions & 2 deletions crates/exex/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use reth_node_builder::{
};
use reth_node_core::node_config::NodeConfig;
use reth_node_ethereum::{
node::{EthereumNetworkBuilder, EthereumPayloadBuilder},
node::{EthereumAddOns, EthereumNetworkBuilder, EthereumPayloadBuilder},
EthEngineTypes, EthEvmConfig,
};
use reth_payload_builder::noop::NoopPayloadBuilderService;
Expand Down Expand Up @@ -125,8 +125,9 @@ where
TestExecutorBuilder,
TestConsensusBuilder,
>;
type AddOns = EthereumAddOns;

fn components_builder(self) -> Self::ComponentsBuilder {
fn components_builder(&self) -> Self::ComponentsBuilder {
ComponentsBuilder::default()
.node_types::<N>()
.pool(TestPoolBuilder::default())
Expand Down
Loading

0 comments on commit 2aa94e9

Please sign in to comment.