mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 10:11:44 +00:00
Fixing merge conflict, having keys generated deterministically for testing.
This commit is contained in:
@@ -5,7 +5,7 @@ use protos::services::{
|
||||
use protos::services_grpc::BeaconBlockServiceClient;
|
||||
use ssz::{ssz_encode, Decodable};
|
||||
use std::sync::Arc;
|
||||
use types::{BeaconBlock, BeaconBlockBody, Eth1Data, Hash256, Signature, Slot};
|
||||
use types::{BeaconBlock, Signature, Slot};
|
||||
|
||||
/// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be
|
||||
/// implemented upon it.
|
||||
@@ -40,33 +40,12 @@ impl BeaconNode for BeaconBlockGrpcClient {
|
||||
|
||||
if reply.has_block() {
|
||||
let block = reply.get_block();
|
||||
let ssz = block.get_ssz();
|
||||
|
||||
let (signature, _) = Signature::ssz_decode(block.get_signature(), 0)
|
||||
.map_err(|_| BeaconNodeError::DecodeFailure)?;
|
||||
let (block, _i) =
|
||||
BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| BeaconNodeError::DecodeFailure)?;
|
||||
|
||||
let (randao_reveal, _) = Signature::ssz_decode(block.get_randao_reveal(), 0)
|
||||
.map_err(|_| BeaconNodeError::DecodeFailure)?;
|
||||
|
||||
// TODO: this conversion is incomplete; fix it.
|
||||
Ok(Some(BeaconBlock {
|
||||
slot: Slot::new(block.get_slot()),
|
||||
previous_block_root: Hash256::zero(),
|
||||
state_root: Hash256::zero(),
|
||||
signature,
|
||||
body: BeaconBlockBody {
|
||||
randao_reveal,
|
||||
eth1_data: Eth1Data {
|
||||
deposit_root: Hash256::zero(),
|
||||
block_hash: Hash256::zero(),
|
||||
},
|
||||
proposer_slashings: vec![],
|
||||
attester_slashings: vec![],
|
||||
attestations: vec![],
|
||||
deposits: vec![],
|
||||
voluntary_exits: vec![],
|
||||
transfers: vec![],
|
||||
},
|
||||
}))
|
||||
Ok(Some(block))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
@@ -79,12 +58,11 @@ impl BeaconNode for BeaconBlockGrpcClient {
|
||||
fn publish_beacon_block(&self, block: BeaconBlock) -> Result<PublishOutcome, BeaconNodeError> {
|
||||
let mut req = PublishBeaconBlockRequest::new();
|
||||
|
||||
let ssz = ssz_encode(&block);
|
||||
|
||||
// TODO: this conversion is incomplete; fix it.
|
||||
let mut grpc_block = GrpcBeaconBlock::new();
|
||||
grpc_block.set_slot(block.slot.as_u64());
|
||||
grpc_block.set_block_root(vec![0]);
|
||||
grpc_block.set_randao_reveal(ssz_encode(&block.body.randao_reveal));
|
||||
grpc_block.set_signature(ssz_encode(&block.signature));
|
||||
grpc_block.set_ssz(ssz);
|
||||
|
||||
req.set_block(grpc_block);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use clap::ArgMatches;
|
||||
use bincode;
|
||||
use bls::Keypair;
|
||||
use clap::ArgMatches;
|
||||
use slog::{debug, error, info};
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
@@ -67,6 +67,7 @@ impl Config {
|
||||
config.spec = match spec_str {
|
||||
"foundation" => ChainSpec::foundation(),
|
||||
"few_validators" => ChainSpec::few_validators(),
|
||||
"lighthouse_testnet" => ChainSpec::lighthouse_testnet(),
|
||||
// Should be impossible due to clap's `possible_values(..)` function.
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use super::epoch_duties::{EpochDuties, EpochDuty};
|
||||
use super::traits::{BeaconNode, BeaconNodeError};
|
||||
use grpcio::CallOption;
|
||||
use protos::services::{GetDutiesRequest, Validators};
|
||||
use protos::services_grpc::ValidatorServiceClient;
|
||||
use ssz::ssz_encode;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use types::{Epoch, PublicKey, Slot};
|
||||
|
||||
impl BeaconNode for ValidatorServiceClient {
|
||||
@@ -21,6 +23,9 @@ impl BeaconNode for ValidatorServiceClient {
|
||||
validators.set_public_keys(pubkeys.iter().map(|v| ssz_encode(v)).collect());
|
||||
req.set_validators(validators);
|
||||
|
||||
// set a timeout for requests
|
||||
// let call_opt = CallOption::default().timeout(Duration::from_secs(2));
|
||||
|
||||
// send the request, get the duties reply
|
||||
let reply = self
|
||||
.get_validator_duties(&req)
|
||||
@@ -31,7 +36,7 @@ impl BeaconNode for ValidatorServiceClient {
|
||||
if !validator_duty.has_duty() {
|
||||
// validator is inactive
|
||||
epoch_duties.insert(pubkeys[index].clone(), None);
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
// active validator
|
||||
let active_duty = validator_duty.get_duty();
|
||||
|
||||
@@ -51,20 +51,23 @@ impl<U: BeaconNode> DutiesManager<U> {
|
||||
/// be a wall-clock (e.g., system time, remote server time, etc.).
|
||||
fn update(&self, epoch: Epoch) -> Result<UpdateOutcome, Error> {
|
||||
let duties = self.beacon_node.request_duties(epoch, &self.pubkeys)?;
|
||||
// If these duties were known, check to see if they're updates or identical.
|
||||
if let Some(known_duties) = self.duties_map.read()?.get(&epoch) {
|
||||
if *known_duties == duties {
|
||||
return Ok(UpdateOutcome::NoChange(epoch));
|
||||
} else {
|
||||
//TODO: Duties could be large here. Remove from display and avoid the clone.
|
||||
self.duties_map.write()?.insert(epoch, duties.clone());
|
||||
return Ok(UpdateOutcome::DutiesChanged(epoch, duties));
|
||||
{
|
||||
// If these duties were known, check to see if they're updates or identical.
|
||||
if let Some(known_duties) = self.duties_map.read()?.get(&epoch) {
|
||||
if *known_duties == duties {
|
||||
return Ok(UpdateOutcome::NoChange(epoch));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
if !self.duties_map.read()?.contains_key(&epoch) {
|
||||
//TODO: Remove clone by removing duties from outcome
|
||||
self.duties_map.write()?.insert(epoch, duties.clone());
|
||||
return Ok(UpdateOutcome::NewDuties(epoch, duties));
|
||||
};
|
||||
}
|
||||
// duties have changed
|
||||
//TODO: Duties could be large here. Remove from display and avoid the clone.
|
||||
self.duties_map.write()?.insert(epoch, duties.clone());
|
||||
return Ok(UpdateOutcome::DutiesChanged(epoch, duties));
|
||||
}
|
||||
|
||||
/// A future wrapping around `update()`. This will perform logic based upon the update
|
||||
|
||||
@@ -43,8 +43,8 @@ fn main() {
|
||||
.short("s")
|
||||
.help("Configuration of Beacon Chain")
|
||||
.takes_value(true)
|
||||
.possible_values(&["foundation", "few_validators"])
|
||||
.default_value("foundation"),
|
||||
.possible_values(&["foundation", "few_validators", "lighthouse_testnet"])
|
||||
.default_value("lighthouse_testnet"),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
|
||||
@@ -43,8 +43,6 @@ pub struct Service {
|
||||
slot_clock: SystemTimeSlotClock,
|
||||
/// The current slot we are processing.
|
||||
current_slot: Slot,
|
||||
/// Duration until the next slot. This is used for initializing the tokio timer interval.
|
||||
duration_to_next_slot: Duration,
|
||||
/// The number of slots per epoch to allow for converting slots to epochs.
|
||||
slots_per_epoch: u64,
|
||||
// GRPC Clients
|
||||
@@ -105,6 +103,7 @@ impl Service {
|
||||
|
||||
// build requisite objects to form Self
|
||||
let genesis_time = node_info.get_genesis_time();
|
||||
let genesis_slot = Slot::from(node_info.get_genesis_slot());
|
||||
|
||||
info!(log,"Beacon node connected"; "Node Version" => node_info.version.clone(), "Chain ID" => node_info.chain_id, "Genesis time" => genesis_time);
|
||||
|
||||
@@ -140,46 +139,21 @@ impl Service {
|
||||
};
|
||||
|
||||
// build the validator slot clock
|
||||
let slot_clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot)
|
||||
.expect("Unable to instantiate SystemTimeSlotClock.");
|
||||
let slot_clock =
|
||||
SystemTimeSlotClock::new(genesis_slot, genesis_time, config.spec.seconds_per_slot)
|
||||
.expect("Unable to instantiate SystemTimeSlotClock.");
|
||||
|
||||
let current_slot = slot_clock
|
||||
.present_slot()
|
||||
.map_err(|e| ErrorKind::SlotClockError(e))?
|
||||
.expect("Genesis must be in the future");
|
||||
|
||||
// calculate the duration to the next slot
|
||||
let duration_to_next_slot = {
|
||||
let seconds_per_slot = config.spec.seconds_per_slot;
|
||||
let syslot_time = SystemTime::now();
|
||||
let duration_since_epoch = syslot_time
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|e| ErrorKind::SystemTimeError(e.to_string()))?;
|
||||
let duration_since_genesis = duration_since_epoch
|
||||
.checked_sub(Duration::from_secs(genesis_time))
|
||||
.expect("Genesis must be in the future. Checked on connection");
|
||||
let elapsed_slots = duration_since_epoch
|
||||
.as_secs()
|
||||
.checked_div(seconds_per_slot as u64)
|
||||
.expect("Seconds per slot should not be 0");
|
||||
|
||||
// the duration to the next slot
|
||||
Duration::from_secs(
|
||||
(elapsed_slots + 1)
|
||||
.checked_mul(seconds_per_slot)
|
||||
.expect("Next slot time should not overflow u64"),
|
||||
)
|
||||
.checked_sub(duration_since_genesis)
|
||||
.expect("This should never saturate")
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
connected_node_version: node_info.version,
|
||||
chain_id: node_info.chain_id as u16,
|
||||
fork,
|
||||
slot_clock,
|
||||
current_slot,
|
||||
duration_to_next_slot,
|
||||
slots_per_epoch: config.spec.slots_per_epoch,
|
||||
beacon_block_client,
|
||||
validator_client,
|
||||
@@ -202,15 +176,18 @@ impl Service {
|
||||
.build()
|
||||
.map_err(|e| format!("Tokio runtime failed: {}", e))?;
|
||||
|
||||
let duration_to_next_slot = service
|
||||
.slot_clock
|
||||
.duration_to_next_slot()
|
||||
.map_err(|e| format!("System clock error: {:?}", e))?
|
||||
.expect("Cannot start before genesis");
|
||||
|
||||
// set up the validator work interval - start at next slot and proceed every slot
|
||||
let interval = {
|
||||
// Set the interval to start at the next slot, and every slot after
|
||||
let slot_duration = Duration::from_secs(config.spec.seconds_per_slot);
|
||||
//TODO: Handle checked add correctly
|
||||
Interval::new(
|
||||
Instant::now() + service.duration_to_next_slot,
|
||||
slot_duration,
|
||||
)
|
||||
Interval::new(Instant::now() + duration_to_next_slot, slot_duration)
|
||||
};
|
||||
|
||||
/* kick off core service */
|
||||
@@ -219,7 +196,7 @@ impl Service {
|
||||
|
||||
// TODO: keypairs are randomly generated; they should be loaded from a file or generated.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
let keypairs = generate_deterministic_keypairs(8);
|
||||
let keypairs = Arc::new(generate_deterministic_keypairs(8));
|
||||
|
||||
/* build requisite objects to pass to core thread */
|
||||
|
||||
@@ -237,54 +214,59 @@ impl Service {
|
||||
});
|
||||
|
||||
// run the core thread
|
||||
runtime
|
||||
.block_on(interval.for_each(move |_| {
|
||||
let log = service.log.clone();
|
||||
runtime.block_on(
|
||||
interval
|
||||
.for_each(move |_| {
|
||||
let log = service.log.clone();
|
||||
|
||||
/* get the current slot and epoch */
|
||||
let current_slot = match service.slot_clock.present_slot() {
|
||||
Err(e) => {
|
||||
error!(log, "SystemTimeError {:?}", e);
|
||||
return Ok(());
|
||||
}
|
||||
Ok(slot) => slot.expect("Genesis is in the future"),
|
||||
};
|
||||
|
||||
let current_epoch = current_slot.epoch(service.slots_per_epoch);
|
||||
|
||||
debug_assert!(
|
||||
current_slot > service.current_slot,
|
||||
"The Timer should poll a new slot"
|
||||
);
|
||||
|
||||
info!(log, "Processing slot: {}", current_slot.as_u64());
|
||||
|
||||
/* check for new duties */
|
||||
|
||||
let cloned_manager = manager.clone();
|
||||
tokio::spawn(futures::future::poll_fn(move || {
|
||||
cloned_manager.run_update(current_epoch.clone(), log.clone())
|
||||
}));
|
||||
|
||||
/* execute any specified duties */
|
||||
|
||||
if let Some(work) = manager.get_current_work(current_slot) {
|
||||
for (_public_key, work_type) in work {
|
||||
if work_type.produce_block {
|
||||
// TODO: Produce a beacon block in a new thread
|
||||
/* get the current slot and epoch */
|
||||
let current_slot = match service.slot_clock.present_slot() {
|
||||
Err(e) => {
|
||||
error!(log, "SystemTimeError {:?}", e);
|
||||
return Ok(());
|
||||
}
|
||||
if work_type.attestation_duty.is_some() {
|
||||
// available AttestationDuty info
|
||||
let attestation_duty =
|
||||
work_type.attestation_duty.expect("Cannot be None");
|
||||
//TODO: Produce an attestation in a new thread
|
||||
Ok(slot) => slot.expect("Genesis is in the future"),
|
||||
};
|
||||
|
||||
let current_epoch = current_slot.epoch(service.slots_per_epoch);
|
||||
|
||||
debug_assert!(
|
||||
current_slot > service.current_slot,
|
||||
"The Timer should poll a new slot"
|
||||
);
|
||||
|
||||
info!(log, "Processing slot: {}", current_slot.as_u64());
|
||||
|
||||
/* check for new duties */
|
||||
|
||||
let cloned_manager = manager.clone();
|
||||
let cloned_log = log.clone();
|
||||
// spawn a new thread separate to the runtime
|
||||
std::thread::spawn(move || {
|
||||
cloned_manager.run_update(current_epoch.clone(), cloned_log.clone());
|
||||
dbg!("Finished thread");
|
||||
});
|
||||
|
||||
/* execute any specified duties */
|
||||
|
||||
if let Some(work) = manager.get_current_work(current_slot) {
|
||||
for (_public_key, work_type) in work {
|
||||
if work_type.produce_block {
|
||||
// TODO: Produce a beacon block in a new thread
|
||||
}
|
||||
if work_type.attestation_duty.is_some() {
|
||||
// available AttestationDuty info
|
||||
let attestation_duty =
|
||||
work_type.attestation_duty.expect("Cannot be None");
|
||||
//TODO: Produce an attestation in a new thread
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}))
|
||||
.map_err(|e| format!("Service thread failed: {:?}", e))?;
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|e| format!("Service thread failed: {:?}", e)),
|
||||
);
|
||||
|
||||
// completed a slot process
|
||||
Ok(())
|
||||
@@ -292,35 +274,6 @@ impl Service {
|
||||
|
||||
/*
|
||||
|
||||
for keypair in keypairs {
|
||||
info!(self.log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id());
|
||||
|
||||
// Spawn a new thread to maintain the validator's `EpochDuties`.
|
||||
let duties_manager_thread = {
|
||||
let spec = spec.clone();
|
||||
let duties_map = duties_map.clone();
|
||||
let slot_clock = self.slot_clock.clone();
|
||||
let log = self.log.clone();
|
||||
let beacon_node = self.validator_client.clone();
|
||||
let pubkey = keypair.pk.clone();
|
||||
thread::spawn(move || {
|
||||
let manager = DutiesManager {
|
||||
duties_map,
|
||||
pubkey,
|
||||
spec,
|
||||
slot_clock,
|
||||
beacon_node,
|
||||
};
|
||||
let mut duties_manager_service = DutiesManagerService {
|
||||
manager,
|
||||
poll_interval_millis,
|
||||
log,
|
||||
};
|
||||
|
||||
duties_manager_service.run();
|
||||
})
|
||||
};
|
||||
|
||||
// Spawn a new thread to perform block production for the validator.
|
||||
let producer_thread = {
|
||||
let spec = spec.clone();
|
||||
|
||||
Reference in New Issue
Block a user