use std::sync::Arc;
use alloy_network::Network;
use node::NodeTestContext;
use reth::{
args::{DiscoveryArgs, NetworkArgs, RpcServerArgs},
builder::{NodeBuilder, NodeConfig, NodeHandle},
network::PeersHandleProvider,
rpc::{
api::eth::{helpers::AddDevSigners, FullEthApiServer},
types::AnyTransactionReceipt,
},
tasks::TaskManager,
};
use reth_chainspec::ChainSpec;
use reth_db::{test_utils::TempDatabase, DatabaseEnv};
use reth_node_builder::{
components::NodeComponentsBuilder, rpc::EthApiBuilderProvider, FullNodeTypesAdapter, Node,
NodeAdapter, NodeAddOns, NodeComponents, NodeTypesWithDBAdapter, NodeTypesWithEngine,
RethFullAdapter,
};
use reth_provider::providers::BlockchainProvider;
use tracing::{span, Level};
use wallet::Wallet;
pub mod node;
pub mod transaction;
pub mod wallet;
mod payload;
mod network;
mod engine_api;
mod rpc;
mod traits;
pub async fn setup<N>(
num_nodes: usize,
chain_spec: Arc<ChainSpec>,
is_dev: bool,
) -> eyre::Result<(Vec<NodeHelperType<N, N::AddOns>>, TaskManager, Wallet)>
where
N: Default + Node<TmpNodeAdapter<N>> + NodeTypesWithEngine<ChainSpec = ChainSpec>,
N::ComponentsBuilder: NodeComponentsBuilder<
TmpNodeAdapter<N>,
Components: NodeComponents<TmpNodeAdapter<N>, Network: PeersHandleProvider>,
>,
N::AddOns: NodeAddOns<
Adapter<N>,
EthApi: FullEthApiServer<
NetworkTypes: Network<
TransactionResponse = reth_rpc_types::WithOtherFields<reth_rpc_types::Transaction>,
ReceiptResponse = AnyTransactionReceipt,
>,
> + AddDevSigners
+ EthApiBuilderProvider<Adapter<N>>,
>,
{
let tasks = TaskManager::current();
let exec = tasks.executor();
let network_config = NetworkArgs {
discovery: DiscoveryArgs { disable_discovery: true, ..DiscoveryArgs::default() },
..NetworkArgs::default()
};
let mut nodes: Vec<NodeTestContext<_, _>> = Vec::with_capacity(num_nodes);
for idx in 0..num_nodes {
let node_config = NodeConfig::test()
.with_chain(chain_spec.clone())
.with_network(network_config.clone())
.with_unused_ports()
.with_rpc(RpcServerArgs::default().with_unused_ports().with_http())
.set_dev(is_dev);
let span = span!(Level::INFO, "node", idx);
let _enter = span.enter();
let NodeHandle { node, node_exit_future: _ } = NodeBuilder::new(node_config.clone())
.testing_node(exec.clone())
.node(Default::default())
.launch()
.await?;
let mut node = NodeTestContext::new(node).await?;
if let Some(previous_node) = nodes.last_mut() {
previous_node.connect(&mut node).await;
}
if idx + 1 == num_nodes && num_nodes > 2 {
if let Some(first_node) = nodes.first_mut() {
node.connect(first_node).await;
}
}
nodes.push(node);
}
Ok((nodes, tasks, Wallet::default().with_chain_id(chain_spec.chain().into())))
}
type TmpDB = Arc<TempDatabase<DatabaseEnv>>;
type TmpNodeAdapter<N> = FullNodeTypesAdapter<
NodeTypesWithDBAdapter<N, TmpDB>,
BlockchainProvider<NodeTypesWithDBAdapter<N, TmpDB>>,
>;
type Adapter<N> = NodeAdapter<
RethFullAdapter<TmpDB, N>,
<<N as Node<TmpNodeAdapter<N>>>::ComponentsBuilder as NodeComponentsBuilder<
RethFullAdapter<TmpDB, N>,
>>::Components,
>;
pub type NodeHelperType<N, AO> = NodeTestContext<Adapter<N>, AO>;