mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-15 02:42:38 +00:00
Add duties service to validator
This commit is contained in:
@@ -1,34 +1,80 @@
|
||||
use crate::block_producer::{BlockProducer, BlockProducerService};
|
||||
use crate::config::ClientConfig;
|
||||
use clap::{App, Arg};
|
||||
use grpcio::{ChannelBuilder, EnvBuilder};
|
||||
use protos::services_grpc::BeaconBlockServiceClient;
|
||||
use slog::{info, o, Drain};
|
||||
use slog::{error, info, o, Drain};
|
||||
use slot_clock::SystemTimeSlotClock;
|
||||
use spec::ChainSpec;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
mod block_producer;
|
||||
mod config;
|
||||
mod duties;
|
||||
|
||||
fn main() {
|
||||
// gRPC
|
||||
let env = Arc::new(EnvBuilder::new().build());
|
||||
let ch = ChannelBuilder::new(env).connect("localhost:50051");
|
||||
let client = Arc::new(BeaconBlockServiceClient::new(ch));
|
||||
|
||||
// Logging
|
||||
let decorator = slog_term::TermDecorator::new().build();
|
||||
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
|
||||
let drain = slog_async::Async::new(drain).build().fuse();
|
||||
let log = slog::Logger::root(drain, o!());
|
||||
|
||||
// CLI
|
||||
let matches = App::new("Lighthouse Validator Client")
|
||||
.version("0.0.1")
|
||||
.author("Sigma Prime <contact@sigmaprime.io>")
|
||||
.about("Eth 2.0 Validator Client")
|
||||
.arg(
|
||||
Arg::with_name("datadir")
|
||||
.long("datadir")
|
||||
.value_name("DIR")
|
||||
.help("Data directory for keys and databases.")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("server")
|
||||
.long("server")
|
||||
.value_name("server")
|
||||
.help("Address to connect to BeaconNode.")
|
||||
.takes_value(true),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let mut config = ClientConfig::default();
|
||||
|
||||
// Custom datadir
|
||||
if let Some(dir) = matches.value_of("datadir") {
|
||||
config.data_dir = PathBuf::from(dir.to_string());
|
||||
}
|
||||
|
||||
// Custom server port
|
||||
if let Some(server_str) = matches.value_of("server") {
|
||||
if let Ok(addr) = server_str.parse::<u16>() {
|
||||
config.server = addr.to_string();
|
||||
} else {
|
||||
error!(log, "Invalid address"; "server" => server_str);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Log configuration
|
||||
info!(log, "";
|
||||
"data_dir" => &config.data_dir.to_str(),
|
||||
"server" => &config.server);
|
||||
|
||||
// gRPC
|
||||
let env = Arc::new(EnvBuilder::new().build());
|
||||
let ch = ChannelBuilder::new(env).connect(&config.server);
|
||||
let client = Arc::new(BeaconBlockServiceClient::new(ch));
|
||||
|
||||
// Ethereum
|
||||
//
|
||||
// TODO: Permit loading a custom spec from file.
|
||||
let spec = Arc::new(ChainSpec::foundation());
|
||||
|
||||
let duration = spec
|
||||
.slot_duration
|
||||
.checked_mul(1_000)
|
||||
.expect("Slot duration overflow when converting from seconds to millis.");
|
||||
|
||||
// Global map of epoch -> validator duties.
|
||||
let epoch_map = Arc::new(RwLock::new(HashMap::new()));
|
||||
let slot_clock = {
|
||||
info!(log, "Genesis time"; "unix_epoch_seconds" => spec.genesis_time);
|
||||
@@ -40,32 +86,14 @@ fn main() {
|
||||
let block_producer =
|
||||
BlockProducer::new(spec.clone(), epoch_map.clone(), slot_clock.clone(), client);
|
||||
|
||||
info!(log, "Slot duration"; "milliseconds" => duration);
|
||||
let poll_interval_millis = spec.slot_duration * 1000 / 10; // 10% epoch time precision.
|
||||
info!(log, "Starting block producer service"; "polls_per_epoch" => spec.slot_duration * 1000 / poll_interval_millis);
|
||||
|
||||
let mut block_producer_service = BlockProducerService {
|
||||
block_producer,
|
||||
poll_interval_millis: spec.epoch_length * 1000 / 100, // 1% epoch time precision.
|
||||
poll_interval_millis,
|
||||
log: log.clone(),
|
||||
};
|
||||
|
||||
block_producer_service.run();
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub struct EpochDuties {
|
||||
block_production_slot: Option<u64>,
|
||||
shard: Option<u64>,
|
||||
}
|
||||
|
||||
impl EpochDuties {
|
||||
pub fn is_block_production_slot(&self, slot: u64) -> bool {
|
||||
match self.block_production_slot {
|
||||
Some(s) if s == slot => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_shard(&self) -> bool {
|
||||
self.shard.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user