From 8f3530f60ce900f41d48c2d407e26cb0a727f9b3 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 8 Mar 2019 13:48:33 +0100 Subject: [PATCH 01/61] created attester_service and started to create an attester_thread in main of validator_client (lighthouse-255) --- validator_client/Cargo.toml | 1 + validator_client/src/attester_service/mod.rs | 51 ++++++++++++++++++++ validator_client/src/main.rs | 29 ++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 validator_client/src/attester_service/mod.rs diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index f76772f28f..cdde71774f 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] block_proposer = { path = "../eth2/block_proposer" } +attester = { path = "../eth2/attester" } bls = { path = "../eth2/utils/bls" } clap = "2.32.0" dirs = "1.0.3" diff --git a/validator_client/src/attester_service/mod.rs b/validator_client/src/attester_service/mod.rs new file mode 100644 index 0000000000..fbf8bd005a --- /dev/null +++ b/validator_client/src/attester_service/mod.rs @@ -0,0 +1,51 @@ +use attester::{Attester, BeaconNode, DutiesReader, PollOutcome as AttesterPollOutcome, Signer}; +use slog::Logger; +use slot_clock::SlotClock; +use std::time::Duration; + +pub struct AttesterService { + pub attester: Attester, + pub poll_interval_millis: u64, + pub log: Logger, +} + +impl AttesterService { + /// Run a loop which polls the Attester each `poll_interval_millis` millseconds. + /// + /// Logs the results of the polls. + pub fn run(&mut self) { + loop { + match self.attester.poll() { + Err(error) => { + error!(self.log, "Attester poll error"; "error" => format!("{:?}", error)) + } + Ok(AttesterPollOutcome::AttestationProduced(slot)) => { + info!(self.log, "Produced Attestation"; "slot" => slot) + } + Ok(AttesterPollOutcome::SlashableAttestationNotProduced(slot)) => { + warn!(self.log, "Slashable attestation was not produced"; "slot" => slot) + } + Ok(AttesterPollOutcome::AttestationNotRequired(slot)) => { + info!(self.log, "Attestation not required"; "slot" => slot) + } + Ok(AttesterPollOutcome::ProducerDutiesUnknown(slot)) => { + error!(self.log, "Attestation duties unknown"; "slot" => slot) + } + Ok(AttesterPollOutcome::SlotAlreadyProcessed(slot)) => { + warn!(self.log, "Attempted to re-process slot"; "slot" => slot) + } + Ok(AttesterPollOutcome::BeaconNodeUnableToProduceAttestation(slot)) => { + error!(self.log, "Beacon node unable to produce attestation"; "slot" => slot) + } + Ok(AttesterPollOutcome::SignerRejection(slot)) => { + error!(self.log, "The cryptographic signer refused to sign the attestation"; "slot" => slot) + } + Ok(AttesterPollOutcome::ValidatorIsUnknown(slot)) => { + error!(self.log, "The Beacon Node does not recognise the validator"; "slot" => slot) + } + }; + + std::thread::sleep(Duration::from_millis(self.poll_interval_millis)); + } + } +} diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index ebab8538c5..2e432ceffc 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,7 +1,9 @@ use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; +use crate::attester_service::AttesterService; use crate::config::ClientConfig; -use block_proposer::{test_utils::LocalSigner, BlockProducer}; +use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; +use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; use bls::Keypair; use clap::{App, Arg}; use grpcio::{ChannelBuilder, EnvBuilder}; @@ -13,7 +15,9 @@ use std::sync::Arc; use std::thread; use types::ChainSpec; +mod attester_service; mod block_producer_service; + mod config; mod duties; @@ -160,7 +164,7 @@ fn main() { // Spawn a new thread to perform block production for the validator. let producer_thread = { let spec = spec.clone(); - let signer = Arc::new(LocalSigner::new(keypair.clone())); + let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone())); let duties_map = duties_map.clone(); let slot_clock = slot_clock.clone(); let log = log.clone(); @@ -178,6 +182,27 @@ fn main() { }) }; + //Spawn a new thread for attestation for the validator. + let attester_thread = { + let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); + let duties_map = duties_map.clone(); + let slot_clock = slot_clock.clone(); + let log = log.clone(); + //TODO: this is wrong, I assume this has to be AttesterGrpcClient, which has to be defined analogous + // to beacon_block_grpc_client.rs + let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone())); + thread::spawn(move || { + let attester = Attester::new(duties_map, slot_clock, client, signer); + let mut attester_service = AttesterService { + attester, + poll_interval_millis, + log, + }; + + block_producer_service.run(); + }) + }; + threads.push((duties_manager_thread, producer_thread)); } From 2215aa4b4640e5bf4a5ace25a184de1dd0ad934c Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 15 Mar 2019 11:44:39 +0100 Subject: [PATCH 02/61] added protos specification for Attester and created first draft for attestation_grpc_client (lighthouse-255) --- protos/src/services.proto | 47 +++++++++++++++++++ .../attestation_grpc_client.rs | 32 +++++++++++++ validator_client/src/attester_service/mod.rs | 5 +- validator_client/src/main.rs | 21 ++++++--- 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 validator_client/src/attester_service/attestation_grpc_client.rs diff --git a/protos/src/services.proto b/protos/src/services.proto index 16e2d4dba7..0523cc958a 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -23,6 +23,11 @@ service ValidatorService { rpc ValidatorIndex(PublicKey) returns (IndexResponse); } +service AttestationService { + rpc ProduceAttestationData (ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); + rpc PublishAttestationData (PublishAttestationDataRequest) returns (PublishAttestationDataResponse); +} + message BeaconBlock { uint64 slot = 1; bytes block_root = 2; @@ -30,6 +35,30 @@ message BeaconBlock { bytes signature = 4; } +message Crosslink { + uint64 epoch = 1; + bytes crosslink_data_root = 2; + +} + +message AttestationData { + uint64 slot = 1; + uint64 shard = 2; + bytes beacon_block_root = 3; + bytes epoch_boundary_root = 4; + bytes crosslink_data_root = 5; + Crosslink latest_crosslink = 6; + uint64 justified_epoch = 7; + bytes justified_block_root = 8; + +} + +message FreeAttestation { + AttestationData attestation_data = 1; + bytes signature = 2; + uint64 validator_index = 3; +} + // Validator requests an unsigned proposal. message ProduceBeaconBlockRequest { uint64 slot = 1; @@ -51,6 +80,24 @@ message PublishBeaconBlockResponse { bytes msg = 2; } +message ProduceAttestationDataRequest { + uint64 slot = 1; + uint64 shard = 2; +} + +message ProduceAttestationDataResponse { + AttestationData attestation_data = 1; +} + +message PublishAttestationDataRequest { + FreeAttestation free_attestation = 1; +} + +message PublishAttestationDataResponse { + bool success = 1; + bytes msg = 2; +} + // A validators duties for some epoch. // TODO: add shard duties. message ValidatorAssignment { diff --git a/validator_client/src/attester_service/attestation_grpc_client.rs b/validator_client/src/attester_service/attestation_grpc_client.rs new file mode 100644 index 0000000000..b3a0bd134e --- /dev/null +++ b/validator_client/src/attester_service/attestation_grpc_client.rs @@ -0,0 +1,32 @@ +use protos::services_grpc::AttestationServiceClient; +use std::sync::Arc; + +use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; +use types::{AttestationData, FreeAttestation, Slot}; + +pub struct AttestationGrpcClient { + client: Arc, +} + +impl AttestationGrpcClient { + pub fn new(client: Arc) -> Self { + Self { client } + } +} + +impl BeaconNode for AttestationGrpcClient { + fn produce_attestation_data( + &self, + slot: Slot, + shard: u64, + ) -> Result, BeaconNodeError> { + Err(BeaconNodeError::DecodeFailure) + } + + fn publish_attestation_data( + &self, + free_attestation: FreeAttestation, + ) -> Result { + Err(BeaconNodeError::DecodeFailure) + } +} diff --git a/validator_client/src/attester_service/mod.rs b/validator_client/src/attester_service/mod.rs index fbf8bd005a..fe5de7647f 100644 --- a/validator_client/src/attester_service/mod.rs +++ b/validator_client/src/attester_service/mod.rs @@ -1,8 +1,11 @@ +mod attestation_grpc_client; use attester::{Attester, BeaconNode, DutiesReader, PollOutcome as AttesterPollOutcome, Signer}; -use slog::Logger; +use slog::{error, info, warn, Logger}; use slot_clock::SlotClock; use std::time::Duration; +pub use self::attestation_grpc_client::AttestationGrpcClient; + pub struct AttesterService { pub attester: Attester, pub poll_interval_millis: u64, diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 2e432ceffc..60bc76553c 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,13 +1,15 @@ use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; -use crate::attester_service::AttesterService; +use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::config::ClientConfig; use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; use bls::Keypair; use clap::{App, Arg}; use grpcio::{ChannelBuilder, EnvBuilder}; -use protos::services_grpc::{BeaconBlockServiceClient, ValidatorServiceClient}; +use protos::services_grpc::{ + AttestationServiceClient, BeaconBlockServiceClient, ValidatorServiceClient, +}; use slog::{error, info, o, Drain}; use slot_clock::SystemTimeSlotClock; use std::path::PathBuf; @@ -106,6 +108,13 @@ fn main() { Arc::new(ValidatorServiceClient::new(ch)) }; + //Beacon node gRPC attester endpoints. + let attester_grpc_client = { + let env = Arc::new(EnvBuilder::new().build()); + let ch = ChannelBuilder::new(env).connect(&config.server); + Arc::new(AttestationServiceClient::new(ch)) + }; + // Spec let spec = Arc::new(config.spec.clone()); @@ -182,15 +191,13 @@ fn main() { }) }; - //Spawn a new thread for attestation for the validator. + // Spawn a new thread for attestation for the validator. let attester_thread = { let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); let duties_map = duties_map.clone(); let slot_clock = slot_clock.clone(); let log = log.clone(); - //TODO: this is wrong, I assume this has to be AttesterGrpcClient, which has to be defined analogous - // to beacon_block_grpc_client.rs - let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone())); + let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone())); thread::spawn(move || { let attester = Attester::new(duties_map, slot_clock, client, signer); let mut attester_service = AttesterService { @@ -199,7 +206,7 @@ fn main() { log, }; - block_producer_service.run(); + attester_service.run(); }) }; From d8099ae00c735058a88ea8205bfede7ee8c50683 Mon Sep 17 00:00:00 2001 From: thojest Date: Mon, 18 Mar 2019 21:12:06 +0100 Subject: [PATCH 03/61] started implementing BeaconNode for AttestationGrpcClient; included correct epoch_map for instantiation of Attester (lighthouse-255) --- .../attester_service/attestation_grpc_client.rs | 12 ++++++++++++ validator_client/src/main.rs | 14 ++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/validator_client/src/attester_service/attestation_grpc_client.rs b/validator_client/src/attester_service/attestation_grpc_client.rs index b3a0bd134e..566d74a39c 100644 --- a/validator_client/src/attester_service/attestation_grpc_client.rs +++ b/validator_client/src/attester_service/attestation_grpc_client.rs @@ -2,6 +2,7 @@ use protos::services_grpc::AttestationServiceClient; use std::sync::Arc; use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; +use protos::services::ProduceAttestationDataRequest; use types::{AttestationData, FreeAttestation, Slot}; pub struct AttestationGrpcClient { @@ -20,6 +21,16 @@ impl BeaconNode for AttestationGrpcClient { slot: Slot, shard: u64, ) -> Result, BeaconNodeError> { + let mut req = ProduceAttestationDataRequest::new(); + req.set_slot(slot.as_u64()); + req.set_shard(shard); + + let reply = self + .client + .produce_attestation_data(&req) + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + + // TODO: return correct AttestationData Err(BeaconNodeError::DecodeFailure) } @@ -27,6 +38,7 @@ impl BeaconNode for AttestationGrpcClient { &self, free_attestation: FreeAttestation, ) -> Result { + // TODO: return correct PublishOutcome Err(BeaconNodeError::DecodeFailure) } } diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 60bc76553c..4664d5dc93 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -2,6 +2,7 @@ use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::config::ClientConfig; +use attester::test_utils::EpochMap; use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; use bls::Keypair; @@ -19,7 +20,6 @@ use types::ChainSpec; mod attester_service; mod block_producer_service; - mod config; mod duties; @@ -143,6 +143,7 @@ fn main() { for keypair in keypairs { info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); + let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); // Spawn a new thread to maintain the validator's `EpochDuties`. let duties_manager_thread = { @@ -191,15 +192,15 @@ fn main() { }) }; - // Spawn a new thread for attestation for the validator. + // Spawn a new thread for attestation for the validator. let attester_thread = { let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); - let duties_map = duties_map.clone(); + let epoch_map = epoch_map_for_attester.clone(); let slot_clock = slot_clock.clone(); let log = log.clone(); let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone())); thread::spawn(move || { - let attester = Attester::new(duties_map, slot_clock, client, signer); + let attester = Attester::new(epoch_map, slot_clock, client, signer); let mut attester_service = AttesterService { attester, poll_interval_millis, @@ -210,13 +211,14 @@ fn main() { }) }; - threads.push((duties_manager_thread, producer_thread)); + threads.push((duties_manager_thread, producer_thread, attester_thread)); } // Naively wait for all the threads to complete. for tuple in threads { - let (manager, producer) = tuple; + let (manager, producer, attester) = tuple; let _ = producer.join(); let _ = manager.join(); + let _ = attester.join(); } } From e758e717533f22ca73223da2047d7bb547cc9f47 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 12:13:48 +1100 Subject: [PATCH 04/61] Tidy services.proto --- protos/src/services.proto | 180 +++++++++++++++++++++----------------- 1 file changed, 98 insertions(+), 82 deletions(-) diff --git a/protos/src/services.proto b/protos/src/services.proto index 0523cc958a..79fffd088f 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -12,22 +12,52 @@ syntax = "proto3"; package ethereum.beacon.rpc.v1; + +/// Service that handles block production service BeaconBlockService { rpc ProduceBeaconBlock(ProduceBeaconBlockRequest) returns (ProduceBeaconBlockResponse); rpc PublishBeaconBlock(PublishBeaconBlockRequest) returns (PublishBeaconBlockResponse); } +/// Service that provides the validator client with requisite knowledge about +//its public keys service ValidatorService { - // rpc ValidatorAssignment(ValidatorAssignmentRequest) returns (ValidatorAssignmentResponse); rpc ProposeBlockSlot(ProposeBlockSlotRequest) returns (ProposeBlockSlotResponse); rpc ValidatorIndex(PublicKey) returns (IndexResponse); + // rpc ValidatorAssignment(ValidatorAssignmentRequest) returns (ValidatorAssignmentResponse); } +/// Service that handles validator attestations service AttestationService { rpc ProduceAttestationData (ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); rpc PublishAttestationData (PublishAttestationDataRequest) returns (PublishAttestationDataResponse); } +/* + * Block Production Service Messages + */ + +// Validator requests an unsigned proposal. +message ProduceBeaconBlockRequest { + uint64 slot = 1; +} + +// Beacon node returns an unsigned proposal. +message ProduceBeaconBlockResponse { + BeaconBlock block = 1; +} + +// Validator submits a signed proposal. +message PublishBeaconBlockRequest { + BeaconBlock block = 1; +} + +// Beacon node indicates a sucessfully submitted proposal. +message PublishBeaconBlockResponse { + bool success = 1; + bytes msg = 2; +} + message BeaconBlock { uint64 slot = 1; bytes block_root = 2; @@ -35,6 +65,73 @@ message BeaconBlock { bytes signature = 4; } +/* + * Validator Service Messages + */ +/* +message ValidatorAssignmentRequest { + uint64 epoch = 1; + bytes validator_index = 2; +} + +// A validators duties for some epoch. +// TODO: add shard duties. +message ValidatorAssignment { + oneof block_production_slot_oneof { + bool block_production_slot_none = 1; + uint64 block_production_slot = 2; + } +} +*/ + +// Validator Assignment + +message PublicKey { + bytes public_key = 1; +} + +message IndexResponse { + uint64 index = 1; +} + + +// Propose slot + +message ProposeBlockSlotRequest { + uint64 epoch = 1; + uint64 validator_index = 2; +} + +message ProposeBlockSlotResponse { + oneof slot_oneof { + bool none = 1; + uint64 slot = 2; + } +} + + +/* + * Attestation Service Messages + */ + +message ProduceAttestationDataRequest { + uint64 slot = 1; + uint64 shard = 2; +} + +message ProduceAttestationDataResponse { + AttestationData attestation_data = 1; +} + +message PublishAttestationDataRequest { + FreeAttestation free_attestation = 1; +} + +message PublishAttestationDataResponse { + bool success = 1; + bytes msg = 2; +} + message Crosslink { uint64 epoch = 1; bytes crosslink_data_root = 2; @@ -58,84 +155,3 @@ message FreeAttestation { bytes signature = 2; uint64 validator_index = 3; } - -// Validator requests an unsigned proposal. -message ProduceBeaconBlockRequest { - uint64 slot = 1; -} - -// Beacon node returns an unsigned proposal. -message ProduceBeaconBlockResponse { - BeaconBlock block = 1; -} - -// Validator submits a signed proposal. -message PublishBeaconBlockRequest { - BeaconBlock block = 1; -} - -// Beacon node indicates a sucessfully submitted proposal. -message PublishBeaconBlockResponse { - bool success = 1; - bytes msg = 2; -} - -message ProduceAttestationDataRequest { - uint64 slot = 1; - uint64 shard = 2; -} - -message ProduceAttestationDataResponse { - AttestationData attestation_data = 1; -} - -message PublishAttestationDataRequest { - FreeAttestation free_attestation = 1; -} - -message PublishAttestationDataResponse { - bool success = 1; - bytes msg = 2; -} - -// A validators duties for some epoch. -// TODO: add shard duties. -message ValidatorAssignment { - oneof block_production_slot_oneof { - bool block_production_slot_none = 1; - uint64 block_production_slot = 2; - } -} - -message ValidatorAssignmentRequest { - uint64 epoch = 1; - bytes validator_index = 2; -} - -/* - * Propose slot - */ - -message ProposeBlockSlotRequest { - uint64 epoch = 1; - uint64 validator_index = 2; -} - -message ProposeBlockSlotResponse { - oneof slot_oneof { - bool none = 1; - uint64 slot = 2; - } -} - -/* - * Validator Assignment - */ - -message PublicKey { - bytes public_key = 1; -} - -message IndexResponse { - uint64 index = 1; -} From 0a59a73894b2c3294da08f7a8cc35150f5e24165 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 12:36:45 +1100 Subject: [PATCH 05/61] Add BeaconNodeService to RPC --- protos/src/services.proto | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/protos/src/services.proto b/protos/src/services.proto index 79fffd088f..45fcfb1203 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -12,6 +12,10 @@ syntax = "proto3"; package ethereum.beacon.rpc.v1; +// Service that currently identifies a beacon node +service BeaconNodeService { + rpc Info(Empty) returns (NodeInfo); +} /// Service that handles block production service BeaconBlockService { @@ -33,6 +37,26 @@ service AttestationService { rpc PublishAttestationData (PublishAttestationDataRequest) returns (PublishAttestationDataResponse); } +/* + * Beacon Node Service Message + */ +message NodeInfo { + string version = 1; + Fork fork = 2; + uint32 chain_id = 3; +} + +message Fork { + bytes previous_version = 1; + bytes current_version = 2; + uint64 epoch = 3; +} + +message Empty { +} + + + /* * Block Production Service Messages */ From 844fdc0fb9122c6ddd21acb849d78421e3560c8e Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 12:39:45 +1100 Subject: [PATCH 06/61] Rename network_id to chain_id --- beacon_node/network/src/sync/simple_sync.rs | 8 ++++---- eth2/types/src/chain_spec.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 0f7de6ab97..d3d0e14750 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -36,7 +36,7 @@ pub struct SimpleSync { /// The current state of the syncing protocol. state: SyncState, /// The network id, for quick HELLO RPC message lookup. - network_id: u8, + chain_id: u8, /// The latest epoch of the syncing chain. latest_finalized_epoch: Epoch, /// The latest block of the syncing chain. @@ -53,7 +53,7 @@ impl SimpleSync { chain: beacon_chain.clone(), known_peers: HashMap::new(), state: SyncState::Idle, - network_id: beacon_chain.get_spec().network_id, + chain_id: beacon_chain.get_spec().chain_id, latest_finalized_epoch: state.finalized_epoch, latest_slot: state.slot - 1, //TODO: Build latest block function into Beacon chain and correct this log: sync_logger, @@ -65,7 +65,7 @@ impl SimpleSync { let state = &self.chain.get_state(); //TODO: Paul to verify the logic of these fields. HelloMessage { - network_id: self.network_id, + network_id: self.chain_id, latest_finalized_root: state.finalized_root, latest_finalized_epoch: state.finalized_epoch, best_root: Hash256::zero(), //TODO: build correct value as a beacon chain function @@ -75,7 +75,7 @@ impl SimpleSync { pub fn validate_peer(&mut self, peer_id: PeerId, hello_message: HelloMessage) -> bool { // network id must match - if hello_message.network_id != self.network_id { + if hello_message.network_id != self.chain_id { return false; } // compare latest epoch and finalized root to see if they exist in our chain diff --git a/eth2/types/src/chain_spec.rs b/eth2/types/src/chain_spec.rs index 65ea5c4d4e..5fa28b6acf 100644 --- a/eth2/types/src/chain_spec.rs +++ b/eth2/types/src/chain_spec.rs @@ -118,7 +118,7 @@ pub struct ChainSpec { * */ pub boot_nodes: Vec, - pub network_id: u8, + pub chain_id: u8, } impl ChainSpec { @@ -255,7 +255,7 @@ impl ChainSpec { * Boot nodes */ boot_nodes: vec![], - network_id: 1, // foundation network id + chain_id: 1, // foundation chain id } } @@ -272,7 +272,7 @@ impl ChainSpec { Self { boot_nodes, - network_id: 2, // lighthouse testnet network id + chain_id: 2, // lighthouse testnet chain id ..ChainSpec::few_validators() } } From ee6a0ccb92bca9a49b023533155e8b837ab8bfe0 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 13:37:24 +1100 Subject: [PATCH 07/61] Initial build of server-side BeaconNodeService RPC --- beacon_node/client/src/lib.rs | 2 +- beacon_node/rpc/Cargo.toml | 8 ++--- beacon_node/rpc/src/beacon_node.rs | 50 ++++++++++++++++++++++++++++++ beacon_node/rpc/src/lib.rs | 28 +++++++++++++++-- 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 beacon_node/rpc/src/beacon_node.rs diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 914e47fcf4..d8da18caef 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -61,7 +61,7 @@ impl Client { // spawn the RPC server if config.rpc_conf.enabled { - rpc::start_server(&config.rpc_conf, &log); + rpc::start_server(&config.rpc_conf, beacon_chain.clone(), &log); } Ok(Client { diff --git a/beacon_node/rpc/Cargo.toml b/beacon_node/rpc/Cargo.toml index 4c3333ee1d..acb68972c4 100644 --- a/beacon_node/rpc/Cargo.toml +++ b/beacon_node/rpc/Cargo.toml @@ -7,7 +7,10 @@ edition = "2018" [dependencies] bls = { path = "../../eth2/utils/bls" } beacon_chain = { path = "../beacon_chain" } - +version = { path = "../version" } +types = { path = "../../eth2/types" } +ssz = { path = "../../eth2/utils/ssz" } +slot_clock = { path = "../../eth2/utils/slot_clock" } protos = { path = "../../protos" } grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] } protobuf = "2.0.2" @@ -16,8 +19,5 @@ db = { path = "../db" } dirs = "1.0.3" futures = "0.1.23" slog = "^2.2.3" -slot_clock = { path = "../../eth2/utils/slot_clock" } slog-term = "^2.4.0" slog-async = "^2.3.0" -types = { path = "../../eth2/types" } -ssz = { path = "../../eth2/utils/ssz" } diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs new file mode 100644 index 0000000000..8dd7721fde --- /dev/null +++ b/beacon_node/rpc/src/beacon_node.rs @@ -0,0 +1,50 @@ +use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain}; +use futures::Future; +use grpcio::{RpcContext, UnarySink}; +use protos::services::{Empty, Fork, NodeInfo}; +use protos::services_grpc::BeaconNodeService; +use slog::{debug, trace, warn}; +use std::sync::Arc; + +#[derive(Clone)] +pub struct BeaconNodeServiceInstance +where + T: ClientDB + Clone, + U: SlotClock + Clone, + F: ForkChoice + Clone, +{ + pub chain: Arc>, + pub log: slog::Logger, +} + +impl BeaconNodeService for BeaconNodeServiceInstance +where + T: ClientDB + Clone, + U: SlotClock + Clone, + F: ForkChoice + Clone, +{ + /// Provides basic node information. + fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { + trace!(self.log, "Node info requested via RPC"); + + let mut node_info = NodeInfo::new(); + node_info.set_version(version::version()); + // get the chain state fork + let state_fork = self.chain.state.read().fork.clone(); + // build the rpc fork struct + let mut fork = Fork::new(); + fork.set_previous_version(state_fork.previous_version.to_vec()); + fork.set_current_version(state_fork.current_version.to_vec()); + fork.set_epoch(state_fork.epoch.into()); + node_info.set_fork(fork); + + node_info.set_chain_id(self.chain.spec.chain_id as u32); + + // send the node_info the requester + let error_log = self.log.clone(); + let f = sink + .success(node_info) + .map_err(move |e| warn!(error_log, "failed to reply {:?}", e)); + ctx.spawn(f) + } +} diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 7f776d7d85..2f2abcc1fc 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -1,20 +1,44 @@ mod beacon_block; +mod beacon_node; pub mod config; mod validator; use self::beacon_block::BeaconBlockServiceInstance; +use self::beacon_node::BeaconNodeServiceInstance; use self::validator::ValidatorServiceInstance; +use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain}; pub use config::Config as RPCConfig; use grpcio::{Environment, Server, ServerBuilder}; -use protos::services_grpc::{create_beacon_block_service, create_validator_service}; +use protos::services_grpc::{ + create_beacon_block_service, create_beacon_node_service, create_validator_service, +}; use std::sync::Arc; use slog::{info, o}; -pub fn start_server(config: &RPCConfig, log: &slog::Logger) -> Server { +pub fn start_server( + config: &RPCConfig, + beacon_chain: Arc>, + log: &slog::Logger, +) -> Server +where + T: ClientDB + Clone + 'static, + U: SlotClock + Clone + 'static, + F: ForkChoice + Clone + 'static, +{ let log = log.new(o!("Service"=>"RPC")); let env = Arc::new(Environment::new(1)); + // build the individual rpc services + + let beacon_node_service = { + let instance = BeaconNodeServiceInstance { + chain: beacon_chain.clone(), + log: log.clone(), + }; + create_beacon_node_service(instance) + }; + let beacon_block_service = { let instance = BeaconBlockServiceInstance { log: log.clone() }; create_beacon_block_service(instance) From 858cf4f1f42b4c56a6fbdf71c44fbb06e2e5097a Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 13:51:17 +1100 Subject: [PATCH 08/61] Add beacon_chain trait for gRPC server --- beacon_node/rpc/src/beacon_chain.rs | 31 +++++++++++++++++++++++++++++ beacon_node/rpc/src/beacon_node.rs | 22 ++++++-------------- beacon_node/rpc/src/lib.rs | 14 +++++-------- 3 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 beacon_node/rpc/src/beacon_chain.rs diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs new file mode 100644 index 0000000000..9b26818767 --- /dev/null +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -0,0 +1,31 @@ +use beacon_chain::BeaconChain as RawBeaconChain; +use beacon_chain::{ + db::ClientDB, + fork_choice::ForkChoice, + parking_lot::RwLockReadGuard, + slot_clock::SlotClock, + types::{BeaconState, ChainSpec}, + CheckPoint, +}; + +/// The RPC's API to the beacon chain. +pub trait BeaconChain: Send + Sync { + fn get_spec(&self) -> &ChainSpec; + + fn get_state(&self) -> RwLockReadGuard; +} + +impl BeaconChain for RawBeaconChain +where + T: ClientDB + Sized, + U: SlotClock, + F: ForkChoice, +{ + fn get_spec(&self) -> &ChainSpec { + &self.spec + } + + fn get_state(&self) -> RwLockReadGuard { + self.state.read() + } +} diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 8dd7721fde..5a542cb905 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -1,4 +1,4 @@ -use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain}; +use crate::beacon_chain::BeaconChain; use futures::Future; use grpcio::{RpcContext, UnarySink}; use protos::services::{Empty, Fork, NodeInfo}; @@ -7,22 +7,12 @@ use slog::{debug, trace, warn}; use std::sync::Arc; #[derive(Clone)] -pub struct BeaconNodeServiceInstance -where - T: ClientDB + Clone, - U: SlotClock + Clone, - F: ForkChoice + Clone, -{ - pub chain: Arc>, +pub struct BeaconNodeServiceInstance { + pub chain: Arc, pub log: slog::Logger, } -impl BeaconNodeService for BeaconNodeServiceInstance -where - T: ClientDB + Clone, - U: SlotClock + Clone, - F: ForkChoice + Clone, -{ +impl BeaconNodeService for BeaconNodeServiceInstance { /// Provides basic node information. fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { trace!(self.log, "Node info requested via RPC"); @@ -30,7 +20,7 @@ where let mut node_info = NodeInfo::new(); node_info.set_version(version::version()); // get the chain state fork - let state_fork = self.chain.state.read().fork.clone(); + let state_fork = self.chain.get_state().fork.clone(); // build the rpc fork struct let mut fork = Fork::new(); fork.set_previous_version(state_fork.previous_version.to_vec()); @@ -38,7 +28,7 @@ where fork.set_epoch(state_fork.epoch.into()); node_info.set_fork(fork); - node_info.set_chain_id(self.chain.spec.chain_id as u32); + node_info.set_chain_id(self.chain.get_spec().chain_id as u32); // send the node_info the requester let error_log = self.log.clone(); diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 2f2abcc1fc..4565abf7ae 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -1,12 +1,13 @@ mod beacon_block; +pub mod beacon_chain; mod beacon_node; pub mod config; mod validator; use self::beacon_block::BeaconBlockServiceInstance; +use self::beacon_chain::BeaconChain; use self::beacon_node::BeaconNodeServiceInstance; use self::validator::ValidatorServiceInstance; -use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain}; pub use config::Config as RPCConfig; use grpcio::{Environment, Server, ServerBuilder}; use protos::services_grpc::{ @@ -16,16 +17,11 @@ use std::sync::Arc; use slog::{info, o}; -pub fn start_server( +pub fn start_server( config: &RPCConfig, - beacon_chain: Arc>, + beacon_chain: Arc, log: &slog::Logger, -) -> Server -where - T: ClientDB + Clone + 'static, - U: SlotClock + Clone + 'static, - F: ForkChoice + Clone + 'static, -{ +) -> Server { let log = log.new(o!("Service"=>"RPC")); let env = Arc::new(Environment::new(1)); From a4cfe682726a035b8f4dce4d395074f98205e8f6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 16:46:52 +1100 Subject: [PATCH 09/61] Thread beacon node RPC server --- beacon_node/client/src/lib.rs | 23 +++++++++++----------- beacon_node/rpc/Cargo.toml | 2 ++ beacon_node/rpc/src/lib.rs | 35 +++++++++++++++++++++++++--------- validator_client/src/config.rs | 2 +- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index d8da18caef..a033da87b6 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -24,12 +24,8 @@ pub struct Client { beacon_chain: Arc>, /// Reference to the network service. pub network: Arc, - /// Future to stop and begin shutdown of the Client. - //TODO: Decide best way to handle shutdown - pub exit: exit_future::Exit, - /// The sending future to call to terminate the Client. - //TODO: Decide best way to handle shutdown - pub exit_signal: Signal, + /// Signal to terminate the RPC server. + pub rpc_exit_signal: Option, /// The clients logger. log: slog::Logger, /// Marker to pin the beacon chain generics. @@ -43,8 +39,6 @@ impl Client { log: slog::Logger, executor: &TaskExecutor, ) -> error::Result { - let (exit_signal, exit) = exit_future::signal(); - // generate a beacon chain let beacon_chain = TClientType::initialise_beacon_chain(&config); @@ -59,16 +53,23 @@ impl Client { network_logger, )?; + let mut rpc_exit_signal = None; // spawn the RPC server if config.rpc_conf.enabled { - rpc::start_server(&config.rpc_conf, beacon_chain.clone(), &log); + rpc_exit_signal = Some(rpc::start_server( + &config.rpc_conf, + executor, + beacon_chain.clone(), + &log, + )); } + println!("Here"); + Ok(Client { config, beacon_chain, - exit, - exit_signal, + rpc_exit_signal, log, network, phantom: PhantomData, diff --git a/beacon_node/rpc/Cargo.toml b/beacon_node/rpc/Cargo.toml index acb68972c4..d405982db1 100644 --- a/beacon_node/rpc/Cargo.toml +++ b/beacon_node/rpc/Cargo.toml @@ -21,3 +21,5 @@ futures = "0.1.23" slog = "^2.2.3" slog-term = "^2.4.0" slog-async = "^2.3.0" +tokio = "0.1.17" +exit-future = "0.1.4" diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 4565abf7ae..02e34781c3 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -9,24 +9,28 @@ use self::beacon_chain::BeaconChain; use self::beacon_node::BeaconNodeServiceInstance; use self::validator::ValidatorServiceInstance; pub use config::Config as RPCConfig; +use futures::{future, Future}; use grpcio::{Environment, Server, ServerBuilder}; use protos::services_grpc::{ create_beacon_block_service, create_beacon_node_service, create_validator_service, }; +use slog::{info, o, warn}; use std::sync::Arc; - -use slog::{info, o}; +use tokio::runtime::TaskExecutor; pub fn start_server( config: &RPCConfig, + executor: &TaskExecutor, beacon_chain: Arc, log: &slog::Logger, -) -> Server { +) -> exit_future::Signal { let log = log.new(o!("Service"=>"RPC")); let env = Arc::new(Environment::new(1)); - // build the individual rpc services + // build a channel to kill the rpc server + let (rpc_exit_signal, rpc_exit) = exit_future::signal(); + // build the individual rpc services let beacon_node_service = { let instance = BeaconNodeServiceInstance { chain: beacon_chain.clone(), @@ -50,9 +54,22 @@ pub fn start_server( .bind(config.listen_address.to_string(), config.port) .build() .unwrap(); - server.start(); - for &(ref host, port) in server.bind_addrs() { - info!(log, "gRPC listening on {}:{}", host, port); - } - server + + let spawn_rpc = { + server.start(); + for &(ref host, port) in server.bind_addrs() { + info!(log, "gRPC listening on {}:{}", host, port); + } + rpc_exit.and_then(move |_| { + info!(log, "RPC Server shutting down"); + server + .shutdown() + .wait() + .map(|_| ()) + .map_err(|e| warn!(log, "RPC server failed to shutdown: {:?}", e))?; + Ok(()) + }) + }; + executor.spawn(spawn_rpc); + rpc_exit_signal } diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 68405ed2f5..49e0a506f8 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -21,7 +21,7 @@ impl ClientConfig { }; fs::create_dir_all(&data_dir) .unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir)); - let server = "localhost:50051".to_string(); + let server = "localhost:5051".to_string(); let spec = ChainSpec::foundation(); Self { data_dir, From 4990569f68f8940cf0cff907716094e18999839b Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 16:48:25 +1100 Subject: [PATCH 10/61] Add BeaconNodeInfo RPC to validator client --- validator_client/src/main.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 4664d5dc93..b4da737c81 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,15 +1,17 @@ -use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; -use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; use crate::attester_service::{AttestationGrpcClient, AttesterService}; +use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use crate::config::ClientConfig; +use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; use attester::test_utils::EpochMap; use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; use bls::Keypair; use clap::{App, Arg}; use grpcio::{ChannelBuilder, EnvBuilder}; +use protos::services::{Empty, NodeInfo}; use protos::services_grpc::{ - AttestationServiceClient, BeaconBlockServiceClient, ValidatorServiceClient, + AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, + ValidatorServiceClient, }; use slog::{error, info, o, Drain}; use slot_clock::SystemTimeSlotClock; @@ -94,6 +96,13 @@ fn main() { "data_dir" => &config.data_dir.to_str(), "server" => &config.server); + // Beacon node gRPC beacon node endpoints. + let beacon_node_grpc_client = { + let env = Arc::new(EnvBuilder::new().build()); + let ch = ChannelBuilder::new(env).connect(&config.server); + Arc::new(BeaconNodeServiceClient::new(ch)) + }; + // Beacon node gRPC beacon block endpoints. let beacon_block_grpc_client = { let env = Arc::new(EnvBuilder::new().build()); @@ -115,12 +124,14 @@ fn main() { Arc::new(AttestationServiceClient::new(ch)) }; + // retrieve node information + let node_info = beacon_node_grpc_client.info(&Empty::new()); + + info!(log, "Beacon node info: {:?}", node_info); + // Spec let spec = Arc::new(config.spec.clone()); - // Clock for determining the present slot. - // TODO: this shouldn't be a static time, instead it should be pulled from the beacon node. - // https://github.com/sigp/lighthouse/issues/160 let genesis_time = 1_549_935_547; let slot_clock = { info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); From a1a5f29caae53c76d9e17b6e333f4d57ba598a38 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 16:56:54 +1100 Subject: [PATCH 11/61] Fix registering of node service --- beacon_node/rpc/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 02e34781c3..3c89bda1f6 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -51,6 +51,7 @@ pub fn start_server( let mut server = ServerBuilder::new(env) .register_service(beacon_block_service) .register_service(validator_service) + .register_service(beacon_node_service) .bind(config.listen_address.to_string(), config.port) .build() .unwrap(); From b2cd771a4203b07c3c19a3c914ed1016bab3edb6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 17:04:55 +1100 Subject: [PATCH 12/61] Shift argument passing into config module --- validator_client/src/config.rs | 37 ++++++++++++++++++++++++++++++++++ validator_client/src/main.rs | 37 ++-------------------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 49e0a506f8..60edc564ad 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -1,3 +1,5 @@ +use clap::ArgMatches; +use slog::{error, info}; use std::fs; use std::path::PathBuf; use types::ChainSpec; @@ -29,4 +31,39 @@ impl ClientConfig { spec, } } + + pub fn parse_args(matches: ArgMatches, log: &slog::Logger) -> Result { + 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::() { + config.server = addr.to_string(); + } else { + error!(log, "Invalid address"; "server" => server_str); + return Err("Invalid address"); + } + } + + // TODO: Permit loading a custom spec from file. + // Custom spec + if let Some(spec_str) = matches.value_of("spec") { + match spec_str { + "foundation" => config.spec = ChainSpec::foundation(), + "few_validators" => config.spec = ChainSpec::few_validators(), + // Should be impossible due to clap's `possible_values(..)` function. + _ => unreachable!(), + }; + } + + // Log configuration + info!(log, ""; + "data_dir" => &config.data_dir.to_str(), + "server" => &config.server); + Ok(config) + } } diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index b4da737c81..c6cf586f3c 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -13,12 +13,10 @@ use protos::services_grpc::{ AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, ValidatorServiceClient, }; -use slog::{error, info, o, Drain}; +use slog::{info, o, Drain}; use slot_clock::SystemTimeSlotClock; -use std::path::PathBuf; use std::sync::Arc; use std::thread; -use types::ChainSpec; mod attester_service; mod block_producer_service; @@ -63,38 +61,7 @@ fn main() { ) .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::() { - config.server = addr.to_string(); - } else { - error!(log, "Invalid address"; "server" => server_str); - return; - } - } - - // TODO: Permit loading a custom spec from file. - // Custom spec - if let Some(spec_str) = matches.value_of("spec") { - match spec_str { - "foundation" => config.spec = ChainSpec::foundation(), - "few_validators" => config.spec = ChainSpec::few_validators(), - // Should be impossible due to clap's `possible_values(..)` function. - _ => unreachable!(), - }; - } - - // Log configuration - info!(log, ""; - "data_dir" => &config.data_dir.to_str(), - "server" => &config.server); + let config = ClientConfig::parse_args(matches, &log).unwrap(); // Beacon node gRPC beacon node endpoints. let beacon_node_grpc_client = { From c4454289d649e2be37b5e17931d706fe6aecfed2 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 17:27:07 +1100 Subject: [PATCH 13/61] Initial implementation of validator service --- validator_client/Cargo.toml | 2 +- validator_client/src/config.rs | 6 +- validator_client/src/main.rs | 166 ++------------------------------ validator_client/src/service.rs | 166 ++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 161 deletions(-) create mode 100644 validator_client/src/service.rs diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index cdde71774f..4bd63715c2 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "validator_client" version = "0.1.0" -authors = ["Paul Hauner "] +authors = ["Paul Hauner ", "Age Manning "] edition = "2018" [dependencies] diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 60edc564ad..0bf320b4f9 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -6,7 +6,7 @@ use types::ChainSpec; /// Stores the core configuration for this validator instance. #[derive(Clone)] -pub struct ClientConfig { +pub struct Config { pub data_dir: PathBuf, pub server: String, pub spec: ChainSpec, @@ -14,7 +14,7 @@ pub struct ClientConfig { const DEFAULT_LIGHTHOUSE_DIR: &str = ".lighthouse-validators"; -impl ClientConfig { +impl Config { /// Build a new configuration from defaults. pub fn default() -> Self { let data_dir = { @@ -33,7 +33,7 @@ impl ClientConfig { } pub fn parse_args(matches: ArgMatches, log: &slog::Logger) -> Result { - let mut config = ClientConfig::default(); + let mut config = Config::default(); // Custom datadir if let Some(dir) = matches.value_of("datadir") { config.data_dir = PathBuf::from(dir.to_string()); diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index c6cf586f3c..0ec392731b 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,27 +1,13 @@ -use crate::attester_service::{AttestationGrpcClient, AttesterService}; -use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; -use crate::config::ClientConfig; -use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; -use attester::test_utils::EpochMap; -use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; -use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; -use bls::Keypair; -use clap::{App, Arg}; -use grpcio::{ChannelBuilder, EnvBuilder}; -use protos::services::{Empty, NodeInfo}; -use protos::services_grpc::{ - AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, - ValidatorServiceClient, -}; -use slog::{info, o, Drain}; -use slot_clock::SystemTimeSlotClock; -use std::sync::Arc; -use std::thread; - mod attester_service; mod block_producer_service; mod config; mod duties; +mod service; + +use crate::config::Config as ValidatorConfig; +use clap::{App, Arg}; +use service::Service as ValidatorService; +use slog::{o, Drain}; fn main() { // Logging @@ -61,142 +47,8 @@ fn main() { ) .get_matches(); - let config = ClientConfig::parse_args(matches, &log).unwrap(); + let config = ValidatorConfig::parse_args(matches, &log).unwrap(); - // Beacon node gRPC beacon node endpoints. - let beacon_node_grpc_client = { - let env = Arc::new(EnvBuilder::new().build()); - let ch = ChannelBuilder::new(env).connect(&config.server); - Arc::new(BeaconNodeServiceClient::new(ch)) - }; - - // Beacon node gRPC beacon block endpoints. - let beacon_block_grpc_client = { - let env = Arc::new(EnvBuilder::new().build()); - let ch = ChannelBuilder::new(env).connect(&config.server); - Arc::new(BeaconBlockServiceClient::new(ch)) - }; - - // Beacon node gRPC validator endpoints. - let validator_grpc_client = { - let env = Arc::new(EnvBuilder::new().build()); - let ch = ChannelBuilder::new(env).connect(&config.server); - Arc::new(ValidatorServiceClient::new(ch)) - }; - - //Beacon node gRPC attester endpoints. - let attester_grpc_client = { - let env = Arc::new(EnvBuilder::new().build()); - let ch = ChannelBuilder::new(env).connect(&config.server); - Arc::new(AttestationServiceClient::new(ch)) - }; - - // retrieve node information - let node_info = beacon_node_grpc_client.info(&Empty::new()); - - info!(log, "Beacon node info: {:?}", node_info); - - // Spec - let spec = Arc::new(config.spec.clone()); - - let genesis_time = 1_549_935_547; - let slot_clock = { - info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); - let clock = SystemTimeSlotClock::new(genesis_time, spec.seconds_per_slot) - .expect("Unable to instantiate SystemTimeSlotClock."); - Arc::new(clock) - }; - - let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision. - info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis); - - /* - * Start threads. - */ - let mut threads = vec![]; - // TODO: keypairs are randomly generated; they should be loaded from a file or generated. - // https://github.com/sigp/lighthouse/issues/160 - let keypairs = vec![Keypair::random()]; - - for keypair in keypairs { - info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); - let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); - let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); - - // 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 = slot_clock.clone(); - let log = log.clone(); - let beacon_node = validator_grpc_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(); - let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone())); - let duties_map = duties_map.clone(); - let slot_clock = slot_clock.clone(); - let log = log.clone(); - let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone())); - thread::spawn(move || { - let block_producer = - BlockProducer::new(spec, duties_map, slot_clock, client, signer); - let mut block_producer_service = BlockProducerService { - block_producer, - poll_interval_millis, - log, - }; - - block_producer_service.run(); - }) - }; - - // Spawn a new thread for attestation for the validator. - let attester_thread = { - let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); - let epoch_map = epoch_map_for_attester.clone(); - let slot_clock = slot_clock.clone(); - let log = log.clone(); - let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone())); - thread::spawn(move || { - let attester = Attester::new(epoch_map, slot_clock, client, signer); - let mut attester_service = AttesterService { - attester, - poll_interval_millis, - log, - }; - - attester_service.run(); - }) - }; - - threads.push((duties_manager_thread, producer_thread, attester_thread)); - } - - // Naively wait for all the threads to complete. - for tuple in threads { - let (manager, producer, attester) = tuple; - let _ = producer.join(); - let _ = manager.join(); - let _ = attester.join(); - } + // start the validator service. + ValidatorService::start(config, log); } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs new file mode 100644 index 0000000000..95f2a23614 --- /dev/null +++ b/validator_client/src/service.rs @@ -0,0 +1,166 @@ +/// The validator service. Connects to a beacon node and signs blocks when required. +use crate::attester_service::{AttestationGrpcClient, AttesterService}; +use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; +use crate::config::Config as ValidatorConfig; +use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; +use attester::test_utils::EpochMap; +use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; +use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; +use bls::Keypair; +use grpcio::{ChannelBuilder, EnvBuilder}; +use protos::services::{Empty, NodeInfo}; +use protos::services_grpc::{ + AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, + ValidatorServiceClient, +}; +use slog::{info, o, Drain}; +use slot_clock::SystemTimeSlotClock; +use std::sync::Arc; +use std::thread; + +/// The validator service. This is the main thread that executes and maintains validator +/// duties. +pub struct Service {} + +impl Service { + pub fn start(config: ValidatorConfig, log: slog::Logger) { + // initialize the RPC clients + + let env = Arc::new(EnvBuilder::new().build()); + // Beacon node gRPC beacon node endpoints. + let beacon_node_grpc_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(BeaconNodeServiceClient::new(ch)) + }; + + // Beacon node gRPC beacon block endpoints. + let beacon_block_grpc_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(BeaconBlockServiceClient::new(ch)) + }; + + // Beacon node gRPC validator endpoints. + let validator_grpc_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(ValidatorServiceClient::new(ch)) + }; + + //Beacon node gRPC attester endpoints. + let attester_grpc_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(AttestationServiceClient::new(ch)) + }; + + // connect to the node and retrieve its properties + // node_info = connect_to_node(beacon_ndoe_grpc_client); + + // retrieve node information + let node_info = beacon_node_grpc_client.info(&Empty::new()); + + info!(log, "Beacon node info: {:?}", node_info); + + // Spec + let spec = Arc::new(config.spec.clone()); + + let genesis_time = 1_549_935_547; + let slot_clock = { + info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); + let clock = SystemTimeSlotClock::new(genesis_time, spec.seconds_per_slot) + .expect("Unable to instantiate SystemTimeSlotClock."); + Arc::new(clock) + }; + + let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision. + info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis); + + /* + * Start threads. + */ + let mut threads = vec![]; + // TODO: keypairs are randomly generated; they should be loaded from a file or generated. + // https://github.com/sigp/lighthouse/issues/160 + let keypairs = vec![Keypair::random()]; + + for keypair in keypairs { + info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); + let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); + let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); + + // 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 = slot_clock.clone(); + let log = log.clone(); + let beacon_node = validator_grpc_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(); + let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone())); + let duties_map = duties_map.clone(); + let slot_clock = slot_clock.clone(); + let log = log.clone(); + let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone())); + thread::spawn(move || { + let block_producer = + BlockProducer::new(spec, duties_map, slot_clock, client, signer); + let mut block_producer_service = BlockProducerService { + block_producer, + poll_interval_millis, + log, + }; + + block_producer_service.run(); + }) + }; + + // Spawn a new thread for attestation for the validator. + let attester_thread = { + let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); + let epoch_map = epoch_map_for_attester.clone(); + let slot_clock = slot_clock.clone(); + let log = log.clone(); + let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone())); + thread::spawn(move || { + let attester = Attester::new(epoch_map, slot_clock, client, signer); + let mut attester_service = AttesterService { + attester, + poll_interval_millis, + log, + }; + + attester_service.run(); + }) + }; + + threads.push((duties_manager_thread, producer_thread, attester_thread)); + } + + // Naively wait for all the threads to complete. + for tuple in threads { + let (manager, producer, attester) = tuple; + let _ = producer.join(); + let _ = manager.join(); + let _ = attester.join(); + } + } +} From 61fc946d54a0de8b937152bf2d799c428ed7b29a Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 22:50:16 +1100 Subject: [PATCH 14/61] Adds initial connection to beacon node with retries --- validator_client/src/service.rs | 79 +++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 95f2a23614..3bf2d61042 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -13,16 +13,75 @@ use protos::services_grpc::{ AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, ValidatorServiceClient, }; -use slog::{info, o, Drain}; +use slog::{info, o, warn, Drain}; use slot_clock::SystemTimeSlotClock; use std::sync::Arc; use std::thread; +use std::time::Duration; +use types::{Epoch, Fork}; /// The validator service. This is the main thread that executes and maintains validator /// duties. -pub struct Service {} +#[derive(Debug)] +pub struct Service { + /// The node we currently connected to. + connected_node_version: String, + /// The chain id we are processing on. + chain_id: u16, + /// The fork state we processing on. + fork: Fork, + // /// The slot clock keeping track of time. + // slot_clock: Arc, +} impl Service { + /// Initial connection to the beacon node to determine its properties. + fn connect_to_node( + node_client: Arc, + seconds_per_slot: u64, + log: &slog::Logger, + ) -> Self { + // retrieve node information + let node_info = loop { + let info = match node_client.info(&Empty::new()) { + Err(e) => { + warn!(log, "Could not connect to node. Error: {}", e); + info!(log, "Retrying in 5 seconds..."); + std::thread::sleep(Duration::from_secs(5)); + continue; + } + Ok(info) => break info, + }; + }; + + info!(log,"Beacon node connected"; "Node Version:" => node_info.version.clone(), "Chain ID:" => node_info.chain_id); + + let proto_fork = node_info.get_fork(); + let mut previous_version: [u8; 4] = [0; 4]; + let mut current_version: [u8; 4] = [0; 4]; + previous_version.copy_from_slice(&proto_fork.get_previous_version()[..4]); + current_version.copy_from_slice(&proto_fork.get_current_version()[..4]); + let fork = Fork { + previous_version, + current_version, + epoch: Epoch::from(proto_fork.get_epoch()), + }; + + let genesis_time = 1_549_935_547; + let slot_clock = { + info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); + let clock = SystemTimeSlotClock::new(genesis_time, seconds_per_slot) + .expect("Unable to instantiate SystemTimeSlotClock."); + Arc::new(clock) + }; + + Self { + connected_node_version: node_info.version, + chain_id: node_info.chain_id as u16, + fork, + } + } + pub fn start(config: ValidatorConfig, log: slog::Logger) { // initialize the RPC clients @@ -51,16 +110,13 @@ impl Service { Arc::new(AttestationServiceClient::new(ch)) }; + let spec = Arc::new(config.spec); // connect to the node and retrieve its properties - // node_info = connect_to_node(beacon_ndoe_grpc_client); + let service = + Service::connect_to_node(beacon_node_grpc_client, spec.seconds_per_slot, &log); - // retrieve node information - let node_info = beacon_node_grpc_client.info(&Empty::new()); - - info!(log, "Beacon node info: {:?}", node_info); - - // Spec - let spec = Arc::new(config.spec.clone()); + let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision. + info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis); let genesis_time = 1_549_935_547; let slot_clock = { @@ -70,9 +126,6 @@ impl Service { Arc::new(clock) }; - let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision. - info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis); - /* * Start threads. */ From 17cd5bb991985ab9f3bebfd6e8614872f34d27de Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 23:01:10 +1100 Subject: [PATCH 15/61] Adds genesis time to node info. Closes #256 --- beacon_node/rpc/src/beacon_node.rs | 14 ++++++++++---- protos/src/services.proto | 2 +- validator_client/src/service.rs | 14 ++++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 5a542cb905..7b00f04f48 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -3,7 +3,7 @@ use futures::Future; use grpcio::{RpcContext, UnarySink}; use protos::services::{Empty, Fork, NodeInfo}; use protos::services_grpc::BeaconNodeService; -use slog::{debug, trace, warn}; +use slog::{trace, warn}; use std::sync::Arc; #[derive(Clone)] @@ -17,17 +17,23 @@ impl BeaconNodeService for BeaconNodeServiceInstance { fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { trace!(self.log, "Node info requested via RPC"); + // build the response let mut node_info = NodeInfo::new(); node_info.set_version(version::version()); - // get the chain state fork - let state_fork = self.chain.get_state().fork.clone(); + + // get the chain state + let state = self.chain.get_state(); + let state_fork = state.fork.clone(); + let genesis_time = state.genesis_time.clone(); + // build the rpc fork struct let mut fork = Fork::new(); fork.set_previous_version(state_fork.previous_version.to_vec()); fork.set_current_version(state_fork.current_version.to_vec()); fork.set_epoch(state_fork.epoch.into()); - node_info.set_fork(fork); + node_info.set_fork(fork); + node_info.set_genesis_time(genesis_time); node_info.set_chain_id(self.chain.get_spec().chain_id as u32); // send the node_info the requester diff --git a/protos/src/services.proto b/protos/src/services.proto index 45fcfb1203..fbcde922dd 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -44,6 +44,7 @@ message NodeInfo { string version = 1; Fork fork = 2; uint32 chain_id = 3; + uint64 genesis_time = 4; } message Fork { @@ -56,7 +57,6 @@ message Empty { } - /* * Block Production Service Messages */ diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 3bf2d61042..df3f640270 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -22,7 +22,6 @@ use types::{Epoch, Fork}; /// The validator service. This is the main thread that executes and maintains validator /// duties. -#[derive(Debug)] pub struct Service { /// The node we currently connected to. connected_node_version: String, @@ -30,8 +29,8 @@ pub struct Service { chain_id: u16, /// The fork state we processing on. fork: Fork, - // /// The slot clock keeping track of time. - // slot_clock: Arc, + /// The slot clock keeping track of time. + slot_clock: Arc, } impl Service { @@ -54,7 +53,10 @@ impl Service { }; }; - info!(log,"Beacon node connected"; "Node Version:" => node_info.version.clone(), "Chain ID:" => node_info.chain_id); + // build requisite objects to form Self + let genesis_time = node_info.get_genesis_time(); + + info!(log,"Beacon node connected"; "Node Version" => node_info.version.clone(), "Chain ID" => node_info.chain_id, "Genesis time" => genesis_time); let proto_fork = node_info.get_fork(); let mut previous_version: [u8; 4] = [0; 4]; @@ -67,9 +69,8 @@ impl Service { epoch: Epoch::from(proto_fork.get_epoch()), }; - let genesis_time = 1_549_935_547; + // build the validator slot clock let slot_clock = { - info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); let clock = SystemTimeSlotClock::new(genesis_time, seconds_per_slot) .expect("Unable to instantiate SystemTimeSlotClock."); Arc::new(clock) @@ -79,6 +80,7 @@ impl Service { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, + slot_clock, } } From 547a750d78443eea0745b9661980ba3b7950a4f2 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 23:21:26 +1100 Subject: [PATCH 16/61] Setup basic structure before tokio runtime addition --- validator_client/src/service.rs | 119 +++++++++++++++++--------------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index df3f640270..bf30038343 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -31,18 +31,44 @@ pub struct Service { fork: Fork, /// The slot clock keeping track of time. slot_clock: Arc, + // GRPC Clients + /// The beacon block GRPC client. + beacon_block_client: Arc, + /// The validator GRPC client. + validator_client: Arc, + /// The attester GRPC client. + attester_client: Arc, + /// The validator client logger. + log: slog::Logger, } impl Service { + /// Initialise the service then run the core thread. + pub fn start(config: ValidatorConfig, log: slog::Logger) { + // connect to the node and retrieve its properties and initialize the gRPC clients + let service = Service::initialize_service(&config, log); + + // we have connected to a node and established its parameters. Spin up the core service + service.run(config); + } + /// Initial connection to the beacon node to determine its properties. - fn connect_to_node( - node_client: Arc, - seconds_per_slot: u64, - log: &slog::Logger, - ) -> Self { + /// + /// This tries to connect to a beacon node. Once connected, it initialised the gRPC clients + /// and returns an instance of the service. + fn initialize_service(config: &ValidatorConfig, log: slog::Logger) -> Self { + // initialise the beacon node client to check for a connection + + let env = Arc::new(EnvBuilder::new().build()); + // Beacon node gRPC beacon node endpoints. + let beacon_node_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(BeaconNodeServiceClient::new(ch)) + }; + // retrieve node information let node_info = loop { - let info = match node_client.info(&Empty::new()) { + let info = match beacon_node_client.info(&Empty::new()) { Err(e) => { warn!(log, "Could not connect to node. Error: {}", e); info!(log, "Retrying in 5 seconds..."); @@ -71,63 +97,44 @@ impl Service { // build the validator slot clock let slot_clock = { - let clock = SystemTimeSlotClock::new(genesis_time, seconds_per_slot) + let clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot) .expect("Unable to instantiate SystemTimeSlotClock."); Arc::new(clock) }; + // initialize the RPC clients + + // Beacon node gRPC beacon block endpoints. + let beacon_block_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(BeaconBlockServiceClient::new(ch)) + }; + + // Beacon node gRPC validator endpoints. + let validator_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(ValidatorServiceClient::new(ch)) + }; + + //Beacon node gRPC attester endpoints. + let attester_client = { + let ch = ChannelBuilder::new(env.clone()).connect(&config.server); + Arc::new(AttestationServiceClient::new(ch)) + }; + Self { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, slot_clock, + beacon_block_client, + validator_client, + attester_client, + log, } } - pub fn start(config: ValidatorConfig, log: slog::Logger) { - // initialize the RPC clients - - let env = Arc::new(EnvBuilder::new().build()); - // Beacon node gRPC beacon node endpoints. - let beacon_node_grpc_client = { - let ch = ChannelBuilder::new(env.clone()).connect(&config.server); - Arc::new(BeaconNodeServiceClient::new(ch)) - }; - - // Beacon node gRPC beacon block endpoints. - let beacon_block_grpc_client = { - let ch = ChannelBuilder::new(env.clone()).connect(&config.server); - Arc::new(BeaconBlockServiceClient::new(ch)) - }; - - // Beacon node gRPC validator endpoints. - let validator_grpc_client = { - let ch = ChannelBuilder::new(env.clone()).connect(&config.server); - Arc::new(ValidatorServiceClient::new(ch)) - }; - - //Beacon node gRPC attester endpoints. - let attester_grpc_client = { - let ch = ChannelBuilder::new(env.clone()).connect(&config.server); - Arc::new(AttestationServiceClient::new(ch)) - }; - - let spec = Arc::new(config.spec); - // connect to the node and retrieve its properties - let service = - Service::connect_to_node(beacon_node_grpc_client, spec.seconds_per_slot, &log); - - let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision. - info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis); - - let genesis_time = 1_549_935_547; - let slot_clock = { - info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); - let clock = SystemTimeSlotClock::new(genesis_time, spec.seconds_per_slot) - .expect("Unable to instantiate SystemTimeSlotClock."); - Arc::new(clock) - }; - + fn run(&mut self, config: ValidatorConfig) { /* * Start threads. */ @@ -136,8 +143,10 @@ impl Service { // https://github.com/sigp/lighthouse/issues/160 let keypairs = vec![Keypair::random()]; + let spec = config.spec; + for keypair in keypairs { - info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); + info!(self.log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); @@ -145,9 +154,9 @@ impl Service { let duties_manager_thread = { let spec = spec.clone(); let duties_map = duties_map.clone(); - let slot_clock = slot_clock.clone(); - let log = log.clone(); - let beacon_node = validator_grpc_client.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 { From 318d6a976e1d2e0100cdd72098e2311d313f07cf Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 23 Mar 2019 00:36:48 +1100 Subject: [PATCH 17/61] Initial tokio timer interval --- validator_client/src/service.rs | 50 +++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index bf30038343..0934a5a160 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -31,6 +31,8 @@ pub struct Service { fork: Fork, /// The slot clock keeping track of time. slot_clock: Arc, + /// The current slot we are processing. + current_slot: Slot, // GRPC Clients /// The beacon block GRPC client. beacon_block_client: Arc, @@ -122,11 +124,14 @@ impl Service { Arc::new(AttestationServiceClient::new(ch)) }; + let current_slot = slot_clock.present_slot().saturating_sub(1); + Self { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, slot_clock, + current_slot, beacon_block_client, validator_client, attester_client, @@ -135,15 +140,50 @@ impl Service { } fn run(&mut self, config: ValidatorConfig) { - /* - * Start threads. - */ - let mut threads = vec![]; + + // generate keypairs + // TODO: keypairs are randomly generated; they should be loaded from a file or generated. // https://github.com/sigp/lighthouse/issues/160 let keypairs = vec![Keypair::random()]; - let spec = config.spec; + // set up the validator service runtime + let runtime = Builder::new().clock(Clock::system()).name_prefix("validator-client-").build().unwrap(); + + // set up the validator work interval - start at next slot and proceed every slot + let interval = { + let time_to_next_slot = { + let syslot_time = SystemTime::now(); + let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH)?; + let mut secs_to_slot = None; + if let Some(duration_since_genesis) = + duration_since_epoch.checked_sub(Duration::from_secs(self.genesis_seconds)) { + // seconds till next slot + secs_to_slot =duration_since_genesis.as_secs().checked_rem(config.spec.seconds_per_slot); + } + secs_to_slot.ok_or_else(0) + } + // 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().checked_add(secs_to_slot)?, slot_duration) + } + + // kick off core service + runtime.spawn(interval.for_each(|_| {})); + + + + let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); + let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); + let manager = DutiesManager { + duties_map, + pubkey, + spec, + slot_clock, + beacon_node, + }; + for keypair in keypairs { info!(self.log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); From 56d33d2e26866e6ebdd02835bb75b5bb3c5a2945 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 23 Mar 2019 11:48:36 +1100 Subject: [PATCH 18/61] Basic tokio slot stream implementation --- validator_client/Cargo.toml | 4 +- validator_client/src/duties/mod.rs | 13 +- validator_client/src/service.rs | 291 ++++++++++++++++------------- 3 files changed, 170 insertions(+), 138 deletions(-) diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index 4bd63715c2..e8cff2622e 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" block_proposer = { path = "../eth2/block_proposer" } attester = { path = "../eth2/attester" } bls = { path = "../eth2/utils/bls" } +ssz = { path = "../eth2/utils/ssz" } clap = "2.32.0" dirs = "1.0.3" grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] } @@ -18,4 +19,5 @@ types = { path = "../eth2/types" } slog = "^2.2.3" slog-term = "^2.4.0" slog-async = "^2.3.0" -ssz = { path = "../eth2/utils/ssz" } +tokio = "0.1.18" +tokio-timer = "0.2.10" diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 29bd81d0aa..c2b95b1c53 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -39,11 +39,11 @@ pub enum Error { /// A polling state machine which ensures the latest `EpochDuties` are obtained from the Beacon /// Node. /// -/// There is a single `DutiesManager` per validator instance. +/// This keeps track of all validator keys and required voting slots. pub struct DutiesManager { pub duties_map: Arc, - /// The validator's public key. - pub pubkey: PublicKey, + /// A list of all public keys known to the validator service. + pub pubkeys: Vec, pub spec: Arc, pub slot_clock: Arc, pub beacon_node: Arc, @@ -54,6 +54,8 @@ impl DutiesManager { /// /// The present `epoch` will be learned from the supplied `SlotClock`. In production this will /// be a wall-clock (e.g., system time, remote server time, etc.). + //TODO: Remove the poll and trust the tokio system-clock timer. Leave for now to ensure the + //timer is accurate. pub fn poll(&self) -> Result { let slot = self .slot_clock @@ -63,7 +65,10 @@ impl DutiesManager { let epoch = slot.epoch(self.spec.slots_per_epoch); - if let Some(duties) = self.beacon_node.request_shuffling(epoch, &self.pubkey)? { + if let Some(duties) = self + .beacon_node + .request_shuffling(epoch, &self.pubkeys[0])? + { // If these duties were known, check to see if they're updates or identical. let result = if let Some(known_duties) = self.duties_map.get(epoch)? { if known_duties == duties { diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 0934a5a160..322c370b5e 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -8,17 +8,23 @@ use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; use bls::Keypair; use grpcio::{ChannelBuilder, EnvBuilder}; -use protos::services::{Empty, NodeInfo}; +use protos::services::Empty; use protos::services_grpc::{ AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, ValidatorServiceClient, }; -use slog::{info, o, warn, Drain}; -use slot_clock::SystemTimeSlotClock; +use slog::{debug, info, warn}; +use slot_clock::{SlotClock, SystemTimeSlotClock}; +use std::ops::Sub; use std::sync::Arc; -use std::thread; -use std::time::Duration; -use types::{Epoch, Fork}; +use std::time::{Duration, Instant, SystemTime}; +use tokio::prelude::*; +use tokio::runtime::Builder; +use tokio::timer::Interval; +use tokio_timer::clock::Clock; +use types::{Epoch, Fork, Slot}; + +//TODO: This service should be simplified in the future. Can be made more steamlined. /// The validator service. This is the main thread that executes and maintains validator /// duties. @@ -33,6 +39,8 @@ pub struct Service { slot_clock: Arc, /// The current slot we are processing. current_slot: Slot, + /// Seconds until the next slot. This is used for initializing the tokio timer interval. + seconds_to_next_slot: Duration, // GRPC Clients /// The beacon block GRPC client. beacon_block_client: Arc, @@ -45,15 +53,6 @@ pub struct Service { } impl Service { - /// Initialise the service then run the core thread. - pub fn start(config: ValidatorConfig, log: slog::Logger) { - // connect to the node and retrieve its properties and initialize the gRPC clients - let service = Service::initialize_service(&config, log); - - // we have connected to a node and established its parameters. Spin up the core service - service.run(config); - } - /// Initial connection to the beacon node to determine its properties. /// /// This tries to connect to a beacon node. Once connected, it initialised the gRPC clients @@ -124,7 +123,24 @@ impl Service { Arc::new(AttestationServiceClient::new(ch)) }; - let current_slot = slot_clock.present_slot().saturating_sub(1); + //TODO: Add error chain. Handle errors + let current_slot = slot_clock.present_slot().unwrap().unwrap().sub(1); + + // calculate seconds to the next slot + let seconds_to_next_slot = { + let syslot_time = SystemTime::now(); + let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH).unwrap(); + let mut secs_to_slot = None; + if let Some(duration_since_genesis) = + duration_since_epoch.checked_sub(Duration::from_secs(genesis_time)) + { + // seconds till next slot + secs_to_slot = duration_since_genesis + .as_secs() + .checked_rem(config.spec.seconds_per_slot); + } + secs_to_slot.unwrap_or_else(|| 0) + }; Self { connected_node_version: node_info.version, @@ -132,6 +148,7 @@ impl Service { fork, slot_clock, current_slot, + seconds_to_next_slot: Duration::from_secs(seconds_to_next_slot), beacon_block_client, validator_client, attester_client, @@ -139,132 +156,140 @@ impl Service { } } - fn run(&mut self, config: ValidatorConfig) { + /// Initialise the service then run the core thread. + pub fn start(config: ValidatorConfig, log: slog::Logger) { + // connect to the node and retrieve its properties and initialize the gRPC clients + let service = Service::initialize_service(&config, log); + + // we have connected to a node and established its parameters. Spin up the core service + + // set up the validator service runtime + let mut runtime = Builder::new() + .clock(Clock::system()) + .name_prefix("validator-client-") + .build() + .unwrap(); + + // set up the validator work interval - start at next slot and proceed every slot + // TODO: Error chain handle errors. + 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.seconds_to_next_slot, slot_duration) + }; + + // kick off core service // generate keypairs // TODO: keypairs are randomly generated; they should be loaded from a file or generated. // https://github.com/sigp/lighthouse/issues/160 - let keypairs = vec![Keypair::random()]; + let keypairs = Arc::new(vec![Keypair::random()]); - // set up the validator service runtime - let runtime = Builder::new().clock(Clock::system()).name_prefix("validator-client-").build().unwrap(); - - // set up the validator work interval - start at next slot and proceed every slot - let interval = { - let time_to_next_slot = { - let syslot_time = SystemTime::now(); - let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH)?; - let mut secs_to_slot = None; - if let Some(duration_since_genesis) = - duration_since_epoch.checked_sub(Duration::from_secs(self.genesis_seconds)) { - // seconds till next slot - secs_to_slot =duration_since_genesis.as_secs().checked_rem(config.spec.seconds_per_slot); - } - secs_to_slot.ok_or_else(0) - } - // 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().checked_add(secs_to_slot)?, slot_duration) - } - - // kick off core service - runtime.spawn(interval.for_each(|_| {})); - - - - let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); - let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); + // build requisite objects to pass to core thread. + let duties_map = Arc::new(EpochDutiesMap::new(config.spec.slots_per_epoch)); + let epoch_map_for_attester = Arc::new(EpochMap::new(config.spec.slots_per_epoch)); let manager = DutiesManager { duties_map, - pubkey, - spec, - slot_clock, - beacon_node, + pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(), + spec: Arc::new(config.spec), + slot_clock: service.slot_clock.clone(), + beacon_node: service.validator_client.clone(), }; - - for keypair in keypairs { - info!(self.log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); - let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); - let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); - - // 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(); - let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone())); - let duties_map = duties_map.clone(); - let slot_clock = slot_clock.clone(); - let log = log.clone(); - let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone())); - thread::spawn(move || { - let block_producer = - BlockProducer::new(spec, duties_map, slot_clock, client, signer); - let mut block_producer_service = BlockProducerService { - block_producer, - poll_interval_millis, - log, - }; - - block_producer_service.run(); - }) - }; - - // Spawn a new thread for attestation for the validator. - let attester_thread = { - let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); - let epoch_map = epoch_map_for_attester.clone(); - let slot_clock = slot_clock.clone(); - let log = log.clone(); - let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone())); - thread::spawn(move || { - let attester = Attester::new(epoch_map, slot_clock, client, signer); - let mut attester_service = AttesterService { - attester, - poll_interval_millis, - log, - }; - - attester_service.run(); - }) - }; - - threads.push((duties_manager_thread, producer_thread, attester_thread)); - } - - // Naively wait for all the threads to complete. - for tuple in threads { - let (manager, producer, attester) = tuple; - let _ = producer.join(); - let _ = manager.join(); - let _ = attester.join(); - } + runtime.block_on(interval.for_each(move |_| { + // update duties + debug!(service.log, "Processing new slot..."); + manager.poll(); + Ok(()) + })); } + + /* + + let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); + let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); + + + 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(); + let signer = Arc::new(BlockProposerLocalSigner::new(keypair.clone())); + let duties_map = duties_map.clone(); + let slot_clock = slot_clock.clone(); + let log = log.clone(); + let client = Arc::new(BeaconBlockGrpcClient::new(beacon_block_grpc_client.clone())); + thread::spawn(move || { + let block_producer = + BlockProducer::new(spec, duties_map, slot_clock, client, signer); + let mut block_producer_service = BlockProducerService { + block_producer, + poll_interval_millis, + log, + }; + + block_producer_service.run(); + }) + }; + + // Spawn a new thread for attestation for the validator. + let attester_thread = { + let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); + let epoch_map = epoch_map_for_attester.clone(); + let slot_clock = slot_clock.clone(); + let log = log.clone(); + let client = Arc::new(AttestationGrpcClient::new(attester_grpc_client.clone())); + thread::spawn(move || { + let attester = Attester::new(epoch_map, slot_clock, client, signer); + let mut attester_service = AttesterService { + attester, + poll_interval_millis, + log, + }; + + attester_service.run(); + }) + }; + + threads.push((duties_manager_thread, producer_thread, attester_thread)); + } + + // Naively wait for all the threads to complete. + for tuple in threads { + let (manager, producer, attester) = tuple; + let _ = producer.join(); + let _ = manager.join(); + let _ = attester.join(); + } + */ } From 18493a4df4e5ab6569214c8af376269ce3db0d6b Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 24 Mar 2019 09:24:50 +1100 Subject: [PATCH 19/61] Adds microsecond duration to validator client --- validator_client/src/service.rs | 40 +++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 322c370b5e..49acd8ad2f 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -39,8 +39,8 @@ pub struct Service { slot_clock: Arc, /// The current slot we are processing. current_slot: Slot, - /// Seconds until the next slot. This is used for initializing the tokio timer interval. - seconds_to_next_slot: Duration, + /// Micro seconds until the next slot. This is used for initializing the tokio timer interval. + micros_to_next_slot: Duration, // GRPC Clients /// The beacon block GRPC client. beacon_block_client: Arc, @@ -82,6 +82,7 @@ impl Service { // build requisite objects to form Self let genesis_time = node_info.get_genesis_time(); + let genesis_time = 1_549_935_547; info!(log,"Beacon node connected"; "Node Version" => node_info.version.clone(), "Chain ID" => node_info.chain_id, "Genesis time" => genesis_time); @@ -127,28 +128,45 @@ impl Service { let current_slot = slot_clock.present_slot().unwrap().unwrap().sub(1); // calculate seconds to the next slot - let seconds_to_next_slot = { + let micros_to_next_slot = { let syslot_time = SystemTime::now(); let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH).unwrap(); - let mut secs_to_slot = None; + debug!(log, "Duration since unix epoch {:?}", duration_since_epoch); + let mut micros_to_slot = None; if let Some(duration_since_genesis) = duration_since_epoch.checked_sub(Duration::from_secs(genesis_time)) { // seconds till next slot - secs_to_slot = duration_since_genesis + debug!(log, "Genesis Time {:?}", genesis_time); + debug!(log, "Duration since genesis {:?}", duration_since_genesis); + micros_to_slot = duration_since_genesis .as_secs() .checked_rem(config.spec.seconds_per_slot); } - secs_to_slot.unwrap_or_else(|| 0) + micros_to_slot.unwrap_or_else(|| 0) + /* + let duration_to_slot = duration_since_genesis + .checked_sub(Duration::from( + duration_since_genesis + .checked_div(config.spec.seconds_per_slot as u64) + .unwrap() + .as_secs() + .checked_mul(config.spec.seconds_per_slot) + .unwrap(), + )) + .unwrap(); + */ }; + info!(log, ""; "Micro Seconds to next slot"=>micros_to_next_slot); + Self { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, slot_clock, current_slot, - seconds_to_next_slot: Duration::from_secs(seconds_to_next_slot), + micros_to_next_slot: Duration::from_micros(micros_to_next_slot), beacon_block_client, validator_client, attester_client, @@ -176,7 +194,7 @@ impl Service { // 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.seconds_to_next_slot, slot_duration) + Interval::new(Instant::now() + service.micros_to_next_slot, slot_duration) }; // kick off core service @@ -200,7 +218,11 @@ impl Service { runtime.block_on(interval.for_each(move |_| { // update duties - debug!(service.log, "Processing new slot..."); + debug!( + service.log, + "Processing slot: {}", + service.slot_clock.present_slot().unwrap().unwrap().as_u64() + ); manager.poll(); Ok(()) })); From 4fdb01e5f06df069aa2d0a1e10237d3135004bc8 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 15:10:26 +1100 Subject: [PATCH 20/61] Correct slot duration interval timer --- validator_client/src/service.rs | 67 ++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 49acd8ad2f..c88db29b85 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -39,8 +39,8 @@ pub struct Service { slot_clock: Arc, /// The current slot we are processing. current_slot: Slot, - /// Micro seconds until the next slot. This is used for initializing the tokio timer interval. - micros_to_next_slot: Duration, + /// Duration until the next slot. This is used for initializing the tokio timer interval. + duration_to_next_slot: Duration, // GRPC Clients /// The beacon block GRPC client. beacon_block_client: Arc, @@ -76,13 +76,25 @@ impl Service { std::thread::sleep(Duration::from_secs(5)); continue; } - Ok(info) => break info, + Ok(info) => { + if SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + > Duration::from_secs(info.genesis_time) + { + warn!( + log, + "Beacon Node's genesis time is in the future. No work to do.\n Exiting" + ); + // return Err("Genesis Time in the future"); + } + break info; + } }; }; // build requisite objects to form Self let genesis_time = node_info.get_genesis_time(); - let genesis_time = 1_549_935_547; info!(log,"Beacon node connected"; "Node Version" => node_info.version.clone(), "Chain ID" => node_info.chain_id, "Genesis time" => genesis_time); @@ -127,46 +139,38 @@ impl Service { //TODO: Add error chain. Handle errors let current_slot = slot_clock.present_slot().unwrap().unwrap().sub(1); - // calculate seconds to the next slot - let micros_to_next_slot = { + // calculate the duration to the next slot + let duration_to_next_slot = { let syslot_time = SystemTime::now(); let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH).unwrap(); - debug!(log, "Duration since unix epoch {:?}", duration_since_epoch); - let mut micros_to_slot = None; + let mut duration_to_next_slot = None; if let Some(duration_since_genesis) = duration_since_epoch.checked_sub(Duration::from_secs(genesis_time)) { - // seconds till next slot - debug!(log, "Genesis Time {:?}", genesis_time); - debug!(log, "Duration since genesis {:?}", duration_since_genesis); - micros_to_slot = duration_since_genesis + let elapsed_slots = duration_since_epoch .as_secs() - .checked_rem(config.spec.seconds_per_slot); + .checked_div(config.spec.seconds_per_slot as u64) + .unwrap(); + duration_to_next_slot = Some( + Duration::from_secs( + (elapsed_slots + 1) + .checked_mul(config.spec.seconds_per_slot) + .unwrap(), + ) + .checked_sub(duration_since_genesis) + .expect("This should never saturate"), + ); } - micros_to_slot.unwrap_or_else(|| 0) - /* - let duration_to_slot = duration_since_genesis - .checked_sub(Duration::from( - duration_since_genesis - .checked_div(config.spec.seconds_per_slot as u64) - .unwrap() - .as_secs() - .checked_mul(config.spec.seconds_per_slot) - .unwrap(), - )) - .unwrap(); - */ + duration_to_next_slot.unwrap_or_else(|| Duration::from_secs(0)) }; - info!(log, ""; "Micro Seconds to next slot"=>micros_to_next_slot); - Self { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, slot_clock, current_slot, - micros_to_next_slot: Duration::from_micros(micros_to_next_slot), + duration_to_next_slot, beacon_block_client, validator_client, attester_client, @@ -194,7 +198,10 @@ impl Service { // 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.micros_to_next_slot, slot_duration) + Interval::new( + Instant::now() + service.duration_to_next_slot, + slot_duration, + ) }; // kick off core service From ca9af49d4eac240abcf3c4b87237a815f20c03df Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 16:50:15 +1100 Subject: [PATCH 21/61] Adds error handling to validator client service --- beacon_node/client/src/lib.rs | 2 - validator_client/Cargo.toml | 1 + validator_client/src/error.rs | 22 ++++++++ validator_client/src/main.rs | 8 ++- validator_client/src/service.rs | 96 ++++++++++++++++++--------------- 5 files changed, 83 insertions(+), 46 deletions(-) create mode 100644 validator_client/src/error.rs diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index a033da87b6..44eab4fe20 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -64,8 +64,6 @@ impl Client { )); } - println!("Here"); - Ok(Client { config, beacon_chain, diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index e8cff2622e..ea97ef5d45 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -21,3 +21,4 @@ slog-term = "^2.4.0" slog-async = "^2.3.0" tokio = "0.1.18" tokio-timer = "0.2.10" +error-chain = "0.12.0" diff --git a/validator_client/src/error.rs b/validator_client/src/error.rs new file mode 100644 index 0000000000..29d7ba8829 --- /dev/null +++ b/validator_client/src/error.rs @@ -0,0 +1,22 @@ +use slot_clock; + +use error_chain::{ + error_chain, error_chain_processing, impl_error_chain_kind, impl_error_chain_processed, + impl_extract_backtrace, +}; + +error_chain! { + links { } + + errors { + SlotClockError(e: slot_clock::SystemTimeSlotClockError) { + description("Error reading system time"), + display("SlotClockError: '{:?}'", e) + } + + SystemTimeError(t: String ) { + description("Error reading system time"), + display("SystemTimeError: '{}'", t) + } + } +} diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 0ec392731b..127df84942 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -2,12 +2,13 @@ mod attester_service; mod block_producer_service; mod config; mod duties; +pub mod error; mod service; use crate::config::Config as ValidatorConfig; use clap::{App, Arg}; use service::Service as ValidatorService; -use slog::{o, Drain}; +use slog::{error, info, o, Drain}; fn main() { // Logging @@ -50,5 +51,8 @@ fn main() { let config = ValidatorConfig::parse_args(matches, &log).unwrap(); // start the validator service. - ValidatorService::start(config, log); + match ValidatorService::start(config, log.clone()) { + Ok(_) => info!(log, "Validator client shutdown successfully."), + Err(e) => error!(log, "Validator exited due to {:?}", e), + } } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index c88db29b85..9eeb308db9 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -3,6 +3,8 @@ use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use crate::config::Config as ValidatorConfig; use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; +use crate::error as error_chain; +use crate::error::ErrorKind; use attester::test_utils::EpochMap; use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use block_proposer::{test_utils::LocalSigner as BlockProposerLocalSigner, BlockProducer}; @@ -13,9 +15,8 @@ use protos::services_grpc::{ AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, ValidatorServiceClient, }; -use slog::{debug, info, warn}; +use slog::{debug, error, info, warn}; use slot_clock::{SlotClock, SystemTimeSlotClock}; -use std::ops::Sub; use std::sync::Arc; use std::time::{Duration, Instant, SystemTime}; use tokio::prelude::*; @@ -57,7 +58,10 @@ impl Service { /// /// This tries to connect to a beacon node. Once connected, it initialised the gRPC clients /// and returns an instance of the service. - fn initialize_service(config: &ValidatorConfig, log: slog::Logger) -> Self { + fn initialize_service( + config: &ValidatorConfig, + log: slog::Logger, + ) -> error_chain::Result { // initialise the beacon node client to check for a connection let env = Arc::new(EnvBuilder::new().build()); @@ -86,7 +90,7 @@ impl Service { log, "Beacon Node's genesis time is in the future. No work to do.\n Exiting" ); - // return Err("Genesis Time in the future"); + return Err("Genesis time in the future".into()); } break info; } @@ -136,35 +140,37 @@ impl Service { Arc::new(AttestationServiceClient::new(ch)) }; - //TODO: Add error chain. Handle errors - let current_slot = slot_clock.present_slot().unwrap().unwrap().sub(1); + 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).unwrap(); - let mut duration_to_next_slot = None; - if let Some(duration_since_genesis) = - duration_since_epoch.checked_sub(Duration::from_secs(genesis_time)) - { - let elapsed_slots = duration_since_epoch - .as_secs() - .checked_div(config.spec.seconds_per_slot as u64) - .unwrap(); - duration_to_next_slot = Some( - Duration::from_secs( - (elapsed_slots + 1) - .checked_mul(config.spec.seconds_per_slot) - .unwrap(), - ) - .checked_sub(duration_since_genesis) - .expect("This should never saturate"), - ); - } - duration_to_next_slot.unwrap_or_else(|| Duration::from_secs(0)) + 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") }; - Self { + Ok(Self { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, @@ -175,13 +181,13 @@ impl Service { validator_client, attester_client, log, - } + }) } /// Initialise the service then run the core thread. - pub fn start(config: ValidatorConfig, log: slog::Logger) { + 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 service = Service::initialize_service(&config, log); + let service = Service::initialize_service(&config, log)?; // we have connected to a node and established its parameters. Spin up the core service @@ -190,10 +196,9 @@ impl Service { .clock(Clock::system()) .name_prefix("validator-client-") .build() - .unwrap(); + .map_err(|e| format!("Tokio runtime failed: {}", e))?; // set up the validator work interval - start at next slot and proceed every slot - // TODO: Error chain handle errors. 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); @@ -223,16 +228,23 @@ impl Service { beacon_node: service.validator_client.clone(), }; - runtime.block_on(interval.for_each(move |_| { - // update duties - debug!( - service.log, - "Processing slot: {}", - service.slot_clock.present_slot().unwrap().unwrap().as_u64() - ); - manager.poll(); - Ok(()) - })); + runtime + .block_on(interval.for_each(move |_| { + // update duties + let current_slot = match service.slot_clock.present_slot() { + Err(e) => { + error!(service.log, "SystemTimeError {:?}", e); + return Ok(()); + } + Ok(slot) => slot.expect("Genesis is in the future"), + }; + + debug!(service.log, "Processing slot: {}", current_slot.as_u64()); + manager.poll(); + Ok(()) + })) + .map_err(|e| format!("Service thread failed: {:?}", e))?; + Ok(()) } /* From 3ad18b4367047b4fab37b3946004c06086c8b080 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 17:47:23 +1100 Subject: [PATCH 22/61] Adds manager duties to validator runtime --- validator_client/src/main.rs | 2 +- validator_client/src/service.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 1c59513a71..84d0cbff7b 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -54,6 +54,6 @@ fn main() { // start the validator service. match ValidatorService::start(config, log.clone()) { Ok(_) => info!(log, "Validator client shutdown successfully."), - Err(e) => error!(log, "Validator exited due to {:?}", e), + Err(e) => error!(log, "Validator exited due to: {}", e.to_string()), } } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 9eeb308db9..720388a618 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -2,6 +2,7 @@ use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use crate::config::Config as ValidatorConfig; +use crate::duties::PollOutcome; use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; use crate::error as error_chain; use crate::error::ErrorKind; @@ -84,7 +85,8 @@ impl Service { if SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() - > Duration::from_secs(info.genesis_time) + .as_secs() + < info.genesis_time { warn!( log, @@ -239,8 +241,32 @@ impl Service { Ok(slot) => slot.expect("Genesis is in the future"), }; + debug_assert!( + current_slot > service.current_slot, + "The Timer should poll a new slot" + ); + debug!(service.log, "Processing slot: {}", current_slot.as_u64()); - manager.poll(); + + // check for new duties + match manager.poll() { + Err(error) => { + error!(service.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) + } + Ok(PollOutcome::NoChange(epoch)) => { + debug!(service.log, "No change in duties"; "epoch" => epoch) + } + Ok(PollOutcome::DutiesChanged(epoch, duties)) => { + info!(service.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) + } + Ok(PollOutcome::NewDuties(epoch, duties)) => { + info!(service.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) + } + Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) => { + error!(service.log, "Epoch or validator unknown"; "epoch" => epoch) + } + }; + Ok(()) })) .map_err(|e| format!("Service thread failed: {:?}", e))?; From a8a3f1c31881b65e47a7f6c02e5c6135c2e95514 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 18:03:23 +1100 Subject: [PATCH 23/61] Removes duty manager service in favour of tokio timer --- validator_client/src/duties/mod.rs | 26 ++++++----------- validator_client/src/duties/service.rs | 40 -------------------------- validator_client/src/service.rs | 20 +++++++------ 3 files changed, 19 insertions(+), 67 deletions(-) delete mode 100644 validator_client/src/duties/service.rs diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index c2b95b1c53..f6460afd28 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -1,21 +1,19 @@ mod epoch_duties; mod grpc; -mod service; #[cfg(test)] mod test_node; mod traits; pub use self::epoch_duties::EpochDutiesMap; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; -pub use self::service::DutiesManagerService; use self::traits::{BeaconNode, BeaconNodeError}; use bls::PublicKey; use slot_clock::SlotClock; use std::sync::Arc; -use types::{ChainSpec, Epoch}; +use types::{ChainSpec, Epoch, Slot}; #[derive(Debug, PartialEq, Clone, Copy)] -pub enum PollOutcome { +pub enum UpdateOutcome { /// The `EpochDuties` were not updated during this poll. NoChange(Epoch), /// The `EpochDuties` for the `epoch` were previously unknown, but obtained in the poll. @@ -50,19 +48,11 @@ pub struct DutiesManager { } impl DutiesManager { - /// Poll the Beacon Node for `EpochDuties`. + /// Check the Beacon Node for `EpochDuties`. /// /// The present `epoch` will be learned from the supplied `SlotClock`. In production this will /// be a wall-clock (e.g., system time, remote server time, etc.). - //TODO: Remove the poll and trust the tokio system-clock timer. Leave for now to ensure the - //timer is accurate. - pub fn poll(&self) -> Result { - let slot = self - .slot_clock - .present_slot() - .map_err(|_| Error::SlotClockError)? - .ok_or(Error::SlotUnknowable)?; - + pub fn update(&self, slot: Slot) -> Result { let epoch = slot.epoch(self.spec.slots_per_epoch); if let Some(duties) = self @@ -72,17 +62,17 @@ impl DutiesManager { // If these duties were known, check to see if they're updates or identical. let result = if let Some(known_duties) = self.duties_map.get(epoch)? { if known_duties == duties { - Ok(PollOutcome::NoChange(epoch)) + Ok(UpdateOutcome::NoChange(epoch)) } else { - Ok(PollOutcome::DutiesChanged(epoch, duties)) + Ok(UpdateOutcome::DutiesChanged(epoch, duties)) } } else { - Ok(PollOutcome::NewDuties(epoch, duties)) + Ok(UpdateOutcome::NewDuties(epoch, duties)) }; self.duties_map.insert(epoch, duties)?; result } else { - Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) + Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) } } } diff --git a/validator_client/src/duties/service.rs b/validator_client/src/duties/service.rs deleted file mode 100644 index bdb6faefae..0000000000 --- a/validator_client/src/duties/service.rs +++ /dev/null @@ -1,40 +0,0 @@ -use super::traits::BeaconNode; -use super::{DutiesManager, PollOutcome}; -use slog::{debug, error, info, Logger}; -use slot_clock::SlotClock; -use std::time::Duration; - -pub struct DutiesManagerService { - pub manager: DutiesManager, - pub poll_interval_millis: u64, - pub log: Logger, -} - -impl DutiesManagerService { - /// Run a loop which polls the manager each `poll_interval_millis` milliseconds. - /// - /// Logs the results of the polls. - pub fn run(&mut self) { - loop { - match self.manager.poll() { - Err(error) => { - error!(self.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) - } - Ok(PollOutcome::NoChange(epoch)) => { - debug!(self.log, "No change in duties"; "epoch" => epoch) - } - Ok(PollOutcome::DutiesChanged(epoch, duties)) => { - info!(self.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) - } - Ok(PollOutcome::NewDuties(epoch, duties)) => { - info!(self.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) - } - Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) => { - error!(self.log, "Epoch or validator unknown"; "epoch" => epoch) - } - }; - - std::thread::sleep(Duration::from_millis(self.poll_interval_millis)); - } - } -} diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 720388a618..8a7e90d104 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -2,8 +2,8 @@ use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use crate::config::Config as ValidatorConfig; -use crate::duties::PollOutcome; -use crate::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; +use crate::duties::UpdateOutcome; +use crate::duties::{DutiesManager, EpochDutiesMap}; use crate::error as error_chain; use crate::error::ErrorKind; use attester::test_utils::EpochMap; @@ -230,9 +230,10 @@ impl Service { beacon_node: service.validator_client.clone(), }; + // run the core thread runtime .block_on(interval.for_each(move |_| { - // update duties + // get the current slot let current_slot = match service.slot_clock.present_slot() { Err(e) => { error!(service.log, "SystemTimeError {:?}", e); @@ -246,23 +247,24 @@ impl Service { "The Timer should poll a new slot" ); - debug!(service.log, "Processing slot: {}", current_slot.as_u64()); + info!(service.log, "Processing slot: {}", current_slot.as_u64()); // check for new duties - match manager.poll() { + // TODO: Convert to its own thread + match manager.update(current_slot) { Err(error) => { error!(service.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) } - Ok(PollOutcome::NoChange(epoch)) => { + Ok(UpdateOutcome::NoChange(epoch)) => { debug!(service.log, "No change in duties"; "epoch" => epoch) } - Ok(PollOutcome::DutiesChanged(epoch, duties)) => { + Ok(UpdateOutcome::DutiesChanged(epoch, duties)) => { info!(service.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) } - Ok(PollOutcome::NewDuties(epoch, duties)) => { + Ok(UpdateOutcome::NewDuties(epoch, duties)) => { info!(service.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) } - Ok(PollOutcome::UnknownValidatorOrEpoch(epoch)) => { + Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) => { error!(service.log, "Epoch or validator unknown"; "epoch" => epoch) } }; From 4cdeb6abe50f96860921eaab58d938e75793b423 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Mon, 25 Mar 2019 18:32:27 +1100 Subject: [PATCH 24/61] Progress towards validator signing attestations. - Added a 'beacon_attester' RPC endpoint, so the BeaconNode can supply attestation data. - Renamed 'attestation_data' to just 'attestation' throughout (except where it is actually just the data structure). --- beacon_node/beacon_chain/src/beacon_chain.rs | 4 +- .../validator_harness/direct_beacon_node.rs | 6 +- beacon_node/rpc/src/beacon_attester.rs | 61 +++++++++++++++++++ eth2/attester/src/lib.rs | 4 +- .../src/test_utils/simulated_beacon_node.rs | 4 +- eth2/attester/src/traits.rs | 4 +- protos/src/services.proto | 18 +++--- .../attestation_grpc_client.rs | 12 ++-- 8 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 beacon_node/rpc/src/beacon_attester.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 816a570c0f..61b5fb58b4 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -280,8 +280,8 @@ where } /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. - pub fn produce_attestation_data(&self, shard: u64) -> Result { - trace!("BeaconChain::produce_attestation_data: shard: {}", shard); + pub fn produce_attestation(&self, shard: u64) -> Result { + trace!("BeaconChain::produce_attestation: shard: {}", shard); let source_epoch = self.state.read().current_justified_epoch; let source_root = *self.state.read().get_block_root( source_epoch.start_slot(self.spec.slots_per_epoch), diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs index d2de354d74..fde8211ab5 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs @@ -50,18 +50,18 @@ impl DirectBeaconNode { } impl AttesterBeaconNode for DirectBeaconNode { - fn produce_attestation_data( + fn produce_attestation( &self, _slot: Slot, shard: u64, ) -> Result, NodeError> { - match self.beacon_chain.produce_attestation_data(shard) { + match self.beacon_chain.produce_attestation(shard) { Ok(attestation_data) => Ok(Some(attestation_data)), Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))), } } - fn publish_attestation_data( + fn publish_attestation( &self, free_attestation: FreeAttestation, ) -> Result { diff --git a/beacon_node/rpc/src/beacon_attester.rs b/beacon_node/rpc/src/beacon_attester.rs new file mode 100644 index 0000000000..36b6a40b24 --- /dev/null +++ b/beacon_node/rpc/src/beacon_attester.rs @@ -0,0 +1,61 @@ +use futures::Future; +use grpcio::{RpcContext, UnarySink}; +use protos::services::{ + Attestation as AttestationProto, ProduceAttestation, ProduceAttestationResponse, + ProduceAttestationRequest, PublishAttestationResponse, PublishAttestationRequest, + PublishAttestation +}; +use protos::services_grpc::BeaconBlockService; +use slog::Logger; + +#[derive(Clone)] +pub struct AttestationServiceInstance { + pub log: Logger, +} + +impl AttestationService for AttestationServiceInstance { + /// Produce a `BeaconBlock` for signing by a validator. + fn produce_attestation( + &mut self, + ctx: RpcContext, + req: ProduceAttestationRequest, + sink: UnarySink, + ) { + println!("producing attestation at slot {}", req.get_slot()); + + // TODO: build a legit block. + let mut attestation = AttestationProto::new(); + attestation.set_slot(req.get_slot()); + // TODO Set the shard to something legit. + attestation.set_shard(0); + attestation.set_block_root(b"cats".to_vec()); + + let mut resp = ProduceAttestationResponse::new(); + resp.set_attestation_data(attestation); + + let f = sink + .success(resp) + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) + } + + /// Accept some fully-formed `BeaconBlock`, process and publish it. + fn publish_attestation( + &mut self, + ctx: RpcContext, + req: PublishAttestationRequest, + sink: UnarySink, + ) { + println!("publishing attestation {:?}", req.get_block()); + + // TODO: actually process the block. + let mut resp = PublishAttestationResponse::new(); + + resp.set_success(true); + + let f = sink + .success(resp) + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) + } +} diff --git a/eth2/attester/src/lib.rs b/eth2/attester/src/lib.rs index 8838f022d9..065fdc9231 100644 --- a/eth2/attester/src/lib.rs +++ b/eth2/attester/src/lib.rs @@ -94,7 +94,7 @@ impl Attester Result { - let attestation_data = match self.beacon_node.produce_attestation_data(slot, shard)? { + let attestation_data = match self.beacon_node.produce_attestation(slot, shard)? { Some(attestation_data) => attestation_data, None => return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(slot)), }; @@ -120,7 +120,7 @@ impl Attester ProduceResult { + fn produce_attestation(&self, slot: Slot, shard: u64) -> ProduceResult { *self.produce_input.write().unwrap() = Some((slot, shard)); match *self.produce_result.read().unwrap() { Some(ref r) => r.clone(), @@ -34,7 +34,7 @@ impl BeaconNode for SimulatedBeaconNode { } } - fn publish_attestation_data(&self, free_attestation: FreeAttestation) -> PublishResult { + fn publish_attestation(&self, free_attestation: FreeAttestation) -> PublishResult { *self.publish_input.write().unwrap() = Some(free_attestation.clone()); match *self.publish_result.read().unwrap() { Some(ref r) => r.clone(), diff --git a/eth2/attester/src/traits.rs b/eth2/attester/src/traits.rs index 6062460cb1..749c6e1a2a 100644 --- a/eth2/attester/src/traits.rs +++ b/eth2/attester/src/traits.rs @@ -14,13 +14,13 @@ pub enum PublishOutcome { /// Defines the methods required to produce and publish blocks on a Beacon Node. pub trait BeaconNode: Send + Sync { - fn produce_attestation_data( + fn produce_attestation( &self, slot: Slot, shard: u64, ) -> Result, BeaconNodeError>; - fn publish_attestation_data( + fn publish_attestation( &self, free_attestation: FreeAttestation, ) -> Result; diff --git a/protos/src/services.proto b/protos/src/services.proto index fbcde922dd..80d512c54a 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -33,8 +33,8 @@ service ValidatorService { /// Service that handles validator attestations service AttestationService { - rpc ProduceAttestationData (ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); - rpc PublishAttestationData (PublishAttestationDataRequest) returns (PublishAttestationDataResponse); + rpc ProduceAttestation(ProduceAttestationRequest) returns (ProduceAttestationResponse); + rpc PublishAttestation(PublishAttestationRequest) returns (PublishAttestationResponse); } /* @@ -138,20 +138,20 @@ message ProposeBlockSlotResponse { * Attestation Service Messages */ -message ProduceAttestationDataRequest { +message ProduceAttestationRequest { uint64 slot = 1; uint64 shard = 2; } -message ProduceAttestationDataResponse { - AttestationData attestation_data = 1; +message ProduceAttestationResponse { + Attestation attestation_data = 1; } -message PublishAttestationDataRequest { +message PublishAttestationRequest { FreeAttestation free_attestation = 1; } -message PublishAttestationDataResponse { +message PublishAttestationResponse { bool success = 1; bytes msg = 2; } @@ -162,7 +162,7 @@ message Crosslink { } -message AttestationData { +message Attestation { uint64 slot = 1; uint64 shard = 2; bytes beacon_block_root = 3; @@ -175,7 +175,7 @@ message AttestationData { } message FreeAttestation { - AttestationData attestation_data = 1; + Attestation attestation_data = 1; bytes signature = 2; uint64 validator_index = 3; } diff --git a/validator_client/src/attester_service/attestation_grpc_client.rs b/validator_client/src/attester_service/attestation_grpc_client.rs index 566d74a39c..5a4701ba93 100644 --- a/validator_client/src/attester_service/attestation_grpc_client.rs +++ b/validator_client/src/attester_service/attestation_grpc_client.rs @@ -2,7 +2,7 @@ use protos::services_grpc::AttestationServiceClient; use std::sync::Arc; use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; -use protos::services::ProduceAttestationDataRequest; +use protos::services::ProduceAttestationRequest; use types::{AttestationData, FreeAttestation, Slot}; pub struct AttestationGrpcClient { @@ -16,25 +16,25 @@ impl AttestationGrpcClient { } impl BeaconNode for AttestationGrpcClient { - fn produce_attestation_data( + fn produce_attestation( &self, slot: Slot, shard: u64, ) -> Result, BeaconNodeError> { - let mut req = ProduceAttestationDataRequest::new(); + let mut req = ProduceAttestationRequest::new(); req.set_slot(slot.as_u64()); req.set_shard(shard); let reply = self .client - .produce_attestation_data(&req) + .produce_attestation(&req) .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - // TODO: return correct AttestationData + // TODO: return correct Attestation Err(BeaconNodeError::DecodeFailure) } - fn publish_attestation_data( + fn publish_attestation( &self, free_attestation: FreeAttestation, ) -> Result { From 05369df7e8a8d82a875224b4f4dea242723081ee Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 22:00:11 +1100 Subject: [PATCH 25/61] Add PubsubMessage and publish function to behaviour --- beacon_node/client/src/lib.rs | 3 +- beacon_node/eth2-libp2p/src/behaviour.rs | 56 +++++++++++++++++++++--- beacon_node/network/src/beacon_chain.rs | 2 +- beacon_node/network/src/service.rs | 13 +++++- beacon_node/rpc/src/beacon_block.rs | 11 ++++- beacon_node/rpc/src/lib.rs | 5 ++- 6 files changed, 78 insertions(+), 12 deletions(-) diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 44eab4fe20..b24d2cb7f2 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -46,7 +46,7 @@ impl Client { // TODO: Add beacon_chain reference to network parameters let network_config = &config.net_conf; let network_logger = log.new(o!("Service" => "Network")); - let (network, _network_send) = NetworkService::new( + let (network, network_send) = NetworkService::new( beacon_chain.clone(), network_config, executor, @@ -59,6 +59,7 @@ impl Client { rpc_exit_signal = Some(rpc::start_server( &config.rpc_conf, executor, + network_send, beacon_chain.clone(), &log, )); diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 3d5b943536..b3c4213b1c 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -14,6 +14,7 @@ use libp2p::{ NetworkBehaviour, PeerId, }; use slog::{debug, o}; +use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz_derive::{Decode, Encode}; use types::Attestation; use types::Topic; @@ -124,6 +125,15 @@ impl Behaviour { } } + /* Behaviour functions */ + + /// Publishes a message on the pubsub (gossipsub) behaviour. + pub fn publish(&mut self, topic: Topic, message: PubsubMessage) { + //encode the message + let message_bytes = ssz_encode(&message); + self.gossipsub.publish(topic, message_bytes); + } + /// Consumes the events list when polled. fn poll( &mut self, @@ -158,12 +168,6 @@ pub enum BehaviourEvent { Message(String), } -#[derive(Debug, Clone)] -pub enum IncomingGossip { - Block(BlockGossip), - Attestation(AttestationGossip), -} - /// Gossipsub message providing notification of a new block. #[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BlockGossip { @@ -175,3 +179,43 @@ pub struct BlockGossip { pub struct AttestationGossip { pub attestation: Attestation, } + +/// Messages that are passed to and from the pubsub (Gossipsub) behaviour. +#[derive(Debug, Clone)] +pub enum PubsubMessage { + Block(BlockGossip), + Attestation(AttestationGossip), +} + +//TODO: Correctly encode/decode enums. Prefixing with integer for now. +impl Encodable for PubsubMessage { + fn ssz_append(&self, s: &mut SszStream) { + match self { + PubsubMessage::Block(block_gossip) => { + 0u32.ssz_append(s); + block_gossip.ssz_append(s); + } + PubsubMessage::Attestation(attestation_gossip) => { + 1u32.ssz_append(s); + attestation_gossip.ssz_append(s); + } + } + } +} + +impl Decodable for PubsubMessage { + fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> { + let (id, index) = u32::ssz_decode(bytes, index)?; + match id { + 1 => { + let (block, index) = BlockGossip::ssz_decode(bytes, index)?; + Ok((PubsubMessage::Block(block), index)) + } + 2 => { + let (attestation, index) = AttestationGossip::ssz_decode(bytes, index)?; + Ok((PubsubMessage::Attestation(attestation), index)) + } + _ => Err(DecodeError::Invalid), + } + } +} diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 26cea00654..c627912a45 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -109,7 +109,7 @@ where let state = self.get_state(); HelloMessage { - network_id: spec.network_id, + network_id: spec.chain_id, latest_finalized_root: state.finalized_root, latest_finalized_epoch: state.finalized_epoch, best_root: self.best_block_root(), diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index a3eb6f0d9d..33ea79c1a6 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -3,7 +3,7 @@ use crate::error; use crate::message_handler::{HandlerMessage, MessageHandler}; use crate::NetworkConfig; use crossbeam_channel::{unbounded as channel, Sender, TryRecvError}; -use eth2_libp2p::RPCEvent; +use eth2_libp2p::{RPCEvent, PublishMessage}; use eth2_libp2p::Service as LibP2PService; use eth2_libp2p::{Libp2pEvent, PeerId}; use futures::prelude::*; @@ -12,6 +12,7 @@ use futures::Stream; use slog::{debug, info, o, trace}; use std::sync::Arc; use tokio::runtime::TaskExecutor; +use types::{BeaconBlock, Topic}; /// Service that handles communication between internal services and the eth2_libp2p network service. pub struct Service { @@ -161,7 +162,12 @@ fn network_service( return Err(eth2_libp2p::error::Error::from( "Network channel disconnected", )); - } + }, + Ok(NetworkMessage::Publish(topic, message) => { + debug!(log, "Sending message on topic {:?}", topic); + libp2p_service.swarm.publish(topic,message) + + } } Ok(Async::NotReady) @@ -174,6 +180,8 @@ pub enum NetworkMessage { /// Send a message to libp2p service. //TODO: Define typing for messages across the wire Send(PeerId, OutgoingMessage), + /// Publish a message to gossipsub + Publish(Topic, PublishMessage), } /// Type of outgoing messages that can be sent through the network service. @@ -184,3 +192,4 @@ pub enum OutgoingMessage { //TODO: Remove NotifierTest, } + diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 96f64e0dd4..9169d695d4 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -9,6 +9,7 @@ use slog::Logger; #[derive(Clone)] pub struct BeaconBlockServiceInstance { + network_chan: crossbeam_channel::Sender, pub log: Logger, } @@ -43,7 +44,15 @@ impl BeaconBlockService for BeaconBlockServiceInstance { req: PublishBeaconBlockRequest, sink: UnarySink, ) { - println!("publishing {:?}", req.get_block()); + let block = req.get_block(); + println!("publishing {:?}", block); + + + // TODO: Build properly + let topic = types::TopicBuilder:: + println!("Sending beacon block to gossipsub"); + network_chan.send(NetworkMessage::Publish( + // TODO: actually process the block. let mut resp = PublishBeaconBlockResponse::new(); diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 3c89bda1f6..e1267270ca 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -21,6 +21,7 @@ use tokio::runtime::TaskExecutor; pub fn start_server( config: &RPCConfig, executor: &TaskExecutor, + network_chan: crossbeam_channel::Sender, beacon_chain: Arc, log: &slog::Logger, ) -> exit_future::Signal { @@ -40,7 +41,9 @@ pub fn start_server( }; let beacon_block_service = { - let instance = BeaconBlockServiceInstance { log: log.clone() }; + let instance = BeaconBlockServiceInstance { + network_chan + log: log.clone() }; create_beacon_block_service(instance) }; let validator_service = { From 52b31b200940850b265ae585d567f3a286f0fae1 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 23:02:51 +1100 Subject: [PATCH 26/61] Implement initial pubsub message handling --- beacon_node/eth2-libp2p/src/behaviour.rs | 57 +++++++++++++++------- beacon_node/eth2-libp2p/src/lib.rs | 3 +- beacon_node/eth2-libp2p/src/service.rs | 26 +++++++--- beacon_node/network/src/beacon_chain.rs | 2 +- beacon_node/network/src/message_handler.rs | 12 ++--- beacon_node/network/src/service.rs | 36 ++++++++------ beacon_node/rpc/Cargo.toml | 2 + beacon_node/rpc/src/beacon_block.rs | 6 ++- eth2/types/src/lib.rs | 2 +- 9 files changed, 95 insertions(+), 51 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index b3c4213b1c..0229d06d50 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -13,11 +13,11 @@ use libp2p::{ tokio_io::{AsyncRead, AsyncWrite}, NetworkBehaviour, PeerId, }; -use slog::{debug, o}; +use slog::{debug, o, warn}; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz_derive::{Decode, Encode}; use types::Attestation; -use types::Topic; +use types::{Topic, TopicHash}; /// Builds the network behaviour for the libp2p Swarm. /// Implements gossipsub message routing. @@ -48,13 +48,33 @@ impl NetworkBehaviourEventProcess { - let gs_message = String::from_utf8_lossy(&message.data); - // TODO: Remove this type - debug only - self.events - .push(BehaviourEvent::Message(gs_message.to_string())) + GossipsubEvent::Message(gs_msg) => { + let pubsub_message = match PubsubMessage::ssz_decode(&gs_msg.data, 0) { + //TODO: Punish peer on error + Err(e) => { + warn!( + self.log, + "Received undecodable message from Peer {:?}", gs_msg.source + ); + return; + } + Ok((msg, _index)) => msg, + }; + + self.events.push(BehaviourEvent::GossipMessage { + source: gs_msg.source, + topics: gs_msg.topics, + message: pubsub_message, + }); } - _ => {} + GossipsubEvent::Subscribed { + peer_id: _, + topic: _, + } + | GossipsubEvent::Unsubscribed { + peer_id: _, + topic: _, + } => {} } } } @@ -125,15 +145,6 @@ impl Behaviour { } } - /* Behaviour functions */ - - /// Publishes a message on the pubsub (gossipsub) behaviour. - pub fn publish(&mut self, topic: Topic, message: PubsubMessage) { - //encode the message - let message_bytes = ssz_encode(&message); - self.gossipsub.publish(topic, message_bytes); - } - /// Consumes the events list when polled. fn poll( &mut self, @@ -157,6 +168,12 @@ impl Behaviour { pub fn send_rpc(&mut self, peer_id: PeerId, rpc_event: RPCEvent) { self.serenity_rpc.send_rpc(peer_id, rpc_event); } + + /// Publishes a message on the pubsub (gossipsub) behaviour. + pub fn publish(&mut self, topic: Topic, message: PubsubMessage) { + let message_bytes = ssz_encode(&message); + self.gossipsub.publish(topic, message_bytes); + } } /// The types of events than can be obtained from polling the behaviour. @@ -165,7 +182,11 @@ pub enum BehaviourEvent { PeerDialed(PeerId), Identified(PeerId, IdentifyInfo), // TODO: This is a stub at the moment - Message(String), + GossipMessage { + source: PeerId, + topics: Vec, + message: PubsubMessage, + }, } /// Gossipsub message providing notification of a new block. diff --git a/beacon_node/eth2-libp2p/src/lib.rs b/beacon_node/eth2-libp2p/src/lib.rs index f7a961bb2e..659d6b01c1 100644 --- a/beacon_node/eth2-libp2p/src/lib.rs +++ b/beacon_node/eth2-libp2p/src/lib.rs @@ -8,12 +8,13 @@ pub mod error; pub mod rpc; mod service; +pub use behaviour::PubsubMessage; pub use config::Config as NetworkConfig; pub use libp2p::{ gossipsub::{GossipsubConfig, GossipsubConfigBuilder}, PeerId, }; -pub use rpc::{HelloMessage, RPCEvent}; +pub use rpc::RPCEvent; pub use service::Libp2pEvent; pub use service::Service; pub use types::multiaddr; diff --git a/beacon_node/eth2-libp2p/src/service.rs b/beacon_node/eth2-libp2p/src/service.rs index e68df2d389..73facee720 100644 --- a/beacon_node/eth2-libp2p/src/service.rs +++ b/beacon_node/eth2-libp2p/src/service.rs @@ -1,4 +1,4 @@ -use crate::behaviour::{Behaviour, BehaviourEvent}; +use crate::behaviour::{Behaviour, BehaviourEvent, PubsubMessage}; use crate::error; use crate::multiaddr::Protocol; use crate::rpc::RPCEvent; @@ -16,7 +16,7 @@ use libp2p::{core, secio, PeerId, Swarm, Transport}; use slog::{debug, info, trace, warn}; use std::io::{Error, ErrorKind}; use std::time::Duration; -use types::TopicBuilder; +use types::{TopicBuilder, TopicHash}; /// The configuration and state of the libp2p components for the beacon node. pub struct Service { @@ -107,9 +107,17 @@ impl Stream for Service { //Behaviour events Ok(Async::Ready(Some(event))) => match event { // TODO: Stub here for debugging - BehaviourEvent::Message(m) => { - debug!(self.log, "Message received: {}", m); - return Ok(Async::Ready(Some(Libp2pEvent::Message(m)))); + BehaviourEvent::GossipMessage { + source, + topics, + message, + } => { + debug!(self.log, "Pubsub message received: {:?}", message); + return Ok(Async::Ready(Some(Libp2pEvent::PubsubMessage { + source, + topics, + message, + }))); } BehaviourEvent::RPC(peer_id, event) => { return Ok(Async::Ready(Some(Libp2pEvent::RPC(peer_id, event)))); @@ -173,6 +181,10 @@ pub enum Libp2pEvent { PeerDialed(PeerId), /// Received information about a peer on the network. Identified(PeerId, IdentifyInfo), - // TODO: Pub-sub testing only. - Message(String), + /// Received pubsub message. + PubsubMessage { + source: PeerId, + topics: Vec, + message: PubsubMessage, + }, } diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index c627912a45..8ec8162ff7 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -7,7 +7,7 @@ use beacon_chain::{ types::{BeaconState, ChainSpec}, AggregationOutcome, CheckPoint, }; -use eth2_libp2p::HelloMessage; +use eth2_libp2p::rpc::HelloMessage; use types::{Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index dcfee96a0a..0efa6b96fd 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -4,7 +4,7 @@ use crate::service::{NetworkMessage, OutgoingMessage}; use crate::sync::SimpleSync; use crossbeam_channel::{unbounded as channel, Sender}; use eth2_libp2p::{ - behaviour::IncomingGossip, + behaviour::PubsubMessage, rpc::{methods::GoodbyeReason, RPCRequest, RPCResponse, RequestId}, PeerId, RPCEvent, }; @@ -41,7 +41,7 @@ pub enum HandlerMessage { /// An RPC response/request has been received. RPC(PeerId, RPCEvent), /// A gossip message has been received. - IncomingGossip(PeerId, IncomingGossip), + PubsubMessage(PeerId, PubsubMessage), } impl MessageHandler { @@ -92,7 +92,7 @@ impl MessageHandler { self.handle_rpc_message(peer_id, rpc_event); } // we have received an RPC message request/response - HandlerMessage::IncomingGossip(peer_id, gossip) => { + HandlerMessage::PubsubMessage(peer_id, gossip) => { self.handle_gossip(peer_id, gossip); } //TODO: Handle all messages @@ -205,13 +205,13 @@ impl MessageHandler { } /// Handle RPC messages - fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: IncomingGossip) { + fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: PubsubMessage) { match gossip_message { - IncomingGossip::Block(message) => { + PubsubMessage::Block(message) => { self.sync .on_block_gossip(peer_id, message, &mut self.network_context) } - IncomingGossip::Attestation(message) => { + PubsubMessage::Attestation(message) => { self.sync .on_attestation_gossip(peer_id, message, &mut self.network_context) } diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index 33ea79c1a6..55c43e4ec6 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -3,16 +3,16 @@ use crate::error; use crate::message_handler::{HandlerMessage, MessageHandler}; use crate::NetworkConfig; use crossbeam_channel::{unbounded as channel, Sender, TryRecvError}; -use eth2_libp2p::{RPCEvent, PublishMessage}; use eth2_libp2p::Service as LibP2PService; use eth2_libp2p::{Libp2pEvent, PeerId}; +use eth2_libp2p::{PubsubMessage, RPCEvent}; use futures::prelude::*; use futures::sync::oneshot; use futures::Stream; use slog::{debug, info, o, trace}; use std::sync::Arc; use tokio::runtime::TaskExecutor; -use types::{BeaconBlock, Topic}; +use types::Topic; /// Service that handles communication between internal services and the eth2_libp2p network service. pub struct Service { @@ -100,6 +100,7 @@ fn spawn_service( Ok(network_exit) } +//TODO: Potentially handle channel errors fn network_service( mut libp2p_service: LibP2PService, network_recv: crossbeam_channel::Receiver, @@ -129,10 +130,17 @@ fn network_service( "We have identified peer: {:?} with {:?}", peer_id, info ); } - Libp2pEvent::Message(m) => debug!( - libp2p_service.log, - "Network Service: Message received: {}", m - ), + Libp2pEvent::PubsubMessage { + source, + topics: _, + message, + } => { + //TODO: Decide if we need to propagate the topic upwards. (Potentially for + //attestations) + message_handler_send + .send(HandlerMessage::PubsubMessage(source, message)) + .map_err(|_| " failed to send pubsub message to handler")?; + } }, Ok(Async::Ready(None)) => unreachable!("Stream never ends"), Ok(Async::NotReady) => break, @@ -157,17 +165,16 @@ fn network_service( } }; } + Ok(NetworkMessage::Publish(topic, message)) => { + debug!(log, "Sending pubsub message on topic {:?}", topic); + libp2p_service.swarm.publish(topic, message); + } Err(TryRecvError::Empty) => break, Err(TryRecvError::Disconnected) => { return Err(eth2_libp2p::error::Error::from( "Network channel disconnected", )); - }, - Ok(NetworkMessage::Publish(topic, message) => { - debug!(log, "Sending message on topic {:?}", topic); - libp2p_service.swarm.publish(topic,message) - - + } } } Ok(Async::NotReady) @@ -180,8 +187,8 @@ pub enum NetworkMessage { /// Send a message to libp2p service. //TODO: Define typing for messages across the wire Send(PeerId, OutgoingMessage), - /// Publish a message to gossipsub - Publish(Topic, PublishMessage), + /// Publish a message to pubsub mechanism. + Publish(Topic, PubsubMessage), } /// Type of outgoing messages that can be sent through the network service. @@ -192,4 +199,3 @@ pub enum OutgoingMessage { //TODO: Remove NotifierTest, } - diff --git a/beacon_node/rpc/Cargo.toml b/beacon_node/rpc/Cargo.toml index d405982db1..e9709c1ced 100644 --- a/beacon_node/rpc/Cargo.toml +++ b/beacon_node/rpc/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] bls = { path = "../../eth2/utils/bls" } beacon_chain = { path = "../beacon_chain" } +network = { path = "../network" } version = { path = "../version" } types = { path = "../../eth2/types" } ssz = { path = "../../eth2/utils/ssz" } @@ -23,3 +24,4 @@ slog-term = "^2.4.0" slog-async = "^2.3.0" tokio = "0.1.17" exit-future = "0.1.4" +crossbeam-channel = "0.3.8" diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 9169d695d4..d124152f13 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -6,6 +6,8 @@ use protos::services::{ }; use protos::services_grpc::BeaconBlockService; use slog::Logger; +use crossbeam_channel; +use network::NetworkMessage; #[derive(Clone)] pub struct BeaconBlockServiceInstance { @@ -48,8 +50,8 @@ impl BeaconBlockService for BeaconBlockServiceInstance { println!("publishing {:?}", block); - // TODO: Build properly - let topic = types::TopicBuilder:: + // TODO: Obtain from the network properly. + let topic = types::TopicBuilder::from("beacon_chain").build(); println!("Sending beacon block to gossipsub"); network_chan.send(NetworkMessage::Publish( diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index 953a9508f9..118e862e8f 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -85,6 +85,6 @@ pub type AttesterMap = HashMap<(u64, u64), Vec>; pub type ProposerMap = HashMap; pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, SecretKey, Signature}; -pub use libp2p::floodsub::{Topic, TopicBuilder}; +pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash}; pub use libp2p::multiaddr; pub use libp2p::Multiaddr; From f7131c2f871fa521c2418ba51f56b22014a25507 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 23:39:39 +1100 Subject: [PATCH 27/61] Fix compile issues and modify type names --- beacon_node/eth2-libp2p/src/behaviour.rs | 28 ++++++++------------- beacon_node/network/src/lib.rs | 1 + beacon_node/network/src/service.rs | 11 +++++--- beacon_node/network/src/sync/simple_sync.rs | 19 +++++++------- beacon_node/rpc/Cargo.toml | 1 + beacon_node/rpc/src/beacon_block.rs | 28 ++++++++++++++------- beacon_node/rpc/src/lib.rs | 6 +++-- 7 files changed, 51 insertions(+), 43 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 0229d06d50..41b7c89657 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -170,9 +170,11 @@ impl Behaviour { } /// Publishes a message on the pubsub (gossipsub) behaviour. - pub fn publish(&mut self, topic: Topic, message: PubsubMessage) { + pub fn publish(&mut self, topics: Vec, message: PubsubMessage) { let message_bytes = ssz_encode(&message); - self.gossipsub.publish(topic, message_bytes); + for topic in topics { + self.gossipsub.publish(topic, message_bytes.clone()); + } } } @@ -189,23 +191,13 @@ pub enum BehaviourEvent { }, } -/// Gossipsub message providing notification of a new block. -#[derive(Encode, Decode, Clone, Debug, PartialEq)] -pub struct BlockGossip { - pub root: BlockRootSlot, -} - -/// Gossipsub message providing notification of a new attestation. -#[derive(Encode, Decode, Clone, Debug, PartialEq)] -pub struct AttestationGossip { - pub attestation: Attestation, -} - /// Messages that are passed to and from the pubsub (Gossipsub) behaviour. #[derive(Debug, Clone)] pub enum PubsubMessage { - Block(BlockGossip), - Attestation(AttestationGossip), + /// Gossipsub message providing notification of a new block. + Block(BlockRootSlot), + /// Gossipsub message providing notification of a new attestation. + Attestation(Attestation), } //TODO: Correctly encode/decode enums. Prefixing with integer for now. @@ -229,11 +221,11 @@ impl Decodable for PubsubMessage { let (id, index) = u32::ssz_decode(bytes, index)?; match id { 1 => { - let (block, index) = BlockGossip::ssz_decode(bytes, index)?; + let (block, index) = BlockRootSlot::ssz_decode(bytes, index)?; Ok((PubsubMessage::Block(block), index)) } 2 => { - let (attestation, index) = AttestationGossip::ssz_decode(bytes, index)?; + let (attestation, index) = Attestation::ssz_decode(bytes, index)?; Ok((PubsubMessage::Attestation(attestation), index)) } _ => Err(DecodeError::Invalid), diff --git a/beacon_node/network/src/lib.rs b/beacon_node/network/src/lib.rs index 87f8368a50..c298e31b4e 100644 --- a/beacon_node/network/src/lib.rs +++ b/beacon_node/network/src/lib.rs @@ -6,4 +6,5 @@ pub mod service; pub mod sync; pub use eth2_libp2p::NetworkConfig; +pub use service::NetworkMessage; pub use service::Service; diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index 55c43e4ec6..b2d2b5a246 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -165,9 +165,9 @@ fn network_service( } }; } - Ok(NetworkMessage::Publish(topic, message)) => { - debug!(log, "Sending pubsub message on topic {:?}", topic); - libp2p_service.swarm.publish(topic, message); + Ok(NetworkMessage::Publish { topics, message }) => { + debug!(log, "Sending pubsub message on topics {:?}", topics); + libp2p_service.swarm.publish(topics, message); } Err(TryRecvError::Empty) => break, Err(TryRecvError::Disconnected) => { @@ -188,7 +188,10 @@ pub enum NetworkMessage { //TODO: Define typing for messages across the wire Send(PeerId, OutgoingMessage), /// Publish a message to pubsub mechanism. - Publish(Topic, PubsubMessage), + Publish { + topics: Vec, + message: PubsubMessage, + }, } /// Type of outgoing messages that can be sent through the network service. diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 4c08a68710..2aa0a1d7dd 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,7 +1,6 @@ use super::import_queue::ImportQueue; use crate::beacon_chain::BeaconChain; use crate::message_handler::NetworkContext; -use eth2_libp2p::behaviour::{AttestationGossip, BlockGossip}; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId}; use eth2_libp2p::PeerId; @@ -9,7 +8,7 @@ use slog::{debug, error, info, o, warn}; use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use types::{Epoch, Hash256, Slot}; +use types::{Attestation, Epoch, Hash256, Slot}; /// The number of slots that we can import blocks ahead of us, before going into full Sync mode. const SLOT_IMPORT_TOLERANCE: u64 = 100; @@ -521,12 +520,12 @@ impl SimpleSync { pub fn on_block_gossip( &mut self, peer_id: PeerId, - msg: BlockGossip, + msg: BlockRootSlot, network: &mut NetworkContext, ) { debug!( self.log, - "BlockGossip"; + "BlockSlot"; "peer" => format!("{:?}", peer_id), ); // TODO: filter out messages that a prior to the finalized slot. @@ -535,12 +534,12 @@ impl SimpleSync { // now. // // Note: only requests the new block -- will fail if we don't have its parents. - if self.import_queue.is_new_block(&msg.root.block_root) { + if self.import_queue.is_new_block(&msg.block_root) { self.request_block_headers( peer_id, BeaconBlockHeadersRequest { - start_root: msg.root.block_root, - start_slot: msg.root.slot, + start_root: msg.block_root, + start_slot: msg.slot, max_headers: 1, skip_slots: 0, }, @@ -555,19 +554,19 @@ impl SimpleSync { pub fn on_attestation_gossip( &mut self, peer_id: PeerId, - msg: AttestationGossip, + msg: Attestation, _network: &mut NetworkContext, ) { debug!( self.log, - "AttestationGossip"; + "Attestation"; "peer" => format!("{:?}", peer_id), ); // Awaiting a proper operations pool before we can import attestations. // // https://github.com/sigp/lighthouse/issues/281 - match self.chain.process_attestation(msg.attestation) { + match self.chain.process_attestation(msg) { Ok(_) => panic!("Impossible, method not implemented."), Err(_) => error!(self.log, "Attestation processing not implemented!"), } diff --git a/beacon_node/rpc/Cargo.toml b/beacon_node/rpc/Cargo.toml index e9709c1ced..3fc52c6b16 100644 --- a/beacon_node/rpc/Cargo.toml +++ b/beacon_node/rpc/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" bls = { path = "../../eth2/utils/bls" } beacon_chain = { path = "../beacon_chain" } network = { path = "../network" } +eth2-libp2p = { path = "../eth2-libp2p" } version = { path = "../version" } types = { path = "../../eth2/types" } ssz = { path = "../../eth2/utils/ssz" } diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index d124152f13..4e1875665b 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -1,17 +1,20 @@ +use crossbeam_channel; +use eth2_libp2p::rpc::methods::BlockRootSlot; +use eth2_libp2p::PubsubMessage; use futures::Future; use grpcio::{RpcContext, UnarySink}; +use network::NetworkMessage; use protos::services::{ BeaconBlock as BeaconBlockProto, ProduceBeaconBlockRequest, ProduceBeaconBlockResponse, PublishBeaconBlockRequest, PublishBeaconBlockResponse, }; use protos::services_grpc::BeaconBlockService; use slog::Logger; -use crossbeam_channel; -use network::NetworkMessage; +use types::{Hash256, Slot}; #[derive(Clone)] pub struct BeaconBlockServiceInstance { - network_chan: crossbeam_channel::Sender, + pub network_chan: crossbeam_channel::Sender, pub log: Logger, } @@ -47,14 +50,21 @@ impl BeaconBlockService for BeaconBlockServiceInstance { sink: UnarySink, ) { let block = req.get_block(); - println!("publishing {:?}", block); + let block_root = Hash256::from_slice(block.get_block_root()); + let block_slot = BlockRootSlot { + block_root, + slot: Slot::from(block.get_slot()), + }; + println!("publishing block with root {:?}", block_root); - - // TODO: Obtain from the network properly. - let topic = types::TopicBuilder::from("beacon_chain").build(); + // TODO: Obtain topics from the network service properly. + let topic = types::TopicBuilder::new("beacon_chain".to_string()).build(); + let message = PubsubMessage::Block(block_slot); println!("Sending beacon block to gossipsub"); - network_chan.send(NetworkMessage::Publish( - + self.network_chan.send(NetworkMessage::Publish { + topics: vec![topic], + message, + }); // TODO: actually process the block. let mut resp = PublishBeaconBlockResponse::new(); diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index e1267270ca..4dfd334879 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -11,6 +11,7 @@ use self::validator::ValidatorServiceInstance; pub use config::Config as RPCConfig; use futures::{future, Future}; use grpcio::{Environment, Server, ServerBuilder}; +use network::NetworkMessage; use protos::services_grpc::{ create_beacon_block_service, create_beacon_node_service, create_validator_service, }; @@ -42,8 +43,9 @@ pub fn start_server( let beacon_block_service = { let instance = BeaconBlockServiceInstance { - network_chan - log: log.clone() }; + network_chan, + log: log.clone(), + }; create_beacon_block_service(instance) }; let validator_service = { From a145824c51fdb872ea600ef756f8312dc4e4e0dc Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 09:46:26 +1100 Subject: [PATCH 28/61] Set BeaconChainHarness to return block This is useful if you want to inspect the block. --- .../beacon_chain/test_harness/src/beacon_chain_harness.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs index 1498796b18..9424974763 100644 --- a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs @@ -207,13 +207,13 @@ impl BeaconChainHarness { /// /// This is the ideal scenario for the Beacon Chain, 100% honest participation from /// validators. - pub fn advance_chain_with_block(&mut self) { + pub fn advance_chain_with_block(&mut self) -> BeaconBlock { self.increment_beacon_chain_slot(); // Produce a new block. let block = self.produce_block(); debug!("Submitting block for processing..."); - match self.beacon_chain.process_block(block) { + match self.beacon_chain.process_block(block.clone()) { Ok(BlockProcessingOutcome::ValidBlock(_)) => {} other => panic!("block processing failed with {:?}", other), }; @@ -233,6 +233,8 @@ impl BeaconChainHarness { }); debug!("Free attestations processed."); + + block } /// Signs a message using some validators secret key with the `Fork` info from the latest state From 33d0f29221f6b816fd05641eba618e49f6bf4401 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Tue, 26 Mar 2019 11:33:24 +1100 Subject: [PATCH 29/61] Remove old tests --- validator_client/src/duties/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index f6460afd28..fa53180a65 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -91,6 +91,7 @@ impl From for Error { } } +/* TODO: Modify tests for new Duties Manager form #[cfg(test)] mod tests { use super::test_node::TestBeaconNode; @@ -104,6 +105,7 @@ mod tests { // // These tests should serve as a good example for future tests. + #[test] pub fn polling() { let spec = Arc::new(ChainSpec::foundation()); @@ -154,3 +156,4 @@ mod tests { ); } } +*/ From ffb3d943556935d50b9815596e876296ec1c1283 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Tue, 26 Mar 2019 11:59:48 +1100 Subject: [PATCH 30/61] Wrap the duty manager in a future for its own thread --- validator_client/Cargo.toml | 1 + validator_client/src/duties/mod.rs | 25 +++++++++++++++++++++- validator_client/src/service.rs | 33 +++++++++--------------------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index eace153fab..570e06d74c 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -32,3 +32,4 @@ tokio = "0.1.18" tokio-timer = "0.2.10" error-chain = "0.12.0" bincode = "^1.1.2" +futures = "0.1.25" diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index fa53180a65..185f80d6df 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -8,6 +8,8 @@ pub use self::epoch_duties::EpochDutiesMap; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; use self::traits::{BeaconNode, BeaconNodeError}; use bls::PublicKey; +use futures::Async; +use slog::{debug, error, info}; use slot_clock::SlotClock; use std::sync::Arc; use types::{ChainSpec, Epoch, Slot}; @@ -52,7 +54,7 @@ impl DutiesManager { /// /// The present `epoch` will be learned from the supplied `SlotClock`. In production this will /// be a wall-clock (e.g., system time, remote server time, etc.). - pub fn update(&self, slot: Slot) -> Result { + fn update(&self, slot: Slot) -> Result { let epoch = slot.epoch(self.spec.slots_per_epoch); if let Some(duties) = self @@ -75,6 +77,27 @@ impl DutiesManager { Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) } } + + /// A future wrapping around `update()`. This will perform logic based upon the update + /// process and complete once the update has completed. + pub fn run_update(&self, slot: Slot, log: slog::Logger) -> Result, ()> { + match self.update(slot) { + Err(error) => error!(log, "Epoch duties poll error"; "error" => format!("{:?}", error)), + Ok(UpdateOutcome::NoChange(epoch)) => { + debug!(log, "No change in duties"; "epoch" => epoch) + } + Ok(UpdateOutcome::DutiesChanged(epoch, duties)) => { + info!(log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) + } + Ok(UpdateOutcome::NewDuties(epoch, duties)) => { + info!(log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) + } + Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) => { + error!(log, "Epoch or validator unknown"; "epoch" => epoch) + } + }; + Ok(Async::Ready(())) + } } impl From for Error { diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 8a7e90d104..9ca591b926 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -222,21 +222,22 @@ impl Service { // build requisite objects to pass to core thread. let duties_map = Arc::new(EpochDutiesMap::new(config.spec.slots_per_epoch)); let epoch_map_for_attester = Arc::new(EpochMap::new(config.spec.slots_per_epoch)); - let manager = DutiesManager { + let manager = Arc::new(DutiesManager { duties_map, pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(), spec: Arc::new(config.spec), slot_clock: service.slot_clock.clone(), beacon_node: service.validator_client.clone(), - }; + }); // run the core thread runtime .block_on(interval.for_each(move |_| { + let log = service.log.clone(); // get the current slot let current_slot = match service.slot_clock.present_slot() { Err(e) => { - error!(service.log, "SystemTimeError {:?}", e); + error!(log, "SystemTimeError {:?}", e); return Ok(()); } Ok(slot) => slot.expect("Genesis is in the future"), @@ -247,28 +248,14 @@ impl Service { "The Timer should poll a new slot" ); - info!(service.log, "Processing slot: {}", current_slot.as_u64()); + info!(log, "Processing slot: {}", current_slot.as_u64()); + + let cloned_manager = manager.clone(); // check for new duties - // TODO: Convert to its own thread - match manager.update(current_slot) { - Err(error) => { - error!(service.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) - } - Ok(UpdateOutcome::NoChange(epoch)) => { - debug!(service.log, "No change in duties"; "epoch" => epoch) - } - Ok(UpdateOutcome::DutiesChanged(epoch, duties)) => { - info!(service.log, "Duties changed (potential re-org)"; "epoch" => epoch, "duties" => format!("{:?}", duties)) - } - Ok(UpdateOutcome::NewDuties(epoch, duties)) => { - info!(service.log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) - } - Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) => { - error!(service.log, "Epoch or validator unknown"; "epoch" => epoch) - } - }; - + tokio::spawn(futures::future::poll_fn(move || { + cloned_manager.run_update(current_slot.clone(), log.clone()) + })); Ok(()) })) .map_err(|e| format!("Service thread failed: {:?}", e))?; From d4fecd8a84e92060ce2da6f2102b6dc9c5449d0a Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 12:32:38 +1100 Subject: [PATCH 31/61] Set GRPC block to be just SSZ --- beacon_node/rpc/src/beacon_block.rs | 58 +++++++++++-------- protos/src/services.proto | 5 +- .../beacon_block_grpc_client.rs | 38 +++--------- 3 files changed, 43 insertions(+), 58 deletions(-) diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 4e1875665b..6317414b03 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -9,8 +9,10 @@ use protos::services::{ PublishBeaconBlockRequest, PublishBeaconBlockResponse, }; use protos::services_grpc::BeaconBlockService; +use slog::debug; use slog::Logger; -use types::{Hash256, Slot}; +use ssz::{Decodable, TreeHash}; +use types::{BeaconBlock, Hash256, Slot}; #[derive(Clone)] pub struct BeaconBlockServiceInstance { @@ -30,8 +32,7 @@ impl BeaconBlockService for BeaconBlockServiceInstance { // TODO: build a legit block. let mut block = BeaconBlockProto::new(); - block.set_slot(req.get_slot()); - block.set_block_root(b"cats".to_vec()); + block.set_ssz(b"cats".to_vec()); let mut resp = ProduceBeaconBlockResponse::new(); resp.set_block(block); @@ -49,30 +50,39 @@ impl BeaconBlockService for BeaconBlockServiceInstance { req: PublishBeaconBlockRequest, sink: UnarySink, ) { + debug!(self.log, "PublishBeaconBlock"); + let block = req.get_block(); - let block_root = Hash256::from_slice(block.get_block_root()); - let block_slot = BlockRootSlot { - block_root, - slot: Slot::from(block.get_slot()), - }; - println!("publishing block with root {:?}", block_root); - // TODO: Obtain topics from the network service properly. - let topic = types::TopicBuilder::new("beacon_chain".to_string()).build(); - let message = PubsubMessage::Block(block_slot); - println!("Sending beacon block to gossipsub"); - self.network_chan.send(NetworkMessage::Publish { - topics: vec![topic], - message, - }); + match BeaconBlock::ssz_decode(block.get_ssz(), 0) { + Ok((block, _i)) => { + let block_root = Hash256::from_slice(&block.hash_tree_root()[..]); - // TODO: actually process the block. - let mut resp = PublishBeaconBlockResponse::new(); - resp.set_success(true); + // TODO: Obtain topics from the network service properly. + let topic = types::TopicBuilder::new("beacon_chain".to_string()).build(); + let message = PubsubMessage::Block(BlockRootSlot { + block_root, + slot: block.slot, + }); - let f = sink - .success(resp) - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - ctx.spawn(f) + println!("Sending beacon block to gossipsub"); + self.network_chan.send(NetworkMessage::Publish { + topics: vec![topic], + message, + }); + + // TODO: actually process the block. + let mut resp = PublishBeaconBlockResponse::new(); + resp.set_success(true); + + let f = sink + .success(resp) + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) + } + Err(e) => { + // + } + } } } diff --git a/protos/src/services.proto b/protos/src/services.proto index 80d512c54a..38c9d0b246 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -83,10 +83,7 @@ message PublishBeaconBlockResponse { } message BeaconBlock { - uint64 slot = 1; - bytes block_root = 2; - bytes randao_reveal = 3; - bytes signature = 4; + bytes ssz = 1; } /* diff --git a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs b/validator_client/src/block_producer_service/beacon_block_grpc_client.rs index 04a02a2213..ba2acfffbb 100644 --- a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs +++ b/validator_client/src/block_producer_service/beacon_block_grpc_client.rs @@ -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 { 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); From 56c9a295936c48b40707dfad596996aec0e17bf7 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 13:28:01 +1100 Subject: [PATCH 32/61] Add logging --- beacon_node/eth2-libp2p/src/behaviour.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 41b7c89657..592fd6fb1e 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -49,12 +49,15 @@ impl NetworkBehaviourEventProcess { + debug!(self.log, "Received GossipEvent"; "msg" => format!("{:?}", gs_msg)); + let pubsub_message = match PubsubMessage::ssz_decode(&gs_msg.data, 0) { //TODO: Punish peer on error Err(e) => { warn!( self.log, - "Received undecodable message from Peer {:?}", gs_msg.source + "Received undecodable message from Peer {:?} error", gs_msg.source; + "error" => format!("{:?}", e) ); return; } From e702896bee1784079f58eeab57966227e62fa3c7 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 13:28:21 +1100 Subject: [PATCH 33/61] Fix ssz decoding bug --- beacon_node/eth2-libp2p/src/behaviour.rs | 28 +++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 592fd6fb1e..2865971838 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -195,7 +195,7 @@ pub enum BehaviourEvent { } /// Messages that are passed to and from the pubsub (Gossipsub) behaviour. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum PubsubMessage { /// Gossipsub message providing notification of a new block. Block(BlockRootSlot), @@ -223,11 +223,11 @@ impl Decodable for PubsubMessage { fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> { let (id, index) = u32::ssz_decode(bytes, index)?; match id { - 1 => { + 0 => { let (block, index) = BlockRootSlot::ssz_decode(bytes, index)?; Ok((PubsubMessage::Block(block), index)) } - 2 => { + 1 => { let (attestation, index) = Attestation::ssz_decode(bytes, index)?; Ok((PubsubMessage::Attestation(attestation), index)) } @@ -235,3 +235,25 @@ impl Decodable for PubsubMessage { } } } + +#[cfg(test)] +mod test { + use super::*; + use types::*; + + #[test] + fn ssz_encoding() { + let original = PubsubMessage::Block(BlockRootSlot { + block_root: Hash256::from_slice(&[42; 32]), + slot: Slot::new(4), + }); + + let encoded = ssz_encode(&original); + + println!("{:?}", encoded); + + let (decoded, _i) = PubsubMessage::ssz_decode(&encoded, 0).unwrap(); + + assert_eq!(original, decoded); + } +} From 3756d8d6816ce37c85dd5f7ebf4ca1d62948902e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 15:04:39 +1100 Subject: [PATCH 34/61] Rename proto NodeInfo -> NodeInfoResponse --- beacon_node/rpc/src/beacon_node.rs | 6 +++--- protos/src/services.proto | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 7b00f04f48..54984c5c2a 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -1,7 +1,7 @@ use crate::beacon_chain::BeaconChain; use futures::Future; use grpcio::{RpcContext, UnarySink}; -use protos::services::{Empty, Fork, NodeInfo}; +use protos::services::{Empty, Fork, NodeInfoResponse}; use protos::services_grpc::BeaconNodeService; use slog::{trace, warn}; use std::sync::Arc; @@ -14,11 +14,11 @@ pub struct BeaconNodeServiceInstance { impl BeaconNodeService for BeaconNodeServiceInstance { /// Provides basic node information. - fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { + fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { trace!(self.log, "Node info requested via RPC"); // build the response - let mut node_info = NodeInfo::new(); + let mut node_info = NodeInfoResponse::new(); node_info.set_version(version::version()); // get the chain state diff --git a/protos/src/services.proto b/protos/src/services.proto index 38c9d0b246..ee4f58d1b0 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -14,7 +14,7 @@ package ethereum.beacon.rpc.v1; // Service that currently identifies a beacon node service BeaconNodeService { - rpc Info(Empty) returns (NodeInfo); + rpc Info(Empty) returns (NodeInfoResponse); } /// Service that handles block production @@ -40,7 +40,7 @@ service AttestationService { /* * Beacon Node Service Message */ -message NodeInfo { +message NodeInfoResponse { string version = 1; Fork fork = 2; uint32 chain_id = 3; @@ -53,8 +53,7 @@ message Fork { uint64 epoch = 3; } -message Empty { -} +message Empty {} /* From 0768d24ffc317be445e8e998db11368a4251ef0f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 15:26:05 +1100 Subject: [PATCH 35/61] Add untested block processing from GRPC --- beacon_node/rpc/src/beacon_block.rs | 107 +++++++++++++++++++++------- beacon_node/rpc/src/beacon_chain.rs | 13 +++- beacon_node/rpc/src/lib.rs | 1 + 3 files changed, 94 insertions(+), 27 deletions(-) diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 6317414b03..29a61766eb 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -1,3 +1,4 @@ +use crate::beacon_chain::BeaconChain; use crossbeam_channel; use eth2_libp2p::rpc::methods::BlockRootSlot; use eth2_libp2p::PubsubMessage; @@ -9,13 +10,15 @@ use protos::services::{ PublishBeaconBlockRequest, PublishBeaconBlockResponse, }; use protos::services_grpc::BeaconBlockService; -use slog::debug; use slog::Logger; +use slog::{debug, error, info, warn}; use ssz::{Decodable, TreeHash}; +use std::sync::Arc; use types::{BeaconBlock, Hash256, Slot}; #[derive(Clone)] pub struct BeaconBlockServiceInstance { + pub chain: Arc, pub network_chan: crossbeam_channel::Sender, pub log: Logger, } @@ -50,39 +53,91 @@ impl BeaconBlockService for BeaconBlockServiceInstance { req: PublishBeaconBlockRequest, sink: UnarySink, ) { - debug!(self.log, "PublishBeaconBlock"); + let mut resp = PublishBeaconBlockResponse::new(); - let block = req.get_block(); + let ssz_serialized_block = req.get_block().get_ssz(); - match BeaconBlock::ssz_decode(block.get_ssz(), 0) { + match BeaconBlock::ssz_decode(ssz_serialized_block, 0) { Ok((block, _i)) => { let block_root = Hash256::from_slice(&block.hash_tree_root()[..]); - // TODO: Obtain topics from the network service properly. - let topic = types::TopicBuilder::new("beacon_chain".to_string()).build(); - let message = PubsubMessage::Block(BlockRootSlot { - block_root, - slot: block.slot, - }); + match self.chain.process_block(block.clone()) { + Ok(outcome) => { + if outcome.sucessfully_processed() { + // Block was successfully processed. + info!( + self.log, + "PublishBeaconBlock"; + "type" => "invalid_block", + "outcome" => format!("{:?}", outcome) + ); - println!("Sending beacon block to gossipsub"); - self.network_chan.send(NetworkMessage::Publish { - topics: vec![topic], - message, - }); + // TODO: Obtain topics from the network service properly. + let topic = + types::TopicBuilder::new("beacon_chain".to_string()).build(); + let message = PubsubMessage::Block(BlockRootSlot { + block_root, + slot: block.slot, + }); + + println!("Sending beacon block to gossipsub"); + self.network_chan.send(NetworkMessage::Publish { + topics: vec![topic], + message, + }); + + resp.set_success(true); + } else if outcome.is_invalid() { + // Block was invalid. + warn!( + self.log, + "PublishBeaconBlock"; + "type" => "invalid_block", + "outcome" => format!("{:?}", outcome) + ); + + resp.set_success(false); + resp.set_msg( + format!("InvalidBlock: {:?}", outcome).as_bytes().to_vec(), + ); + } else { + // Some failure during processing. + error!( + self.log, + "PublishBeaconBlock"; + "type" => "other", + "outcome" => format!("{:?}", outcome) + ); + + resp.set_success(false); + resp.set_msg(format!("other: {:?}", outcome).as_bytes().to_vec()); + } + } + Err(e) => { + // Some failure during processing. + error!( + self.log, + "PublishBeaconBlock"; + "type" => "failed_to_process", + "error" => format!("{:?}", e) + ); + + resp.set_success(false); + resp.set_msg(format!("failed_to_process: {:?}", e).as_bytes().to_vec()); + } + } - // TODO: actually process the block. - let mut resp = PublishBeaconBlockResponse::new(); resp.set_success(true); + } + Err(_) => { + resp.set_success(false); + resp.set_msg(b"Invalid SSZ".to_vec()); + } + }; - let f = sink - .success(resp) - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - ctx.spawn(f) - } - Err(e) => { - // - } - } + let f = sink + .success(resp) + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) } } diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs index 9b26818767..0551a80246 100644 --- a/beacon_node/rpc/src/beacon_chain.rs +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -5,14 +5,18 @@ use beacon_chain::{ parking_lot::RwLockReadGuard, slot_clock::SlotClock, types::{BeaconState, ChainSpec}, - CheckPoint, }; +pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; +use types::BeaconBlock; /// The RPC's API to the beacon chain. pub trait BeaconChain: Send + Sync { fn get_spec(&self) -> &ChainSpec; fn get_state(&self) -> RwLockReadGuard; + + fn process_block(&self, block: BeaconBlock) + -> Result; } impl BeaconChain for RawBeaconChain @@ -28,4 +32,11 @@ where fn get_state(&self) -> RwLockReadGuard { self.state.read() } + + fn process_block( + &self, + block: BeaconBlock, + ) -> Result { + self.process_block(block) + } } diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 4dfd334879..289a81f3af 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -43,6 +43,7 @@ pub fn start_server( let beacon_block_service = { let instance = BeaconBlockServiceInstance { + chain: beacon_chain.clone(), network_chan, log: log.clone(), }; From 00b546e6b8c120f6629de3261282c77fae170665 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 15:44:28 +1100 Subject: [PATCH 36/61] Update slot clock to use genesis slot --- beacon_node/beacon_chain/src/initialise.rs | 16 ++++++++++++---- beacon_node/rpc/src/beacon_node.rs | 1 + .../slot_clock/src/system_time_slot_clock.rs | 11 ++++++++++- protos/src/services.proto | 1 + validator_client/src/service.rs | 6 ++++-- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/beacon_node/beacon_chain/src/initialise.rs b/beacon_node/beacon_chain/src/initialise.rs index 7d3c87965f..58b3aee84c 100644 --- a/beacon_node/beacon_chain/src/initialise.rs +++ b/beacon_node/beacon_chain/src/initialise.rs @@ -35,8 +35,12 @@ pub fn initialise_beacon_chain( genesis_block.state_root = Hash256::from_slice(&genesis_state.hash_tree_root()); // Slot clock - let slot_clock = SystemTimeSlotClock::new(genesis_state.genesis_time, spec.seconds_per_slot) - .expect("Unable to load SystemTimeSlotClock"); + let slot_clock = SystemTimeSlotClock::new( + spec.genesis_slot, + genesis_state.genesis_time, + spec.seconds_per_slot, + ) + .expect("Unable to load SystemTimeSlotClock"); // Choose the fork choice let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); @@ -72,8 +76,12 @@ pub fn initialise_test_beacon_chain( genesis_block.state_root = Hash256::from_slice(&genesis_state.hash_tree_root()); // Slot clock - let slot_clock = SystemTimeSlotClock::new(genesis_state.genesis_time, spec.seconds_per_slot) - .expect("Unable to load SystemTimeSlotClock"); + let slot_clock = SystemTimeSlotClock::new( + spec.genesis_slot, + genesis_state.genesis_time, + spec.seconds_per_slot, + ) + .expect("Unable to load SystemTimeSlotClock"); // Choose the fork choice let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 54984c5c2a..c92ab66162 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -34,6 +34,7 @@ impl BeaconNodeService for BeaconNodeServiceInstance { node_info.set_fork(fork); node_info.set_genesis_time(genesis_time); + node_info.set_genesis_slot(self.chain.get_spec().genesis_slot.as_u64()); node_info.set_chain_id(self.chain.get_spec().chain_id as u32); // send the node_info the requester diff --git a/eth2/utils/slot_clock/src/system_time_slot_clock.rs b/eth2/utils/slot_clock/src/system_time_slot_clock.rs index 99f051985f..816180d6e6 100644 --- a/eth2/utils/slot_clock/src/system_time_slot_clock.rs +++ b/eth2/utils/slot_clock/src/system_time_slot_clock.rs @@ -13,6 +13,7 @@ pub enum Error { /// Determines the present slot based upon the present system time. #[derive(Clone)] pub struct SystemTimeSlotClock { + genesis_slot: Slot, genesis_seconds: u64, slot_duration_seconds: u64, } @@ -22,6 +23,7 @@ impl SystemTimeSlotClock { /// /// Returns an Error if `slot_duration_seconds == 0`. pub fn new( + genesis_slot: Slot, genesis_seconds: u64, slot_duration_seconds: u64, ) -> Result { @@ -29,6 +31,7 @@ impl SystemTimeSlotClock { Err(Error::SlotDurationIsZero) } else { Ok(Self { + genesis_slot, genesis_seconds, slot_duration_seconds, }) @@ -44,9 +47,11 @@ impl SlotClock for SystemTimeSlotClock { let duration_since_epoch = syslot_time.duration_since(SystemTime::UNIX_EPOCH)?; let duration_since_genesis = duration_since_epoch.checked_sub(Duration::from_secs(self.genesis_seconds)); + match duration_since_genesis { None => Ok(None), - Some(d) => Ok(slot_from_duration(self.slot_duration_seconds, d)), + Some(d) => Ok(slot_from_duration(self.slot_duration_seconds, d) + .and_then(|s| Some(s + self.genesis_slot))), } } } @@ -74,6 +79,7 @@ mod tests { #[test] fn test_slot_now() { let slot_time = 100; + let genesis_slot = Slot::new(0); let now = SystemTime::now(); let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap(); @@ -81,18 +87,21 @@ mod tests { let genesis = since_epoch.as_secs() - slot_time * 89; let clock = SystemTimeSlotClock { + genesis_slot, genesis_seconds: genesis, slot_duration_seconds: slot_time, }; assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(89))); let clock = SystemTimeSlotClock { + genesis_slot, genesis_seconds: since_epoch.as_secs(), slot_duration_seconds: slot_time, }; assert_eq!(clock.present_slot().unwrap(), Some(Slot::new(0))); let clock = SystemTimeSlotClock { + genesis_slot, genesis_seconds: since_epoch.as_secs() - slot_time * 42 - 5, slot_duration_seconds: slot_time, }; diff --git a/protos/src/services.proto b/protos/src/services.proto index ee4f58d1b0..6b2afceebb 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -45,6 +45,7 @@ message NodeInfoResponse { Fork fork = 2; uint32 chain_id = 3; uint64 genesis_time = 4; + uint64 genesis_slot = 5; } message Fork { diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 8a7e90d104..eb0771049b 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -101,6 +101,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); @@ -117,8 +118,9 @@ impl Service { // build the validator slot clock let slot_clock = { - let clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot) - .expect("Unable to instantiate SystemTimeSlotClock."); + let clock = + SystemTimeSlotClock::new(genesis_slot, genesis_time, config.spec.seconds_per_slot) + .expect("Unable to instantiate SystemTimeSlotClock."); Arc::new(clock) }; From 9224558e060c0982908104aed29bde9fead539de Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 15:53:49 +1100 Subject: [PATCH 37/61] Set genesis time for testing state builder --- eth2/types/src/test_utils/testing_beacon_state_builder.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eth2/types/src/test_utils/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs index 473cd4166f..fcdbb00ae1 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -120,8 +120,10 @@ impl TestingBeaconStateBuilder { }) .collect(); + let genesis_time = 1549929600; // 12/2/2019 (arbitrary) + let mut state = BeaconState::genesis( - 0, + genesis_time, Eth1Data { deposit_root: Hash256::zero(), block_hash: Hash256::zero(), From 7c31c052f3589a7930a2e7277b23a43ca9aaad28 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Tue, 26 Mar 2019 15:59:00 +1100 Subject: [PATCH 38/61] Temp commit - Re-building validator RPC API --- protos/src/services.proto | 29 ++++++++++++++++++--------- validator_client/src/duties/grpc.rs | 12 ++++++----- validator_client/src/duties/mod.rs | 19 ++++++------------ validator_client/src/duties/traits.rs | 5 ++--- validator_client/src/service.rs | 17 ++++++++-------- 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/protos/src/services.proto b/protos/src/services.proto index 80d512c54a..bcfd353c7d 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -27,7 +27,8 @@ service BeaconBlockService { //its public keys service ValidatorService { rpc ProposeBlockSlot(ProposeBlockSlotRequest) returns (ProposeBlockSlotResponse); - rpc ValidatorIndex(PublicKey) returns (IndexResponse); + /// Given a set of public keys, returns their respective indicies + rpc ValidatorIndex(PublicKeys) returns (Indicies); // rpc ValidatorAssignment(ValidatorAssignmentRequest) returns (ValidatorAssignmentResponse); } @@ -110,12 +111,12 @@ message ValidatorAssignment { // Validator Assignment -message PublicKey { - bytes public_key = 1; +message PublicKeys { + repeated bytes public_key = 1; } -message IndexResponse { - uint64 index = 1; +message Indicies { + repeated uint64 index = 1; } @@ -123,16 +124,26 @@ message IndexResponse { message ProposeBlockSlotRequest { uint64 epoch = 1; - uint64 validator_index = 2; + repeated uint64 validator_index = 2; } -message ProposeBlockSlotResponse { - oneof slot_oneof { +message GetDutiesResponse { + repeated oneof slot_oneof { bool none = 1; - uint64 slot = 2; + ValidatorDuty duty = 2; } } +ValidatorDuty { + oneof block_oneof { + bool none = 1; + uint64 block_produce_slot = 2; + } + uint64 committee_slot = 1; + uint64 committee_shard = 2; + uint64 committee_index = 3; +} + /* * Attestation Service Messages diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 94f843b639..1f7297f0e6 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -1,6 +1,6 @@ use super::traits::{BeaconNode, BeaconNodeError}; use super::EpochDuties; -use protos::services::{ProposeBlockSlotRequest, PublicKey as IndexRequest}; +use protos::services::{ProposeBlockSlotRequest, PublicKeys as IndexRequest}; use protos::services_grpc::ValidatorServiceClient; use ssz::ssz_encode; use types::{Epoch, PublicKey, Slot}; @@ -15,12 +15,14 @@ impl BeaconNode for ValidatorServiceClient { fn request_shuffling( &self, epoch: Epoch, - public_key: &PublicKey, + pubkeys: &[PublicKey], ) -> Result, BeaconNodeError> { - // Lookup the validator index for the supplied public key. - let validator_index = { + // Lookup the validator indexes for all the supplied public keys. + let validator_indices = { let mut req = IndexRequest::new(); - req.set_public_key(ssz_encode(public_key).to_vec()); + for public_key in pubkeys { + req.mut_public_key().push(ssz_encode(public_key)); + } let resp = self .validator_index(&req) .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 185f80d6df..77d6d43b60 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -10,9 +10,8 @@ use self::traits::{BeaconNode, BeaconNodeError}; use bls::PublicKey; use futures::Async; use slog::{debug, error, info}; -use slot_clock::SlotClock; use std::sync::Arc; -use types::{ChainSpec, Epoch, Slot}; +use types::{Epoch, Slot}; #[derive(Debug, PartialEq, Clone, Copy)] pub enum UpdateOutcome { @@ -30,8 +29,6 @@ pub enum UpdateOutcome { #[derive(Debug, PartialEq)] pub enum Error { - SlotClockError, - SlotUnknowable, EpochMapPoisoned, BeaconNodeError(BeaconNodeError), } @@ -40,27 +37,23 @@ pub enum Error { /// Node. /// /// This keeps track of all validator keys and required voting slots. -pub struct DutiesManager { +pub struct DutiesManager { pub duties_map: Arc, /// A list of all public keys known to the validator service. pub pubkeys: Vec, - pub spec: Arc, - pub slot_clock: Arc, + pub slots_per_epoch: u64, pub beacon_node: Arc, } -impl DutiesManager { +impl DutiesManager { /// Check the Beacon Node for `EpochDuties`. /// /// The present `epoch` will be learned from the supplied `SlotClock`. In production this will /// be a wall-clock (e.g., system time, remote server time, etc.). fn update(&self, slot: Slot) -> Result { - let epoch = slot.epoch(self.spec.slots_per_epoch); + let epoch = slot.epoch(self.slots_per_epoch); - if let Some(duties) = self - .beacon_node - .request_shuffling(epoch, &self.pubkeys[0])? - { + if let Some(duties) = self.beacon_node.request_shuffling(epoch, &self.pubkeys)? { // If these duties were known, check to see if they're updates or identical. let result = if let Some(known_duties) = self.duties_map.get(epoch)? { if known_duties == duties { diff --git a/validator_client/src/duties/traits.rs b/validator_client/src/duties/traits.rs index 5bf7da1fdd..a3dcade4b7 100644 --- a/validator_client/src/duties/traits.rs +++ b/validator_client/src/duties/traits.rs @@ -1,6 +1,5 @@ use super::EpochDuties; -use bls::PublicKey; -use types::Epoch; +use types::{Epoch, PublicKey}; #[derive(Debug, PartialEq, Clone)] pub enum BeaconNodeError { @@ -15,6 +14,6 @@ pub trait BeaconNode: Send + Sync { fn request_shuffling( &self, epoch: Epoch, - public_key: &PublicKey, + pubkeys: &[PublicKey], ) -> Result, BeaconNodeError>; } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 9ca591b926..e322712996 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -222,11 +222,11 @@ impl Service { // build requisite objects to pass to core thread. let duties_map = Arc::new(EpochDutiesMap::new(config.spec.slots_per_epoch)); let epoch_map_for_attester = Arc::new(EpochMap::new(config.spec.slots_per_epoch)); + let manager = Arc::new(DutiesManager { duties_map, pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(), - spec: Arc::new(config.spec), - slot_clock: service.slot_clock.clone(), + slots_per_epoch: config.spec.slots_per_epoch.clone(), beacon_node: service.validator_client.clone(), }); @@ -234,6 +234,7 @@ impl Service { runtime .block_on(interval.for_each(move |_| { let log = service.log.clone(); + // get the current slot let current_slot = match service.slot_clock.present_slot() { Err(e) => { @@ -250,24 +251,24 @@ impl Service { info!(log, "Processing slot: {}", current_slot.as_u64()); - let cloned_manager = manager.clone(); - // check for new duties + let cloned_manager = manager.clone(); tokio::spawn(futures::future::poll_fn(move || { cloned_manager.run_update(current_slot.clone(), log.clone()) })); + + // execute any specified duties + Ok(()) })) .map_err(|e| format!("Service thread failed: {:?}", e))?; + + // completed a slot process Ok(()) } /* - let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch)); - let epoch_map_for_attester = Arc::new(EpochMap::new(spec.slots_per_epoch)); - - for keypair in keypairs { info!(self.log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); From c27fdbe37fbceab145e6b4e99844705a7e630c47 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 10:28:27 +1100 Subject: [PATCH 39/61] Try to load keys from file when starting client --- beacon_node/beacon_chain/src/initialise.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/src/initialise.rs b/beacon_node/beacon_chain/src/initialise.rs index 58b3aee84c..0951e06fbb 100644 --- a/beacon_node/beacon_chain/src/initialise.rs +++ b/beacon_node/beacon_chain/src/initialise.rs @@ -28,7 +28,7 @@ pub fn initialise_beacon_chain( let block_store = Arc::new(BeaconBlockStore::new(db.clone())); let state_store = Arc::new(BeaconStateStore::new(db.clone())); - let state_builder = TestingBeaconStateBuilder::from_deterministic_keypairs(8, &spec); + let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, &spec); let (genesis_state, _keypairs) = state_builder.build(); let mut genesis_block = BeaconBlock::empty(&spec); @@ -69,7 +69,7 @@ pub fn initialise_test_beacon_chain( let block_store = Arc::new(BeaconBlockStore::new(db.clone())); let state_store = Arc::new(BeaconStateStore::new(db.clone())); - let state_builder = TestingBeaconStateBuilder::from_deterministic_keypairs(8, spec); + let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec); let (genesis_state, _keypairs) = state_builder.build(); let mut genesis_block = BeaconBlock::empty(spec); From c82bad7602ba8257a432cba9ab4ba05859d0af9e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 10:34:52 +1100 Subject: [PATCH 40/61] Add cache builds when advancing chain slot --- beacon_node/beacon_chain/src/beacon_chain.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index f0290d9391..2caee2f988 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -342,6 +342,10 @@ where // If required, transition the new state to the present slot. for _ in state.slot.as_u64()..present_slot.as_u64() { + // Ensure the next epoch state caches are built in case of an epoch transition. + state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, &self.spec)?; + state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, &self.spec)?; + per_slot_processing(&mut *state, &latest_block_header, &self.spec)?; } From b88750960736f94b3b20f1d71dcbdf634debd798 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 10:35:21 +1100 Subject: [PATCH 41/61] Fix double advance-cache calls bug --- eth2/state_processing/src/per_slot_processing.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/eth2/state_processing/src/per_slot_processing.rs b/eth2/state_processing/src/per_slot_processing.rs index 8f02b70e3a..c6b5312c70 100644 --- a/eth2/state_processing/src/per_slot_processing.rs +++ b/eth2/state_processing/src/per_slot_processing.rs @@ -20,7 +20,6 @@ pub fn per_slot_processing( if (state.slot + 1) % spec.slots_per_epoch == 0 { per_epoch_processing(state, spec)?; - state.advance_caches(); } state.slot += 1; From b006586d19d44d40882747d10f5b04346b9f0895 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 10:36:20 +1100 Subject: [PATCH 42/61] Add slot timer to beacon node --- beacon_node/Cargo.toml | 1 + beacon_node/beacon_chain/src/beacon_chain.rs | 14 ++++ beacon_node/client/Cargo.toml | 1 + beacon_node/client/src/lib.rs | 76 ++++++++++++++++++- beacon_node/client/src/notifier.rs | 4 +- beacon_node/src/run.rs | 2 + eth2/utils/slot_clock/src/lib.rs | 3 + .../slot_clock/src/system_time_slot_clock.rs | 28 +++++++ .../slot_clock/src/testing_slot_clock.rs | 6 ++ 9 files changed, 132 insertions(+), 3 deletions(-) diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index e7aaf938de..a090c1cc54 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -14,6 +14,7 @@ slog-term = "^2.4.0" slog-async = "^2.3.0" ctrlc = { version = "3.1.1", features = ["termination"] } tokio = "0.1.15" +tokio-timer = "0.2.10" futures = "0.1.25" exit-future = "0.1.3" state_processing = { path = "../eth2/state_processing" } diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 2caee2f988..6fcb3fde40 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -409,6 +409,20 @@ where } } + /// Reads the slot clock (see `self.read_slot_clock()` and returns the number of slots since + /// genesis. + pub fn slots_since_genesis(&self) -> Option { + let now = self.read_slot_clock()?; + + if now < self.spec.genesis_slot { + None + } else { + Some(SlotHeight::from( + now.as_u64() - self.spec.genesis_slot.as_u64(), + )) + } + } + /// Returns slot of the present state. /// /// This is distinct to `read_slot_clock`, which reads from the actual system clock. If diff --git a/beacon_node/client/Cargo.toml b/beacon_node/client/Cargo.toml index 12c1b5c802..8956dbb07d 100644 --- a/beacon_node/client/Cargo.toml +++ b/beacon_node/client/Cargo.toml @@ -14,6 +14,7 @@ types = { path = "../../eth2/types" } slot_clock = { path = "../../eth2/utils/slot_clock" } error-chain = "0.12.0" slog = "^2.2.3" +ssz = { path = "../../eth2/utils/ssz" } tokio = "0.1.15" clap = "2.32.0" dirs = "1.0.3" diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index b24d2cb7f2..2ce181aa19 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -9,11 +9,17 @@ use beacon_chain::BeaconChain; pub use client_config::ClientConfig; pub use client_types::ClientTypes; use exit_future::Signal; +use futures::{future::Future, Stream}; use network::Service as NetworkService; -use slog::o; +use slog::{error, info, o}; +use slot_clock::SlotClock; +use ssz::TreeHash; use std::marker::PhantomData; use std::sync::Arc; +use std::time::{Duration, Instant}; use tokio::runtime::TaskExecutor; +use tokio::timer::Interval; +use types::Hash256; /// Main beacon node client service. This provides the connection and initialisation of the clients /// sub-services in multiple threads. @@ -26,6 +32,8 @@ pub struct Client { pub network: Arc, /// Signal to terminate the RPC server. pub rpc_exit_signal: Option, + /// Signal to terminate the slot timer. + pub slot_timer_exit_signal: Option, /// The clients logger. log: slog::Logger, /// Marker to pin the beacon chain generics. @@ -42,6 +50,17 @@ impl Client { // generate a beacon chain let beacon_chain = TClientType::initialise_beacon_chain(&config); + { + let state = beacon_chain.state.read(); + let state_root = Hash256::from_slice(&state.hash_tree_root()); + info!( + log, + "ChainInitialized"; + "state_root" => format!("{}", state_root), + "genesis_time" => format!("{}", state.genesis_time), + ); + } + // Start the network service, libp2p and syncing threads // TODO: Add beacon_chain reference to network parameters let network_config = &config.net_conf; @@ -65,10 +84,65 @@ impl Client { )); } + let (slot_timer_exit_signal, exit) = exit_future::signal(); + if let Ok(Some(duration_to_next_slot)) = beacon_chain.slot_clock.duration_to_next_slot() { + // 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() + duration_to_next_slot, slot_duration) + }; + + let chain = beacon_chain.clone(); + let log = log.new(o!("Service" => "SlotTimer")); + + let state_slot = chain.state.read().slot; + let wall_clock_slot = chain.read_slot_clock().unwrap(); + let slots_since_genesis = chain.slots_since_genesis().unwrap(); + info!( + log, + "Starting SlotTimer"; + "state_slot" => state_slot, + "wall_clock_slot" => wall_clock_slot, + "slots_since_genesis" => slots_since_genesis, + "catchup_distance" => wall_clock_slot - state_slot, + ); + executor.spawn( + exit.until( + interval + .for_each(move |_| { + if let Some(genesis_height) = chain.slots_since_genesis() { + match chain.catchup_state() { + Ok(_) => info!( + log, + "NewSlot"; + "slot" => chain.state.read().slot, + "slots_since_genesis" => genesis_height, + ), + Err(e) => error!( + log, + "StateCatchupFailed"; + "state_slot" => chain.state.read().slot, + "slots_since_genesis" => genesis_height, + "error" => format!("{:?}", e), + ), + }; + } + + Ok(()) + }) + .map_err(|_| ()), + ) + .map(|_| ()), + ); + } + Ok(Client { config, beacon_chain, rpc_exit_signal, + slot_timer_exit_signal: Some(slot_timer_exit_signal), log, network, phantom: PhantomData, diff --git a/beacon_node/client/src/notifier.rs b/beacon_node/client/src/notifier.rs index 335183c7de..91a9f3a261 100644 --- a/beacon_node/client/src/notifier.rs +++ b/beacon_node/client/src/notifier.rs @@ -2,7 +2,7 @@ use crate::Client; use crate::ClientTypes; use exit_future::Exit; use futures::{Future, Stream}; -use slog::{debug, info, o}; +use slog::{debug, o}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; use tokio::runtime::TaskExecutor; @@ -22,7 +22,7 @@ pub fn run(client: &Client, executor: TaskExecutor, exit: Exi // build heartbeat logic here let heartbeat = move |_| { - info!(log, "Temp heartbeat output"); + debug!(log, "Temp heartbeat output"); //TODO: Remove this logic. Testing only let mut count = counter.lock().unwrap(); *count += 1; diff --git a/beacon_node/src/run.rs b/beacon_node/src/run.rs index b3b2844526..1d9156124f 100644 --- a/beacon_node/src/run.rs +++ b/beacon_node/src/run.rs @@ -6,10 +6,12 @@ use futures::Future; use slog::info; use std::cell::RefCell; use tokio::runtime::Builder; +use tokio_timer::clock::Clock; pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Result<()> { let mut runtime = Builder::new() .name_prefix("main-") + .clock(Clock::system()) .build() .map_err(|e| format!("{:?}", e))?; diff --git a/eth2/utils/slot_clock/src/lib.rs b/eth2/utils/slot_clock/src/lib.rs index 0379d50d9a..fd5a2d1d7d 100644 --- a/eth2/utils/slot_clock/src/lib.rs +++ b/eth2/utils/slot_clock/src/lib.rs @@ -3,10 +3,13 @@ mod testing_slot_clock; pub use crate::system_time_slot_clock::{Error as SystemTimeSlotClockError, SystemTimeSlotClock}; pub use crate::testing_slot_clock::{Error as TestingSlotClockError, TestingSlotClock}; +use std::time::Duration; pub use types::Slot; pub trait SlotClock: Send + Sync { type Error; fn present_slot(&self) -> Result, Self::Error>; + + fn duration_to_next_slot(&self) -> Result, Self::Error>; } diff --git a/eth2/utils/slot_clock/src/system_time_slot_clock.rs b/eth2/utils/slot_clock/src/system_time_slot_clock.rs index 816180d6e6..4dfc6b37da 100644 --- a/eth2/utils/slot_clock/src/system_time_slot_clock.rs +++ b/eth2/utils/slot_clock/src/system_time_slot_clock.rs @@ -54,6 +54,10 @@ impl SlotClock for SystemTimeSlotClock { .and_then(|s| Some(s + self.genesis_slot))), } } + + fn duration_to_next_slot(&self) -> Result, Error> { + duration_to_next_slot(self.genesis_seconds, self.slot_duration_seconds) + } } impl From for Error { @@ -67,6 +71,30 @@ fn slot_from_duration(slot_duration_seconds: u64, duration: Duration) -> Option< duration.as_secs().checked_div(slot_duration_seconds)?, )) } +// calculate the duration to the next slot +fn duration_to_next_slot( + genesis_time: u64, + seconds_per_slot: u64, +) -> Result, Error> { + let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; + let genesis_time = Duration::from_secs(genesis_time); + + if now < genesis_time { + return Ok(None); + } + + let since_genesis = now - genesis_time; + + let elapsed_slots = since_genesis.as_secs() / seconds_per_slot; + + let next_slot_start_seconds = (elapsed_slots + 1) + .checked_mul(seconds_per_slot) + .expect("Next slot time should not overflow u64"); + + let time_to_next_slot = Duration::from_secs(next_slot_start_seconds) - since_genesis; + + Ok(Some(time_to_next_slot)) +} #[cfg(test)] mod tests { diff --git a/eth2/utils/slot_clock/src/testing_slot_clock.rs b/eth2/utils/slot_clock/src/testing_slot_clock.rs index 80ee405397..b5c36dfa0a 100644 --- a/eth2/utils/slot_clock/src/testing_slot_clock.rs +++ b/eth2/utils/slot_clock/src/testing_slot_clock.rs @@ -1,5 +1,6 @@ use super::SlotClock; use std::sync::RwLock; +use std::time::Duration; use types::Slot; #[derive(Debug, PartialEq)] @@ -32,6 +33,11 @@ impl SlotClock for TestingSlotClock { let slot = *self.slot.read().expect("TestingSlotClock poisoned."); Ok(Some(Slot::new(slot))) } + + /// Always returns a duration of 1 second. + fn duration_to_next_slot(&self) -> Result, Error> { + Ok(Some(Duration::from_secs(1))) + } } #[cfg(test)] From 8733740f8b08ff44d30977a4b089fd3eec471b85 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 10:36:37 +1100 Subject: [PATCH 43/61] Move genesis time closer to now --- eth2/types/src/test_utils/testing_beacon_state_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth2/types/src/test_utils/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs index fcdbb00ae1..493f234a2e 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -120,7 +120,7 @@ impl TestingBeaconStateBuilder { }) .collect(); - let genesis_time = 1549929600; // 12/2/2019 (arbitrary) + let genesis_time = 1553638359; // arbitrary let mut state = BeaconState::genesis( genesis_time, From f18941a01c21e4854ca9342e5bcd084ccfd37af6 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 11:25:15 +1100 Subject: [PATCH 44/61] Block client startup until state is ready --- beacon_node/client/src/lib.rs | 93 ++++++++++++++++++----------- beacon_node/rpc/src/beacon_block.rs | 7 ++- 2 files changed, 63 insertions(+), 37 deletions(-) diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 2ce181aa19..807fd9301e 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -8,7 +8,9 @@ pub mod notifier; use beacon_chain::BeaconChain; pub use client_config::ClientConfig; pub use client_types::ClientTypes; +use db::ClientDB; use exit_future::Signal; +use fork_choice::ForkChoice; use futures::{future::Future, Stream}; use network::Service as NetworkService; use slog::{error, info, o}; @@ -50,16 +52,34 @@ impl Client { // generate a beacon chain let beacon_chain = TClientType::initialise_beacon_chain(&config); + if beacon_chain.read_slot_clock().is_none() { + panic!("Cannot start client before genesis!") + } + + // Block starting the client until we have caught the state up to the current slot. + // + // If we don't block here we create an initial scenario where we're unable to process any + // blocks and we're basically useless. { - let state = beacon_chain.state.read(); - let state_root = Hash256::from_slice(&state.hash_tree_root()); + let state_slot = beacon_chain.state.read().slot; + let wall_clock_slot = beacon_chain.read_slot_clock().unwrap(); + let slots_since_genesis = beacon_chain.slots_since_genesis().unwrap(); info!( log, - "ChainInitialized"; - "state_root" => format!("{}", state_root), - "genesis_time" => format!("{}", state.genesis_time), + "Initializing state"; + "state_slot" => state_slot, + "wall_clock_slot" => wall_clock_slot, + "slots_since_genesis" => slots_since_genesis, + "catchup_distance" => wall_clock_slot - state_slot, ); } + do_state_catchup(&beacon_chain, &log); + info!( + log, + "State initialized"; + "state_slot" => beacon_chain.state.read().slot, + "wall_clock_slot" => beacon_chain.read_slot_clock().unwrap(), + ); // Start the network service, libp2p and syncing threads // TODO: Add beacon_chain reference to network parameters @@ -96,39 +116,11 @@ impl Client { let chain = beacon_chain.clone(); let log = log.new(o!("Service" => "SlotTimer")); - - let state_slot = chain.state.read().slot; - let wall_clock_slot = chain.read_slot_clock().unwrap(); - let slots_since_genesis = chain.slots_since_genesis().unwrap(); - info!( - log, - "Starting SlotTimer"; - "state_slot" => state_slot, - "wall_clock_slot" => wall_clock_slot, - "slots_since_genesis" => slots_since_genesis, - "catchup_distance" => wall_clock_slot - state_slot, - ); executor.spawn( exit.until( interval .for_each(move |_| { - if let Some(genesis_height) = chain.slots_since_genesis() { - match chain.catchup_state() { - Ok(_) => info!( - log, - "NewSlot"; - "slot" => chain.state.read().slot, - "slots_since_genesis" => genesis_height, - ), - Err(e) => error!( - log, - "StateCatchupFailed"; - "state_slot" => chain.state.read().slot, - "slots_since_genesis" => genesis_height, - "error" => format!("{:?}", e), - ), - }; - } + do_state_catchup(&chain, &log); Ok(()) }) @@ -149,3 +141,36 @@ impl Client { }) } } + +fn do_state_catchup(chain: &Arc>, log: &slog::Logger) +where + T: ClientDB, + U: SlotClock, + F: ForkChoice, +{ + if let Some(genesis_height) = chain.slots_since_genesis() { + let result = chain.catchup_state(); + + let common = o!( + "best_slot" => chain.head().beacon_block.slot, + "latest_block_root" => format!("{}", chain.head().beacon_block_root), + "wall_clock_slot" => chain.read_slot_clock().unwrap(), + "state_slot" => chain.state.read().slot, + "slots_since_genesis" => genesis_height, + ); + + match result { + Ok(_) => info!( + log, + "NewSlot"; + common + ), + Err(e) => error!( + log, + "StateCatchupFailed"; + "error" => format!("{:?}", e), + common + ), + }; + } +} diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 29a61766eb..f6b426c18f 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -68,7 +68,8 @@ impl BeaconBlockService for BeaconBlockServiceInstance { info!( self.log, "PublishBeaconBlock"; - "type" => "invalid_block", + "type" => "valid_block", + "block_slot" => block.slot, "outcome" => format!("{:?}", outcome) ); @@ -102,10 +103,10 @@ impl BeaconBlockService for BeaconBlockServiceInstance { ); } else { // Some failure during processing. - error!( + warn!( self.log, "PublishBeaconBlock"; - "type" => "other", + "type" => "unable_to_import", "outcome" => format!("{:?}", outcome) ); From 0c4306cd1876a3d5375510fb25440408d4d87028 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 11:57:38 +1100 Subject: [PATCH 45/61] Move genesis time forward --- eth2/types/src/test_utils/testing_beacon_state_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth2/types/src/test_utils/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs index 493f234a2e..3951820d86 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -120,7 +120,7 @@ impl TestingBeaconStateBuilder { }) .collect(); - let genesis_time = 1553638359; // arbitrary + let genesis_time = 1553647464; // arbitrary let mut state = BeaconState::genesis( genesis_time, From 12936e73a777abc1d876747b0b5dd1248a55c1db Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 12:57:05 +1100 Subject: [PATCH 46/61] Set gossip logs to `info` --- beacon_node/network/src/sync/simple_sync.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 2aa0a1d7dd..85949fa98e 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -523,9 +523,9 @@ impl SimpleSync { msg: BlockRootSlot, network: &mut NetworkContext, ) { - debug!( + info!( self.log, - "BlockSlot"; + "NewGossipBlock"; "peer" => format!("{:?}", peer_id), ); // TODO: filter out messages that a prior to the finalized slot. @@ -557,9 +557,9 @@ impl SimpleSync { msg: Attestation, _network: &mut NetworkContext, ) { - debug!( + info!( self.log, - "Attestation"; + "NewAttestationGossip"; "peer" => format!("{:?}", peer_id), ); From bc8ec51fe5235da040f4dbf608b152b776331abd Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 15:41:51 +1100 Subject: [PATCH 47/61] Update EpochDuty RPC and core functionality --- protos/src/services.proto | 50 +++++-------- validator_client/src/duties/epoch_duties.rs | 30 +++++--- validator_client/src/duties/grpc.rs | 78 ++++++++++----------- validator_client/src/duties/traits.rs | 9 +-- 4 files changed, 79 insertions(+), 88 deletions(-) diff --git a/protos/src/services.proto b/protos/src/services.proto index bcfd353c7d..dea9b7a379 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -26,10 +26,9 @@ service BeaconBlockService { /// Service that provides the validator client with requisite knowledge about //its public keys service ValidatorService { - rpc ProposeBlockSlot(ProposeBlockSlotRequest) returns (ProposeBlockSlotResponse); - /// Given a set of public keys, returns their respective indicies - rpc ValidatorIndex(PublicKeys) returns (Indicies); - // rpc ValidatorAssignment(ValidatorAssignmentRequest) returns (ValidatorAssignmentResponse); + // Gets the block proposer slot and comittee slot that a validator needs to + // perform work on. + rpc GetValidatorDuties(GetDutiesRequest) returns (GetDutiesResponse); } /// Service that handles validator attestations @@ -93,58 +92,41 @@ message BeaconBlock { /* * Validator Service Messages */ -/* -message ValidatorAssignmentRequest { - uint64 epoch = 1; - bytes validator_index = 2; -} - -// A validators duties for some epoch. -// TODO: add shard duties. -message ValidatorAssignment { - oneof block_production_slot_oneof { - bool block_production_slot_none = 1; - uint64 block_production_slot = 2; - } -} -*/ // Validator Assignment -message PublicKeys { +// the public keys of the validators +message Validators { repeated bytes public_key = 1; } -message Indicies { - repeated uint64 index = 1; -} - - // Propose slot - -message ProposeBlockSlotRequest { +message GetDutiesRequest { uint64 epoch = 1; - repeated uint64 validator_index = 2; + Validators validators = 2; } message GetDutiesResponse { - repeated oneof slot_oneof { + repeated ActiveValidator active_validator = 1; +} + +message ActiveValidator { + oneof slot_oneof { bool none = 1; ValidatorDuty duty = 2; } } -ValidatorDuty { +message ValidatorDuty { oneof block_oneof { bool none = 1; uint64 block_produce_slot = 2; } - uint64 committee_slot = 1; - uint64 committee_shard = 2; - uint64 committee_index = 3; + uint64 committee_slot = 3; + uint64 committee_shard = 4; + uint64 committee_index = 5; } - /* * Attestation Service Messages */ diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 71f5f26ab5..41d3a24c22 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -1,30 +1,40 @@ use block_proposer::{DutiesReader, DutiesReaderError}; use std::collections::HashMap; use std::sync::RwLock; -use types::{Epoch, Fork, Slot}; +use types::{Epoch, Fork, PublicKey, Slot}; /// The information required for a validator to propose and attest during some epoch. /// /// Generally obtained from a Beacon Node, this information contains the validators canonical index -/// (thier sequence in the global validator induction process) and the "shuffling" for that index +/// (their sequence in the global validator induction process) and the "shuffling" for that index /// for some epoch. #[derive(Debug, PartialEq, Clone, Copy, Default)] -pub struct EpochDuties { - pub validator_index: u64, +pub struct EpochDuty { pub block_production_slot: Option, - // Future shard info + pub committee_slot: Slot, + pub committee_shard: u64, + pub committee_index: u64, } -impl EpochDuties { - /// Returns `true` if the supplied `slot` is a slot in which the validator should produce a - /// block. - pub fn is_block_production_slot(&self, slot: Slot) -> bool { +impl EpochDuty { + /// Returns `true` if work needs to be done in the supplied `slot` + pub fn is_work_slot(&self, slot: Slot) -> bool { + // if validator is required to produce a slot return true match self.block_production_slot { - Some(s) if s == slot => true, + Some(s) if s == slot => return true, _ => false, } + + if self.committee_slot == slot { + return true; + } + return false; } } +/// Maps a list of public keys (many validators) to an EpochDuty. +pub struct EpochDuties { + inner: HashMap>, +} pub enum EpochDutiesMapError { Poisoned, diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 1f7297f0e6..91da512e37 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -1,56 +1,54 @@ +use super::epoch_duties::{EpochDuties, EpochDuty}; use super::traits::{BeaconNode, BeaconNodeError}; -use super::EpochDuties; -use protos::services::{ProposeBlockSlotRequest, PublicKeys as IndexRequest}; +use protos::services::{ + ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty, Validators, +}; use protos::services_grpc::ValidatorServiceClient; use ssz::ssz_encode; +use std::collections::HashMap; use types::{Epoch, PublicKey, Slot}; impl BeaconNode for ValidatorServiceClient { - /// Request the shuffling from the Beacon Node (BN). - /// - /// As this function takes a `PublicKey`, it will first attempt to resolve the public key into - /// a validator index, then call the BN for production/attestation duties. - /// - /// Note: presently only block production information is returned. - fn request_shuffling( + /// Requests all duties (block signing and committee attesting) from the Beacon Node (BN). + fn request_duties( &self, epoch: Epoch, pubkeys: &[PublicKey], - ) -> Result, BeaconNodeError> { - // Lookup the validator indexes for all the supplied public keys. - let validator_indices = { - let mut req = IndexRequest::new(); - for public_key in pubkeys { - req.mut_public_key().push(ssz_encode(public_key)); - } - let resp = self - .validator_index(&req) - .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - resp.get_index() - }; - - let mut req = ProposeBlockSlotRequest::new(); - req.set_validator_index(validator_index); + ) -> Result { + // Get the required duties from all validators + // build the request + let mut req = GetDutiesRequest::new(); req.set_epoch(epoch.as_u64()); + let validators = Validators::new().mut_public_key(); + for pubkey in pubkeys { + validators.push(pubkey); + } + req.set_validators(validators); + // send the request, get the duties reply let reply = self - .propose_block_slot(&req) + .get_validator_duties(&req) .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - let block_production_slot = if reply.has_slot() { - Some(reply.get_slot()) - } else { - None - }; - - let block_production_slot = match block_production_slot { - Some(slot) => Some(Slot::new(slot)), - None => None, - }; - - Ok(Some(EpochDuties { - validator_index, - block_production_slot, - })) + let mut epoch_duties: HashMap> = HashMap::new(); + for (index, validator_duty) in reply.get_active_validator().enumerate() { + if let Some(duty) = validator_duty.has_slot() { + // the validator is active + //build the EpochDuty + let active_duty = duty.get_duty(); + let block_produce_slot = active_duty.get_block_produce_slot(); + let epoch_duty = EpochDuty { + block_produce_slot, + committee_slot: active_duty.get_committee_slot(), + committee_shard: active_duty.get_committee_shard(), + committee_index: active_duty.get_committee_index(), + }; + epoch_duties.insert(pubkeys[index], Some(epoch_duty)); + } else { + // validator is not active and has no duties + epoch_duties.insert(pubkeys[index], None); + } + } + Ok(epoch_duties) } } diff --git a/validator_client/src/duties/traits.rs b/validator_client/src/duties/traits.rs index a3dcade4b7..3aa8fbab28 100644 --- a/validator_client/src/duties/traits.rs +++ b/validator_client/src/duties/traits.rs @@ -8,12 +8,13 @@ pub enum BeaconNodeError { /// Defines the methods required to obtain a validators shuffling from a Beacon Node. pub trait BeaconNode: Send + Sync { - /// Get the shuffling for the given epoch and public key. + /// Gets the duties for all validators. /// - /// Returns Ok(None) if the public key is unknown, or the shuffling for that epoch is unknown. - fn request_shuffling( + /// Returns a vector of EpochDuties for each validator public key. The entry will be None for + /// validators that are not activated. + fn request_duties( &self, epoch: Epoch, pubkeys: &[PublicKey], - ) -> Result, BeaconNodeError>; + ) -> Result>, BeaconNodeError>; } From 46181408ba566a528214a8bd1b29b4f4af55870a Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 19:47:08 +1100 Subject: [PATCH 48/61] Epoch duties update --- protos/src/services.proto | 8 +- validator_client/src/duties/epoch_duties.rs | 117 +++++++++++--------- validator_client/src/duties/grpc.rs | 49 ++++---- validator_client/src/duties/mod.rs | 60 +++++----- validator_client/src/duties/traits.rs | 2 +- validator_client/src/service.rs | 40 ++++--- 6 files changed, 152 insertions(+), 124 deletions(-) diff --git a/protos/src/services.proto b/protos/src/services.proto index dea9b7a379..7a4bbc9779 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -97,7 +97,7 @@ message BeaconBlock { // the public keys of the validators message Validators { - repeated bytes public_key = 1; + repeated bytes public_keys = 1; } // Propose slot @@ -107,11 +107,11 @@ message GetDutiesRequest { } message GetDutiesResponse { - repeated ActiveValidator active_validator = 1; + repeated ActiveValidator active_validators = 1; } message ActiveValidator { - oneof slot_oneof { + oneof duty_oneof { bool none = 1; ValidatorDuty duty = 2; } @@ -120,7 +120,7 @@ message ActiveValidator { message ValidatorDuty { oneof block_oneof { bool none = 1; - uint64 block_produce_slot = 2; + uint64 block_production_slot = 2; } uint64 committee_slot = 3; uint64 committee_shard = 4; diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 41d3a24c22..de787c4b8e 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -1,7 +1,13 @@ -use block_proposer::{DutiesReader, DutiesReaderError}; use std::collections::HashMap; -use std::sync::RwLock; -use types::{Epoch, Fork, PublicKey, Slot}; +use std::ops::{Deref, DerefMut}; +use types::{Epoch, PublicKey, Slot}; + +/// The type of work a validator is required to do in a given slot. +#[derive(Debug, Clone)] +pub struct WorkType { + produce_block: bool, + produce_attestation: bool, +} /// The information required for a validator to propose and attest during some epoch. /// @@ -17,84 +23,91 @@ pub struct EpochDuty { } impl EpochDuty { - /// Returns `true` if work needs to be done in the supplied `slot` - pub fn is_work_slot(&self, slot: Slot) -> bool { + /// Returns `WorkType` if work needs to be done in the supplied `slot` + pub fn is_work_slot(&self, slot: Slot) -> Option { // if validator is required to produce a slot return true - match self.block_production_slot { - Some(s) if s == slot => return true, + let produce_block = match self.block_production_slot { + Some(s) if s == slot => true, _ => false, + }; + + let mut produce_attestation = false; + if self.committee_slot == slot { + produce_attestation = true; } - if self.committee_slot == slot { - return true; + if produce_block | produce_attestation { + return Some(WorkType { + produce_block, + produce_attestation, + }); } - return false; + None } } /// Maps a list of public keys (many validators) to an EpochDuty. -pub struct EpochDuties { - inner: HashMap>, -} +pub type EpochDuties = HashMap>; pub enum EpochDutiesMapError { Poisoned, + UnknownEpoch, + UnknownValidator, } /// Maps an `epoch` to some `EpochDuties` for a single validator. pub struct EpochDutiesMap { pub slots_per_epoch: u64, - pub map: RwLock>, + pub map: HashMap, } impl EpochDutiesMap { pub fn new(slots_per_epoch: u64) -> Self { Self { slots_per_epoch, - map: RwLock::new(HashMap::new()), + map: HashMap::new(), } } - - pub fn get(&self, epoch: Epoch) -> Result, EpochDutiesMapError> { - let map = self.map.read().map_err(|_| EpochDutiesMapError::Poisoned)?; - match map.get(&epoch) { - Some(duties) => Ok(Some(*duties)), - None => Ok(None), - } - } - - pub fn insert( - &self, - epoch: Epoch, - epoch_duties: EpochDuties, - ) -> Result, EpochDutiesMapError> { - let mut map = self - .map - .write() - .map_err(|_| EpochDutiesMapError::Poisoned)?; - Ok(map.insert(epoch, epoch_duties)) - } } -impl DutiesReader for EpochDutiesMap { - fn is_block_production_slot(&self, slot: Slot) -> Result { +// Expose the hashmap methods +impl Deref for EpochDutiesMap { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.map + } +} +impl DerefMut for EpochDutiesMap { + fn deref_mut(&mut self) -> &mut HashMap { + &mut self.map + } +} + +impl EpochDutiesMap { + /// Checks if the validator has work to do. + fn is_work_slot( + &self, + slot: Slot, + pubkey: &PublicKey, + ) -> Result, EpochDutiesMapError> { let epoch = slot.epoch(self.slots_per_epoch); - let map = self.map.read().map_err(|_| DutiesReaderError::Poisoned)?; - let duties = map + let epoch_duties = self + .map .get(&epoch) - .ok_or_else(|| DutiesReaderError::UnknownEpoch)?; - Ok(duties.is_block_production_slot(slot)) - } - - fn fork(&self) -> Result { - // TODO: this is garbage data. - // - // It will almost certainly cause signatures to fail verification. - Ok(Fork { - previous_version: [0; 4], - current_version: [0; 4], - epoch: Epoch::new(0), - }) + .ok_or_else(|| EpochDutiesMapError::UnknownEpoch)?; + if let Some(epoch_duty) = epoch_duties.get(pubkey) { + if let Some(duty) = epoch_duty { + // Retrieves the duty for a validator at a given slot + return Ok(duty.is_work_slot(slot)); + } else { + // the validator isn't active + return Ok(None); + } + } else { + // validator isn't known + return Err(EpochDutiesMapError::UnknownValidator); + } } } diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 91da512e37..32ac86435a 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -1,8 +1,6 @@ use super::epoch_duties::{EpochDuties, EpochDuty}; use super::traits::{BeaconNode, BeaconNodeError}; -use protos::services::{ - ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty, Validators, -}; +use protos::services::{GetDutiesRequest, Validators}; use protos::services_grpc::ValidatorServiceClient; use ssz::ssz_encode; use std::collections::HashMap; @@ -19,10 +17,8 @@ impl BeaconNode for ValidatorServiceClient { // build the request let mut req = GetDutiesRequest::new(); req.set_epoch(epoch.as_u64()); - let validators = Validators::new().mut_public_key(); - for pubkey in pubkeys { - validators.push(pubkey); - } + let mut validators = Validators::new(); + validators.set_public_keys(pubkeys.iter().map(|v| ssz_encode(v)).collect()); req.set_validators(validators); // send the request, get the duties reply @@ -30,24 +26,29 @@ impl BeaconNode for ValidatorServiceClient { .get_validator_duties(&req) .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - let mut epoch_duties: HashMap> = HashMap::new(); - for (index, validator_duty) in reply.get_active_validator().enumerate() { - if let Some(duty) = validator_duty.has_slot() { - // the validator is active - //build the EpochDuty - let active_duty = duty.get_duty(); - let block_produce_slot = active_duty.get_block_produce_slot(); - let epoch_duty = EpochDuty { - block_produce_slot, - committee_slot: active_duty.get_committee_slot(), - committee_shard: active_duty.get_committee_shard(), - committee_index: active_duty.get_committee_index(), - }; - epoch_duties.insert(pubkeys[index], Some(epoch_duty)); - } else { - // validator is not active and has no duties - epoch_duties.insert(pubkeys[index], None); + let mut epoch_duties: HashMap> = HashMap::new(); + for (index, validator_duty) in reply.get_active_validators().iter().enumerate() { + if !validator_duty.has_duty() { + // validator is inactive + epoch_duties.insert(pubkeys[index].clone(), None); + break; } + // active validator + let active_duty = validator_duty.get_duty(); + let block_production_slot = { + if active_duty.has_block_production_slot() { + Some(Slot::from(active_duty.get_block_production_slot())) + } else { + None + } + }; + let epoch_duty = EpochDuty { + block_production_slot, + committee_slot: Slot::from(active_duty.get_committee_slot()), + committee_shard: active_duty.get_committee_shard(), + committee_index: active_duty.get_committee_index(), + }; + epoch_duties.insert(pubkeys[index].clone(), Some(epoch_duty)); } Ok(epoch_duties) } diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 77d6d43b60..20a477910b 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -1,7 +1,8 @@ mod epoch_duties; mod grpc; -#[cfg(test)] -mod test_node; +// TODO: reintroduce tests +//#[cfg(test)] +//mod test_node; mod traits; pub use self::epoch_duties::EpochDutiesMap; @@ -11,9 +12,10 @@ use bls::PublicKey; use futures::Async; use slog::{debug, error, info}; use std::sync::Arc; -use types::{Epoch, Slot}; +use std::sync::RwLock; +use types::Epoch; -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone)] pub enum UpdateOutcome { /// The `EpochDuties` were not updated during this poll. NoChange(Epoch), @@ -29,8 +31,11 @@ pub enum UpdateOutcome { #[derive(Debug, PartialEq)] pub enum Error { + DutiesMapPoisoned, EpochMapPoisoned, BeaconNodeError(BeaconNodeError), + UnknownEpoch, + UnknownValidator, } /// A polling state machine which ensures the latest `EpochDuties` are obtained from the Beacon @@ -38,43 +43,37 @@ pub enum Error { /// /// This keeps track of all validator keys and required voting slots. pub struct DutiesManager { - pub duties_map: Arc, + pub duties_map: RwLock, /// A list of all public keys known to the validator service. pub pubkeys: Vec, - pub slots_per_epoch: u64, pub beacon_node: Arc, } impl DutiesManager { /// Check the Beacon Node for `EpochDuties`. /// - /// The present `epoch` will be learned from the supplied `SlotClock`. In production this will /// be a wall-clock (e.g., system time, remote server time, etc.). - fn update(&self, slot: Slot) -> Result { - let epoch = slot.epoch(self.slots_per_epoch); - - if let Some(duties) = self.beacon_node.request_shuffling(epoch, &self.pubkeys)? { - // If these duties were known, check to see if they're updates or identical. - let result = if let Some(known_duties) = self.duties_map.get(epoch)? { - if known_duties == duties { - Ok(UpdateOutcome::NoChange(epoch)) - } else { - Ok(UpdateOutcome::DutiesChanged(epoch, duties)) - } + fn update(&self, epoch: Epoch) -> Result { + let duties = self.beacon_node.request_duties(epoch, &self.pubkeys)?; + // If these duties were known, check to see if they're updates or identical. + let result = if let Some(known_duties) = self.duties_map.read()?.get(&epoch) { + if *known_duties == duties { + return Ok(UpdateOutcome::NoChange(epoch)); } else { - Ok(UpdateOutcome::NewDuties(epoch, duties)) - }; - self.duties_map.insert(epoch, duties)?; - result + //TODO: Duties could be large here. Remove from display and avoid the clone. + return Ok(UpdateOutcome::DutiesChanged(epoch, duties.clone())); + } } else { - Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) - } + Ok(UpdateOutcome::NewDuties(epoch, duties.clone())) + }; + self.duties_map.write()?.insert(epoch, duties); + result } /// A future wrapping around `update()`. This will perform logic based upon the update /// process and complete once the update has completed. - pub fn run_update(&self, slot: Slot, log: slog::Logger) -> Result, ()> { - match self.update(slot) { + pub fn run_update(&self, epoch: Epoch, log: slog::Logger) -> Result, ()> { + match self.update(epoch) { Err(error) => error!(log, "Epoch duties poll error"; "error" => format!("{:?}", error)), Ok(UpdateOutcome::NoChange(epoch)) => { debug!(log, "No change in duties"; "epoch" => epoch) @@ -93,16 +92,25 @@ impl DutiesManager { } } +//TODO: Use error_chain to handle errors impl From for Error { fn from(e: BeaconNodeError) -> Error { Error::BeaconNodeError(e) } } +//TODO: Use error_chain to handle errors +impl From> for Error { + fn from(e: std::sync::PoisonError) -> Error { + Error::DutiesMapPoisoned + } +} impl From for Error { fn from(e: EpochDutiesMapError) -> Error { match e { EpochDutiesMapError::Poisoned => Error::EpochMapPoisoned, + EpochDutiesMapError::UnknownEpoch => Error::UnknownEpoch, + EpochDutiesMapError::UnknownValidator => Error::UnknownValidator, } } } diff --git a/validator_client/src/duties/traits.rs b/validator_client/src/duties/traits.rs index 3aa8fbab28..374bed9f61 100644 --- a/validator_client/src/duties/traits.rs +++ b/validator_client/src/duties/traits.rs @@ -16,5 +16,5 @@ pub trait BeaconNode: Send + Sync { &self, epoch: Epoch, pubkeys: &[PublicKey], - ) -> Result>, BeaconNodeError>; + ) -> Result; } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index e322712996..c8e03e73d0 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -37,12 +37,14 @@ pub struct Service { chain_id: u16, /// The fork state we processing on. fork: Fork, - /// The slot clock keeping track of time. - slot_clock: Arc, + /// The slot clock for this 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 /// The beacon block GRPC client. beacon_block_client: Arc, @@ -74,7 +76,7 @@ impl Service { // retrieve node information let node_info = loop { - let info = match beacon_node_client.info(&Empty::new()) { + match beacon_node_client.info(&Empty::new()) { Err(e) => { warn!(log, "Could not connect to node. Error: {}", e); info!(log, "Retrying in 5 seconds..."); @@ -115,13 +117,6 @@ impl Service { epoch: Epoch::from(proto_fork.get_epoch()), }; - // build the validator slot clock - let slot_clock = { - let clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot) - .expect("Unable to instantiate SystemTimeSlotClock."); - Arc::new(clock) - }; - // initialize the RPC clients // Beacon node gRPC beacon block endpoints. @@ -142,6 +137,10 @@ impl Service { Arc::new(AttestationServiceClient::new(ch)) }; + // build the validator slot clock + let slot_clock = SystemTimeSlotClock::new(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))? @@ -179,6 +178,7 @@ impl Service { slot_clock, current_slot, duration_to_next_slot, + slots_per_epoch: config.spec.slots_per_epoch, beacon_block_client, validator_client, attester_client, @@ -211,7 +211,7 @@ impl Service { ) }; - // kick off core service + /* kick off core service */ // generate keypairs @@ -219,14 +219,18 @@ impl Service { // https://github.com/sigp/lighthouse/issues/160 let keypairs = Arc::new(vec![Keypair::random()]); - // build requisite objects to pass to core thread. - let duties_map = Arc::new(EpochDutiesMap::new(config.spec.slots_per_epoch)); - let epoch_map_for_attester = Arc::new(EpochMap::new(config.spec.slots_per_epoch)); + /* build requisite objects to pass to core thread */ + // 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 = EpochDutiesMap::new(config.spec.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. let manager = Arc::new(DutiesManager { duties_map, pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(), - slots_per_epoch: config.spec.slots_per_epoch.clone(), beacon_node: service.validator_client.clone(), }); @@ -244,6 +248,8 @@ impl Service { 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" @@ -252,9 +258,9 @@ impl Service { info!(log, "Processing slot: {}", current_slot.as_u64()); // check for new duties - let cloned_manager = manager.clone(); + let mut cloned_manager = manager.clone(); tokio::spawn(futures::future::poll_fn(move || { - cloned_manager.run_update(current_slot.clone(), log.clone()) + cloned_manager.run_update(current_epoch.clone(), log.clone()) })); // execute any specified duties From cde049df1f16bc5cd39fb1de499b904410577043 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 19:52:05 +1100 Subject: [PATCH 49/61] Adds RwLock to EpochDuty --- validator_client/src/service.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index c8e03e73d0..f47570f744 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -19,6 +19,7 @@ use protos::services_grpc::{ use slog::{debug, error, info, warn}; use slot_clock::{SlotClock, SystemTimeSlotClock}; use std::sync::Arc; +use std::sync::RwLock; use std::time::{Duration, Instant, SystemTime}; use tokio::prelude::*; use tokio::runtime::Builder; @@ -224,7 +225,7 @@ impl Service { // 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 = EpochDutiesMap::new(config.spec.slots_per_epoch); + let duties_map = RwLock::new(EpochDutiesMap::new(config.spec.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. From 1f437a3e7b939a57fe98e5902b3f5bf8daa00926 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 21:08:28 +1100 Subject: [PATCH 50/61] Implements RPC Server side of epoch duties --- beacon_node/rpc/src/lib.rs | 5 +- beacon_node/rpc/src/validator.rs | 131 ++++++++++++++++++++++++------- protos/src/services.proto | 4 +- 3 files changed, 108 insertions(+), 32 deletions(-) diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 4dfd334879..20cd62b1df 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -49,7 +49,10 @@ pub fn start_server( create_beacon_block_service(instance) }; let validator_service = { - let instance = ValidatorServiceInstance { log: log.clone() }; + let instance = ValidatorServiceInstance { + chain: beacon_chain.clone(), + log: log.clone(), + }; create_validator_service(instance) }; diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index f894deca6b..8071fac5cf 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -2,59 +2,132 @@ use bls::PublicKey; use futures::Future; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use protos::services::{ - IndexResponse, ProposeBlockSlotRequest, ProposeBlockSlotResponse, PublicKey as PublicKeyRequest, -}; + GetDutiesRequest, GetDutiesResponse, Validators}; use protos::services_grpc::ValidatorService; use slog::{debug, Logger}; use ssz::Decodable; +use std::sync::Arc; +use crate::beacon_chain::BeaconChain; #[derive(Clone)] pub struct ValidatorServiceInstance { + pub chain: Arc, pub log: Logger, } +//TODO: Refactor Errors impl ValidatorService for ValidatorServiceInstance { - fn validator_index( + + /// For a list of validator public keys, this function returns the slot at which each + /// validator must propose a block, attest to a shard, their shard committee and the shard they + /// need to attest to. + fn get_validator_duties ( &mut self, ctx: RpcContext, - req: PublicKeyRequest, - sink: UnarySink, + req: GetDutiesRequest, + sink: UnarySink, ) { - if let Ok((public_key, _)) = PublicKey::ssz_decode(req.get_public_key(), 0) { - debug!(self.log, "RPC request"; "endpoint" => "ValidatorIndex", "public_key" => public_key.concatenated_hex_id()); + let validators = req.get_validators(); + debug!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch()); - let mut resp = IndexResponse::new(); + let epoch = req.get_epoch(); + let mut resp = GetDutiesResponse::new(); - // TODO: return a legit value. - resp.set_index(1); + let spec = self.chain.spec; + let state = self.chain.state.read(); - let f = sink - .success(resp) - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - ctx.spawn(f) - } else { - let f = sink + //TODO: Decide whether to rebuild the cache + //TODO: Get the active validator indicies + //let active_validator_indices = self.chain.state.read().get_cached_active_validator_indices( + let active_validator_indices = &[1,2,3,4,5,6,7,8]; + // TODO: Is this the most efficient? Perhaps we cache this data structure. + + // this is an array of validators who are to propose this epoch + // TODO: RelativeEpoch? + let validator_proposers = 0..spec.slots_per_epoch.to_iter().map(|slot| state.get_beacon_proposer_index(slot, epoch, &spec)).collect(); + + // get the duties for each validator + for validator in validators { + let active_validator = ActiveValidator::new(); + + + let public_key = match PublicKey::ssz_decode(validator, 0) { + Ok((v_) => v, + Err(_) => { + let f = sink .fail(RpcStatus::new( RpcStatusCode::InvalidArgument, Some("Invalid public_key".to_string()), )) + //TODO: Handle error correctly .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - ctx.spawn(f) - } - } + return ctx.spawn(f); + } + }; - fn propose_block_slot( - &mut self, - ctx: RpcContext, - req: ProposeBlockSlotRequest, - sink: UnarySink, - ) { - debug!(self.log, "RPC request"; "endpoint" => "ProposeBlockSlot", "epoch" => req.get_epoch(), "validator_index" => req.get_validator_index()); + // is the validator active + let val_index = match state.get_validator_index(&public_key) { + Ok(index) => { + if active_validator_indices.contains(index) { + // validator is active, return the index + index + } + else { + // validator is inactive, go to the next validator + active_validator.set_none(); + resp.push(active_validator); + break; + } + }, + // the cache is not built, throw an error + Err(_) =>{ + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::FailedPreCondition, + Some("Beacon state cache is not built".to_string()), + )) + //TODO: Handle error correctly + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; - let mut resp = ProposeBlockSlotResponse::new(); + // we have an active validator, set its duties + let duty = ValidatorDuty::new(); + + // check if it needs to propose a block + let Some(slot) = validator_proposers.iter().position(|&v| val_index ==v) { + duty.set_block_production_slot(slot); + } + else { + // no blocks to propose this epoch + duty.set_none() + } + + // get attestation duties + let attestation_duties = match state.get_attestation_duties(val_index, &spec) { + Ok(v) => v, + // the cache is not built, throw an error + Err(_) =>{ + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::FailedPreCondition, + Some("Beacon state cache is not built".to_string()), + )) + //TODO: Handle error correctly + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; + + duty.set_committee_index(attestation_duties.committee_index); + duty.set_attestation_slot(attestation_duties.slot); + duty.set_attestation_shard(attestation_duties.shard); + + active_validator.set_duty(duty); + resp.push(active_validator); + } - // TODO: return a legit value. - resp.set_slot(1); let f = sink .success(resp) diff --git a/protos/src/services.proto b/protos/src/services.proto index 7a4bbc9779..82a2703e6f 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -122,8 +122,8 @@ message ValidatorDuty { bool none = 1; uint64 block_production_slot = 2; } - uint64 committee_slot = 3; - uint64 committee_shard = 4; + uint64 attestation_slot = 3; + uint64 attestation_shard = 4; uint64 committee_index = 5; } From 086ebb1485cb558cddd5441b7971ccd9ac8ff96c Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 21:32:53 +1100 Subject: [PATCH 51/61] Fix beacon node rpc compilation issues --- beacon_node/rpc/src/validator.rs | 122 ++++++++++++++++--------------- 1 file changed, 64 insertions(+), 58 deletions(-) diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 8071fac5cf..47886a9df6 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -1,13 +1,12 @@ +use crate::beacon_chain::BeaconChain; use bls::PublicKey; use futures::Future; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; -use protos::services::{ - GetDutiesRequest, GetDutiesResponse, Validators}; +use protos::services::{ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty}; use protos::services_grpc::ValidatorService; use slog::{debug, Logger}; use ssz::Decodable; use std::sync::Arc; -use crate::beacon_chain::BeaconChain; #[derive(Clone)] pub struct ValidatorServiceInstance { @@ -17,11 +16,10 @@ pub struct ValidatorServiceInstance { //TODO: Refactor Errors impl ValidatorService for ValidatorServiceInstance { - /// For a list of validator public keys, this function returns the slot at which each /// validator must propose a block, attest to a shard, their shard committee and the shard they /// need to attest to. - fn get_validator_duties ( + fn get_validator_duties( &mut self, ctx: RpcContext, req: GetDutiesRequest, @@ -32,102 +30,110 @@ impl ValidatorService for ValidatorServiceInstance { let epoch = req.get_epoch(); let mut resp = GetDutiesResponse::new(); + let resp_validators = resp.mut_active_validators(); - let spec = self.chain.spec; - let state = self.chain.state.read(); + let spec = self.chain.get_spec(); + let state = self.chain.get_state(); //TODO: Decide whether to rebuild the cache //TODO: Get the active validator indicies //let active_validator_indices = self.chain.state.read().get_cached_active_validator_indices( - let active_validator_indices = &[1,2,3,4,5,6,7,8]; + let active_validator_indices = vec![1, 2, 3, 4, 5, 6, 7, 8]; // TODO: Is this the most efficient? Perhaps we cache this data structure. // this is an array of validators who are to propose this epoch // TODO: RelativeEpoch? - let validator_proposers = 0..spec.slots_per_epoch.to_iter().map(|slot| state.get_beacon_proposer_index(slot, epoch, &spec)).collect(); + //let validator_proposers = [0..spec.slots_per_epoch].iter().map(|slot| state.get_beacon_proposer_index(Slot::from(slot), epoch, &spec)).collect(); + let validator_proposers: Vec = vec![1, 2, 3, 4, 5]; // get the duties for each validator - for validator in validators { - let active_validator = ActiveValidator::new(); + for validator_pk in validators.get_public_keys() { + let mut active_validator = ActiveValidator::new(); - - let public_key = match PublicKey::ssz_decode(validator, 0) { - Ok((v_) => v, - Err(_) => { - let f = sink - .fail(RpcStatus::new( - RpcStatusCode::InvalidArgument, - Some("Invalid public_key".to_string()), - )) - //TODO: Handle error correctly - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - return ctx.spawn(f); - } + let public_key = match PublicKey::ssz_decode(validator_pk, 0) { + Ok((v, _index)) => v, + Err(_) => { + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some("Invalid public_key".to_string()), + )) + //TODO: Handle error correctly + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } }; // is the validator active let val_index = match state.get_validator_index(&public_key) { - Ok(index) => { - if active_validator_indices.contains(index) { + Ok(Some(index)) => { + if active_validator_indices.contains(&index) { // validator is active, return the index index - } - else { + } else { // validator is inactive, go to the next validator - active_validator.set_none(); - resp.push(active_validator); + active_validator.set_none(false); + resp_validators.push(active_validator); break; } - }, + } + // validator index is not known, skip it + Ok(_) => { + active_validator.set_none(false); + resp_validators.push(active_validator); + break; + } // the cache is not built, throw an error - Err(_) =>{ + Err(_) => { let f = sink - .fail(RpcStatus::new( - RpcStatusCode::FailedPreCondition, - Some("Beacon state cache is not built".to_string()), - )) - //TODO: Handle error correctly - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + .fail(RpcStatus::new( + RpcStatusCode::FailedPrecondition, + Some("Beacon state cache is not built".to_string()), + )) + //TODO: Handle error correctly + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); return ctx.spawn(f); } }; // we have an active validator, set its duties - let duty = ValidatorDuty::new(); + let mut duty = ValidatorDuty::new(); - // check if it needs to propose a block - let Some(slot) = validator_proposers.iter().position(|&v| val_index ==v) { - duty.set_block_production_slot(slot); - } - else { + // check if the validator needs to propose a block + if let Some(slot) = validator_proposers + .iter() + .position(|&v| val_index as u64 == v) + { + duty.set_block_production_slot(epoch * spec.slots_per_epoch + slot as u64); + } else { // no blocks to propose this epoch - duty.set_none() + duty.set_none(false) } // get attestation duties let attestation_duties = match state.get_attestation_duties(val_index, &spec) { - Ok(v) => v, + Ok(Some(v)) => v, + Ok(_) => unreachable!(), //we've checked the validator index // the cache is not built, throw an error - Err(_) =>{ + Err(_) => { let f = sink - .fail(RpcStatus::new( - RpcStatusCode::FailedPreCondition, - Some("Beacon state cache is not built".to_string()), - )) - //TODO: Handle error correctly - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + .fail(RpcStatus::new( + RpcStatusCode::FailedPrecondition, + Some("Beacon state cache is not built".to_string()), + )) + //TODO: Handle error correctly + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); return ctx.spawn(f); } }; - duty.set_committee_index(attestation_duties.committee_index); - duty.set_attestation_slot(attestation_duties.slot); + duty.set_committee_index(attestation_duties.committee_index as u64); + duty.set_attestation_slot(attestation_duties.slot.as_u64()); duty.set_attestation_shard(attestation_duties.shard); active_validator.set_duty(duty); - resp.push(active_validator); - } - + resp_validators.push(active_validator); + } let f = sink .success(resp) From a315e9da493a0c7d9abab637f4dda7c0c06e0aa4 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 21:36:06 +1100 Subject: [PATCH 52/61] Rename fields in validator client EpochDuties --- validator_client/src/duties/epoch_duties.rs | 6 +++--- validator_client/src/duties/grpc.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index de787c4b8e..24b01e620c 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -17,8 +17,8 @@ pub struct WorkType { #[derive(Debug, PartialEq, Clone, Copy, Default)] pub struct EpochDuty { pub block_production_slot: Option, - pub committee_slot: Slot, - pub committee_shard: u64, + pub attestation_slot: Slot, + pub attestation_shard: u64, pub committee_index: u64, } @@ -32,7 +32,7 @@ impl EpochDuty { }; let mut produce_attestation = false; - if self.committee_slot == slot { + if self.attestation_slot == slot { produce_attestation = true; } diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 32ac86435a..0a4b3dffe3 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -44,8 +44,8 @@ impl BeaconNode for ValidatorServiceClient { }; let epoch_duty = EpochDuty { block_production_slot, - committee_slot: Slot::from(active_duty.get_committee_slot()), - committee_shard: active_duty.get_committee_shard(), + attestation_slot: Slot::from(active_duty.get_attestation_slot()), + attestation_shard: active_duty.get_attestation_shard(), committee_index: active_duty.get_committee_index(), }; epoch_duties.insert(pubkeys[index].clone(), Some(epoch_duty)); From 75195bbbf46bf1b027c2a84ec5b0872cc16e312d Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 22:22:51 +1100 Subject: [PATCH 53/61] Implement work finding logic in validator client --- validator_client/src/duties/epoch_duties.rs | 6 +-- validator_client/src/duties/mod.rs | 45 ++++++++++++++------- validator_client/src/service.rs | 20 +++++++-- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 24b01e620c..a74ded18b6 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -5,8 +5,8 @@ use types::{Epoch, PublicKey, Slot}; /// The type of work a validator is required to do in a given slot. #[derive(Debug, Clone)] pub struct WorkType { - produce_block: bool, - produce_attestation: bool, + pub produce_block: bool, + pub produce_attestation: bool, } /// The information required for a validator to propose and attest during some epoch. @@ -85,7 +85,7 @@ impl DerefMut for EpochDutiesMap { impl EpochDutiesMap { /// Checks if the validator has work to do. - fn is_work_slot( + pub fn is_work_slot( &self, slot: Slot, pubkey: &PublicKey, diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 20a477910b..94b8450838 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -5,15 +5,14 @@ mod grpc; //mod test_node; mod traits; -pub use self::epoch_duties::EpochDutiesMap; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; +pub use self::epoch_duties::{EpochDutiesMap, WorkType}; use self::traits::{BeaconNode, BeaconNodeError}; -use bls::PublicKey; use futures::Async; use slog::{debug, error, info}; use std::sync::Arc; use std::sync::RwLock; -use types::Epoch; +use types::{Epoch, PublicKey, Slot}; #[derive(Debug, PartialEq, Clone)] pub enum UpdateOutcome { @@ -24,9 +23,6 @@ pub enum UpdateOutcome { /// New `EpochDuties` were obtained, different to those which were previously known. This is /// likely to be the result of chain re-organisation. DutiesChanged(Epoch, EpochDuties), - /// The Beacon Node was unable to return the duties as the validator is unknown, or the - /// shuffling for the epoch is unknown. - UnknownValidatorOrEpoch(Epoch), } #[derive(Debug, PartialEq)] @@ -56,18 +52,19 @@ impl DutiesManager { fn update(&self, epoch: Epoch) -> Result { let duties = self.beacon_node.request_duties(epoch, &self.pubkeys)?; // If these duties were known, check to see if they're updates or identical. - let result = if let Some(known_duties) = self.duties_map.read()?.get(&epoch) { + 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. - return Ok(UpdateOutcome::DutiesChanged(epoch, duties.clone())); + self.duties_map.write()?.insert(epoch, duties.clone()); + return Ok(UpdateOutcome::DutiesChanged(epoch, duties)); } } else { - Ok(UpdateOutcome::NewDuties(epoch, duties.clone())) + //TODO: Remove clone by removing duties from outcome + self.duties_map.write()?.insert(epoch, duties.clone()); + return Ok(UpdateOutcome::NewDuties(epoch, duties)); }; - self.duties_map.write()?.insert(epoch, duties); - result } /// A future wrapping around `update()`. This will perform logic based upon the update @@ -84,12 +81,30 @@ impl DutiesManager { Ok(UpdateOutcome::NewDuties(epoch, duties)) => { info!(log, "New duties obtained"; "epoch" => epoch, "duties" => format!("{:?}", duties)) } - Ok(UpdateOutcome::UnknownValidatorOrEpoch(epoch)) => { - error!(log, "Epoch or validator unknown"; "epoch" => epoch) - } }; Ok(Async::Ready(())) } + + /// Returns a list of (Public, WorkType) indicating all the validators that have work to perform + /// this slot. + pub fn get_current_work(&self, slot: Slot) -> Option> { + let mut current_work: Vec<(PublicKey, WorkType)> = Vec::new(); + + // if the map is poisoned, return None + let duties = self.duties_map.read().ok()?; + + for validator_pk in &self.pubkeys { + match duties.is_work_slot(slot, &validator_pk) { + Ok(Some(work_type)) => current_work.push((validator_pk.clone(), work_type)), + Ok(None) => {} // No work for this validator + Err(_) => {} // Unknown epoch or validator, no work + } + } + if current_work.is_empty() { + return None; + } + Some(current_work) + } } //TODO: Use error_chain to handle errors @@ -101,7 +116,7 @@ impl From for Error { //TODO: Use error_chain to handle errors impl From> for Error { - fn from(e: std::sync::PoisonError) -> Error { + fn from(_e: std::sync::PoisonError) -> Error { Error::DutiesMapPoisoned } } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index f47570f744..07d88d344b 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -240,7 +240,7 @@ impl Service { .block_on(interval.for_each(move |_| { let log = service.log.clone(); - // get the current slot + /* get the current slot and epoch */ let current_slot = match service.slot_clock.present_slot() { Err(e) => { error!(log, "SystemTimeError {:?}", e); @@ -258,13 +258,25 @@ impl Service { info!(log, "Processing slot: {}", current_slot.as_u64()); - // check for new duties - let mut cloned_manager = manager.clone(); + /* 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 + /* 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.produce_attestation { + //TODO: Produce an attestation in a new thread + } + } + } Ok(()) })) From d3af95d1eba1725b21370447f6d02611c010ecf6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 22:41:55 +1100 Subject: [PATCH 54/61] Returns attestation duty for validator client processing --- validator_client/src/duties/epoch_duties.rs | 34 +++++++++++++-------- validator_client/src/duties/mod.rs | 8 ++--- validator_client/src/service.rs | 5 ++- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index a74ded18b6..5c23e82b1e 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -1,12 +1,17 @@ use std::collections::HashMap; use std::ops::{Deref, DerefMut}; -use types::{Epoch, PublicKey, Slot}; +use types::{AttestationDuty, Epoch, PublicKey, Slot}; -/// The type of work a validator is required to do in a given slot. +/// When work needs to be performed by a validator, this type is given back to the main service +/// which indicates all the information that required to process the work. +/// +/// Note: This is calculated per slot, so a validator knows which slot is related to this struct. #[derive(Debug, Clone)] -pub struct WorkType { +pub struct WorkInfo { + /// Validator needs to produce a block. pub produce_block: bool, - pub produce_attestation: bool, + /// Validator needs to produce an attestation. This supplies the required attestation data. + pub attestation_duty: Option, } /// The information required for a validator to propose and attest during some epoch. @@ -23,23 +28,28 @@ pub struct EpochDuty { } impl EpochDuty { - /// Returns `WorkType` if work needs to be done in the supplied `slot` - pub fn is_work_slot(&self, slot: Slot) -> Option { + /// Returns `WorkInfo` if work needs to be done in the supplied `slot` + pub fn is_work_slot(&self, slot: Slot) -> Option { // if validator is required to produce a slot return true let produce_block = match self.block_production_slot { Some(s) if s == slot => true, _ => false, }; - let mut produce_attestation = false; + // if the validator is required to attest to a shard, create the data + let mut attestation_duty = None; if self.attestation_slot == slot { - produce_attestation = true; + attestation_duty = Some(AttestationDuty { + slot, + shard: self.attestation_shard, + committee_index: self.committee_index as usize, + }); } - if produce_block | produce_attestation { - return Some(WorkType { + if produce_block | attestation_duty.is_some() { + return Some(WorkInfo { produce_block, - produce_attestation, + attestation_duty, }); } None @@ -89,7 +99,7 @@ impl EpochDutiesMap { &self, slot: Slot, pubkey: &PublicKey, - ) -> Result, EpochDutiesMapError> { + ) -> Result, EpochDutiesMapError> { let epoch = slot.epoch(self.slots_per_epoch); let epoch_duties = self diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 94b8450838..51470827c5 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -6,7 +6,7 @@ mod grpc; mod traits; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; -pub use self::epoch_duties::{EpochDutiesMap, WorkType}; +pub use self::epoch_duties::{EpochDutiesMap, WorkInfo}; use self::traits::{BeaconNode, BeaconNodeError}; use futures::Async; use slog::{debug, error, info}; @@ -85,10 +85,10 @@ impl DutiesManager { Ok(Async::Ready(())) } - /// Returns a list of (Public, WorkType) indicating all the validators that have work to perform + /// Returns a list of (Public, WorkInfo) indicating all the validators that have work to perform /// this slot. - pub fn get_current_work(&self, slot: Slot) -> Option> { - let mut current_work: Vec<(PublicKey, WorkType)> = Vec::new(); + pub fn get_current_work(&self, slot: Slot) -> Option> { + let mut current_work: Vec<(PublicKey, WorkInfo)> = Vec::new(); // if the map is poisoned, return None let duties = self.duties_map.read().ok()?; diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 07d88d344b..f62ade97e8 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -272,7 +272,10 @@ impl Service { if work_type.produce_block { // TODO: Produce a beacon block in a new thread } - if work_type.produce_attestation { + 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 } } From 4caaf82892378d05c1c45d9f166a65729c08c4d8 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Thu, 28 Mar 2019 13:14:41 +1100 Subject: [PATCH 55/61] Correct validator get duties RPC server logic --- beacon_node/rpc/src/validator.rs | 133 +++++++++++++++++++------------ validator_client/src/service.rs | 29 ------- 2 files changed, 80 insertions(+), 82 deletions(-) diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 47886a9df6..af902ca733 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -4,9 +4,10 @@ use futures::Future; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use protos::services::{ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty}; use protos::services_grpc::ValidatorService; -use slog::{debug, Logger}; +use slog::{debug, warn, Logger}; use ssz::Decodable; use std::sync::Arc; +use types::{Epoch, RelativeEpoch}; #[derive(Clone)] pub struct ValidatorServiceInstance { @@ -28,23 +29,47 @@ impl ValidatorService for ValidatorServiceInstance { let validators = req.get_validators(); debug!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch()); - let epoch = req.get_epoch(); + let epoch = Epoch::from(req.get_epoch()); let mut resp = GetDutiesResponse::new(); let resp_validators = resp.mut_active_validators(); let spec = self.chain.get_spec(); let state = self.chain.get_state(); - //TODO: Decide whether to rebuild the cache - //TODO: Get the active validator indicies - //let active_validator_indices = self.chain.state.read().get_cached_active_validator_indices( - let active_validator_indices = vec![1, 2, 3, 4, 5, 6, 7, 8]; - // TODO: Is this the most efficient? Perhaps we cache this data structure. + let relative_epoch = + match RelativeEpoch::from_epoch(state.slot.epoch(spec.slots_per_epoch), epoch) { + Ok(v) => v, + Err(e) => { + // incorrect epoch + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::FailedPrecondition, + Some(format!("Invalid epoch: {:?}", e)), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; - // this is an array of validators who are to propose this epoch - // TODO: RelativeEpoch? - //let validator_proposers = [0..spec.slots_per_epoch].iter().map(|slot| state.get_beacon_proposer_index(Slot::from(slot), epoch, &spec)).collect(); - let validator_proposers: Vec = vec![1, 2, 3, 4, 5]; + let validator_proposers: Result, _> = epoch + .slot_iter(spec.slots_per_epoch) + .map(|slot| state.get_beacon_proposer_index(slot, relative_epoch, &spec)) + .collect(); + let validator_proposers = match validator_proposers { + Ok(v) => v, + Err(_) => { + // could not get the validator proposer index + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some("Invalid public_key".to_string()), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?} : {:?}", req, e)); + return ctx.spawn(f); + } + }; // get the duties for each validator for validator_pk in validators.get_public_keys() { @@ -53,45 +78,65 @@ impl ValidatorService for ValidatorServiceInstance { let public_key = match PublicKey::ssz_decode(validator_pk, 0) { Ok((v, _index)) => v, Err(_) => { + let log_clone = self.log.clone(); let f = sink .fail(RpcStatus::new( RpcStatusCode::InvalidArgument, Some("Invalid public_key".to_string()), )) - //TODO: Handle error correctly - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + .map_err(move |e| warn!(log_clone, "failed to reply {:?}", req)); return ctx.spawn(f); } }; - // is the validator active + // get the validator index let val_index = match state.get_validator_index(&public_key) { - Ok(Some(index)) => { - if active_validator_indices.contains(&index) { - // validator is active, return the index - index - } else { - // validator is inactive, go to the next validator - active_validator.set_none(false); - resp_validators.push(active_validator); - break; - } - } - // validator index is not known, skip it - Ok(_) => { + Ok(Some(index)) => index, + Ok(None) => { + // index not present in registry, set the duties for this key to None + warn!( + self.log, + "RPC requested a public key that is not in the registry: {:?}", public_key + ); active_validator.set_none(false); resp_validators.push(active_validator); break; } // the cache is not built, throw an error - Err(_) => { + Err(e) => { + let log_clone = self.log.clone(); let f = sink .fail(RpcStatus::new( RpcStatusCode::FailedPrecondition, - Some("Beacon state cache is not built".to_string()), + Some(format!("Beacon state error {:?}", e)), )) - //TODO: Handle error correctly - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + .map_err(move |e| warn!(log_clone, "Failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; + + // get attestation duties and check if validator is active + let attestation_duties = match state.get_attestation_duties(val_index, &spec) { + Ok(Some(v)) => v, + Ok(_) => { + // validator is inactive, go to the next validator + warn!( + self.log, + "RPC requested an inactive validator key: {:?}", public_key + ); + active_validator.set_none(false); + resp_validators.push(active_validator); + break; + } + // the cache is not built, throw an error + Err(e) => { + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::FailedPrecondition, + Some(format!("Beacon state error {:?}", e)), + )) + .map_err(move |e| warn!(log_clone, "Failed to reply {:?}: {:?}", req, e)); return ctx.spawn(f); } }; @@ -100,33 +145,15 @@ impl ValidatorService for ValidatorServiceInstance { let mut duty = ValidatorDuty::new(); // check if the validator needs to propose a block - if let Some(slot) = validator_proposers - .iter() - .position(|&v| val_index as u64 == v) - { - duty.set_block_production_slot(epoch * spec.slots_per_epoch + slot as u64); + if let Some(slot) = validator_proposers.iter().position(|&v| val_index == v) { + duty.set_block_production_slot( + epoch.start_slot(spec.slots_per_epoch).as_u64() + slot as u64, + ); } else { // no blocks to propose this epoch duty.set_none(false) } - // get attestation duties - let attestation_duties = match state.get_attestation_duties(val_index, &spec) { - Ok(Some(v)) => v, - Ok(_) => unreachable!(), //we've checked the validator index - // the cache is not built, throw an error - Err(_) => { - let f = sink - .fail(RpcStatus::new( - RpcStatusCode::FailedPrecondition, - Some("Beacon state cache is not built".to_string()), - )) - //TODO: Handle error correctly - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - return ctx.spawn(f); - } - }; - duty.set_committee_index(attestation_duties.committee_index as u64); duty.set_attestation_slot(attestation_duties.slot.as_u64()); duty.set_attestation_shard(attestation_duties.shard); diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index f62ade97e8..6c44684141 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -291,35 +291,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(); From 6f0c0e47c368f1f231786516ebeb4e0ea925deed Mon Sep 17 00:00:00 2001 From: Age Manning Date: Thu, 28 Mar 2019 14:32:02 +1100 Subject: [PATCH 56/61] Update Validator RPC and cache building --- beacon_node/beacon_chain/src/beacon_chain.rs | 5 +++++ beacon_node/rpc/src/validator.rs | 8 ++++---- eth2/types/src/test_utils/testing_beacon_state_builder.rs | 2 +- validator_client/src/config.rs | 3 ++- validator_client/src/main.rs | 4 ++-- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 6fcb3fde40..27398b6c9f 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -348,6 +348,11 @@ where per_slot_processing(&mut *state, &latest_block_header, &self.spec)?; } + state.build_epoch_cache(RelativeEpoch::Previous, &self.spec)?; + state.build_epoch_cache(RelativeEpoch::Current, &self.spec)?; + state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, &self.spec)?; + state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, &self.spec)?; + state.update_pubkey_cache()?; Ok(()) } diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index af902ca733..e9e10b1d07 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -58,13 +58,13 @@ impl ValidatorService for ValidatorServiceInstance { .collect(); let validator_proposers = match validator_proposers { Ok(v) => v, - Err(_) => { + Err(e) => { // could not get the validator proposer index let log_clone = self.log.clone(); let f = sink .fail(RpcStatus::new( - RpcStatusCode::InvalidArgument, - Some("Invalid public_key".to_string()), + RpcStatusCode::FailedPrecondition, + Some(format!("Could not find beacon proposers: {:?}", e)), )) .map_err(move |e| warn!(log_clone, "failed to reply {:?} : {:?}", req, e)); return ctx.spawn(f); @@ -82,7 +82,7 @@ impl ValidatorService for ValidatorServiceInstance { let f = sink .fail(RpcStatus::new( RpcStatusCode::InvalidArgument, - Some("Invalid public_key".to_string()), + Some("apurple Invalid public_key".to_string()), )) .map_err(move |e| warn!(log_clone, "failed to reply {:?}", req)); return ctx.spawn(f); diff --git a/eth2/types/src/test_utils/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs index 3951820d86..79977c277d 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -120,7 +120,7 @@ impl TestingBeaconStateBuilder { }) .collect(); - let genesis_time = 1553647464; // arbitrary + let genesis_time = 1553740824; // arbitrary let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 78900374a2..3d426e8c70 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -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!(), }; diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 84d0cbff7b..d044030fe6 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -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(); From cc4ccd4017c0e85bdbfed08fc92454da6b110285 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Thu, 28 Mar 2019 17:16:43 +1100 Subject: [PATCH 57/61] Corrects read/write race condition --- beacon_node/rpc/src/validator.rs | 6 +- validator_client/src/duties/grpc.rs | 5 ++ validator_client/src/duties/mod.rs | 23 ++++--- validator_client/src/service.rs | 95 +++++++++++++++-------------- 4 files changed, 72 insertions(+), 57 deletions(-) diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index e9e10b1d07..c2e10885dc 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -4,7 +4,7 @@ use futures::Future; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use protos::services::{ActiveValidator, GetDutiesRequest, GetDutiesResponse, ValidatorDuty}; use protos::services_grpc::ValidatorService; -use slog::{debug, warn, Logger}; +use slog::{debug, info, warn, Logger}; use ssz::Decodable; use std::sync::Arc; use types::{Epoch, RelativeEpoch}; @@ -72,6 +72,7 @@ impl ValidatorService for ValidatorServiceInstance { }; // get the duties for each validator + dbg!(validators.get_public_keys()); for validator_pk in validators.get_public_keys() { let mut active_validator = ActiveValidator::new(); @@ -82,12 +83,13 @@ impl ValidatorService for ValidatorServiceInstance { let f = sink .fail(RpcStatus::new( RpcStatusCode::InvalidArgument, - Some("apurple Invalid public_key".to_string()), + Some("Invalid public_key".to_string()), )) .map_err(move |e| warn!(log_clone, "failed to reply {:?}", req)); return ctx.spawn(f); } }; + info!(self.log,""; "Public key" => format!("{:?}",public_key)); // get the validator index let val_index = match state.get_validator_index(&public_key) { diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 0a4b3dffe3..a3ec6f52b1 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -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) diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 51470827c5..0e962053e1 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -51,20 +51,23 @@ impl DutiesManager { /// be a wall-clock (e.g., system time, remote server time, etc.). fn update(&self, epoch: Epoch) -> Result { 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 diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index cd427337cf..1cb086b9e0 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -195,8 +195,8 @@ 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 = Arc::new(vec![Keypair::random()]); - + let keypairs: Arc> = + Arc::new((0..10).into_iter().map(|_| Keypair::random()).collect()); /* build requisite objects to pass to core thread */ // Builds a mapping of Epoch -> Map(PublicKey, EpochDuty) @@ -213,54 +213,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(()) From 2a2660ce620351784370c66d673a1bb5d811c58e Mon Sep 17 00:00:00 2001 From: Age Manning Date: Thu, 28 Mar 2019 17:22:09 +1100 Subject: [PATCH 58/61] Cleanup debug issues, corrects RPC server logic --- beacon_node/rpc/src/validator.rs | 6 ++---- eth2/types/src/test_utils/testing_beacon_state_builder.rs | 2 +- validator_client/src/duties/grpc.rs | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index c2e10885dc..936c95f52f 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -72,7 +72,6 @@ impl ValidatorService for ValidatorServiceInstance { }; // get the duties for each validator - dbg!(validators.get_public_keys()); for validator_pk in validators.get_public_keys() { let mut active_validator = ActiveValidator::new(); @@ -89,7 +88,6 @@ impl ValidatorService for ValidatorServiceInstance { return ctx.spawn(f); } }; - info!(self.log,""; "Public key" => format!("{:?}",public_key)); // get the validator index let val_index = match state.get_validator_index(&public_key) { @@ -102,7 +100,7 @@ impl ValidatorService for ValidatorServiceInstance { ); active_validator.set_none(false); resp_validators.push(active_validator); - break; + continue; } // the cache is not built, throw an error Err(e) => { @@ -128,7 +126,7 @@ impl ValidatorService for ValidatorServiceInstance { ); active_validator.set_none(false); resp_validators.push(active_validator); - break; + continue; } // the cache is not built, throw an error Err(e) => { diff --git a/eth2/types/src/test_utils/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs index 79977c277d..b38e8b5273 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -120,7 +120,7 @@ impl TestingBeaconStateBuilder { }) .collect(); - let genesis_time = 1553740824; // arbitrary + let genesis_time = 1553753928; // arbitrary let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index a3ec6f52b1..511ffa34a2 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -36,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(); From 6937da09943f08710e2f4ba56a62dbbf7243d98b Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 28 Mar 2019 18:58:00 +1100 Subject: [PATCH 59/61] Added deterministic key generation for testing purposes. --- validator_client/src/service.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index f62ade97e8..745fb42b95 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -26,6 +26,7 @@ use tokio::runtime::Builder; use tokio::timer::Interval; use tokio_timer::clock::Clock; use types::{Epoch, Fork, Slot}; +use types::test_utils::generate_deterministic_keypairs; //TODO: This service should be simplified in the future. Can be made more steamlined. @@ -218,7 +219,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 = Arc::new(vec![Keypair::random()]); + let keypairs = generate_deterministic_keypairs(8); /* build requisite objects to pass to core thread */ From ca0849edc5f670936c1b8223c9f0a482c527fdf0 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 28 Mar 2019 19:02:33 +1100 Subject: [PATCH 60/61] Fix cargo fmt error It's a rookie mistake and you hate to see it --- eth2/attester/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/eth2/attester/src/lib.rs b/eth2/attester/src/lib.rs index 065fdc9231..270c1e4d79 100644 --- a/eth2/attester/src/lib.rs +++ b/eth2/attester/src/lib.rs @@ -119,8 +119,7 @@ impl Attester Date: Thu, 28 Mar 2019 19:52:36 +1100 Subject: [PATCH 61/61] rustfmt fix. --- validator_client/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 178a998fea..83e7608550 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -25,8 +25,8 @@ use tokio::prelude::*; use tokio::runtime::Builder; use tokio::timer::Interval; use tokio_timer::clock::Clock; -use types::{Epoch, Fork, Slot}; use types::test_utils::generate_deterministic_keypairs; +use types::{Epoch, Fork, Slot}; //TODO: This service should be simplified in the future. Can be made more steamlined.