mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 00:42:42 +00:00
Merge branch 'deposit-flow' into kill-grpc
This commit is contained in:
@@ -36,6 +36,7 @@ members = [
|
|||||||
"beacon_node/eth1",
|
"beacon_node/eth1",
|
||||||
"beacon_node/beacon_chain",
|
"beacon_node/beacon_chain",
|
||||||
"beacon_node/websocket_server",
|
"beacon_node/websocket_server",
|
||||||
|
"tests/beacon_chain_sim",
|
||||||
"tests/ef_tests",
|
"tests/ef_tests",
|
||||||
"tests/eth1_test_rig",
|
"tests/eth1_test_rig",
|
||||||
"tests/node_test_rig",
|
"tests/node_test_rig",
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ clap = "2.33.0"
|
|||||||
slog = "2.5.2"
|
slog = "2.5.2"
|
||||||
slog-term = "2.4.2"
|
slog-term = "2.4.2"
|
||||||
slog-async = "2.3.0"
|
slog-async = "2.3.0"
|
||||||
validator_client = { path = "../validator_client" }
|
|
||||||
types = { path = "../eth2/types" }
|
types = { path = "../eth2/types" }
|
||||||
dirs = "2.0.2"
|
dirs = "2.0.2"
|
||||||
environment = { path = "../lighthouse/environment" }
|
environment = { path = "../lighthouse/environment" }
|
||||||
@@ -22,3 +21,4 @@ libc = "0.2.65"
|
|||||||
eth2_ssz = { path = "../eth2/utils/ssz" }
|
eth2_ssz = { path = "../eth2/utils/ssz" }
|
||||||
eth2_ssz_derive = { path = "../eth2/utils/ssz_derive" }
|
eth2_ssz_derive = { path = "../eth2/utils/ssz_derive" }
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
validator_client = { path = "../validator_client" }
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
mod cli;
|
mod cli;
|
||||||
pub mod validator;
|
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use environment::RuntimeContext;
|
use environment::RuntimeContext;
|
||||||
@@ -7,7 +6,7 @@ use slog::{crit, info};
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use types::{ChainSpec, EthSpec};
|
use types::{ChainSpec, EthSpec};
|
||||||
use validator::{ValidatorDirectory, ValidatorDirectoryBuilder};
|
use validator_client::validator_directory::{ValidatorDirectory, ValidatorDirectoryBuilder};
|
||||||
|
|
||||||
pub use cli::cli_app;
|
pub use cli::cli_app;
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ pub struct ClientBuilder<T: BeaconChainTypes> {
|
|||||||
libp2p_network: Option<Arc<NetworkService<T>>>,
|
libp2p_network: Option<Arc<NetworkService<T>>>,
|
||||||
libp2p_network_send: Option<UnboundedSender<NetworkMessage>>,
|
libp2p_network_send: Option<UnboundedSender<NetworkMessage>>,
|
||||||
http_listen_addr: Option<SocketAddr>,
|
http_listen_addr: Option<SocketAddr>,
|
||||||
|
grpc_listen_addr: Option<(String, u16)>,
|
||||||
websocket_listen_addr: Option<SocketAddr>,
|
websocket_listen_addr: Option<SocketAddr>,
|
||||||
eth_spec_instance: T::EthSpec,
|
eth_spec_instance: T::EthSpec,
|
||||||
}
|
}
|
||||||
@@ -93,6 +94,7 @@ where
|
|||||||
libp2p_network: None,
|
libp2p_network: None,
|
||||||
libp2p_network_send: None,
|
libp2p_network_send: None,
|
||||||
http_listen_addr: None,
|
http_listen_addr: None,
|
||||||
|
grpc_listen_addr: None,
|
||||||
websocket_listen_addr: None,
|
websocket_listen_addr: None,
|
||||||
eth_spec_instance,
|
eth_spec_instance,
|
||||||
}
|
}
|
||||||
@@ -425,6 +427,7 @@ where
|
|||||||
beacon_chain: self.beacon_chain,
|
beacon_chain: self.beacon_chain,
|
||||||
libp2p_network: self.libp2p_network,
|
libp2p_network: self.libp2p_network,
|
||||||
http_listen_addr: self.http_listen_addr,
|
http_listen_addr: self.http_listen_addr,
|
||||||
|
grpc_listen_addr: self.grpc_listen_addr,
|
||||||
websocket_listen_addr: self.websocket_listen_addr,
|
websocket_listen_addr: self.websocket_listen_addr,
|
||||||
_exit_signals: self.exit_signals,
|
_exit_signals: self.exit_signals,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ pub mod builder;
|
|||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
use beacon_chain::BeaconChain;
|
use beacon_chain::BeaconChain;
|
||||||
|
use eth2_libp2p::{Enr, Multiaddr};
|
||||||
use exit_future::Signal;
|
use exit_future::Signal;
|
||||||
use network::Service as NetworkService;
|
use network::Service as NetworkService;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@@ -24,6 +25,7 @@ pub struct Client<T: BeaconChainTypes> {
|
|||||||
libp2p_network: Option<Arc<NetworkService<T>>>,
|
libp2p_network: Option<Arc<NetworkService<T>>>,
|
||||||
http_listen_addr: Option<SocketAddr>,
|
http_listen_addr: Option<SocketAddr>,
|
||||||
websocket_listen_addr: Option<SocketAddr>,
|
websocket_listen_addr: Option<SocketAddr>,
|
||||||
|
grpc_listen_addr: Option<(String, u16)>,
|
||||||
/// Exit signals will "fire" when dropped, causing each service to exit gracefully.
|
/// Exit signals will "fire" when dropped, causing each service to exit gracefully.
|
||||||
_exit_signals: Vec<Signal>,
|
_exit_signals: Vec<Signal>,
|
||||||
}
|
}
|
||||||
@@ -39,6 +41,11 @@ impl<T: BeaconChainTypes> Client<T> {
|
|||||||
self.http_listen_addr
|
self.http_listen_addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the address of the client's gRPC API server, if it was started.
|
||||||
|
pub fn grpc_listen_addr(&self) -> Option<(String, u16)> {
|
||||||
|
self.grpc_listen_addr.clone()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the address of the client's WebSocket API server, if it was started.
|
/// Returns the address of the client's WebSocket API server, if it was started.
|
||||||
pub fn websocket_listen_addr(&self) -> Option<SocketAddr> {
|
pub fn websocket_listen_addr(&self) -> Option<SocketAddr> {
|
||||||
self.websocket_listen_addr
|
self.websocket_listen_addr
|
||||||
@@ -48,6 +55,16 @@ impl<T: BeaconChainTypes> Client<T> {
|
|||||||
pub fn libp2p_listen_port(&self) -> Option<u16> {
|
pub fn libp2p_listen_port(&self) -> Option<u16> {
|
||||||
self.libp2p_network.as_ref().map(|n| n.listen_port())
|
self.libp2p_network.as_ref().map(|n| n.listen_port())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the list of libp2p addresses the client is listening to.
|
||||||
|
pub fn libp2p_listen_addresses(&self) -> Option<Vec<Multiaddr>> {
|
||||||
|
self.libp2p_network.as_ref().map(|n| n.listen_multiaddrs())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the local libp2p ENR of this node, for network discovery.
|
||||||
|
pub fn enr(&self) -> Option<Enr> {
|
||||||
|
self.libp2p_network.as_ref().map(|n| n.local_enr())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: BeaconChainTypes> Drop for Client<T> {
|
impl<T: BeaconChainTypes> Drop for Client<T> {
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ impl<E: EthSpec> Environment<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a `Context` where the `service_name` is added to the logger output.
|
/// Returns a `Context` where the `service_name` is added to the logger output.
|
||||||
pub fn service_context(&mut self, service_name: &'static str) -> RuntimeContext<E> {
|
pub fn service_context(&mut self, service_name: String) -> RuntimeContext<E> {
|
||||||
RuntimeContext {
|
RuntimeContext {
|
||||||
executor: self.runtime.executor(),
|
executor: self.runtime.executor(),
|
||||||
log: self.log.new(o!("service" => service_name)),
|
log: self.log.new(o!("service" => service_name)),
|
||||||
|
|||||||
12
tests/beacon_chain_sim/Cargo.toml
Normal file
12
tests/beacon_chain_sim/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "beacon_chain_sim"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
node_test_rig = { path = "../node_test_rig" }
|
||||||
|
types = { path = "../../eth2/types" }
|
||||||
|
validator_client = { path = "../../validator_client" }
|
||||||
120
tests/beacon_chain_sim/src/main.rs
Normal file
120
tests/beacon_chain_sim/src/main.rs
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
use node_test_rig::{
|
||||||
|
environment::{EnvironmentBuilder, RuntimeContext},
|
||||||
|
testing_client_config, ClientConfig, ClientGenesis, LocalBeaconNode, LocalValidatorClient,
|
||||||
|
ProductionClient, ValidatorConfig,
|
||||||
|
};
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
use types::EthSpec;
|
||||||
|
|
||||||
|
pub type BeaconNode<E> = LocalBeaconNode<ProductionClient<E>>;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let nodes = 4;
|
||||||
|
let validators_per_node = 64 / nodes;
|
||||||
|
|
||||||
|
match simulation(nodes, validators_per_node) {
|
||||||
|
Ok(()) => println!("Simulation exited successfully"),
|
||||||
|
Err(e) => println!("Simulation exited with error: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simulation(num_nodes: usize, validators_per_node: usize) -> Result<(), String> {
|
||||||
|
if num_nodes < 1 {
|
||||||
|
return Err("Must have at least one node".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut env = EnvironmentBuilder::minimal()
|
||||||
|
.async_logger("debug")?
|
||||||
|
.multi_threaded_tokio_runtime()?
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
let mut base_config = testing_client_config();
|
||||||
|
|
||||||
|
let now = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("should get system time")
|
||||||
|
.as_secs();
|
||||||
|
base_config.genesis = ClientGenesis::Interop {
|
||||||
|
genesis_time: now,
|
||||||
|
validator_count: num_nodes * validators_per_node,
|
||||||
|
};
|
||||||
|
|
||||||
|
let boot_node =
|
||||||
|
BeaconNode::production(env.service_context("boot_node".into()), base_config.clone());
|
||||||
|
|
||||||
|
let mut nodes = (1..num_nodes)
|
||||||
|
.map(|i| {
|
||||||
|
let context = env.service_context(format!("node_{}", i));
|
||||||
|
new_with_bootnode_via_enr(context, &boot_node, base_config.clone())
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let _validators = nodes
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, node)| {
|
||||||
|
let mut context = env.service_context(format!("validator_{}", i));
|
||||||
|
|
||||||
|
// Pull the spec from the beacon node's beacon chain, in case there were some changes
|
||||||
|
// to the spec after the node booted.
|
||||||
|
context.eth2_config.spec = node
|
||||||
|
.client
|
||||||
|
.beacon_chain()
|
||||||
|
.expect("should have beacon chain")
|
||||||
|
.spec
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
let indices =
|
||||||
|
(i * validators_per_node..(i + 1) * validators_per_node).collect::<Vec<_>>();
|
||||||
|
new_validator_client(
|
||||||
|
env.service_context(format!("validator_{}", i)),
|
||||||
|
node,
|
||||||
|
ValidatorConfig::default(),
|
||||||
|
&indices,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
nodes.insert(0, boot_node);
|
||||||
|
|
||||||
|
env.block_until_ctrl_c()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this function does not result in nodes connecting to each other. This is a bug due to
|
||||||
|
// using a 0 port for discovery. Age is fixing it.
|
||||||
|
fn new_with_bootnode_via_enr<E: EthSpec>(
|
||||||
|
context: RuntimeContext<E>,
|
||||||
|
boot_node: &BeaconNode<E>,
|
||||||
|
base_config: ClientConfig,
|
||||||
|
) -> BeaconNode<E> {
|
||||||
|
let mut config = base_config;
|
||||||
|
config.network.boot_nodes.push(
|
||||||
|
boot_node
|
||||||
|
.client
|
||||||
|
.enr()
|
||||||
|
.expect("bootnode must have a network"),
|
||||||
|
);
|
||||||
|
|
||||||
|
BeaconNode::production(context, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_validator_client<E: EthSpec>(
|
||||||
|
context: RuntimeContext<E>,
|
||||||
|
beacon_node: &BeaconNode<E>,
|
||||||
|
base_config: ValidatorConfig,
|
||||||
|
keypair_indices: &[usize],
|
||||||
|
) -> LocalValidatorClient<E> {
|
||||||
|
let mut config = base_config;
|
||||||
|
|
||||||
|
let (grpc_endpoint, grpc_port) = beacon_node
|
||||||
|
.client
|
||||||
|
.grpc_listen_addr()
|
||||||
|
.expect("Must have gRPC started");
|
||||||
|
|
||||||
|
config.server = grpc_endpoint;
|
||||||
|
config.server_grpc_port = grpc_port;
|
||||||
|
|
||||||
|
LocalValidatorClient::production_with_insecure_keypairs(context, config, keypair_indices)
|
||||||
|
}
|
||||||
@@ -16,3 +16,4 @@ serde = "1.0"
|
|||||||
futures = "0.1.25"
|
futures = "0.1.25"
|
||||||
genesis = { path = "../../beacon_node/genesis" }
|
genesis = { path = "../../beacon_node/genesis" }
|
||||||
remote_beacon_node = { path = "../../eth2/utils/remote_beacon_node" }
|
remote_beacon_node = { path = "../../eth2/utils/remote_beacon_node" }
|
||||||
|
validator_client = { path = "../../validator_client" }
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
use beacon_node::{
|
use beacon_node::{beacon_chain::BeaconChainTypes, Client, ProductionBeaconNode};
|
||||||
beacon_chain::BeaconChainTypes, Client, ClientConfig, ClientGenesis, ProductionBeaconNode,
|
|
||||||
ProductionClient,
|
|
||||||
};
|
|
||||||
use environment::RuntimeContext;
|
use environment::RuntimeContext;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use remote_beacon_node::RemoteBeaconNode;
|
use remote_beacon_node::RemoteBeaconNode;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
use types::EthSpec;
|
use types::EthSpec;
|
||||||
|
use validator_client::{validator_directory::ValidatorDirectoryBuilder, ProductionValidatorClient};
|
||||||
|
|
||||||
|
pub use beacon_node::{ClientConfig, ClientGenesis, ProductionClient};
|
||||||
pub use environment;
|
pub use environment;
|
||||||
|
pub use validator_client::Config as ValidatorConfig;
|
||||||
|
|
||||||
/// Provides a beacon node that is running in the current process. Useful for testing purposes.
|
/// Provides a beacon node that is running in the current process (i.e., local). Useful for testing
|
||||||
|
/// purposes.
|
||||||
pub struct LocalBeaconNode<T> {
|
pub struct LocalBeaconNode<T> {
|
||||||
pub client: T,
|
pub client: T,
|
||||||
pub datadir: TempDir,
|
pub datadir: TempDir,
|
||||||
@@ -18,8 +21,13 @@ pub struct LocalBeaconNode<T> {
|
|||||||
|
|
||||||
impl<E: EthSpec> LocalBeaconNode<ProductionClient<E>> {
|
impl<E: EthSpec> LocalBeaconNode<ProductionClient<E>> {
|
||||||
/// Starts a new, production beacon node.
|
/// Starts a new, production beacon node.
|
||||||
pub fn production(context: RuntimeContext<E>) -> Self {
|
pub fn production(context: RuntimeContext<E>, mut client_config: ClientConfig) -> Self {
|
||||||
let (client_config, datadir) = testing_client_config();
|
// 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)
|
let client = ProductionBeaconNode::new(context, client_config)
|
||||||
.wait()
|
.wait()
|
||||||
@@ -42,28 +50,84 @@ impl<T: BeaconChainTypes> LocalBeaconNode<Client<T>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn testing_client_config() -> (ClientConfig, TempDir) {
|
pub fn testing_client_config() -> ClientConfig {
|
||||||
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
|
||||||
let tempdir = TempDir::new("lighthouse_node_test_rig")
|
|
||||||
.expect("should create temp directory for client datadir");
|
|
||||||
|
|
||||||
let mut client_config = ClientConfig::default();
|
let mut client_config = ClientConfig::default();
|
||||||
|
|
||||||
client_config.data_dir = tempdir.path().into();
|
|
||||||
|
|
||||||
// Setting ports to `0` means that the OS will choose some available port.
|
// Setting ports to `0` means that the OS will choose some available port.
|
||||||
client_config.network.libp2p_port = 0;
|
client_config.network.libp2p_port = 0;
|
||||||
client_config.network.discovery_port = 0;
|
client_config.network.discovery_port = 0;
|
||||||
client_config.rpc.port = 0;
|
|
||||||
client_config.rest_api.port = 0;
|
client_config.rest_api.port = 0;
|
||||||
client_config.websocket_server.port = 0;
|
client_config.websocket_server.port = 0;
|
||||||
|
|
||||||
|
client_config.dummy_eth1_backend = true;
|
||||||
|
|
||||||
|
let now = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("should get system time")
|
||||||
|
.as_secs();
|
||||||
|
|
||||||
client_config.genesis = ClientGenesis::Interop {
|
client_config.genesis = ClientGenesis::Interop {
|
||||||
validator_count: 8,
|
validator_count: 8,
|
||||||
genesis_time: 13_371_337,
|
genesis_time: now,
|
||||||
};
|
};
|
||||||
|
|
||||||
client_config.dummy_eth1_backend = true;
|
client_config.dummy_eth1_backend = true;
|
||||||
|
|
||||||
(client_config, tempdir)
|
client_config
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LocalValidatorClient<T: EthSpec> {
|
||||||
|
pub client: ProductionValidatorClient<T>,
|
||||||
|
pub datadir: TempDir,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: EthSpec> LocalValidatorClient<E> {
|
||||||
|
pub fn production_with_insecure_keypairs(
|
||||||
|
context: RuntimeContext<E>,
|
||||||
|
config: ValidatorConfig,
|
||||||
|
keypair_indices: &[usize],
|
||||||
|
) -> Self {
|
||||||
|
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
||||||
|
let datadir = TempDir::new("lighthouse-beacon-node")
|
||||||
|
.expect("should create temp directory for client datadir");
|
||||||
|
|
||||||
|
keypair_indices.iter().for_each(|i| {
|
||||||
|
ValidatorDirectoryBuilder::default()
|
||||||
|
.spec(context.eth2_config.spec.clone())
|
||||||
|
.full_deposit_amount()
|
||||||
|
.expect("should set full deposit amount")
|
||||||
|
.insecure_keypairs(*i)
|
||||||
|
.create_directory(PathBuf::from(datadir.path()))
|
||||||
|
.expect("should create directory")
|
||||||
|
.write_keypair_files()
|
||||||
|
.expect("should write keypair files")
|
||||||
|
.write_eth1_data_file()
|
||||||
|
.expect("should write eth1 data file")
|
||||||
|
.build()
|
||||||
|
.expect("should build dir");
|
||||||
|
});
|
||||||
|
|
||||||
|
Self::new(context, config, datadir)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn production(context: RuntimeContext<E>, config: ValidatorConfig) -> Self {
|
||||||
|
// Creates a temporary directory that will be deleted once this `TempDir` is dropped.
|
||||||
|
let datadir = TempDir::new("lighthouse-validator")
|
||||||
|
.expect("should create temp directory for client datadir");
|
||||||
|
|
||||||
|
Self::new(context, config, datadir)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new(context: RuntimeContext<E>, mut config: ValidatorConfig, datadir: TempDir) -> Self {
|
||||||
|
config.data_dir = datadir.path().into();
|
||||||
|
|
||||||
|
let client =
|
||||||
|
ProductionValidatorClient::new(context, config).expect("should start validator client");
|
||||||
|
|
||||||
|
client
|
||||||
|
.start_service()
|
||||||
|
.expect("should start validator client");
|
||||||
|
|
||||||
|
Self { client, datadir }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ edition = "2018"
|
|||||||
name = "validator_client"
|
name = "validator_client"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempdir = "0.3"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bls = { path = "../eth2/utils/bls" }
|
|
||||||
eth2_ssz = "0.1.2"
|
eth2_ssz = "0.1.2"
|
||||||
eth2_config = { path = "../eth2/utils/eth2_config" }
|
eth2_config = { path = "../eth2/utils/eth2_config" }
|
||||||
tree_hash = "0.1.0"
|
tree_hash = "0.1.0"
|
||||||
@@ -35,3 +37,8 @@ environment = { path = "../lighthouse/environment" }
|
|||||||
parking_lot = "0.7"
|
parking_lot = "0.7"
|
||||||
exit-future = "0.1.4"
|
exit-future = "0.1.4"
|
||||||
libc = "0.2.65"
|
libc = "0.2.65"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
eth2_ssz_derive = { path = "../eth2/utils/ssz_derive" }
|
||||||
|
hex = "0.4"
|
||||||
|
deposit_contract = { path = "../eth2/utils/deposit_contract" }
|
||||||
|
bls = { path = "../eth2/utils/bls" }
|
||||||
|
|||||||
@@ -1,5 +1,21 @@
|
|||||||
use crate::config::{DEFAULT_SERVER, DEFAULT_SERVER_GRPC_PORT, DEFAULT_SERVER_HTTP_PORT};
|
use crate::config::Config;
|
||||||
use clap::{App, Arg, SubCommand};
|
use clap::{App, Arg, SubCommand};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
/// The default configuration. Is in lazy_static because clap requires references, therefore we
|
||||||
|
/// can't initialize the defaults in the `cli_app` function
|
||||||
|
static ref DEFAULTS: Config = {
|
||||||
|
Config::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
static ref DEFAULT_SERVER_GRPC_PORT: String = {
|
||||||
|
format!("{}", DEFAULTS.server_grpc_port)
|
||||||
|
};
|
||||||
|
static ref DEFAULT_SERVER_HTTP_PORT: String = {
|
||||||
|
format!("{}", DEFAULTS.server_http_port)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
||||||
App::new("Validator Client")
|
App::new("Validator Client")
|
||||||
@@ -35,7 +51,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
|||||||
.long("server")
|
.long("server")
|
||||||
.value_name("NETWORK_ADDRESS")
|
.value_name("NETWORK_ADDRESS")
|
||||||
.help("Address to connect to BeaconNode.")
|
.help("Address to connect to BeaconNode.")
|
||||||
.default_value(DEFAULT_SERVER)
|
.default_value(&DEFAULTS.server)
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@@ -44,7 +60,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
|||||||
.short("g")
|
.short("g")
|
||||||
.value_name("PORT")
|
.value_name("PORT")
|
||||||
.help("Port to use for gRPC API connection to the server.")
|
.help("Port to use for gRPC API connection to the server.")
|
||||||
.default_value(DEFAULT_SERVER_GRPC_PORT)
|
.default_value(&DEFAULT_SERVER_GRPC_PORT)
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@@ -53,7 +69,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
|
|||||||
.short("h")
|
.short("h")
|
||||||
.value_name("PORT")
|
.value_name("PORT")
|
||||||
.help("Port to use for HTTP API connection to the server.")
|
.help("Port to use for HTTP API connection to the server.")
|
||||||
.default_value(DEFAULT_SERVER_HTTP_PORT)
|
.default_value(&DEFAULT_SERVER_HTTP_PORT)
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
use crate::validator_directory::ValidatorDirectory;
|
||||||
use bincode;
|
use bincode;
|
||||||
use bls::Keypair;
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use slog::{error, warn};
|
use slog::{error, warn};
|
||||||
@@ -9,13 +9,9 @@ use std::ops::Range;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use types::{
|
use types::{
|
||||||
test_utils::{generate_deterministic_keypair, load_keypairs_from_yaml},
|
test_utils::{generate_deterministic_keypair, load_keypairs_from_yaml},
|
||||||
EthSpec, MainnetEthSpec,
|
EthSpec, Keypair, MainnetEthSpec,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const DEFAULT_SERVER: &str = "localhost";
|
|
||||||
pub const DEFAULT_SERVER_GRPC_PORT: &str = "5051";
|
|
||||||
pub const DEFAULT_SERVER_HTTP_PORT: &str = "5052";
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum KeySource {
|
pub enum KeySource {
|
||||||
/// Load the keypairs from disk.
|
/// Load the keypairs from disk.
|
||||||
@@ -58,16 +54,12 @@ impl Default for Config {
|
|||||||
/// Build a new configuration from defaults.
|
/// Build a new configuration from defaults.
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
data_dir: PathBuf::from(".lighthouse-validator"),
|
data_dir: PathBuf::from(".lighthouse/validators"),
|
||||||
key_source: <_>::default(),
|
key_source: <_>::default(),
|
||||||
log_file: PathBuf::from(""),
|
log_file: PathBuf::from(""),
|
||||||
server: DEFAULT_SERVER.into(),
|
server: "localhost".into(),
|
||||||
server_grpc_port: DEFAULT_SERVER_GRPC_PORT
|
server_grpc_port: 5051,
|
||||||
.parse::<u16>()
|
server_http_port: 5052,
|
||||||
.expect("gRPC port constant should be valid"),
|
|
||||||
server_http_port: DEFAULT_SERVER_GRPC_PORT
|
|
||||||
.parse::<u16>()
|
|
||||||
.expect("HTTP port constant should be valid"),
|
|
||||||
slots_per_epoch: MainnetEthSpec::slots_per_epoch(),
|
slots_per_epoch: MainnetEthSpec::slots_per_epoch(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,75 +98,44 @@ impl Config {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a single keypair from the given `path`.
|
/// Loads the validator keys from disk.
|
||||||
///
|
///
|
||||||
/// `path` should be the path to a directory containing a private key. The file name of `path`
|
/// ## Errors
|
||||||
/// must align with the public key loaded from it, otherwise an error is returned.
|
|
||||||
///
|
///
|
||||||
/// An error will be returned if `path` is a file (not a directory).
|
/// Returns an error if the base directory does not exist, however it does not return for any
|
||||||
fn read_keypair_file(&self, path: PathBuf) -> Result<Keypair, String> {
|
/// invalid directories/files. Instead, it just filters out failures and logs errors. This
|
||||||
if !path.is_dir() {
|
/// behaviour is intended to avoid the scenario where a single invalid file can stop all
|
||||||
return Err("Is not a directory".into());
|
/// validators.
|
||||||
}
|
|
||||||
|
|
||||||
let key_filename: PathBuf = path.join(DEFAULT_PRIVATE_KEY_FILENAME);
|
|
||||||
|
|
||||||
if !key_filename.is_file() {
|
|
||||||
return Err(format!(
|
|
||||||
"Private key is not a file: {:?}",
|
|
||||||
key_filename.to_str()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut key_file = File::open(key_filename.clone())
|
|
||||||
.map_err(|e| format!("Unable to open private key file: {}", e))?;
|
|
||||||
|
|
||||||
let key: Keypair = bincode::deserialize_from(&mut key_file)
|
|
||||||
.map_err(|e| format!("Unable to deserialize private key: {:?}", e))?;
|
|
||||||
|
|
||||||
let ki = key.identifier();
|
|
||||||
if ki
|
|
||||||
!= path
|
|
||||||
.file_name()
|
|
||||||
.ok_or_else(|| "Invalid path".to_string())?
|
|
||||||
.to_string_lossy()
|
|
||||||
{
|
|
||||||
Err(format!(
|
|
||||||
"The validator key ({:?}) did not match the directory filename {:?}.",
|
|
||||||
ki,
|
|
||||||
path.to_str()
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Ok(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fetch_keys_from_disk(&self, log: &slog::Logger) -> Result<Vec<Keypair>, String> {
|
pub fn fetch_keys_from_disk(&self, log: &slog::Logger) -> Result<Vec<Keypair>, String> {
|
||||||
Ok(
|
let base_dir = self
|
||||||
fs::read_dir(&self.full_data_dir().expect("Data dir must exist"))
|
.full_data_dir()
|
||||||
.map_err(|e| format!("Failed to read datadir: {:?}", e))?
|
.ok_or_else(|| format!("Base directory does not exist: {:?}", self.full_data_dir()))?;
|
||||||
.filter_map(|validator_dir| {
|
|
||||||
let path = validator_dir.ok()?.path();
|
|
||||||
|
|
||||||
if path.is_dir() {
|
let keypairs = fs::read_dir(&base_dir)
|
||||||
match self.read_keypair_file(path.clone()) {
|
.map_err(|e| format!("Failed to read base directory: {:?}", e))?
|
||||||
Ok(keypair) => Some(keypair),
|
.filter_map(|validator_dir| {
|
||||||
Err(e) => {
|
let path = validator_dir.ok()?.path();
|
||||||
error!(
|
|
||||||
log,
|
if path.is_dir() {
|
||||||
"Failed to parse a validator keypair";
|
match ValidatorDirectory::load_for_signing(path.clone()) {
|
||||||
"error" => e,
|
Ok(validator_directory) => validator_directory.voting_keypair,
|
||||||
"path" => path.to_str(),
|
Err(e) => {
|
||||||
);
|
error!(
|
||||||
None
|
log,
|
||||||
}
|
"Failed to load a validator directory";
|
||||||
|
"error" => e,
|
||||||
|
"path" => path.to_str(),
|
||||||
|
);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
})
|
} else {
|
||||||
.collect(),
|
None
|
||||||
)
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(keypairs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_testing_keypairs(
|
pub fn fetch_testing_keypairs(
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ mod duties;
|
|||||||
mod error;
|
mod error;
|
||||||
mod service;
|
mod service;
|
||||||
mod signer;
|
mod signer;
|
||||||
|
pub mod validator_directory;
|
||||||
|
|
||||||
pub use cli::cli_app;
|
pub use cli::cli_app;
|
||||||
pub use config::Config;
|
pub use config::Config;
|
||||||
@@ -42,20 +43,36 @@ pub struct ProductionValidatorClient<T: EthSpec> {
|
|||||||
impl<T: EthSpec> ProductionValidatorClient<T> {
|
impl<T: EthSpec> ProductionValidatorClient<T> {
|
||||||
/// Instantiates the validator client, _without_ starting the timers to trigger block
|
/// Instantiates the validator client, _without_ starting the timers to trigger block
|
||||||
/// and attestation production.
|
/// and attestation production.
|
||||||
pub fn new_from_cli(context: RuntimeContext<T>, matches: &ArgMatches) -> Result<Self, String> {
|
pub fn new_from_cli(
|
||||||
|
mut context: RuntimeContext<T>,
|
||||||
|
matches: &ArgMatches,
|
||||||
|
) -> Result<Self, String> {
|
||||||
let mut log = context.log.clone();
|
let mut log = context.log.clone();
|
||||||
|
|
||||||
let (client_config, eth2_config) = get_configs(&matches, &mut log)
|
let (config, eth2_config) = get_configs(&matches, &mut log)
|
||||||
.map_err(|e| format!("Unable to initialize config: {}", e))?;
|
.map_err(|e| format!("Unable to initialize config: {}", e))?;
|
||||||
|
|
||||||
|
// TODO: the eth2 config in the env is being completely ignored.
|
||||||
|
//
|
||||||
|
// See https://github.com/sigp/lighthouse/issues/602
|
||||||
|
context.eth2_config = eth2_config;
|
||||||
|
|
||||||
|
Self::new(context, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Instantiates the validator client, _without_ starting the timers to trigger block
|
||||||
|
/// and attestation production.
|
||||||
|
pub fn new(context: RuntimeContext<T>, config: Config) -> Result<Self, String> {
|
||||||
|
let log = context.log.clone();
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
log,
|
log,
|
||||||
"Starting validator client";
|
"Starting validator client";
|
||||||
"datadir" => client_config.full_data_dir().expect("Unable to find datadir").to_str(),
|
"datadir" => config.full_data_dir().expect("Unable to find datadir").to_str(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let service: Service<ValidatorServiceClient, Keypair, T> =
|
let service: Service<ValidatorServiceClient, Keypair, T> =
|
||||||
Service::initialize_service(client_config, eth2_config, log.clone())
|
Service::initialize_service(config, context.eth2_config.clone(), log.clone())
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ use crate::config::Config as ValidatorConfig;
|
|||||||
use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap};
|
use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap};
|
||||||
use crate::error as error_chain;
|
use crate::error as error_chain;
|
||||||
use crate::signer::Signer;
|
use crate::signer::Signer;
|
||||||
use bls::Keypair;
|
|
||||||
use eth2_config::Eth2Config;
|
use eth2_config::Eth2Config;
|
||||||
use grpcio::{ChannelBuilder, EnvBuilder};
|
use grpcio::{ChannelBuilder, EnvBuilder};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
@@ -28,7 +27,7 @@ use slot_clock::{SlotClock, SystemTimeSlotClock};
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use types::{ChainSpec, Epoch, EthSpec, Fork, Slot};
|
use types::{ChainSpec, Epoch, EthSpec, Fork, Keypair, Slot};
|
||||||
|
|
||||||
/// The validator service. This is the main thread that executes and maintains validator
|
/// The validator service. This is the main thread that executes and maintains validator
|
||||||
/// duties.
|
/// duties.
|
||||||
@@ -162,11 +161,13 @@ impl<E: EthSpec> Service<ValidatorServiceClient, Keypair, E> {
|
|||||||
// Load generated keypairs
|
// Load generated keypairs
|
||||||
let keypairs = Arc::new(client_config.fetch_keys(&log)?);
|
let keypairs = Arc::new(client_config.fetch_keys(&log)?);
|
||||||
|
|
||||||
let slots_per_epoch = E::slots_per_epoch();
|
info!(
|
||||||
|
log,
|
||||||
|
"Keypairs loaded";
|
||||||
|
"local_validator_count" => keypairs.len()
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: keypairs are randomly generated; they should be loaded from a file or generated.
|
let slots_per_epoch = E::slots_per_epoch();
|
||||||
// https://github.com/sigp/lighthouse/issues/160
|
|
||||||
//let keypairs = Arc::new(generate_deterministic_keypairs(8));
|
|
||||||
|
|
||||||
// Builds a mapping of Epoch -> Map(PublicKey, EpochDuty)
|
// Builds a mapping of Epoch -> Map(PublicKey, EpochDuty)
|
||||||
// where EpochDuty contains slot numbers and attestation data that each validator needs to
|
// where EpochDuty contains slot numbers and attestation data that each validator needs to
|
||||||
|
|||||||
Reference in New Issue
Block a user