Remove dupe info between ChainSpec and EthSpec

This commit is contained in:
Paul Hauner
2019-06-08 07:57:25 -04:00
parent f69d9093a3
commit e74d49fc8a
57 changed files with 299 additions and 252 deletions

View File

@@ -39,6 +39,8 @@ pub struct AttestationProducer<'a, B: BeaconNodeAttestation, S: Signer> {
pub beacon_node: Arc<B>,
/// The signer to sign the block.
pub signer: &'a S,
/// Used for caclulating epoch.
pub slots_per_epoch: u64,
}
impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> {
@@ -78,7 +80,7 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> {
/// The slash-protection code is not yet implemented. There is zero protection against
/// slashing.
pub fn produce_attestation(&mut self) -> Result<ValidatorEvent, Error> {
let epoch = self.duty.slot.epoch(self.spec.slots_per_epoch);
let epoch = self.duty.slot.epoch(self.slots_per_epoch);
let attestation = self
.beacon_node

View File

@@ -48,6 +48,8 @@ pub struct BlockProducer<'a, B: BeaconNodeBlock, S: Signer> {
pub beacon_node: Arc<B>,
/// The signer to sign the block.
pub signer: &'a S,
/// Used for caclulating epoch.
pub slots_per_epoch: u64,
}
impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> {
@@ -84,7 +86,7 @@ impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> {
/// The slash-protection code is not yet implemented. There is zero protection against
/// slashing.
pub fn produce_block(&mut self) -> Result<ValidatorEvent, Error> {
let epoch = self.slot.epoch(self.spec.slots_per_epoch);
let epoch = self.slot.epoch(self.slots_per_epoch);
let message = epoch.tree_hash_root();
let randao_reveal = match self.signer.sign_message(
@@ -186,9 +188,9 @@ mod tests {
let beacon_node = Arc::new(SimulatedBeaconNode::default());
let signer = Arc::new(LocalSigner::new(Keypair::random()));
let mut epoch_map = EpochMap::new(spec.slots_per_epoch);
let mut epoch_map = EpochMap::new(T::slots_per_epoch());
let produce_slot = Slot::new(100);
let produce_epoch = produce_slot.epoch(spec.slots_per_epoch);
let produce_epoch = produce_slot.epoch(T::slots_per_epoch());
epoch_map.map.insert(produce_epoch, produce_slot);
let epoch_map = Arc::new(epoch_map);
@@ -233,7 +235,7 @@ mod tests {
);
// In an epoch without known duties...
let slot = (produce_epoch.as_u64() + 1) * spec.slots_per_epoch;
let slot = (produce_epoch.as_u64() + 1) * T::slots_per_epoch();
slot_clock.set_slot(slot);
assert_eq!(
block_proposer.poll(),

View File

@@ -19,6 +19,7 @@ pub struct Config {
pub server: String,
/// The chain specification that we are connecting to
pub spec: ChainSpec,
pub slots_per_epoch: u64,
}
const DEFAULT_PRIVATE_KEY_FILENAME: &str = "private.key";
@@ -33,12 +34,13 @@ impl Default for Config {
let server = "localhost:5051".to_string();
let spec = FoundationEthSpec::spec();
let spec = FoundationEthSpec::default_spec();
Self {
data_dir,
server,
spec,
slots_per_epoch: FoundationEthSpec::slots_per_epoch(),
}
}
}
@@ -67,9 +69,9 @@ impl Config {
if let Some(spec_str) = args.value_of("spec") {
info!(log, "Using custom spec: {:?}", spec_str);
config.spec = match spec_str {
"foundation" => FoundationEthSpec::spec(),
"few_validators" => FewValidatorsEthSpec::spec(),
"lighthouse_testnet" => LighthouseTestnetEthSpec::spec(),
"foundation" => FoundationEthSpec::default_spec(),
"few_validators" => FewValidatorsEthSpec::default_spec(),
"lighthouse_testnet" => LighthouseTestnetEthSpec::default_spec(),
// Should be impossible due to clap's `possible_values(..)` function.
_ => unreachable!(),
};

View File

@@ -164,7 +164,7 @@ mod tests {
#[test]
pub fn polling() {
let spec = Arc::new(ChainSpec::foundation());
let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch));
let duties_map = Arc::new(EpochDutiesMap::new(T::slots_per_epoch()));
let keypair = Keypair::random();
let slot_clock = Arc::new(TestingSlotClock::new(0));
let beacon_node = Arc::new(TestBeaconNode::default());

View File

@@ -47,6 +47,7 @@ pub struct Service<B: BeaconNodeDuties + 'static, S: Signer + 'static> {
slot_clock: SystemTimeSlotClock,
/// The current slot we are processing.
current_slot: Slot,
slots_per_epoch: u64,
/// The chain specification for this clients instance.
spec: Arc<ChainSpec>,
/// The duties manager which maintains the state of when to perform actions.
@@ -177,7 +178,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
// Builds a mapping of Epoch -> Map(PublicKey, EpochDuty)
// where EpochDuty contains slot numbers and attestation data that each validator needs to
// produce work on.
let duties_map = RwLock::new(EpochDutiesMap::new(config.spec.slots_per_epoch));
let duties_map = RwLock::new(EpochDutiesMap::new(config.slots_per_epoch));
// builds a manager which maintains the list of current duties for all known validators
// and can check when a validator needs to perform a task.
@@ -194,6 +195,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
fork,
slot_clock,
current_slot,
slots_per_epoch: config.slots_per_epoch,
spec,
duties_manager,
beacon_block_client,
@@ -204,7 +206,10 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
/// Initialise the service then run the core thread.
// TODO: Improve handling of generic BeaconNode types, to stub grpcClient
pub fn start(config: ValidatorConfig, log: slog::Logger) -> error_chain::Result<()> {
pub fn start(
config: ValidatorConfig,
log: slog::Logger,
) -> error_chain::Result<()> {
// connect to the node and retrieve its properties and initialize the gRPC clients
let mut service =
Service::<ValidatorServiceClient, Keypair>::initialize_service(config, log)?;
@@ -274,7 +279,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
Ok(slot) => slot.expect("Genesis is in the future"),
};
let current_epoch = current_slot.epoch(self.spec.slots_per_epoch);
let current_epoch = current_slot.epoch(self.slots_per_epoch);
// this is a fatal error. If the slot clock repeats, there is something wrong with
// the timer, terminate immediately.
@@ -291,7 +296,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
fn check_for_duties(&mut self) {
let cloned_manager = self.duties_manager.clone();
let cloned_log = self.log.clone();
let current_epoch = self.current_slot.epoch(self.spec.slots_per_epoch);
let current_epoch = self.current_slot.epoch(self.slots_per_epoch);
// spawn a new thread separate to the runtime
// TODO: Handle thread termination/timeout
// TODO: Add duties thread back in, with channel to process duties in duty change.
@@ -316,6 +321,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
let spec = self.spec.clone();
let beacon_node = self.beacon_block_client.clone();
let log = self.log.clone();
let slots_per_epoch = self.slots_per_epoch;
std::thread::spawn(move || {
info!(log, "Producing a block"; "Validator"=> format!("{}", signers[signer_index]));
let signer = &signers[signer_index];
@@ -325,6 +331,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
spec,
beacon_node,
signer,
slots_per_epoch,
};
block_producer.handle_produce_block(log);
});
@@ -337,6 +344,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
let spec = self.spec.clone();
let beacon_node = self.attestation_client.clone();
let log = self.log.clone();
let slots_per_epoch = self.slots_per_epoch;
std::thread::spawn(move || {
info!(log, "Producing an attestation"; "Validator"=> format!("{}", signers[signer_index]));
let signer = &signers[signer_index];
@@ -346,6 +354,7 @@ impl<B: BeaconNodeDuties + 'static, S: Signer + 'static> Service<B, S> {
spec,
beacon_node,
signer,
slots_per_epoch,
};
attestation_producer.handle_produce_attestation(log);
});