Files
lighthouse/tests/node_test_rig/src/lib.rs
2019-11-19 13:29:29 +11:00

66 lines
2.3 KiB
Rust

use beacon_node::{beacon_chain::BeaconChainTypes, Client, ClientGenesis, ProductionBeaconNode};
use environment::RuntimeContext;
use futures::Future;
use remote_beacon_node::RemoteBeaconNode;
use std::path::PathBuf;
use tempdir::TempDir;
use types::EthSpec;
pub use beacon_node::{ClientConfig, ProductionClient};
pub use environment;
/// Provides a beacon node that is running in the current process. Useful for testing purposes.
pub struct LocalBeaconNode<T> {
pub client: T,
pub datadir: TempDir,
}
impl<E: EthSpec> LocalBeaconNode<ProductionClient<E>> {
/// Starts a new, production beacon node.
pub fn production(context: RuntimeContext<E>, mut client_config: ClientConfig) -> Self {
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
let datadir = TempDir::new("lighthouse_node_test_rig")
.expect("should create temp directory for client datadir");
client_config.data_dir = datadir.path().into();
client_config.network.network_dir = PathBuf::from(datadir.path()).join("network");
let client = ProductionBeaconNode::new(context, client_config)
.wait()
.expect("should build production client")
.into_inner();
LocalBeaconNode { client, datadir }
}
}
impl<T: BeaconChainTypes> LocalBeaconNode<Client<T>> {
/// Returns a `RemoteBeaconNode` that can connect to `self`. Useful for testing the node as if
/// it were external this process.
pub fn remote_node(&self) -> Result<RemoteBeaconNode<T::EthSpec>, String> {
Ok(RemoteBeaconNode::new(
self.client
.http_listen_addr()
.ok_or_else(|| "A remote beacon node must have a http server".to_string())?,
)?)
}
}
pub fn testing_client_config() -> ClientConfig {
let mut client_config = ClientConfig::default();
// Setting ports to `0` means that the OS will choose some available port.
client_config.network.libp2p_port = 0;
client_config.network.discovery_port = 0;
client_config.rpc.port = 0;
client_config.rest_api.port = 0;
client_config.websocket_server.port = 0;
client_config.genesis = ClientGenesis::Interop {
validator_count: 8,
genesis_time: 13_371_337,
};
client_config
}