From 8f3530f60ce900f41d48c2d407e26cb0a727f9b3 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 8 Mar 2019 13:48:33 +0100 Subject: [PATCH 001/191] 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 08f15517f292e9f90f16cce3c022ca521f3204d1 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Tue, 12 Mar 2019 19:45:58 +1100 Subject: [PATCH 002/191] Added rustfmt check to Jenkins build, and moved to top of Travis build. --- .travis.yml | 2 +- Jenkinsfile | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d43d21a005..d1d3836705 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,9 @@ before_install: - sudo chown $USER /usr/local/bin/protoc - sudo chown -R $USER /usr/local/include/google script: + - cargo fmt --all -- --check - cargo build --verbose --all - cargo test --verbose --all - - cargo fmt --all -- --check rust: - stable - beta diff --git a/Jenkinsfile b/Jenkinsfile index 42755d5f7f..3377d4dc7a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,6 +6,11 @@ pipeline { } } stages { + stage('Check') { + steps { + sh 'cargo fmt --all -- --check' + } + } stage('Build') { steps { sh 'cargo build' From 197cd6341dc18f06f581722b48a28f88e3426760 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Tue, 12 Mar 2019 19:54:52 +1100 Subject: [PATCH 003/191] Installed rustfmt in Dockerfile. --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 063ece3cdf..03e74a5127 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ FROM rust:latest +RUN rustup component add rustfmt + RUN apt-get update && apt-get install -y clang libclang-dev cmake build-essential git unzip autoconf libtool RUN git clone https://github.com/google/protobuf.git && \ From 6bc977b9e657d5a3b16f69caadb65ff2dd22922c Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Tue, 12 Mar 2019 22:26:01 +1100 Subject: [PATCH 004/191] Moved rustfmt commands after build, so that it passes. Also added clippy into CI checks, but allowing warnings to pass for now. --- .travis.yml | 3 ++- Dockerfile | 6 ++++-- Jenkinsfile | 13 +++++++------ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index d1d3836705..37416ed42a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,9 @@ before_install: - sudo chown $USER /usr/local/bin/protoc - sudo chown -R $USER /usr/local/include/google script: - - cargo fmt --all -- --check - cargo build --verbose --all + - cargo fmt --all -- --check + - cargo clippy - cargo test --verbose --all rust: - stable diff --git a/Dockerfile b/Dockerfile index 03e74a5127..6691efa971 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,5 @@ FROM rust:latest -RUN rustup component add rustfmt - RUN apt-get update && apt-get install -y clang libclang-dev cmake build-essential git unzip autoconf libtool RUN git clone https://github.com/google/protobuf.git && \ @@ -17,3 +15,7 @@ RUN git clone https://github.com/google/protobuf.git && \ RUN mkdir /cargocache && chmod -R ugo+rwX /cargocache + +ENV CARGO_HOME /cargocache + +RUN rustup component add rustfmt clippy diff --git a/Jenkinsfile b/Jenkinsfile index 3377d4dc7a..da032ce5ba 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,20 +2,21 @@ pipeline { agent { dockerfile { filename 'Dockerfile' - args '-v cargo-cache:/cargocache:rw -e "CARGO_HOME=/cargocache"' + args '-v cargo-cache:/cargocache:rw' } } stages { - stage('Check') { - steps { - sh 'cargo fmt --all -- --check' - } - } stage('Build') { steps { sh 'cargo build' } } + stage('Check') { + steps { + sh 'cargo fmt --all -- --check' + sh 'cargo clippy' + } + } stage('Test') { steps { sh 'cargo test --all' From ed5f6971189afe2c60d7fee455c2177cd1d5872f Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Tue, 12 Mar 2019 22:36:20 +1100 Subject: [PATCH 005/191] Added clippy component installation to Travis CI yml file. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 37416ed42a..0d520aabb4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,3 +21,4 @@ matrix: fast_finish: true install: - rustup component add rustfmt + - rustup component add clippy From 2215aa4b4640e5bf4a5ace25a184de1dd0ad934c Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 15 Mar 2019 11:44:39 +0100 Subject: [PATCH 006/191] 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 007/191] 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 6d5bba1dcca492780526deec2f806bee6eb0ea14 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 21 Mar 2019 16:08:14 +1100 Subject: [PATCH 008/191] Commenting out clippy, since he's not coming until later. --- .travis.yml | 3 ++- Jenkinsfile | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0d520aabb4..b6cab34662 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,8 @@ before_install: script: - cargo build --verbose --all - cargo fmt --all -- --check - - cargo clippy + # No clippy until later... + #- cargo clippy - cargo test --verbose --all rust: - stable diff --git a/Jenkinsfile b/Jenkinsfile index da032ce5ba..e217a6130d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,7 +14,8 @@ pipeline { stage('Check') { steps { sh 'cargo fmt --all -- --check' - sh 'cargo clippy' + // No clippy until later... + //sh 'cargo clippy' } } stage('Test') { From ca18d4390aff4bfbf78d361be65916de9b21325f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 21 Mar 2019 17:17:01 +1100 Subject: [PATCH 009/191] Add first changes to syncing logic - Adds testing framework - Breaks out new `NetworkContext` object --- beacon_node/beacon_chain/src/lib.rs | 1 + .../beacon_chain/src/test_utils/mod.rs | 3 + .../testing_beacon_chain_builder.rs | 50 +++++ .../test_harness/src/beacon_chain_harness.rs | 11 +- beacon_node/libp2p/Cargo.toml | 1 + beacon_node/libp2p/src/rpc/methods.rs | 28 +++ beacon_node/network/Cargo.toml | 4 + beacon_node/network/src/beacon_chain.rs | 41 ++++ beacon_node/network/src/lib.rs | 4 +- beacon_node/network/src/message_handler.rs | 151 +++++++------- beacon_node/network/src/service.rs | 1 + beacon_node/network/src/sync/simple_sync.rs | 115 ++++++++++- beacon_node/network/tests/tests.rs | 184 ++++++++++++++++++ .../testing_beacon_state_builder.rs | 1 + 14 files changed, 513 insertions(+), 82 deletions(-) create mode 100644 beacon_node/beacon_chain/src/test_utils/mod.rs create mode 100644 beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs create mode 100644 beacon_node/network/tests/tests.rs diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 2137c0edfd..5fa7e7a77b 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -3,6 +3,7 @@ mod beacon_chain; mod checkpoint; mod errors; pub mod initialise; +pub mod test_utils; pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; diff --git a/beacon_node/beacon_chain/src/test_utils/mod.rs b/beacon_node/beacon_chain/src/test_utils/mod.rs new file mode 100644 index 0000000000..ad251a3c9e --- /dev/null +++ b/beacon_node/beacon_chain/src/test_utils/mod.rs @@ -0,0 +1,3 @@ +mod testing_beacon_chain_builder; + +pub use testing_beacon_chain_builder::TestingBeaconChainBuilder; diff --git a/beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs b/beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs new file mode 100644 index 0000000000..5c5477e556 --- /dev/null +++ b/beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs @@ -0,0 +1,50 @@ +pub use crate::{BeaconChain, BeaconChainError, CheckPoint}; +use db::{ + stores::{BeaconBlockStore, BeaconStateStore}, + MemoryDB, +}; +use fork_choice::BitwiseLMDGhost; +use slot_clock::TestingSlotClock; +use ssz::TreeHash; +use std::sync::Arc; +use types::test_utils::TestingBeaconStateBuilder; +use types::*; + +type TestingBeaconChain = BeaconChain>; + +pub struct TestingBeaconChainBuilder { + state_builder: TestingBeaconStateBuilder, +} + +impl TestingBeaconChainBuilder { + pub fn build(self, spec: &ChainSpec) -> TestingBeaconChain { + let db = Arc::new(MemoryDB::open()); + let block_store = Arc::new(BeaconBlockStore::new(db.clone())); + let state_store = Arc::new(BeaconStateStore::new(db.clone())); + let slot_clock = TestingSlotClock::new(spec.genesis_slot.as_u64()); + let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); + + let (genesis_state, _keypairs) = self.state_builder.build(); + + let mut genesis_block = BeaconBlock::empty(&spec); + genesis_block.state_root = Hash256::from_slice(&genesis_state.hash_tree_root()); + + // Create the Beacon Chain + BeaconChain::from_genesis( + state_store.clone(), + block_store.clone(), + slot_clock, + genesis_state, + genesis_block, + spec.clone(), + fork_choice, + ) + .unwrap() + } +} + +impl From for TestingBeaconChainBuilder { + fn from(state_builder: TestingBeaconStateBuilder) -> TestingBeaconChainBuilder { + TestingBeaconChainBuilder { state_builder } + } +} 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 bc5c93b94a..800dd3ce6d 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 @@ -36,14 +36,21 @@ impl BeaconChainHarness { /// - A keypair, `BlockProducer` and `Attester` for each validator. /// - A new BeaconChain struct where the given validators are in the genesis. pub fn new(spec: ChainSpec, validator_count: usize) -> Self { + let state_builder = + TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec); + Self::from_beacon_state_builder(state_builder, spec) + } + + pub fn from_beacon_state_builder( + state_builder: TestingBeaconStateBuilder, + spec: ChainSpec, + ) -> Self { let db = Arc::new(MemoryDB::open()); let block_store = Arc::new(BeaconBlockStore::new(db.clone())); let state_store = Arc::new(BeaconStateStore::new(db.clone())); let slot_clock = TestingSlotClock::new(spec.genesis_slot.as_u64()); let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); - let state_builder = - TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec); let (genesis_state, keypairs) = state_builder.build(); let mut genesis_block = BeaconBlock::empty(&spec); diff --git a/beacon_node/libp2p/Cargo.toml b/beacon_node/libp2p/Cargo.toml index dcbc04d0b9..e863c4d788 100644 --- a/beacon_node/libp2p/Cargo.toml +++ b/beacon_node/libp2p/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Age Manning "] edition = "2018" [dependencies] +beacon_chain = { path = "../beacon_chain" } # SigP repository until PR is merged libp2p = { git = "https://github.com/SigP/rust-libp2p", branch = "gossipsub" } types = { path = "../../eth2/types" } diff --git a/beacon_node/libp2p/src/rpc/methods.rs b/beacon_node/libp2p/src/rpc/methods.rs index 3014afd0ff..f05ade7ff5 100644 --- a/beacon_node/libp2p/src/rpc/methods.rs +++ b/beacon_node/libp2p/src/rpc/methods.rs @@ -1,3 +1,4 @@ +use beacon_chain::parking_lot::RwLockReadGuard; /// Available RPC methods types and ids. use ssz_derive::{Decode, Encode}; use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; @@ -60,6 +61,20 @@ pub enum RPCRequest { BeaconChainState(BeaconChainStateRequest), } +impl RPCRequest { + pub fn method_id(&self) -> u16 { + let method = match self { + RPCRequest::Hello(_) => RPCMethod::Hello, + RPCRequest::Goodbye(_) => RPCMethod::Goodbye, + RPCRequest::BeaconBlockRoots(_) => RPCMethod::BeaconBlockRoots, + RPCRequest::BeaconBlockHeaders(_) => RPCMethod::BeaconBlockHeaders, + RPCRequest::BeaconBlockBodies(_) => RPCMethod::BeaconBlockBodies, + RPCRequest::BeaconChainState(_) => RPCMethod::BeaconChainState, + }; + method.into() + } +} + #[derive(Debug, Clone)] pub enum RPCResponse { Hello(HelloMessage), @@ -69,6 +84,19 @@ pub enum RPCResponse { BeaconChainState(BeaconChainStateResponse), } +impl RPCResponse { + pub fn method_id(&self) -> u16 { + let method = match self { + RPCResponse::Hello(_) => RPCMethod::Hello, + RPCResponse::BeaconBlockRoots(_) => RPCMethod::BeaconBlockRoots, + RPCResponse::BeaconBlockHeaders(_) => RPCMethod::BeaconBlockHeaders, + RPCResponse::BeaconBlockBodies(_) => RPCMethod::BeaconBlockBodies, + RPCResponse::BeaconChainState(_) => RPCMethod::BeaconChainState, + }; + method.into() + } +} + /* Request/Response data structures for RPC methods */ /// The HELLO request/response handshake message. diff --git a/beacon_node/network/Cargo.toml b/beacon_node/network/Cargo.toml index 8b87a9d502..260ee0896b 100644 --- a/beacon_node/network/Cargo.toml +++ b/beacon_node/network/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Age Manning "] edition = "2018" +[dev-dependencies] +test_harness = { path = "../beacon_chain/test_harness" } +sloggers = "0.3.2" + [dependencies] beacon_chain = { path = "../beacon_chain" } libp2p = { path = "../libp2p" } diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 91628cc7e9..63e1eb6dd4 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -7,6 +7,8 @@ use beacon_chain::{ types::{BeaconState, ChainSpec}, CheckPoint, }; +use libp2p::HelloMessage; +use types::{Epoch, Hash256, Slot}; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { @@ -14,9 +16,19 @@ pub trait BeaconChain: Send + Sync { fn get_state(&self) -> RwLockReadGuard; + fn slot(&self) -> Slot; + fn head(&self) -> RwLockReadGuard; + fn best_slot(&self) -> Slot; + + fn best_block_root(&self) -> Hash256; + fn finalized_head(&self) -> RwLockReadGuard; + + fn finalized_epoch(&self) -> Epoch; + + fn hello_message(&self) -> HelloMessage; } impl BeaconChain for RawBeaconChain @@ -33,11 +45,40 @@ where self.state.read() } + fn slot(&self) -> Slot { + self.get_state().slot + } + fn head(&self) -> RwLockReadGuard { self.head() } + fn finalized_epoch(&self) -> Epoch { + self.get_state().finalized_epoch + } + fn finalized_head(&self) -> RwLockReadGuard { self.finalized_head() } + + fn best_slot(&self) -> Slot { + self.head().beacon_block.slot + } + + fn best_block_root(&self) -> Hash256 { + self.head().beacon_block_root + } + + fn hello_message(&self) -> HelloMessage { + let spec = self.get_spec(); + let state = self.get_state(); + + HelloMessage { + network_id: spec.network_id, + latest_finalized_root: state.finalized_root, + latest_finalized_epoch: state.finalized_epoch, + best_root: self.best_block_root(), + best_slot: self.best_slot(), + } + } } diff --git a/beacon_node/network/src/lib.rs b/beacon_node/network/src/lib.rs index 1e47b9a731..822b05509a 100644 --- a/beacon_node/network/src/lib.rs +++ b/beacon_node/network/src/lib.rs @@ -1,8 +1,8 @@ /// This crate provides the network server for Lighthouse. pub mod beacon_chain; pub mod error; -mod message_handler; -mod service; +pub mod message_handler; +pub mod service; pub mod sync; pub use libp2p::NetworkConfig; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 4cd0ab951c..8e390b4aff 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -5,32 +5,28 @@ use crate::sync::SimpleSync; use crossbeam_channel::{unbounded as channel, Sender}; use futures::future; use libp2p::{ - rpc::{RPCMethod, RPCRequest, RPCResponse}, + rpc::{RPCRequest, RPCResponse}, HelloMessage, PeerId, RPCEvent, }; +use slog::debug; use slog::warn; -use slog::{debug, trace}; use std::collections::HashMap; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Instant; /// Timeout for RPC requests. -const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); +// const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); /// Timeout before banning a peer for non-identification. -const HELLO_TIMEOUT: Duration = Duration::from_secs(30); +// const HELLO_TIMEOUT: Duration = Duration::from_secs(30); /// Handles messages received from the network and client and organises syncing. pub struct MessageHandler { /// Currently loaded and initialised beacon chain. - chain: Arc, + _chain: Arc, /// The syncing framework. sync: SimpleSync, - /// The network channel to relay messages to the Network service. - network_send: crossbeam_channel::Sender, - /// A mapping of peers and the RPC id we have sent an RPC request to. - requests: HashMap<(PeerId, u64), Instant>, - /// A counter of request id for each peer. - request_ids: HashMap, + /// The context required to send messages to, and process messages from peers. + network_context: NetworkContext, /// The `MessageHandler` logger. log: slog::Logger, } @@ -65,13 +61,9 @@ impl MessageHandler { let sync = SimpleSync::new(beacon_chain.clone(), &log); let mut handler = MessageHandler { - // TODO: The handler may not need a chain, perhaps only sync? - chain: beacon_chain.clone(), + _chain: beacon_chain.clone(), sync, - network_send, - requests: HashMap::new(), - request_ids: HashMap::new(), - + network_context: NetworkContext::new(network_send, log.clone()), log: log.clone(), }; @@ -93,8 +85,7 @@ impl MessageHandler { match message { // we have initiated a connection to a peer HandlerMessage::PeerDialed(peer_id) => { - let id = self.generate_request_id(&peer_id); - self.send_hello(peer_id, id, true); + self.sync.on_connect(&peer_id, &mut self.network_context); } // we have received an RPC message request/response HandlerMessage::RPC(peer_id, rpc_event) => { @@ -118,9 +109,11 @@ impl MessageHandler { /// A new RPC request has been received from the network. fn handle_rpc_request(&mut self, peer_id: PeerId, id: u64, request: RPCRequest) { + // TODO: ensure the id is legit match request { RPCRequest::Hello(hello_message) => { - self.handle_hello_request(peer_id, id, hello_message) + self.sync + .on_hello(&peer_id, hello_message, &mut self.network_context) } // TODO: Handle all requests _ => {} @@ -131,7 +124,12 @@ impl MessageHandler { // we match on id and ignore responses past the timeout. fn handle_rpc_response(&mut self, peer_id: PeerId, id: u64, response: RPCResponse) { // if response id is related to a request, ignore (likely RPC timeout) - if self.requests.remove(&(peer_id.clone(), id)).is_none() { + if self + .network_context + .requests + .remove(&(peer_id.clone(), id)) + .is_none() + { debug!(self.log, "Unrecognized response from peer: {:?}", peer_id); return; } @@ -145,16 +143,10 @@ impl MessageHandler { } } - /// Handle a HELLO RPC request message. - fn handle_hello_request(&mut self, peer_id: PeerId, id: u64, hello_message: HelloMessage) { - // send back a HELLO message - self.send_hello(peer_id.clone(), id, false); - // validate the peer - self.validate_hello(peer_id, hello_message); - } - /// Validate a HELLO RPC message. fn validate_hello(&mut self, peer_id: PeerId, message: HelloMessage) { + self.sync + .on_hello(&peer_id, message.clone(), &mut self.network_context); // validate the peer if !self.sync.validate_peer(peer_id.clone(), message) { debug!( @@ -164,8 +156,68 @@ impl MessageHandler { //TODO: block/ban the peer } } +} - /* General RPC helper functions */ +pub struct NetworkContext { + /// The network channel to relay messages to the Network service. + network_send: crossbeam_channel::Sender, + /// A mapping of peers and the RPC id we have sent an RPC request to. + requests: HashMap<(PeerId, u64), Instant>, + /// A counter of request id for each peer. + request_ids: HashMap, + /// The `MessageHandler` logger. + log: slog::Logger, +} + +impl NetworkContext { + pub fn new(network_send: crossbeam_channel::Sender, log: slog::Logger) -> Self { + Self { + network_send, + requests: HashMap::new(), + request_ids: HashMap::new(), + log, + } + } + + pub fn send_rpc_request(&mut self, peer_id: PeerId, rpc_request: RPCRequest) { + let id = self.generate_request_id(&peer_id); + self.send_rpc_event( + peer_id, + RPCEvent::Request { + id, + method_id: rpc_request.method_id(), + body: rpc_request, + }, + ); + } + + pub fn send_rpc_response(&mut self, peer_id: PeerId, rpc_response: RPCResponse) { + let id = self.generate_request_id(&peer_id); + self.send_rpc_event( + peer_id, + RPCEvent::Response { + id, + method_id: rpc_response.method_id(), + result: rpc_response, + }, + ); + } + + fn send_rpc_event(&self, peer_id: PeerId, rpc_event: RPCEvent) { + self.send(peer_id, OutgoingMessage::RPC(rpc_event)) + } + + fn send(&self, peer_id: PeerId, outgoing_message: OutgoingMessage) { + self.network_send + .send(NetworkMessage::Send(peer_id, outgoing_message)) + .unwrap_or_else(|_| { + warn!( + self.log, + "Could not send RPC message to the network service" + ) + }); + // + } /// Generates a new request id for a peer. fn generate_request_id(&mut self, peer_id: &PeerId) -> u64 { @@ -185,41 +237,4 @@ impl MessageHandler { ); id } - - /// Sends a HELLO RPC request or response to a newly connected peer. - //TODO: The boolean determines if sending request/respond, will be cleaner in the RPC re-write - fn send_hello(&mut self, peer_id: PeerId, id: u64, is_request: bool) { - let rpc_event = if is_request { - RPCEvent::Request { - id, - method_id: RPCMethod::Hello.into(), - body: RPCRequest::Hello(self.sync.generate_hello()), - } - } else { - RPCEvent::Response { - id, - method_id: RPCMethod::Hello.into(), - result: RPCResponse::Hello(self.sync.generate_hello()), - } - }; - - // send the hello request to the network - trace!(self.log, "Sending HELLO message to peer {:?}", peer_id); - self.send_rpc(peer_id, rpc_event); - } - - /// Sends an RPC request/response to the network server. - fn send_rpc(&self, peer_id: PeerId, rpc_event: RPCEvent) { - self.network_send - .send(NetworkMessage::Send( - peer_id, - OutgoingMessage::RPC(rpc_event), - )) - .unwrap_or_else(|_| { - warn!( - self.log, - "Could not send RPC message to the network service" - ) - }); - } } diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index c3045d280c..d01188f5d0 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -6,6 +6,7 @@ use crossbeam_channel::{unbounded as channel, Sender, TryRecvError}; use futures::prelude::*; use futures::sync::oneshot; use futures::Stream; +use libp2p::rpc::RPCResponse; use libp2p::RPCEvent; use libp2p::Service as LibP2PService; use libp2p::{Libp2pEvent, PeerId}; diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 95c7092c3c..ae6a9e7a1f 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,11 +1,16 @@ use crate::beacon_chain::BeaconChain; -use libp2p::rpc::HelloMessage; +use crate::message_handler::{MessageHandler, NetworkContext}; +use crate::service::NetworkMessage; +use crossbeam_channel::Sender; +use libp2p::rpc::{HelloMessage, RPCMethod, RPCRequest, RPCResponse}; use libp2p::PeerId; use slog::{debug, o}; use std::collections::HashMap; use std::sync::Arc; use types::{Epoch, Hash256, Slot}; +type NetworkSender = Sender; + /// The number of slots that we can import blocks ahead of us, before going into full Sync mode. const SLOT_IMPORT_TOLERANCE: u64 = 100; @@ -17,6 +22,32 @@ pub struct PeerSyncInfo { best_slot: Slot, } +impl PeerSyncInfo { + pub fn is_on_chain(&self, chain: &Arc) -> bool { + // TODO: make useful. + true + } + + pub fn has_higher_finalized_epoch(&self, chain: &Arc) -> bool { + self.latest_finalized_epoch > chain.get_state().finalized_epoch + } + + pub fn has_higher_best_slot(&self, chain: &Arc) -> bool { + self.latest_finalized_epoch > chain.get_state().finalized_epoch + } +} + +impl From for PeerSyncInfo { + fn from(hello: HelloMessage) -> PeerSyncInfo { + PeerSyncInfo { + latest_finalized_root: hello.latest_finalized_root, + latest_finalized_epoch: hello.latest_finalized_epoch, + best_root: hello.best_root, + best_slot: hello.best_slot, + } + } +} + /// The current syncing state. #[derive(PartialEq)] pub enum SyncState { @@ -60,17 +91,81 @@ impl SimpleSync { } } + pub fn on_connect(&self, peer_id: &PeerId, network: &mut NetworkContext) { + network.send_rpc_request( + peer_id.clone(), + RPCRequest::Hello(self.chain.hello_message()), + ); + } + + pub fn on_hello_request( + &self, + peer_id: &PeerId, + hello: HelloMessage, + network: &mut NetworkContext, + ) { + network.send_rpc_response( + peer_id.clone(), + RPCResponse::Hello(self.chain.hello_message()), + ); + self.on_hello(peer_id, hello, network); + } + + pub fn on_hello(&self, peer_id: &PeerId, hello: HelloMessage, network: &mut NetworkContext) { + // network id must match + if hello.network_id != self.network_id { + debug!(self.log, "Bad network id. Peer: {:?}", peer_id); + return; + } + + let peer = PeerSyncInfo::from(hello); + + /* + if peer.has_higher_finalized_epoch(&self.chain) { + // we need blocks + let peer_slot = peer.latest_finalized_epoch.start_slot(spec.slots_per_epoch); + let our_slot = self.chain.finalized_epoch(); + let required_slots = peer_slot - our_slot; + } else { + if !peer.is_on_chain(&self.chain) { + return (true, responses); + } + // + } + */ + + /* + // compare latest epoch and finalized root to see if they exist in our chain + if peer_info.latest_finalized_epoch <= self.latest_finalized_epoch { + // ensure their finalized root is in our chain + // TODO: Get the finalized root at hello_message.latest_epoch and ensure they match + //if (hello_message.latest_finalized_root == self.chain.get_state() { + // return false; + // } + } + + // the client is valid, add it to our list of known_peers and request sync if required + // update peer list if peer already exists + let peer_info = PeerSyncInfo::from(hello); + + debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); + self.known_peers.insert(peer_id, peer_info); + + // set state to sync + if self.state == SyncState::Idle + && hello_message.best_slot > self.latest_slot + SLOT_IMPORT_TOLERANCE + { + self.state = SyncState::Downloading; + //TODO: Start requesting blocks from known peers. Ideally in batches + } + + true + */ + } + /// Generates our current state in the form of a HELLO RPC message. pub fn generate_hello(&self) -> HelloMessage { - let state = &self.chain.get_state(); - //TODO: Paul to verify the logic of these fields. - HelloMessage { - network_id: self.network_id, - latest_finalized_root: state.finalized_root, - latest_finalized_epoch: state.finalized_epoch, - best_root: state.latest_block_roots[0], //TODO: build correct value as a beacon chain function - best_slot: state.slot - 1, - } + self.chain.hello_message() } pub fn validate_peer(&mut self, peer_id: PeerId, hello_message: HelloMessage) -> bool { diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs new file mode 100644 index 0000000000..dc0832feda --- /dev/null +++ b/beacon_node/network/tests/tests.rs @@ -0,0 +1,184 @@ +use beacon_chain::test_utils::TestingBeaconChainBuilder; +use crossbeam_channel::{unbounded, Receiver, Sender}; +use libp2p::rpc::{HelloMessage, RPCMethod, RPCRequest, RPCResponse}; +use libp2p::{PeerId, RPCEvent}; +use network::beacon_chain::BeaconChain as NetworkBeaconChain; +use network::message_handler::{HandlerMessage, MessageHandler}; +use network::service::{NetworkMessage, OutgoingMessage}; +use sloggers::terminal::{Destination, TerminalLoggerBuilder}; +use sloggers::types::Severity; +use sloggers::Build; +use std::sync::Arc; +use test_harness::BeaconChainHarness; +use tokio::runtime::TaskExecutor; +use types::{test_utils::TestingBeaconStateBuilder, *}; + +pub struct SyncNode { + pub id: usize, + sender: Sender, + receiver: Receiver, +} + +impl SyncNode { + pub fn new( + id: usize, + executor: &TaskExecutor, + chain: Arc, + logger: slog::Logger, + ) -> Self { + let (network_sender, network_receiver) = unbounded(); + let message_handler_sender = + MessageHandler::spawn(chain, network_sender, executor, logger).unwrap(); + + Self { + id, + sender: message_handler_sender, + receiver: network_receiver, + } + } + + fn send(&self, message: HandlerMessage) { + self.sender.send(message).unwrap(); + } + + fn recv(&self) -> NetworkMessage { + self.receiver.recv().unwrap() + } + + fn recv_rpc_response(&self) -> RPCResponse { + let network_message = self.recv(); + match network_message { + NetworkMessage::Send( + _peer_id, + OutgoingMessage::RPC(RPCEvent::Response { + id: _, + method_id: _, + result, + }), + ) => result, + _ => panic!("get_rpc_response failed! got {:?}", network_message), + } + } + + fn recv_rpc_request(&self) -> RPCRequest { + let network_message = self.recv(); + match network_message { + NetworkMessage::Send( + _peer_id, + OutgoingMessage::RPC(RPCEvent::Request { + id: _, + method_id: _, + body, + }), + ) => body, + _ => panic!("get_rpc_request failed! got {:?}", network_message), + } + } +} + +fn get_logger() -> slog::Logger { + let mut builder = TerminalLoggerBuilder::new(); + builder.level(Severity::Debug); + builder.destination(Destination::Stderr); + builder.build().unwrap() +} + +pub struct SyncMaster { + harness: BeaconChainHarness, + peer_id: PeerId, + response_ids: Vec, +} + +impl SyncMaster { + fn from_beacon_state_builder( + state_builder: TestingBeaconStateBuilder, + node_count: usize, + spec: &ChainSpec, + ) -> Self { + let harness = BeaconChainHarness::from_beacon_state_builder(state_builder, spec.clone()); + let peer_id = PeerId::random(); + let response_ids = vec![0; node_count]; + + Self { + harness, + peer_id, + response_ids, + } + } + + pub fn response_id(&mut self, node: &SyncNode) -> u64 { + let id = self.response_ids[node.id]; + self.response_ids[node.id] += 1; + id + } + + pub fn do_hello_with(&mut self, node: &SyncNode) { + let message = HandlerMessage::PeerDialed(self.peer_id.clone()); + node.send(message); + + let request = node.recv_rpc_request(); + + match request { + RPCRequest::Hello(_hello) => { + let hello = self.harness.beacon_chain.hello_message(); + let response = self.rpc_response(node, RPCResponse::Hello(hello)); + node.send(response); + } + _ => panic!("Got message other than hello from node."), + } + } + + fn rpc_response(&mut self, node: &SyncNode, rpc_response: RPCResponse) -> HandlerMessage { + HandlerMessage::RPC( + self.peer_id.clone(), + RPCEvent::Response { + id: self.response_id(node), + method_id: RPCMethod::Hello.into(), + result: rpc_response, + }, + ) + } +} + +fn test_setup( + state_builder: TestingBeaconStateBuilder, + node_count: usize, + spec: &ChainSpec, + logger: slog::Logger, +) -> (tokio::runtime::Runtime, SyncMaster, Vec) { + let runtime = tokio::runtime::Runtime::new().unwrap(); + + let mut nodes = Vec::with_capacity(node_count); + for id in 0..node_count { + let local_chain = TestingBeaconChainBuilder::from(state_builder.clone()).build(&spec); + let node = SyncNode::new( + id, + &runtime.executor(), + Arc::new(local_chain), + logger.clone(), + ); + + nodes.push(node); + } + + let master = SyncMaster::from_beacon_state_builder(state_builder, node_count, &spec); + + (runtime, master, nodes) +} + +#[test] +fn first_test() { + let logger = get_logger(); + let spec = ChainSpec::few_validators(); + let validator_count = 8; + let node_count = 1; + + let state_builder = + TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec); + + let (runtime, mut master, nodes) = test_setup(state_builder, node_count, &spec, logger.clone()); + + master.do_hello_with(&nodes[0]); + + runtime.shutdown_now(); +} 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 e76a01e49b..7a7a902de3 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -23,6 +23,7 @@ pub fn keypairs_path() -> PathBuf { /// Builds a beacon state to be used for testing purposes. /// /// This struct should **never be used for production purposes.** +#[derive(Clone)] pub struct TestingBeaconStateBuilder { state: BeaconState, keypairs: Vec, From 430702b38dbad76a082a252772081af84842e2cb Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 21 Mar 2019 18:21:26 +1100 Subject: [PATCH 010/191] Clean println's out of state processing --- beacon_node/beacon_chain/src/beacon_chain.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index bf87adf10d..01787f95bc 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -596,7 +596,6 @@ where // Transition the parent state to the present slot. let mut state = parent_state; - println!("parent process state: {:?}", state.latest_block_header); let previous_block_header = parent_block.into_header(); for _ in state.slot.as_u64()..present_slot.as_u64() { if let Err(e) = per_slot_processing(&mut state, &previous_block_header, &self.spec) { @@ -614,8 +613,6 @@ where )); } - println!("process state: {:?}", state.latest_block_header); - let state_root = state.canonical_root(); if block.state_root != state_root { @@ -706,8 +703,6 @@ where per_block_processing_without_verifying_block_signature(&mut state, &block, &self.spec)?; - println!("produce state: {:?}", state.latest_block_header); - let state_root = state.canonical_root(); block.state_root = state_root; From f3c81cda99ff6e85b79780c4dc8b4d0d84796025 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 21 Mar 2019 18:21:50 +1100 Subject: [PATCH 011/191] Implement hello responses. --- beacon_node/libp2p/src/rpc/methods.rs | 47 +++--- beacon_node/libp2p/src/rpc/mod.rs | 2 +- beacon_node/network/src/message_handler.rs | 27 ++-- beacon_node/network/src/service.rs | 1 - beacon_node/network/src/sync/simple_sync.rs | 158 +++++++++----------- beacon_node/network/tests/tests.rs | 33 +++- 6 files changed, 136 insertions(+), 132 deletions(-) diff --git a/beacon_node/libp2p/src/rpc/methods.rs b/beacon_node/libp2p/src/rpc/methods.rs index f05ade7ff5..381fc8b015 100644 --- a/beacon_node/libp2p/src/rpc/methods.rs +++ b/beacon_node/libp2p/src/rpc/methods.rs @@ -1,4 +1,3 @@ -use beacon_chain::parking_lot::RwLockReadGuard; /// Available RPC methods types and ids. use ssz_derive::{Decode, Encode}; use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; @@ -115,75 +114,75 @@ pub struct HelloMessage { } /// Request a number of beacon block roots from a peer. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockRootsRequest { /// The starting slot of the requested blocks. - start_slot: Slot, + pub start_slot: Slot, /// The number of blocks from the start slot. - count: u64, // this must be less than 32768. //TODO: Enforce this in the lower layers + pub count: u64, // this must be less than 32768. //TODO: Enforce this in the lower layers } /// Response containing a number of beacon block roots from a peer. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockRootsResponse { /// List of requested blocks and associated slots. - roots: Vec, + pub roots: Vec, } /// Contains a block root and associated slot. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BlockRootSlot { /// The block root. - block_root: Hash256, + pub block_root: Hash256, /// The block slot. - slot: Slot, + pub slot: Slot, } /// Request a number of beacon block headers from a peer. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockHeadersRequest { /// The starting header hash of the requested headers. - start_root: Hash256, + pub start_root: Hash256, /// The starting slot of the requested headers. - start_slot: Slot, + pub start_slot: Slot, /// The maximum number of headers than can be returned. - max_headers: u64, + pub max_headers: u64, /// The maximum number of slots to skip between blocks. - skip_slots: u64, + pub skip_slots: u64, } /// Response containing requested block headers. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockHeadersResponse { /// The list of requested beacon block headers. - headers: Vec, + pub headers: Vec, } /// Request a number of beacon block bodies from a peer. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockBodiesRequest { /// The list of beacon block bodies being requested. - block_roots: Hash256, + pub block_roots: Hash256, } /// Response containing the list of requested beacon block bodies. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockBodiesResponse { /// The list of beacon block bodies being requested. - block_bodies: Vec, + pub block_bodies: Vec, } /// Request values for tree hashes which yield a blocks `state_root`. -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconChainStateRequest { /// The tree hashes that a value is requested for. - hashes: Vec, + pub hashes: Vec, } /// Request values for tree hashes which yield a blocks `state_root`. // Note: TBD -#[derive(Encode, Decode, Clone, Debug)] +#[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconChainStateResponse { /// The values corresponding the to the requested tree hashes. - values: bool, //TBD - stubbed with encodeable bool + pub values: bool, //TBD - stubbed with encodeable bool } diff --git a/beacon_node/libp2p/src/rpc/mod.rs b/beacon_node/libp2p/src/rpc/mod.rs index a1cfadafe6..a1573ec93d 100644 --- a/beacon_node/libp2p/src/rpc/mod.rs +++ b/beacon_node/libp2p/src/rpc/mod.rs @@ -2,7 +2,7 @@ /// /// This is purpose built for Ethereum 2.0 serenity and the protocol listens on /// `/eth/serenity/rpc/1.0.0` -mod methods; +pub mod methods; mod protocol; use futures::prelude::*; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 8e390b4aff..afd407abe6 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -6,7 +6,7 @@ use crossbeam_channel::{unbounded as channel, Sender}; use futures::future; use libp2p::{ rpc::{RPCRequest, RPCResponse}, - HelloMessage, PeerId, RPCEvent, + PeerId, RPCEvent, }; use slog::debug; use slog::warn; @@ -85,7 +85,7 @@ impl MessageHandler { match message { // we have initiated a connection to a peer HandlerMessage::PeerDialed(peer_id) => { - self.sync.on_connect(&peer_id, &mut self.network_context); + self.sync.on_connect(peer_id, &mut self.network_context); } // we have received an RPC message request/response HandlerMessage::RPC(peer_id, rpc_event) => { @@ -113,7 +113,7 @@ impl MessageHandler { match request { RPCRequest::Hello(hello_message) => { self.sync - .on_hello(&peer_id, hello_message, &mut self.network_context) + .on_hello(peer_id, hello_message, &mut self.network_context) } // TODO: Handle all requests _ => {} @@ -136,26 +136,13 @@ impl MessageHandler { match response { RPCResponse::Hello(hello_message) => { debug!(self.log, "Hello response received from peer: {:?}", peer_id); - self.validate_hello(peer_id, hello_message); + self.sync + .on_hello(peer_id, hello_message, &mut self.network_context); } // TODO: Handle all responses _ => {} } } - - /// Validate a HELLO RPC message. - fn validate_hello(&mut self, peer_id: PeerId, message: HelloMessage) { - self.sync - .on_hello(&peer_id, message.clone(), &mut self.network_context); - // validate the peer - if !self.sync.validate_peer(peer_id.clone(), message) { - debug!( - self.log, - "Peer dropped due to mismatching HELLO messages: {:?}", peer_id - ); - //TODO: block/ban the peer - } - } } pub struct NetworkContext { @@ -179,6 +166,10 @@ impl NetworkContext { } } + pub fn disconnect(&self, _peer_id: PeerId) { + // TODO: disconnect peers. + } + pub fn send_rpc_request(&mut self, peer_id: PeerId, rpc_request: RPCRequest) { let id = self.generate_request_id(&peer_id); self.send_rpc_event( diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index d01188f5d0..c3045d280c 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -6,7 +6,6 @@ use crossbeam_channel::{unbounded as channel, Sender, TryRecvError}; use futures::prelude::*; use futures::sync::oneshot; use futures::Stream; -use libp2p::rpc::RPCResponse; use libp2p::RPCEvent; use libp2p::Service as LibP2PService; use libp2p::{Libp2pEvent, PeerId}; diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index ae6a9e7a1f..0d75f3739f 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,8 +1,9 @@ use crate::beacon_chain::BeaconChain; -use crate::message_handler::{MessageHandler, NetworkContext}; +use crate::message_handler::NetworkContext; use crate::service::NetworkMessage; use crossbeam_channel::Sender; -use libp2p::rpc::{HelloMessage, RPCMethod, RPCRequest, RPCResponse}; +use libp2p::rpc::methods::*; +use libp2p::rpc::{RPCRequest, RPCResponse}; use libp2p::PeerId; use slog::{debug, o}; use std::collections::HashMap; @@ -15,6 +16,7 @@ type NetworkSender = Sender; const SLOT_IMPORT_TOLERANCE: u64 = 100; /// Keeps track of syncing information for known connected peers. +#[derive(Clone, Copy)] pub struct PeerSyncInfo { latest_finalized_root: Hash256, latest_finalized_epoch: Epoch, @@ -23,18 +25,37 @@ pub struct PeerSyncInfo { } impl PeerSyncInfo { - pub fn is_on_chain(&self, chain: &Arc) -> bool { + fn is_on_chain(&self, chain: &Arc) -> bool { // TODO: make useful. true } - pub fn has_higher_finalized_epoch(&self, chain: &Arc) -> bool { + fn has_higher_finalized_epoch(&self, chain: &Arc) -> bool { self.latest_finalized_epoch > chain.get_state().finalized_epoch } - pub fn has_higher_best_slot(&self, chain: &Arc) -> bool { + fn has_higher_best_slot(&self, chain: &Arc) -> bool { self.latest_finalized_epoch > chain.get_state().finalized_epoch } + + pub fn status(&self, chain: &Arc) -> PeerStatus { + if self.has_higher_finalized_epoch(chain) { + PeerStatus::HigherFinalizedEpoch + } else if !self.is_on_chain(chain) { + PeerStatus::HigherFinalizedEpoch + } else if self.has_higher_best_slot(chain) { + PeerStatus::HigherBestSlot + } else { + PeerStatus::NotInteresting + } + } +} + +pub enum PeerStatus { + OnDifferentChain, + HigherFinalizedEpoch, + HigherBestSlot, + NotInteresting, } impl From for PeerSyncInfo { @@ -91,16 +112,13 @@ impl SimpleSync { } } - pub fn on_connect(&self, peer_id: &PeerId, network: &mut NetworkContext) { - network.send_rpc_request( - peer_id.clone(), - RPCRequest::Hello(self.chain.hello_message()), - ); + pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) { + network.send_rpc_request(peer_id, RPCRequest::Hello(self.chain.hello_message())); } pub fn on_hello_request( - &self, - peer_id: &PeerId, + &mut self, + peer_id: PeerId, hello: HelloMessage, network: &mut NetworkContext, ) { @@ -111,97 +129,63 @@ impl SimpleSync { self.on_hello(peer_id, hello, network); } - pub fn on_hello(&self, peer_id: &PeerId, hello: HelloMessage, network: &mut NetworkContext) { + pub fn on_hello(&mut self, peer_id: PeerId, hello: HelloMessage, network: &mut NetworkContext) { + let spec = self.chain.get_spec(); + // network id must match if hello.network_id != self.network_id { debug!(self.log, "Bad network id. Peer: {:?}", peer_id); + network.disconnect(peer_id); return; } let peer = PeerSyncInfo::from(hello); - - /* - if peer.has_higher_finalized_epoch(&self.chain) { - // we need blocks - let peer_slot = peer.latest_finalized_epoch.start_slot(spec.slots_per_epoch); - let our_slot = self.chain.finalized_epoch(); - let required_slots = peer_slot - our_slot; - } else { - if !peer.is_on_chain(&self.chain) { - return (true, responses); - } - // - } - */ - - /* - // compare latest epoch and finalized root to see if they exist in our chain - if peer_info.latest_finalized_epoch <= self.latest_finalized_epoch { - // ensure their finalized root is in our chain - // TODO: Get the finalized root at hello_message.latest_epoch and ensure they match - //if (hello_message.latest_finalized_root == self.chain.get_state() { - // return false; - // } - } - - // the client is valid, add it to our list of known_peers and request sync if required - // update peer list if peer already exists - let peer_info = PeerSyncInfo::from(hello); - debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); - self.known_peers.insert(peer_id, peer_info); + self.known_peers.insert(peer_id.clone(), peer); - // set state to sync - if self.state == SyncState::Idle - && hello_message.best_slot > self.latest_slot + SLOT_IMPORT_TOLERANCE - { + match peer.status(&self.chain) { + PeerStatus::OnDifferentChain => { + debug!(self.log, "Peer is on different chain. Peer: {:?}", peer_id); + + network.disconnect(peer_id); + } + PeerStatus::HigherFinalizedEpoch => { + let start_slot = peer.latest_finalized_epoch.start_slot(spec.slots_per_epoch); + let required_slots = start_slot - self.chain.slot(); + + self.request_block_roots(peer_id, start_slot, required_slots.as_u64(), network); + } + PeerStatus::HigherBestSlot => { + let start_slot = peer.best_slot; + let required_slots = start_slot - self.chain.slot(); + + self.request_block_roots(peer_id, start_slot, required_slots.as_u64(), network); + } + PeerStatus::NotInteresting => {} + } + } + + fn request_block_roots( + &mut self, + peer_id: PeerId, + start_slot: Slot, + count: u64, + network: &mut NetworkContext, + ) { + // Potentially set state to sync. + if self.state == SyncState::Idle && count > SLOT_IMPORT_TOLERANCE { self.state = SyncState::Downloading; - //TODO: Start requesting blocks from known peers. Ideally in batches } - true - */ + // TODO: handle count > max count. + network.send_rpc_request( + peer_id.clone(), + RPCRequest::BeaconBlockRoots(BeaconBlockRootsRequest { start_slot, count }), + ); } /// Generates our current state in the form of a HELLO RPC message. pub fn generate_hello(&self) -> HelloMessage { self.chain.hello_message() } - - 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 { - return false; - } - // compare latest epoch and finalized root to see if they exist in our chain - if hello_message.latest_finalized_epoch <= self.latest_finalized_epoch { - // ensure their finalized root is in our chain - // TODO: Get the finalized root at hello_message.latest_epoch and ensure they match - //if (hello_message.latest_finalized_root == self.chain.get_state() { - // return false; - // } - } - - // the client is valid, add it to our list of known_peers and request sync if required - // update peer list if peer already exists - let peer_info = PeerSyncInfo { - latest_finalized_root: hello_message.latest_finalized_root, - latest_finalized_epoch: hello_message.latest_finalized_epoch, - best_root: hello_message.best_root, - best_slot: hello_message.best_slot, - }; - - debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); - self.known_peers.insert(peer_id, peer_info); - - // set state to sync - if self.state == SyncState::Idle - && hello_message.best_slot > self.latest_slot + SLOT_IMPORT_TOLERANCE - { - self.state = SyncState::Downloading; - //TODO: Start requesting blocks from known peers. Ideally in batches - } - - true - } } diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index dc0832feda..b5635cf8cf 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -1,5 +1,6 @@ use beacon_chain::test_utils::TestingBeaconChainBuilder; use crossbeam_channel::{unbounded, Receiver, Sender}; +use libp2p::rpc::methods::*; use libp2p::rpc::{HelloMessage, RPCMethod, RPCRequest, RPCResponse}; use libp2p::{PeerId, RPCEvent}; use network::beacon_chain::BeaconChain as NetworkBeaconChain; @@ -9,6 +10,7 @@ use sloggers::terminal::{Destination, TerminalLoggerBuilder}; use sloggers::types::Severity; use sloggers::Build; use std::sync::Arc; +use std::time::Duration; use test_harness::BeaconChainHarness; use tokio::runtime::TaskExecutor; use types::{test_utils::TestingBeaconStateBuilder, *}; @@ -42,7 +44,9 @@ impl SyncNode { } fn recv(&self) -> NetworkMessage { - self.receiver.recv().unwrap() + self.receiver + .recv_timeout(Duration::from_millis(500)) + .unwrap() } fn recv_rpc_response(&self) -> RPCResponse { @@ -106,6 +110,12 @@ impl SyncMaster { } } + pub fn build_blocks(&mut self, blocks: usize) { + for _ in 0..blocks { + self.harness.advance_chain_with_block(); + } + } + pub fn response_id(&mut self, node: &SyncNode) -> u64 { let id = self.response_ids[node.id]; self.response_ids[node.id] += 1; @@ -140,6 +150,17 @@ impl SyncMaster { } } +fn assert_sent_block_root_request(node: &SyncNode, expected: BeaconBlockRootsRequest) { + let request = node.recv_rpc_request(); + + match request { + RPCRequest::BeaconBlockRoots(response) => { + assert_eq!(expected, response, "Bad block roots response"); + } + _ => assert!(false, "Did not get block root request"), + } +} + fn test_setup( state_builder: TestingBeaconStateBuilder, node_count: usize, @@ -178,7 +199,17 @@ fn first_test() { let (runtime, mut master, nodes) = test_setup(state_builder, node_count, &spec, logger.clone()); + master.build_blocks(10); + master.do_hello_with(&nodes[0]); + assert_sent_block_root_request( + &nodes[0], + BeaconBlockRootsRequest { + start_slot: Slot::new(1), + count: 10, + }, + ); + runtime.shutdown_now(); } From 7cbee46227fb51f581b9d9c9c8dfb4f058e9dd18 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 22 Mar 2019 10:08:40 +1100 Subject: [PATCH 012/191] Add FakeSignature and FakeAggregateSignature They replace Signature and FakeAggregateSignature when compling with debug. Compiling with release uses the real structs. --- .travis.yml | 2 + .../utils/bls/src/fake_aggregate_signature.rs | 117 ++++++++++++++++++ eth2/utils/bls/src/fake_signature.rs | 117 ++++++++++++++++++ eth2/utils/bls/src/lib.rs | 20 ++- 4 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 eth2/utils/bls/src/fake_aggregate_signature.rs create mode 100644 eth2/utils/bls/src/fake_signature.rs diff --git a/.travis.yml b/.travis.yml index d43d21a005..44e78ee048 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,9 @@ before_install: - sudo chown -R $USER /usr/local/include/google script: - cargo build --verbose --all + - cargo build --verbose --release --all - cargo test --verbose --all + - cargo test --verbose --release --all - cargo fmt --all -- --check rust: - stable diff --git a/eth2/utils/bls/src/fake_aggregate_signature.rs b/eth2/utils/bls/src/fake_aggregate_signature.rs new file mode 100644 index 0000000000..23e2b54ef3 --- /dev/null +++ b/eth2/utils/bls/src/fake_aggregate_signature.rs @@ -0,0 +1,117 @@ +use super::{fake_signature::FakeSignature, AggregatePublicKey}; +use serde::de::{Deserialize, Deserializer}; +use serde::ser::{Serialize, Serializer}; +use serde_hex::{encode as hex_encode, PrefixedHexVisitor}; +use ssz::{ + decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, +}; + +const SIGNATURE_LENGTH: usize = 48; + +/// A BLS aggregate signature. +/// +/// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ +/// serialization). +#[derive(Debug, PartialEq, Clone, Default, Eq)] +pub struct FakeAggregateSignature { + bytes: Vec, +} + +impl FakeAggregateSignature { + /// Creates a new all-zero's signature + pub fn new() -> Self { + Self::zero() + } + + /// Creates a new all-zero's signature + pub fn zero() -> Self { + Self { + bytes: vec![0; SIGNATURE_LENGTH], + } + } + + /// Does glorious nothing. + pub fn add(&mut self, _signature: &FakeSignature) { + // Do nothing. + } + + /// _Always_ returns `true`. + pub fn verify( + &self, + _msg: &[u8], + _domain: u64, + _aggregate_public_key: &AggregatePublicKey, + ) -> bool { + true + } + + /// _Always_ returns `true`. + pub fn verify_multiple( + &self, + _messages: &[&[u8]], + _domain: u64, + _aggregate_public_keys: &[&AggregatePublicKey], + ) -> bool { + true + } +} + +impl Encodable for FakeAggregateSignature { + fn ssz_append(&self, s: &mut SszStream) { + s.append_vec(&self.bytes); + } +} + +impl Decodable for FakeAggregateSignature { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (sig_bytes, i) = decode_ssz_list(bytes, i)?; + Ok((FakeAggregateSignature { bytes: sig_bytes }, i)) + } +} + +impl Serialize for FakeAggregateSignature { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&hex_encode(ssz_encode(self))) + } +} + +impl<'de> Deserialize<'de> for FakeAggregateSignature { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?; + let (obj, _) = <_>::ssz_decode(&bytes[..], 0) + .map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?; + Ok(obj) + } +} + +impl TreeHash for FakeAggregateSignature { + fn hash_tree_root(&self) -> Vec { + hash(&self.bytes) + } +} + +#[cfg(test)] +mod tests { + use super::super::{Keypair, Signature}; + use super::*; + use ssz::ssz_encode; + + #[test] + pub fn test_ssz_round_trip() { + let keypair = Keypair::random(); + + let mut original = FakeAggregateSignature::new(); + original.add(&Signature::new(&[42, 42], 0, &keypair.sk)); + + let bytes = ssz_encode(&original); + let (decoded, _) = FakeAggregateSignature::ssz_decode(&bytes, 0).unwrap(); + + assert_eq!(original, decoded); + } +} diff --git a/eth2/utils/bls/src/fake_signature.rs b/eth2/utils/bls/src/fake_signature.rs new file mode 100644 index 0000000000..81b7310c81 --- /dev/null +++ b/eth2/utils/bls/src/fake_signature.rs @@ -0,0 +1,117 @@ +use super::serde_vistors::HexVisitor; +use super::{PublicKey, SecretKey}; +use hex::encode as hex_encode; +use serde::de::{Deserialize, Deserializer}; +use serde::ser::{Serialize, Serializer}; +use ssz::{ + decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, +}; + +const SIGNATURE_LENGTH: usize = 48; + +/// A single BLS signature. +/// +/// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ +/// serialization). +#[derive(Debug, PartialEq, Clone, Eq)] +pub struct FakeSignature { + bytes: Vec, +} + +impl FakeSignature { + /// Creates a new all-zero's signature + pub fn new(_msg: &[u8], _domain: u64, _sk: &SecretKey) -> Self { + FakeSignature::zero() + } + + /// Creates a new all-zero's signature + pub fn zero() -> Self { + Self { + bytes: vec![0; SIGNATURE_LENGTH], + } + } + + /// Creates a new all-zero's signature + pub fn new_hashed(_x_real_hashed: &[u8], _x_imaginary_hashed: &[u8], _sk: &SecretKey) -> Self { + FakeSignature::zero() + } + + /// _Always_ returns `true`. + pub fn verify(&self, _msg: &[u8], _domain: u64, _pk: &PublicKey) -> bool { + true + } + + /// _Always_ returns true. + pub fn verify_hashed( + &self, + _x_real_hashed: &[u8], + _x_imaginary_hashed: &[u8], + _pk: &PublicKey, + ) -> bool { + true + } + + /// Returns a new empty signature. + pub fn empty_signature() -> Self { + FakeSignature::zero() + } +} + +impl Encodable for FakeSignature { + fn ssz_append(&self, s: &mut SszStream) { + s.append_vec(&self.bytes); + } +} + +impl Decodable for FakeSignature { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (sig_bytes, i) = decode_ssz_list(bytes, i)?; + Ok((FakeSignature { bytes: sig_bytes }, i)) + } +} + +impl TreeHash for FakeSignature { + fn hash_tree_root(&self) -> Vec { + hash(&self.bytes) + } +} + +impl Serialize for FakeSignature { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&hex_encode(ssz_encode(self))) + } +} + +impl<'de> Deserialize<'de> for FakeSignature { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let bytes = deserializer.deserialize_str(HexVisitor)?; + let (pubkey, _) = <_>::ssz_decode(&bytes[..], 0) + .map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?; + Ok(pubkey) + } +} + +#[cfg(test)] +mod tests { + use super::super::Keypair; + use super::*; + use ssz::ssz_encode; + + #[test] + pub fn test_ssz_round_trip() { + let keypair = Keypair::random(); + + let original = FakeSignature::new(&[42, 42], 0, &keypair.sk); + + let bytes = ssz_encode(&original); + let (decoded, _) = FakeSignature::ssz_decode(&bytes, 0).unwrap(); + + assert_eq!(original, decoded); + } +} diff --git a/eth2/utils/bls/src/lib.rs b/eth2/utils/bls/src/lib.rs index 38a1299084..32cce54713 100644 --- a/eth2/utils/bls/src/lib.rs +++ b/eth2/utils/bls/src/lib.rs @@ -2,19 +2,33 @@ extern crate bls_aggregates; extern crate ssz; mod aggregate_public_key; -mod aggregate_signature; mod keypair; mod public_key; mod secret_key; mod serde_vistors; + +#[cfg(not(debug_assertions))] +mod aggregate_signature; +#[cfg(not(debug_assertions))] mod signature; +#[cfg(not(debug_assertions))] +pub use crate::aggregate_signature::AggregateSignature; +#[cfg(not(debug_assertions))] +pub use crate::signature::Signature; + +#[cfg(debug_assertions)] +mod fake_aggregate_signature; +#[cfg(debug_assertions)] +mod fake_signature; +#[cfg(debug_assertions)] +pub use crate::fake_aggregate_signature::FakeAggregateSignature as AggregateSignature; +#[cfg(debug_assertions)] +pub use crate::fake_signature::FakeSignature as Signature; pub use crate::aggregate_public_key::AggregatePublicKey; -pub use crate::aggregate_signature::AggregateSignature; pub use crate::keypair::Keypair; pub use crate::public_key::PublicKey; pub use crate::secret_key::SecretKey; -pub use crate::signature::Signature; pub const BLS_AGG_SIG_BYTE_SIZE: usize = 96; From 13872dfbb9e01b24de173bf5e169e0e5543936e6 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Fri, 22 Mar 2019 10:38:45 +1100 Subject: [PATCH 013/191] Added the release and debug build/tests to Jenkins, as well as Travis. --- Jenkinsfile | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 42755d5f7f..1a3afad87f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,20 +1,22 @@ pipeline { - agent { + agent { dockerfile { filename 'Dockerfile' args '-v cargo-cache:/cargocache:rw -e "CARGO_HOME=/cargocache"' } } - stages { - stage('Build') { - steps { - sh 'cargo build' - } - } - stage('Test') { + stages { + stage('Build') { steps { - sh 'cargo test --all' + sh 'cargo build --verbose --all' + sh 'cargo build --verbose --all --release' } } - } + stage('Test') { + steps { + sh 'cargo test --verbose --all' + sh 'cargo test --verbose --all --release' + } + } + } } From 4fc6e435d2a0c79c93fd1dc474404da6a3a67ce3 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 22 Mar 2019 11:38:57 +1100 Subject: [PATCH 014/191] Add type alias to test_harness --- .../beacon_chain/test_harness/src/beacon_chain_harness.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 4b5d5558f5..1207fcf28f 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 @@ -15,6 +15,8 @@ use std::iter::FromIterator; use std::sync::Arc; use types::{test_utils::TestingBeaconStateBuilder, *}; +type TestingBeaconChain = BeaconChain>; + /// The beacon chain harness simulates a single beacon node with `validator_count` validators connected /// to it. Each validator is provided a borrow to the beacon chain, where it may read /// information and submit blocks/attestations for processing. @@ -23,7 +25,7 @@ use types::{test_utils::TestingBeaconStateBuilder, *}; /// is not useful for testing that multiple beacon nodes can reach consensus. pub struct BeaconChainHarness { pub db: Arc, - pub beacon_chain: Arc>>, + pub beacon_chain: Arc, pub block_store: Arc>, pub state_store: Arc>, pub validators: Vec, From f96a3282b5072b6f30e16b9e94cccbfe64d7a3a7 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 22 Mar 2019 11:39:16 +1100 Subject: [PATCH 015/191] Pass first sync test --- beacon_node/network/src/sync/simple_sync.rs | 85 ++++++++++----------- beacon_node/network/tests/tests.rs | 62 ++++++++++----- 2 files changed, 81 insertions(+), 66 deletions(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 6a40ecf60e..0b2f736c1d 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,7 +1,5 @@ use crate::beacon_chain::BeaconChain; use crate::message_handler::NetworkContext; -use crate::service::NetworkMessage; -use crossbeam_channel::Sender; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse}; use eth2_libp2p::PeerId; @@ -10,14 +8,13 @@ use std::collections::HashMap; use std::sync::Arc; use types::{Epoch, Hash256, Slot}; -type NetworkSender = Sender; - /// The number of slots that we can import blocks ahead of us, before going into full Sync mode. const SLOT_IMPORT_TOLERANCE: u64 = 100; /// Keeps track of syncing information for known connected peers. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub struct PeerSyncInfo { + network_id: u8, latest_finalized_root: Hash256, latest_finalized_epoch: Epoch, best_root: Hash256, @@ -25,25 +22,24 @@ pub struct PeerSyncInfo { } impl PeerSyncInfo { - fn is_on_chain(&self, chain: &Arc) -> bool { - // TODO: make useful. - true + fn is_on_same_chain(&self, other: Self) -> bool { + self.network_id == other.network_id } - fn has_higher_finalized_epoch(&self, chain: &Arc) -> bool { - self.latest_finalized_epoch > chain.get_state().finalized_epoch + fn has_higher_finalized_epoch_than(&self, other: Self) -> bool { + self.latest_finalized_epoch > other.latest_finalized_epoch } - fn has_higher_best_slot(&self, chain: &Arc) -> bool { - self.latest_finalized_epoch > chain.get_state().finalized_epoch + fn has_higher_best_slot_than(&self, other: Self) -> bool { + self.best_slot > other.best_slot } - pub fn status(&self, chain: &Arc) -> PeerStatus { - if self.has_higher_finalized_epoch(chain) { + pub fn status_compared_to(&self, other: Self) -> PeerStatus { + if self.has_higher_finalized_epoch_than(other) { PeerStatus::HigherFinalizedEpoch - } else if !self.is_on_chain(chain) { - PeerStatus::HigherFinalizedEpoch - } else if self.has_higher_best_slot(chain) { + } else if !self.is_on_same_chain(other) { + PeerStatus::OnDifferentChain + } else if self.has_higher_best_slot_than(other) { PeerStatus::HigherBestSlot } else { PeerStatus::NotInteresting @@ -62,6 +58,7 @@ pub enum PeerStatus { impl From for PeerSyncInfo { fn from(hello: HelloMessage) -> PeerSyncInfo { PeerSyncInfo { + network_id: hello.network_id, latest_finalized_root: hello.latest_finalized_root, latest_finalized_epoch: hello.latest_finalized_epoch, best_root: hello.best_root, @@ -70,6 +67,12 @@ impl From for PeerSyncInfo { } } +impl From<&Arc> for PeerSyncInfo { + fn from(chain: &Arc) -> PeerSyncInfo { + Self::from(chain.hello_message()) + } +} + /// The current syncing state. #[derive(PartialEq)] pub enum SyncState { @@ -88,12 +91,6 @@ pub struct SimpleSync { known_peers: HashMap, /// The current state of the syncing protocol. state: SyncState, - /// The network id, for quick HELLO RPC message lookup. - network_id: u8, - /// The latest epoch of the syncing chain. - latest_finalized_epoch: Epoch, - /// The latest block of the syncing chain. - latest_slot: Slot, /// Sync logger. log: slog::Logger, } @@ -106,9 +103,6 @@ impl SimpleSync { chain: beacon_chain.clone(), known_peers: HashMap::new(), state: SyncState::Idle, - network_id: beacon_chain.get_spec().network_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, } } @@ -133,40 +127,39 @@ impl SimpleSync { pub fn on_hello(&mut self, peer_id: PeerId, hello: HelloMessage, network: &mut NetworkContext) { let spec = self.chain.get_spec(); + let remote = PeerSyncInfo::from(hello); + let local = PeerSyncInfo::from(&self.chain); + let remote_status = remote.status_compared_to(local); + // network id must match - if hello.network_id != self.network_id { - debug!(self.log, "Bad network id. Peer: {:?}", peer_id); - network.disconnect(peer_id); - return; + if remote_status != PeerStatus::OnDifferentChain { + debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); + self.known_peers.insert(peer_id.clone(), remote); } - let peer = PeerSyncInfo::from(hello); - debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); - self.known_peers.insert(peer_id.clone(), peer); - - debug!( - self.log, - "Peer hello. Status: {:?}", - peer.status(&self.chain) - ); - - match peer.status(&self.chain) { + match remote_status { PeerStatus::OnDifferentChain => { debug!(self.log, "Peer is on different chain. Peer: {:?}", peer_id); network.disconnect(peer_id); } PeerStatus::HigherFinalizedEpoch => { - let start_slot = peer.latest_finalized_epoch.start_slot(spec.slots_per_epoch); - let required_slots = start_slot - self.chain.slot(); + let start_slot = remote + .latest_finalized_epoch + .start_slot(spec.slots_per_epoch); + let required_slots = start_slot - local.best_slot; self.request_block_roots(peer_id, start_slot, required_slots.as_u64(), network); } PeerStatus::HigherBestSlot => { - let start_slot = peer.best_slot; - let required_slots = start_slot - self.chain.slot(); + let required_slots = remote.best_slot - local.best_slot; - self.request_block_roots(peer_id, start_slot, required_slots.as_u64(), network); + self.request_block_roots( + peer_id, + local.best_slot, + required_slots.as_u64(), + network, + ); } PeerStatus::NotInteresting => {} } diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index 7941ffb991..dea57982e7 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -1,4 +1,3 @@ -use beacon_chain::test_utils::TestingBeaconChainBuilder; use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender}; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCMethod, RPCRequest, RPCResponse}; @@ -9,7 +8,6 @@ use network::service::{NetworkMessage, OutgoingMessage}; use sloggers::terminal::{Destination, TerminalLoggerBuilder}; use sloggers::types::Severity; use sloggers::Build; -use std::sync::Arc; use std::time::Duration; use test_harness::BeaconChainHarness; use tokio::runtime::TaskExecutor; @@ -19,26 +17,40 @@ pub struct SyncNode { pub id: usize, sender: Sender, receiver: Receiver, + harness: BeaconChainHarness, } impl SyncNode { - pub fn new( + fn from_beacon_state_builder( id: usize, executor: &TaskExecutor, - chain: Arc, + state_builder: TestingBeaconStateBuilder, + spec: &ChainSpec, logger: slog::Logger, ) -> Self { + let harness = BeaconChainHarness::from_beacon_state_builder(state_builder, spec.clone()); + let (network_sender, network_receiver) = unbounded(); - let message_handler_sender = - MessageHandler::spawn(chain, network_sender, executor, logger).unwrap(); + let message_handler_sender = MessageHandler::spawn( + harness.beacon_chain.clone(), + network_sender, + executor, + logger, + ) + .unwrap(); Self { id, sender: message_handler_sender, receiver: network_receiver, + harness, } } + fn increment_beacon_chain_slot(&mut self) { + self.harness.increment_beacon_chain_slot(); + } + fn send(&self, message: HandlerMessage) { self.sender.send(message).unwrap(); } @@ -47,7 +59,11 @@ impl SyncNode { self.receiver.recv_timeout(Duration::from_millis(500)) } - fn recv_rpc_response(&self) -> Result { + fn hello_message(&self) -> HelloMessage { + self.harness.beacon_chain.hello_message() + } + + fn _recv_rpc_response(&self) -> Result { let network_message = self.recv()?; Ok(match network_message { NetworkMessage::Send( @@ -108,12 +124,6 @@ impl SyncMaster { } } - pub fn build_blocks(&mut self, blocks: usize) { - for _ in 0..blocks { - self.harness.advance_chain_with_block(); - } - } - pub fn response_id(&mut self, node: &SyncNode) -> u64 { let id = self.response_ids[node.id]; self.response_ids[node.id] += 1; @@ -169,11 +179,11 @@ fn test_setup( let mut nodes = Vec::with_capacity(node_count); for id in 0..node_count { - let local_chain = TestingBeaconChainBuilder::from(state_builder.clone()).build(&spec); - let node = SyncNode::new( + let node = SyncNode::from_beacon_state_builder( id, &runtime.executor(), - Arc::new(local_chain), + state_builder.clone(), + &spec, logger.clone(), ); @@ -185,6 +195,15 @@ fn test_setup( (runtime, master, nodes) } +pub fn build_blocks(blocks: usize, master: &mut SyncMaster, nodes: &mut Vec) { + for _ in 0..blocks { + master.harness.advance_chain_with_block(); + for i in 0..nodes.len() { + nodes[i].increment_beacon_chain_slot(); + } + } +} + #[test] fn first_test() { let logger = get_logger(); @@ -195,17 +214,20 @@ fn first_test() { let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec); - let (runtime, mut master, nodes) = test_setup(state_builder, node_count, &spec, logger.clone()); + let (runtime, mut master, mut nodes) = + test_setup(state_builder, node_count, &spec, logger.clone()); - master.build_blocks(10); + let original_node_slot = nodes[0].hello_message().best_slot; + + build_blocks(2, &mut master, &mut nodes); master.do_hello_with(&nodes[0]); assert_sent_block_root_request( &nodes[0], BeaconBlockRootsRequest { - start_slot: Slot::new(1), - count: 10, + start_slot: original_node_slot, + count: 2, }, ); From e758e717533f22ca73223da2047d7bb547cc9f47 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 12:13:48 +1100 Subject: [PATCH 016/191] 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 017/191] 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 018/191] 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 019/191] 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 020/191] 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 96ba1c8f77064a24c3a8ab1b03954d80d333c21b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 22 Mar 2019 14:20:49 +1100 Subject: [PATCH 021/191] Implement `get_block_roots` for syncing --- beacon_node/beacon_chain/src/beacon_chain.rs | 64 ++++++++++++++++++++ beacon_node/network/src/beacon_chain.rs | 16 ++++- beacon_node/network/src/message_handler.rs | 13 +++- beacon_node/network/src/sync/simple_sync.rs | 12 ++++ beacon_node/network/tests/tests.rs | 61 +++++++++++++------ 5 files changed, 146 insertions(+), 20 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 4b151d70bd..dccd9842ec 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -122,6 +122,70 @@ where }) } + /// Returns beacon block roots for `count` slots, starting from `start_slot`. + /// + /// ## Errors: + /// + /// - `SlotOutOfBounds`: Unable to return the full specified range. + /// - `SlotOutOfBounds`: Unable to load a state from the DB. + /// - `SlotOutOfBounds`: Start slot is higher than the first slot. + /// - Other: BeaconState` is inconsistent. + pub fn get_block_roots( + &self, + start_slot: Slot, + count: Slot, + ) -> Result, BeaconStateError> { + let spec = &self.spec; + + let mut roots: Vec = vec![]; + let mut state = self.state.read().clone(); + let mut slot = start_slot + count - 1; + + loop { + // Return if the slot required is greater than the current state. + if slot >= state.slot { + return Err(BeaconStateError::SlotOutOfBounds); + } + + // If the slot is within the range of the current state's block roots, append the root + // to the output vec. + // + // If we get `SlotOutOfBounds` error, load the oldest known state to the present state + // from the DB. + match state.get_block_root(slot, spec) { + Ok(root) => { + roots.push(*root); + + if slot == start_slot { + break; + } else { + slot -= 1; + } + } + Err(BeaconStateError::SlotOutOfBounds) => { + // Read the earliest historic state in the current slot. + let earliest_historic_slot = + state.slot - Slot::from(spec.slots_per_historical_root); + // Load the earlier state from disk. + let new_state_root = state.get_state_root(earliest_historic_slot, spec)?; + + // Break if the DB is unable to load the state. + state = match self.state_store.get_deserialized(&new_state_root) { + Ok(Some(state)) => state, + _ => break, + } + } + Err(e) => return Err(e), + }; + } + + if (slot == start_slot) && (roots.len() == count.as_usize()) { + Ok(roots) + } else { + Err(BeaconStateError::SlotOutOfBounds) + } + } + /// Update the canonical head to some new values. pub fn update_canonical_head( &self, diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 5246c87c2a..ba429e688e 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -8,7 +8,7 @@ use beacon_chain::{ CheckPoint, }; use eth2_libp2p::HelloMessage; -use types::{Epoch, Hash256, Slot}; +use types::{BeaconStateError, Epoch, Hash256, Slot}; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { @@ -29,6 +29,12 @@ pub trait BeaconChain: Send + Sync { fn finalized_epoch(&self) -> Epoch; fn hello_message(&self) -> HelloMessage; + + fn get_block_roots( + &self, + start_slot: Slot, + count: Slot, + ) -> Result, BeaconStateError>; } impl BeaconChain for RawBeaconChain @@ -81,4 +87,12 @@ where best_slot: self.best_slot(), } } + + fn get_block_roots( + &self, + start_slot: Slot, + count: Slot, + ) -> Result, BeaconStateError> { + self.get_block_roots(start_slot, count) + } } diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 5b39de997a..99a263ed85 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -139,8 +139,19 @@ impl MessageHandler { self.sync .on_hello(peer_id, hello_message, &mut self.network_context); } + RPCResponse::BeaconBlockRoots(response) => { + debug!( + self.log, + "BeaconBlockRoots response received from peer: {:?}", peer_id + ); + self.sync.on_beacon_block_roots_response( + peer_id, + response, + &mut self.network_context, + ) + } // TODO: Handle all responses - _ => {} + _ => panic!("Unknown response: {:?}", response), } } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 0b2f736c1d..ab29d0db50 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -165,6 +165,15 @@ impl SimpleSync { } } + pub fn on_beacon_block_roots_response( + &mut self, + peer_id: PeerId, + reponse: BeaconBlockRootsResponse, + network: &mut NetworkContext, + ) { + // + } + fn request_block_roots( &mut self, peer_id: PeerId, @@ -174,9 +183,12 @@ impl SimpleSync { ) { // Potentially set state to sync. if self.state == SyncState::Idle && count > SLOT_IMPORT_TOLERANCE { + debug!(self.log, "Entering downloading sync state."); self.state = SyncState::Downloading; } + debug!(self.log, "Requesting {} blocks from {:?}.", count, &peer_id); + // TODO: handle count > max count. network.send_rpc_request( peer_id.clone(), diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index dea57982e7..fbfb827f20 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -63,6 +63,15 @@ impl SyncNode { self.harness.beacon_chain.hello_message() } + pub fn get_block_root_request(&self) -> BeaconBlockRootsRequest { + let request = self.recv_rpc_request().expect("No block root request"); + + match request { + RPCRequest::BeaconBlockRoots(response) => response, + _ => panic!("Did not get block root request"), + } + } + fn _recv_rpc_response(&self) -> Result { let network_message = self.recv()?; Ok(match network_message { @@ -146,6 +155,34 @@ impl SyncMaster { } } + pub fn respond_to_block_roots_request( + &mut self, + node: &SyncNode, + request: BeaconBlockRootsRequest, + ) { + let roots = self + .harness + .beacon_chain + .get_block_roots(request.start_slot, Slot::from(request.count)) + .expect("Beacon chain did not give blocks"); + + let roots = roots + .iter() + .enumerate() + .map(|(i, root)| BlockRootSlot { + block_root: *root, + slot: Slot::from(i) + request.start_slot, + }) + .collect(); + + let response = RPCResponse::BeaconBlockRoots(BeaconBlockRootsResponse { roots }); + self.send_rpc_response(node, response) + } + + fn send_rpc_response(&mut self, node: &SyncNode, rpc_response: RPCResponse) { + node.send(self.rpc_response(node, rpc_response)); + } + fn rpc_response(&mut self, node: &SyncNode, rpc_response: RPCResponse) -> HandlerMessage { HandlerMessage::RPC( self.peer_id.clone(), @@ -158,17 +195,6 @@ impl SyncMaster { } } -fn assert_sent_block_root_request(node: &SyncNode, expected: BeaconBlockRootsRequest) { - let request = node.recv_rpc_request().expect("No block root request"); - - match request { - RPCRequest::BeaconBlockRoots(response) => { - assert_eq!(expected, response, "Bad block roots response"); - } - _ => assert!(false, "Did not get block root request"), - } -} - fn test_setup( state_builder: TestingBeaconStateBuilder, node_count: usize, @@ -223,13 +249,12 @@ fn first_test() { master.do_hello_with(&nodes[0]); - assert_sent_block_root_request( - &nodes[0], - BeaconBlockRootsRequest { - start_slot: original_node_slot, - count: 2, - }, - ); + let request = nodes[0].get_block_root_request(); + assert_eq!(request.start_slot, original_node_slot); + assert_eq!(request.count, 2); + master.respond_to_block_roots_request(&nodes[0], request); + + std::thread::sleep(Duration::from_millis(500)); runtime.shutdown_now(); } From a4cfe682726a035b8f4dce4d395074f98205e8f6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 16:46:52 +1100 Subject: [PATCH 022/191] 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 023/191] 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 024/191] 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 025/191] 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 026/191] 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 027/191] 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 028/191] 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 029/191] 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 030/191] 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 031/191] 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 4b5b5851a64a4cd2920bb7f4ba0a10b6a5d1ac0b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 23 Mar 2019 13:23:44 +1100 Subject: [PATCH 032/191] Allow sync to to request block bodies. --- beacon_node/beacon_chain/src/beacon_chain.rs | 30 ++- beacon_node/eth2-libp2p/src/rpc/methods.rs | 2 +- beacon_node/network/Cargo.toml | 1 + beacon_node/network/src/beacon_chain.rs | 16 +- beacon_node/network/src/message_handler.rs | 17 +- beacon_node/network/src/sync/simple_sync.rs | 200 +++++++++++++++++-- beacon_node/network/tests/tests.rs | 72 ++++++- 7 files changed, 305 insertions(+), 33 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index dccd9842ec..33198f0a39 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -142,8 +142,14 @@ where let mut slot = start_slot + count - 1; loop { - // Return if the slot required is greater than the current state. - if slot >= state.slot { + // If the highest slot requested is that of the current state insert the root of the + // head block, unless the head block's slot is not matching. + if slot == state.slot && self.head().beacon_block.slot == slot { + roots.push(self.head().beacon_block_root); + + slot -= 1; + continue; + } else if slot >= state.slot { return Err(BeaconStateError::SlotOutOfBounds); } @@ -180,12 +186,25 @@ where } if (slot == start_slot) && (roots.len() == count.as_usize()) { - Ok(roots) + // Reverse the ordering of the roots. We extracted them in reverse order to make it + // simpler to lookup historic states. + // + // This is a potential optimisation target. + Ok(roots.iter().rev().cloned().collect()) } else { Err(BeaconStateError::SlotOutOfBounds) } } + /// Returns the block at the given root, if any. + /// + /// ## Errors + /// + /// May return a database error. + pub fn get_block(&self, block_root: &Hash256) -> Result, Error> { + Ok(self.block_store.get_deserialized(block_root)?) + } + /// Update the canonical head to some new values. pub fn update_canonical_head( &self, @@ -622,6 +641,11 @@ where } } + /// Returns `true` if the given block root has not been processed. + pub fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result { + Ok(!self.block_store.exists(beacon_block_root)?) + } + /// Accept some block and attempt to add it to block DAG. /// /// Will accept blocks from prior slots, however it will reject any block from a future slot. diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index 381fc8b015..f6a5f28296 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -162,7 +162,7 @@ pub struct BeaconBlockHeadersResponse { #[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockBodiesRequest { /// The list of beacon block bodies being requested. - pub block_roots: Hash256, + pub block_roots: Vec, } /// Response containing the list of requested beacon block bodies. diff --git a/beacon_node/network/Cargo.toml b/beacon_node/network/Cargo.toml index a53097159b..c6411a0205 100644 --- a/beacon_node/network/Cargo.toml +++ b/beacon_node/network/Cargo.toml @@ -14,6 +14,7 @@ eth2-libp2p = { path = "../eth2-libp2p" } version = { path = "../version" } types = { path = "../../eth2/types" } slog = "2.4.1" +ssz = { path = "../../eth2/utils/ssz" } futures = "0.1.25" error-chain = "0.12.0" crossbeam-channel = "0.3.8" diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index ba429e688e..e2829cfa61 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -8,7 +8,9 @@ use beacon_chain::{ CheckPoint, }; use eth2_libp2p::HelloMessage; -use types::{BeaconStateError, Epoch, Hash256, Slot}; +use types::{BeaconBlock, BeaconStateError, Epoch, Hash256, Slot}; + +pub use beacon_chain::BeaconChainError; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { @@ -20,6 +22,8 @@ pub trait BeaconChain: Send + Sync { fn head(&self) -> RwLockReadGuard; + fn get_block(&self, block_root: &Hash256) -> Result, BeaconChainError>; + fn best_slot(&self) -> Slot; fn best_block_root(&self) -> Hash256; @@ -35,6 +39,8 @@ pub trait BeaconChain: Send + Sync { start_slot: Slot, count: Slot, ) -> Result, BeaconStateError>; + + fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result; } impl BeaconChain for RawBeaconChain @@ -59,6 +65,10 @@ where self.head() } + fn get_block(&self, block_root: &Hash256) -> Result, BeaconChainError> { + self.get_block(block_root) + } + fn finalized_epoch(&self) -> Epoch { self.get_state().finalized_epoch } @@ -95,4 +105,8 @@ where ) -> Result, BeaconStateError> { self.get_block_roots(start_slot, count) } + + fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result { + self.is_new_block_root(beacon_block_root) + } } diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 99a263ed85..1a790eee1f 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -142,7 +142,7 @@ impl MessageHandler { RPCResponse::BeaconBlockRoots(response) => { debug!( self.log, - "BeaconBlockRoots response received from peer: {:?}", peer_id + "BeaconBlockRoots response received"; "peer" => format!("{:?}", peer_id) ); self.sync.on_beacon_block_roots_response( peer_id, @@ -150,6 +150,17 @@ impl MessageHandler { &mut self.network_context, ) } + RPCResponse::BeaconBlockHeaders(response) => { + debug!( + self.log, + "BeaconBlockHeaders response received"; "peer" => format!("{:?}", peer_id) + ); + self.sync.on_beacon_block_headers_response( + peer_id, + response, + &mut self.network_context, + ) + } // TODO: Handle all responses _ => panic!("Unknown response: {:?}", response), } @@ -233,10 +244,6 @@ impl NetworkContext { }; // register RPC request self.requests.insert((peer_id.clone(), id), Instant::now()); - debug!( - self.log, - "Hello request registered with peer: {:?}", peer_id - ); id } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index ab29d0db50..ee0646dbb0 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -3,10 +3,12 @@ use crate::message_handler::NetworkContext; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse}; use eth2_libp2p::PeerId; -use slog::{debug, o}; +use slog::{debug, error, o, warn}; +use ssz::TreeHash; use std::collections::HashMap; use std::sync::Arc; -use types::{Epoch, Hash256, Slot}; +use std::time::Instant; +use types::{BeaconBlockHeader, 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; @@ -78,7 +80,7 @@ impl From<&Arc> for PeerSyncInfo { pub enum SyncState { Idle, Downloading, - Stopped, + _Stopped, } /// Simple Syncing protocol. @@ -89,6 +91,8 @@ pub struct SimpleSync { chain: Arc, /// A mapping of Peers to their respective PeerSyncInfo. known_peers: HashMap, + /// A queue to allow importing of blocks + import_queue: ImportQueue, /// The current state of the syncing protocol. state: SyncState, /// Sync logger. @@ -97,11 +101,12 @@ pub struct SimpleSync { impl SimpleSync { pub fn new(beacon_chain: Arc, log: &slog::Logger) -> Self { - let state = beacon_chain.get_state(); let sync_logger = log.new(o!("Service"=> "Sync")); + let import_queue = ImportQueue::new(beacon_chain.clone(), log.clone()); SimpleSync { chain: beacon_chain.clone(), known_peers: HashMap::new(), + import_queue, state: SyncState::Idle, log: sync_logger, } @@ -149,15 +154,24 @@ impl SimpleSync { .start_slot(spec.slots_per_epoch); let required_slots = start_slot - local.best_slot; - self.request_block_roots(peer_id, start_slot, required_slots.as_u64(), network); + self.request_block_roots( + peer_id, + BeaconBlockRootsRequest { + start_slot, + count: required_slots.into(), + }, + network, + ); } PeerStatus::HigherBestSlot => { let required_slots = remote.best_slot - local.best_slot; self.request_block_roots( peer_id, - local.best_slot, - required_slots.as_u64(), + BeaconBlockRootsRequest { + start_slot: local.best_slot + 1, + count: required_slots.into(), + }, network, ); } @@ -168,32 +182,109 @@ impl SimpleSync { pub fn on_beacon_block_roots_response( &mut self, peer_id: PeerId, - reponse: BeaconBlockRootsResponse, + response: BeaconBlockRootsResponse, network: &mut NetworkContext, ) { + if response.roots.is_empty() { + warn!( + self.log, + "Peer returned empty block roots response. PeerId: {:?}", peer_id + ); + return; + } + + let new_root_index = self.import_queue.first_new_root(&response.roots); + + // If a new block root is found, request it and all the headers following it. // + // We make an assumption here that if we don't know a block then we don't know of all + // it's parents. This might not be the case if syncing becomes more sophisticated. + if let Some(i) = new_root_index { + let new = &response.roots[i]; + + self.request_block_headers( + peer_id, + BeaconBlockHeadersRequest { + start_root: new.block_root, + start_slot: new.slot, + max_headers: (response.roots.len() - i) as u64, + skip_slots: 0, + }, + network, + ) + } + } + + pub fn on_beacon_block_headers_response( + &mut self, + peer_id: PeerId, + response: BeaconBlockHeadersResponse, + network: &mut NetworkContext, + ) { + if response.headers.is_empty() { + warn!( + self.log, + "Peer returned empty block headers response. PeerId: {:?}", peer_id + ); + return; + } + + let block_roots = self.import_queue.enqueue_headers(response.headers); + + if !block_roots.is_empty() { + self.request_block_bodies(peer_id, BeaconBlockBodiesRequest { block_roots }, network); + } } fn request_block_roots( &mut self, peer_id: PeerId, - start_slot: Slot, - count: u64, + request: BeaconBlockRootsRequest, network: &mut NetworkContext, ) { // Potentially set state to sync. - if self.state == SyncState::Idle && count > SLOT_IMPORT_TOLERANCE { + if self.state == SyncState::Idle && request.count > SLOT_IMPORT_TOLERANCE { debug!(self.log, "Entering downloading sync state."); self.state = SyncState::Downloading; } - debug!(self.log, "Requesting {} blocks from {:?}.", count, &peer_id); + debug!( + self.log, + "Requesting {} block roots from {:?}.", request.count, &peer_id + ); // TODO: handle count > max count. - network.send_rpc_request( - peer_id.clone(), - RPCRequest::BeaconBlockRoots(BeaconBlockRootsRequest { start_slot, count }), + network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockRoots(request)); + } + + fn request_block_headers( + &mut self, + peer_id: PeerId, + request: BeaconBlockHeadersRequest, + network: &mut NetworkContext, + ) { + debug!( + self.log, + "Requesting {} headers from {:?}.", request.max_headers, &peer_id ); + + network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockHeaders(request)); + } + + fn request_block_bodies( + &mut self, + peer_id: PeerId, + request: BeaconBlockBodiesRequest, + network: &mut NetworkContext, + ) { + debug!( + self.log, + "Requesting {} bodies from {:?}.", + request.block_roots.len(), + &peer_id + ); + + network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockBodies(request)); } /// Generates our current state in the form of a HELLO RPC message. @@ -201,3 +292,82 @@ impl SimpleSync { self.chain.hello_message() } } + +pub struct ImportQueue { + /// BeaconChain + pub chain: Arc, + /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. + pub partials: HashMap, + /// Logging + log: slog::Logger, +} + +impl ImportQueue { + pub fn new(chain: Arc, log: slog::Logger) -> Self { + Self { + chain, + partials: HashMap::new(), + log, + } + } + + fn is_new_block(&self, block_root: &Hash256) -> bool { + self.chain + .is_new_block_root(&block_root) + .unwrap_or_else(|_| { + error!(self.log, "Unable to determine if block is new."); + true + }) + } + + /// Returns the index of the first new root in the list of block roots. + pub fn first_new_root(&mut self, roots: &[BlockRootSlot]) -> Option { + for root in roots { + println!("root {}", root.block_root); + } + roots + .iter() + .position(|brs| self.is_new_block(&brs.block_root)) + } + + /// Adds the `headers` to the `partials` queue. Returns a list of `Hash256` block roots for + /// which we should use to request `BeaconBlockBodies`. + /// + /// If a `header` is not in the queue and has not been processed by the chain it is added to + /// the queue and it's block root is included in the output. + /// + /// If a `header` is already in the queue, but not yet processed by the chain the block root is + /// included in the output and the `inserted` time for the partial record is set to + /// `Instant::now()`. Updating the `inserted` time stops the partial from becoming stale. + pub fn enqueue_headers(&mut self, headers: Vec) -> Vec { + let mut required_bodies: Vec = vec![]; + + for header in headers { + let block_root = Hash256::from_slice(&header.hash_tree_root()[..]); + + if self.is_new_block(&block_root) { + self.insert_partial(block_root, header); + required_bodies.push(block_root) + } + } + + required_bodies + } + + fn insert_partial(&mut self, block_root: Hash256, header: BeaconBlockHeader) { + self.partials.insert( + header.block_body_root, + PartialBeaconBlock { + block_root, + header, + inserted: Instant::now(), + }, + ); + } +} + +pub struct PartialBeaconBlock { + pub block_root: Hash256, + pub header: BeaconBlockHeader, + pub inserted: Instant, +} diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index fbfb827f20..076a3f529a 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -67,7 +67,16 @@ impl SyncNode { let request = self.recv_rpc_request().expect("No block root request"); match request { - RPCRequest::BeaconBlockRoots(response) => response, + RPCRequest::BeaconBlockRoots(request) => request, + _ => panic!("Did not get block root request"), + } + } + + pub fn get_block_headers_request(&self) -> BeaconBlockHeadersRequest { + let request = self.recv_rpc_request().expect("No block headers request"); + + match request { + RPCRequest::BeaconBlockHeaders(request) => request, _ => panic!("Did not get block root request"), } } @@ -164,9 +173,7 @@ impl SyncMaster { .harness .beacon_chain .get_block_roots(request.start_slot, Slot::from(request.count)) - .expect("Beacon chain did not give blocks"); - - let roots = roots + .expect("Beacon chain did not give block roots") .iter() .enumerate() .map(|(i, root)| BlockRootSlot { @@ -179,6 +186,43 @@ impl SyncMaster { self.send_rpc_response(node, response) } + pub fn respond_to_block_headers_request( + &mut self, + node: &SyncNode, + request: BeaconBlockHeadersRequest, + ) { + let roots = self + .harness + .beacon_chain + .get_block_roots(request.start_slot, Slot::from(request.max_headers)) + .expect("Beacon chain did not give blocks"); + + if roots.is_empty() { + panic!("Roots was empty when trying to get headers.") + } + + assert_eq!( + roots[0], request.start_root, + "Got the wrong start root when getting headers" + ); + + let headers: Vec = roots + .iter() + .map(|root| { + let block = self + .harness + .beacon_chain + .get_block(root) + .expect("Failed to load block") + .expect("Block did not exist"); + block.block_header() + }) + .collect(); + + let response = RPCResponse::BeaconBlockHeaders(BeaconBlockHeadersResponse { headers }); + self.send_rpc_response(node, response) + } + fn send_rpc_response(&mut self, node: &SyncNode, rpc_response: RPCResponse) { node.send(self.rpc_response(node, rpc_response)); } @@ -228,6 +272,11 @@ pub fn build_blocks(blocks: usize, master: &mut SyncMaster, nodes: &mut Vec Date: Sat, 23 Mar 2019 18:48:09 +1100 Subject: [PATCH 033/191] Implement block imports for sync --- beacon_node/beacon_chain/src/beacon_chain.rs | 24 ++- beacon_node/network/src/beacon_chain.rs | 12 +- beacon_node/network/src/message_handler.rs | 11 ++ beacon_node/network/src/sync/simple_sync.rs | 160 +++++++++++++++++-- beacon_node/network/tests/tests.rs | 41 ++++- eth2/types/src/beacon_block_header.rs | 13 ++ 6 files changed, 239 insertions(+), 22 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 33198f0a39..eb8df6f2a8 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -46,6 +46,26 @@ pub enum BlockProcessingOutcome { InvalidBlock(InvalidBlock), } +impl BlockProcessingOutcome { + /// Returns `true` if the block was objectively invalid and we should disregard the peer who + /// sent it. + pub fn is_invalid(&self) -> bool { + match self { + BlockProcessingOutcome::ValidBlock(_) => false, + BlockProcessingOutcome::InvalidBlock(r) => match r { + InvalidBlock::FutureSlot => true, + InvalidBlock::StateRootMismatch => true, + InvalidBlock::ParentUnknown => false, + InvalidBlock::SlotProcessingError(_) => false, + InvalidBlock::PerBlockProcessingError(e) => match e { + BlockProcessingError::Invalid(_) => true, + BlockProcessingError::BeaconStateError(_) => false, + }, + }, + } + } +} + pub struct BeaconChain { pub block_store: Arc>, pub state_store: Arc>, @@ -685,10 +705,10 @@ where // TODO: check the block proposer signature BEFORE doing a state transition. This will // significantly lower exposure surface to DoS attacks. - // Transition the parent state to the present slot. + // Transition the parent state to the block slot. let mut state = parent_state; let previous_block_header = parent_block.block_header(); - for _ in state.slot.as_u64()..present_slot.as_u64() { + for _ in state.slot.as_u64()..block.slot.as_u64() { if let Err(e) = per_slot_processing(&mut state, &previous_block_header, &self.spec) { return Ok(BlockProcessingOutcome::InvalidBlock( InvalidBlock::SlotProcessingError(e), diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index e2829cfa61..bb4e8e71e6 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -10,7 +10,7 @@ use beacon_chain::{ use eth2_libp2p::HelloMessage; use types::{BeaconBlock, BeaconStateError, Epoch, Hash256, Slot}; -pub use beacon_chain::BeaconChainError; +pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { @@ -34,6 +34,9 @@ pub trait BeaconChain: Send + Sync { fn hello_message(&self) -> HelloMessage; + fn process_block(&self, block: BeaconBlock) + -> Result; + fn get_block_roots( &self, start_slot: Slot, @@ -98,6 +101,13 @@ where } } + fn process_block( + &self, + block: BeaconBlock, + ) -> Result { + self.process_block(block) + } + fn get_block_roots( &self, start_slot: Slot, diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 1a790eee1f..2a84616e56 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -161,6 +161,17 @@ impl MessageHandler { &mut self.network_context, ) } + RPCResponse::BeaconBlockBodies(response) => { + debug!( + self.log, + "BeaconBlockBodies response received"; "peer" => format!("{:?}", peer_id) + ); + self.sync.on_beacon_block_bodies_response( + peer_id, + response, + &mut self.network_context, + ) + } // TODO: Handle all responses _ => panic!("Unknown response: {:?}", response), } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index ee0646dbb0..b190f787fe 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -3,12 +3,12 @@ use crate::message_handler::NetworkContext; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse}; use eth2_libp2p::PeerId; -use slog::{debug, error, o, warn}; +use slog::{debug, error, info, o, warn}; use ssz::TreeHash; use std::collections::HashMap; use std::sync::Arc; -use std::time::Instant; -use types::{BeaconBlockHeader, Epoch, Hash256, Slot}; +use std::time::{Duration, Instant}; +use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, 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; @@ -102,7 +102,11 @@ pub struct SimpleSync { impl SimpleSync { pub fn new(beacon_chain: Arc, log: &slog::Logger) -> Self { let sync_logger = log.new(o!("Service"=> "Sync")); - let import_queue = ImportQueue::new(beacon_chain.clone(), log.clone()); + + let queue_item_stale_time = Duration::from_secs(600); + + let import_queue = + ImportQueue::new(beacon_chain.clone(), queue_item_stale_time, log.clone()); SimpleSync { chain: beacon_chain.clone(), known_peers: HashMap::new(), @@ -229,13 +233,72 @@ impl SimpleSync { return; } - let block_roots = self.import_queue.enqueue_headers(response.headers); + let block_roots = self + .import_queue + .enqueue_headers(response.headers, peer_id.clone()); if !block_roots.is_empty() { self.request_block_bodies(peer_id, BeaconBlockBodiesRequest { block_roots }, network); } } + pub fn on_beacon_block_bodies_response( + &mut self, + peer_id: PeerId, + response: BeaconBlockBodiesResponse, + network: &mut NetworkContext, + ) { + self.import_queue + .enqueue_bodies(response.block_bodies, peer_id.clone()); + self.process_import_queue(network); + } + + pub fn process_import_queue(&mut self, network: &mut NetworkContext) { + let mut blocks: Vec<(Hash256, BeaconBlock, PeerId)> = self + .import_queue + .partials + .iter() + .filter_map(|(key, partial)| { + if let Some(_) = partial.body { + let (block, _root) = partial.clone().complete().expect("Body must be Some"); + Some((*key, block, partial.sender.clone())) + } else { + None + } + }) + .collect(); + + // Sort the blocks to be in ascending slot order. + blocks.sort_unstable_by(|a, b| a.1.slot.partial_cmp(&b.1.slot).unwrap()); + + let mut imported_keys = vec![]; + + for (key, block, sender) in blocks { + match self.chain.process_block(block) { + Ok(outcome) => { + if outcome.is_invalid() { + warn!(self.log, "Invalid block: {:?}", outcome); + network.disconnect(sender); + } else { + imported_keys.push(key) + } + } + Err(e) => { + error!(self.log, "Error during block processing"; "error" => format!("{:?}", e)) + } + } + } + + println!("imported_keys.len: {:?}", imported_keys.len()); + + if !imported_keys.is_empty() { + info!(self.log, "Imported {} blocks", imported_keys.len()); + for key in imported_keys { + self.import_queue.partials.remove(&key); + } + } + } + fn request_block_roots( &mut self, peer_id: PeerId, @@ -298,19 +361,41 @@ pub struct ImportQueue { pub chain: Arc, /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. pub partials: HashMap, + /// Time before a queue entry is consider state. + pub stale_time: Duration, /// Logging log: slog::Logger, } impl ImportQueue { - pub fn new(chain: Arc, log: slog::Logger) -> Self { + pub fn new(chain: Arc, stale_time: Duration, log: slog::Logger) -> Self { Self { chain, partials: HashMap::new(), + stale_time, log, } } + pub fn remove_stale(&mut self) { + let keys: Vec = self + .partials + .iter() + .filter_map(|(key, partial)| { + if partial.inserted + self.stale_time >= Instant::now() { + Some(*key) + } else { + None + } + }) + .collect(); + + keys.iter().for_each(|key| { + self.partials.remove(&key); + }); + } + + /// Returns `true` if `self.chain` has not yet processed this block. fn is_new_block(&self, block_root: &Hash256) -> bool { self.chain .is_new_block_root(&block_root) @@ -322,9 +407,6 @@ impl ImportQueue { /// Returns the index of the first new root in the list of block roots. pub fn first_new_root(&mut self, roots: &[BlockRootSlot]) -> Option { - for root in roots { - println!("root {}", root.block_root); - } roots .iter() .position(|brs| self.is_new_block(&brs.block_root)) @@ -339,14 +421,18 @@ impl ImportQueue { /// If a `header` is already in the queue, but not yet processed by the chain the block root is /// included in the output and the `inserted` time for the partial record is set to /// `Instant::now()`. Updating the `inserted` time stops the partial from becoming stale. - pub fn enqueue_headers(&mut self, headers: Vec) -> Vec { + pub fn enqueue_headers( + &mut self, + headers: Vec, + sender: PeerId, + ) -> Vec { let mut required_bodies: Vec = vec![]; for header in headers { let block_root = Hash256::from_slice(&header.hash_tree_root()[..]); if self.is_new_block(&block_root) { - self.insert_partial(block_root, header); + self.insert_header(block_root, header, sender.clone()); required_bodies.push(block_root) } } @@ -354,20 +440,60 @@ impl ImportQueue { required_bodies } - fn insert_partial(&mut self, block_root: Hash256, header: BeaconBlockHeader) { - self.partials.insert( - header.block_body_root, - PartialBeaconBlock { + /// If there is a matching `header` for this `body`, adds it to the queue. + /// + /// If there is no `header` for the `body`, the body is simply discarded. + pub fn enqueue_bodies(&mut self, bodies: Vec, sender: PeerId) { + for body in bodies { + self.insert_body(body, sender.clone()); + } + } + + /// Inserts a header to the queue. + /// + /// If the header already exists, the `inserted` time is set to `now` and not other + /// modifications are made. + fn insert_header(&mut self, block_root: Hash256, header: BeaconBlockHeader, sender: PeerId) { + self.partials + .entry(header.block_body_root) + .and_modify(|p| p.inserted = Instant::now()) + .or_insert(PartialBeaconBlock { block_root, header, + body: None, inserted: Instant::now(), - }, - ); + sender, + }); + } + + /// Updates an existing partial with the `body`. + /// + /// If there is no header for the `body`, the body is simply discarded. + fn insert_body(&mut self, body: BeaconBlockBody, sender: PeerId) { + let body_root = Hash256::from_slice(&body.hash_tree_root()[..]); + + self.partials.entry(body_root).and_modify(|p| { + if body_root == p.header.block_body_root { + p.body = Some(body); + p.inserted = Instant::now(); + p.sender = sender; + } + }); } } +#[derive(Clone, Debug)] pub struct PartialBeaconBlock { pub block_root: Hash256, pub header: BeaconBlockHeader, + pub body: Option, pub inserted: Instant, + pub sender: PeerId, +} + +impl PartialBeaconBlock { + /// Given a `body`, consumes `self` and returns a complete `BeaconBlock` along with its root. + pub fn complete(self) -> Option<(BeaconBlock, Hash256)> { + Some((self.header.into_block(self.body?), self.block_root)) + } } diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index 076a3f529a..57587717bd 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -77,7 +77,16 @@ impl SyncNode { match request { RPCRequest::BeaconBlockHeaders(request) => request, - _ => panic!("Did not get block root request"), + _ => panic!("Did not get block headers request"), + } + } + + pub fn get_block_bodies_request(&self) -> BeaconBlockBodiesRequest { + let request = self.recv_rpc_request().expect("No block bodies request"); + + match request { + RPCRequest::BeaconBlockBodies(request) => request, + _ => panic!("Did not get block bodies request"), } } @@ -223,6 +232,29 @@ impl SyncMaster { self.send_rpc_response(node, response) } + pub fn respond_to_block_bodies_request( + &mut self, + node: &SyncNode, + request: BeaconBlockBodiesRequest, + ) { + let block_bodies: Vec = request + .block_roots + .iter() + .map(|root| { + let block = self + .harness + .beacon_chain + .get_block(root) + .expect("Failed to load block") + .expect("Block did not exist"); + block.body + }) + .collect(); + + let response = RPCResponse::BeaconBlockBodies(BeaconBlockBodiesResponse { block_bodies }); + self.send_rpc_response(node, response) + } + fn send_rpc_response(&mut self, node: &SyncNode, rpc_response: RPCResponse) { node.send(self.rpc_response(node, rpc_response)); } @@ -311,6 +343,11 @@ fn first_test() { master.respond_to_block_headers_request(&nodes[0], headers_request); - std::thread::sleep(Duration::from_millis(500)); + let bodies_request = nodes[0].get_block_bodies_request(); + assert_eq!(bodies_request.block_roots.len(), 2); + + master.respond_to_block_bodies_request(&nodes[0], bodies_request); + + std::thread::sleep(Duration::from_millis(10000)); runtime.shutdown_now(); } diff --git a/eth2/types/src/beacon_block_header.rs b/eth2/types/src/beacon_block_header.rs index 3d8b08cc84..f4bee27e1e 100644 --- a/eth2/types/src/beacon_block_header.rs +++ b/eth2/types/src/beacon_block_header.rs @@ -37,6 +37,19 @@ impl BeaconBlockHeader { pub fn canonical_root(&self) -> Hash256 { Hash256::from_slice(&self.hash_tree_root()[..]) } + + /// Given a `body`, consumes `self` and returns a complete `BeaconBlock`. + /// + /// Spec v0.5.0 + pub fn into_block(self, body: BeaconBlockBody) -> BeaconBlock { + BeaconBlock { + slot: self.slot, + previous_block_root: self.previous_block_root, + state_root: self.state_root, + body, + signature: self.signature, + } + } } #[cfg(test)] From 18493a4df4e5ab6569214c8af376269ce3db0d6b Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 24 Mar 2019 09:24:50 +1100 Subject: [PATCH 034/191] 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 3eae7b3fc5258fa611ecab23aa3dc8d72f4e8fb9 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 12:49:59 +1100 Subject: [PATCH 035/191] Add skip slot support to BeaconChain fns --- beacon_node/beacon_chain/src/beacon_chain.rs | 87 ++++++++++++++------ beacon_node/network/src/beacon_chain.rs | 41 +++++++-- 2 files changed, 97 insertions(+), 31 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index eb8df6f2a8..d5fd113a80 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -142,7 +142,38 @@ where }) } - /// Returns beacon block roots for `count` slots, starting from `start_slot`. + /// Returns the beacon block body for each beacon block root in `roots`. + /// + /// Fails if any root in `roots` does not have a corresponding block. + pub fn get_block_bodies(&self, roots: &[Hash256]) -> Result, Error> { + let bodies: Result, _> = roots + .iter() + .map(|root| match self.get_block(root)? { + Some(block) => Ok(block.body), + None => Err(Error::DBInconsistent("Missing block".into())), + }) + .collect(); + + Ok(bodies?) + } + + /// Returns the beacon block header for each beacon block root in `roots`. + /// + /// Fails if any root in `roots` does not have a corresponding block. + pub fn get_block_headers(&self, roots: &[Hash256]) -> Result, Error> { + let headers: Result, _> = roots + .iter() + .map(|root| match self.get_block(root)? { + Some(block) => Ok(block.block_header()), + None => Err(Error::DBInconsistent("Missing block".into())), + }) + .collect(); + + Ok(headers?) + } + + /// Returns `count `beacon block roots, starting from `start_slot` with an + /// interval of `skip` slots between each root. /// /// ## Errors: /// @@ -152,40 +183,45 @@ where /// - Other: BeaconState` is inconsistent. pub fn get_block_roots( &self, - start_slot: Slot, - count: Slot, - ) -> Result, BeaconStateError> { + earliest_slot: Slot, + count: usize, + skip: usize, + ) -> Result, Error> { let spec = &self.spec; + let step_by = Slot::from(skip + 1); let mut roots: Vec = vec![]; + + // The state for reading block roots. Will be updated with an older state if slots go too + // far back in history. let mut state = self.state.read().clone(); - let mut slot = start_slot + count - 1; + + // The final slot in this series, will be reduced by `skip` each loop iteration. + let mut slot = earliest_slot + Slot::from(count * (skip + 1)) - 1; + + // If the highest slot requested is that of the current state insert the root of the + // head block, unless the head block's slot is not matching. + if slot == state.slot && self.head().beacon_block.slot == slot { + roots.push(self.head().beacon_block_root); + + slot -= step_by; + } else if slot >= state.slot { + return Err(BeaconStateError::SlotOutOfBounds.into()); + } loop { - // If the highest slot requested is that of the current state insert the root of the - // head block, unless the head block's slot is not matching. - if slot == state.slot && self.head().beacon_block.slot == slot { - roots.push(self.head().beacon_block_root); - - slot -= 1; - continue; - } else if slot >= state.slot { - return Err(BeaconStateError::SlotOutOfBounds); - } - // If the slot is within the range of the current state's block roots, append the root // to the output vec. // - // If we get `SlotOutOfBounds` error, load the oldest known state to the present state - // from the DB. + // If we get `SlotOutOfBounds` error, load the oldest available historic + // state from the DB. match state.get_block_root(slot, spec) { Ok(root) => { - roots.push(*root); - - if slot == start_slot { + if slot < earliest_slot { break; } else { - slot -= 1; + roots.push(*root); + slot -= step_by; } } Err(BeaconStateError::SlotOutOfBounds) => { @@ -201,18 +237,19 @@ where _ => break, } } - Err(e) => return Err(e), + Err(e) => return Err(e.into()), }; } - if (slot == start_slot) && (roots.len() == count.as_usize()) { + // Return the results if they pass a sanity check. + if (slot <= earliest_slot) && (roots.len() == count) { // Reverse the ordering of the roots. We extracted them in reverse order to make it // simpler to lookup historic states. // // This is a potential optimisation target. Ok(roots.iter().rev().cloned().collect()) } else { - Err(BeaconStateError::SlotOutOfBounds) + Err(BeaconStateError::SlotOutOfBounds.into()) } } diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index bb4e8e71e6..cc54e8ae09 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -8,7 +8,7 @@ use beacon_chain::{ CheckPoint, }; use eth2_libp2p::HelloMessage; -use types::{BeaconBlock, BeaconStateError, Epoch, Hash256, Slot}; +use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; @@ -40,8 +40,19 @@ pub trait BeaconChain: Send + Sync { fn get_block_roots( &self, start_slot: Slot, - count: Slot, - ) -> Result, BeaconStateError>; + count: usize, + skip: usize, + ) -> Result, BeaconChainError>; + + fn get_block_headers( + &self, + start_slot: Slot, + count: usize, + skip: usize, + ) -> Result, BeaconChainError>; + + fn get_block_bodies(&self, roots: &[Hash256]) + -> Result, BeaconChainError>; fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result; } @@ -111,9 +122,27 @@ where fn get_block_roots( &self, start_slot: Slot, - count: Slot, - ) -> Result, BeaconStateError> { - self.get_block_roots(start_slot, count) + count: usize, + skip: usize, + ) -> Result, BeaconChainError> { + self.get_block_roots(start_slot, count, skip) + } + + fn get_block_headers( + &self, + start_slot: Slot, + count: usize, + skip: usize, + ) -> Result, BeaconChainError> { + let roots = self.get_block_roots(start_slot, count, skip)?; + self.get_block_headers(&roots) + } + + fn get_block_bodies( + &self, + roots: &[Hash256], + ) -> Result, BeaconChainError> { + self.get_block_bodies(roots) } fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result { From 6b39c693af28675c488202ad8def2980fb3c772d Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 12:50:23 +1100 Subject: [PATCH 036/191] Extend syncing --- beacon_node/network/src/message_handler.rs | 49 ++--- beacon_node/network/src/sync/simple_sync.rs | 158 ++++++++++++++-- beacon_node/network/tests/tests.rs | 196 +++++++++++++++++++- 3 files changed, 364 insertions(+), 39 deletions(-) diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 2a84616e56..58ba0171db 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -113,10 +113,24 @@ impl MessageHandler { match request { RPCRequest::Hello(hello_message) => { self.sync - .on_hello(peer_id, hello_message, &mut self.network_context) + .on_hello_request(peer_id, hello_message, &mut self.network_context) } + RPCRequest::BeaconBlockRoots(request) => { + self.sync + .on_beacon_block_roots_request(peer_id, request, &mut self.network_context) + } + RPCRequest::BeaconBlockHeaders(request) => self.sync.on_beacon_block_headers_request( + peer_id, + request, + &mut self.network_context, + ), + RPCRequest::BeaconBlockBodies(request) => self.sync.on_beacon_block_bodies_request( + peer_id, + request, + &mut self.network_context, + ), // TODO: Handle all requests - _ => {} + _ => panic!("Unknown request: {:?}", request), } } @@ -133,48 +147,41 @@ impl MessageHandler { debug!(self.log, "Unrecognized response from peer: {:?}", peer_id); return; } - match response { + let response_str = match response { RPCResponse::Hello(hello_message) => { - debug!(self.log, "Hello response received from peer: {:?}", peer_id); self.sync - .on_hello(peer_id, hello_message, &mut self.network_context); + .on_hello_response(peer_id, hello_message, &mut self.network_context); + "Hello" } RPCResponse::BeaconBlockRoots(response) => { - debug!( - self.log, - "BeaconBlockRoots response received"; "peer" => format!("{:?}", peer_id) - ); self.sync.on_beacon_block_roots_response( peer_id, response, &mut self.network_context, - ) + ); + "BeaconBlockRoots" } RPCResponse::BeaconBlockHeaders(response) => { - debug!( - self.log, - "BeaconBlockHeaders response received"; "peer" => format!("{:?}", peer_id) - ); self.sync.on_beacon_block_headers_response( peer_id, response, &mut self.network_context, - ) + ); + "BeaconBlockHeaders" } RPCResponse::BeaconBlockBodies(response) => { - debug!( - self.log, - "BeaconBlockBodies response received"; "peer" => format!("{:?}", peer_id) - ); self.sync.on_beacon_block_bodies_response( peer_id, response, &mut self.network_context, - ) + ); + "BeaconBlockBodies" } // TODO: Handle all responses _ => panic!("Unknown response: {:?}", response), - } + }; + + debug!(self.log, "RPCResponse"; "type" => response_str); } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index b190f787fe..4726419d50 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -126,14 +126,31 @@ impl SimpleSync { hello: HelloMessage, network: &mut NetworkContext, ) { + // Say hello back. network.send_rpc_response( peer_id.clone(), RPCResponse::Hello(self.chain.hello_message()), ); - self.on_hello(peer_id, hello, network); + + self.process_hello(peer_id, hello, network); } - pub fn on_hello(&mut self, peer_id: PeerId, hello: HelloMessage, network: &mut NetworkContext) { + pub fn on_hello_response( + &mut self, + peer_id: PeerId, + hello: HelloMessage, + network: &mut NetworkContext, + ) { + // Process the hello message, without sending back another hello. + self.process_hello(peer_id, hello, network); + } + + fn process_hello( + &mut self, + peer_id: PeerId, + hello: HelloMessage, + network: &mut NetworkContext, + ) { let spec = self.chain.get_spec(); let remote = PeerSyncInfo::from(hello); @@ -142,7 +159,7 @@ impl SimpleSync { // network id must match if remote_status != PeerStatus::OnDifferentChain { - debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); + info!(self.log, "HandshakeSuccess"; "peer" => format!("{:?}", peer_id)); self.known_peers.insert(peer_id.clone(), remote); } @@ -183,6 +200,44 @@ impl SimpleSync { } } + pub fn on_beacon_block_roots_request( + &mut self, + peer_id: PeerId, + request: BeaconBlockRootsRequest, + network: &mut NetworkContext, + ) { + let roots = match self + .chain + .get_block_roots(request.start_slot, request.count as usize, 0) + { + Ok(roots) => roots, + Err(e) => { + // TODO: return RPC error. + warn!( + self.log, + "RPCRequest"; "peer" => format!("{:?}", peer_id), + "request" => "BeaconBlockRoots", + "error" => format!("{:?}", e) + ); + return; + } + }; + + let roots = roots + .iter() + .enumerate() + .map(|(i, &block_root)| BlockRootSlot { + slot: request.start_slot + Slot::from(i), + block_root, + }) + .collect(); + + network.send_rpc_response( + peer_id, + RPCResponse::BeaconBlockRoots(BeaconBlockRootsResponse { roots }), + ) + } + pub fn on_beacon_block_roots_response( &mut self, peer_id: PeerId, @@ -219,6 +274,36 @@ impl SimpleSync { } } + pub fn on_beacon_block_headers_request( + &mut self, + peer_id: PeerId, + request: BeaconBlockHeadersRequest, + network: &mut NetworkContext, + ) { + let headers = match self.chain.get_block_headers( + request.start_slot, + request.max_headers as usize, + request.skip_slots as usize, + ) { + Ok(headers) => headers, + Err(e) => { + // TODO: return RPC error. + warn!( + self.log, + "RPCRequest"; "peer" => format!("{:?}", peer_id), + "request" => "BeaconBlockHeaders", + "error" => format!("{:?}", e) + ); + return; + } + }; + + network.send_rpc_response( + peer_id, + RPCResponse::BeaconBlockHeaders(BeaconBlockHeadersResponse { headers }), + ) + } + pub fn on_beacon_block_headers_response( &mut self, peer_id: PeerId, @@ -237,9 +322,33 @@ impl SimpleSync { .import_queue .enqueue_headers(response.headers, peer_id.clone()); - if !block_roots.is_empty() { - self.request_block_bodies(peer_id, BeaconBlockBodiesRequest { block_roots }, network); - } + self.request_block_bodies(peer_id, BeaconBlockBodiesRequest { block_roots }, network); + } + + pub fn on_beacon_block_bodies_request( + &mut self, + peer_id: PeerId, + request: BeaconBlockBodiesRequest, + network: &mut NetworkContext, + ) { + let block_bodies = match self.chain.get_block_bodies(&request.block_roots) { + Ok(bodies) => bodies, + Err(e) => { + // TODO: return RPC error. + warn!( + self.log, + "RPCRequest"; "peer" => format!("{:?}", peer_id), + "request" => "BeaconBlockBodies", + "error" => format!("{:?}", e) + ); + return; + } + }; + + network.send_rpc_response( + peer_id, + RPCResponse::BeaconBlockBodies(BeaconBlockBodiesResponse { block_bodies }), + ) } pub fn on_beacon_block_bodies_response( @@ -250,6 +359,11 @@ impl SimpleSync { ) { self.import_queue .enqueue_bodies(response.block_bodies, peer_id.clone()); + + // Clear out old entries + self.import_queue.remove_stale(); + + // Import blocks, if possible. self.process_import_queue(network); } @@ -268,10 +382,14 @@ impl SimpleSync { }) .collect(); + if !blocks.is_empty() { + info!(self.log, "Processing blocks"; "count" => blocks.len()); + } + // Sort the blocks to be in ascending slot order. blocks.sort_unstable_by(|a, b| a.1.slot.partial_cmp(&b.1.slot).unwrap()); - let mut imported_keys = vec![]; + let mut keys_to_delete = vec![]; for (key, block, sender) in blocks { match self.chain.process_block(block) { @@ -279,8 +397,10 @@ impl SimpleSync { if outcome.is_invalid() { warn!(self.log, "Invalid block: {:?}", outcome); network.disconnect(sender); + keys_to_delete.push(key) } else { - imported_keys.push(key) + // TODO: don't delete if was not invalid but not successfully processed. + keys_to_delete.push(key) } } Err(e) => { @@ -289,11 +409,9 @@ impl SimpleSync { } } - println!("imported_keys.len: {:?}", imported_keys.len()); - - if !imported_keys.is_empty() { - info!(self.log, "Imported {} blocks", imported_keys.len()); - for key in imported_keys { + if !keys_to_delete.is_empty() { + info!(self.log, "Processed {} blocks", keys_to_delete.len()); + for key in keys_to_delete { self.import_queue.partials.remove(&key); } } @@ -313,7 +431,10 @@ impl SimpleSync { debug!( self.log, - "Requesting {} block roots from {:?}.", request.count, &peer_id + "RPCRequest"; + "type" => "BeaconBlockRoots", + "count" => request.count, + "peer" => format!("{:?}", peer_id) ); // TODO: handle count > max count. @@ -328,7 +449,10 @@ impl SimpleSync { ) { debug!( self.log, - "Requesting {} headers from {:?}.", request.max_headers, &peer_id + "RPCRequest"; + "type" => "BeaconBlockHeaders", + "max_headers" => request.max_headers, + "peer" => format!("{:?}", peer_id) ); network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockHeaders(request)); @@ -377,6 +501,10 @@ impl ImportQueue { } } + /// Flushes all stale entries from the queue. + /// + /// An entry is stale if it has as a `inserted` time that is more than `self.stale_time` in the + /// past. pub fn remove_stale(&mut self) { let keys: Vec = self .partials diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index 57587717bd..b951d7d2a2 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -17,6 +17,7 @@ pub struct SyncNode { pub id: usize, sender: Sender, receiver: Receiver, + peer_id: PeerId, harness: BeaconChainHarness, } @@ -43,6 +44,7 @@ impl SyncNode { id, sender: message_handler_sender, receiver: network_receiver, + peer_id: PeerId::random(), harness, } } @@ -63,6 +65,138 @@ impl SyncNode { self.harness.beacon_chain.hello_message() } + pub fn connect_to(&mut self, node: &SyncNode) { + let message = HandlerMessage::PeerDialed(self.peer_id.clone()); + node.send(message); + } + + /// Reads the receive queue from one node and passes the message to the other. Also returns a + /// copy of the message. + /// + /// self -----> node + /// | + /// us + /// + /// Named after the unix `tee` command. + fn tee(&mut self, node: &SyncNode) -> NetworkMessage { + let network_message = self.recv().expect("Timeout on tee"); + + let handler_message = match network_message.clone() { + NetworkMessage::Send(peer_id, OutgoingMessage::RPC(event)) => { + HandlerMessage::RPC(peer_id, event) + } + _ => panic!("tee cannot parse {:?}", network_message), + }; + + node.send(handler_message); + + network_message + } + + fn tee_hello_request(&mut self, node: &SyncNode) -> HelloMessage { + let request = self.tee_rpc_request(node); + + match request { + RPCRequest::Hello(message) => message, + _ => panic!("tee_hello_request got: {:?}", request), + } + } + + fn tee_hello_response(&mut self, node: &SyncNode) -> HelloMessage { + let response = self.tee_rpc_response(node); + + match response { + RPCResponse::Hello(message) => message, + _ => panic!("tee_hello_response got: {:?}", response), + } + } + + fn tee_block_root_request(&mut self, node: &SyncNode) -> BeaconBlockRootsRequest { + let msg = self.tee_rpc_request(node); + + match msg { + RPCRequest::BeaconBlockRoots(data) => data, + _ => panic!("tee_block_root_request got: {:?}", msg), + } + } + + fn tee_block_root_response(&mut self, node: &SyncNode) -> BeaconBlockRootsResponse { + let msg = self.tee_rpc_response(node); + + match msg { + RPCResponse::BeaconBlockRoots(data) => data, + _ => panic!("tee_block_root_response got: {:?}", msg), + } + } + + fn tee_block_header_request(&mut self, node: &SyncNode) -> BeaconBlockHeadersRequest { + let msg = self.tee_rpc_request(node); + + match msg { + RPCRequest::BeaconBlockHeaders(data) => data, + _ => panic!("tee_block_header_request got: {:?}", msg), + } + } + + fn tee_block_header_response(&mut self, node: &SyncNode) -> BeaconBlockHeadersResponse { + let msg = self.tee_rpc_response(node); + + match msg { + RPCResponse::BeaconBlockHeaders(data) => data, + _ => panic!("tee_block_header_response got: {:?}", msg), + } + } + + fn tee_block_body_request(&mut self, node: &SyncNode) -> BeaconBlockBodiesRequest { + let msg = self.tee_rpc_request(node); + + match msg { + RPCRequest::BeaconBlockBodies(data) => data, + _ => panic!("tee_block_body_request got: {:?}", msg), + } + } + + fn tee_block_body_response(&mut self, node: &SyncNode) -> BeaconBlockBodiesResponse { + let msg = self.tee_rpc_response(node); + + match msg { + RPCResponse::BeaconBlockBodies(data) => data, + _ => panic!("tee_block_body_response got: {:?}", msg), + } + } + + fn tee_rpc_request(&mut self, node: &SyncNode) -> RPCRequest { + let network_message = self.tee(node); + + match network_message { + NetworkMessage::Send( + _peer_id, + OutgoingMessage::RPC(RPCEvent::Request { + id: _, + method_id: _, + body, + }), + ) => body, + _ => panic!("tee_rpc_request failed! got {:?}", network_message), + } + } + + fn tee_rpc_response(&mut self, node: &SyncNode) -> RPCResponse { + let network_message = self.tee(node); + + match network_message { + NetworkMessage::Send( + _peer_id, + OutgoingMessage::RPC(RPCEvent::Response { + id: _, + method_id: _, + result, + }), + ) => result, + _ => panic!("tee_rpc_response failed! got {:?}", network_message), + } + } + pub fn get_block_root_request(&self) -> BeaconBlockRootsRequest { let request = self.recv_rpc_request().expect("No block root request"); @@ -181,7 +315,7 @@ impl SyncMaster { let roots = self .harness .beacon_chain - .get_block_roots(request.start_slot, Slot::from(request.count)) + .get_block_roots(request.start_slot, request.count as usize, 0) .expect("Beacon chain did not give block roots") .iter() .enumerate() @@ -203,7 +337,11 @@ impl SyncMaster { let roots = self .harness .beacon_chain - .get_block_roots(request.start_slot, Slot::from(request.max_headers)) + .get_block_roots( + request.start_slot, + request.max_headers as usize, + request.skip_slots as usize, + ) .expect("Beacon chain did not give blocks"); if roots.is_empty() { @@ -312,7 +450,7 @@ pub fn build_blocks(blocks: usize, master: &mut SyncMaster, nodes: &mut Vec Date: Sun, 24 Mar 2019 13:06:17 +1100 Subject: [PATCH 037/191] Tidy logging, fix bug with stale time in queue --- beacon_node/network/src/message_handler.rs | 2 +- beacon_node/network/src/sync/simple_sync.rs | 34 +++++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 58ba0171db..24fe04950b 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -181,7 +181,7 @@ impl MessageHandler { _ => panic!("Unknown response: {:?}", response), }; - debug!(self.log, "RPCResponse"; "type" => response_str); + debug!(self.log, "RPCResponse({})", response_str); } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 4726419d50..ff6092be1a 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -13,6 +13,9 @@ use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slo /// The number of slots that we can import blocks ahead of us, before going into full Sync mode. const SLOT_IMPORT_TOLERANCE: u64 = 100; +/// The amount of seconds a block (or partial block) may exist in the import queue. +const QUEUE_STALE_SECS: u64 = 60; + /// Keeps track of syncing information for known connected peers. #[derive(Clone, Copy, Debug)] pub struct PeerSyncInfo { @@ -103,7 +106,7 @@ impl SimpleSync { pub fn new(beacon_chain: Arc, log: &slog::Logger) -> Self { let sync_logger = log.new(o!("Service"=> "Sync")); - let queue_item_stale_time = Duration::from_secs(600); + let queue_item_stale_time = Duration::from_secs(QUEUE_STALE_SECS); let import_queue = ImportQueue::new(beacon_chain.clone(), queue_item_stale_time, log.clone()); @@ -165,7 +168,11 @@ impl SimpleSync { match remote_status { PeerStatus::OnDifferentChain => { - debug!(self.log, "Peer is on different chain. Peer: {:?}", peer_id); + info!( + self.log, "Failure"; + "peer" => format!("{:?}", peer_id), + "reason" => "network_id" + ); network.disconnect(peer_id); } @@ -431,8 +438,7 @@ impl SimpleSync { debug!( self.log, - "RPCRequest"; - "type" => "BeaconBlockRoots", + "RPCRequest(BeaconBlockRoots)"; "count" => request.count, "peer" => format!("{:?}", peer_id) ); @@ -449,8 +455,7 @@ impl SimpleSync { ) { debug!( self.log, - "RPCRequest"; - "type" => "BeaconBlockHeaders", + "RPCRequest(BeaconBlockHeaders)"; "max_headers" => request.max_headers, "peer" => format!("{:?}", peer_id) ); @@ -466,9 +471,9 @@ impl SimpleSync { ) { debug!( self.log, - "Requesting {} bodies from {:?}.", - request.block_roots.len(), - &peer_id + "RPCRequest(BeaconBlockBodies)"; + "count" => request.block_roots.len(), + "peer" => format!("{:?}", peer_id) ); network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockBodies(request)); @@ -510,7 +515,7 @@ impl ImportQueue { .partials .iter() .filter_map(|(key, partial)| { - if partial.inserted + self.stale_time >= Instant::now() { + if partial.inserted + self.stale_time <= Instant::now() { Some(*key) } else { None @@ -518,6 +523,15 @@ impl ImportQueue { }) .collect(); + if !keys.is_empty() { + debug!( + self.log, + "ImportQueue removing stale entries"; + "stale_count" => keys.len(), + "stale_time_seconds" => self.stale_time.as_secs() + ); + } + keys.iter().for_each(|key| { self.partials.remove(&key); }); From 15f853416bab576ef9c21e2813e52b8b6c7031e9 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 13:59:27 +1100 Subject: [PATCH 038/191] Add more detail to sync logs --- beacon_node/network/src/message_handler.rs | 13 +-- beacon_node/network/src/sync/simple_sync.rs | 114 ++++++++++++++------ 2 files changed, 85 insertions(+), 42 deletions(-) diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 24fe04950b..77a2ab8db9 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -8,8 +8,7 @@ use eth2_libp2p::{ PeerId, RPCEvent, }; use futures::future; -use slog::debug; -use slog::warn; +use slog::{debug, warn}; use std::collections::HashMap; use std::sync::Arc; use std::time::Instant; @@ -144,14 +143,13 @@ impl MessageHandler { .remove(&(peer_id.clone(), id)) .is_none() { - debug!(self.log, "Unrecognized response from peer: {:?}", peer_id); + debug!(self.log, "Unrecognised response from peer: {:?}", peer_id); return; } - let response_str = match response { + match response { RPCResponse::Hello(hello_message) => { self.sync .on_hello_response(peer_id, hello_message, &mut self.network_context); - "Hello" } RPCResponse::BeaconBlockRoots(response) => { self.sync.on_beacon_block_roots_response( @@ -159,7 +157,6 @@ impl MessageHandler { response, &mut self.network_context, ); - "BeaconBlockRoots" } RPCResponse::BeaconBlockHeaders(response) => { self.sync.on_beacon_block_headers_response( @@ -167,7 +164,6 @@ impl MessageHandler { response, &mut self.network_context, ); - "BeaconBlockHeaders" } RPCResponse::BeaconBlockBodies(response) => { self.sync.on_beacon_block_bodies_response( @@ -175,13 +171,10 @@ impl MessageHandler { response, &mut self.network_context, ); - "BeaconBlockBodies" } // TODO: Handle all responses _ => panic!("Unknown response: {:?}", response), }; - - debug!(self.log, "RPCResponse({})", response_str); } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index ff6092be1a..7609f5750d 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -120,6 +120,8 @@ impl SimpleSync { } pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) { + info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id)); + network.send_rpc_request(peer_id, RPCRequest::Hello(self.chain.hello_message())); } @@ -129,6 +131,8 @@ impl SimpleSync { hello: HelloMessage, network: &mut NetworkContext, ) { + debug!(self.log, "HelloRequest"; "peer" => format!("{:?}", peer_id)); + // Say hello back. network.send_rpc_response( peer_id.clone(), @@ -144,6 +148,8 @@ impl SimpleSync { hello: HelloMessage, network: &mut NetworkContext, ) { + debug!(self.log, "HelloResponse"; "peer" => format!("{:?}", peer_id)); + // Process the hello message, without sending back another hello. self.process_hello(peer_id, hello, network); } @@ -210,12 +216,19 @@ impl SimpleSync { pub fn on_beacon_block_roots_request( &mut self, peer_id: PeerId, - request: BeaconBlockRootsRequest, + req: BeaconBlockRootsRequest, network: &mut NetworkContext, ) { + debug!( + self.log, + "BlockRootsRequest"; + "peer" => format!("{:?}", peer_id), + "count" => req.count, + ); + let roots = match self .chain - .get_block_roots(request.start_slot, request.count as usize, 0) + .get_block_roots(req.start_slot, req.count as usize, 0) { Ok(roots) => roots, Err(e) => { @@ -223,7 +236,7 @@ impl SimpleSync { warn!( self.log, "RPCRequest"; "peer" => format!("{:?}", peer_id), - "request" => "BeaconBlockRoots", + "req" => "BeaconBlockRoots", "error" => format!("{:?}", e) ); return; @@ -234,7 +247,7 @@ impl SimpleSync { .iter() .enumerate() .map(|(i, &block_root)| BlockRootSlot { - slot: request.start_slot + Slot::from(i), + slot: req.start_slot + Slot::from(i), block_root, }) .collect(); @@ -248,10 +261,17 @@ impl SimpleSync { pub fn on_beacon_block_roots_response( &mut self, peer_id: PeerId, - response: BeaconBlockRootsResponse, + res: BeaconBlockRootsResponse, network: &mut NetworkContext, ) { - if response.roots.is_empty() { + debug!( + self.log, + "BlockRootsResponse"; + "peer" => format!("{:?}", peer_id), + "count" => res.roots.len(), + ); + + if res.roots.is_empty() { warn!( self.log, "Peer returned empty block roots response. PeerId: {:?}", peer_id @@ -259,21 +279,21 @@ impl SimpleSync { return; } - let new_root_index = self.import_queue.first_new_root(&response.roots); + let new_root_index = self.import_queue.first_new_root(&res.roots); // If a new block root is found, request it and all the headers following it. // // We make an assumption here that if we don't know a block then we don't know of all // it's parents. This might not be the case if syncing becomes more sophisticated. if let Some(i) = new_root_index { - let new = &response.roots[i]; + let new = &res.roots[i]; self.request_block_headers( peer_id, BeaconBlockHeadersRequest { start_root: new.block_root, start_slot: new.slot, - max_headers: (response.roots.len() - i) as u64, + max_headers: (res.roots.len() - i) as u64, skip_slots: 0, }, network, @@ -284,13 +304,20 @@ impl SimpleSync { pub fn on_beacon_block_headers_request( &mut self, peer_id: PeerId, - request: BeaconBlockHeadersRequest, + req: BeaconBlockHeadersRequest, network: &mut NetworkContext, ) { + debug!( + self.log, + "BlockHeadersRequest"; + "peer" => format!("{:?}", peer_id), + "count" => req.max_headers, + ); + let headers = match self.chain.get_block_headers( - request.start_slot, - request.max_headers as usize, - request.skip_slots as usize, + req.start_slot, + req.max_headers as usize, + req.skip_slots as usize, ) { Ok(headers) => headers, Err(e) => { @@ -298,7 +325,7 @@ impl SimpleSync { warn!( self.log, "RPCRequest"; "peer" => format!("{:?}", peer_id), - "request" => "BeaconBlockHeaders", + "req" => "BeaconBlockHeaders", "error" => format!("{:?}", e) ); return; @@ -314,10 +341,17 @@ impl SimpleSync { pub fn on_beacon_block_headers_response( &mut self, peer_id: PeerId, - response: BeaconBlockHeadersResponse, + res: BeaconBlockHeadersResponse, network: &mut NetworkContext, ) { - if response.headers.is_empty() { + debug!( + self.log, + "BlockHeadersResponse"; + "peer" => format!("{:?}", peer_id), + "count" => res.headers.len(), + ); + + if res.headers.is_empty() { warn!( self.log, "Peer returned empty block headers response. PeerId: {:?}", peer_id @@ -325,9 +359,11 @@ impl SimpleSync { return; } + // Enqueue the headers, obtaining a list of the roots of the headers which were newly added + // to the queue. let block_roots = self .import_queue - .enqueue_headers(response.headers, peer_id.clone()); + .enqueue_headers(res.headers, peer_id.clone()); self.request_block_bodies(peer_id, BeaconBlockBodiesRequest { block_roots }, network); } @@ -335,17 +371,24 @@ impl SimpleSync { pub fn on_beacon_block_bodies_request( &mut self, peer_id: PeerId, - request: BeaconBlockBodiesRequest, + req: BeaconBlockBodiesRequest, network: &mut NetworkContext, ) { - let block_bodies = match self.chain.get_block_bodies(&request.block_roots) { + debug!( + self.log, + "BlockBodiesRequest"; + "peer" => format!("{:?}", peer_id), + "count" => req.block_roots.len(), + ); + + let block_bodies = match self.chain.get_block_bodies(&req.block_roots) { Ok(bodies) => bodies, Err(e) => { // TODO: return RPC error. warn!( self.log, "RPCRequest"; "peer" => format!("{:?}", peer_id), - "request" => "BeaconBlockBodies", + "req" => "BeaconBlockBodies", "error" => format!("{:?}", e) ); return; @@ -361,11 +404,18 @@ impl SimpleSync { pub fn on_beacon_block_bodies_response( &mut self, peer_id: PeerId, - response: BeaconBlockBodiesResponse, + res: BeaconBlockBodiesResponse, network: &mut NetworkContext, ) { + debug!( + self.log, + "BlockBodiesResponse"; + "peer" => format!("{:?}", peer_id), + "count" => res.block_bodies.len(), + ); + self.import_queue - .enqueue_bodies(response.block_bodies, peer_id.clone()); + .enqueue_bodies(res.block_bodies, peer_id.clone()); // Clear out old entries self.import_queue.remove_stale(); @@ -427,11 +477,11 @@ impl SimpleSync { fn request_block_roots( &mut self, peer_id: PeerId, - request: BeaconBlockRootsRequest, + req: BeaconBlockRootsRequest, network: &mut NetworkContext, ) { // Potentially set state to sync. - if self.state == SyncState::Idle && request.count > SLOT_IMPORT_TOLERANCE { + if self.state == SyncState::Idle && req.count > SLOT_IMPORT_TOLERANCE { debug!(self.log, "Entering downloading sync state."); self.state = SyncState::Downloading; } @@ -439,44 +489,44 @@ impl SimpleSync { debug!( self.log, "RPCRequest(BeaconBlockRoots)"; - "count" => request.count, + "count" => req.count, "peer" => format!("{:?}", peer_id) ); // TODO: handle count > max count. - network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockRoots(request)); + network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockRoots(req)); } fn request_block_headers( &mut self, peer_id: PeerId, - request: BeaconBlockHeadersRequest, + req: BeaconBlockHeadersRequest, network: &mut NetworkContext, ) { debug!( self.log, "RPCRequest(BeaconBlockHeaders)"; - "max_headers" => request.max_headers, + "max_headers" => req.max_headers, "peer" => format!("{:?}", peer_id) ); - network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockHeaders(request)); + network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockHeaders(req)); } fn request_block_bodies( &mut self, peer_id: PeerId, - request: BeaconBlockBodiesRequest, + req: BeaconBlockBodiesRequest, network: &mut NetworkContext, ) { debug!( self.log, "RPCRequest(BeaconBlockBodies)"; - "count" => request.block_roots.len(), + "count" => req.block_roots.len(), "peer" => format!("{:?}", peer_id) ); - network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockBodies(request)); + network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockBodies(req)); } /// Generates our current state in the form of a HELLO RPC message. From 5f4f67f46f1f089b598f960e9e2b868e4e6c1a6d Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 14:22:12 +1100 Subject: [PATCH 039/191] Swap ImportQueue from a Map to a Vec There's an edge case where different blocks can have the same block body. --- beacon_node/beacon_chain/src/beacon_chain.rs | 9 ++++ beacon_node/network/src/sync/simple_sync.rs | 54 ++++++++++++-------- beacon_node/network/tests/tests.rs | 3 ++ 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index d5fd113a80..966e73210b 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -64,6 +64,15 @@ impl BlockProcessingOutcome { }, } } + + /// Returns `true` if the block was successfully processed and can be removed from any import + /// queues or temporary storage. + pub fn sucessfully_processed(&self) -> bool { + match self { + BlockProcessingOutcome::ValidBlock(_) => true, + _ => false, + } + } } pub struct BeaconChain { diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 7609f5750d..b77a976b1d 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -425,14 +425,15 @@ impl SimpleSync { } pub fn process_import_queue(&mut self, network: &mut NetworkContext) { - let mut blocks: Vec<(Hash256, BeaconBlock, PeerId)> = self + let mut blocks: Vec<(usize, BeaconBlock, PeerId)> = self .import_queue .partials .iter() - .filter_map(|(key, partial)| { + .enumerate() + .filter_map(|(i, partial)| { if let Some(_) = partial.body { let (block, _root) = partial.clone().complete().expect("Body must be Some"); - Some((*key, block, partial.sender.clone())) + Some((i, block, partial.sender.clone())) } else { None } @@ -469,7 +470,7 @@ impl SimpleSync { if !keys_to_delete.is_empty() { info!(self.log, "Processed {} blocks", keys_to_delete.len()); for key in keys_to_delete { - self.import_queue.partials.remove(&key); + self.import_queue.partials.remove(key); } } } @@ -539,7 +540,7 @@ pub struct ImportQueue { /// BeaconChain pub chain: Arc, /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. - pub partials: HashMap, + pub partials: Vec, /// Time before a queue entry is consider state. pub stale_time: Duration, /// Logging @@ -550,7 +551,7 @@ impl ImportQueue { pub fn new(chain: Arc, stale_time: Duration, log: slog::Logger) -> Self { Self { chain, - partials: HashMap::new(), + partials: vec![], stale_time, log, } @@ -561,29 +562,30 @@ impl ImportQueue { /// An entry is stale if it has as a `inserted` time that is more than `self.stale_time` in the /// past. pub fn remove_stale(&mut self) { - let keys: Vec = self + let stale_indices: Vec = self .partials .iter() - .filter_map(|(key, partial)| { + .enumerate() + .filter_map(|(i, partial)| { if partial.inserted + self.stale_time <= Instant::now() { - Some(*key) + Some(i) } else { None } }) .collect(); - if !keys.is_empty() { + if !stale_indices.is_empty() { debug!( self.log, "ImportQueue removing stale entries"; - "stale_count" => keys.len(), + "stale_items" => stale_indices.len(), "stale_time_seconds" => self.stale_time.as_secs() ); } - keys.iter().for_each(|key| { - self.partials.remove(&key); + stale_indices.iter().for_each(|&i| { + self.partials.remove(i); }); } @@ -646,29 +648,39 @@ impl ImportQueue { /// If the header already exists, the `inserted` time is set to `now` and not other /// modifications are made. fn insert_header(&mut self, block_root: Hash256, header: BeaconBlockHeader, sender: PeerId) { - self.partials - .entry(header.block_body_root) - .and_modify(|p| p.inserted = Instant::now()) - .or_insert(PartialBeaconBlock { + if let Some(i) = self + .partials + .iter() + .position(|p| p.block_root == block_root) + { + self.partials[i].inserted = Instant::now(); + } else { + self.partials.push(PartialBeaconBlock { block_root, header, body: None, inserted: Instant::now(), sender, - }); + }) + } } /// Updates an existing partial with the `body`. /// /// If there is no header for the `body`, the body is simply discarded. + /// + /// If the body already existed, the `inserted` time is set to `now`. fn insert_body(&mut self, body: BeaconBlockBody, sender: PeerId) { let body_root = Hash256::from_slice(&body.hash_tree_root()[..]); - self.partials.entry(body_root).and_modify(|p| { + self.partials.iter_mut().for_each(|mut p| { if body_root == p.header.block_body_root { - p.body = Some(body); p.inserted = Instant::now(); - p.sender = sender; + + if p.body.is_none() { + p.body = Some(body.clone()); + p.sender = sender.clone(); + } } }); } diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index b951d7d2a2..2952e5105b 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -511,7 +511,10 @@ fn sync_two_nodes() { // Node A builds out a longer, better chain. for _ in 0..blocks { + // Node A should build a block. node_a.harness.advance_chain_with_block(); + // Node B should just increment it's slot without a block. + node_b.harness.increment_beacon_chain_slot(); } node_a.harness.run_fork_choice(); From 1ea995963254ea4e4e8cd7f1a52d27d4a1b2e1ba Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 15:18:21 +1100 Subject: [PATCH 040/191] Fix bug with block processing in sync --- beacon_node/network/src/sync/simple_sync.rs | 104 +++++++++++++------- 1 file changed, 66 insertions(+), 38 deletions(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index b77a976b1d..369564a5e9 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -425,54 +425,45 @@ impl SimpleSync { } pub fn process_import_queue(&mut self, network: &mut NetworkContext) { - let mut blocks: Vec<(usize, BeaconBlock, PeerId)> = self - .import_queue - .partials - .iter() - .enumerate() - .filter_map(|(i, partial)| { - if let Some(_) = partial.body { - let (block, _root) = partial.clone().complete().expect("Body must be Some"); - Some((i, block, partial.sender.clone())) - } else { - None - } - }) - .collect(); + let mut successful = 0; + let mut invalid = 0; + let mut errored = 0; - if !blocks.is_empty() { - info!(self.log, "Processing blocks"; "count" => blocks.len()); - } - - // Sort the blocks to be in ascending slot order. - blocks.sort_unstable_by(|a, b| a.1.slot.partial_cmp(&b.1.slot).unwrap()); - - let mut keys_to_delete = vec![]; - - for (key, block, sender) in blocks { + // Loop through all of the complete blocks in the queue. + for (queue_index, block, sender) in self.import_queue.complete_blocks() { match self.chain.process_block(block) { Ok(outcome) => { if outcome.is_invalid() { - warn!(self.log, "Invalid block: {:?}", outcome); + invalid += 1; + warn!( + self.log, + "InvalidBlock"; + "sender_peer_id" => format!("{:?}", sender), + "reason" => format!("{:?}", outcome), + ); network.disconnect(sender); - keys_to_delete.push(key) - } else { - // TODO: don't delete if was not invalid but not successfully processed. - keys_to_delete.push(key) + } + + // If this results to true, the item will be removed from the queue. + if outcome.sucessfully_processed() { + successful += 1; + self.import_queue.partials.remove(queue_index); } } Err(e) => { - error!(self.log, "Error during block processing"; "error" => format!("{:?}", e)) + errored += 1; + error!(self.log, "BlockProcessingError"; "error" => format!("{:?}", e)); } } } - if !keys_to_delete.is_empty() { - info!(self.log, "Processed {} blocks", keys_to_delete.len()); - for key in keys_to_delete { - self.import_queue.partials.remove(key); - } - } + info!( + self.log, + "ProcessBlocks"; + "invalid" => invalid, + "successful" => successful, + "errored" => errored, + ) } fn request_block_roots( @@ -557,6 +548,35 @@ impl ImportQueue { } } + /// Completes all possible partials into `BeaconBlock` and returns them, sorted by slot number. + /// Does not delete the partials from the queue, this must be done manually. + /// + /// Returns `(queue_index, block, sender)`: + /// + /// - `queue_index`: used to remove the entry if it is successfully processed. + /// - `block`: the completed block. + /// - `sender`: the `PeerId` the provided the `BeaconBlockBody` which completed the partial. + pub fn complete_blocks(&self) -> Vec<(usize, BeaconBlock, PeerId)> { + let mut completable: Vec<(usize, &PartialBeaconBlock)> = self + .partials + .iter() + .enumerate() + .filter(|(_i, partial)| partial.completable()) + .collect(); + + // Sort the completable partials to be in ascending slot order. + completable.sort_unstable_by(|a, b| a.1.header.slot.partial_cmp(&b.1.header.slot).unwrap()); + + completable + .iter() + .map(|(i, partial)| { + let (block, _root, sender) = + (*partial).clone().complete().expect("Body must be Some"); + (*i, block, sender) + }) + .collect() + } + /// Flushes all stale entries from the queue. /// /// An entry is stale if it has as a `inserted` time that is more than `self.stale_time` in the @@ -696,8 +716,16 @@ pub struct PartialBeaconBlock { } impl PartialBeaconBlock { + pub fn completable(&self) -> bool { + self.body.is_some() + } + /// Given a `body`, consumes `self` and returns a complete `BeaconBlock` along with its root. - pub fn complete(self) -> Option<(BeaconBlock, Hash256)> { - Some((self.header.into_block(self.body?), self.block_root)) + pub fn complete(self) -> Option<(BeaconBlock, Hash256, PeerId)> { + Some(( + self.header.into_block(self.body?), + self.block_root, + self.sender, + )) } } From 3dc5595a6fbbc3fd22e09fbcd36ca978d4b4186b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 16:35:07 +1100 Subject: [PATCH 041/191] Fix last errors stopping full chain sync --- beacon_node/beacon_chain/src/beacon_chain.rs | 87 +++++++++++++------ beacon_node/beacon_chain/src/checkpoint.rs | 2 +- beacon_node/beacon_chain/src/errors.rs | 5 ++ .../test_harness/src/beacon_chain_harness.rs | 4 +- beacon_node/network/src/sync/simple_sync.rs | 60 ++++++------- beacon_node/network/tests/tests.rs | 24 ++++- 6 files changed, 121 insertions(+), 61 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 966e73210b..745ba51553 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -26,7 +26,10 @@ pub enum ValidBlock { #[derive(Debug, PartialEq)] pub enum InvalidBlock { /// The block slot is greater than the present slot. - FutureSlot, + FutureSlot { + present_slot: Slot, + block_slot: Slot, + }, /// The block state_root does not match the generated state. StateRootMismatch, /// The blocks parent_root is unknown. @@ -53,7 +56,7 @@ impl BlockProcessingOutcome { match self { BlockProcessingOutcome::ValidBlock(_) => false, BlockProcessingOutcome::InvalidBlock(r) => match r { - InvalidBlock::FutureSlot => true, + InvalidBlock::FutureSlot { .. } => true, InvalidBlock::StateRootMismatch => true, InvalidBlock::ParentUnknown => false, InvalidBlock::SlotProcessingError(_) => false, @@ -302,6 +305,49 @@ where self.canonical_head.read() } + /// Updates the canonical `BeaconState` with the supplied state. + /// + /// Advances the chain forward to the present slot. This method is better than just setting + /// state and calling `catchup_state` as it will not result in an old state being installed and + /// then having it iteratively updated -- in such a case it's possible for another thread to + /// find the state at an old slot. + pub fn update_state(&self, mut state: BeaconState) -> Result<(), Error> { + let latest_block_header = self.head().beacon_block.block_header(); + + let present_slot = match self.slot_clock.present_slot() { + Ok(Some(slot)) => slot, + _ => return Err(Error::UnableToReadSlot), + }; + + // If required, transition the new state to the present slot. + for _ in state.slot.as_u64()..present_slot.as_u64() { + per_slot_processing(&mut state, &latest_block_header, &self.spec)?; + } + + *self.state.write() = state; + + Ok(()) + } + + /// Ensures the current canonical `BeaconState` has been transitioned to match the `slot_clock`. + pub fn catchup_state(&self) -> Result<(), Error> { + let latest_block_header = self.head().beacon_block.block_header(); + + let present_slot = match self.slot_clock.present_slot() { + Ok(Some(slot)) => slot, + _ => return Err(Error::UnableToReadSlot), + }; + + let mut state = self.state.write(); + + // If required, transition the new state to the present slot. + for _ in state.slot.as_u64()..present_slot.as_u64() { + per_slot_processing(&mut *state, &latest_block_header, &self.spec)?; + } + + Ok(()) + } + /// Update the justified head to some new values. pub fn update_finalized_head( &self, @@ -325,28 +371,6 @@ where self.finalized_head.read() } - /// Advance the `self.state` `BeaconState` to the supplied slot. - /// - /// This will perform per_slot and per_epoch processing as required. - /// - /// The `previous_block_root` will be set to the root of the current head block (as determined - /// by the fork-choice rule). - /// - /// It is important to note that this is _not_ the state corresponding to the canonical head - /// block, instead it is that state which may or may not have had additional per slot/epoch - /// processing applied to it. - pub fn advance_state(&self, slot: Slot) -> Result<(), SlotProcessingError> { - let state_slot = self.state.read().slot; - - let latest_block_header = self.head().beacon_block.block_header(); - - for _ in state_slot.as_u64()..slot.as_u64() { - per_slot_processing(&mut *self.state.write(), &latest_block_header, &self.spec)?; - } - - Ok(()) - } - /// Returns the validator index (if any) for the given public key. /// /// Information is retrieved from the present `beacon_state.validator_registry`. @@ -724,7 +748,10 @@ where if block.slot > present_slot { return Ok(BlockProcessingOutcome::InvalidBlock( - InvalidBlock::FutureSlot, + InvalidBlock::FutureSlot { + present_slot, + block_slot: block.slot, + }, )); } @@ -800,8 +827,9 @@ where // run instead. if self.head().beacon_block_root == parent_block_root { self.update_canonical_head(block.clone(), block_root, state.clone(), state_root); - // Update the local state variable. - *self.state.write() = state; + + // Update the canonical `BeaconState`. + self.update_state(state)?; } Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed)) @@ -891,7 +919,10 @@ where .ok_or_else(|| Error::MissingBeaconState(block.state_root))?; let state_root = state.canonical_root(); - self.update_canonical_head(block, block_root, state, state_root); + self.update_canonical_head(block, block_root, state.clone(), state_root); + + // Update the canonical `BeaconState`. + self.update_state(state)?; } Ok(()) diff --git a/beacon_node/beacon_chain/src/checkpoint.rs b/beacon_node/beacon_chain/src/checkpoint.rs index 828e462de3..78227e5c83 100644 --- a/beacon_node/beacon_chain/src/checkpoint.rs +++ b/beacon_node/beacon_chain/src/checkpoint.rs @@ -3,7 +3,7 @@ use types::{BeaconBlock, BeaconState, Hash256}; /// Represents some block and it's associated state. Generally, this will be used for tracking the /// head, justified head and finalized head. -#[derive(Clone, Serialize)] +#[derive(Clone, Serialize, PartialEq, Debug)] pub struct CheckPoint { pub beacon_block: BeaconBlock, pub beacon_block_root: Hash256, diff --git a/beacon_node/beacon_chain/src/errors.rs b/beacon_node/beacon_chain/src/errors.rs index b5f17efd24..a84e4b10e9 100644 --- a/beacon_node/beacon_chain/src/errors.rs +++ b/beacon_node/beacon_chain/src/errors.rs @@ -1,5 +1,6 @@ use fork_choice::ForkChoiceError; use state_processing::BlockProcessingError; +use state_processing::SlotProcessingError; use types::*; macro_rules! easy_from_to { @@ -16,14 +17,18 @@ macro_rules! easy_from_to { pub enum BeaconChainError { InsufficientValidators, BadRecentBlockRoots, + UnableToReadSlot, BeaconStateError(BeaconStateError), DBInconsistent(String), DBError(String), ForkChoiceError(ForkChoiceError), MissingBeaconBlock(Hash256), MissingBeaconState(Hash256), + SlotProcessingError(SlotProcessingError), } +easy_from_to!(SlotProcessingError, BeaconChainError); + #[derive(Debug, PartialEq)] pub enum BlockProductionError { UnableToGetBlockRootFromState, 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 1207fcf28f..1498796b18 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 @@ -131,7 +131,9 @@ impl BeaconChainHarness { ); self.beacon_chain.slot_clock.set_slot(slot.as_u64()); - self.beacon_chain.advance_state(slot).unwrap(); + self.beacon_chain + .catchup_state() + .expect("Failed to catch state"); slot } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 369564a5e9..76d630b9a8 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -430,7 +430,7 @@ impl SimpleSync { let mut errored = 0; // Loop through all of the complete blocks in the queue. - for (queue_index, block, sender) in self.import_queue.complete_blocks() { + for (block_root, block, sender) in self.import_queue.complete_blocks() { match self.chain.process_block(block) { Ok(outcome) => { if outcome.is_invalid() { @@ -447,7 +447,7 @@ impl SimpleSync { // If this results to true, the item will be removed from the queue. if outcome.sucessfully_processed() { successful += 1; - self.import_queue.partials.remove(queue_index); + self.import_queue.remove(block_root); } } Err(e) => { @@ -457,13 +457,15 @@ impl SimpleSync { } } - info!( - self.log, - "ProcessBlocks"; - "invalid" => invalid, - "successful" => successful, - "errored" => errored, - ) + if successful > 0 { + info!(self.log, "Imported {} blocks", successful) + } + if invalid > 0 { + warn!(self.log, "Rejected {} invalid blocks", invalid) + } + if errored > 0 { + warn!(self.log, "Failed to process {} blocks", errored) + } } fn request_block_roots( @@ -548,33 +550,35 @@ impl ImportQueue { } } - /// Completes all possible partials into `BeaconBlock` and returns them, sorted by slot number. - /// Does not delete the partials from the queue, this must be done manually. + /// Completes all possible partials into `BeaconBlock` and returns them, sorted by increasing + /// slot number. Does not delete the partials from the queue, this must be done manually. /// /// Returns `(queue_index, block, sender)`: /// - /// - `queue_index`: used to remove the entry if it is successfully processed. + /// - `block_root`: may be used to remove the entry if it is successfully processed. /// - `block`: the completed block. /// - `sender`: the `PeerId` the provided the `BeaconBlockBody` which completed the partial. - pub fn complete_blocks(&self) -> Vec<(usize, BeaconBlock, PeerId)> { - let mut completable: Vec<(usize, &PartialBeaconBlock)> = self + pub fn complete_blocks(&self) -> Vec<(Hash256, BeaconBlock, PeerId)> { + let mut complete: Vec<(Hash256, BeaconBlock, PeerId)> = self .partials .iter() - .enumerate() - .filter(|(_i, partial)| partial.completable()) + .filter_map(|partial| partial.clone().complete()) .collect(); // Sort the completable partials to be in ascending slot order. - completable.sort_unstable_by(|a, b| a.1.header.slot.partial_cmp(&b.1.header.slot).unwrap()); + complete.sort_unstable_by(|a, b| a.1.slot.partial_cmp(&b.1.slot).unwrap()); - completable + complete + } + + /// Removes the first `PartialBeaconBlock` with a matching `block_root`, returning the partial + /// if it exists. + pub fn remove(&mut self, block_root: Hash256) -> Option { + let position = self + .partials .iter() - .map(|(i, partial)| { - let (block, _root, sender) = - (*partial).clone().complete().expect("Body must be Some"); - (*i, block, sender) - }) - .collect() + .position(|p| p.block_root == block_root)?; + Some(self.partials.remove(position)) } /// Flushes all stale entries from the queue. @@ -716,15 +720,11 @@ pub struct PartialBeaconBlock { } impl PartialBeaconBlock { - pub fn completable(&self) -> bool { - self.body.is_some() - } - /// Given a `body`, consumes `self` and returns a complete `BeaconBlock` along with its root. - pub fn complete(self) -> Option<(BeaconBlock, Hash256, PeerId)> { + pub fn complete(self) -> Option<(Hash256, BeaconBlock, PeerId)> { Some(( - self.header.into_block(self.body?), self.block_root, + self.header.into_block(self.body?), self.sender, )) } diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index 2952e5105b..e12bf26280 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -541,6 +541,28 @@ fn sync_two_nodes() { // A provides block bodies to B. node_a.tee_block_body_response(&node_b); - std::thread::sleep(Duration::from_secs(60)); + std::thread::sleep(Duration::from_secs(10)); + + node_b.harness.run_fork_choice(); + + let node_a_chain = node_a + .harness + .beacon_chain + .chain_dump() + .expect("Can't dump node a chain"); + + let node_b_chain = node_b + .harness + .beacon_chain + .chain_dump() + .expect("Can't dump node b chain"); + + assert_eq!( + node_a_chain.len(), + node_b_chain.len(), + "Chains should be equal length" + ); + assert_eq!(node_a_chain, node_b_chain, "Chains should be identical"); + runtime.shutdown_now(); } From 796b68dc042920a49336aa646cbb04ed16da1add Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 17:51:32 +1100 Subject: [PATCH 042/191] Implement Goodbye and BeaconState msg handlers --- beacon_node/eth2-libp2p/src/rpc/methods.rs | 52 ++++++++++++++++++++- beacon_node/eth2-libp2p/src/rpc/protocol.rs | 4 +- beacon_node/network/src/message_handler.rs | 22 ++++++--- beacon_node/network/src/sync/simple_sync.rs | 10 ++++ 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index f6a5f28296..85ef7e06f7 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -1,3 +1,4 @@ +use ssz::{Decodable, DecodeError, Encodable, SszStream}; /// Available RPC methods types and ids. use ssz_derive::{Decode, Encode}; use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; @@ -53,7 +54,7 @@ impl Into for RPCMethod { #[derive(Debug, Clone)] pub enum RPCRequest { Hello(HelloMessage), - Goodbye(u64), + Goodbye(GoodbyeReason), BeaconBlockRoots(BeaconBlockRootsRequest), BeaconBlockHeaders(BeaconBlockHeadersRequest), BeaconBlockBodies(BeaconBlockBodiesRequest), @@ -113,6 +114,55 @@ pub struct HelloMessage { pub best_slot: Slot, } +/// The reason given for a `Goodbye` message. +/// +/// Note: any unknown `u64::into(n)` will resolve to `GoodbyeReason::Unknown` for any unknown `n`, +/// however `GoodbyeReason::Unknown.into()` will go into `0_u64`. Therefore de-serializing then +/// re-serializing may not return the same bytes. +#[derive(Debug, Clone)] +pub enum GoodbyeReason { + ClientShutdown, + IrreleventNetwork, + Fault, + Unknown, +} + +impl From for GoodbyeReason { + fn from(id: u64) -> GoodbyeReason { + match id { + 1 => GoodbyeReason::ClientShutdown, + 2 => GoodbyeReason::IrreleventNetwork, + 3 => GoodbyeReason::Fault, + _ => GoodbyeReason::Unknown, + } + } +} + +impl Into for GoodbyeReason { + fn into(self) -> u64 { + match self { + GoodbyeReason::Unknown => 0, + GoodbyeReason::ClientShutdown => 1, + GoodbyeReason::IrreleventNetwork => 2, + GoodbyeReason::Fault => 3, + } + } +} + +impl Encodable for GoodbyeReason { + fn ssz_append(&self, s: &mut SszStream) { + let id: u64 = (*self).clone().into(); + id.ssz_append(s); + } +} + +impl Decodable for GoodbyeReason { + fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> { + let (id, index) = u64::ssz_decode(bytes, index)?; + Ok((Self::from(id), index)) + } +} + /// Request a number of beacon block roots from a peer. #[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BeaconBlockRootsRequest { diff --git a/beacon_node/eth2-libp2p/src/rpc/protocol.rs b/beacon_node/eth2-libp2p/src/rpc/protocol.rs index f4fe26fac3..b328dd0ddf 100644 --- a/beacon_node/eth2-libp2p/src/rpc/protocol.rs +++ b/beacon_node/eth2-libp2p/src/rpc/protocol.rs @@ -82,8 +82,8 @@ fn decode(packet: Vec) -> Result { RPCRequest::Hello(hello_body) } RPCMethod::Goodbye => { - let (goodbye_code, _index) = u64::ssz_decode(&packet, index)?; - RPCRequest::Goodbye(goodbye_code) + let (goodbye_reason, _index) = GoodbyeReason::ssz_decode(&packet, index)?; + RPCRequest::Goodbye(goodbye_reason) } RPCMethod::BeaconBlockRoots => { let (block_roots_request, _index) = diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 77a2ab8db9..57923b2c3e 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -107,13 +107,14 @@ impl MessageHandler { } /// A new RPC request has been received from the network. - fn handle_rpc_request(&mut self, peer_id: PeerId, id: u64, request: RPCRequest) { - // TODO: ensure the id is legit + fn handle_rpc_request(&mut self, peer_id: PeerId, _id: u64, request: RPCRequest) { + // TODO: process the `id`. match request { RPCRequest::Hello(hello_message) => { self.sync .on_hello_request(peer_id, hello_message, &mut self.network_context) } + RPCRequest::Goodbye(goodbye_reason) => self.sync.on_goodbye(peer_id, goodbye_reason), RPCRequest::BeaconBlockRoots(request) => { self.sync .on_beacon_block_roots_request(peer_id, request, &mut self.network_context) @@ -128,8 +129,11 @@ impl MessageHandler { request, &mut self.network_context, ), - // TODO: Handle all requests - _ => panic!("Unknown request: {:?}", request), + RPCRequest::BeaconChainState(_) => { + // We do not implement this endpoint, it is not required and will only likely be + // useful for light-client support in later phases. + warn!(self.log, "BeaconChainState RPC call is not supported."); + } } } @@ -172,8 +176,14 @@ impl MessageHandler { &mut self.network_context, ); } - // TODO: Handle all responses - _ => panic!("Unknown response: {:?}", response), + RPCResponse::BeaconChainState(_) => { + // We do not implement this endpoint, it is not required and will only likely be + // useful for light-client support in later phases. + // + // Theoretically, we shouldn't reach this code because we should never send a + // beacon state RPC request. + warn!(self.log, "BeaconChainState RPC call is not supported."); + } }; } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 76d630b9a8..14564aa378 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -119,6 +119,16 @@ impl SimpleSync { } } + pub fn on_goodbye(&mut self, peer_id: PeerId, reason: GoodbyeReason) { + info!( + self.log, "PeerGoodbye"; + "peer" => format!("{:?}", peer_id), + "reason" => format!("{:?}", reason), + ); + + self.known_peers.remove(&peer_id); + } + pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) { info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id)); From 07b6f7084292c864b3d0f969c36abd6c6dde4ecd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 17:58:30 +1100 Subject: [PATCH 043/191] Ignore syncing tests (they run too long) --- beacon_node/network/tests/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index e12bf26280..110450dc90 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -450,6 +450,7 @@ pub fn build_blocks(blocks: usize, master: &mut SyncMaster, nodes: &mut Vec Date: Sun, 24 Mar 2019 18:31:03 +1100 Subject: [PATCH 044/191] Add comments to SimpleSync --- beacon_node/network/src/sync/simple_sync.rs | 75 +++++++++++++++++++-- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 14564aa378..d6b9e63ae0 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -27,18 +27,22 @@ pub struct PeerSyncInfo { } impl PeerSyncInfo { + /// Returns `true` if the peer is on the same chain as `other`. fn is_on_same_chain(&self, other: Self) -> bool { self.network_id == other.network_id } + /// Returns `true` if the peer has a higher finalized epoch than `other`. fn has_higher_finalized_epoch_than(&self, other: Self) -> bool { self.latest_finalized_epoch > other.latest_finalized_epoch } + /// Returns `true` if the peer has a higher best slot than `other`. fn has_higher_best_slot_than(&self, other: Self) -> bool { self.best_slot > other.best_slot } + /// Returns the `PeerStatus` of `self` in relation to `other`. pub fn status_compared_to(&self, other: Self) -> PeerStatus { if self.has_higher_finalized_epoch_than(other) { PeerStatus::HigherFinalizedEpoch @@ -52,11 +56,17 @@ impl PeerSyncInfo { } } +/// The status of a peers view on the chain, relative to some other view of the chain (presumably +/// our view). #[derive(PartialEq, Clone, Copy, Debug)] pub enum PeerStatus { + /// The peer is on a completely different chain. OnDifferentChain, + /// The peer has a higher finalized epoch. HigherFinalizedEpoch, + /// The peer has a higher best slot. HigherBestSlot, + /// The peer has the same or lesser view of the chain. We have nothing to request of them. NotInteresting, } @@ -87,8 +97,6 @@ pub enum SyncState { } /// Simple Syncing protocol. -//TODO: Decide for HELLO messages whether its better to keep current in RAM or build on the fly -//when asked. pub struct SimpleSync { /// A reference to the underlying beacon chain. chain: Arc, @@ -103,6 +111,7 @@ pub struct SimpleSync { } impl SimpleSync { + /// Instantiate a `SimpleSync` instance, with no peers and an empty queue. pub fn new(beacon_chain: Arc, log: &slog::Logger) -> Self { let sync_logger = log.new(o!("Service"=> "Sync")); @@ -119,7 +128,15 @@ impl SimpleSync { } } - pub fn on_goodbye(&mut self, peer_id: PeerId, reason: GoodbyeReason) { + /// Handle a `Goodbye` message from a peer. + /// + /// Removes the peer from `known_peers`. + pub fn on_goodbye( + &mut self, + peer_id: PeerId, + reason: GoodbyeReason, + ddnetwork: &mut NetworkContext, + ) { info!( self.log, "PeerGoodbye"; "peer" => format!("{:?}", peer_id), @@ -129,12 +146,18 @@ impl SimpleSync { self.known_peers.remove(&peer_id); } + /// Handle the connection of a new peer. + /// + /// Sends a `Hello` message to the peer. pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) { info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id)); network.send_rpc_request(peer_id, RPCRequest::Hello(self.chain.hello_message())); } + /// Handle a `Hello` request. + /// + /// Processes the `HelloMessage` from the remote peer and sends back our `Hello`. pub fn on_hello_request( &mut self, peer_id: PeerId, @@ -152,6 +175,7 @@ impl SimpleSync { self.process_hello(peer_id, hello, network); } + /// Process a `Hello` response from a peer. pub fn on_hello_response( &mut self, peer_id: PeerId, @@ -164,6 +188,9 @@ impl SimpleSync { self.process_hello(peer_id, hello, network); } + /// Process a `Hello` message, requesting new blocks if appropriate. + /// + /// Disconnects the peer if required. fn process_hello( &mut self, peer_id: PeerId, @@ -223,6 +250,7 @@ impl SimpleSync { } } + /// Handle a `BeaconBlockRoots` request from the peer. pub fn on_beacon_block_roots_request( &mut self, peer_id: PeerId, @@ -268,6 +296,7 @@ impl SimpleSync { ) } + /// Handle a `BeaconBlockRoots` response from the peer. pub fn on_beacon_block_roots_response( &mut self, peer_id: PeerId, @@ -311,6 +340,7 @@ impl SimpleSync { } } + /// Handle a `BeaconBlockHeaders` request from the peer. pub fn on_beacon_block_headers_request( &mut self, peer_id: PeerId, @@ -348,6 +378,7 @@ impl SimpleSync { ) } + /// Handle a `BeaconBlockHeaders` response from the peer. pub fn on_beacon_block_headers_response( &mut self, peer_id: PeerId, @@ -378,6 +409,7 @@ impl SimpleSync { self.request_block_bodies(peer_id, BeaconBlockBodiesRequest { block_roots }, network); } + /// Handle a `BeaconBlockBodies` request from the peer. pub fn on_beacon_block_bodies_request( &mut self, peer_id: PeerId, @@ -411,6 +443,7 @@ impl SimpleSync { ) } + /// Handle a `BeaconBlockBodies` response from the peer. pub fn on_beacon_block_bodies_response( &mut self, peer_id: PeerId, @@ -434,6 +467,10 @@ impl SimpleSync { self.process_import_queue(network); } + /// Iterate through the `import_queue` and process any complete blocks. + /// + /// If a block is successfully processed it is removed from the queue, otherwise it remains in + /// the queue. pub fn process_import_queue(&mut self, network: &mut NetworkContext) { let mut successful = 0; let mut invalid = 0; @@ -478,6 +515,7 @@ impl SimpleSync { } } + /// Request some `BeaconBlockRoots` from the remote peer. fn request_block_roots( &mut self, peer_id: PeerId, @@ -501,6 +539,7 @@ impl SimpleSync { network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockRoots(req)); } + /// Request some `BeaconBlockHeaders` from the remote peer. fn request_block_headers( &mut self, peer_id: PeerId, @@ -517,6 +556,7 @@ impl SimpleSync { network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockHeaders(req)); } + /// Request some `BeaconBlockBodies` from the remote peer. fn request_block_bodies( &mut self, peer_id: PeerId, @@ -539,18 +579,30 @@ impl SimpleSync { } } +/// Provides a queue for fully and partially built `BeaconBlock`s. +/// +/// The queue is fundamentally a `Vec` where no two items have the same +/// `item.block_root`. This struct it backed by a `Vec` not a `HashMap` for the following two +/// reasons: +/// +/// - When we receive a `BeaconBlockBody`, the only way we can find it's matching +/// `BeaconBlockHeader` is to find a header such that `header.beacon_block_body == +/// hash_tree_root(body)`. Therefore, if we used a `HashMap` we would need to use the root of +/// `BeaconBlockBody` as the key. +/// - It is possible for multiple distinct blocks to have identical `BeaconBlockBodies`. Therefore +/// we cannot use a `HashMap` keyed by the root of `BeaconBlockBody`. pub struct ImportQueue { - /// BeaconChain pub chain: Arc, /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. pub partials: Vec, - /// Time before a queue entry is consider state. + /// Time before a queue entry is considered state. pub stale_time: Duration, /// Logging log: slog::Logger, } impl ImportQueue { + /// Return a new, empty queue. pub fn new(chain: Arc, stale_time: Duration, log: slog::Logger) -> Self { Self { chain, @@ -649,6 +701,10 @@ impl ImportQueue { /// If a `header` is already in the queue, but not yet processed by the chain the block root is /// included in the output and the `inserted` time for the partial record is set to /// `Instant::now()`. Updating the `inserted` time stops the partial from becoming stale. + /// + /// Presently the queue enforces that a `BeaconBlockHeader` _must_ be received before its + /// `BeaconBlockBody`. This is not a natural requirement and we could enhance the queue to lift + /// this restraint. pub fn enqueue_headers( &mut self, headers: Vec, @@ -720,17 +776,24 @@ impl ImportQueue { } } +/// Individual components of a `BeaconBlock`, potentially all that are required to form a full +/// `BeaconBlock`. #[derive(Clone, Debug)] pub struct PartialBeaconBlock { + /// `BeaconBlock` root. pub block_root: Hash256, pub header: BeaconBlockHeader, pub body: Option, + /// The instant at which this record was created or last meaningfully modified. Used to + /// determine if an entry is stale and should be removed. pub inserted: Instant, + /// The `PeerId` that last meaningfully contributed to this item. pub sender: PeerId, } impl PartialBeaconBlock { - /// Given a `body`, consumes `self` and returns a complete `BeaconBlock` along with its root. + /// Consumes `self` and returns a full built `BeaconBlock`, it's root and the `sender` + /// `PeerId`, if enough information exists to complete the block. Otherwise, returns `None`. pub fn complete(self) -> Option<(Hash256, BeaconBlock, PeerId)> { Some(( self.block_root, From 755a09d164f7790cd74fda8efd3ae8d033579a88 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 18:34:44 +1100 Subject: [PATCH 045/191] Move ImportQueue into own file --- beacon_node/network/src/sync/import_queue.rs | 232 +++++++++++++++++++ beacon_node/network/src/sync/mod.rs | 1 + beacon_node/network/src/sync/simple_sync.rs | 230 +----------------- 3 files changed, 236 insertions(+), 227 deletions(-) create mode 100644 beacon_node/network/src/sync/import_queue.rs diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs new file mode 100644 index 0000000000..6508af89e6 --- /dev/null +++ b/beacon_node/network/src/sync/import_queue.rs @@ -0,0 +1,232 @@ +use crate::beacon_chain::BeaconChain; +use eth2_libp2p::rpc::methods::*; +use eth2_libp2p::PeerId; +use slog::{debug, error}; +use ssz::TreeHash; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Hash256}; + +/// Provides a queue for fully and partially built `BeaconBlock`s. +/// +/// The queue is fundamentally a `Vec` where no two items have the same +/// `item.block_root`. This struct it backed by a `Vec` not a `HashMap` for the following two +/// reasons: +/// +/// - When we receive a `BeaconBlockBody`, the only way we can find it's matching +/// `BeaconBlockHeader` is to find a header such that `header.beacon_block_body == +/// hash_tree_root(body)`. Therefore, if we used a `HashMap` we would need to use the root of +/// `BeaconBlockBody` as the key. +/// - It is possible for multiple distinct blocks to have identical `BeaconBlockBodies`. Therefore +/// we cannot use a `HashMap` keyed by the root of `BeaconBlockBody`. +pub struct ImportQueue { + pub chain: Arc, + /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. + pub partials: Vec, + /// Time before a queue entry is considered state. + pub stale_time: Duration, + /// Logging + log: slog::Logger, +} + +impl ImportQueue { + /// Return a new, empty queue. + pub fn new(chain: Arc, stale_time: Duration, log: slog::Logger) -> Self { + Self { + chain, + partials: vec![], + stale_time, + log, + } + } + + /// Completes all possible partials into `BeaconBlock` and returns them, sorted by increasing + /// slot number. Does not delete the partials from the queue, this must be done manually. + /// + /// Returns `(queue_index, block, sender)`: + /// + /// - `block_root`: may be used to remove the entry if it is successfully processed. + /// - `block`: the completed block. + /// - `sender`: the `PeerId` the provided the `BeaconBlockBody` which completed the partial. + pub fn complete_blocks(&self) -> Vec<(Hash256, BeaconBlock, PeerId)> { + let mut complete: Vec<(Hash256, BeaconBlock, PeerId)> = self + .partials + .iter() + .filter_map(|partial| partial.clone().complete()) + .collect(); + + // Sort the completable partials to be in ascending slot order. + complete.sort_unstable_by(|a, b| a.1.slot.partial_cmp(&b.1.slot).unwrap()); + + complete + } + + /// Removes the first `PartialBeaconBlock` with a matching `block_root`, returning the partial + /// if it exists. + pub fn remove(&mut self, block_root: Hash256) -> Option { + let position = self + .partials + .iter() + .position(|p| p.block_root == block_root)?; + Some(self.partials.remove(position)) + } + + /// Flushes all stale entries from the queue. + /// + /// An entry is stale if it has as a `inserted` time that is more than `self.stale_time` in the + /// past. + pub fn remove_stale(&mut self) { + let stale_indices: Vec = self + .partials + .iter() + .enumerate() + .filter_map(|(i, partial)| { + if partial.inserted + self.stale_time <= Instant::now() { + Some(i) + } else { + None + } + }) + .collect(); + + if !stale_indices.is_empty() { + debug!( + self.log, + "ImportQueue removing stale entries"; + "stale_items" => stale_indices.len(), + "stale_time_seconds" => self.stale_time.as_secs() + ); + } + + stale_indices.iter().for_each(|&i| { + self.partials.remove(i); + }); + } + + /// Returns `true` if `self.chain` has not yet processed this block. + fn is_new_block(&self, block_root: &Hash256) -> bool { + self.chain + .is_new_block_root(&block_root) + .unwrap_or_else(|_| { + error!(self.log, "Unable to determine if block is new."); + true + }) + } + + /// Returns the index of the first new root in the list of block roots. + pub fn first_new_root(&mut self, roots: &[BlockRootSlot]) -> Option { + roots + .iter() + .position(|brs| self.is_new_block(&brs.block_root)) + } + + /// Adds the `headers` to the `partials` queue. Returns a list of `Hash256` block roots for + /// which we should use to request `BeaconBlockBodies`. + /// + /// If a `header` is not in the queue and has not been processed by the chain it is added to + /// the queue and it's block root is included in the output. + /// + /// If a `header` is already in the queue, but not yet processed by the chain the block root is + /// included in the output and the `inserted` time for the partial record is set to + /// `Instant::now()`. Updating the `inserted` time stops the partial from becoming stale. + /// + /// Presently the queue enforces that a `BeaconBlockHeader` _must_ be received before its + /// `BeaconBlockBody`. This is not a natural requirement and we could enhance the queue to lift + /// this restraint. + pub fn enqueue_headers( + &mut self, + headers: Vec, + sender: PeerId, + ) -> Vec { + let mut required_bodies: Vec = vec![]; + + for header in headers { + let block_root = Hash256::from_slice(&header.hash_tree_root()[..]); + + if self.is_new_block(&block_root) { + self.insert_header(block_root, header, sender.clone()); + required_bodies.push(block_root) + } + } + + required_bodies + } + + /// If there is a matching `header` for this `body`, adds it to the queue. + /// + /// If there is no `header` for the `body`, the body is simply discarded. + pub fn enqueue_bodies(&mut self, bodies: Vec, sender: PeerId) { + for body in bodies { + self.insert_body(body, sender.clone()); + } + } + + /// Inserts a header to the queue. + /// + /// If the header already exists, the `inserted` time is set to `now` and not other + /// modifications are made. + fn insert_header(&mut self, block_root: Hash256, header: BeaconBlockHeader, sender: PeerId) { + if let Some(i) = self + .partials + .iter() + .position(|p| p.block_root == block_root) + { + self.partials[i].inserted = Instant::now(); + } else { + self.partials.push(PartialBeaconBlock { + block_root, + header, + body: None, + inserted: Instant::now(), + sender, + }) + } + } + + /// Updates an existing partial with the `body`. + /// + /// If there is no header for the `body`, the body is simply discarded. + /// + /// If the body already existed, the `inserted` time is set to `now`. + fn insert_body(&mut self, body: BeaconBlockBody, sender: PeerId) { + let body_root = Hash256::from_slice(&body.hash_tree_root()[..]); + + self.partials.iter_mut().for_each(|mut p| { + if body_root == p.header.block_body_root { + p.inserted = Instant::now(); + + if p.body.is_none() { + p.body = Some(body.clone()); + p.sender = sender.clone(); + } + } + }); + } +} + +/// Individual components of a `BeaconBlock`, potentially all that are required to form a full +/// `BeaconBlock`. +#[derive(Clone, Debug)] +pub struct PartialBeaconBlock { + /// `BeaconBlock` root. + pub block_root: Hash256, + pub header: BeaconBlockHeader, + pub body: Option, + /// The instant at which this record was created or last meaningfully modified. Used to + /// determine if an entry is stale and should be removed. + pub inserted: Instant, + /// The `PeerId` that last meaningfully contributed to this item. + pub sender: PeerId, +} + +impl PartialBeaconBlock { + /// Consumes `self` and returns a full built `BeaconBlock`, it's root and the `sender` + /// `PeerId`, if enough information exists to complete the block. Otherwise, returns `None`. + pub fn complete(self) -> Option<(Hash256, BeaconBlock, PeerId)> { + Some(( + self.block_root, + self.header.into_block(self.body?), + self.sender, + )) + } +} diff --git a/beacon_node/network/src/sync/mod.rs b/beacon_node/network/src/sync/mod.rs index 8f5216b857..fac1b46eb0 100644 --- a/beacon_node/network/src/sync/mod.rs +++ b/beacon_node/network/src/sync/mod.rs @@ -1,3 +1,4 @@ +mod import_queue; /// Syncing for lighthouse. /// /// Stores the various syncing methods for the beacon chain. diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index d6b9e63ae0..c23a6ec569 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,14 +1,14 @@ +use super::import_queue::ImportQueue; use crate::beacon_chain::BeaconChain; use crate::message_handler::NetworkContext; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse}; use eth2_libp2p::PeerId; use slog::{debug, error, info, o, warn}; -use ssz::TreeHash; use std::collections::HashMap; use std::sync::Arc; -use std::time::{Duration, Instant}; -use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; +use std::time::Duration; +use types::{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; @@ -578,227 +578,3 @@ impl SimpleSync { self.chain.hello_message() } } - -/// Provides a queue for fully and partially built `BeaconBlock`s. -/// -/// The queue is fundamentally a `Vec` where no two items have the same -/// `item.block_root`. This struct it backed by a `Vec` not a `HashMap` for the following two -/// reasons: -/// -/// - When we receive a `BeaconBlockBody`, the only way we can find it's matching -/// `BeaconBlockHeader` is to find a header such that `header.beacon_block_body == -/// hash_tree_root(body)`. Therefore, if we used a `HashMap` we would need to use the root of -/// `BeaconBlockBody` as the key. -/// - It is possible for multiple distinct blocks to have identical `BeaconBlockBodies`. Therefore -/// we cannot use a `HashMap` keyed by the root of `BeaconBlockBody`. -pub struct ImportQueue { - pub chain: Arc, - /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. - pub partials: Vec, - /// Time before a queue entry is considered state. - pub stale_time: Duration, - /// Logging - log: slog::Logger, -} - -impl ImportQueue { - /// Return a new, empty queue. - pub fn new(chain: Arc, stale_time: Duration, log: slog::Logger) -> Self { - Self { - chain, - partials: vec![], - stale_time, - log, - } - } - - /// Completes all possible partials into `BeaconBlock` and returns them, sorted by increasing - /// slot number. Does not delete the partials from the queue, this must be done manually. - /// - /// Returns `(queue_index, block, sender)`: - /// - /// - `block_root`: may be used to remove the entry if it is successfully processed. - /// - `block`: the completed block. - /// - `sender`: the `PeerId` the provided the `BeaconBlockBody` which completed the partial. - pub fn complete_blocks(&self) -> Vec<(Hash256, BeaconBlock, PeerId)> { - let mut complete: Vec<(Hash256, BeaconBlock, PeerId)> = self - .partials - .iter() - .filter_map(|partial| partial.clone().complete()) - .collect(); - - // Sort the completable partials to be in ascending slot order. - complete.sort_unstable_by(|a, b| a.1.slot.partial_cmp(&b.1.slot).unwrap()); - - complete - } - - /// Removes the first `PartialBeaconBlock` with a matching `block_root`, returning the partial - /// if it exists. - pub fn remove(&mut self, block_root: Hash256) -> Option { - let position = self - .partials - .iter() - .position(|p| p.block_root == block_root)?; - Some(self.partials.remove(position)) - } - - /// Flushes all stale entries from the queue. - /// - /// An entry is stale if it has as a `inserted` time that is more than `self.stale_time` in the - /// past. - pub fn remove_stale(&mut self) { - let stale_indices: Vec = self - .partials - .iter() - .enumerate() - .filter_map(|(i, partial)| { - if partial.inserted + self.stale_time <= Instant::now() { - Some(i) - } else { - None - } - }) - .collect(); - - if !stale_indices.is_empty() { - debug!( - self.log, - "ImportQueue removing stale entries"; - "stale_items" => stale_indices.len(), - "stale_time_seconds" => self.stale_time.as_secs() - ); - } - - stale_indices.iter().for_each(|&i| { - self.partials.remove(i); - }); - } - - /// Returns `true` if `self.chain` has not yet processed this block. - fn is_new_block(&self, block_root: &Hash256) -> bool { - self.chain - .is_new_block_root(&block_root) - .unwrap_or_else(|_| { - error!(self.log, "Unable to determine if block is new."); - true - }) - } - - /// Returns the index of the first new root in the list of block roots. - pub fn first_new_root(&mut self, roots: &[BlockRootSlot]) -> Option { - roots - .iter() - .position(|brs| self.is_new_block(&brs.block_root)) - } - - /// Adds the `headers` to the `partials` queue. Returns a list of `Hash256` block roots for - /// which we should use to request `BeaconBlockBodies`. - /// - /// If a `header` is not in the queue and has not been processed by the chain it is added to - /// the queue and it's block root is included in the output. - /// - /// If a `header` is already in the queue, but not yet processed by the chain the block root is - /// included in the output and the `inserted` time for the partial record is set to - /// `Instant::now()`. Updating the `inserted` time stops the partial from becoming stale. - /// - /// Presently the queue enforces that a `BeaconBlockHeader` _must_ be received before its - /// `BeaconBlockBody`. This is not a natural requirement and we could enhance the queue to lift - /// this restraint. - pub fn enqueue_headers( - &mut self, - headers: Vec, - sender: PeerId, - ) -> Vec { - let mut required_bodies: Vec = vec![]; - - for header in headers { - let block_root = Hash256::from_slice(&header.hash_tree_root()[..]); - - if self.is_new_block(&block_root) { - self.insert_header(block_root, header, sender.clone()); - required_bodies.push(block_root) - } - } - - required_bodies - } - - /// If there is a matching `header` for this `body`, adds it to the queue. - /// - /// If there is no `header` for the `body`, the body is simply discarded. - pub fn enqueue_bodies(&mut self, bodies: Vec, sender: PeerId) { - for body in bodies { - self.insert_body(body, sender.clone()); - } - } - - /// Inserts a header to the queue. - /// - /// If the header already exists, the `inserted` time is set to `now` and not other - /// modifications are made. - fn insert_header(&mut self, block_root: Hash256, header: BeaconBlockHeader, sender: PeerId) { - if let Some(i) = self - .partials - .iter() - .position(|p| p.block_root == block_root) - { - self.partials[i].inserted = Instant::now(); - } else { - self.partials.push(PartialBeaconBlock { - block_root, - header, - body: None, - inserted: Instant::now(), - sender, - }) - } - } - - /// Updates an existing partial with the `body`. - /// - /// If there is no header for the `body`, the body is simply discarded. - /// - /// If the body already existed, the `inserted` time is set to `now`. - fn insert_body(&mut self, body: BeaconBlockBody, sender: PeerId) { - let body_root = Hash256::from_slice(&body.hash_tree_root()[..]); - - self.partials.iter_mut().for_each(|mut p| { - if body_root == p.header.block_body_root { - p.inserted = Instant::now(); - - if p.body.is_none() { - p.body = Some(body.clone()); - p.sender = sender.clone(); - } - } - }); - } -} - -/// Individual components of a `BeaconBlock`, potentially all that are required to form a full -/// `BeaconBlock`. -#[derive(Clone, Debug)] -pub struct PartialBeaconBlock { - /// `BeaconBlock` root. - pub block_root: Hash256, - pub header: BeaconBlockHeader, - pub body: Option, - /// The instant at which this record was created or last meaningfully modified. Used to - /// determine if an entry is stale and should be removed. - pub inserted: Instant, - /// The `PeerId` that last meaningfully contributed to this item. - pub sender: PeerId, -} - -impl PartialBeaconBlock { - /// Consumes `self` and returns a full built `BeaconBlock`, it's root and the `sender` - /// `PeerId`, if enough information exists to complete the block. Otherwise, returns `None`. - pub fn complete(self) -> Option<(Hash256, BeaconBlock, PeerId)> { - Some(( - self.block_root, - self.header.into_block(self.body?), - self.sender, - )) - } -} From 267477ffc248e35487358a7f8d57b2a0de23e3a2 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 24 Mar 2019 18:37:23 +1100 Subject: [PATCH 046/191] Fix typo from previous commit --- beacon_node/network/src/sync/simple_sync.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index c23a6ec569..4ee3490430 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -131,12 +131,7 @@ impl SimpleSync { /// Handle a `Goodbye` message from a peer. /// /// Removes the peer from `known_peers`. - pub fn on_goodbye( - &mut self, - peer_id: PeerId, - reason: GoodbyeReason, - ddnetwork: &mut NetworkContext, - ) { + pub fn on_goodbye(&mut self, peer_id: PeerId, reason: GoodbyeReason) { info!( self.log, "PeerGoodbye"; "peer" => format!("{:?}", peer_id), From 8f4a2fbde1cb53a90d98c17f7abbf3e363246d02 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 6 Mar 2019 14:46:12 +1100 Subject: [PATCH 047/191] Implement transaction pool basics --- Cargo.toml | 1 + eth2/operation_pool/Cargo.toml | 9 + eth2/operation_pool/src/lib.rs | 308 ++++++++++++++++++ .../src/per_block_processing.rs | 11 +- .../src/per_block_processing/errors.rs | 5 + .../per_block_processing/verify_deposit.rs | 6 +- .../src/per_block_processing/verify_exit.rs | 8 +- .../per_block_processing/verify_transfer.rs | 38 ++- eth2/types/Cargo.toml | 1 + eth2/types/src/transfer.rs | 5 +- 10 files changed, 377 insertions(+), 15 deletions(-) create mode 100644 eth2/operation_pool/Cargo.toml create mode 100644 eth2/operation_pool/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index cb070cc2da..00be99e320 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "eth2/attester", "eth2/block_proposer", "eth2/fork_choice", + "eth2/operation_pool", "eth2/state_processing", "eth2/types", "eth2/utils/bls", diff --git a/eth2/operation_pool/Cargo.toml b/eth2/operation_pool/Cargo.toml new file mode 100644 index 0000000000..e68d318b5e --- /dev/null +++ b/eth2/operation_pool/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "operation_pool" +version = "0.1.0" +authors = ["Michael Sproul "] +edition = "2018" + +[dependencies] +types = { path = "../types" } +state_processing = { path = "../../eth2/state_processing" } diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs new file mode 100644 index 0000000000..7c25250993 --- /dev/null +++ b/eth2/operation_pool/src/lib.rs @@ -0,0 +1,308 @@ +use std::collections::{btree_map::Entry, BTreeMap, HashSet}; + +use state_processing::per_block_processing::{ + verify_deposit_merkle_proof, verify_exit, verify_proposer_slashing, verify_transfer, + verify_transfer_partial, +}; +use types::{ + AttesterSlashing, BeaconState, ChainSpec, Deposit, ProposerSlashing, Transfer, VoluntaryExit, +}; + +#[cfg(test)] +const VERIFY_DEPOSIT_PROOFS: bool = false; +#[cfg(not(test))] +const VERIFY_DEPOSIT_PROOFS: bool = true; + +#[derive(Default)] +pub struct OperationPool { + /// Map from deposit index to deposit data. + // NOTE: We assume that there is only one deposit per index + // because the Eth1 data is updated (at most) once per epoch, + // and the spec doesn't seem to accomodate for re-orgs on a time-frame + // longer than an epoch + deposits: BTreeMap, + /// Map from attester index to slashing. + attester_slashings: BTreeMap, + /// Map from proposer index to slashing. + proposer_slashings: BTreeMap, + /// Map from exiting validator to their exit data. + voluntary_exits: BTreeMap, + /// Set of transfers. + transfers: HashSet, +} + +#[derive(Debug, PartialEq, Clone)] +pub enum DepositInsertStatus { + /// The deposit was not already in the pool. + Fresh, + /// The deposit already existed in the pool. + Duplicate, + /// The deposit conflicted with an existing deposit, which was replaced. + Replaced(Deposit), +} + +impl OperationPool { + /// Create a new operation pool. + pub fn new() -> Self { + Self::default() + } + + /// Add a deposit to the pool. + /// + /// No two distinct deposits should be added with the same index. + pub fn insert_deposit(&mut self, deposit: Deposit) -> DepositInsertStatus { + use DepositInsertStatus::*; + + match self.deposits.entry(deposit.index) { + Entry::Vacant(entry) => { + entry.insert(deposit); + Fresh + } + Entry::Occupied(mut entry) => { + if entry.get() == &deposit { + Duplicate + } else { + Replaced(entry.insert(deposit)) + } + } + } + } + + /// Get an ordered list of deposits for inclusion in a block. + /// + /// Take at most the maximum number of deposits, beginning from the current deposit index. + pub fn get_deposits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { + let start_idx = state.deposit_index; + (start_idx..start_idx + spec.max_deposits) + .map(|idx| self.deposits.get(&idx)) + .take_while(|deposit| { + // NOTE: we don't use verify_deposit, because it requires the + // deposit's index to match the state's, and we would like to return + // a batch with increasing indices + deposit.map_or(false, |deposit| { + !VERIFY_DEPOSIT_PROOFS || verify_deposit_merkle_proof(state, deposit, spec) + }) + }) + .flatten() + .cloned() + .collect() + } + + /// Remove all deposits with index less than the deposit index of the latest finalised block. + pub fn prune_deposits(&mut self, state: &BeaconState) -> BTreeMap { + let deposits_keep = self.deposits.split_off(&state.deposit_index); + std::mem::replace(&mut self.deposits, deposits_keep) + } + + /// Insert a proposer slashing into the pool. + pub fn insert_proposer_slashing( + &mut self, + slashing: ProposerSlashing, + state: &BeaconState, + spec: &ChainSpec, + ) -> Result<(), ()> { + // TODO: should maybe insert anyway if the proposer is unknown in the validator index, + // because they could *become* known later + // FIXME: error handling + verify_proposer_slashing(&slashing, state, spec).map_err(|_| ())?; + self.proposer_slashings + .insert(slashing.proposer_index, slashing); + Ok(()) + } + + /// Only check whether the implicated validator has already been slashed, because + /// all slashings in the pool were validated upon insertion. + // TODO: we need a mechanism to avoid including a proposer slashing and an attester + // slashing for the same validator in the same block + pub fn get_proposer_slashings( + &self, + state: &BeaconState, + spec: &ChainSpec, + ) -> Vec { + // We sort by validator index, which is safe, because a validator can only supply + // so many valid slashings for lower-indexed validators (and even that is unlikely) + filter_limit_operations( + self.proposer_slashings.values(), + |slashing| { + state + .validator_registry + .get(slashing.proposer_index as usize) + .map_or(false, |validator| !validator.slashed) + }, + spec.max_proposer_slashings, + ) + } + + /// Prune slashings for all slashed or withdrawn validators. + pub fn prune_proposer_slashings(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + let to_prune = self + .proposer_slashings + .keys() + .flat_map(|&validator_index| { + finalized_state + .validator_registry + .get(validator_index as usize) + .filter(|validator| { + validator.slashed + || validator.is_withdrawable_at(finalized_state.current_epoch(spec)) + }) + .map(|_| validator_index) + }) + .collect::>(); + + for validator_index in to_prune { + self.proposer_slashings.remove(&validator_index); + } + } + + // TODO: copy ProposerSlashing code for AttesterSlashing + + /// Insert a voluntary exit, validating it almost-entirely (future exits are permitted). + pub fn insert_voluntary_exit( + &mut self, + exit: VoluntaryExit, + state: &BeaconState, + spec: &ChainSpec, + ) -> Result<(), ()> { + verify_exit(state, &exit, spec, false).map_err(|_| ())?; + self.voluntary_exits.insert(exit.validator_index, exit); + Ok(()) + } + + /// Get a list of voluntary exits for inclusion in a block. + // TODO: could optimise this by eliding the checks that have already been done on insert + pub fn get_voluntary_exits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { + filter_limit_operations( + self.voluntary_exits.values(), + |exit| verify_exit(state, exit, spec, true).is_ok(), + spec.max_voluntary_exits, + ) + } + + /// Prune if validator has already exited at the last finalized state. + pub fn prune_voluntary_exits(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + let to_prune = self + .voluntary_exits + .keys() + .flat_map(|&validator_index| { + finalized_state + .validator_registry + .get(validator_index as usize) + .filter(|validator| validator.is_exited_at(finalized_state.current_epoch(spec))) + .map(|_| validator_index) + }) + .collect::>(); + + for validator_index in to_prune { + self.voluntary_exits.remove(&validator_index); + } + } + + /// Insert a transfer into the pool, checking it for validity in the process. + pub fn insert_transfer( + &mut self, + transfer: Transfer, + state: &BeaconState, + spec: &ChainSpec, + ) -> Result<(), ()> { + // The signature of the transfer isn't hashed, but because we check + // it before we insert into the HashSet, we can't end up with duplicate + // transactions. + verify_transfer_partial(state, &transfer, spec, true).map_err(|_| ())?; + self.transfers.insert(transfer); + Ok(()) + } + + /// Get a list of transfers for inclusion in a block. + // TODO: improve the economic optimality of this function by taking the transfer + // fees into account, and dependencies between transfers in the same block + // e.g. A pays B, B pays C + pub fn get_transfers(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { + filter_limit_operations( + &self.transfers, + |transfer| verify_transfer(state, transfer, spec).is_ok(), + spec.max_transfers, + ) + } + + /// Prune the set of transfers by removing all those whose slot has already passed. + pub fn prune_transfers(&mut self, finalized_state: &BeaconState) { + self.transfers = self + .transfers + .drain() + .filter(|transfer| transfer.slot > finalized_state.slot) + .collect(); + } +} + +/// Filter up to a maximum number of operations out of a slice. +fn filter_limit_operations<'a, T: 'a, I, F>(operations: I, filter: F, limit: u64) -> Vec +where + I: IntoIterator, + F: Fn(&T) -> bool, + T: Clone, +{ + operations + .into_iter() + .filter(|x| filter(*x)) + .take(limit as usize) + .cloned() + .collect() +} + +#[cfg(test)] +mod tests { + use super::DepositInsertStatus::*; + use super::*; + use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; + + #[test] + fn insert_deposit() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let mut op_pool = OperationPool::new(); + let deposit1 = Deposit::random_for_test(&mut rng); + let mut deposit2 = Deposit::random_for_test(&mut rng); + deposit2.index = deposit1.index; + + assert_eq!(op_pool.insert_deposit(deposit1.clone()), Fresh); + assert_eq!(op_pool.insert_deposit(deposit1.clone()), Duplicate); + assert_eq!(op_pool.insert_deposit(deposit2), Replaced(deposit1)); + } + + #[test] + fn get_deposits_max() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let mut op_pool = OperationPool::new(); + let spec = ChainSpec::foundation(); + let start = 10000; + let max_deposits = spec.max_deposits; + let extra = 5; + let offset = 1; + assert!(offset <= extra); + + let proto_deposit = Deposit::random_for_test(&mut rng); + let deposits = (start..start + max_deposits + extra) + .map(|index| { + let mut deposit = proto_deposit.clone(); + deposit.index = index; + deposit + }) + .collect::>(); + + for deposit in &deposits { + assert_eq!(op_pool.insert_deposit(deposit.clone()), Fresh); + } + + let mut state = BeaconState::random_for_test(&mut rng); + state.deposit_index = start + offset; + let deposits_for_block = op_pool.get_deposits(&state, &spec); + + assert_eq!(deposits_for_block.len() as u64, max_deposits); + assert_eq!( + deposits_for_block[..], + deposits[offset as usize..(offset + max_deposits) as usize] + ); + } + + // TODO: more tests +} diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index dc83abb3f3..e0e3595529 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -1,4 +1,3 @@ -use self::verify_proposer_slashing::verify_proposer_slashing; use crate::common::slash_validator; use errors::{BlockInvalid as Invalid, BlockProcessingError as Error, IntoWithIndex}; use rayon::prelude::*; @@ -8,11 +7,15 @@ use types::*; pub use self::verify_attester_slashing::{ gather_attester_slashing_indices, verify_attester_slashing, }; +pub use self::verify_proposer_slashing::verify_proposer_slashing; pub use validate_attestation::{validate_attestation, validate_attestation_without_signature}; -pub use verify_deposit::{get_existing_validator_index, verify_deposit, verify_deposit_index}; +pub use verify_deposit::{ + get_existing_validator_index, verify_deposit, verify_deposit_index, + verify_deposit_merkle_proof, +}; pub use verify_exit::verify_exit; pub use verify_slashable_attestation::verify_slashable_attestation; -pub use verify_transfer::{execute_transfer, verify_transfer}; +pub use verify_transfer::{execute_transfer, verify_transfer, verify_transfer_partial}; pub mod errors; mod validate_attestation; @@ -426,7 +429,7 @@ pub fn process_exits( .par_iter() .enumerate() .try_for_each(|(i, exit)| { - verify_exit(&state, exit, spec).map_err(|e| e.into_with_index(i)) + verify_exit(&state, exit, spec, true).map_err(|e| e.into_with_index(i)) })?; // Update the state in series. diff --git a/eth2/state_processing/src/per_block_processing/errors.rs b/eth2/state_processing/src/per_block_processing/errors.rs index c0fe252de4..6614f6f608 100644 --- a/eth2/state_processing/src/per_block_processing/errors.rs +++ b/eth2/state_processing/src/per_block_processing/errors.rs @@ -390,6 +390,11 @@ pub enum TransferInvalid { /// /// (state_slot, transfer_slot) StateSlotMismatch(Slot, Slot), + /// The `transfer.slot` is in the past relative to the state slot. + /// + /// + /// (state_slot, transfer_slot) + TransferSlotInPast(Slot, Slot), /// The `transfer.from` validator has been activated and is not withdrawable. /// /// (from_validator) diff --git a/eth2/state_processing/src/per_block_processing/verify_deposit.rs b/eth2/state_processing/src/per_block_processing/verify_deposit.rs index a3a0f5734c..1b974d972a 100644 --- a/eth2/state_processing/src/per_block_processing/verify_deposit.rs +++ b/eth2/state_processing/src/per_block_processing/verify_deposit.rs @@ -89,7 +89,11 @@ pub fn get_existing_validator_index( /// Verify that a deposit is included in the state's eth1 deposit root. /// /// Spec v0.5.0 -fn verify_deposit_merkle_proof(state: &BeaconState, deposit: &Deposit, spec: &ChainSpec) -> bool { +pub fn verify_deposit_merkle_proof( + state: &BeaconState, + deposit: &Deposit, + spec: &ChainSpec, +) -> bool { let leaf = hash(&get_serialized_deposit_data(deposit)); verify_merkle_proof( Hash256::from_slice(&leaf), diff --git a/eth2/state_processing/src/per_block_processing/verify_exit.rs b/eth2/state_processing/src/per_block_processing/verify_exit.rs index 7893cea966..14dad3442c 100644 --- a/eth2/state_processing/src/per_block_processing/verify_exit.rs +++ b/eth2/state_processing/src/per_block_processing/verify_exit.rs @@ -7,11 +7,17 @@ use types::*; /// /// Returns `Ok(())` if the `Exit` is valid, otherwise indicates the reason for invalidity. /// +/// The `check_future_epoch` argument determines whether the exit's epoch should be checked +/// against the state's current epoch to ensure it doesn't occur in the future. +/// It should ordinarily be set to true, except for operations stored for +/// some time (such as in the OperationPool). +/// /// Spec v0.5.0 pub fn verify_exit( state: &BeaconState, exit: &VoluntaryExit, spec: &ChainSpec, + check_future_epoch: bool, ) -> Result<(), Error> { let validator = state .validator_registry @@ -32,7 +38,7 @@ pub fn verify_exit( // Exits must specify an epoch when they become valid; they are not valid before then. verify!( - state.current_epoch(spec) >= exit.epoch, + !check_future_epoch || state.current_epoch(spec) >= exit.epoch, Invalid::FutureEpoch { state: state.current_epoch(spec), exit: exit.epoch diff --git a/eth2/state_processing/src/per_block_processing/verify_transfer.rs b/eth2/state_processing/src/per_block_processing/verify_transfer.rs index f873cd8503..4f3815797c 100644 --- a/eth2/state_processing/src/per_block_processing/verify_transfer.rs +++ b/eth2/state_processing/src/per_block_processing/verify_transfer.rs @@ -15,6 +15,19 @@ pub fn verify_transfer( state: &BeaconState, transfer: &Transfer, spec: &ChainSpec, +) -> Result<(), Error> { + verify_transfer_partial(state, transfer, spec, false) +} + +/// Parametric version of `verify_transfer` that allows some checks to be skipped. +/// +/// In everywhere except the operation pool, `verify_transfer` should be preferred over this +/// function. +pub fn verify_transfer_partial( + state: &BeaconState, + transfer: &Transfer, + spec: &ChainSpec, + for_op_pool: bool, ) -> Result<(), Error> { let sender_balance = *state .validator_balances @@ -27,17 +40,18 @@ pub fn verify_transfer( .ok_or_else(|| Error::Invalid(Invalid::FeeOverflow(transfer.amount, transfer.fee)))?; verify!( - sender_balance >= transfer.amount, + for_op_pool || sender_balance >= transfer.amount, Invalid::FromBalanceInsufficient(transfer.amount, sender_balance) ); verify!( - sender_balance >= transfer.fee, + for_op_pool || sender_balance >= transfer.fee, Invalid::FromBalanceInsufficient(transfer.fee, sender_balance) ); verify!( - (sender_balance == total_amount) + for_op_pool + || (sender_balance == total_amount) || (sender_balance >= (total_amount + spec.min_deposit_amount)), Invalid::InvalidResultingFromBalance( sender_balance - total_amount, @@ -45,10 +59,17 @@ pub fn verify_transfer( ) ); - verify!( - state.slot == transfer.slot, - Invalid::StateSlotMismatch(state.slot, transfer.slot) - ); + if for_op_pool { + verify!( + state.slot <= transfer.slot, + Invalid::TransferSlotInPast(state.slot, transfer.slot) + ); + } else { + verify!( + state.slot == transfer.slot, + Invalid::StateSlotMismatch(state.slot, transfer.slot) + ); + } let sender_validator = state .validator_registry @@ -57,7 +78,8 @@ pub fn verify_transfer( let epoch = state.slot.epoch(spec.slots_per_epoch); verify!( - sender_validator.is_withdrawable_at(epoch) + for_op_pool + || sender_validator.is_withdrawable_at(epoch) || sender_validator.activation_epoch == spec.far_future_epoch, Invalid::FromValidatorIneligableForTransfer(transfer.sender) ); diff --git a/eth2/types/Cargo.toml b/eth2/types/Cargo.toml index e4ccfd63eb..8554b5c543 100644 --- a/eth2/types/Cargo.toml +++ b/eth2/types/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" bls = { path = "../utils/bls" } boolean-bitfield = { path = "../utils/boolean-bitfield" } dirs = "1.0" +derivative = "1.0" ethereum-types = "0.5" hashing = { path = "../utils/hashing" } hex = "0.3" diff --git a/eth2/types/src/transfer.rs b/eth2/types/src/transfer.rs index 2570d7b3f9..4b10ce1cad 100644 --- a/eth2/types/src/transfer.rs +++ b/eth2/types/src/transfer.rs @@ -1,6 +1,7 @@ use super::Slot; use crate::test_utils::TestRandom; use bls::{PublicKey, Signature}; +use derivative::Derivative; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz::TreeHash; @@ -12,7 +13,6 @@ use test_random_derive::TestRandom; /// Spec v0.5.0 #[derive( Debug, - PartialEq, Clone, Serialize, Deserialize, @@ -21,7 +21,9 @@ use test_random_derive::TestRandom; TreeHash, TestRandom, SignedRoot, + Derivative, )] +#[derivative(PartialEq, Eq, Hash)] pub struct Transfer { pub sender: u64, pub recipient: u64, @@ -29,6 +31,7 @@ pub struct Transfer { pub fee: u64, pub slot: Slot, pub pubkey: PublicKey, + #[derivative(Hash = "ignore")] pub signature: Signature, } From c2e5d3c45a5adf40c51206e7b04f1199986823ca Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 19 Mar 2019 10:22:14 +1100 Subject: [PATCH 048/191] BLS: fix description of AggregatePublicKey --- eth2/utils/bls/src/aggregate_public_key.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth2/utils/bls/src/aggregate_public_key.rs b/eth2/utils/bls/src/aggregate_public_key.rs index 2174a43cb0..2e6ee7882b 100644 --- a/eth2/utils/bls/src/aggregate_public_key.rs +++ b/eth2/utils/bls/src/aggregate_public_key.rs @@ -1,7 +1,7 @@ use super::PublicKey; use bls_aggregates::AggregatePublicKey as RawAggregatePublicKey; -/// A single BLS signature. +/// A BLS aggregate public key. /// /// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ /// serialization). @@ -17,7 +17,7 @@ impl AggregatePublicKey { self.0.add(public_key.as_raw()) } - /// Returns the underlying signature. + /// Returns the underlying public key. pub fn as_raw(&self) -> &RawAggregatePublicKey { &self.0 } From 1fca8a063c0118e84c82b69a8d800d109c65861b Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 19 Mar 2019 19:15:33 +1100 Subject: [PATCH 049/191] Operation pool: add attestation support --- eth2/operation_pool/Cargo.toml | 5 +- eth2/operation_pool/src/lib.rs | 128 +++++++++++++++++++++++++++++++-- eth2/types/src/attestation.rs | 19 +++++ 3 files changed, 146 insertions(+), 6 deletions(-) diff --git a/eth2/operation_pool/Cargo.toml b/eth2/operation_pool/Cargo.toml index e68d318b5e..07cb618649 100644 --- a/eth2/operation_pool/Cargo.toml +++ b/eth2/operation_pool/Cargo.toml @@ -5,5 +5,8 @@ authors = ["Michael Sproul "] edition = "2018" [dependencies] +int_to_bytes = { path = "../utils/int_to_bytes" } +itertools = "0.8" types = { path = "../types" } -state_processing = { path = "../../eth2/state_processing" } +state_processing = { path = "../state_processing" } +ssz = { path = "../utils/ssz" } diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 7c25250993..7a647450ca 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -1,11 +1,15 @@ -use std::collections::{btree_map::Entry, BTreeMap, HashSet}; - +use int_to_bytes::int_to_bytes8; +use itertools::Itertools; +use ssz::ssz_encode; use state_processing::per_block_processing::{ - verify_deposit_merkle_proof, verify_exit, verify_proposer_slashing, verify_transfer, - verify_transfer_partial, + validate_attestation, verify_deposit_merkle_proof, verify_exit, verify_proposer_slashing, + verify_transfer, verify_transfer_partial, }; +use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet}; +use types::chain_spec::Domain; use types::{ - AttesterSlashing, BeaconState, ChainSpec, Deposit, ProposerSlashing, Transfer, VoluntaryExit, + Attestation, AttestationData, AttesterSlashing, BeaconState, ChainSpec, Deposit, Epoch, + ProposerSlashing, Transfer, VoluntaryExit, }; #[cfg(test)] @@ -15,6 +19,8 @@ const VERIFY_DEPOSIT_PROOFS: bool = true; #[derive(Default)] pub struct OperationPool { + /// Map from attestation ID (see below) to vectors of attestations. + attestations: HashMap>, /// Map from deposit index to deposit data. // NOTE: We assume that there is only one deposit per index // because the Eth1 data is updated (at most) once per epoch, @@ -31,6 +37,54 @@ pub struct OperationPool { transfers: HashSet, } +/// Serialized `AttestationData` augmented with a domain to encode the fork info. +#[derive(PartialEq, Eq, Clone, Hash, Debug)] +struct AttestationId(Vec); + +/// Number of domain bytes that the end of an attestation ID is padded with. +const DOMAIN_BYTES_LEN: usize = 8; + +impl AttestationId { + fn from_data(attestation: &AttestationData, state: &BeaconState, spec: &ChainSpec) -> Self { + let mut bytes = ssz_encode(attestation); + let epoch = attestation.slot.epoch(spec.slots_per_epoch); + bytes.extend_from_slice(&AttestationId::compute_domain_bytes(epoch, state, spec)); + AttestationId(bytes) + } + + fn compute_domain_bytes(epoch: Epoch, state: &BeaconState, spec: &ChainSpec) -> Vec { + int_to_bytes8(spec.get_domain(epoch, Domain::Attestation, &state.fork)) + } + + fn domain_bytes_match(&self, domain_bytes: &[u8]) -> bool { + &self.0[self.0.len() - DOMAIN_BYTES_LEN..] == domain_bytes + } +} + +/// Compute a fitness score for an attestation. +/// +/// The score is calculated by determining the number of *new* attestations that +/// the aggregate attestation introduces, and is proportional to the size of the reward we will +/// receive for including it in a block. +// TODO: this could be optimised with a map from validator index to whether that validator has +// attested in the *current* epoch. Alternatively, we could cache an index that allows us to +// quickly look up the attestations in the current epoch for a given shard. +fn attestation_score(attestation: &Attestation, state: &BeaconState) -> usize { + // Bitfield of validators whose attestations are new/fresh. + let mut new_validators = attestation.aggregation_bitfield.clone(); + + state + .current_epoch_attestations + .iter() + .filter(|current_attestation| current_attestation.data.shard == attestation.data.shard) + .for_each(|current_attestation| { + // Remove the validators who have signed the existing attestation (they are not new) + new_validators.difference_inplace(¤t_attestation.aggregation_bitfield); + }); + + new_validators.num_set_bits() +} + #[derive(Debug, PartialEq, Clone)] pub enum DepositInsertStatus { /// The deposit was not already in the pool. @@ -47,6 +101,70 @@ impl OperationPool { Self::default() } + /// Insert an attestation into the pool, aggregating it with existing attestations if possible. + pub fn insert_attestation( + &mut self, + attestation: Attestation, + state: &BeaconState, + spec: &ChainSpec, + ) -> Result<(), ()> { + // Check that attestation signatures are valid. + // FIXME: should disable the time-dependent checks. + validate_attestation(state, &attestation, spec).map_err(|_| ())?; + + let id = AttestationId::from_data(&attestation.data, state, spec); + + let existing_attestations = match self.attestations.entry(id) { + hash_map::Entry::Vacant(entry) => { + entry.insert(vec![attestation]); + return Ok(()); + } + hash_map::Entry::Occupied(entry) => entry.into_mut(), + }; + + let mut aggregated = false; + for existing_attestation in existing_attestations.iter_mut() { + if existing_attestation.signers_disjoint_from(&attestation) { + existing_attestation.aggregate(&attestation); + aggregated = true; + } else if *existing_attestation == attestation { + aggregated = true; + } + } + + if !aggregated { + existing_attestations.push(attestation); + } + + Ok(()) + } + + /// Get a list of attestations for inclusion in a block. + pub fn get_attestations(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { + // Attestations for the current fork... + // TODO: should we also check domain bytes for the previous epoch? + let current_epoch = state.slot.epoch(spec.slots_per_epoch); + let domain_bytes = AttestationId::compute_domain_bytes(current_epoch, state, spec); + self.attestations + .iter() + .filter(|(key, _)| key.domain_bytes_match(&domain_bytes)) + .flat_map(|(_, attestations)| attestations) + // That are valid... + .filter(|attestation| validate_attestation(state, attestation, spec).is_ok()) + // Scored by the number of new attestations they introduce (descending) + .map(|att| (att, attestation_score(att, state))) + .sorted_by_key(|&(_, score)| std::cmp::Reverse(score)) + // Limited to the maximum number of attestations per block + .take(spec.max_attestations as usize) + .map(|(att, _)| att) + .cloned() + .collect() + } + + pub fn prune_attestations(&self, _finalized_state: &BeaconState, _spec: &ChainSpec) { + // TODO + } + /// Add a deposit to the pool. /// /// No two distinct deposits should be added with the same index. diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 0b660466e1..6c572c8528 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -28,6 +28,25 @@ pub struct Attestation { pub aggregate_signature: AggregateSignature, } +impl Attestation { + /// Are the aggregation bitfields of these attestations disjoint? + pub fn signers_disjoint_from(&self, other: &Attestation) -> bool { + self.aggregation_bitfield.intersection(&other.aggregation_bitfield).is_zero() + } + + /// Aggregate another Attestation into this one. + /// + /// The aggregation bitfields must be disjoint, and the data must be the same. + pub fn aggregate(&mut self, other: &Attestation) { + debug_assert_eq!(self.data, other.data); + debug_assert!(self.signers_disjoint_from(other)); + + self.aggregation_bitfield.union_inplace(&other.aggregation_bitfield); + self.custody_bitfield.union_inplace(&other.custody_bitfield); + // FIXME: signature aggregation once our BLS library wraps it + } +} + #[cfg(test)] mod tests { use super::*; From 18a7bd243c1d5fe865ef954d3fc799f6d5b9cab7 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 19 Mar 2019 19:18:33 +1100 Subject: [PATCH 050/191] Bitfield: implement union/intersection/difference --- eth2/utils/boolean-bitfield/src/lib.rs | 92 ++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index 443cd06dad..e37f2488d6 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -89,6 +89,11 @@ impl BooleanBitfield { self.len() == 0 } + /// Returns true if all bits are set to 0. + pub fn is_zero(&self) -> bool { + self.0.none() + } + /// Returns the number of bytes required to represent this bitfield. pub fn num_bytes(&self) -> usize { self.to_bytes().len() @@ -104,6 +109,44 @@ impl BooleanBitfield { pub fn to_bytes(&self) -> Vec { self.0.to_bytes() } + + /// Compute the intersection (binary-and) of this bitfield with another. Lengths must match. + pub fn intersection(&self, other: &Self) -> Self { + let mut res = self.clone(); + res.intersection_inplace(other); + res + } + + /// Like `intersection` but in-place (updates `self`). + pub fn intersection_inplace(&mut self, other: &Self) { + self.0.intersect(&other.0); + } + + /// Compute the union (binary-or) of this bitfield with another. Lengths must match. + pub fn union(&self, other: &Self) -> Self { + let mut res = self.clone(); + res.union_inplace(other); + res + } + + /// Like `union` but in-place (updates `self`). + pub fn union_inplace(&mut self, other: &Self) { + self.0.union(&other.0); + } + + /// Compute the difference (binary-minus) of this bitfield with another. Lengths must match. + /// + /// Computes `self - other`. + pub fn difference(&self, other: &Self) -> Self { + let mut res = self.clone(); + res.difference_inplace(other); + res + } + + /// Like `difference` but in-place (updates `self`). + pub fn difference_inplace(&mut self, other: &Self) { + self.0.difference(&other.0); + } } impl default::Default for BooleanBitfield { @@ -427,4 +470,53 @@ mod tests { let c = BooleanBitfield::from_bytes(&vec![6, 8, 17][..]); assert_eq!(c, a & b); } + + #[test] + fn test_is_zero() { + let yes_data: &[&[u8]] = &[&[], &[0], &[0, 0], &[0, 0, 0]]; + for bytes in yes_data { + assert!(BooleanBitfield::from_bytes(bytes).is_zero()); + } + let no_data: &[&[u8]] = &[&[1], &[6], &[0, 1], &[0, 0, 1], &[0, 0, 255]]; + for bytes in no_data { + assert!(!BooleanBitfield::from_bytes(bytes).is_zero()); + } + } + + #[test] + fn test_intersection() { + let a = BooleanBitfield::from_bytes(&[0b1100, 0b0001]); + let b = BooleanBitfield::from_bytes(&[0b1011, 0b1001]); + let c = BooleanBitfield::from_bytes(&[0b1000, 0b0001]); + assert_eq!(a.intersection(&b), c); + assert_eq!(b.intersection(&a), c); + assert_eq!(a.intersection(&c), c); + assert_eq!(b.intersection(&c), c); + assert_eq!(a.intersection(&a), a); + assert_eq!(b.intersection(&b), b); + assert_eq!(c.intersection(&c), c); + } + + #[test] + fn test_union() { + let a = BooleanBitfield::from_bytes(&[0b1100, 0b0001]); + let b = BooleanBitfield::from_bytes(&[0b1011, 0b1001]); + let c = BooleanBitfield::from_bytes(&[0b1111, 0b1001]); + assert_eq!(a.union(&b), c); + assert_eq!(b.union(&a), c); + assert_eq!(a.union(&a), a); + assert_eq!(b.union(&b), b); + assert_eq!(c.union(&c), c); + } + + #[test] + fn test_difference() { + let a = BooleanBitfield::from_bytes(&[0b1100, 0b0001]); + let b = BooleanBitfield::from_bytes(&[0b1011, 0b1001]); + let a_b = BooleanBitfield::from_bytes(&[0b0100, 0b0000]); + let b_a = BooleanBitfield::from_bytes(&[0b0011, 0b1000]); + assert_eq!(a.difference(&b), a_b); + assert_eq!(b.difference(&a), b_a); + assert!(a.difference(&a).is_zero()); + } } From 8a7c51271ed06e75fc0f45894a54ab489b2561bc Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 19 Mar 2019 19:19:21 +1100 Subject: [PATCH 051/191] Bitfield: use BitOr instead of BitAnd for union Closes #314 --- eth2/utils/boolean-bitfield/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index e37f2488d6..cdd0bc3d77 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -168,10 +168,11 @@ impl cmp::PartialEq for BooleanBitfield { /// Create a new bitfield that is a union of two other bitfields. /// /// For example `union(0101, 1000) == 1101` -impl std::ops::BitAnd for BooleanBitfield { +// TODO: length-independent intersection for BitAnd +impl std::ops::BitOr for BooleanBitfield { type Output = Self; - fn bitand(self, other: Self) -> Self { + fn bitor(self, other: Self) -> Self { let (biggest, smallest) = if self.len() > other.len() { (&self, &other) } else { @@ -464,11 +465,11 @@ mod tests { } #[test] - fn test_bitand() { + fn test_bitor() { let a = BooleanBitfield::from_bytes(&vec![2, 8, 1][..]); let b = BooleanBitfield::from_bytes(&vec![4, 8, 16][..]); let c = BooleanBitfield::from_bytes(&vec![6, 8, 17][..]); - assert_eq!(c, a & b); + assert_eq!(c, a | b); } #[test] From a8224aa4ec05fe467abaf21d7b1a6f1a9239f584 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 10:14:31 +1100 Subject: [PATCH 052/191] Operation pool: add prune_all --- eth2/operation_pool/src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 7a647450ca..93ff74652e 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -351,6 +351,16 @@ impl OperationPool { .filter(|transfer| transfer.slot > finalized_state.slot) .collect(); } + + /// Prune all types of transactions given the latest finalized state. + pub fn prune_all(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + self.prune_attestations(finalized_state, spec); + self.prune_deposits(finalized_state); + self.prune_proposer_slashings(finalized_state, spec); + // FIXME: add attester slashings + self.prune_voluntary_exits(finalized_state, spec); + self.prune_transfers(finalized_state); + } } /// Filter up to a maximum number of operations out of a slice. From 9c2dfba843c2210df161537f186eb87cb3c18bda Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 10:47:19 +1100 Subject: [PATCH 053/191] Operation pool: prune attestations --- eth2/operation_pool/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 93ff74652e..4932a7e0a5 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -161,8 +161,18 @@ impl OperationPool { .collect() } - pub fn prune_attestations(&self, _finalized_state: &BeaconState, _spec: &ChainSpec) { - // TODO + /// Remove attestations which are too old to be included in a block. + // TODO: we could probably prune other attestations here: + // - ones that are completely covered by attestations included in the state + // - maybe ones invalidated by the confirmation of one fork over another + pub fn prune_attestations(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + self.attestations.retain(|_, attestations| { + // All the attestations in this bucket have the same data, so we only need to + // check the first one. + attestations.first().map_or(false, |att| { + finalized_state.slot < att.data.slot + spec.slots_per_epoch + }) + }); } /// Add a deposit to the pool. From 05dd936a97fbcc837e74ba35fc6e5972f7cadac2 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 12:44:37 +1100 Subject: [PATCH 054/191] Operation pool: deposit pruning tests --- eth2/operation_pool/src/lib.rs | 91 ++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 14 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 4932a7e0a5..938a3db0c5 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -1,6 +1,7 @@ use int_to_bytes::int_to_bytes8; use itertools::Itertools; use ssz::ssz_encode; +use state_processing::per_block_processing::errors::ProposerSlashingValidationError; use state_processing::per_block_processing::{ validate_attestation, verify_deposit_merkle_proof, verify_exit, verify_proposer_slashing, verify_transfer, verify_transfer_partial, @@ -92,7 +93,7 @@ pub enum DepositInsertStatus { /// The deposit already existed in the pool. Duplicate, /// The deposit conflicted with an existing deposit, which was replaced. - Replaced(Deposit), + Replaced(Box), } impl OperationPool { @@ -190,7 +191,7 @@ impl OperationPool { if entry.get() == &deposit { Duplicate } else { - Replaced(entry.insert(deposit)) + Replaced(Box::new(entry.insert(deposit))) } } } @@ -222,17 +223,21 @@ impl OperationPool { std::mem::replace(&mut self.deposits, deposits_keep) } + /// The number of deposits stored in the pool. + pub fn num_deposits(&self) -> usize { + self.deposits.len() + } + /// Insert a proposer slashing into the pool. pub fn insert_proposer_slashing( &mut self, slashing: ProposerSlashing, state: &BeaconState, spec: &ChainSpec, - ) -> Result<(), ()> { + ) -> Result<(), ProposerSlashingValidationError> { // TODO: should maybe insert anyway if the proposer is unknown in the validator index, // because they could *become* known later - // FIXME: error handling - verify_proposer_slashing(&slashing, state, spec).map_err(|_| ())?; + verify_proposer_slashing(&slashing, state, spec)?; self.proposer_slashings .insert(slashing.proposer_index, slashing); Ok(()) @@ -404,7 +409,22 @@ mod tests { assert_eq!(op_pool.insert_deposit(deposit1.clone()), Fresh); assert_eq!(op_pool.insert_deposit(deposit1.clone()), Duplicate); - assert_eq!(op_pool.insert_deposit(deposit2), Replaced(deposit1)); + assert_eq!( + op_pool.insert_deposit(deposit2), + Replaced(Box::new(deposit1)) + ); + } + + // Create `count` dummy deposits with sequential deposit IDs beginning from `start`. + fn dummy_deposits(rng: &mut XorShiftRng, start: u64, count: u64) -> Vec { + let proto_deposit = Deposit::random_for_test(rng); + (start..start + count) + .map(|index| { + let mut deposit = proto_deposit.clone(); + deposit.index = index; + deposit + }) + .collect() } #[test] @@ -418,14 +438,7 @@ mod tests { let offset = 1; assert!(offset <= extra); - let proto_deposit = Deposit::random_for_test(&mut rng); - let deposits = (start..start + max_deposits + extra) - .map(|index| { - let mut deposit = proto_deposit.clone(); - deposit.index = index; - deposit - }) - .collect::>(); + let deposits = dummy_deposits(&mut rng, start, max_deposits + extra); for deposit in &deposits { assert_eq!(op_pool.insert_deposit(deposit.clone()), Fresh); @@ -442,5 +455,55 @@ mod tests { ); } + #[test] + fn prune_deposits() { + let rng = &mut XorShiftRng::from_seed([42; 16]); + let mut op_pool = OperationPool::new(); + let spec = ChainSpec::foundation(); + + let start1 = 100; + let count = 100; + let gap = 25; + let start2 = start1 + count + gap; + + let deposits1 = dummy_deposits(rng, start1, count); + let deposits2 = dummy_deposits(rng, start2, count); + + for d in deposits1.into_iter().chain(deposits2) { + op_pool.insert_deposit(d); + } + + assert_eq!(op_pool.num_deposits(), 2 * count as usize); + + let mut state = BeaconState::random_for_test(rng); + state.deposit_index = start1; + + // Pruning the first bunch of deposits in batches of 5 should work. + let step = 5; + let mut pool_size = step + 2 * count as usize; + for i in (start1..=(start1 + count)).step_by(step) { + state.deposit_index = i; + op_pool.prune_deposits(&state); + pool_size -= step; + assert_eq!(op_pool.num_deposits(), pool_size); + } + assert_eq!(pool_size, count as usize); + // Pruning in the gap should do nothing. + for i in (start1 + count..start2).step_by(step) { + state.deposit_index = i; + op_pool.prune_deposits(&state); + assert_eq!(op_pool.num_deposits(), count as usize); + } + // Same again for the later deposits. + pool_size += step; + for i in (start2..=(start2 + count)).step_by(step) { + state.deposit_index = i; + op_pool.prune_deposits(&state); + pool_size -= step; + assert_eq!(op_pool.num_deposits(), pool_size); + } + assert_eq!(op_pool.num_deposits(), 0); + } + // TODO: more tests } From 03c01c8a8db8313e8c7de6c6edb41c69eaace4ee Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 13:06:06 +1100 Subject: [PATCH 055/191] Operation pool: HashMap instead of BTreeMap --- eth2/operation_pool/src/lib.rs | 75 +++++++++++++++++----------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 938a3db0c5..c625cdc832 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -10,7 +10,7 @@ use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet}; use types::chain_spec::Domain; use types::{ Attestation, AttestationData, AttesterSlashing, BeaconState, ChainSpec, Deposit, Epoch, - ProposerSlashing, Transfer, VoluntaryExit, + ProposerSlashing, Transfer, Validator, VoluntaryExit, }; #[cfg(test)] @@ -29,11 +29,11 @@ pub struct OperationPool { // longer than an epoch deposits: BTreeMap, /// Map from attester index to slashing. - attester_slashings: BTreeMap, + attester_slashings: HashMap, /// Map from proposer index to slashing. - proposer_slashings: BTreeMap, + proposer_slashings: HashMap, /// Map from exiting validator to their exit data. - voluntary_exits: BTreeMap, + voluntary_exits: HashMap, /// Set of transfers. transfers: HashSet, } @@ -268,24 +268,14 @@ impl OperationPool { /// Prune slashings for all slashed or withdrawn validators. pub fn prune_proposer_slashings(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { - let to_prune = self - .proposer_slashings - .keys() - .flat_map(|&validator_index| { - finalized_state - .validator_registry - .get(validator_index as usize) - .filter(|validator| { - validator.slashed - || validator.is_withdrawable_at(finalized_state.current_epoch(spec)) - }) - .map(|_| validator_index) - }) - .collect::>(); - - for validator_index in to_prune { - self.proposer_slashings.remove(&validator_index); - } + prune_validator_hash_map( + &mut self.proposer_slashings, + |validator| { + validator.slashed + || validator.is_withdrawable_at(finalized_state.current_epoch(spec)) + }, + finalized_state, + ); } // TODO: copy ProposerSlashing code for AttesterSlashing @@ -314,21 +304,11 @@ impl OperationPool { /// Prune if validator has already exited at the last finalized state. pub fn prune_voluntary_exits(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { - let to_prune = self - .voluntary_exits - .keys() - .flat_map(|&validator_index| { - finalized_state - .validator_registry - .get(validator_index as usize) - .filter(|validator| validator.is_exited_at(finalized_state.current_epoch(spec))) - .map(|_| validator_index) - }) - .collect::>(); - - for validator_index in to_prune { - self.voluntary_exits.remove(&validator_index); - } + prune_validator_hash_map( + &mut self.voluntary_exits, + |validator| validator.is_exited_at(finalized_state.current_epoch(spec)), + finalized_state, + ); } /// Insert a transfer into the pool, checking it for validity in the process. @@ -393,6 +373,26 @@ where .collect() } +/// Remove all entries from the given hash map for which `prune_if` returns true. +/// +/// The keys in the map should be validator indices, which will be looked up +/// in the state's validator registry and then passed to `prune_if`. +/// Entries for unknown validators will be kept. +fn prune_validator_hash_map( + map: &mut HashMap, + prune_if: F, + finalized_state: &BeaconState, +) where + F: Fn(&Validator) -> bool, +{ + map.retain(|&validator_index, _| { + finalized_state + .validator_registry + .get(validator_index as usize) + .map_or(true, |validator| !prune_if(validator)) + }); +} + #[cfg(test)] mod tests { use super::DepositInsertStatus::*; @@ -459,7 +459,6 @@ mod tests { fn prune_deposits() { let rng = &mut XorShiftRng::from_seed([42; 16]); let mut op_pool = OperationPool::new(); - let spec = ChainSpec::foundation(); let start1 = 100; let count = 100; From b2fe14e12c23304241a6e57252ced39920f45c5c Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 15:57:41 +1100 Subject: [PATCH 056/191] Operation pool: refactor verify_deposit/exit --- eth2/operation_pool/src/lib.rs | 134 ++++++++++++------ .../src/per_block_processing.rs | 5 +- .../per_block_processing/verify_deposit.rs | 6 +- .../src/per_block_processing/verify_exit.rs | 27 +++- eth2/types/src/attestation.rs | 7 +- 5 files changed, 118 insertions(+), 61 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index c625cdc832..ecf4e41e7b 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -3,8 +3,8 @@ use itertools::Itertools; use ssz::ssz_encode; use state_processing::per_block_processing::errors::ProposerSlashingValidationError; use state_processing::per_block_processing::{ - validate_attestation, verify_deposit_merkle_proof, verify_exit, verify_proposer_slashing, - verify_transfer, verify_transfer_partial, + validate_attestation, verify_deposit, verify_exit, verify_exit_time_independent_only, + verify_proposer_slashing, verify_transfer, verify_transfer_partial, }; use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet}; use types::chain_spec::Domain; @@ -179,19 +179,27 @@ impl OperationPool { /// Add a deposit to the pool. /// /// No two distinct deposits should be added with the same index. - pub fn insert_deposit(&mut self, deposit: Deposit) -> DepositInsertStatus { + pub fn insert_deposit( + &mut self, + deposit: Deposit, + state: &BeaconState, + spec: &ChainSpec, + ) -> Result { use DepositInsertStatus::*; match self.deposits.entry(deposit.index) { Entry::Vacant(entry) => { + // FIXME: error prop + verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec).map_err(|_| ())?; entry.insert(deposit); - Fresh + Ok(Fresh) } Entry::Occupied(mut entry) => { if entry.get() == &deposit { - Duplicate + Ok(Duplicate) } else { - Replaced(Box::new(entry.insert(deposit))) + verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec).map_err(|_| ())?; + Ok(Replaced(Box::new(entry.insert(deposit)))) } } } @@ -204,14 +212,7 @@ impl OperationPool { let start_idx = state.deposit_index; (start_idx..start_idx + spec.max_deposits) .map(|idx| self.deposits.get(&idx)) - .take_while(|deposit| { - // NOTE: we don't use verify_deposit, because it requires the - // deposit's index to match the state's, and we would like to return - // a batch with increasing indices - deposit.map_or(false, |deposit| { - !VERIFY_DEPOSIT_PROOFS || verify_deposit_merkle_proof(state, deposit, spec) - }) - }) + .take_while(Option::is_some) .flatten() .cloned() .collect() @@ -287,7 +288,7 @@ impl OperationPool { state: &BeaconState, spec: &ChainSpec, ) -> Result<(), ()> { - verify_exit(state, &exit, spec, false).map_err(|_| ())?; + verify_exit_time_independent_only(state, &exit, spec).map_err(|_| ())?; self.voluntary_exits.insert(exit.validator_index, exit); Ok(()) } @@ -297,7 +298,7 @@ impl OperationPool { pub fn get_voluntary_exits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { filter_limit_operations( self.voluntary_exits.values(), - |exit| verify_exit(state, exit, spec, true).is_ok(), + |exit| verify_exit(state, exit, spec).is_ok(), spec.max_voluntary_exits, ) } @@ -398,53 +399,51 @@ mod tests { use super::DepositInsertStatus::*; use super::*; use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; + use types::*; #[test] fn insert_deposit() { - let mut rng = XorShiftRng::from_seed([42; 16]); + let rng = &mut XorShiftRng::from_seed([42; 16]); + let (ref spec, ref state) = test_state(rng); let mut op_pool = OperationPool::new(); - let deposit1 = Deposit::random_for_test(&mut rng); - let mut deposit2 = Deposit::random_for_test(&mut rng); + let deposit1 = make_deposit(rng, state, spec); + let mut deposit2 = make_deposit(rng, state, spec); deposit2.index = deposit1.index; - assert_eq!(op_pool.insert_deposit(deposit1.clone()), Fresh); - assert_eq!(op_pool.insert_deposit(deposit1.clone()), Duplicate); assert_eq!( - op_pool.insert_deposit(deposit2), - Replaced(Box::new(deposit1)) + op_pool.insert_deposit(deposit1.clone(), state, spec), + Ok(Fresh) + ); + assert_eq!( + op_pool.insert_deposit(deposit1.clone(), state, spec), + Ok(Duplicate) + ); + assert_eq!( + op_pool.insert_deposit(deposit2, state, spec), + Ok(Replaced(Box::new(deposit1))) ); - } - - // Create `count` dummy deposits with sequential deposit IDs beginning from `start`. - fn dummy_deposits(rng: &mut XorShiftRng, start: u64, count: u64) -> Vec { - let proto_deposit = Deposit::random_for_test(rng); - (start..start + count) - .map(|index| { - let mut deposit = proto_deposit.clone(); - deposit.index = index; - deposit - }) - .collect() } #[test] fn get_deposits_max() { - let mut rng = XorShiftRng::from_seed([42; 16]); + let rng = &mut XorShiftRng::from_seed([42; 16]); + let (spec, mut state) = test_state(rng); let mut op_pool = OperationPool::new(); - let spec = ChainSpec::foundation(); let start = 10000; let max_deposits = spec.max_deposits; let extra = 5; let offset = 1; assert!(offset <= extra); - let deposits = dummy_deposits(&mut rng, start, max_deposits + extra); + let deposits = dummy_deposits(rng, &state, &spec, start, max_deposits + extra); for deposit in &deposits { - assert_eq!(op_pool.insert_deposit(deposit.clone()), Fresh); + assert_eq!( + op_pool.insert_deposit(deposit.clone(), &state, &spec), + Ok(Fresh) + ); } - let mut state = BeaconState::random_for_test(&mut rng); state.deposit_index = start + offset; let deposits_for_block = op_pool.get_deposits(&state, &spec); @@ -458,18 +457,20 @@ mod tests { #[test] fn prune_deposits() { let rng = &mut XorShiftRng::from_seed([42; 16]); + let (spec, state) = test_state(rng); let mut op_pool = OperationPool::new(); let start1 = 100; - let count = 100; + // test is super slow in debug mode if this parameter is too high + let count = 5; let gap = 25; let start2 = start1 + count + gap; - let deposits1 = dummy_deposits(rng, start1, count); - let deposits2 = dummy_deposits(rng, start2, count); + let deposits1 = dummy_deposits(rng, &state, &spec, start1, count); + let deposits2 = dummy_deposits(rng, &state, &spec, start2, count); for d in deposits1.into_iter().chain(deposits2) { - op_pool.insert_deposit(d); + assert!(op_pool.insert_deposit(d, &state, &spec).is_ok()); } assert_eq!(op_pool.num_deposits(), 2 * count as usize); @@ -504,5 +505,50 @@ mod tests { assert_eq!(op_pool.num_deposits(), 0); } + // Create a random deposit (with a valid proof of posession) + fn make_deposit(rng: &mut XorShiftRng, state: &BeaconState, spec: &ChainSpec) -> Deposit { + let keypair = Keypair::random(); + let mut deposit = Deposit::random_for_test(rng); + let mut deposit_input = DepositInput { + pubkey: keypair.pk.clone(), + withdrawal_credentials: Hash256::zero(), + proof_of_possession: Signature::empty_signature(), + }; + deposit_input.proof_of_possession = deposit_input.create_proof_of_possession( + &keypair.sk, + state.slot.epoch(spec.slots_per_epoch), + &state.fork, + spec, + ); + deposit.deposit_data.deposit_input = deposit_input; + deposit + } + + // Create `count` dummy deposits with sequential deposit IDs beginning from `start`. + fn dummy_deposits( + rng: &mut XorShiftRng, + state: &BeaconState, + spec: &ChainSpec, + start: u64, + count: u64, + ) -> Vec { + let proto_deposit = make_deposit(rng, state, spec); + (start..start + count) + .map(|index| { + let mut deposit = proto_deposit.clone(); + deposit.index = index; + deposit + }) + .collect() + } + + fn test_state(rng: &mut XorShiftRng) -> (ChainSpec, BeaconState) { + let spec = ChainSpec::foundation(); + let mut state = BeaconState::random_for_test(rng); + state.fork = Fork::genesis(&spec); + + (spec, state) + } + // TODO: more tests } diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index e0e3595529..617da00d4d 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -11,9 +11,8 @@ pub use self::verify_proposer_slashing::verify_proposer_slashing; pub use validate_attestation::{validate_attestation, validate_attestation_without_signature}; pub use verify_deposit::{ get_existing_validator_index, verify_deposit, verify_deposit_index, - verify_deposit_merkle_proof, }; -pub use verify_exit::verify_exit; +pub use verify_exit::{verify_exit, verify_exit_time_independent_only}; pub use verify_slashable_attestation::verify_slashable_attestation; pub use verify_transfer::{execute_transfer, verify_transfer, verify_transfer_partial}; @@ -429,7 +428,7 @@ pub fn process_exits( .par_iter() .enumerate() .try_for_each(|(i, exit)| { - verify_exit(&state, exit, spec, true).map_err(|e| e.into_with_index(i)) + verify_exit(&state, exit, spec).map_err(|e| e.into_with_index(i)) })?; // Update the state in series. diff --git a/eth2/state_processing/src/per_block_processing/verify_deposit.rs b/eth2/state_processing/src/per_block_processing/verify_deposit.rs index 1b974d972a..a3a0f5734c 100644 --- a/eth2/state_processing/src/per_block_processing/verify_deposit.rs +++ b/eth2/state_processing/src/per_block_processing/verify_deposit.rs @@ -89,11 +89,7 @@ pub fn get_existing_validator_index( /// Verify that a deposit is included in the state's eth1 deposit root. /// /// Spec v0.5.0 -pub fn verify_deposit_merkle_proof( - state: &BeaconState, - deposit: &Deposit, - spec: &ChainSpec, -) -> bool { +fn verify_deposit_merkle_proof(state: &BeaconState, deposit: &Deposit, spec: &ChainSpec) -> bool { let leaf = hash(&get_serialized_deposit_data(deposit)); verify_merkle_proof( Hash256::from_slice(&leaf), diff --git a/eth2/state_processing/src/per_block_processing/verify_exit.rs b/eth2/state_processing/src/per_block_processing/verify_exit.rs index 14dad3442c..a3b6943950 100644 --- a/eth2/state_processing/src/per_block_processing/verify_exit.rs +++ b/eth2/state_processing/src/per_block_processing/verify_exit.rs @@ -7,17 +7,30 @@ use types::*; /// /// Returns `Ok(())` if the `Exit` is valid, otherwise indicates the reason for invalidity. /// -/// The `check_future_epoch` argument determines whether the exit's epoch should be checked -/// against the state's current epoch to ensure it doesn't occur in the future. -/// It should ordinarily be set to true, except for operations stored for -/// some time (such as in the OperationPool). -/// /// Spec v0.5.0 pub fn verify_exit( state: &BeaconState, exit: &VoluntaryExit, spec: &ChainSpec, - check_future_epoch: bool, +) -> Result<(), Error> { + verify_exit_parametric(state, exit, spec, false) +} + +/// Like `verify_exit` but doesn't run checks which may become true in future states. +pub fn verify_exit_time_independent_only( + state: &BeaconState, + exit: &VoluntaryExit, + spec: &ChainSpec, +) -> Result<(), Error> { + verify_exit_parametric(state, exit, spec, true) +} + +/// Parametric version of `verify_exit` that skips some checks if `time_independent_only` is true. +fn verify_exit_parametric( + state: &BeaconState, + exit: &VoluntaryExit, + spec: &ChainSpec, + time_independent_only: bool, ) -> Result<(), Error> { let validator = state .validator_registry @@ -38,7 +51,7 @@ pub fn verify_exit( // Exits must specify an epoch when they become valid; they are not valid before then. verify!( - !check_future_epoch || state.current_epoch(spec) >= exit.epoch, + time_independent_only || state.current_epoch(spec) >= exit.epoch, Invalid::FutureEpoch { state: state.current_epoch(spec), exit: exit.epoch diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 6c572c8528..043711015a 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -31,7 +31,9 @@ pub struct Attestation { impl Attestation { /// Are the aggregation bitfields of these attestations disjoint? pub fn signers_disjoint_from(&self, other: &Attestation) -> bool { - self.aggregation_bitfield.intersection(&other.aggregation_bitfield).is_zero() + self.aggregation_bitfield + .intersection(&other.aggregation_bitfield) + .is_zero() } /// Aggregate another Attestation into this one. @@ -41,7 +43,8 @@ impl Attestation { debug_assert_eq!(self.data, other.data); debug_assert!(self.signers_disjoint_from(other)); - self.aggregation_bitfield.union_inplace(&other.aggregation_bitfield); + self.aggregation_bitfield + .union_inplace(&other.aggregation_bitfield); self.custody_bitfield.union_inplace(&other.custody_bitfield); // FIXME: signature aggregation once our BLS library wraps it } From 95ed40222827c5aba0259ce4a128ab51af54631b Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 16:13:06 +1100 Subject: [PATCH 057/191] op-pool: rename to verify_*_time_independent_only --- eth2/operation_pool/src/lib.rs | 4 +-- .../src/per_block_processing.rs | 8 ++--- .../per_block_processing/verify_transfer.rs | 30 +++++++++++-------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index ecf4e41e7b..ff5a7416b1 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -4,7 +4,7 @@ use ssz::ssz_encode; use state_processing::per_block_processing::errors::ProposerSlashingValidationError; use state_processing::per_block_processing::{ validate_attestation, verify_deposit, verify_exit, verify_exit_time_independent_only, - verify_proposer_slashing, verify_transfer, verify_transfer_partial, + verify_proposer_slashing, verify_transfer, verify_transfer_time_independent_only, }; use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet}; use types::chain_spec::Domain; @@ -322,7 +322,7 @@ impl OperationPool { // The signature of the transfer isn't hashed, but because we check // it before we insert into the HashSet, we can't end up with duplicate // transactions. - verify_transfer_partial(state, &transfer, spec, true).map_err(|_| ())?; + verify_transfer_time_independent_only(state, &transfer, spec).map_err(|_| ())?; self.transfers.insert(transfer); Ok(()) } diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index 617da00d4d..55e2c29d00 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -9,12 +9,12 @@ pub use self::verify_attester_slashing::{ }; pub use self::verify_proposer_slashing::verify_proposer_slashing; pub use validate_attestation::{validate_attestation, validate_attestation_without_signature}; -pub use verify_deposit::{ - get_existing_validator_index, verify_deposit, verify_deposit_index, -}; +pub use verify_deposit::{get_existing_validator_index, verify_deposit, verify_deposit_index}; pub use verify_exit::{verify_exit, verify_exit_time_independent_only}; pub use verify_slashable_attestation::verify_slashable_attestation; -pub use verify_transfer::{execute_transfer, verify_transfer, verify_transfer_partial}; +pub use verify_transfer::{ + execute_transfer, verify_transfer, verify_transfer_time_independent_only, +}; pub mod errors; mod validate_attestation; diff --git a/eth2/state_processing/src/per_block_processing/verify_transfer.rs b/eth2/state_processing/src/per_block_processing/verify_transfer.rs index 4f3815797c..ac9e9aa097 100644 --- a/eth2/state_processing/src/per_block_processing/verify_transfer.rs +++ b/eth2/state_processing/src/per_block_processing/verify_transfer.rs @@ -16,18 +16,24 @@ pub fn verify_transfer( transfer: &Transfer, spec: &ChainSpec, ) -> Result<(), Error> { - verify_transfer_partial(state, transfer, spec, false) + verify_transfer_parametric(state, transfer, spec, false) } -/// Parametric version of `verify_transfer` that allows some checks to be skipped. -/// -/// In everywhere except the operation pool, `verify_transfer` should be preferred over this -/// function. -pub fn verify_transfer_partial( +/// Like `verify_transfer` but doesn't run checks which may become true in future states. +pub fn verify_transfer_time_independent_only( state: &BeaconState, transfer: &Transfer, spec: &ChainSpec, - for_op_pool: bool, +) -> Result<(), Error> { + verify_transfer_parametric(state, transfer, spec, true) +} + +/// Parametric version of `verify_transfer` that allows some checks to be skipped. +fn verify_transfer_parametric( + state: &BeaconState, + transfer: &Transfer, + spec: &ChainSpec, + time_independent_only: bool, ) -> Result<(), Error> { let sender_balance = *state .validator_balances @@ -40,17 +46,17 @@ pub fn verify_transfer_partial( .ok_or_else(|| Error::Invalid(Invalid::FeeOverflow(transfer.amount, transfer.fee)))?; verify!( - for_op_pool || sender_balance >= transfer.amount, + time_independent_only || sender_balance >= transfer.amount, Invalid::FromBalanceInsufficient(transfer.amount, sender_balance) ); verify!( - for_op_pool || sender_balance >= transfer.fee, + time_independent_only || sender_balance >= transfer.fee, Invalid::FromBalanceInsufficient(transfer.fee, sender_balance) ); verify!( - for_op_pool + time_independent_only || (sender_balance == total_amount) || (sender_balance >= (total_amount + spec.min_deposit_amount)), Invalid::InvalidResultingFromBalance( @@ -59,7 +65,7 @@ pub fn verify_transfer_partial( ) ); - if for_op_pool { + if time_independent_only { verify!( state.slot <= transfer.slot, Invalid::TransferSlotInPast(state.slot, transfer.slot) @@ -78,7 +84,7 @@ pub fn verify_transfer_partial( let epoch = state.slot.epoch(spec.slots_per_epoch); verify!( - for_op_pool + time_independent_only || sender_validator.is_withdrawable_at(epoch) || sender_validator.activation_epoch == spec.far_future_epoch, Invalid::FromValidatorIneligableForTransfer(transfer.sender) From 3396f2f08e05d82485b2317db04bce54831f29da Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 16:28:04 +1100 Subject: [PATCH 058/191] op-pool: propagate errors, sort by transfer fee --- eth2/operation_pool/src/lib.rs | 46 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index ff5a7416b1..c6af777e6d 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -1,7 +1,10 @@ use int_to_bytes::int_to_bytes8; use itertools::Itertools; use ssz::ssz_encode; -use state_processing::per_block_processing::errors::ProposerSlashingValidationError; +use state_processing::per_block_processing::errors::{ + AttestationValidationError, DepositValidationError, ExitValidationError, + ProposerSlashingValidationError, TransferValidationError, +}; use state_processing::per_block_processing::{ validate_attestation, verify_deposit, verify_exit, verify_exit_time_independent_only, verify_proposer_slashing, verify_transfer, verify_transfer_time_independent_only, @@ -108,10 +111,10 @@ impl OperationPool { attestation: Attestation, state: &BeaconState, spec: &ChainSpec, - ) -> Result<(), ()> { + ) -> Result<(), AttestationValidationError> { // Check that attestation signatures are valid. // FIXME: should disable the time-dependent checks. - validate_attestation(state, &attestation, spec).map_err(|_| ())?; + validate_attestation(state, &attestation, spec)?; let id = AttestationId::from_data(&attestation.data, state, spec); @@ -143,7 +146,7 @@ impl OperationPool { /// Get a list of attestations for inclusion in a block. pub fn get_attestations(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { // Attestations for the current fork... - // TODO: should we also check domain bytes for the previous epoch? + // FIXME: should we also check domain bytes for the previous epoch? let current_epoch = state.slot.epoch(spec.slots_per_epoch); let domain_bytes = AttestationId::compute_domain_bytes(current_epoch, state, spec); self.attestations @@ -184,13 +187,12 @@ impl OperationPool { deposit: Deposit, state: &BeaconState, spec: &ChainSpec, - ) -> Result { + ) -> Result { use DepositInsertStatus::*; match self.deposits.entry(deposit.index) { Entry::Vacant(entry) => { - // FIXME: error prop - verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec).map_err(|_| ())?; + verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec)?; entry.insert(deposit); Ok(Fresh) } @@ -198,7 +200,7 @@ impl OperationPool { if entry.get() == &deposit { Ok(Duplicate) } else { - verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec).map_err(|_| ())?; + verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec)?; Ok(Replaced(Box::new(entry.insert(deposit)))) } } @@ -279,7 +281,7 @@ impl OperationPool { ); } - // TODO: copy ProposerSlashing code for AttesterSlashing + // FIXME: copy ProposerSlashing code for AttesterSlashing /// Insert a voluntary exit, validating it almost-entirely (future exits are permitted). pub fn insert_voluntary_exit( @@ -287,14 +289,13 @@ impl OperationPool { exit: VoluntaryExit, state: &BeaconState, spec: &ChainSpec, - ) -> Result<(), ()> { - verify_exit_time_independent_only(state, &exit, spec).map_err(|_| ())?; + ) -> Result<(), ExitValidationError> { + verify_exit_time_independent_only(state, &exit, spec)?; self.voluntary_exits.insert(exit.validator_index, exit); Ok(()) } /// Get a list of voluntary exits for inclusion in a block. - // TODO: could optimise this by eliding the checks that have already been done on insert pub fn get_voluntary_exits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { filter_limit_operations( self.voluntary_exits.values(), @@ -318,25 +319,26 @@ impl OperationPool { transfer: Transfer, state: &BeaconState, spec: &ChainSpec, - ) -> Result<(), ()> { + ) -> Result<(), TransferValidationError> { // The signature of the transfer isn't hashed, but because we check // it before we insert into the HashSet, we can't end up with duplicate // transactions. - verify_transfer_time_independent_only(state, &transfer, spec).map_err(|_| ())?; + verify_transfer_time_independent_only(state, &transfer, spec)?; self.transfers.insert(transfer); Ok(()) } /// Get a list of transfers for inclusion in a block. - // TODO: improve the economic optimality of this function by taking the transfer - // fees into account, and dependencies between transfers in the same block - // e.g. A pays B, B pays C + // TODO: improve the economic optimality of this function by accounting for + // dependencies between transfers in the same block e.g. A pays B, B pays C pub fn get_transfers(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { - filter_limit_operations( - &self.transfers, - |transfer| verify_transfer(state, transfer, spec).is_ok(), - spec.max_transfers, - ) + self.transfers + .iter() + .filter(|transfer| verify_transfer(state, transfer, spec).is_ok()) + .sorted_by_key(|transfer| std::cmp::Reverse(transfer.fee)) + .take(spec.max_transfers as usize) + .cloned() + .collect() } /// Prune the set of transfers by removing all those whose slot has already passed. From e512f7c0e1cc0d836e1aac2adcea4bc5b2b7af23 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 20 Mar 2019 16:52:58 +1100 Subject: [PATCH 059/191] op-pool: validate_attestation_time_independent_only --- eth2/operation_pool/src/lib.rs | 8 +- .../src/per_block_processing.rs | 5 +- .../validate_attestation.rs | 108 +++++++++++------- 3 files changed, 75 insertions(+), 46 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index c6af777e6d..6d276600ad 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -6,8 +6,9 @@ use state_processing::per_block_processing::errors::{ ProposerSlashingValidationError, TransferValidationError, }; use state_processing::per_block_processing::{ - validate_attestation, verify_deposit, verify_exit, verify_exit_time_independent_only, - verify_proposer_slashing, verify_transfer, verify_transfer_time_independent_only, + validate_attestation, validate_attestation_time_independent_only, verify_deposit, verify_exit, + verify_exit_time_independent_only, verify_proposer_slashing, verify_transfer, + verify_transfer_time_independent_only, }; use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet}; use types::chain_spec::Domain; @@ -113,8 +114,7 @@ impl OperationPool { spec: &ChainSpec, ) -> Result<(), AttestationValidationError> { // Check that attestation signatures are valid. - // FIXME: should disable the time-dependent checks. - validate_attestation(state, &attestation, spec)?; + validate_attestation_time_independent_only(state, &attestation, spec)?; let id = AttestationId::from_data(&attestation.data, state, spec); diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index 55e2c29d00..031b919c19 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -8,7 +8,10 @@ pub use self::verify_attester_slashing::{ gather_attester_slashing_indices, verify_attester_slashing, }; pub use self::verify_proposer_slashing::verify_proposer_slashing; -pub use validate_attestation::{validate_attestation, validate_attestation_without_signature}; +pub use validate_attestation::{ + validate_attestation, validate_attestation_time_independent_only, + validate_attestation_without_signature, +}; pub use verify_deposit::{get_existing_validator_index, verify_deposit, verify_deposit_index}; pub use verify_exit::{verify_exit, verify_exit_time_independent_only}; pub use verify_slashable_attestation::verify_slashable_attestation; diff --git a/eth2/state_processing/src/per_block_processing/validate_attestation.rs b/eth2/state_processing/src/per_block_processing/validate_attestation.rs index 2143988a46..3b89bec99c 100644 --- a/eth2/state_processing/src/per_block_processing/validate_attestation.rs +++ b/eth2/state_processing/src/per_block_processing/validate_attestation.rs @@ -14,7 +14,16 @@ pub fn validate_attestation( attestation: &Attestation, spec: &ChainSpec, ) -> Result<(), Error> { - validate_attestation_signature_optional(state, attestation, spec, true) + validate_attestation_parametric(state, attestation, spec, true, false) +} + +/// Like `validate_attestation` but doesn't run checks which may become true in future states. +pub fn validate_attestation_time_independent_only( + state: &BeaconState, + attestation: &Attestation, + spec: &ChainSpec, +) -> Result<(), Error> { + validate_attestation_parametric(state, attestation, spec, true, true) } /// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the @@ -28,7 +37,7 @@ pub fn validate_attestation_without_signature( attestation: &Attestation, spec: &ChainSpec, ) -> Result<(), Error> { - validate_attestation_signature_optional(state, attestation, spec, false) + validate_attestation_parametric(state, attestation, spec, false, false) } /// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the @@ -36,15 +45,13 @@ pub fn validate_attestation_without_signature( /// /// /// Spec v0.5.0 -fn validate_attestation_signature_optional( +fn validate_attestation_parametric( state: &BeaconState, attestation: &Attestation, spec: &ChainSpec, verify_signature: bool, + time_independent_only: bool, ) -> Result<(), Error> { - let state_epoch = state.slot.epoch(spec.slots_per_epoch); - let attestation_epoch = attestation.data.slot.epoch(spec.slots_per_epoch); - // Can't submit pre-historic attestations. verify!( attestation.data.slot >= spec.genesis_slot, @@ -65,7 +72,8 @@ fn validate_attestation_signature_optional( // Can't submit attestation too quickly. verify!( - attestation.data.slot + spec.min_attestation_inclusion_delay <= state.slot, + time_independent_only + || attestation.data.slot + spec.min_attestation_inclusion_delay <= state.slot, Invalid::IncludedTooEarly { state: state.slot, delay: spec.min_attestation_inclusion_delay, @@ -74,40 +82,8 @@ fn validate_attestation_signature_optional( ); // Verify the justified epoch and root is correct. - if attestation_epoch >= state_epoch { - verify!( - attestation.data.source_epoch == state.current_justified_epoch, - Invalid::WrongJustifiedEpoch { - state: state.current_justified_epoch, - attestation: attestation.data.source_epoch, - is_current: true, - } - ); - verify!( - attestation.data.source_root == state.current_justified_root, - Invalid::WrongJustifiedRoot { - state: state.current_justified_root, - attestation: attestation.data.source_root, - is_current: true, - } - ); - } else { - verify!( - attestation.data.source_epoch == state.previous_justified_epoch, - Invalid::WrongJustifiedEpoch { - state: state.previous_justified_epoch, - attestation: attestation.data.source_epoch, - is_current: false, - } - ); - verify!( - attestation.data.source_root == state.previous_justified_root, - Invalid::WrongJustifiedRoot { - state: state.previous_justified_root, - attestation: attestation.data.source_root, - is_current: true, - } - ); + if !time_independent_only { + verify_justified_epoch_and_root(attestation, state, spec)?; } // Check that the crosslink data is valid. @@ -188,6 +164,56 @@ fn validate_attestation_signature_optional( Ok(()) } +/// Verify that the `source_epoch` and `source_root` of an `Attestation` correctly +/// match the current (or previous) justified epoch and root from the state. +/// +/// Spec v0.5.0 +fn verify_justified_epoch_and_root( + attestation: &Attestation, + state: &BeaconState, + spec: &ChainSpec, +) -> Result<(), Error> { + let state_epoch = state.slot.epoch(spec.slots_per_epoch); + let attestation_epoch = attestation.data.slot.epoch(spec.slots_per_epoch); + + if attestation_epoch >= state_epoch { + verify!( + attestation.data.source_epoch == state.current_justified_epoch, + Invalid::WrongJustifiedEpoch { + state: state.current_justified_epoch, + attestation: attestation.data.source_epoch, + is_current: true, + } + ); + verify!( + attestation.data.source_root == state.current_justified_root, + Invalid::WrongJustifiedRoot { + state: state.current_justified_root, + attestation: attestation.data.source_root, + is_current: true, + } + ); + } else { + verify!( + attestation.data.source_epoch == state.previous_justified_epoch, + Invalid::WrongJustifiedEpoch { + state: state.previous_justified_epoch, + attestation: attestation.data.source_epoch, + is_current: false, + } + ); + verify!( + attestation.data.source_root == state.previous_justified_root, + Invalid::WrongJustifiedRoot { + state: state.previous_justified_root, + attestation: attestation.data.source_root, + is_current: true, + } + ); + } + Ok(()) +} + /// Verifies an aggregate signature for some given `AttestationData`, returning `true` if the /// `aggregate_signature` is valid. /// From 22a90a0224c38c7b7913d520d9c90bc0fa5005c1 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 25 Mar 2019 11:56:30 +1100 Subject: [PATCH 060/191] op-pool: check previous epoch in get_attestations --- eth2/operation_pool/src/lib.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 6d276600ad..fc894da271 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -145,13 +145,17 @@ impl OperationPool { /// Get a list of attestations for inclusion in a block. pub fn get_attestations(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { - // Attestations for the current fork... - // FIXME: should we also check domain bytes for the previous epoch? - let current_epoch = state.slot.epoch(spec.slots_per_epoch); - let domain_bytes = AttestationId::compute_domain_bytes(current_epoch, state, spec); + // Attestations for the current fork, which may be from the current or previous epoch. + let prev_epoch = state.previous_epoch(spec); + let current_epoch = state.current_epoch(spec); + let prev_domain_bytes = AttestationId::compute_domain_bytes(prev_epoch, state, spec); + let curr_domain_bytes = AttestationId::compute_domain_bytes(current_epoch, state, spec); self.attestations .iter() - .filter(|(key, _)| key.domain_bytes_match(&domain_bytes)) + .filter(|(key, _)| { + key.domain_bytes_match(&prev_domain_bytes) + || key.domain_bytes_match(&curr_domain_bytes) + }) .flat_map(|(_, attestations)| attestations) // That are valid... .filter(|attestation| validate_attestation(state, attestation, spec).is_ok()) From 0bf8cb953eeaa86662455e0025be0e7f1f4b4808 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 25 Mar 2019 12:44:30 +1100 Subject: [PATCH 061/191] BLS: wrap AggregateSignature::add_aggregate --- eth2/utils/bls/src/aggregate_signature.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/eth2/utils/bls/src/aggregate_signature.rs b/eth2/utils/bls/src/aggregate_signature.rs index 7b80d3bbfd..44eafdcdf5 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -27,6 +27,11 @@ impl AggregateSignature { self.0.add(signature.as_raw()) } + /// Add (aggregate) another `AggregateSignature`. + pub fn add_aggregate(&mut self, agg_signature: &AggregateSignature) { + self.0.add_aggregate(&agg_signature.0) + } + /// Verify the `AggregateSignature` against an `AggregatePublicKey`. /// /// Only returns `true` if the set of keys in the `AggregatePublicKey` match the set of keys From bde7ab79c800203a1624a014db656017155ff381 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 25 Mar 2019 12:45:24 +1100 Subject: [PATCH 062/191] types: aggregate signatures in attestations --- eth2/types/src/attestation.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 043711015a..dabccfde7e 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -46,7 +46,8 @@ impl Attestation { self.aggregation_bitfield .union_inplace(&other.aggregation_bitfield); self.custody_bitfield.union_inplace(&other.custody_bitfield); - // FIXME: signature aggregation once our BLS library wraps it + self.aggregate_signature + .add_aggregate(&other.aggregate_signature); } } From 708d9b5674aeb1921778a489a4129ba4194e6713 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 25 Mar 2019 14:27:20 +1100 Subject: [PATCH 063/191] Add basic Gossip sync handlers --- beacon_node/beacon_chain/src/lib.rs | 1 + beacon_node/eth2-libp2p/src/rpc/methods.rs | 20 ++++++- beacon_node/eth2-libp2p/src/rpc/mod.rs | 2 +- beacon_node/network/src/beacon_chain.rs | 21 ++++++- beacon_node/network/src/message_handler.rs | 23 +++++++- beacon_node/network/src/sync/import_queue.rs | 2 +- beacon_node/network/src/sync/simple_sync.rs | 58 ++++++++++++++++++++ 7 files changed, 119 insertions(+), 8 deletions(-) diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 5fa7e7a77b..48a42b941e 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -8,6 +8,7 @@ pub mod test_utils; pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; pub use self::errors::BeaconChainError; +pub use attestation_aggregator::Outcome as AggregationOutcome; pub use db; pub use fork_choice; pub use parking_lot; diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index 85ef7e06f7..47e47f3eb1 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -1,7 +1,7 @@ use ssz::{Decodable, DecodeError, Encodable, SszStream}; /// Available RPC methods types and ids. use ssz_derive::{Decode, Encode}; -use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; +use types::{Attestation, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; #[derive(Debug)] /// Available Serenity Libp2p RPC methods @@ -97,6 +97,12 @@ impl RPCResponse { } } +#[derive(Debug, Clone)] +pub enum IncomingGossip { + Block(BlockGossip), + Attestation(AttestationGossip), +} + /* Request/Response data structures for RPC methods */ /// The HELLO request/response handshake message. @@ -236,3 +242,15 @@ pub struct BeaconChainStateResponse { /// The values corresponding the to the requested tree hashes. pub values: bool, //TBD - stubbed with encodeable bool } + +/// 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, +} diff --git a/beacon_node/eth2-libp2p/src/rpc/mod.rs b/beacon_node/eth2-libp2p/src/rpc/mod.rs index a1573ec93d..925c36616a 100644 --- a/beacon_node/eth2-libp2p/src/rpc/mod.rs +++ b/beacon_node/eth2-libp2p/src/rpc/mod.rs @@ -11,7 +11,7 @@ use libp2p::core::swarm::{ ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters, }; use libp2p::{Multiaddr, PeerId}; -pub use methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse}; +pub use methods::{HelloMessage, IncomingGossip, RPCMethod, RPCRequest, RPCResponse}; pub use protocol::{RPCEvent, RPCProtocol}; use slog::o; use std::marker::PhantomData; diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index cc54e8ae09..26cea00654 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -5,10 +5,10 @@ use beacon_chain::{ parking_lot::RwLockReadGuard, slot_clock::SlotClock, types::{BeaconState, ChainSpec}, - CheckPoint, + AggregationOutcome, CheckPoint, }; use eth2_libp2p::HelloMessage; -use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; +use types::{Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; @@ -37,6 +37,11 @@ pub trait BeaconChain: Send + Sync { fn process_block(&self, block: BeaconBlock) -> Result; + fn process_attestation( + &self, + attestation: Attestation, + ) -> Result; + fn get_block_roots( &self, start_slot: Slot, @@ -119,6 +124,18 @@ where self.process_block(block) } + fn process_attestation( + &self, + _attestation: Attestation, + ) -> Result { + // Awaiting a proper operations pool before we can import attestations. + // + // Returning a useless error for now. + // + // https://github.com/sigp/lighthouse/issues/281 + return Err(BeaconChainError::DBInconsistent("CANNOT PROCESS".into())); + } + fn get_block_roots( &self, start_slot: Slot, diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 57923b2c3e..a788e83c94 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::{ - rpc::{RPCRequest, RPCResponse}, + rpc::{IncomingGossip, RPCRequest, RPCResponse}, PeerId, RPCEvent, }; use futures::future; @@ -39,8 +39,8 @@ pub enum HandlerMessage { PeerDisconnected(PeerId), /// An RPC response/request has been received. RPC(PeerId, RPCEvent), - /// A block has been imported. - BlockImported(), //TODO: This comes from pub-sub - decide its contents + /// A gossip message has been received. + IncomingGossip(PeerId, IncomingGossip), } impl MessageHandler { @@ -90,6 +90,10 @@ impl MessageHandler { HandlerMessage::RPC(peer_id, rpc_event) => { self.handle_rpc_message(peer_id, rpc_event); } + // we have received an RPC message request/response + HandlerMessage::IncomingGossip(peer_id, gossip) => { + self.handle_gossip(peer_id, gossip); + } //TODO: Handle all messages _ => {} } @@ -186,6 +190,19 @@ impl MessageHandler { } }; } + + /// Handle RPC messages + fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: IncomingGossip) { + match gossip_message { + IncomingGossip::Block(message) => { + self.sync + .on_block_gossip(peer_id, message, &mut self.network_context) + } + IncomingGossip::Attestation(message) => { + // + } + } + } } pub struct NetworkContext { diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs index 6508af89e6..17cbd2f12e 100644 --- a/beacon_node/network/src/sync/import_queue.rs +++ b/beacon_node/network/src/sync/import_queue.rs @@ -104,7 +104,7 @@ impl ImportQueue { } /// Returns `true` if `self.chain` has not yet processed this block. - fn is_new_block(&self, block_root: &Hash256) -> bool { + pub fn is_new_block(&self, block_root: &Hash256) -> bool { self.chain .is_new_block_root(&block_root) .unwrap_or_else(|_| { diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 4ee3490430..06ccbafd38 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -204,6 +204,8 @@ impl SimpleSync { self.known_peers.insert(peer_id.clone(), remote); } + // TODO: boot peer if finalization is wrong. + match remote_status { PeerStatus::OnDifferentChain => { info!( @@ -462,6 +464,62 @@ impl SimpleSync { self.process_import_queue(network); } + /// Process a gossip message declaring a new block. + pub fn on_block_gossip( + &mut self, + peer_id: PeerId, + msg: BlockGossip, + network: &mut NetworkContext, + ) { + debug!( + self.log, + "BlockGossip"; + "peer" => format!("{:?}", peer_id), + ); + // TODO: filter out messages that a prior to the finalized slot. + // + // TODO: if the block is a few more slots ahead, try to get all block roots from then until + // 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) { + self.request_block_headers( + peer_id, + BeaconBlockHeadersRequest { + start_root: msg.root.block_root, + start_slot: msg.root.slot, + max_headers: 1, + skip_slots: 0, + }, + network, + ) + } + } + + /// Process a gossip message declaring a new attestation. + /// + /// Not currently implemented. + pub fn on_attestation_gossip( + &mut self, + peer_id: PeerId, + msg: AttestationGossip, + _network: &mut NetworkContext, + ) { + debug!( + self.log, + "AttestationGossip"; + "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) { + Ok(_) => panic!("Impossible, method not implemented."), + Err(_) => error!(self.log, "Attestation processing not implemented!"), + } + } + /// Iterate through the `import_queue` and process any complete blocks. /// /// If a block is successfully processed it is removed from the queue, otherwise it remains in From 4fdb01e5f06df069aa2d0a1e10237d3135004bc8 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 15:10:26 +1100 Subject: [PATCH 064/191] 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 ebb9ced0a47331980ed74c42ffe4f638ff55979a Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 25 Mar 2019 15:30:46 +1100 Subject: [PATCH 065/191] Improve peer status handling --- beacon_node/network/src/message_handler.rs | 8 +- beacon_node/network/src/sync/simple_sync.rs | 108 ++++++++++++++------ 2 files changed, 81 insertions(+), 35 deletions(-) diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index a788e83c94..5b3fe1a63a 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::{ - rpc::{IncomingGossip, RPCRequest, RPCResponse}, + rpc::{methods::GoodbyeReason, IncomingGossip, RPCRequest, RPCResponse}, PeerId, RPCEvent, }; use futures::future; @@ -199,7 +199,8 @@ impl MessageHandler { .on_block_gossip(peer_id, message, &mut self.network_context) } IncomingGossip::Attestation(message) => { - // + self.sync + .on_attestation_gossip(peer_id, message, &mut self.network_context) } } } @@ -226,7 +227,8 @@ impl NetworkContext { } } - pub fn disconnect(&self, _peer_id: PeerId) { + pub fn disconnect(&mut self, peer_id: PeerId, reason: GoodbyeReason) { + self.send_rpc_request(peer_id, RPCRequest::Goodbye(reason)) // TODO: disconnect peers. } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 06ccbafd38..05c1a04304 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -27,9 +27,9 @@ pub struct PeerSyncInfo { } impl PeerSyncInfo { - /// Returns `true` if the peer is on the same chain as `other`. - fn is_on_same_chain(&self, other: Self) -> bool { - self.network_id == other.network_id + /// Returns `true` if the has a different network ID to `other`. + fn has_different_network_id_to(&self, other: Self) -> bool { + self.network_id != other.network_id } /// Returns `true` if the peer has a higher finalized epoch than `other`. @@ -41,19 +41,6 @@ impl PeerSyncInfo { fn has_higher_best_slot_than(&self, other: Self) -> bool { self.best_slot > other.best_slot } - - /// Returns the `PeerStatus` of `self` in relation to `other`. - pub fn status_compared_to(&self, other: Self) -> PeerStatus { - if self.has_higher_finalized_epoch_than(other) { - PeerStatus::HigherFinalizedEpoch - } else if !self.is_on_same_chain(other) { - PeerStatus::OnDifferentChain - } else if self.has_higher_best_slot_than(other) { - PeerStatus::HigherBestSlot - } else { - PeerStatus::NotInteresting - } - } } /// The status of a peers view on the chain, relative to some other view of the chain (presumably @@ -61,7 +48,9 @@ impl PeerSyncInfo { #[derive(PartialEq, Clone, Copy, Debug)] pub enum PeerStatus { /// The peer is on a completely different chain. - OnDifferentChain, + DifferentNetworkId, + /// The peer lists a finalized epoch for which we have a different root. + FinalizedEpochNotInChain, /// The peer has a higher finalized epoch. HigherFinalizedEpoch, /// The peer has a higher best slot. @@ -70,6 +59,18 @@ pub enum PeerStatus { NotInteresting, } +impl PeerStatus { + pub fn should_handshake(&self) -> bool { + match self { + PeerStatus::DifferentNetworkId => false, + PeerStatus::FinalizedEpochNotInChain => false, + PeerStatus::HigherFinalizedEpoch => true, + PeerStatus::HigherBestSlot => true, + PeerStatus::NotInteresting => true, + } + } +} + impl From for PeerSyncInfo { fn from(hello: HelloMessage) -> PeerSyncInfo { PeerSyncInfo { @@ -183,6 +184,51 @@ impl SimpleSync { self.process_hello(peer_id, hello, network); } + /// Returns a `PeerStatus` for some peer. + fn peer_status(&self, peer: PeerSyncInfo) -> PeerStatus { + let local = PeerSyncInfo::from(&self.chain); + + if peer.has_different_network_id_to(local) { + return PeerStatus::DifferentNetworkId; + } + + if local.has_higher_finalized_epoch_than(peer) { + let peer_finalized_slot = peer + .latest_finalized_epoch + .start_slot(self.chain.get_spec().slots_per_epoch); + + let local_roots = self.chain.get_block_roots(peer_finalized_slot, 1, 0); + + if let Ok(local_roots) = local_roots { + if let Some(local_root) = local_roots.get(0) { + if *local_root != peer.latest_finalized_root { + return PeerStatus::FinalizedEpochNotInChain; + } + } else { + error!( + self.log, + "Cannot get root for peer finalized slot."; + "error" => "empty roots" + ); + } + } else { + error!( + self.log, + "Cannot get root for peer finalized slot."; + "error" => format!("{:?}", local_roots) + ); + } + } + + if peer.has_higher_finalized_epoch_than(local) { + PeerStatus::HigherFinalizedEpoch + } else if peer.has_higher_best_slot_than(local) { + PeerStatus::HigherBestSlot + } else { + PeerStatus::NotInteresting + } + } + /// Process a `Hello` message, requesting new blocks if appropriate. /// /// Disconnects the peer if required. @@ -196,26 +242,22 @@ impl SimpleSync { let remote = PeerSyncInfo::from(hello); let local = PeerSyncInfo::from(&self.chain); - let remote_status = remote.status_compared_to(local); + let remote_status = self.peer_status(remote); - // network id must match - if remote_status != PeerStatus::OnDifferentChain { + if remote_status.should_handshake() { info!(self.log, "HandshakeSuccess"; "peer" => format!("{:?}", peer_id)); self.known_peers.insert(peer_id.clone(), remote); + } else { + info!( + self.log, "HandshakeFailure"; + "peer" => format!("{:?}", peer_id), + "reason" => "network_id" + ); + network.disconnect(peer_id.clone(), GoodbyeReason::IrreleventNetwork); } - // TODO: boot peer if finalization is wrong. - + // If required, send requests for blocks. match remote_status { - PeerStatus::OnDifferentChain => { - info!( - self.log, "Failure"; - "peer" => format!("{:?}", peer_id), - "reason" => "network_id" - ); - - network.disconnect(peer_id); - } PeerStatus::HigherFinalizedEpoch => { let start_slot = remote .latest_finalized_epoch @@ -243,6 +285,8 @@ impl SimpleSync { network, ); } + PeerStatus::FinalizedEpochNotInChain => {} + PeerStatus::DifferentNetworkId => {} PeerStatus::NotInteresting => {} } } @@ -541,7 +585,7 @@ impl SimpleSync { "sender_peer_id" => format!("{:?}", sender), "reason" => format!("{:?}", outcome), ); - network.disconnect(sender); + network.disconnect(sender, GoodbyeReason::Fault); } // If this results to true, the item will be removed from the queue. From da219ffd1020c24a8b2316fa2246ec45aa0496d2 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Mon, 25 Mar 2019 16:13:46 +1100 Subject: [PATCH 066/191] Added cargo cache back into it. --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index e217a6130d..0401c0df53 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,7 +2,7 @@ pipeline { agent { dockerfile { filename 'Dockerfile' - args '-v cargo-cache:/cargocache:rw' + args '-v cargo-cache:/cargocache:rw -e "CARGO_HOME=/cargocache"' } } stages { From 32a025bdf78d818939bfa047f3389246d23eb6d8 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 25 Mar 2019 16:48:44 +1100 Subject: [PATCH 067/191] Introduced `RequestId` newtype --- beacon_node/eth2-libp2p/src/rpc/mod.rs | 2 +- beacon_node/eth2-libp2p/src/rpc/protocol.rs | 58 +++++++++++++- beacon_node/network/src/message_handler.rs | 88 ++++++++++++--------- beacon_node/network/src/sync/simple_sync.rs | 12 ++- beacon_node/network/tests/tests.rs | 16 ++-- 5 files changed, 125 insertions(+), 51 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/rpc/mod.rs b/beacon_node/eth2-libp2p/src/rpc/mod.rs index 925c36616a..e04540416a 100644 --- a/beacon_node/eth2-libp2p/src/rpc/mod.rs +++ b/beacon_node/eth2-libp2p/src/rpc/mod.rs @@ -12,7 +12,7 @@ use libp2p::core::swarm::{ }; use libp2p::{Multiaddr, PeerId}; pub use methods::{HelloMessage, IncomingGossip, RPCMethod, RPCRequest, RPCResponse}; -pub use protocol::{RPCEvent, RPCProtocol}; +pub use protocol::{RPCEvent, RPCProtocol, RequestId}; use slog::o; use std::marker::PhantomData; use tokio::io::{AsyncRead, AsyncWrite}; diff --git a/beacon_node/eth2-libp2p/src/rpc/protocol.rs b/beacon_node/eth2-libp2p/src/rpc/protocol.rs index b328dd0ddf..2c8945cb83 100644 --- a/beacon_node/eth2-libp2p/src/rpc/protocol.rs +++ b/beacon_node/eth2-libp2p/src/rpc/protocol.rs @@ -1,6 +1,7 @@ use super::methods::*; use libp2p::core::{upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo}; -use ssz::{ssz_encode, Decodable, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError as SSZDecodeError, Encodable, SszStream}; +use std::hash::{Hash, Hasher}; use std::io; use std::iter; use tokio::io::{AsyncRead, AsyncWrite}; @@ -29,16 +30,65 @@ impl Default for RPCProtocol { } } +/// A monotonic counter for ordering `RPCRequest`s. +#[derive(Debug, Clone, PartialEq, Default)] +pub struct RequestId(u64); + +impl RequestId { + /// Increment the request id. + pub fn increment(&mut self) { + self.0 += 1 + } + + /// Return the previous id. + pub fn previous(&self) -> Self { + Self(self.0 - 1) + } +} + +impl Eq for RequestId {} + +impl Hash for RequestId { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + +impl From for RequestId { + fn from(x: u64) -> RequestId { + RequestId(x) + } +} + +impl Into for RequestId { + fn into(self) -> u64 { + self.0 + } +} + +impl Encodable for RequestId { + fn ssz_append(&self, s: &mut SszStream) { + self.0.ssz_append(s); + } +} + +impl Decodable for RequestId { + fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), SSZDecodeError> { + let (id, index) = u64::ssz_decode(bytes, index)?; + Ok((Self::from(id), index)) + } +} + /// The RPC types which are sent/received in this protocol. #[derive(Debug, Clone)] pub enum RPCEvent { Request { - id: u64, + id: RequestId, method_id: u16, body: RPCRequest, }, Response { - id: u64, + id: RequestId, method_id: u16, //TODO: Remove and process decoding upstream result: RPCResponse, }, @@ -72,7 +122,7 @@ fn decode(packet: Vec) -> Result { // decode the header of the rpc // request/response let (request, index) = bool::ssz_decode(&packet, 0)?; - let (id, index) = u64::ssz_decode(&packet, index)?; + let (id, index) = RequestId::ssz_decode(&packet, index)?; let (method_id, index) = u16::ssz_decode(&packet, index)?; if request { diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 5b3fe1a63a..12fb2fa6ee 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::{ - rpc::{methods::GoodbyeReason, IncomingGossip, RPCRequest, RPCResponse}, + rpc::{methods::GoodbyeReason, IncomingGossip, RPCRequest, RPCResponse, RequestId}, PeerId, RPCEvent, }; use futures::future; @@ -111,25 +111,31 @@ impl MessageHandler { } /// A new RPC request has been received from the network. - fn handle_rpc_request(&mut self, peer_id: PeerId, _id: u64, request: RPCRequest) { + fn handle_rpc_request(&mut self, peer_id: PeerId, request_id: RequestId, request: RPCRequest) { // TODO: process the `id`. match request { - RPCRequest::Hello(hello_message) => { - self.sync - .on_hello_request(peer_id, hello_message, &mut self.network_context) - } + RPCRequest::Hello(hello_message) => self.sync.on_hello_request( + peer_id, + request_id, + hello_message, + &mut self.network_context, + ), RPCRequest::Goodbye(goodbye_reason) => self.sync.on_goodbye(peer_id, goodbye_reason), - RPCRequest::BeaconBlockRoots(request) => { - self.sync - .on_beacon_block_roots_request(peer_id, request, &mut self.network_context) - } + RPCRequest::BeaconBlockRoots(request) => self.sync.on_beacon_block_roots_request( + peer_id, + request_id, + request, + &mut self.network_context, + ), RPCRequest::BeaconBlockHeaders(request) => self.sync.on_beacon_block_headers_request( peer_id, + request_id, request, &mut self.network_context, ), RPCRequest::BeaconBlockBodies(request) => self.sync.on_beacon_block_bodies_request( peer_id, + request_id, request, &mut self.network_context, ), @@ -143,17 +149,23 @@ impl MessageHandler { /// An RPC response has been received from the network. // we match on id and ignore responses past the timeout. - fn handle_rpc_response(&mut self, peer_id: PeerId, id: u64, response: RPCResponse) { - // if response id is related to a request, ignore (likely RPC timeout) + fn handle_rpc_response(&mut self, peer_id: PeerId, id: RequestId, response: RPCResponse) { + // if response id is not related to a request, ignore (likely RPC timeout) if self .network_context - .requests - .remove(&(peer_id.clone(), id)) + .outstanding_outgoing_request_ids + .remove(&(peer_id.clone(), id.clone())) .is_none() { - debug!(self.log, "Unrecognised response from peer: {:?}", peer_id); + warn!( + self.log, + "Unknown ResponseId for incoming RPCRequest"; + "peer" => format!("{:?}", peer_id), + "request_id" => format!("{:?}", id) + ); return; } + match response { RPCResponse::Hello(hello_message) => { self.sync @@ -210,9 +222,9 @@ pub struct NetworkContext { /// The network channel to relay messages to the Network service. network_send: crossbeam_channel::Sender, /// A mapping of peers and the RPC id we have sent an RPC request to. - requests: HashMap<(PeerId, u64), Instant>, - /// A counter of request id for each peer. - request_ids: HashMap, + outstanding_outgoing_request_ids: HashMap<(PeerId, RequestId), Instant>, + /// Stores the next `RequestId` we should include on an outgoing `RPCRequest` to a `PeerId`. + outgoing_request_ids: HashMap, /// The `MessageHandler` logger. log: slog::Logger, } @@ -221,8 +233,8 @@ impl NetworkContext { pub fn new(network_send: crossbeam_channel::Sender, log: slog::Logger) -> Self { Self { network_send, - requests: HashMap::new(), - request_ids: HashMap::new(), + outstanding_outgoing_request_ids: HashMap::new(), + outgoing_request_ids: HashMap::new(), log, } } @@ -234,6 +246,10 @@ impl NetworkContext { pub fn send_rpc_request(&mut self, peer_id: PeerId, rpc_request: RPCRequest) { let id = self.generate_request_id(&peer_id); + + self.outstanding_outgoing_request_ids + .insert((peer_id.clone(), id.clone()), Instant::now()); + self.send_rpc_event( peer_id, RPCEvent::Request { @@ -244,12 +260,16 @@ impl NetworkContext { ); } - pub fn send_rpc_response(&mut self, peer_id: PeerId, rpc_response: RPCResponse) { - let id = self.generate_request_id(&peer_id); + pub fn send_rpc_response( + &mut self, + peer_id: PeerId, + request_id: RequestId, + rpc_response: RPCResponse, + ) { self.send_rpc_event( peer_id, RPCEvent::Response { - id, + id: request_id, method_id: rpc_response.method_id(), result: rpc_response, }, @@ -272,18 +292,14 @@ impl NetworkContext { // } - /// Generates a new request id for a peer. - fn generate_request_id(&mut self, peer_id: &PeerId) -> u64 { - // generate a unique id for the peer - let id = { - let borrowed_id = self.request_ids.entry(peer_id.clone()).or_insert_with(|| 0); - let id = borrowed_id.clone(); - //increment the counter - *borrowed_id += 1; - id - }; - // register RPC request - self.requests.insert((peer_id.clone(), id), Instant::now()); - id + /// Returns the next `RequestId` for sending an `RPCRequest` to the `peer_id`. + fn generate_request_id(&mut self, peer_id: &PeerId) -> RequestId { + let next_id = self + .outgoing_request_ids + .entry(peer_id.clone()) + .and_modify(|id| id.increment()) + .or_insert_with(|| RequestId::from(1)); + + next_id.previous() } } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 05c1a04304..37c2c4c260 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -2,7 +2,7 @@ use super::import_queue::ImportQueue; use crate::beacon_chain::BeaconChain; use crate::message_handler::NetworkContext; use eth2_libp2p::rpc::methods::*; -use eth2_libp2p::rpc::{RPCRequest, RPCResponse}; +use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId}; use eth2_libp2p::PeerId; use slog::{debug, error, info, o, warn}; use std::collections::HashMap; @@ -157,6 +157,7 @@ impl SimpleSync { pub fn on_hello_request( &mut self, peer_id: PeerId, + request_id: RequestId, hello: HelloMessage, network: &mut NetworkContext, ) { @@ -165,6 +166,7 @@ impl SimpleSync { // Say hello back. network.send_rpc_response( peer_id.clone(), + request_id, RPCResponse::Hello(self.chain.hello_message()), ); @@ -256,7 +258,7 @@ impl SimpleSync { network.disconnect(peer_id.clone(), GoodbyeReason::IrreleventNetwork); } - // If required, send requests for blocks. + // If required, send additional requests. match remote_status { PeerStatus::HigherFinalizedEpoch => { let start_slot = remote @@ -295,6 +297,7 @@ impl SimpleSync { pub fn on_beacon_block_roots_request( &mut self, peer_id: PeerId, + request_id: RequestId, req: BeaconBlockRootsRequest, network: &mut NetworkContext, ) { @@ -333,6 +336,7 @@ impl SimpleSync { network.send_rpc_response( peer_id, + request_id, RPCResponse::BeaconBlockRoots(BeaconBlockRootsResponse { roots }), ) } @@ -385,6 +389,7 @@ impl SimpleSync { pub fn on_beacon_block_headers_request( &mut self, peer_id: PeerId, + request_id: RequestId, req: BeaconBlockHeadersRequest, network: &mut NetworkContext, ) { @@ -415,6 +420,7 @@ impl SimpleSync { network.send_rpc_response( peer_id, + request_id, RPCResponse::BeaconBlockHeaders(BeaconBlockHeadersResponse { headers }), ) } @@ -454,6 +460,7 @@ impl SimpleSync { pub fn on_beacon_block_bodies_request( &mut self, peer_id: PeerId, + request_id: RequestId, req: BeaconBlockBodiesRequest, network: &mut NetworkContext, ) { @@ -480,6 +487,7 @@ impl SimpleSync { network.send_rpc_response( peer_id, + request_id, RPCResponse::BeaconBlockBodies(BeaconBlockBodiesResponse { block_bodies }), ) } diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index 110450dc90..9cead1b557 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -1,6 +1,6 @@ use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender}; use eth2_libp2p::rpc::methods::*; -use eth2_libp2p::rpc::{RPCMethod, RPCRequest, RPCResponse}; +use eth2_libp2p::rpc::{RPCMethod, RPCRequest, RPCResponse, RequestId}; use eth2_libp2p::{PeerId, RPCEvent}; use network::beacon_chain::BeaconChain as NetworkBeaconChain; use network::message_handler::{HandlerMessage, MessageHandler}; @@ -82,8 +82,8 @@ impl SyncNode { let network_message = self.recv().expect("Timeout on tee"); let handler_message = match network_message.clone() { - NetworkMessage::Send(peer_id, OutgoingMessage::RPC(event)) => { - HandlerMessage::RPC(peer_id, event) + NetworkMessage::Send(_to_peer_id, OutgoingMessage::RPC(event)) => { + HandlerMessage::RPC(self.peer_id.clone(), event) } _ => panic!("tee cannot parse {:?}", network_message), }; @@ -265,7 +265,7 @@ fn get_logger() -> slog::Logger { pub struct SyncMaster { harness: BeaconChainHarness, peer_id: PeerId, - response_ids: Vec, + response_ids: Vec, } impl SyncMaster { @@ -276,7 +276,7 @@ impl SyncMaster { ) -> Self { let harness = BeaconChainHarness::from_beacon_state_builder(state_builder, spec.clone()); let peer_id = PeerId::random(); - let response_ids = vec![0; node_count]; + let response_ids = vec![RequestId::from(0); node_count]; Self { harness, @@ -285,9 +285,9 @@ impl SyncMaster { } } - pub fn response_id(&mut self, node: &SyncNode) -> u64 { - let id = self.response_ids[node.id]; - self.response_ids[node.id] += 1; + pub fn response_id(&mut self, node: &SyncNode) -> RequestId { + let id = self.response_ids[node.id].clone(); + self.response_ids[node.id].increment(); id } From ca9af49d4eac240abcf3c4b87237a815f20c03df Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 16:50:15 +1100 Subject: [PATCH 068/191] 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 518359e89845a7d16f99253b07560fa8a667e101 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 25 Mar 2019 16:58:20 +1100 Subject: [PATCH 069/191] op-pool: implement attester slashings --- eth2/operation_pool/src/lib.rs | 120 +++++++++++++++--- .../src/per_block_processing.rs | 3 +- .../verify_attester_slashing.rs | 21 ++- 3 files changed, 122 insertions(+), 22 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index fc894da271..fadbf449d4 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -2,12 +2,13 @@ use int_to_bytes::int_to_bytes8; use itertools::Itertools; use ssz::ssz_encode; use state_processing::per_block_processing::errors::{ - AttestationValidationError, DepositValidationError, ExitValidationError, - ProposerSlashingValidationError, TransferValidationError, + AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, + ExitValidationError, ProposerSlashingValidationError, TransferValidationError, }; use state_processing::per_block_processing::{ - validate_attestation, validate_attestation_time_independent_only, verify_deposit, verify_exit, - verify_exit_time_independent_only, verify_proposer_slashing, verify_transfer, + gather_attester_slashing_indices_modular, validate_attestation, + validate_attestation_time_independent_only, verify_attester_slashing, verify_deposit, + verify_exit, verify_exit_time_independent_only, verify_proposer_slashing, verify_transfer, verify_transfer_time_independent_only, }; use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet}; @@ -32,8 +33,8 @@ pub struct OperationPool { // and the spec doesn't seem to accomodate for re-orgs on a time-frame // longer than an epoch deposits: BTreeMap, - /// Map from attester index to slashing. - attester_slashings: HashMap, + /// Map from two attestation IDs to a slashing for those IDs. + attester_slashings: HashMap<(AttestationId, AttestationId), AttesterSlashing>, /// Map from proposer index to slashing. proposer_slashings: HashMap, /// Map from exiting validator to their exit data. @@ -250,18 +251,44 @@ impl OperationPool { Ok(()) } - /// Only check whether the implicated validator has already been slashed, because - /// all slashings in the pool were validated upon insertion. - // TODO: we need a mechanism to avoid including a proposer slashing and an attester - // slashing for the same validator in the same block - pub fn get_proposer_slashings( + /// Compute the tuple ID that is used to identify an attester slashing. + /// + /// Depends on the fork field of the state, but not on the state's epoch. + fn attester_slashing_id( + slashing: &AttesterSlashing, + state: &BeaconState, + spec: &ChainSpec, + ) -> (AttestationId, AttestationId) { + ( + AttestationId::from_data(&slashing.slashable_attestation_1.data, state, spec), + AttestationId::from_data(&slashing.slashable_attestation_2.data, state, spec), + ) + } + + /// Insert an attester slashing into the pool. + pub fn insert_attester_slashing( + &mut self, + slashing: AttesterSlashing, + state: &BeaconState, + spec: &ChainSpec, + ) -> Result<(), AttesterSlashingValidationError> { + verify_attester_slashing(state, &slashing, true, spec)?; + let id = Self::attester_slashing_id(&slashing, state, spec); + self.attester_slashings.insert(id, slashing); + Ok(()) + } + + /// Get proposer and attester slashings for inclusion in a block. + /// + /// This function computes both types of slashings together, because + /// attester slashings may be invalidated by proposer slashings included + /// earlier in the block. + pub fn get_slashings( &self, state: &BeaconState, spec: &ChainSpec, - ) -> Vec { - // We sort by validator index, which is safe, because a validator can only supply - // so many valid slashings for lower-indexed validators (and even that is unlikely) - filter_limit_operations( + ) -> (Vec, Vec) { + let proposer_slashings = filter_limit_operations( self.proposer_slashings.values(), |slashing| { state @@ -270,10 +297,48 @@ impl OperationPool { .map_or(false, |validator| !validator.slashed) }, spec.max_proposer_slashings, - ) + ); + + // Set of validators to be slashed, so we don't attempt to construct invalid attester + // slashings. + let mut to_be_slashed = proposer_slashings + .iter() + .map(|s| s.proposer_index) + .collect::>(); + + let attester_slashings = self + .attester_slashings + .iter() + .filter(|(id, slashing)| { + // Check the fork. + Self::attester_slashing_id(slashing, state, spec) == **id + }) + .filter(|(_, slashing)| { + // Take all slashings that will slash 1 or more validators. + let slashed_validators = gather_attester_slashing_indices_modular( + state, + slashing, + |index, validator| validator.slashed || to_be_slashed.contains(&index), + spec, + ); + + // Extend the `to_be_slashed` set so subsequent iterations don't try to include + // useless slashings. + if let Ok(validators) = slashed_validators { + to_be_slashed.extend(validators); + true + } else { + false + } + }) + .take(spec.max_attester_slashings as usize) + .map(|(_, slashing)| slashing.clone()) + .collect(); + + (proposer_slashings, attester_slashings) } - /// Prune slashings for all slashed or withdrawn validators. + /// Prune proposer slashings for all slashed or withdrawn validators. pub fn prune_proposer_slashings(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { prune_validator_hash_map( &mut self.proposer_slashings, @@ -285,7 +350,22 @@ impl OperationPool { ); } - // FIXME: copy ProposerSlashing code for AttesterSlashing + /// Prune attester slashings for all slashed or withdrawn validators, or attestations on another + /// fork. + pub fn prune_attester_slashings(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + self.attester_slashings.retain(|id, slashing| { + let fork_ok = &Self::attester_slashing_id(slashing, finalized_state, spec) == id; + let curr_epoch = finalized_state.current_epoch(spec); + let slashing_ok = gather_attester_slashing_indices_modular( + finalized_state, + slashing, + |_, validator| validator.slashed || validator.is_withdrawable_at(curr_epoch), + spec, + ) + .is_ok(); + fork_ok && slashing_ok + }); + } /// Insert a voluntary exit, validating it almost-entirely (future exits are permitted). pub fn insert_voluntary_exit( @@ -359,13 +439,13 @@ impl OperationPool { self.prune_attestations(finalized_state, spec); self.prune_deposits(finalized_state); self.prune_proposer_slashings(finalized_state, spec); - // FIXME: add attester slashings + self.prune_attester_slashings(finalized_state, spec); self.prune_voluntary_exits(finalized_state, spec); self.prune_transfers(finalized_state); } } -/// Filter up to a maximum number of operations out of a slice. +/// Filter up to a maximum number of operations out of an iterator. fn filter_limit_operations<'a, T: 'a, I, F>(operations: I, filter: F, limit: u64) -> Vec where I: IntoIterator, diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index 031b919c19..e79f5f08c3 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -5,7 +5,8 @@ use ssz::{SignedRoot, TreeHash}; use types::*; pub use self::verify_attester_slashing::{ - gather_attester_slashing_indices, verify_attester_slashing, + gather_attester_slashing_indices, gather_attester_slashing_indices_modular, + verify_attester_slashing, }; pub use self::verify_proposer_slashing::verify_proposer_slashing; pub use validate_attestation::{ diff --git a/eth2/state_processing/src/per_block_processing/verify_attester_slashing.rs b/eth2/state_processing/src/per_block_processing/verify_attester_slashing.rs index a198d2a3e2..abf99da641 100644 --- a/eth2/state_processing/src/per_block_processing/verify_attester_slashing.rs +++ b/eth2/state_processing/src/per_block_processing/verify_attester_slashing.rs @@ -47,6 +47,25 @@ pub fn gather_attester_slashing_indices( attester_slashing: &AttesterSlashing, spec: &ChainSpec, ) -> Result, Error> { + gather_attester_slashing_indices_modular( + state, + attester_slashing, + |_, validator| validator.slashed, + spec, + ) +} + +/// Same as `gather_attester_slashing_indices` but allows the caller to specify the criteria +/// for determining whether a given validator should be considered slashed. +pub fn gather_attester_slashing_indices_modular( + state: &BeaconState, + attester_slashing: &AttesterSlashing, + is_slashed: F, + spec: &ChainSpec, +) -> Result, Error> +where + F: Fn(u64, &Validator) -> bool, +{ let slashable_attestation_1 = &attester_slashing.slashable_attestation_1; let slashable_attestation_2 = &attester_slashing.slashable_attestation_2; @@ -57,7 +76,7 @@ pub fn gather_attester_slashing_indices( .get(*i as usize) .ok_or_else(|| Error::Invalid(Invalid::UnknownValidator(*i)))?; - if slashable_attestation_2.validator_indices.contains(&i) & !validator.slashed { + if slashable_attestation_2.validator_indices.contains(&i) & !is_slashed(*i, validator) { // TODO: verify that we should reject any slashable attestation which includes a // withdrawn validator. PH has asked the question on gitter, awaiting response. verify!( From 3ad18b4367047b4fab37b3946004c06086c8b080 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 17:47:23 +1100 Subject: [PATCH 070/191] 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 071/191] 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 072/191] 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 098e63ac327ea8a7b64df2d4fbdbe5f794f37671 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 25 Mar 2019 18:59:50 +1100 Subject: [PATCH 073/191] Move gossip structs into behaviour --- beacon_node/eth2-libp2p/src/behaviour.rs | 21 +++++++++++++++++++++ beacon_node/eth2-libp2p/src/rpc/methods.rs | 18 ------------------ beacon_node/eth2-libp2p/src/rpc/mod.rs | 2 +- beacon_node/network/src/message_handler.rs | 3 ++- beacon_node/network/src/sync/simple_sync.rs | 1 + 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 458b32cf93..3d5b943536 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -1,3 +1,4 @@ +use crate::rpc::methods::BlockRootSlot; use crate::rpc::{RPCEvent, RPCMessage, Rpc}; use crate::NetworkConfig; use futures::prelude::*; @@ -13,6 +14,8 @@ use libp2p::{ NetworkBehaviour, PeerId, }; use slog::{debug, o}; +use ssz_derive::{Decode, Encode}; +use types::Attestation; use types::Topic; /// Builds the network behaviour for the libp2p Swarm. @@ -154,3 +157,21 @@ pub enum BehaviourEvent { // TODO: This is a stub at the moment 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 { + pub root: BlockRootSlot, +} + +/// Gossipsub message providing notification of a new attestation. +#[derive(Encode, Decode, Clone, Debug, PartialEq)] +pub struct AttestationGossip { + pub attestation: Attestation, +} diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index 47e47f3eb1..ad3233be71 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -97,12 +97,6 @@ impl RPCResponse { } } -#[derive(Debug, Clone)] -pub enum IncomingGossip { - Block(BlockGossip), - Attestation(AttestationGossip), -} - /* Request/Response data structures for RPC methods */ /// The HELLO request/response handshake message. @@ -242,15 +236,3 @@ pub struct BeaconChainStateResponse { /// The values corresponding the to the requested tree hashes. pub values: bool, //TBD - stubbed with encodeable bool } - -/// 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, -} diff --git a/beacon_node/eth2-libp2p/src/rpc/mod.rs b/beacon_node/eth2-libp2p/src/rpc/mod.rs index e04540416a..08573aa52b 100644 --- a/beacon_node/eth2-libp2p/src/rpc/mod.rs +++ b/beacon_node/eth2-libp2p/src/rpc/mod.rs @@ -11,7 +11,7 @@ use libp2p::core::swarm::{ ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters, }; use libp2p::{Multiaddr, PeerId}; -pub use methods::{HelloMessage, IncomingGossip, RPCMethod, RPCRequest, RPCResponse}; +pub use methods::{HelloMessage, RPCMethod, RPCRequest, RPCResponse}; pub use protocol::{RPCEvent, RPCProtocol, RequestId}; use slog::o; use std::marker::PhantomData; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 12fb2fa6ee..dcfee96a0a 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -4,7 +4,8 @@ use crate::service::{NetworkMessage, OutgoingMessage}; use crate::sync::SimpleSync; use crossbeam_channel::{unbounded as channel, Sender}; use eth2_libp2p::{ - rpc::{methods::GoodbyeReason, IncomingGossip, RPCRequest, RPCResponse, RequestId}, + behaviour::IncomingGossip, + rpc::{methods::GoodbyeReason, RPCRequest, RPCResponse, RequestId}, PeerId, RPCEvent, }; use futures::future; diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 37c2c4c260..4c08a68710 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,6 +1,7 @@ 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; From 05369df7e8a8d82a875224b4f4dea242723081ee Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 25 Mar 2019 22:00:11 +1100 Subject: [PATCH 074/191] 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 075/191] 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 076/191] 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 077/191] 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 078/191] 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 079/191] 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 080/191] 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 081/191] 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 082/191] 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 083/191] 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 084/191] 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 085/191] 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 086/191] 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 087/191] 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 1584469b7c754da9c45f36bf944ea14a21292da7 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Tue, 26 Mar 2019 17:41:43 +1100 Subject: [PATCH 088/191] Renamed attestation -> attestation_data for fetch, but not publish, to acknowledge the difference in the spec. Also started implementing the gRPC get_attestation_data functionality in the BeaconNode. --- .../validator_harness/direct_beacon_node.rs | 2 +- beacon_node/rpc/src/beacon_attester.rs | 59 +++++++++++++------ eth2/attester/src/lib.rs | 2 +- .../src/test_utils/simulated_beacon_node.rs | 2 +- eth2/attester/src/traits.rs | 2 +- protos/src/services.proto | 12 ++-- .../attestation_grpc_client.rs | 6 +- 7 files changed, 53 insertions(+), 32 deletions(-) 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 fde8211ab5..17630833bb 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,7 +50,7 @@ impl DirectBeaconNode { } impl AttesterBeaconNode for DirectBeaconNode { - fn produce_attestation( + fn produce_attestation_data( &self, _slot: Slot, shard: u64, diff --git a/beacon_node/rpc/src/beacon_attester.rs b/beacon_node/rpc/src/beacon_attester.rs index 36b6a40b24..88caacdd0b 100644 --- a/beacon_node/rpc/src/beacon_attester.rs +++ b/beacon_node/rpc/src/beacon_attester.rs @@ -1,45 +1,66 @@ +use crate::beacon_chain::BeaconChain; use futures::Future; -use grpcio::{RpcContext, UnarySink}; +use grpcio::{RpcContext, UnarySink, RpcStatus, RpcStatusCode}; use protos::services::{ - Attestation as AttestationProto, ProduceAttestation, ProduceAttestationResponse, - ProduceAttestationRequest, PublishAttestationResponse, PublishAttestationRequest, + AttestationData as AttestationDataProto, ProduceAttestationData, ProduceAttestationDataResponse, + ProduceAttestationDataRequest, PublishAttestationResponse, PublishAttestationRequest, PublishAttestation }; use protos::services_grpc::BeaconBlockService; -use slog::Logger; +use slog::{Logger, info, warn, error}; #[derive(Clone)] pub struct AttestationServiceInstance { + pub chain: Arc, pub log: Logger, } impl AttestationService for AttestationServiceInstance { - /// Produce a `BeaconBlock` for signing by a validator. - fn produce_attestation( + /// Produce the `AttestationData` for signing by a validator. + fn produce_attestation_data( &mut self, ctx: RpcContext, - req: ProduceAttestationRequest, - sink: UnarySink, + req: ProduceAttestationDataRequest, + sink: UnarySink, ) { - println!("producing attestation at slot {}", req.get_slot()); + info!(&self.log, "Attempting to produce 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()); + // get the chain spec & state + let spec = self.chain.get_spec(); + let state = self.chain.get_state(); - let mut resp = ProduceAttestationResponse::new(); - resp.set_attestation_data(attestation); + // Start by performing some checks + // Check that the the AttestionData is for the current slot (otherwise it will not be valid) + if req.get_slot() != state.slot { + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::OutOfRange, + "AttestationData request for a slot that is not the current slot." + )) + .map_err(move |e| error!(&self.log, "Failed to reply with failure {:?}: {:?}", req, e)); + } + + // Then collect the data we need for the AttesatationData object + //let beacon_block_root = state.latest_block_roots.first().ok_or_else(|e| ) + + // And finally build the AttestationData object + let mut attestation_data = AttestationDataProto::new(); + attestation_data.set_slot(state.slot.as_u64()); + attestation_data.set_shard(spec.genesis_start_shard); + attestation_data.set_beacon_block_root(b"cats".to_vec()); + //attestation_data. + + let mut resp = ProduceAttestationDataResponse::new(); + resp.set_attestation_data(attestation_data); let f = sink .success(resp) - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + .map_err(move |e| error!("Failed to reply with success {:?}: {:?}", req, e)); ctx.spawn(f) } - /// Accept some fully-formed `BeaconBlock`, process and publish it. + /// Accept some fully-formed `FreeAttestation` from the validator, + /// store it, and aggregate it into an `Attestation`. fn publish_attestation( &mut self, ctx: RpcContext, diff --git a/eth2/attester/src/lib.rs b/eth2/attester/src/lib.rs index 065fdc9231..3f13555e3d 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(slot, shard)? { + let attestation_data = match self.beacon_node.produce_attestation_data(slot, shard)? { Some(attestation_data) => attestation_data, None => return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(slot)), }; diff --git a/eth2/attester/src/test_utils/simulated_beacon_node.rs b/eth2/attester/src/test_utils/simulated_beacon_node.rs index 84a203cdb5..d19f434223 100644 --- a/eth2/attester/src/test_utils/simulated_beacon_node.rs +++ b/eth2/attester/src/test_utils/simulated_beacon_node.rs @@ -26,7 +26,7 @@ impl SimulatedBeaconNode { } impl BeaconNode for SimulatedBeaconNode { - fn produce_attestation(&self, slot: Slot, shard: u64) -> ProduceResult { + fn produce_attestation_data(&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(), diff --git a/eth2/attester/src/traits.rs b/eth2/attester/src/traits.rs index 749c6e1a2a..2fd6940af2 100644 --- a/eth2/attester/src/traits.rs +++ b/eth2/attester/src/traits.rs @@ -14,7 +14,7 @@ pub enum PublishOutcome { /// Defines the methods required to produce and publish blocks on a Beacon Node. pub trait BeaconNode: Send + Sync { - fn produce_attestation( + fn produce_attestation_data( &self, slot: Slot, shard: u64, diff --git a/protos/src/services.proto b/protos/src/services.proto index 80d512c54a..35b9c32af1 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -33,7 +33,7 @@ service ValidatorService { /// Service that handles validator attestations service AttestationService { - rpc ProduceAttestation(ProduceAttestationRequest) returns (ProduceAttestationResponse); + rpc ProduceAttestation(ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); rpc PublishAttestation(PublishAttestationRequest) returns (PublishAttestationResponse); } @@ -138,13 +138,13 @@ message ProposeBlockSlotResponse { * Attestation Service Messages */ -message ProduceAttestationRequest { +message ProduceAttestationDataRequest { uint64 slot = 1; uint64 shard = 2; } -message ProduceAttestationResponse { - Attestation attestation_data = 1; +message ProduceAttestationDataResponse { + AttestationData attestation_data = 1; } message PublishAttestationRequest { @@ -162,7 +162,7 @@ message Crosslink { } -message Attestation { +message AttestationData { uint64 slot = 1; uint64 shard = 2; bytes beacon_block_root = 3; @@ -175,7 +175,7 @@ message Attestation { } message FreeAttestation { - Attestation attestation_data = 1; + AttestationData 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 5a4701ba93..f55acc4f71 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::ProduceAttestationRequest; +use protos::services::ProduceAttestationDataRequest; use types::{AttestationData, FreeAttestation, Slot}; pub struct AttestationGrpcClient { @@ -16,12 +16,12 @@ impl AttestationGrpcClient { } impl BeaconNode for AttestationGrpcClient { - fn produce_attestation( + fn produce_attestation_data( &self, slot: Slot, shard: u64, ) -> Result, BeaconNodeError> { - let mut req = ProduceAttestationRequest::new(); + let mut req = ProduceAttestationDataRequest::new(); req.set_slot(slot.as_u64()); req.set_shard(shard); From 99dbed86f150e27aed61206c2683553183b56d43 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 26 Mar 2019 18:20:01 +1100 Subject: [PATCH 089/191] types: PendingAttestation::from_attestation --- eth2/state_processing/src/per_block_processing.rs | 8 +------- eth2/types/src/pending_attestation.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index e79f5f08c3..6c52a26764 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -322,13 +322,7 @@ pub fn process_attestations( // Update the state in series. for attestation in attestations { - let pending_attestation = PendingAttestation { - data: attestation.data.clone(), - aggregation_bitfield: attestation.aggregation_bitfield.clone(), - custody_bitfield: attestation.custody_bitfield.clone(), - inclusion_slot: state.slot, - }; - + let pending_attestation = PendingAttestation::from_attestation(attestation, state.slot); let attestation_epoch = attestation.data.slot.epoch(spec.slots_per_epoch); if attestation_epoch == state.current_epoch(spec) { diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index ca50b6d1c4..938e59beff 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -1,5 +1,5 @@ use crate::test_utils::TestRandom; -use crate::{AttestationData, Bitfield, Slot}; +use crate::{Attestation, AttestationData, Bitfield, Slot}; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode, TreeHash}; @@ -16,6 +16,18 @@ pub struct PendingAttestation { pub inclusion_slot: Slot, } +impl PendingAttestation { + /// Create a `PendingAttestation` from an `Attestation`, at the given `inclusion_slot`. + pub fn from_attestation(attestation: &Attestation, inclusion_slot: Slot) -> Self { + PendingAttestation { + data: attestation.data.clone(), + aggregation_bitfield: attestation.aggregation_bitfield.clone(), + custody_bitfield: attestation.custody_bitfield.clone(), + inclusion_slot, + } + } +} + #[cfg(test)] mod tests { use super::*; From 033ae1b7478c98b3f0828001e2cbddc92d80ce97 Mon Sep 17 00:00:00 2001 From: Kirk Baird Date: Tue, 26 Mar 2019 18:28:29 +1100 Subject: [PATCH 090/191] WIP begin testing --- eth2/state_processing/tests/tests.rs | 48 +++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/eth2/state_processing/tests/tests.rs b/eth2/state_processing/tests/tests.rs index 39882cafbe..0363da8101 100644 --- a/eth2/state_processing/tests/tests.rs +++ b/eth2/state_processing/tests/tests.rs @@ -1,4 +1,7 @@ +use state_processing::{per_block_processing, per_block_processing_without_verifying_block_signature, per_slot_processing}; use serde_derive::Deserialize; +use serde_yaml; +use std::{fs::File, io::prelude::*, path::PathBuf}; use types::*; #[allow(unused_imports)] use yaml_utils; @@ -21,10 +24,7 @@ pub struct TestDoc { } #[test] -fn yaml() { - use serde_yaml; - use std::{fs::File, io::prelude::*, path::PathBuf}; - +fn test_read_yaml() { // Test sanity-check_small-config_32-vals.yaml let mut file = { let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -57,3 +57,43 @@ fn yaml() { let _doc: TestDoc = serde_yaml::from_str(&yaml_str.as_str()).unwrap(); } + +#[test] +fn run_state_transition_tests_small() { + // Test sanity-check_small-config_32-vals.yaml + let mut file = { + let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + file_path_buf.push("yaml_utils/specs/sanity-check_small-config_32-vals.yaml"); + + File::open(file_path_buf).unwrap() + }; + let mut yaml_str = String::new(); + file.read_to_string(&mut yaml_str).unwrap(); + yaml_str = yaml_str.to_lowercase(); + + let doc: TestDoc = serde_yaml::from_str(&yaml_str.as_str()).unwrap(); + + // Run Tests + for (i, test_case) in doc.test_cases.iter().enumerate() { + let mut state = test_case.initial_state.clone(); + for block in test_case.blocks.iter() { + while block.slot > state.slot { + let latest_block_header = state.latest_block_header.clone(); + let res = per_slot_processing(&mut state, &latest_block_header, &test_case.config).unwrap(); + } + if test_case.verify_signatures { + let res = per_block_processing(&mut state, &block, &test_case.config); + if res.is_err() { + println!("{:?}", i); + println!("{:?}", res); + }; + } else { + let res = per_block_processing_without_verifying_block_signature(&mut state, &block, &test_case.config); + if res.is_err() { + println!("{:?}", i); + println!("{:?}", res); + } + } + } + } +} From e5a3b3dd067773ae1c31d9bfa27c888e9505b669 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 26 Mar 2019 18:29:02 +1100 Subject: [PATCH 091/191] op-pool: attestation tests --- eth2/operation_pool/src/lib.rs | 290 ++++++++++++++++++++++++++++++++- 1 file changed, 289 insertions(+), 1 deletion(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index fadbf449d4..e67a201c76 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -144,6 +144,11 @@ impl OperationPool { Ok(()) } + /// Total number of attestations in the pool, including attestations for the same data. + pub fn num_attestations(&self) -> usize { + self.attestations.values().map(|atts| atts.len()).sum() + } + /// Get a list of attestations for inclusion in a block. pub fn get_attestations(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { // Attestations for the current fork, which may be from the current or previous epoch. @@ -161,6 +166,7 @@ impl OperationPool { // That are valid... .filter(|attestation| validate_attestation(state, attestation, spec).is_ok()) // Scored by the number of new attestations they introduce (descending) + // TODO: need to consider attestations introduced in THIS block .map(|att| (att, attestation_score(att, state))) .sorted_by_key(|&(_, score)| std::cmp::Reverse(score)) // Limited to the maximum number of attestations per block @@ -484,7 +490,7 @@ fn prune_validator_hash_map( mod tests { use super::DepositInsertStatus::*; use super::*; - use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; + use types::test_utils::*; use types::*; #[test] @@ -636,5 +642,287 @@ mod tests { (spec, state) } + /// Create a signed attestation for use in tests. + /// Signed by all validators in `committee[signing_range]` and `committee[extra_signer]`. + fn signed_attestation>( + committee: &CrosslinkCommittee, + keypairs: &[Keypair], + signing_range: R, + slot: Slot, + state: &BeaconState, + spec: &ChainSpec, + extra_signer: Option, + ) -> Attestation { + let mut builder = TestingAttestationBuilder::new( + state, + &committee.committee, + slot, + committee.shard, + spec, + ); + let signers = &committee.committee[signing_range]; + let committee_keys = signers.iter().map(|&i| &keypairs[i].sk).collect::>(); + builder.sign(signers, &committee_keys, &state.fork, spec); + extra_signer.map(|c_idx| { + let validator_index = committee.committee[c_idx]; + builder.sign( + &[validator_index], + &[&keypairs[validator_index].sk], + &state.fork, + spec, + ) + }); + builder.build() + } + + /// Test state for attestation-related tests. + fn attestation_test_state( + spec: &ChainSpec, + num_committees: usize, + ) -> (BeaconState, Vec) { + let num_validators = + num_committees * (spec.slots_per_epoch * spec.target_committee_size) as usize; + let mut state_builder = + TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(num_validators, spec); + let slot_offset = 100000; + let slot = spec.genesis_slot + slot_offset; + state_builder.teleport_to_slot(slot, spec); + state_builder.build_caches(spec).unwrap(); + state_builder.build() + } + + /// Set the latest crosslink in the state to match the attestation. + fn fake_latest_crosslink(att: &Attestation, state: &mut BeaconState, spec: &ChainSpec) { + state.latest_crosslinks[att.data.shard as usize] = Crosslink { + crosslink_data_root: att.data.crosslink_data_root, + epoch: att.data.slot.epoch(spec.slots_per_epoch), + }; + } + + #[test] + fn test_attestation_score() { + let spec = &ChainSpec::foundation(); + let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); + let slot = state.slot - 1; + let committees = state + .get_crosslink_committees_at_slot(slot, spec) + .unwrap() + .clone(); + + for committee in committees { + let att1 = signed_attestation(&committee, keypairs, ..2, slot, state, spec, None); + let att2 = signed_attestation(&committee, keypairs, .., slot, state, spec, None); + + assert_eq!( + att1.aggregation_bitfield.num_set_bits(), + attestation_score(&att1, state) + ); + + state + .current_epoch_attestations + .push(PendingAttestation::from_attestation(&att1, state.slot)); + + assert_eq!( + committee.committee.len() - 2, + attestation_score(&att2, &state) + ); + } + } + + /// End-to-end test of basic attestation handling. + #[test] + fn attestation_aggregation_insert_get_prune() { + let spec = &ChainSpec::foundation(); + let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); + let mut op_pool = OperationPool::new(); + + let slot = state.slot - 1; + let committees = state + .get_crosslink_committees_at_slot(slot, spec) + .unwrap() + .clone(); + + assert_eq!( + committees.len(), + 1, + "we expect just one committee with this many validators" + ); + + for committee in &committees { + let step_size = 2; + for i in (0..committee.committee.len()).step_by(step_size) { + let att = signed_attestation( + committee, + keypairs, + i..i + step_size, + slot, + state, + spec, + None, + ); + fake_latest_crosslink(&att, state, spec); + op_pool.insert_attestation(att, state, spec).unwrap(); + } + } + + assert_eq!(op_pool.attestations.len(), committees.len()); + assert_eq!(op_pool.num_attestations(), committees.len()); + + // Before the min attestation inclusion delay, get_attestations shouldn't return anything. + assert_eq!(op_pool.get_attestations(state, spec).len(), 0); + + // Then once the delay has elapsed, we should get a single aggregated attestation. + state.slot += spec.min_attestation_inclusion_delay; + + let block_attestations = op_pool.get_attestations(state, spec); + assert_eq!(block_attestations.len(), committees.len()); + + let agg_att = &block_attestations[0]; + assert_eq!( + agg_att.aggregation_bitfield.num_set_bits(), + spec.target_committee_size as usize + ); + + // Prune attestations shouldn't do anything at this point. + op_pool.prune_attestations(state, spec); + assert_eq!(op_pool.num_attestations(), committees.len()); + + // But once we advance to an epoch after the attestation, it should prune it out of + // existence. + state.slot = slot + spec.slots_per_epoch; + op_pool.prune_attestations(state, spec); + assert_eq!(op_pool.num_attestations(), 0); + } + + /// Adding an attestation already in the pool should not increase the size of the pool. + #[test] + fn attestation_duplicate() { + let spec = &ChainSpec::foundation(); + let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); + let mut op_pool = OperationPool::new(); + + let slot = state.slot - 1; + let committees = state + .get_crosslink_committees_at_slot(slot, spec) + .unwrap() + .clone(); + + for committee in &committees { + let att = signed_attestation(committee, keypairs, .., slot, state, spec, None); + fake_latest_crosslink(&att, state, spec); + op_pool + .insert_attestation(att.clone(), state, spec) + .unwrap(); + op_pool.insert_attestation(att, state, spec).unwrap(); + } + + assert_eq!(op_pool.num_attestations(), committees.len()); + } + + /// Adding lots of attestations that only intersect pairwise should lead to two aggregate + /// attestations. + #[test] + fn attestation_pairwise_overlapping() { + let spec = &ChainSpec::foundation(); + let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); + let mut op_pool = OperationPool::new(); + + let slot = state.slot - 1; + let committees = state + .get_crosslink_committees_at_slot(slot, spec) + .unwrap() + .clone(); + + let step_size = 2; + for committee in &committees { + // Create attestations that overlap on `step_size` validators, like: + // {0,1,2,3}, {2,3,4,5}, {4,5,6,7}, ... + for i in (0..committee.committee.len() - step_size).step_by(step_size) { + let att = signed_attestation( + committee, + keypairs, + i..i + 2 * step_size, + slot, + state, + spec, + None, + ); + fake_latest_crosslink(&att, state, spec); + op_pool.insert_attestation(att, state, spec).unwrap(); + } + } + + // The attestations should get aggregated into two attestations that comprise all + // validators. + assert_eq!(op_pool.attestations.len(), committees.len()); + assert_eq!(op_pool.num_attestations(), 2 * committees.len()); + } + + /// Create a bunch of attestations signed by a small number of validators, and another + /// bunch signed by a larger number, such that there are at least `max_attestations` + /// signed by the larger number. Then, check that `get_attestations` only returns the + /// high-quality attestations. To ensure that no aggregation occurs, ALL attestations + /// are also signed by the 0th member of the committee. + #[test] + fn attestation_get_max() { + let spec = &ChainSpec::foundation(); + let small_step_size = 2; + let big_step_size = 4; + let (ref mut state, ref keypairs) = attestation_test_state(spec, big_step_size); + let mut op_pool = OperationPool::new(); + + let slot = state.slot - 1; + let committees = state + .get_crosslink_committees_at_slot(slot, spec) + .unwrap() + .clone(); + + let max_attestations = spec.max_attestations as usize; + let target_committee_size = spec.target_committee_size as usize; + + let mut insert_attestations = |committee, step_size| { + for i in (0..target_committee_size).step_by(step_size) { + let att = signed_attestation( + committee, + keypairs, + i..i + step_size, + slot, + state, + spec, + if i == 0 { None } else { Some(0) }, + ); + fake_latest_crosslink(&att, state, spec); + op_pool.insert_attestation(att, state, spec).unwrap(); + } + }; + + for committee in &committees { + assert_eq!(committee.committee.len(), target_committee_size); + // Attestations signed by only 2-3 validators + insert_attestations(committee, small_step_size); + // Attestations signed by 4+ validators + insert_attestations(committee, big_step_size); + } + + let num_small = target_committee_size / small_step_size; + let num_big = target_committee_size / big_step_size; + + assert_eq!(op_pool.attestations.len(), committees.len()); + assert_eq!( + op_pool.num_attestations(), + (num_small + num_big) * committees.len() + ); + assert!(op_pool.num_attestations() > max_attestations); + + state.slot += spec.min_attestation_inclusion_delay; + let best_attestations = op_pool.get_attestations(state, spec); + assert_eq!(best_attestations.len(), max_attestations); + + // All the best attestations should be signed by at least `big_step_size` (4) validators. + for att in &best_attestations { + assert!(att.aggregation_bitfield.num_set_bits() >= big_step_size); + } + } + // TODO: more tests } From c27fdbe37fbceab145e6b4e99844705a7e630c47 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 27 Mar 2019 10:28:27 +1100 Subject: [PATCH 092/191] 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 093/191] 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 094/191] 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 095/191] 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 096/191] 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 097/191] 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 098/191] 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 099/191] 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 c9a7977d6998183458ab7a5d365ac08d8fa69257 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Wed, 27 Mar 2019 14:30:09 +1100 Subject: [PATCH 100/191] Renamed some functions, trying to get beaconnode attestation stuff to work. --- beacon_node/beacon_chain/src/beacon_chain.rs | 2 +- beacon_node/rpc/src/beacon_attester.rs | 17 +++++++---------- protos/src/services.proto | 2 +- .../attester_service/attestation_grpc_client.rs | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 61b5fb58b4..110d1a99df 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -280,7 +280,7 @@ where } /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. - pub fn produce_attestation(&self, shard: u64) -> Result { + pub fn produce_attestation_data(&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( diff --git a/beacon_node/rpc/src/beacon_attester.rs b/beacon_node/rpc/src/beacon_attester.rs index 88caacdd0b..4166c30d4d 100644 --- a/beacon_node/rpc/src/beacon_attester.rs +++ b/beacon_node/rpc/src/beacon_attester.rs @@ -9,6 +9,8 @@ use protos::services::{ use protos::services_grpc::BeaconBlockService; use slog::{Logger, info, warn, error}; +const TEST_SHARD_PHASE_ZERO: u8 = 0; + #[derive(Clone)] pub struct AttestationServiceInstance { pub chain: Arc, @@ -29,9 +31,11 @@ impl AttestationService for AttestationServiceInstance { let spec = self.chain.get_spec(); let state = self.chain.get_state(); + let slot_requested = req.get_slot(); + // Start by performing some checks // Check that the the AttestionData is for the current slot (otherwise it will not be valid) - if req.get_slot() != state.slot { + if slot_requested != state.slot { let f = sink .fail(RpcStatus::new( RpcStatusCode::OutOfRange, @@ -40,15 +44,8 @@ impl AttestationService for AttestationServiceInstance { .map_err(move |e| error!(&self.log, "Failed to reply with failure {:?}: {:?}", req, e)); } - // Then collect the data we need for the AttesatationData object - //let beacon_block_root = state.latest_block_roots.first().ok_or_else(|e| ) - - // And finally build the AttestationData object - let mut attestation_data = AttestationDataProto::new(); - attestation_data.set_slot(state.slot.as_u64()); - attestation_data.set_shard(spec.genesis_start_shard); - attestation_data.set_beacon_block_root(b"cats".to_vec()); - //attestation_data. + // Then get the AttestationData from the beacon chain (for shard 0 for now) + let attestation_data = self.chain.produce_attestation_data(TEST_SHARD_PHASE_ZERO); let mut resp = ProduceAttestationDataResponse::new(); resp.set_attestation_data(attestation_data); diff --git a/protos/src/services.proto b/protos/src/services.proto index 35b9c32af1..1dfb53c06c 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -33,7 +33,7 @@ service ValidatorService { /// Service that handles validator attestations service AttestationService { - rpc ProduceAttestation(ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); + rpc ProduceAttestationData(ProduceAttestationDataRequest) returns (ProduceAttestationDataResponse); rpc PublishAttestation(PublishAttestationRequest) returns (PublishAttestationResponse); } diff --git a/validator_client/src/attester_service/attestation_grpc_client.rs b/validator_client/src/attester_service/attestation_grpc_client.rs index f55acc4f71..bfb7f67c60 100644 --- a/validator_client/src/attester_service/attestation_grpc_client.rs +++ b/validator_client/src/attester_service/attestation_grpc_client.rs @@ -27,7 +27,7 @@ impl BeaconNode for AttestationGrpcClient { let reply = self .client - .produce_attestation(&req) + .produce_attestation_data(&req) .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; // TODO: return correct Attestation From bc8ec51fe5235da040f4dbf608b152b776331abd Mon Sep 17 00:00:00 2001 From: Age Manning Date: Wed, 27 Mar 2019 15:41:51 +1100 Subject: [PATCH 101/191] 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 102/191] 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 103/191] 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 104/191] 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 105/191] 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 106/191] 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 107/191] 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 108/191] 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 bda381a2648fa21d420b14703bffa55539fc44d7 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 28 Mar 2019 09:38:39 +1100 Subject: [PATCH 109/191] More progress towards getting the attester working. --- eth2/attester/src/lib.rs | 2 + validator_client/src/service.rs | 164 +++++++++++++++++--------------- 2 files changed, 87 insertions(+), 79 deletions(-) diff --git a/eth2/attester/src/lib.rs b/eth2/attester/src/lib.rs index 3f13555e3d..7b1d261456 100644 --- a/eth2/attester/src/lib.rs +++ b/eth2/attester/src/lib.rs @@ -99,6 +99,8 @@ impl Attester return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(slot)), }; + dbg!(&attestation_data); + if !self.safe_to_produce(&attestation_data) { return Ok(PollOutcome::SlashableAttestationNotProduced(slot)); } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 8a7e90d104..4c6a499973 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -25,9 +25,12 @@ use tokio::runtime::Builder; use tokio::timer::Interval; use tokio_timer::clock::Clock; use types::{Epoch, Fork, Slot}; +use std::thread; //TODO: This service should be simplified in the future. Can be made more steamlined. +const POLL_INTERVAL_MILLIS: u64 = 100; + /// The validator service. This is the main thread that executes and maintains validator /// duties. pub struct Service { @@ -217,7 +220,10 @@ 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 = match config.fetch_keys(&log) { + Some(kps) => kps, + None => panic!("No key pairs found, cannot start validator client without. Try running ./account_manager generate first.") + }; // build requisite objects to pass to core thread. let duties_map = Arc::new(EpochDutiesMap::new(config.spec.slots_per_epoch)); @@ -272,87 +278,87 @@ impl Service { Ok(()) })) .map_err(|e| format!("Service thread failed: {:?}", e))?; + + let mut threads = vec![]; + + for keypair in keypairs { + info!(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 slot_clock = service.slot_clock.clone(); + let log = log.clone(); + let attester_grpc_client = Arc::new(AttestationGrpcClient::new(attester_client.clone())); + thread::spawn(move || { + let attester = Attester::new(epoch_map_for_attester, slot_clock, attester_grpc_client, signer); + let mut attester_service = AttesterService { + attester, + poll_interval_millis, + log, + }; + + attester_service.run(); + }) + }; + + //threads.push((duties_manager_thread, producer_thread, attester_thread)); + threads.push((attester_thread)); + } + 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; From 4caaf82892378d05c1c45d9f166a65729c08c4d8 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Thu, 28 Mar 2019 13:14:41 +1100 Subject: [PATCH 110/191] 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 111/191] 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 112/191] 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 113/191] 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 114/191] 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 115/191] 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 116/191] 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. From ba71e8adca2a6f1e899492bdcd776dc45f981cc3 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 28 Mar 2019 20:55:07 +1100 Subject: [PATCH 117/191] Merged age-validator-client into luke's changes on validator_client, and fixed all the merge conflicts. --- Jenkinsfile | 22 ++- beacon_node/Cargo.toml | 1 + beacon_node/beacon_chain/src/beacon_chain.rs | 23 +++ beacon_node/beacon_chain/src/initialise.rs | 20 +- beacon_node/client/Cargo.toml | 1 + beacon_node/client/src/lib.rs | 101 +++++++++- beacon_node/client/src/notifier.rs | 4 +- beacon_node/eth2-libp2p/src/behaviour.rs | 33 +++- beacon_node/network/src/sync/simple_sync.rs | 8 +- beacon_node/rpc/src/beacon_block.rs | 110 ++++++++--- beacon_node/rpc/src/beacon_chain.rs | 13 +- beacon_node/rpc/src/beacon_node.rs | 7 +- beacon_node/rpc/src/lib.rs | 1 + beacon_node/rpc/src/validator.rs | 135 ++++++++------ beacon_node/src/run.rs | 2 + eth2/attester/src/lib.rs | 3 +- .../src/per_slot_processing.rs | 1 - .../testing_beacon_state_builder.rs | 4 +- eth2/utils/slot_clock/src/lib.rs | 3 + .../slot_clock/src/system_time_slot_clock.rs | 39 +++- .../slot_clock/src/testing_slot_clock.rs | 6 + protos/src/services.proto | 13 +- .../beacon_block_grpc_client.rs | 38 +--- validator_client/src/config.rs | 3 +- validator_client/src/duties/grpc.rs | 7 +- validator_client/src/duties/mod.rs | 23 ++- validator_client/src/main.rs | 4 +- validator_client/src/service.rs | 173 ++++++++---------- 28 files changed, 540 insertions(+), 258 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 42755d5f7f..1a3afad87f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,20 +1,22 @@ pipeline { - agent { + agent { dockerfile { filename 'Dockerfile' args '-v cargo-cache:/cargocache:rw -e "CARGO_HOME=/cargocache"' } } - stages { - stage('Build') { - steps { - sh 'cargo build' - } - } - stage('Test') { + stages { + stage('Build') { steps { - sh 'cargo test --all' + sh 'cargo build --verbose --all' + sh 'cargo build --verbose --all --release' } } - } + stage('Test') { + steps { + sh 'cargo test --verbose --all' + sh 'cargo test --verbose --all --release' + } + } + } } 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 9323d1334e..a9a28ea397 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -342,8 +342,17 @@ 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)?; } + 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(()) } @@ -405,6 +414,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/beacon_chain/src/initialise.rs b/beacon_node/beacon_chain/src/initialise.rs index 7d3c87965f..0951e06fbb 100644 --- a/beacon_node/beacon_chain/src/initialise.rs +++ b/beacon_node/beacon_chain/src/initialise.rs @@ -28,15 +28,19 @@ 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); 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()); @@ -65,15 +69,19 @@ 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); 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/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..807fd9301e 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -8,12 +8,20 @@ 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::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 +34,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 +52,35 @@ 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_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, + "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 let network_config = &config.net_conf; @@ -65,13 +104,73 @@ 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")); + executor.spawn( + exit.until( + interval + .for_each(move |_| { + do_state_catchup(&chain, &log); + + 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, }) } } + +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/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/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 41b7c89657..2865971838 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; } @@ -192,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), @@ -220,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)) } @@ -232,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); + } +} 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), ); diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 4e1875665b..f6b426c18f 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; @@ -10,10 +11,14 @@ use protos::services::{ }; use protos::services_grpc::BeaconBlockService; use slog::Logger; -use types::{Hash256, Slot}; +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, } @@ -30,8 +35,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,26 +53,88 @@ impl BeaconBlockService for BeaconBlockServiceInstance { req: PublishBeaconBlockRequest, sink: UnarySink, ) { - 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, - }); - - // TODO: actually process the block. let mut resp = PublishBeaconBlockResponse::new(); - resp.set_success(true); + + let ssz_serialized_block = req.get_block().get_ssz(); + + match BeaconBlock::ssz_decode(ssz_serialized_block, 0) { + Ok((block, _i)) => { + let block_root = Hash256::from_slice(&block.hash_tree_root()[..]); + + match self.chain.process_block(block.clone()) { + Ok(outcome) => { + if outcome.sucessfully_processed() { + // Block was successfully processed. + info!( + self.log, + "PublishBeaconBlock"; + "type" => "valid_block", + "block_slot" => block.slot, + "outcome" => format!("{:?}", outcome) + ); + + // 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. + warn!( + self.log, + "PublishBeaconBlock"; + "type" => "unable_to_import", + "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()); + } + } + + resp.set_success(true); + } + Err(_) => { + resp.set_success(false); + resp.set_msg(b"Invalid SSZ".to_vec()); + } + }; let f = sink .success(resp) 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/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 7b00f04f48..c92ab66162 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 @@ -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/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 20cd62b1df..2d47b4a69a 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(), }; diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 47886a9df6..936c95f52f 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, info, 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(e) => { + // could not get the validator proposer index + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + 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); + } + }; // 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; + continue; } // 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); + continue; + } + // 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/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/attester/src/lib.rs b/eth2/attester/src/lib.rs index 8dd83fa041..53d81e8974 100644 --- a/eth2/attester/src/lib.rs +++ b/eth2/attester/src/lib.rs @@ -90,8 +90,7 @@ impl Attester { aggregate_signature: agg_sig, }; - self.beacon_node - .publish_attestation(attestation)?; + self.beacon_node.publish_attestation(attestation)?; Ok(PollOutcome::AttestationProduced(attestation_duty.slot)) } 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; 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..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,8 +120,10 @@ impl TestingBeaconStateBuilder { }) .collect(); + let genesis_time = 1553753928; // arbitrary + let mut state = BeaconState::genesis( - 0, + genesis_time, Eth1Data { deposit_root: Hash256::zero(), block_hash: Hash256::zero(), 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 99f051985f..4dfc6b37da 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,11 +47,17 @@ 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))), } } + + fn duration_to_next_slot(&self) -> Result, Error> { + duration_to_next_slot(self.genesis_seconds, self.slot_duration_seconds) + } } impl From for Error { @@ -62,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 { @@ -74,6 +107,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 +115,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/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)] diff --git a/protos/src/services.proto b/protos/src/services.proto index bdb311fd63..b61e5aac0f 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,11 +40,12 @@ service AttestationService { /* * Beacon Node Service Message */ -message NodeInfo { +message NodeInfoResponse { string version = 1; Fork fork = 2; uint32 chain_id = 3; uint64 genesis_time = 4; + uint64 genesis_slot = 5; } message Fork { @@ -53,8 +54,7 @@ message Fork { uint64 epoch = 3; } -message Empty { -} +message Empty {} /* @@ -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); 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/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 4bb76e14c6..041d41adde 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, Keypair, Slot}; impl BeaconNode for ValidatorServiceClient { @@ -21,6 +23,9 @@ impl BeaconNode for ValidatorServiceClient { validators.set_public_keys(signers.iter().map(|v| ssz_encode(&v.pk)).collect()); req.set_validators(validators); + // set a timeout for requests + // let call_opt = CallOption::default().timeout(Duration::from_secs(2)); + // send the request, get the duties reply let reply = self .get_validator_duties(&req) @@ -31,7 +36,7 @@ impl BeaconNode for ValidatorServiceClient { if !validator_duty.has_duty() { // validator is inactive epoch_duties.insert(signers[index].clone(), None); - break; + continue; } // active validator let active_duty = validator_duty.get_duty(); diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index e239ca7952..9d92215928 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -52,20 +52,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.signers)?; - // 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/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(); diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index c2caed59df..86f310a06d 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -25,6 +25,7 @@ use tokio::prelude::*; use tokio::runtime::Builder; use tokio::timer::Interval; use tokio_timer::clock::Clock; +use types::test_utils::generate_deterministic_keypairs; use types::{Epoch, Fork, Slot}; use std::thread; @@ -45,8 +46,6 @@ pub struct Service { slot_clock: SystemTimeSlotClock, /// The current slot we are processing. current_slot: Slot, - /// Duration until the next slot. This is used for initializing the tokio timer interval. - duration_to_next_slot: Duration, /// The number of slots per epoch to allow for converting slots to epochs. slots_per_epoch: u64, // GRPC Clients @@ -107,6 +106,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); @@ -142,46 +142,21 @@ impl Service { }; // build the validator slot clock - let slot_clock = SystemTimeSlotClock::new(genesis_time, config.spec.seconds_per_slot) - .expect("Unable to instantiate SystemTimeSlotClock."); + let slot_clock = + SystemTimeSlotClock::new(genesis_slot, genesis_time, config.spec.seconds_per_slot) + .expect("Unable to instantiate SystemTimeSlotClock."); let current_slot = slot_clock .present_slot() .map_err(|e| ErrorKind::SlotClockError(e))? .expect("Genesis must be in the future"); - // calculate the duration to the next slot - let duration_to_next_slot = { - let seconds_per_slot = config.spec.seconds_per_slot; - let syslot_time = SystemTime::now(); - let duration_since_epoch = syslot_time - .duration_since(SystemTime::UNIX_EPOCH) - .map_err(|e| ErrorKind::SystemTimeError(e.to_string()))?; - let duration_since_genesis = duration_since_epoch - .checked_sub(Duration::from_secs(genesis_time)) - .expect("Genesis must be in the future. Checked on connection"); - let elapsed_slots = duration_since_epoch - .as_secs() - .checked_div(seconds_per_slot as u64) - .expect("Seconds per slot should not be 0"); - - // the duration to the next slot - Duration::from_secs( - (elapsed_slots + 1) - .checked_mul(seconds_per_slot) - .expect("Next slot time should not overflow u64"), - ) - .checked_sub(duration_since_genesis) - .expect("This should never saturate") - }; - Ok(Self { connected_node_version: node_info.version, chain_id: node_info.chain_id as u16, fork, slot_clock, current_slot, - duration_to_next_slot, slots_per_epoch: config.spec.slots_per_epoch, beacon_block_client, validator_client, @@ -204,15 +179,18 @@ impl Service { .build() .map_err(|e| format!("Tokio runtime failed: {}", e))?; + let duration_to_next_slot = service + .slot_clock + .duration_to_next_slot() + .map_err(|e| format!("System clock error: {:?}", e))? + .expect("Cannot start before genesis"); + // set up the validator work interval - start at next slot and proceed every slot let interval = { // Set the interval to start at the next slot, and every slot after let slot_duration = Duration::from_secs(config.spec.seconds_per_slot); //TODO: Handle checked add correctly - Interval::new( - Instant::now() + service.duration_to_next_slot, - slot_duration, - ) + Interval::new(Instant::now() + duration_to_next_slot, slot_duration) }; /* kick off core service */ @@ -221,11 +199,14 @@ impl Service { // TODO: keypairs are randomly generated; they should be loaded from a file or generated. // https://github.com/sigp/lighthouse/issues/160 + /* In future, load generated keys from disk. let keypairs = match config.fetch_keys(&log.clone()) { Some(kps) => kps, None => panic!("No key pairs found, cannot start validator client without. Try running ./account_manager generate first.") }; + */ + let keypairs = Arc::new(generate_deterministic_keypairs(8)); /* build requisite objects to pass to core thread */ @@ -243,71 +224,75 @@ 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_log = log.clone(); - let cloned_manager = manager.clone(); - tokio::spawn(futures::future::poll_fn(move || { - cloned_manager.run_update(current_epoch.clone(), cloned_log.clone()) - })); - - /* execute any specified duties */ - - if let Some(work) = manager.get_current_work(current_slot) { - for (keypair, 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"); - let attester_grpc_client = - Arc::new( - AttestationGrpcClient::new( - service.attester_client.clone() - ) - ); - let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); - let attester = - Attester::new( - attester_grpc_client, - signer); - let mut attester_service = AttesterService { - attester, - poll_interval_millis: POLL_INTERVAL_MILLIS, - log: log.clone(), - }; - attester_service.run(); + 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 (keypair, 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"); + let attester_grpc_client = + Arc::new( + AttestationGrpcClient::new( + service.attester_client.clone() + ) + ); + let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); + let attester = + Attester::new( + attester_grpc_client, + signer); + let mut attester_service = AttesterService { + attester, + poll_interval_millis: POLL_INTERVAL_MILLIS, + log: log.clone(), + }; + attester_service.run(); + } } } - } - Ok(()) - })) - .map_err(|e| format!("Service thread failed: {:?}", e))?; + Ok(()) + }) + .map_err(|e| format!("Service thread failed: {:?}", e)), + ); // completed a slot process Ok(()) From 867af4bc6ad5efcaeba3d6891031c2bcb3997a8c Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 28 Mar 2019 21:00:38 +1100 Subject: [PATCH 118/191] Made the 'signers' an Arc, so that things compile. --- validator_client/src/duties/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 9d92215928..0b86e3ce93 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -42,7 +42,7 @@ pub struct DutiesManager { pub duties_map: RwLock, /// A list of all signer objects known to the validator service. // TODO: Generalise the signers, so that they're not just keypairs - pub signers: Vec, + pub signers: Arc>, pub beacon_node: Arc, } @@ -97,7 +97,7 @@ impl DutiesManager { // if the map is poisoned, return None let duties = self.duties_map.read().ok()?; - for validator_signer in &self.signers { + for validator_signer in self.signers.iter() { match duties.is_work_slot(slot, &validator_signer) { Ok(Some(work_type)) => current_work.push((validator_signer.clone(), work_type)), Ok(None) => {} // No work for this validator From 87acaac8a03b2735b20e64887c7d5e0a5627f55a Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 28 Mar 2019 21:01:47 +1100 Subject: [PATCH 119/191] Ran cargo fmt. --- .../test_harness/src/validator_harness/mod.rs | 8 ++-- eth2/attester/src/lib.rs | 37 +++++++++++-------- .../src/test_utils/simulated_beacon_node.rs | 2 +- eth2/attester/src/traits.rs | 2 +- eth2/block_proposer/src/lib.rs | 18 ++------- .../attestation_grpc_client.rs | 2 +- validator_client/src/duties/epoch_duties.rs | 2 +- validator_client/src/duties/mod.rs | 2 +- validator_client/src/service.rs | 16 +++----- 9 files changed, 38 insertions(+), 51 deletions(-) diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs index 074ede06a6..43ad03ea71 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs @@ -2,13 +2,13 @@ mod direct_beacon_node; mod direct_duties; mod local_signer; +use crate::direct_beacon_node::DirectBeaconNode; use attester::PollOutcome as AttestationPollOutcome; use attester::{Attester, Error as AttestationPollError}; use beacon_chain::BeaconChain; use block_proposer::PollOutcome as BlockPollOutcome; use block_proposer::{BlockProducer, Error as BlockPollError}; use db::MemoryDB; -use crate::direct_beacon_node::DirectBeaconNode; use fork_choice::BitwiseLMDGhost; use local_signer::LocalSigner; use slot_clock::TestingSlotClock; @@ -32,10 +32,8 @@ type TestingBlockProducer = BlockProducer< LocalSigner, >; -type TestingAttester = Attester< - DirectBeaconNode>, - LocalSigner, ->; +type TestingAttester = + Attester>, LocalSigner>; /// A `BlockProducer` and `Attester` which sign using a common keypair. /// diff --git a/eth2/attester/src/lib.rs b/eth2/attester/src/lib.rs index 53d81e8974..b5cdf5920b 100644 --- a/eth2/attester/src/lib.rs +++ b/eth2/attester/src/lib.rs @@ -3,8 +3,10 @@ mod traits; use ssz::TreeHash; use std::sync::Arc; -use types::{AttestationData, AttestationDataAndCustodyBit, Attestation, Signature, - AggregateSignature, Slot, AttestationDuty, Bitfield}; +use types::{ + AggregateSignature, Attestation, AttestationData, AttestationDataAndCustodyBit, + AttestationDuty, Bitfield, Signature, Slot, +}; pub use self::traits::{ BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, PublishOutcome, Signer, @@ -59,20 +61,28 @@ impl Attester { } impl Attester { - - fn produce_attestation(&mut self, attestation_duty: AttestationDuty) -> Result { - let attestation_data = match self.beacon_node.produce_attestation_data( - attestation_duty.slot, - attestation_duty.shard - )? { + fn produce_attestation( + &mut self, + attestation_duty: AttestationDuty, + ) -> Result { + let attestation_data = match self + .beacon_node + .produce_attestation_data(attestation_duty.slot, attestation_duty.shard)? + { Some(attestation_data) => attestation_data, - None => return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(attestation_duty.slot)), + None => { + return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation( + attestation_duty.slot, + )) + } }; dbg!(&attestation_data); if !self.safe_to_produce(&attestation_data) { - return Ok(PollOutcome::SlashableAttestationNotProduced(attestation_duty.slot)); + return Ok(PollOutcome::SlashableAttestationNotProduced( + attestation_duty.slot, + )); } let signature = match self.sign_attestation_data(&attestation_data) { @@ -82,7 +92,6 @@ impl Attester { let mut agg_sig = AggregateSignature::new(); agg_sig.add(&signature); - let attestation = Attestation { aggregation_bitfield: Bitfield::new(), data: attestation_data, @@ -172,10 +181,7 @@ mod tests { let attest_epoch = attest_slot / spec.slots_per_epoch; let attest_shard = 12; - let mut attester = Attester::new( - beacon_node.clone(), - signer.clone(), - ); + let mut attester = Attester::new(beacon_node.clone(), signer.clone()); // Configure responses from the BeaconNode. beacon_node.set_next_produce_result(Ok(Some(AttestationData::random_for_test(&mut rng)))); @@ -220,6 +226,5 @@ mod tests { Ok(PollOutcome::ProducerDutiesUnknown(slot)) ); */ - } } diff --git a/eth2/attester/src/test_utils/simulated_beacon_node.rs b/eth2/attester/src/test_utils/simulated_beacon_node.rs index 8e6c7c31d8..eed6a38b9e 100644 --- a/eth2/attester/src/test_utils/simulated_beacon_node.rs +++ b/eth2/attester/src/test_utils/simulated_beacon_node.rs @@ -1,6 +1,6 @@ use crate::traits::{BeaconNode, BeaconNodeError, PublishOutcome}; use std::sync::RwLock; -use types::{AttestationData, Attestation, Slot}; +use types::{Attestation, AttestationData, Slot}; type ProduceResult = Result, BeaconNodeError>; type PublishResult = Result; diff --git a/eth2/attester/src/traits.rs b/eth2/attester/src/traits.rs index 1c6d38578c..94d138ae07 100644 --- a/eth2/attester/src/traits.rs +++ b/eth2/attester/src/traits.rs @@ -1,4 +1,4 @@ -use types::{AttestationData, Attestation, Signature, Slot}; +use types::{Attestation, AttestationData, Signature, Slot}; #[derive(Debug, PartialEq, Clone)] pub enum BeaconNodeError { diff --git a/eth2/block_proposer/src/lib.rs b/eth2/block_proposer/src/lib.rs index 1d2a4af716..646391c383 100644 --- a/eth2/block_proposer/src/lib.rs +++ b/eth2/block_proposer/src/lib.rs @@ -4,7 +4,7 @@ mod traits; use slot_clock::SlotClock; use ssz::{SignedRoot, TreeHash}; use std::sync::Arc; -use types::{BeaconBlock, ChainSpec, Domain, Slot, Fork}; +use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; pub use self::traits::{ BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, PublishOutcome, Signer, @@ -57,11 +57,7 @@ pub struct BlockProducer { impl BlockProducer { /// Returns a new instance where `last_processed_slot == 0`. - pub fn new( - spec: Arc, - beacon_node: Arc, - signer: Arc, - ) -> Self { + pub fn new(spec: Arc, beacon_node: Arc, signer: Arc) -> Self { Self { last_processed_slot: None, spec, @@ -72,7 +68,6 @@ impl BlockProducer { } impl BlockProducer { - /* No longer needed because we don't poll any more /// "Poll" to see if the validator is required to take any action. /// @@ -129,7 +124,6 @@ impl BlockProducer { /// The slash-protection code is not yet implemented. There is zero protection against /// slashing. fn produce_block(&mut self, slot: Slot, fork: Fork) -> Result { - let randao_reveal = { // TODO: add domain, etc to this message. Also ensure result matches `into_to_bytes32`. let message = slot.epoch(self.spec.slots_per_epoch).hash_tree_root(); @@ -238,12 +232,8 @@ mod tests { let beacon_node = Arc::new(SimulatedBeaconNode::default()); let signer = Arc::new(LocalSigner::new(Keypair::random())); - - let mut block_proposer = BlockProducer::new( - spec.clone(), - beacon_node.clone(), - signer.clone(), - ); + let mut block_proposer = + BlockProducer::new(spec.clone(), beacon_node.clone(), signer.clone()); // Configure responses from the BeaconNode. beacon_node.set_next_produce_result(Ok(Some(BeaconBlock::random_for_test(&mut rng)))); diff --git a/validator_client/src/attester_service/attestation_grpc_client.rs b/validator_client/src/attester_service/attestation_grpc_client.rs index 97ec058992..05173d8248 100644 --- a/validator_client/src/attester_service/attestation_grpc_client.rs +++ b/validator_client/src/attester_service/attestation_grpc_client.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; use protos::services::ProduceAttestationDataRequest; -use types::{AttestationData, Attestation, Slot}; +use types::{Attestation, AttestationData, Slot}; pub struct AttestationGrpcClient { client: Arc, diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 984dc6e00d..47c9d96c49 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -44,7 +44,7 @@ impl EpochDuty { slot, shard: self.attestation_shard, committee_index: self.committee_index as usize, - validator_index: self.validator_index as usize + validator_index: self.validator_index as usize, }); } diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 0b86e3ce93..de08f6ce7f 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -102,7 +102,7 @@ impl DutiesManager { Ok(Some(work_type)) => current_work.push((validator_signer.clone(), work_type)), Ok(None) => {} // No work for this validator //TODO: This should really log an error, as we shouldn't end up with an err here. - Err(_) => {} // Unknown epoch or validator, no work + Err(_) => {} // Unknown epoch or validator, no work } } if current_work.is_empty() { diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 86f310a06d..36da36a1f1 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -20,6 +20,7 @@ use slog::{debug, error, info, warn}; use slot_clock::{SlotClock, SystemTimeSlotClock}; use std::sync::Arc; use std::sync::RwLock; +use std::thread; use std::time::{Duration, Instant, SystemTime}; use tokio::prelude::*; use tokio::runtime::Builder; @@ -27,7 +28,6 @@ use tokio::timer::Interval; use tokio_timer::clock::Clock; use types::test_utils::generate_deterministic_keypairs; use types::{Epoch, Fork, Slot}; -use std::thread; //TODO: This service should be simplified in the future. Can be made more steamlined. @@ -268,17 +268,11 @@ impl Service { // available AttestationDuty info let attestation_duty = work_type.attestation_duty.expect("Cannot be None"); - let attester_grpc_client = - Arc::new( - AttestationGrpcClient::new( - service.attester_client.clone() - ) - ); + let attester_grpc_client = Arc::new(AttestationGrpcClient::new( + service.attester_client.clone(), + )); let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); - let attester = - Attester::new( - attester_grpc_client, - signer); + let attester = Attester::new(attester_grpc_client, signer); let mut attester_service = AttesterService { attester, poll_interval_millis: POLL_INTERVAL_MILLIS, From 6c8abd8990e552dc847205edf5b3dc2f33c5fcc9 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Fri, 29 Mar 2019 00:02:41 +1100 Subject: [PATCH 120/191] Fixed merge conflict fail. --- validator_client/src/duties/mod.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 569a99efa2..de08f6ce7f 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -51,11 +51,7 @@ impl DutiesManager { /// /// be a wall-clock (e.g., system time, remote server time, etc.). fn update(&self, epoch: Epoch) -> Result { -<<<<<<< HEAD let duties = self.beacon_node.request_duties(epoch, &self.signers)?; -======= - let duties = self.beacon_node.request_duties(epoch, &self.pubkeys)?; ->>>>>>> master { // 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) { From 1e760d6719acc9bae01a8d2be7af21785c05aa9e Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 00:43:53 +1100 Subject: [PATCH 121/191] Add Display for PublicKey --- eth2/utils/bls/src/public_key.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/eth2/utils/bls/src/public_key.rs b/eth2/utils/bls/src/public_key.rs index 5a348f530d..5c4c3204c5 100644 --- a/eth2/utils/bls/src/public_key.rs +++ b/eth2/utils/bls/src/public_key.rs @@ -7,6 +7,7 @@ use ssz::{ decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, }; use std::default; +use std::fmt; use std::hash::{Hash, Hasher}; /// A single BLS signature. @@ -54,6 +55,12 @@ impl PublicKey { } } +impl fmt::Display for PublicKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.concatenated_hex_id()) + } +} + impl default::Default for PublicKey { fn default() -> Self { let secret_key = SecretKey::random(); From 405ea619e2de512013e7a02449660554a8b7b4ae Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 00:45:39 +1100 Subject: [PATCH 122/191] Clean up validator output --- validator_client/src/service.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 83e7608550..614a66b611 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -235,7 +235,7 @@ impl Service { "The Timer should poll a new slot" ); - info!(log, "Processing slot: {}", current_slot.as_u64()); + info!(log, "Processing"; "slot" => current_slot.as_u64(), "epoch" => current_epoch.as_u64()); /* check for new duties */ @@ -243,8 +243,7 @@ impl Service { 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"); + let _empty_error = cloned_manager.run_update(current_epoch.clone(), cloned_log.clone()); }); /* execute any specified duties */ From 68b33620c29b6d0f6f3dad591f1fdc301573e9a6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 02:23:03 +1100 Subject: [PATCH 123/191] Implement Display and clean validator output --- .../test_utils/testing_beacon_state_builder.rs | 2 +- validator_client/src/duties/epoch_duties.rs | 17 ++++++++++++++++- validator_client/src/duties/mod.rs | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) 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 b38e8b5273..5e4cebd576 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 = 1553753928; // arbitrary + let genesis_time = 1553776331; // arbitrary let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 5c23e82b1e..f177377894 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::fmt; use std::ops::{Deref, DerefMut}; use types::{AttestationDuty, Epoch, PublicKey, Slot}; @@ -55,11 +56,25 @@ impl EpochDuty { None } } + +impl fmt::Display for EpochDuty { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut display_block = String::from("None"); + if let Some(block_slot) = self.block_production_slot { + display_block = block_slot.to_string(); + } + write!( + f, + "produce block slot: {}, attestation slot: {}, attestation shard: {}", + display_block, self.attestation_slot, self.attestation_shard + ) + } +} + /// Maps a list of public keys (many validators) to an EpochDuty. pub type EpochDuties = HashMap>; pub enum EpochDutiesMapError { - Poisoned, UnknownEpoch, UnknownValidator, } diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 0e962053e1..9fce1a3536 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -82,7 +82,8 @@ impl DutiesManager { 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)) + info!(log, "New duties obtained"; "epoch" => epoch); + print_duties(&log, duties); } }; Ok(Async::Ready(())) @@ -126,13 +127,22 @@ impl From> for Error { impl From for Error { fn from(e: EpochDutiesMapError) -> Error { match e { - EpochDutiesMapError::Poisoned => Error::EpochMapPoisoned, EpochDutiesMapError::UnknownEpoch => Error::UnknownEpoch, EpochDutiesMapError::UnknownValidator => Error::UnknownValidator, } } } +fn print_duties(log: &slog::Logger, duties: EpochDuties) { + for (pk, duty) in duties.iter() { + if let Some(display_duty) = duty { + info!(log, "Validator: {}",pk; "Duty" => format!("{}",display_duty)); + } else { + info!(log, "Validator: {}",pk; "Duty" => "None"); + } + } +} + /* TODO: Modify tests for new Duties Manager form #[cfg(test)] mod tests { From be592c86d11fcd6cc0b80e0939d657355ac512b3 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Fri, 29 Mar 2019 10:39:37 +1100 Subject: [PATCH 124/191] Started migrating FreeAttestation to Attestation in the harnesses - doesn't compile yet. --- .../test_harness/src/beacon_chain_harness.rs | 12 ++++++------ .../src/validator_harness/direct_beacon_node.rs | 14 +++++++------- .../test_harness/src/validator_harness/mod.rs | 14 ++------------ 3 files changed, 15 insertions(+), 25 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 9424974763..9f6643c7d3 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 @@ -137,11 +137,11 @@ impl BeaconChainHarness { slot } - /// Gather the `FreeAttestation`s from the valiators. + /// Gather the `Attestation`s from the valiators. /// /// Note: validators will only produce attestations _once per slot_. So, if you call this twice /// you'll only get attestations on the first run. - pub fn gather_free_attesations(&mut self) -> Vec { + pub fn gather_attesations(&mut self) -> Vec { let present_slot = self.beacon_chain.present_slot(); let attesting_validators = self @@ -158,7 +158,7 @@ impl BeaconChainHarness { let attesting_validators: HashSet = HashSet::from_iter(attesting_validators.iter().cloned()); - let free_attestations: Vec = self + let attestations: Vec = self .validators .par_iter_mut() .enumerate() @@ -176,8 +176,8 @@ impl BeaconChainHarness { .collect(); debug!( - "Gathered {} FreeAttestations for slot {}.", - free_attestations.len(), + "Gathered {} Attestations for slot {}.", + attestations.len(), present_slot ); @@ -232,7 +232,7 @@ impl BeaconChainHarness { .unwrap(); }); - debug!("Free attestations processed."); + debug!("attestations processed."); block } 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 17630833bb..7fc0376c8b 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 @@ -12,7 +12,7 @@ use fork_choice::ForkChoice; use parking_lot::RwLock; use slot_clock::SlotClock; use std::sync::Arc; -use types::{AttestationData, BeaconBlock, FreeAttestation, Signature, Slot}; +use types::{AttestationData, BeaconBlock, Attestation, Signature, Slot}; // mod attester; // mod producer; @@ -20,13 +20,13 @@ use types::{AttestationData, BeaconBlock, FreeAttestation, Signature, Slot}; /// Connect directly to a borrowed `BeaconChain` instance so an attester/producer can request/submit /// blocks/attestations. /// -/// `BeaconBlock`s and `FreeAttestation`s are not actually published to the `BeaconChain`, instead +/// `BeaconBlock`s and `Attestation`s are not actually published to the `BeaconChain`, instead /// they are stored inside this struct. This is to allow one to benchmark the submission of the /// block/attestation directly, or modify it before submission. pub struct DirectBeaconNode { beacon_chain: Arc>, published_blocks: RwLock>, - published_attestations: RwLock>, + published_attestations: RwLock>, } impl DirectBeaconNode { @@ -44,7 +44,7 @@ impl DirectBeaconNode { } /// Get the last published attestation (if any). - pub fn last_published_free_attestation(&self) -> Option { + pub fn last_published_free_attestation(&self) -> Option { Some(self.published_attestations.read().last()?.clone()) } } @@ -55,7 +55,7 @@ impl AttesterBeaconNode for DirectBeac _slot: Slot, shard: u64, ) -> Result, NodeError> { - match self.beacon_chain.produce_attestation(shard) { + match self.beacon_chain.produce_attestation_data(shard) { Ok(attestation_data) => Ok(Some(attestation_data)), Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))), } @@ -63,9 +63,9 @@ impl AttesterBeaconNode for DirectBeac fn publish_attestation( &self, - free_attestation: FreeAttestation, + attestation: Attestation, ) -> Result { - self.published_attestations.write().push(free_attestation); + self.published_attestations.write().push(attestation); Ok(AttestationPublishOutcome::ValidAttestation) } } diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs index 43ad03ea71..60258c794c 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs @@ -2,7 +2,7 @@ mod direct_beacon_node; mod direct_duties; mod local_signer; -use crate::direct_beacon_node::DirectBeaconNode; +use crate::validator_harness::direct_beacon_node::DirectBeaconNode; use attester::PollOutcome as AttestationPollOutcome; use attester::{Attester, Error as AttestationPollError}; use beacon_chain::BeaconChain; @@ -44,10 +44,8 @@ pub struct ValidatorHarness { pub block_producer: TestingBlockProducer, pub attester: TestingAttester, pub spec: Arc, - pub epoch_map: Arc>>, pub keypair: Keypair, pub beacon_node: Arc>>, - pub slot_clock: Arc, pub signer: Arc, } @@ -61,22 +59,16 @@ impl ValidatorHarness { beacon_chain: Arc>>, spec: Arc, ) -> Self { - let slot_clock = Arc::new(TestingSlotClock::new(spec.genesis_slot.as_u64())); let signer = Arc::new(LocalSigner::new(keypair.clone())); let beacon_node = Arc::new(DirectBeaconNode::new(beacon_chain.clone())); - let epoch_map = Arc::new(DirectDuties::new(keypair.pk.clone(), beacon_chain.clone())); let block_producer = BlockProducer::new( spec.clone(), - epoch_map.clone(), - slot_clock.clone(), beacon_node.clone(), signer.clone(), ); let attester = Attester::new( - epoch_map.clone(), - slot_clock.clone(), beacon_node.clone(), signer.clone(), ); @@ -85,10 +77,8 @@ impl ValidatorHarness { block_producer, attester, spec, - epoch_map, keypair, beacon_node, - slot_clock, signer, } } @@ -113,7 +103,7 @@ impl ValidatorHarness { /// Run the `poll` function on the `Attester` and produce a `FreeAttestation`. /// /// An error is returned if the attester refuses to attest. - pub fn produce_free_attestation(&mut self) -> Result { + pub fn produce_attestation(&mut self) -> Result { match self.attester.poll() { Ok(AttestationPollOutcome::AttestationProduced(_)) => {} Ok(outcome) => return Err(AttestationProduceError::DidNotProduce(outcome)), From aa29a66facc79db11b3d45f827b5010ad363b5ac Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 10:49:43 +1100 Subject: [PATCH 125/191] Add node chain-id validation for validation client --- validator_client/src/service.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 614a66b611..fb0304f87a 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -1,4 +1,13 @@ -/// The validator service. Connects to a beacon node and signs blocks when required. +/// The Validator Client service. +/// +/// Connects to a beacon node and negotiates the correct chain id. +/// +/// Once connected, the service loads known validators keypairs from disk. Every slot, +/// the service pings the beacon node, asking for new duties for each of the validators. +/// +/// When a validator needs to either produce a block or sign an attestation, it requests the +/// data from the beacon node and performs the signing before publishing the block to the beacon +/// node. use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use crate::config::Config as ValidatorConfig; @@ -36,8 +45,6 @@ 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 for this service. slot_clock: SystemTimeSlotClock, @@ -74,7 +81,7 @@ impl Service { Arc::new(BeaconNodeServiceClient::new(ch)) }; - // retrieve node information + // retrieve node information and validate the beacon node let node_info = loop { match beacon_node_client.info(&Empty::new()) { Err(e) => { @@ -84,18 +91,27 @@ impl Service { continue; } Ok(info) => { + // verify the node's genesis time if SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs() < info.genesis_time { - warn!( + error!( log, "Beacon Node's genesis time is in the future. No work to do.\n Exiting" ); return Err("Genesis time in the future".into()); } + // verify the node's chain id + if config.spec.chain_id != info.chain_id as u8 { + error!( + log, + "Beacon Node's genesis time is in the future. No work to do.\n Exiting" + ); + return Err(format!("Beacon node has the wrong chain id. Expected chain id: {}, node's chain id: {}", config.spec.chain_id, info.chain_id).into()); + } break info; } }; @@ -150,7 +166,6 @@ impl Service { Ok(Self { connected_node_version: node_info.version, - chain_id: node_info.chain_id as u16, fork, slot_clock, current_slot, From f8201edddda0e5bbace08dd205e1bd5145377d76 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 14:52:08 +1100 Subject: [PATCH 126/191] Initial layout of beacon block production --- validator_client/src/beacon_block_node.rs | 23 ++ .../beacon_block_grpc_client.rs | 81 ------- .../block_producer_service/block_producer.rs | 227 ++++++++++++++++++ .../src/block_producer_service/mod.rs | 125 ++++++---- validator_client/src/service.rs | 3 +- 5 files changed, 323 insertions(+), 136 deletions(-) create mode 100644 validator_client/src/beacon_block_node.rs delete mode 100644 validator_client/src/block_producer_service/beacon_block_grpc_client.rs create mode 100644 validator_client/src/block_producer_service/block_producer.rs diff --git a/validator_client/src/beacon_block_node.rs b/validator_client/src/beacon_block_node.rs new file mode 100644 index 0000000000..bc85ef194a --- /dev/null +++ b/validator_client/src/beacon_block_node.rs @@ -0,0 +1,23 @@ +#[Derive(Debug, PartialEq, Clone)] +pub enum BeaconNodeError { + RemoteFailure(String), + DecodeFailure, +} + +/// Defines the methods required to produce and publish blocks on a Beacon Node. Abstracts the +/// actual beacon node. +pub trait BeaconNode: Send + Sync { + /// Request that the node produces a block. + /// + /// Returns Ok(None) if the Beacon Node is unable to produce at the given slot. + fn produce_beacon_block( + &self, + slot: Slot, + randao_reveal: &Signature, + ) -> Result, BeaconNodeError>; + + /// Request that the node publishes a block. + /// + /// Returns `true` if the publish was sucessful. + fn publish_beacon_block(&self, block: BeaconBlock) -> Result; +} 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 deleted file mode 100644 index ba2acfffbb..0000000000 --- a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs +++ /dev/null @@ -1,81 +0,0 @@ -use block_proposer::{BeaconNode, BeaconNodeError, PublishOutcome}; -use protos::services::{ - BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, -}; -use protos::services_grpc::BeaconBlockServiceClient; -use ssz::{ssz_encode, Decodable}; -use std::sync::Arc; -use types::{BeaconBlock, Signature, Slot}; - -/// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be -/// implemented upon it. -pub struct BeaconBlockGrpcClient { - client: Arc, -} - -impl BeaconBlockGrpcClient { - pub fn new(client: Arc) -> Self { - Self { client } - } -} - -impl BeaconNode for BeaconBlockGrpcClient { - /// Request a Beacon Node (BN) to produce a new block at the supplied slot. - /// - /// Returns `None` if it is not possible to produce at the supplied slot. For example, if the - /// BN is unable to find a parent block. - fn produce_beacon_block( - &self, - slot: Slot, - // TODO: use randao_reveal, when proto APIs have been updated. - _randao_reveal: &Signature, - ) -> Result, BeaconNodeError> { - let mut req = ProduceBeaconBlockRequest::new(); - req.set_slot(slot.as_u64()); - - let reply = self - .client - .produce_beacon_block(&req) - .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - - if reply.has_block() { - let block = reply.get_block(); - let ssz = block.get_ssz(); - - let (block, _i) = - BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| BeaconNodeError::DecodeFailure)?; - - Ok(Some(block)) - } else { - Ok(None) - } - } - - /// Request a Beacon Node (BN) to publish a block. - /// - /// Generally, this will be called after a `produce_beacon_block` call with a block that has - /// been completed (signed) by the validator client. - 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_ssz(ssz); - - req.set_block(grpc_block); - - let reply = self - .client - .publish_beacon_block(&req) - .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - - if reply.get_success() { - Ok(PublishOutcome::ValidBlock) - } else { - // TODO: distinguish between different errors - Ok(PublishOutcome::InvalidBlock("Publish failed".to_string())) - } - } -} diff --git a/validator_client/src/block_producer_service/block_producer.rs b/validator_client/src/block_producer_service/block_producer.rs new file mode 100644 index 0000000000..187f2b6499 --- /dev/null +++ b/validator_client/src/block_producer_service/block_producer.rs @@ -0,0 +1,227 @@ +pub mod test_utils; +mod traits; + +use slot_clock::SlotClock; +use ssz::{SignedRoot, TreeHash}; +use std::sync::Arc; +use types::{BeaconBlock, ChainSpec, Domain, Slot}; + +#[derive(Debug, PartialEq)] +pub enum Error { + SlotClockError, + SlotUnknowable, + EpochMapPoisoned, + SlotClockPoisoned, + EpochLengthIsZero, + BeaconNodeError(BeaconNodeError), +} + +#[derive(Debug, PartialEq)] +pub enum BlockProducerEvent { + /// A new block was produced. + BlockProduced(Slot), + /// A block was not produced as it would have been slashable. + SlashableBlockNotProduced(Slot), + /// The Beacon Node was unable to produce a block at that slot. + BeaconNodeUnableToProduceBlock(Slot), + /// The signer failed to sign the message. + SignerRejection(Slot), + /// The public key for this validator is not an active validator. + ValidatorIsUnknown(Slot), +} + +/// This struct contains the logic for requesting and signing beacon blocks for a validator. The +/// validator can abstractly sign via the Signer trait object. +pub struct BlockProducer { + /// The current fork. + pub fork: Fork, + /// The current slot to produce a block for. + pub slot: Slot, + /// The current epoch. + pub epoch: Epoch, + /// The beacon node to connect to. + pub beacon_node: Arc, + /// The signer to sign the block. + pub signer: Arc, +} + +impl BlockProducer { + + /// Produce a block at some slot. + /// + /// Assumes that a block is required at this slot (does not check the duties). + /// + /// Ensures the message is not slashable. + /// + /// !!! UNSAFE !!! + /// + /// The slash-protection code is not yet implemented. There is zero protection against + /// slashing. + fn produce_block(&mut self) -> Result { + + let randao_reveal = { + // TODO: add domain, etc to this message. Also ensure result matches `into_to_bytes32`. + let message = slot.epoch(self.spec.slots_per_epoch).hash_tree_root(); + + match self.signer.sign_randao_reveal( + &message, + self.spec + .get_domain(slot.epoch(self.spec.slots_per_epoch), Domain::Randao, &fork), + ) { + None => return Ok(PollOutcome::SignerRejection(slot)), + Some(signature) => signature, + } + }; + + if let Some(block) = self + .beacon_node + .produce_beacon_block(slot, &randao_reveal)? + { + if self.safe_to_produce(&block) { + let domain = self.spec.get_domain( + slot.epoch(self.spec.slots_per_epoch), + Domain::BeaconBlock, + &fork, + ); + if let Some(block) = self.sign_block(block, domain) { + self.beacon_node.publish_beacon_block(block)?; + Ok(PollOutcome::BlockProduced(slot)) + } else { + Ok(PollOutcome::SignerRejection(slot)) + } + } else { + Ok(PollOutcome::SlashableBlockNotProduced(slot)) + } + } else { + Ok(PollOutcome::BeaconNodeUnableToProduceBlock(slot)) + } + } + + /// Consumes a block, returning that block signed by the validators private key. + /// + /// Important: this function will not check to ensure the block is not slashable. This must be + /// done upstream. + fn sign_block(&mut self, mut block: BeaconBlock, domain: u64) -> Option { + self.store_produce(&block); + + match self + .signer + .sign_block_proposal(&block.signed_root()[..], domain) + { + None => None, + Some(signature) => { + block.signature = signature; + Some(block) + } + } + } + + /// Returns `true` if signing a block is safe (non-slashable). + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn safe_to_produce(&self, _block: &BeaconBlock) -> bool { + // TODO: ensure the producer doesn't produce slashable blocks. + // https://github.com/sigp/lighthouse/issues/160 + true + } + + /// Record that a block was produced so that slashable votes may not be made in the future. + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn store_produce(&mut self, _block: &BeaconBlock) { + // TODO: record this block production to prevent future slashings. + // https://github.com/sigp/lighthouse/issues/160 + } +} + +impl From for Error { + fn from(e: BeaconNodeError) -> Error { + Error::BeaconNodeError(e) + } +} + + +/* Old tests - Re-work for new logic +#[cfg(test)] +mod tests { + use super::test_utils::{EpochMap, LocalSigner, SimulatedBeaconNode}; + use super::*; + use slot_clock::TestingSlotClock; + use types::{ + test_utils::{SeedableRng, TestRandom, XorShiftRng}, + Keypair, + }; + + // TODO: implement more thorough testing. + // https://github.com/sigp/lighthouse/issues/160 + // + // These tests should serve as a good example for future tests. + + #[test] + pub fn polling() { + let mut rng = XorShiftRng::from_seed([42; 16]); + + let spec = Arc::new(ChainSpec::foundation()); + let slot_clock = Arc::new(TestingSlotClock::new(0)); + let beacon_node = Arc::new(SimulatedBeaconNode::default()); + let signer = Arc::new(LocalSigner::new(Keypair::random())); + + let mut epoch_map = EpochMap::new(spec.slots_per_epoch); + let produce_slot = Slot::new(100); + let produce_epoch = produce_slot.epoch(spec.slots_per_epoch); + epoch_map.map.insert(produce_epoch, produce_slot); + let epoch_map = Arc::new(epoch_map); + + let mut block_proposer = BlockProducer::new( + spec.clone(), + epoch_map.clone(), + slot_clock.clone(), + beacon_node.clone(), + signer.clone(), + ); + + // Configure responses from the BeaconNode. + beacon_node.set_next_produce_result(Ok(Some(BeaconBlock::random_for_test(&mut rng)))); + beacon_node.set_next_publish_result(Ok(PublishOutcome::ValidBlock)); + + // One slot before production slot... + slot_clock.set_slot(produce_slot.as_u64() - 1); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1)) + ); + + // On the produce slot... + slot_clock.set_slot(produce_slot.as_u64()); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::BlockProduced(produce_slot.into())) + ); + + // Trying the same produce slot again... + slot_clock.set_slot(produce_slot.as_u64()); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::SlotAlreadyProcessed(produce_slot)) + ); + + // One slot after the produce slot... + slot_clock.set_slot(produce_slot.as_u64() + 1); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1)) + ); + + // In an epoch without known duties... + let slot = (produce_epoch.as_u64() + 1) * spec.slots_per_epoch; + slot_clock.set_slot(slot); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot))) + ); + } +} diff --git a/validator_client/src/block_producer_service/mod.rs b/validator_client/src/block_producer_service/mod.rs index 91e7606a7f..8a8cce6134 100644 --- a/validator_client/src/block_producer_service/mod.rs +++ b/validator_client/src/block_producer_service/mod.rs @@ -1,61 +1,80 @@ -mod beacon_block_grpc_client; -// mod block_producer_service; - -use block_proposer::{ - BeaconNode, BlockProducer, DutiesReader, PollOutcome as BlockProducerPollOutcome, Signer, +use protos::services::{ + BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, }; -use slog::{error, info, warn, Logger}; -use slot_clock::SlotClock; -use std::time::Duration; +use protos::services_grpc::BeaconBlockServiceClient; +use ssz::{ssz_encode, Decodable}; +use std::sync::Arc; +use types::{BeaconBlock, Signature, Slot}; -pub use self::beacon_block_grpc_client::BeaconBlockGrpcClient; - -pub struct BlockProducerService { - pub block_producer: BlockProducer, - pub poll_interval_millis: u64, - pub log: Logger, +/// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be +/// implemented upon it. +pub struct BeaconBlockGrpcClient { + inner: Arc, } -impl BlockProducerService { - /// Run a loop which polls the block producer each `poll_interval_millis` millseconds. - /// - /// Logs the results of the polls. - pub fn run(&mut self) { - loop { - match self.block_producer.poll() { - Err(error) => { - error!(self.log, "Block producer poll error"; "error" => format!("{:?}", error)) - } - Ok(BlockProducerPollOutcome::BlockProduced(slot)) => { - info!(self.log, "Produced block"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::SlashableBlockNotProduced(slot)) => { - warn!(self.log, "Slashable block was not signed"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::BlockProductionNotRequired(slot)) => { - info!(self.log, "Block production not required"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::ProducerDutiesUnknown(slot)) => { - error!(self.log, "Block production duties unknown"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::SlotAlreadyProcessed(slot)) => { - warn!(self.log, "Attempted to re-process slot"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::BeaconNodeUnableToProduceBlock(slot)) => { - error!(self.log, "Beacon node unable to produce block"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::SignerRejection(slot)) => { - error!(self.log, "The cryptographic signer refused to sign the block"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::ValidatorIsUnknown(slot)) => { - error!(self.log, "The Beacon Node does not recognise the validator"; "slot" => slot) - } - Ok(BlockProducerPollOutcome::UnableToGetFork(slot)) => { - error!(self.log, "Unable to get a `Fork` struct to generate signature domains"; "slot" => slot) - } - }; +impl BeaconBlockGrpcClient { + pub fn new(client: Arc) -> Self { + Self { inner: client } + } +} - std::thread::sleep(Duration::from_millis(self.poll_interval_millis)); +impl BeaconNode for BeaconBlockGrpcClient { + /// Request a Beacon Node (BN) to produce a new block at the supplied slot. + /// + /// Returns `None` if it is not possible to produce at the supplied slot. For example, if the + /// BN is unable to find a parent block. + fn produce_beacon_block( + &self, + slot: Slot, + // TODO: use randao_reveal, when proto APIs have been updated. + _randao_reveal: &Signature, + ) -> Result, BeaconNodeError> { + let mut req = ProduceBeaconBlockRequest::new(); + req.set_slot(slot.as_u64()); + + let reply = self + .client + .produce_beacon_block(&req) + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + + if reply.has_block() { + let block = reply.get_block(); + let ssz = block.get_ssz(); + + let (block, _i) = + BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| BeaconNodeError::DecodeFailure)?; + + Ok(Some(block)) + } else { + Ok(None) + } + } + + /// Request a Beacon Node (BN) to publish a block. + /// + /// Generally, this will be called after a `produce_beacon_block` call with a block that has + /// been completed (signed) by the validator client. + 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_ssz(ssz); + + req.set_block(grpc_block); + + let reply = self + .client + .publish_beacon_block(&req) + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + + if reply.get_success() { + Ok(PublishOutcome::ValidBlock) + } else { + // TODO: distinguish between different errors + Ok(PublishOutcome::InvalidBlock("Publish failed".to_string())) } } } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index fb0304f87a..64ed7cb03b 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -78,7 +78,7 @@ impl Service { // Beacon node gRPC beacon node endpoints. let beacon_node_client = { let ch = ChannelBuilder::new(env.clone()).connect(&config.server); - Arc::new(BeaconNodeServiceClient::new(ch)) + BeaconNodeServiceClient::new(ch) }; // retrieve node information and validate the beacon node @@ -287,7 +287,6 @@ impl Service { } /* - // Spawn a new thread to perform block production for the validator. let producer_thread = { let spec = spec.clone(); From eea772de3eb059530460fb845632312bc6d6f358 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 16:33:27 +1100 Subject: [PATCH 127/191] Implement block producer for validator client --- protos/src/services.proto | 3 + validator_client/Cargo.toml | 5 -- validator_client/src/beacon_block_node.rs | 23 ------- .../beacon_block_node.rs | 33 ++++++++++ .../block_producer_service/block_producer.rs | 60 ++++++++----------- .../src/block_producer_service/mod.rs | 33 ++++++---- validator_client/src/lib.rs | 3 - validator_client/src/main.rs | 1 + validator_client/src/service.rs | 2 +- validator_client/src/signer.rs | 7 +++ 10 files changed, 92 insertions(+), 78 deletions(-) delete mode 100644 validator_client/src/beacon_block_node.rs create mode 100644 validator_client/src/block_producer_service/beacon_block_node.rs delete mode 100644 validator_client/src/lib.rs create mode 100644 validator_client/src/signer.rs diff --git a/protos/src/services.proto b/protos/src/services.proto index dd82855a19..e5095f386b 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -19,7 +19,9 @@ service BeaconNodeService { /// Service that handles block production service BeaconBlockService { + // Requests a block to be signed from the beacon node. rpc ProduceBeaconBlock(ProduceBeaconBlockRequest) returns (ProduceBeaconBlockResponse); + // Responds to the node the signed block to be published. rpc PublishBeaconBlock(PublishBeaconBlockRequest) returns (PublishBeaconBlockResponse); } @@ -64,6 +66,7 @@ message Empty {} // Validator requests an unsigned proposal. message ProduceBeaconBlockRequest { uint64 slot = 1; + bytes randao_reveal = 2; } // Beacon node returns an unsigned proposal. diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index 570e06d74c..209ebf25e7 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -8,11 +8,6 @@ edition = "2018" name = "validator_client" path = "src/main.rs" -[lib] -name = "validator_client" -path = "src/lib.rs" - - [dependencies] block_proposer = { path = "../eth2/block_proposer" } attester = { path = "../eth2/attester" } diff --git a/validator_client/src/beacon_block_node.rs b/validator_client/src/beacon_block_node.rs deleted file mode 100644 index bc85ef194a..0000000000 --- a/validator_client/src/beacon_block_node.rs +++ /dev/null @@ -1,23 +0,0 @@ -#[Derive(Debug, PartialEq, Clone)] -pub enum BeaconNodeError { - RemoteFailure(String), - DecodeFailure, -} - -/// Defines the methods required to produce and publish blocks on a Beacon Node. Abstracts the -/// actual beacon node. -pub trait BeaconNode: Send + Sync { - /// Request that the node produces a block. - /// - /// Returns Ok(None) if the Beacon Node is unable to produce at the given slot. - fn produce_beacon_block( - &self, - slot: Slot, - randao_reveal: &Signature, - ) -> Result, BeaconNodeError>; - - /// Request that the node publishes a block. - /// - /// Returns `true` if the publish was sucessful. - fn publish_beacon_block(&self, block: BeaconBlock) -> Result; -} diff --git a/validator_client/src/block_producer_service/beacon_block_node.rs b/validator_client/src/block_producer_service/beacon_block_node.rs new file mode 100644 index 0000000000..5a581a4a7b --- /dev/null +++ b/validator_client/src/block_producer_service/beacon_block_node.rs @@ -0,0 +1,33 @@ +use types::{BeaconBlock, Signature, Slot}; +#[derive(Debug, PartialEq, Clone)] +pub enum BeaconBlockNodeError { + RemoteFailure(String), + DecodeFailure, +} + +#[derive(Debug, PartialEq, Clone)] +pub enum PublishOutcome { + ValidBlock, + InvalidBlock(String), +} + +/// Defines the methods required to produce and publish blocks on a Beacon Node. Abstracts the +/// actual beacon node. +pub trait BeaconBlockNode: Send + Sync { + /// Request that the node produces a block. + /// + /// Returns Ok(None) if the Beacon Node is unable to produce at the given slot. + fn produce_beacon_block( + &self, + slot: Slot, + randao_reveal: &Signature, + ) -> Result, BeaconBlockNodeError>; + + /// Request that the node publishes a block. + /// + /// Returns `true` if the publish was successful. + fn publish_beacon_block( + &self, + block: BeaconBlock, + ) -> Result; +} diff --git a/validator_client/src/block_producer_service/block_producer.rs b/validator_client/src/block_producer_service/block_producer.rs index 187f2b6499..e71e6cd4bb 100644 --- a/validator_client/src/block_producer_service/block_producer.rs +++ b/validator_client/src/block_producer_service/block_producer.rs @@ -1,10 +1,8 @@ -pub mod test_utils; -mod traits; - -use slot_clock::SlotClock; +use super::beacon_block_node::{BeaconBlockNode, BeaconBlockNodeError}; +use crate::signer::Signer; use ssz::{SignedRoot, TreeHash}; use std::sync::Arc; -use types::{BeaconBlock, ChainSpec, Domain, Slot}; +use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; #[derive(Debug, PartialEq)] pub enum Error { @@ -13,11 +11,11 @@ pub enum Error { EpochMapPoisoned, SlotClockPoisoned, EpochLengthIsZero, - BeaconNodeError(BeaconNodeError), + BeaconBlockNodeError(BeaconBlockNodeError), } #[derive(Debug, PartialEq)] -pub enum BlockProducerEvent { +pub enum ValidatorEvent { /// A new block was produced. BlockProduced(Slot), /// A block was not produced as it would have been slashable. @@ -32,21 +30,20 @@ pub enum BlockProducerEvent { /// This struct contains the logic for requesting and signing beacon blocks for a validator. The /// validator can abstractly sign via the Signer trait object. -pub struct BlockProducer { +pub struct BlockProducer { /// The current fork. pub fork: Fork, /// The current slot to produce a block for. pub slot: Slot, /// The current epoch. - pub epoch: Epoch, + pub spec: Arc, /// The beacon node to connect to. pub beacon_node: Arc, /// The signer to sign the block. pub signer: Arc, } -impl BlockProducer { - +impl BlockProducer { /// Produce a block at some slot. /// /// Assumes that a block is required at this slot (does not check the duties). @@ -57,43 +54,38 @@ impl BlockProducer { /// /// The slash-protection code is not yet implemented. There is zero protection against /// slashing. - fn produce_block(&mut self) -> Result { + fn produce_block(&mut self) -> Result { + let epoch = self.slot.epoch(self.spec.slots_per_epoch); let randao_reveal = { - // TODO: add domain, etc to this message. Also ensure result matches `into_to_bytes32`. - let message = slot.epoch(self.spec.slots_per_epoch).hash_tree_root(); - - match self.signer.sign_randao_reveal( + let message = epoch.hash_tree_root(); + let randao_reveal = match self.signer.sign_randao_reveal( &message, - self.spec - .get_domain(slot.epoch(self.spec.slots_per_epoch), Domain::Randao, &fork), + self.spec.get_domain(epoch, Domain::Randao, &self.fork), ) { - None => return Ok(PollOutcome::SignerRejection(slot)), + None => return Ok(ValidatorEvent::SignerRejection(self.slot)), Some(signature) => signature, - } + }; + randao_reveal }; if let Some(block) = self .beacon_node - .produce_beacon_block(slot, &randao_reveal)? + .produce_beacon_block(self.slot, &randao_reveal)? { if self.safe_to_produce(&block) { - let domain = self.spec.get_domain( - slot.epoch(self.spec.slots_per_epoch), - Domain::BeaconBlock, - &fork, - ); + let domain = self.spec.get_domain(epoch, Domain::BeaconBlock, &self.fork); if let Some(block) = self.sign_block(block, domain) { self.beacon_node.publish_beacon_block(block)?; - Ok(PollOutcome::BlockProduced(slot)) + Ok(ValidatorEvent::BlockProduced(self.slot)) } else { - Ok(PollOutcome::SignerRejection(slot)) + Ok(ValidatorEvent::SignerRejection(self.slot)) } } else { - Ok(PollOutcome::SlashableBlockNotProduced(slot)) + Ok(ValidatorEvent::SlashableBlockNotProduced(self.slot)) } } else { - Ok(PollOutcome::BeaconNodeUnableToProduceBlock(slot)) + Ok(ValidatorEvent::BeaconNodeUnableToProduceBlock(self.slot)) } } @@ -138,13 +130,12 @@ impl BlockProducer { } } -impl From for Error { - fn from(e: BeaconNodeError) -> Error { - Error::BeaconNodeError(e) +impl From for Error { + fn from(e: BeaconBlockNodeError) -> Error { + Error::BeaconBlockNodeError(e) } } - /* Old tests - Re-work for new logic #[cfg(test)] mod tests { @@ -225,3 +216,4 @@ mod tests { ); } } +*/ diff --git a/validator_client/src/block_producer_service/mod.rs b/validator_client/src/block_producer_service/mod.rs index 8a8cce6134..fe34f627d8 100644 --- a/validator_client/src/block_producer_service/mod.rs +++ b/validator_client/src/block_producer_service/mod.rs @@ -1,3 +1,7 @@ +mod beacon_block_node; +mod block_producer; + +use self::beacon_block_node::*; use protos::services::{ BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, }; @@ -9,16 +13,16 @@ use types::{BeaconBlock, Signature, Slot}; /// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be /// implemented upon it. pub struct BeaconBlockGrpcClient { - inner: Arc, + client: Arc, } impl BeaconBlockGrpcClient { pub fn new(client: Arc) -> Self { - Self { inner: client } + Self { client } } } -impl BeaconNode for BeaconBlockGrpcClient { +impl BeaconBlockNode for BeaconBlockGrpcClient { /// Request a Beacon Node (BN) to produce a new block at the supplied slot. /// /// Returns `None` if it is not possible to produce at the supplied slot. For example, if the @@ -26,23 +30,26 @@ impl BeaconNode for BeaconBlockGrpcClient { fn produce_beacon_block( &self, slot: Slot, - // TODO: use randao_reveal, when proto APIs have been updated. - _randao_reveal: &Signature, - ) -> Result, BeaconNodeError> { + randao_reveal: &Signature, + ) -> Result, BeaconBlockNodeError> { + // request a beacon block from the node let mut req = ProduceBeaconBlockRequest::new(); req.set_slot(slot.as_u64()); + req.set_randao_reveal(ssz_encode(randao_reveal)); + //TODO: Determine if we want an explicit timeout let reply = self .client .produce_beacon_block(&req) - .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; + // format the reply if reply.has_block() { let block = reply.get_block(); let ssz = block.get_ssz(); - let (block, _i) = - BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| BeaconNodeError::DecodeFailure)?; + let (block, _i) = BeaconBlock::ssz_decode(&ssz, 0) + .map_err(|_| BeaconBlockNodeError::DecodeFailure)?; Ok(Some(block)) } else { @@ -54,12 +61,14 @@ impl BeaconNode for BeaconBlockGrpcClient { /// /// Generally, this will be called after a `produce_beacon_block` call with a block that has /// been completed (signed) by the validator client. - fn publish_beacon_block(&self, block: BeaconBlock) -> Result { + 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_ssz(ssz); @@ -68,7 +77,7 @@ impl BeaconNode for BeaconBlockGrpcClient { let reply = self .client .publish_beacon_block(&req) - .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; if reply.get_success() { Ok(PublishOutcome::ValidBlock) diff --git a/validator_client/src/lib.rs b/validator_client/src/lib.rs deleted file mode 100644 index 470a070e87..0000000000 --- a/validator_client/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod config; - -pub use crate::config::Config; diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index d044030fe6..0b11ed0d04 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -4,6 +4,7 @@ mod config; mod duties; pub mod error; mod service; +mod signer; use crate::config::Config as ValidatorClientConfig; use clap::{App, Arg}; diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 64ed7cb03b..9c0f5d23c1 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -9,7 +9,7 @@ /// data from the beacon node and performs the signing before publishing the block to the beacon /// node. use crate::attester_service::{AttestationGrpcClient, AttesterService}; -use crate::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; +use crate::block_producer_service::BeaconBlockGrpcClient; use crate::config::Config as ValidatorConfig; use crate::duties::UpdateOutcome; use crate::duties::{DutiesManager, EpochDutiesMap}; diff --git a/validator_client/src/signer.rs b/validator_client/src/signer.rs new file mode 100644 index 0000000000..85bf35b167 --- /dev/null +++ b/validator_client/src/signer.rs @@ -0,0 +1,7 @@ +use types::Signature; + +/// Signs message using an internally-maintained private key. +pub trait Signer { + fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option; + fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option; +} From 9cdb7bb716f375df88f185ff590c633cc18928a0 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 17:28:07 +1100 Subject: [PATCH 128/191] Restructure of validator client service and block producer --- .../beacon_block_node.rs | 0 .../block_producer.rs | 0 .../mod.rs | 0 .../{traits.rs => beacon_node_duties.rs} | 6 +- validator_client/src/duties/grpc.rs | 8 +- validator_client/src/duties/mod.rs | 16 +- validator_client/src/main.rs | 2 +- validator_client/src/service.rs | 251 ++++++++---------- 8 files changed, 131 insertions(+), 152 deletions(-) rename validator_client/src/{block_producer_service => block_producer}/beacon_block_node.rs (100%) rename validator_client/src/{block_producer_service => block_producer}/block_producer.rs (100%) rename validator_client/src/{block_producer_service => block_producer}/mod.rs (100%) rename validator_client/src/duties/{traits.rs => beacon_node_duties.rs} (79%) diff --git a/validator_client/src/block_producer_service/beacon_block_node.rs b/validator_client/src/block_producer/beacon_block_node.rs similarity index 100% rename from validator_client/src/block_producer_service/beacon_block_node.rs rename to validator_client/src/block_producer/beacon_block_node.rs diff --git a/validator_client/src/block_producer_service/block_producer.rs b/validator_client/src/block_producer/block_producer.rs similarity index 100% rename from validator_client/src/block_producer_service/block_producer.rs rename to validator_client/src/block_producer/block_producer.rs diff --git a/validator_client/src/block_producer_service/mod.rs b/validator_client/src/block_producer/mod.rs similarity index 100% rename from validator_client/src/block_producer_service/mod.rs rename to validator_client/src/block_producer/mod.rs diff --git a/validator_client/src/duties/traits.rs b/validator_client/src/duties/beacon_node_duties.rs similarity index 79% rename from validator_client/src/duties/traits.rs rename to validator_client/src/duties/beacon_node_duties.rs index 374bed9f61..efe9e836de 100644 --- a/validator_client/src/duties/traits.rs +++ b/validator_client/src/duties/beacon_node_duties.rs @@ -2,12 +2,12 @@ use super::EpochDuties; use types::{Epoch, PublicKey}; #[derive(Debug, PartialEq, Clone)] -pub enum BeaconNodeError { +pub enum BeaconNodeDutiesError { RemoteFailure(String), } /// Defines the methods required to obtain a validators shuffling from a Beacon Node. -pub trait BeaconNode: Send + Sync { +pub trait BeaconNodeDuties: Send + Sync { /// Gets the duties for all validators. /// /// Returns a vector of EpochDuties for each validator public key. The entry will be None for @@ -16,5 +16,5 @@ pub trait BeaconNode: Send + Sync { &self, epoch: Epoch, pubkeys: &[PublicKey], - ) -> Result; + ) -> Result; } diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 511ffa34a2..f05307141e 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -1,5 +1,5 @@ +use super::beacon_node_duties::{BeaconNodeDuties, BeaconNodeDutiesError}; use super::epoch_duties::{EpochDuties, EpochDuty}; -use super::traits::{BeaconNode, BeaconNodeError}; use grpcio::CallOption; use protos::services::{GetDutiesRequest, Validators}; use protos::services_grpc::ValidatorServiceClient; @@ -8,13 +8,13 @@ use std::collections::HashMap; use std::time::Duration; use types::{Epoch, PublicKey, Slot}; -impl BeaconNode for ValidatorServiceClient { +impl BeaconNodeDuties for ValidatorServiceClient { /// Requests all duties (block signing and committee attesting) from the Beacon Node (BN). fn request_duties( &self, epoch: Epoch, pubkeys: &[PublicKey], - ) -> Result { + ) -> Result { // Get the required duties from all validators // build the request let mut req = GetDutiesRequest::new(); @@ -29,7 +29,7 @@ impl BeaconNode for ValidatorServiceClient { // send the request, get the duties reply let reply = self .get_validator_duties(&req) - .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + .map_err(|err| BeaconNodeDutiesError::RemoteFailure(format!("{:?}", err)))?; let mut epoch_duties: HashMap> = HashMap::new(); for (index, validator_duty) in reply.get_active_validators().iter().enumerate() { diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 9fce1a3536..c16fc81fc9 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -1,13 +1,13 @@ +mod beacon_node_duties; mod epoch_duties; mod grpc; // TODO: reintroduce tests //#[cfg(test)] //mod test_node; -mod traits; +pub use self::beacon_node_duties::{BeaconNodeDuties, BeaconNodeDutiesError}; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; pub use self::epoch_duties::{EpochDutiesMap, WorkInfo}; -use self::traits::{BeaconNode, BeaconNodeError}; use futures::Async; use slog::{debug, error, info}; use std::sync::Arc; @@ -29,7 +29,7 @@ pub enum UpdateOutcome { pub enum Error { DutiesMapPoisoned, EpochMapPoisoned, - BeaconNodeError(BeaconNodeError), + BeaconNodeDutiesError(BeaconNodeDutiesError), UnknownEpoch, UnknownValidator, } @@ -38,14 +38,14 @@ pub enum Error { /// Node. /// /// This keeps track of all validator keys and required voting slots. -pub struct DutiesManager { +pub struct DutiesManager { pub duties_map: RwLock, /// A list of all public keys known to the validator service. pub pubkeys: Vec, pub beacon_node: Arc, } -impl DutiesManager { +impl DutiesManager { /// Check the Beacon Node for `EpochDuties`. /// /// be a wall-clock (e.g., system time, remote server time, etc.). @@ -112,9 +112,9 @@ impl DutiesManager { } //TODO: Use error_chain to handle errors -impl From for Error { - fn from(e: BeaconNodeError) -> Error { - Error::BeaconNodeError(e) +impl From for Error { + fn from(e: BeaconNodeDutiesError) -> Error { + Error::BeaconNodeDutiesError(e) } } diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 0b11ed0d04..6f02cf6eed 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,5 +1,5 @@ mod attester_service; -mod block_producer_service; +mod block_producer; mod config; mod duties; pub mod error; diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 9c0f5d23c1..398f6d7770 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -9,15 +9,14 @@ /// data from the beacon node and performs the signing before publishing the block to the beacon /// node. use crate::attester_service::{AttestationGrpcClient, AttesterService}; +use crate::block_producer::BlockProducer; use crate::block_producer_service::BeaconBlockGrpcClient; use crate::config::Config as ValidatorConfig; -use crate::duties::UpdateOutcome; -use crate::duties::{DutiesManager, EpochDutiesMap}; +use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap, UpdateOutcome}; 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}; use bls::Keypair; use grpcio::{ChannelBuilder, EnvBuilder}; use protos::services::Empty; @@ -35,13 +34,13 @@ use tokio::runtime::Builder; use tokio::timer::Interval; use tokio_timer::clock::Clock; use types::test_utils::generate_deterministic_keypairs; -use types::{Epoch, Fork, Slot}; +use types::{ChainSpec, 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. -pub struct Service { +pub struct Service { /// The node we currently connected to. connected_node_version: String, /// The chain id we are processing on. @@ -50,28 +49,25 @@ pub struct Service { slot_clock: SystemTimeSlotClock, /// The current slot we are processing. current_slot: Slot, - /// The number of slots per epoch to allow for converting slots to epochs. - slots_per_epoch: u64, + /// The chain specification for this clients instance. + spec: Arc, + /// The duties manager which maintains the state of when to perform actions. + duties_manager: Arc>, // GRPC Clients /// The beacon block GRPC client. - beacon_block_client: Arc, - /// The validator GRPC client. - validator_client: Arc, + beacon_block_client: Arc, /// The attester GRPC client. attester_client: Arc, /// The validator client logger. log: slog::Logger, } -impl Service { +impl Service { /// 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 /// and returns an instance of the service. - fn initialize_service( - config: &ValidatorConfig, - log: slog::Logger, - ) -> error_chain::Result { + 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()); @@ -139,7 +135,9 @@ impl Service { // Beacon node gRPC beacon block endpoints. let beacon_block_client = { let ch = ChannelBuilder::new(env.clone()).connect(&config.server); - Arc::new(BeaconBlockServiceClient::new(ch)) + let beacon_block_service_client = Arc::new(BeaconBlockServiceClient::new(ch)); + // a wrapper around the service client to implement the beacon block node trait + Arc::new(BeaconBlockGrpcClient::new(beacon_block_service_client)) }; // Beacon node gRPC validator endpoints. @@ -164,14 +162,37 @@ impl Service { .map_err(|e| ErrorKind::SlotClockError(e))? .expect("Genesis must be in the future"); + let spec = Arc::new(config.spec); + + /* Generate the duties manager */ + + // 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 = Arc::new(generate_deterministic_keypairs(8)); + + // Builds a mapping of Epoch -> Map(PublicKey, EpochDuty) + // where EpochDuty contains slot numbers and attestation data that each validator needs to + // produce work on. + let duties_map = RwLock::new(EpochDutiesMap::new(config.spec.slots_per_epoch)); + + // 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 duties_manager = Arc::new(DutiesManager { + duties_map, + pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(), + beacon_node: validator_client, + }); + Ok(Self { connected_node_version: node_info.version, fork, slot_clock, current_slot, - slots_per_epoch: config.spec.slots_per_epoch, + spec, + duties_manager, beacon_block_client, - validator_client, attester_client, log, }) @@ -180,7 +201,7 @@ impl Service { /// Initialise the service then run the core thread. 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 @@ -205,137 +226,95 @@ impl Service { Interval::new(Instant::now() + duration_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 = Arc::new(generate_deterministic_keypairs(8)); - - /* 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 = 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. - let manager = Arc::new(DutiesManager { - duties_map, - pubkeys: keypairs.iter().map(|keypair| keypair.pk.clone()).collect(), - beacon_node: service.validator_client.clone(), - }); - - // run the core thread + /* kick off the core service */ 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(), "epoch" => current_epoch.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 || { - let _empty_error = cloned_manager.run_update(current_epoch.clone(), cloned_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 - } - 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 - } - } - } - + // if a non-fatal error occurs, proceed to the next slot. + let _ignore_error = service.per_slot_execution(); + // completed a slot process Ok(()) }) .map_err(|e| format!("Service thread failed: {:?}", e)), ); - - // completed a slot process + // validator client exited Ok(()) } - /* - // 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, - }; + /// The execution logic that runs every slot. + // Errors are logged to output, and core execution continues unless fatal errors occur. + fn per_slot_execution(&mut self) -> error_chain::Result<()> { + /* get the new current slot and epoch */ + self.update_current_slot()?; - block_producer_service.run(); - }) - }; + /* check for new duties */ + self.check_for_duties(); - // 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, - }; + /* process any required duties for validators */ + self.process_duties(); - attester_service.run(); - }) - }; - - threads.push((duties_manager_thread, producer_thread, attester_thread)); + Ok(()) } - // 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(); + /// Updates the known current slot and epoch. + fn update_current_slot(&mut self) -> error_chain::Result<()> { + let current_slot = match self.slot_clock.present_slot() { + Err(e) => { + error!(self.log, "SystemTimeError {:?}", e); + return Err("Could not read system time".into()); + } + Ok(slot) => slot.expect("Genesis is in the future"), + }; + + let current_epoch = current_slot.epoch(self.spec.slots_per_epoch); + + // this is a fatal error. If the slot clock repeats, there is something wrong with + // the timer, terminate immediately. + assert!( + current_slot > self.current_slot, + "The Timer should poll a new slot" + ); + self.current_slot = current_slot; + info!(self.log, "Processing"; "slot" => current_slot.as_u64(), "epoch" => current_epoch.as_u64()); + Ok(()) + } + + /// For all known validator keypairs, update any known duties from the beacon node. + fn check_for_duties(&mut self) { + let cloned_manager = self.duties_manager.clone(); + let cloned_log = self.log.clone(); + let current_epoch = self.current_slot.epoch(self.spec.slots_per_epoch); + // spawn a new thread separate to the runtime + // TODO: Handle thread termination/timeout + std::thread::spawn(move || { + // the return value is a future which returns ready. + // built to be compatible with the tokio runtime. + let _empty = cloned_manager.run_update(current_epoch.clone(), cloned_log.clone()); + }); + } + + /// If there are any duties to process, spawn a separate thread and perform required actions. + fn process_duties(&mut self) { + if let Some(work) = self.duties_manager.get_current_work(self.current_slot) { + for (_public_key, work_type) in work { + if work_type.produce_block { + // spawns a thread to produce a beacon block + std::thread::spawn(move || { + let block_producer = BlockProducer { + fork: self.fork, + slot: self.current_slot, + spec: self.spec.clone(), + }; + }); + + // 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 + } + } + } } - */ } From dd2351020cdf975ff8f20b003d4348502575ef72 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 29 Mar 2019 17:58:02 +1100 Subject: [PATCH 129/191] Impl `add_aggregate` for FakeAggSig --- eth2/utils/bls/src/fake_aggregate_signature.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/eth2/utils/bls/src/fake_aggregate_signature.rs b/eth2/utils/bls/src/fake_aggregate_signature.rs index 23e2b54ef3..85b06b9564 100644 --- a/eth2/utils/bls/src/fake_aggregate_signature.rs +++ b/eth2/utils/bls/src/fake_aggregate_signature.rs @@ -35,6 +35,11 @@ impl FakeAggregateSignature { // Do nothing. } + /// Does glorious nothing. + pub fn add_aggregate(&mut self, _agg_sig: &FakeAggregateSignature) { + // Do nothing. + } + /// _Always_ returns `true`. pub fn verify( &self, From 46a978a5a92effb1bde7ce74a1e94308e038f0b1 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 29 Mar 2019 18:30:03 +1100 Subject: [PATCH 130/191] Implement op pool for all ops execpt attestations --- beacon_node/beacon_chain/Cargo.toml | 1 + beacon_node/beacon_chain/src/beacon_chain.rs | 258 ++++--------------- eth2/operation_pool/src/lib.rs | 2 +- 3 files changed, 51 insertions(+), 210 deletions(-) diff --git a/beacon_node/beacon_chain/Cargo.toml b/beacon_node/beacon_chain/Cargo.toml index b5471be5fa..55d4bacfdc 100644 --- a/beacon_node/beacon_chain/Cargo.toml +++ b/beacon_node/beacon_chain/Cargo.toml @@ -15,6 +15,7 @@ hashing = { path = "../../eth2/utils/hashing" } fork_choice = { path = "../../eth2/fork_choice" } parking_lot = "0.7" log = "0.4" +operation_pool = { path = "../../eth2/operation_pool" } env_logger = "0.6" serde = "1.0" serde_derive = "1.0" diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 27398b6c9f..1d0cfe8e39 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -6,7 +6,8 @@ use db::{ ClientDB, DBError, }; use fork_choice::{ForkChoice, ForkChoiceError}; -use log::{debug, trace}; +use log::{debug, trace, warn}; +use operation_pool::OperationPool; use parking_lot::{RwLock, RwLockReadGuard}; use slot_clock::SlotClock; use ssz::ssz_encode; @@ -83,6 +84,7 @@ pub struct BeaconChain { pub state_store: Arc>, pub slot_clock: U, pub attestation_aggregator: RwLock, + pub op_pool: RwLock, pub deposits_for_inclusion: RwLock>, pub exits_for_inclusion: RwLock>, pub transfers_for_inclusion: RwLock>, @@ -141,6 +143,7 @@ where state_store, slot_clock, attestation_aggregator, + op_pool: RwLock::new(OperationPool::new()), deposits_for_inclusion: RwLock::new(vec![]), exits_for_inclusion: RwLock::new(vec![]), transfers_for_inclusion: RwLock::new(vec![]), @@ -540,218 +543,48 @@ where /// Accept some deposit and queue it for inclusion in an appropriate block. pub fn receive_deposit_for_inclusion(&self, deposit: Deposit) { - // TODO: deposits are not checked for validity; check them. - // - // https://github.com/sigp/lighthouse/issues/276 - self.deposits_for_inclusion.write().push(deposit); - } - - /// Return a vec of deposits suitable for inclusion in some block. - pub fn get_deposits_for_block(&self) -> Vec { - // TODO: deposits are indiscriminately included; check them for validity. - // - // https://github.com/sigp/lighthouse/issues/275 - self.deposits_for_inclusion.read().clone() - } - - /// Takes a list of `Deposits` that were included in recent blocks and removes them from the - /// inclusion queue. - /// - /// This ensures that `Deposits` are not included twice in successive blocks. - pub fn set_deposits_as_included(&self, included_deposits: &[Deposit]) { - // TODO: method does not take forks into account; consider this. - // - // https://github.com/sigp/lighthouse/issues/275 - let mut indices_to_delete = vec![]; - - for included in included_deposits { - for (i, for_inclusion) in self.deposits_for_inclusion.read().iter().enumerate() { - if included == for_inclusion { - indices_to_delete.push(i); - } - } - } - - let deposits_for_inclusion = &mut self.deposits_for_inclusion.write(); - for i in indices_to_delete { - deposits_for_inclusion.remove(i); - } + // Bad deposits are ignored. + let _ = self + .op_pool + .write() + .insert_deposit(deposit, &*self.state.read(), &self.spec); } /// Accept some exit and queue it for inclusion in an appropriate block. pub fn receive_exit_for_inclusion(&self, exit: VoluntaryExit) { - // TODO: exits are not checked for validity; check them. - // - // https://github.com/sigp/lighthouse/issues/276 - self.exits_for_inclusion.write().push(exit); - } - - /// Return a vec of exits suitable for inclusion in some block. - pub fn get_exits_for_block(&self) -> Vec { - // TODO: exits are indiscriminately included; check them for validity. - // - // https://github.com/sigp/lighthouse/issues/275 - self.exits_for_inclusion.read().clone() - } - - /// Takes a list of `Deposits` that were included in recent blocks and removes them from the - /// inclusion queue. - /// - /// This ensures that `Deposits` are not included twice in successive blocks. - pub fn set_exits_as_included(&self, included_exits: &[VoluntaryExit]) { - // TODO: method does not take forks into account; consider this. - let mut indices_to_delete = vec![]; - - for included in included_exits { - for (i, for_inclusion) in self.exits_for_inclusion.read().iter().enumerate() { - if included == for_inclusion { - indices_to_delete.push(i); - } - } - } - - let exits_for_inclusion = &mut self.exits_for_inclusion.write(); - for i in indices_to_delete { - exits_for_inclusion.remove(i); - } + // Bad exits are ignored + let _ = self + .op_pool + .write() + .insert_voluntary_exit(exit, &*self.state.read(), &self.spec); } /// Accept some transfer and queue it for inclusion in an appropriate block. pub fn receive_transfer_for_inclusion(&self, transfer: Transfer) { - // TODO: transfers are not checked for validity; check them. - // - // https://github.com/sigp/lighthouse/issues/276 - self.transfers_for_inclusion.write().push(transfer); - } - - /// Return a vec of transfers suitable for inclusion in some block. - pub fn get_transfers_for_block(&self) -> Vec { - // TODO: transfers are indiscriminately included; check them for validity. - // - // https://github.com/sigp/lighthouse/issues/275 - self.transfers_for_inclusion.read().clone() - } - - /// Takes a list of `Deposits` that were included in recent blocks and removes them from the - /// inclusion queue. - /// - /// This ensures that `Deposits` are not included twice in successive blocks. - pub fn set_transfers_as_included(&self, included_transfers: &[Transfer]) { - // TODO: method does not take forks into account; consider this. - let mut indices_to_delete = vec![]; - - for included in included_transfers { - for (i, for_inclusion) in self.transfers_for_inclusion.read().iter().enumerate() { - if included == for_inclusion { - indices_to_delete.push(i); - } - } - } - - let transfers_for_inclusion = &mut self.transfers_for_inclusion.write(); - for i in indices_to_delete { - transfers_for_inclusion.remove(i); - } + // Bad transfers are ignored. + let _ = self + .op_pool + .write() + .insert_transfer(transfer, &*self.state.read(), &self.spec); } /// Accept some proposer slashing and queue it for inclusion in an appropriate block. pub fn receive_proposer_slashing_for_inclusion(&self, proposer_slashing: ProposerSlashing) { - // TODO: proposer_slashings are not checked for validity; check them. - // - // https://github.com/sigp/lighthouse/issues/276 - self.proposer_slashings_for_inclusion - .write() - .push(proposer_slashing); - } - - /// Return a vec of proposer slashings suitable for inclusion in some block. - pub fn get_proposer_slashings_for_block(&self) -> Vec { - // TODO: proposer_slashings are indiscriminately included; check them for validity. - // - // https://github.com/sigp/lighthouse/issues/275 - self.proposer_slashings_for_inclusion.read().clone() - } - - /// Takes a list of `ProposerSlashings` that were included in recent blocks and removes them - /// from the inclusion queue. - /// - /// This ensures that `ProposerSlashings` are not included twice in successive blocks. - pub fn set_proposer_slashings_as_included( - &self, - included_proposer_slashings: &[ProposerSlashing], - ) { - // TODO: method does not take forks into account; consider this. - // - // https://github.com/sigp/lighthouse/issues/275 - let mut indices_to_delete = vec![]; - - for included in included_proposer_slashings { - for (i, for_inclusion) in self - .proposer_slashings_for_inclusion - .read() - .iter() - .enumerate() - { - if included == for_inclusion { - indices_to_delete.push(i); - } - } - } - - let proposer_slashings_for_inclusion = &mut self.proposer_slashings_for_inclusion.write(); - for i in indices_to_delete { - proposer_slashings_for_inclusion.remove(i); - } + // Bad proposer slashings are ignored. + let _ = self.op_pool.write().insert_proposer_slashing( + proposer_slashing, + &*self.state.read(), + &self.spec, + ); } /// Accept some attester slashing and queue it for inclusion in an appropriate block. pub fn receive_attester_slashing_for_inclusion(&self, attester_slashing: AttesterSlashing) { - // TODO: attester_slashings are not checked for validity; check them. - // - // https://github.com/sigp/lighthouse/issues/276 - self.attester_slashings_for_inclusion - .write() - .push(attester_slashing); - } - - /// Return a vec of attester slashings suitable for inclusion in some block. - pub fn get_attester_slashings_for_block(&self) -> Vec { - // TODO: attester_slashings are indiscriminately included; check them for validity. - // - // https://github.com/sigp/lighthouse/issues/275 - self.attester_slashings_for_inclusion.read().clone() - } - - /// Takes a list of `AttesterSlashings` that were included in recent blocks and removes them - /// from the inclusion queue. - /// - /// This ensures that `AttesterSlashings` are not included twice in successive blocks. - pub fn set_attester_slashings_as_included( - &self, - included_attester_slashings: &[AttesterSlashing], - ) { - // TODO: method does not take forks into account; consider this. - // - // https://github.com/sigp/lighthouse/issues/275 - let mut indices_to_delete = vec![]; - - for included in included_attester_slashings { - for (i, for_inclusion) in self - .attester_slashings_for_inclusion - .read() - .iter() - .enumerate() - { - if included == for_inclusion { - indices_to_delete.push(i); - } - } - } - - let attester_slashings_for_inclusion = &mut self.attester_slashings_for_inclusion.write(); - for i in indices_to_delete { - attester_slashings_for_inclusion.remove(i); - } + let _ = self.op_pool.write().insert_attester_slashing( + attester_slashing, + &*self.state.read(), + &self.spec, + ); } /// Returns `true` if the given block root has not been processed. @@ -832,13 +665,6 @@ where self.block_store.put(&block_root, &ssz_encode(&block)[..])?; self.state_store.put(&state_root, &ssz_encode(&state)[..])?; - // Update the inclusion queues so they aren't re-submitted. - self.set_deposits_as_included(&block.body.deposits[..]); - self.set_transfers_as_included(&block.body.transfers[..]); - self.set_exits_as_included(&block.body.voluntary_exits[..]); - self.set_proposer_slashings_as_included(&block.body.proposer_slashings[..]); - self.set_attester_slashings_as_included(&block.body.attester_slashings[..]); - // run the fork_choice add_block logic self.fork_choice .write() @@ -888,6 +714,11 @@ where .get_block_root(state.slot - 1, &self.spec) .map_err(|_| BlockProductionError::UnableToGetBlockRootFromState)?; + let (proposer_slashings, attester_slashings) = self + .op_pool + .read() + .get_slashings(&*self.state.read(), &self.spec); + let mut block = BeaconBlock { slot: state.slot, previous_block_root, @@ -900,12 +731,21 @@ where deposit_root: Hash256::zero(), block_hash: Hash256::zero(), }, - proposer_slashings: self.get_proposer_slashings_for_block(), - attester_slashings: self.get_attester_slashings_for_block(), + proposer_slashings, + attester_slashings, attestations, - deposits: self.get_deposits_for_block(), - voluntary_exits: self.get_exits_for_block(), - transfers: self.get_transfers_for_block(), + deposits: self + .op_pool + .read() + .get_deposits(&*self.state.read(), &self.spec), + voluntary_exits: self + .op_pool + .read() + .get_voluntary_exits(&*self.state.read(), &self.spec), + transfers: self + .op_pool + .read() + .get_transfers(&*self.state.read(), &self.spec), }, }; diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index e67a201c76..c3de95b48d 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -21,7 +21,7 @@ use types::{ #[cfg(test)] const VERIFY_DEPOSIT_PROOFS: bool = false; #[cfg(not(test))] -const VERIFY_DEPOSIT_PROOFS: bool = true; +const VERIFY_DEPOSIT_PROOFS: bool = false; // TODO: enable this #[derive(Default)] pub struct OperationPool { From 8b1a91e9eec1d11ad833586ad43526eb465c6f21 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 29 Mar 2019 18:40:50 +1100 Subject: [PATCH 131/191] Add `process_attestation` to `BeaconChain` --- beacon_node/beacon_chain/src/beacon_chain.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 1d0cfe8e39..5eadb37636 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -541,6 +541,17 @@ where Ok(aggregation_outcome) } + /// Accept a new attestation from the network. + /// + /// If valid, the attestation is added to the `op_pool` and aggregated with another attestation + /// if possible. + pub fn process_attestation(&self, attestation: Attestation) { + let _ = + self.op_pool + .write() + .insert_attestation(attestation, &*self.state.read(), &self.spec); + } + /// Accept some deposit and queue it for inclusion in an appropriate block. pub fn receive_deposit_for_inclusion(&self, deposit: Deposit) { // Bad deposits are ignored. @@ -701,9 +712,9 @@ where trace!("Finding attestations for new block..."); let attestations = self - .attestation_aggregator + .op_pool .read() - .get_attestations_for_state(&state, &self.spec); + .get_attestations(&*self.state.read(), &self.spec); trace!( "Inserting {} attestation(s) into new block.", From 2b538510623cc8609f542617c948e876a7a348f5 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 29 Mar 2019 18:54:01 +1100 Subject: [PATCH 132/191] Pass errors back from block ops processing --- beacon_node/beacon_chain/src/beacon_chain.rs | 69 +++++++++++-------- .../test_harness/src/beacon_chain_harness.rs | 16 +++-- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 5eadb37636..4d3ba9cab9 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -7,10 +7,15 @@ use db::{ }; use fork_choice::{ForkChoice, ForkChoiceError}; use log::{debug, trace, warn}; +use operation_pool::DepositInsertStatus; use operation_pool::OperationPool; use parking_lot::{RwLock, RwLockReadGuard}; use slot_clock::SlotClock; use ssz::ssz_encode; +pub use state_processing::per_block_processing::errors::{ + AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, + ExitValidationError, ProposerSlashingValidationError, TransferValidationError, +}; use state_processing::{ per_block_processing, per_block_processing_without_verifying_block_signature, per_slot_processing, BlockProcessingError, SlotProcessingError, @@ -545,57 +550,67 @@ where /// /// If valid, the attestation is added to the `op_pool` and aggregated with another attestation /// if possible. - pub fn process_attestation(&self, attestation: Attestation) { - let _ = - self.op_pool - .write() - .insert_attestation(attestation, &*self.state.read(), &self.spec); + pub fn process_attestation( + &self, + attestation: Attestation, + ) -> Result<(), AttestationValidationError> { + self.op_pool + .write() + .insert_attestation(attestation, &*self.state.read(), &self.spec) } /// Accept some deposit and queue it for inclusion in an appropriate block. - pub fn receive_deposit_for_inclusion(&self, deposit: Deposit) { - // Bad deposits are ignored. - let _ = self - .op_pool + pub fn receive_deposit_for_inclusion( + &self, + deposit: Deposit, + ) -> Result { + self.op_pool .write() - .insert_deposit(deposit, &*self.state.read(), &self.spec); + .insert_deposit(deposit, &*self.state.read(), &self.spec) } /// Accept some exit and queue it for inclusion in an appropriate block. - pub fn receive_exit_for_inclusion(&self, exit: VoluntaryExit) { - // Bad exits are ignored - let _ = self - .op_pool + pub fn receive_exit_for_inclusion( + &self, + exit: VoluntaryExit, + ) -> Result<(), ExitValidationError> { + self.op_pool .write() - .insert_voluntary_exit(exit, &*self.state.read(), &self.spec); + .insert_voluntary_exit(exit, &*self.state.read(), &self.spec) } /// Accept some transfer and queue it for inclusion in an appropriate block. - pub fn receive_transfer_for_inclusion(&self, transfer: Transfer) { - // Bad transfers are ignored. - let _ = self - .op_pool + pub fn receive_transfer_for_inclusion( + &self, + transfer: Transfer, + ) -> Result<(), TransferValidationError> { + self.op_pool .write() - .insert_transfer(transfer, &*self.state.read(), &self.spec); + .insert_transfer(transfer, &*self.state.read(), &self.spec) } /// Accept some proposer slashing and queue it for inclusion in an appropriate block. - pub fn receive_proposer_slashing_for_inclusion(&self, proposer_slashing: ProposerSlashing) { - // Bad proposer slashings are ignored. - let _ = self.op_pool.write().insert_proposer_slashing( + pub fn receive_proposer_slashing_for_inclusion( + &self, + proposer_slashing: ProposerSlashing, + ) -> Result<(), ProposerSlashingValidationError> { + self.op_pool.write().insert_proposer_slashing( proposer_slashing, &*self.state.read(), &self.spec, - ); + ) } /// Accept some attester slashing and queue it for inclusion in an appropriate block. - pub fn receive_attester_slashing_for_inclusion(&self, attester_slashing: AttesterSlashing) { - let _ = self.op_pool.write().insert_attester_slashing( + pub fn receive_attester_slashing_for_inclusion( + &self, + attester_slashing: AttesterSlashing, + ) -> Result<(), AttesterSlashingValidationError> { + self.op_pool.write().insert_attester_slashing( attester_slashing, &*self.state.read(), &self.spec, - ); + ) } /// Returns `true` if the given block root has not been processed. 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 9424974763..0784e5fd3d 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 @@ -285,7 +285,9 @@ impl BeaconChainHarness { /// If a new `ValidatorHarness` was created, the validator should become fully operational as /// if the validator were created during `BeaconChainHarness` instantiation. pub fn add_deposit(&mut self, deposit: Deposit, keypair: Option) { - self.beacon_chain.receive_deposit_for_inclusion(deposit); + self.beacon_chain + .receive_deposit_for_inclusion(deposit) + .unwrap(); // If a keypair is present, add a new `ValidatorHarness` to the rig. if let Some(keypair) = keypair { @@ -301,24 +303,28 @@ impl BeaconChainHarness { /// will stop receiving duties from the beacon chain and just do nothing when prompted to /// produce/attest. pub fn add_exit(&mut self, exit: VoluntaryExit) { - self.beacon_chain.receive_exit_for_inclusion(exit); + self.beacon_chain.receive_exit_for_inclusion(exit).unwrap(); } /// Submit an transfer to the `BeaconChain` for inclusion in some block. pub fn add_transfer(&mut self, transfer: Transfer) { - self.beacon_chain.receive_transfer_for_inclusion(transfer); + self.beacon_chain + .receive_transfer_for_inclusion(transfer) + .unwrap(); } /// Submit a proposer slashing to the `BeaconChain` for inclusion in some block. pub fn add_proposer_slashing(&mut self, proposer_slashing: ProposerSlashing) { self.beacon_chain - .receive_proposer_slashing_for_inclusion(proposer_slashing); + .receive_proposer_slashing_for_inclusion(proposer_slashing) + .unwrap(); } /// Submit an attester slashing to the `BeaconChain` for inclusion in some block. pub fn add_attester_slashing(&mut self, attester_slashing: AttesterSlashing) { self.beacon_chain - .receive_attester_slashing_for_inclusion(attester_slashing); + .receive_attester_slashing_for_inclusion(attester_slashing) + .unwrap(); } /// Executes the fork choice rule on the `BeaconChain`, selecting a new canonical head. From 8bf7a83f373a34070fb7765daaa7dd20fd28867c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 29 Mar 2019 19:09:01 +1100 Subject: [PATCH 133/191] Rename op processing methods on BeaconChain --- beacon_node/beacon_chain/src/beacon_chain.rs | 28 ++++++++----------- .../test_harness/src/beacon_chain_harness.rs | 14 ++++------ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 4d3ba9cab9..d3b9e2bdcf 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -6,7 +6,7 @@ use db::{ ClientDB, DBError, }; use fork_choice::{ForkChoice, ForkChoiceError}; -use log::{debug, trace, warn}; +use log::{debug, trace}; use operation_pool::DepositInsertStatus; use operation_pool::OperationPool; use parking_lot::{RwLock, RwLockReadGuard}; @@ -560,7 +560,7 @@ where } /// Accept some deposit and queue it for inclusion in an appropriate block. - pub fn receive_deposit_for_inclusion( + pub fn process_deposit( &self, deposit: Deposit, ) -> Result { @@ -570,27 +570,21 @@ where } /// Accept some exit and queue it for inclusion in an appropriate block. - pub fn receive_exit_for_inclusion( - &self, - exit: VoluntaryExit, - ) -> Result<(), ExitValidationError> { + pub fn process_voluntary_exit(&self, exit: VoluntaryExit) -> Result<(), ExitValidationError> { self.op_pool .write() .insert_voluntary_exit(exit, &*self.state.read(), &self.spec) } /// Accept some transfer and queue it for inclusion in an appropriate block. - pub fn receive_transfer_for_inclusion( - &self, - transfer: Transfer, - ) -> Result<(), TransferValidationError> { + pub fn process_transfer(&self, transfer: Transfer) -> Result<(), TransferValidationError> { self.op_pool .write() .insert_transfer(transfer, &*self.state.read(), &self.spec) } /// Accept some proposer slashing and queue it for inclusion in an appropriate block. - pub fn receive_proposer_slashing_for_inclusion( + pub fn process_proposer_slashing( &self, proposer_slashing: ProposerSlashing, ) -> Result<(), ProposerSlashingValidationError> { @@ -602,7 +596,7 @@ where } /// Accept some attester slashing and queue it for inclusion in an appropriate block. - pub fn receive_attester_slashing_for_inclusion( + pub fn process_attester_slashing( &self, attester_slashing: AttesterSlashing, ) -> Result<(), AttesterSlashingValidationError> { @@ -613,11 +607,6 @@ where ) } - /// Returns `true` if the given block root has not been processed. - pub fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result { - Ok(!self.block_store.exists(beacon_block_root)?) - } - /// Accept some block and attempt to add it to block DAG. /// /// Will accept blocks from prior slots, however it will reject any block from a future slot. @@ -817,6 +806,11 @@ where Ok(()) } + /// Returns `true` if the given block root has not been processed. + pub fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result { + Ok(!self.block_store.exists(beacon_block_root)?) + } + /// Dumps the entire canonical chain, from the head to genesis to a vector for analysis. /// /// This could be a very expensive operation and should only be done in testing/analysis 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 0784e5fd3d..7d6a690e09 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 @@ -285,9 +285,7 @@ impl BeaconChainHarness { /// If a new `ValidatorHarness` was created, the validator should become fully operational as /// if the validator were created during `BeaconChainHarness` instantiation. pub fn add_deposit(&mut self, deposit: Deposit, keypair: Option) { - self.beacon_chain - .receive_deposit_for_inclusion(deposit) - .unwrap(); + self.beacon_chain.process_deposit(deposit).unwrap(); // If a keypair is present, add a new `ValidatorHarness` to the rig. if let Some(keypair) = keypair { @@ -303,27 +301,25 @@ impl BeaconChainHarness { /// will stop receiving duties from the beacon chain and just do nothing when prompted to /// produce/attest. pub fn add_exit(&mut self, exit: VoluntaryExit) { - self.beacon_chain.receive_exit_for_inclusion(exit).unwrap(); + self.beacon_chain.process_voluntary_exit(exit).unwrap(); } /// Submit an transfer to the `BeaconChain` for inclusion in some block. pub fn add_transfer(&mut self, transfer: Transfer) { - self.beacon_chain - .receive_transfer_for_inclusion(transfer) - .unwrap(); + self.beacon_chain.process_transfer(transfer).unwrap(); } /// Submit a proposer slashing to the `BeaconChain` for inclusion in some block. pub fn add_proposer_slashing(&mut self, proposer_slashing: ProposerSlashing) { self.beacon_chain - .receive_proposer_slashing_for_inclusion(proposer_slashing) + .process_proposer_slashing(proposer_slashing) .unwrap(); } /// Submit an attester slashing to the `BeaconChain` for inclusion in some block. pub fn add_attester_slashing(&mut self, attester_slashing: AttesterSlashing) { self.beacon_chain - .receive_attester_slashing_for_inclusion(attester_slashing) + .process_attester_slashing(attester_slashing) .unwrap(); } From e418cd11837e31207aeca4b0ddf5004e11e89efb Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 29 Mar 2019 23:45:53 +1100 Subject: [PATCH 134/191] Refactor main validator service --- .../src/block_producer/block_producer.rs | 219 -------------- validator_client/src/block_producer/grpc.rs | 86 ++++++ validator_client/src/block_producer/mod.rs | 272 +++++++++++++----- validator_client/src/main.rs | 4 +- validator_client/src/service.rs | 4 +- 5 files changed, 294 insertions(+), 291 deletions(-) delete mode 100644 validator_client/src/block_producer/block_producer.rs create mode 100644 validator_client/src/block_producer/grpc.rs diff --git a/validator_client/src/block_producer/block_producer.rs b/validator_client/src/block_producer/block_producer.rs deleted file mode 100644 index e71e6cd4bb..0000000000 --- a/validator_client/src/block_producer/block_producer.rs +++ /dev/null @@ -1,219 +0,0 @@ -use super::beacon_block_node::{BeaconBlockNode, BeaconBlockNodeError}; -use crate::signer::Signer; -use ssz::{SignedRoot, TreeHash}; -use std::sync::Arc; -use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; - -#[derive(Debug, PartialEq)] -pub enum Error { - SlotClockError, - SlotUnknowable, - EpochMapPoisoned, - SlotClockPoisoned, - EpochLengthIsZero, - BeaconBlockNodeError(BeaconBlockNodeError), -} - -#[derive(Debug, PartialEq)] -pub enum ValidatorEvent { - /// A new block was produced. - BlockProduced(Slot), - /// A block was not produced as it would have been slashable. - SlashableBlockNotProduced(Slot), - /// The Beacon Node was unable to produce a block at that slot. - BeaconNodeUnableToProduceBlock(Slot), - /// The signer failed to sign the message. - SignerRejection(Slot), - /// The public key for this validator is not an active validator. - ValidatorIsUnknown(Slot), -} - -/// This struct contains the logic for requesting and signing beacon blocks for a validator. The -/// validator can abstractly sign via the Signer trait object. -pub struct BlockProducer { - /// The current fork. - pub fork: Fork, - /// The current slot to produce a block for. - pub slot: Slot, - /// The current epoch. - pub spec: Arc, - /// The beacon node to connect to. - pub beacon_node: Arc, - /// The signer to sign the block. - pub signer: Arc, -} - -impl BlockProducer { - /// Produce a block at some slot. - /// - /// Assumes that a block is required at this slot (does not check the duties). - /// - /// Ensures the message is not slashable. - /// - /// !!! UNSAFE !!! - /// - /// The slash-protection code is not yet implemented. There is zero protection against - /// slashing. - fn produce_block(&mut self) -> Result { - let epoch = self.slot.epoch(self.spec.slots_per_epoch); - - let randao_reveal = { - let message = epoch.hash_tree_root(); - let randao_reveal = match self.signer.sign_randao_reveal( - &message, - self.spec.get_domain(epoch, Domain::Randao, &self.fork), - ) { - None => return Ok(ValidatorEvent::SignerRejection(self.slot)), - Some(signature) => signature, - }; - randao_reveal - }; - - if let Some(block) = self - .beacon_node - .produce_beacon_block(self.slot, &randao_reveal)? - { - if self.safe_to_produce(&block) { - let domain = self.spec.get_domain(epoch, Domain::BeaconBlock, &self.fork); - if let Some(block) = self.sign_block(block, domain) { - self.beacon_node.publish_beacon_block(block)?; - Ok(ValidatorEvent::BlockProduced(self.slot)) - } else { - Ok(ValidatorEvent::SignerRejection(self.slot)) - } - } else { - Ok(ValidatorEvent::SlashableBlockNotProduced(self.slot)) - } - } else { - Ok(ValidatorEvent::BeaconNodeUnableToProduceBlock(self.slot)) - } - } - - /// Consumes a block, returning that block signed by the validators private key. - /// - /// Important: this function will not check to ensure the block is not slashable. This must be - /// done upstream. - fn sign_block(&mut self, mut block: BeaconBlock, domain: u64) -> Option { - self.store_produce(&block); - - match self - .signer - .sign_block_proposal(&block.signed_root()[..], domain) - { - None => None, - Some(signature) => { - block.signature = signature; - Some(block) - } - } - } - - /// Returns `true` if signing a block is safe (non-slashable). - /// - /// !!! UNSAFE !!! - /// - /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. - fn safe_to_produce(&self, _block: &BeaconBlock) -> bool { - // TODO: ensure the producer doesn't produce slashable blocks. - // https://github.com/sigp/lighthouse/issues/160 - true - } - - /// Record that a block was produced so that slashable votes may not be made in the future. - /// - /// !!! UNSAFE !!! - /// - /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. - fn store_produce(&mut self, _block: &BeaconBlock) { - // TODO: record this block production to prevent future slashings. - // https://github.com/sigp/lighthouse/issues/160 - } -} - -impl From for Error { - fn from(e: BeaconBlockNodeError) -> Error { - Error::BeaconBlockNodeError(e) - } -} - -/* Old tests - Re-work for new logic -#[cfg(test)] -mod tests { - use super::test_utils::{EpochMap, LocalSigner, SimulatedBeaconNode}; - use super::*; - use slot_clock::TestingSlotClock; - use types::{ - test_utils::{SeedableRng, TestRandom, XorShiftRng}, - Keypair, - }; - - // TODO: implement more thorough testing. - // https://github.com/sigp/lighthouse/issues/160 - // - // These tests should serve as a good example for future tests. - - #[test] - pub fn polling() { - let mut rng = XorShiftRng::from_seed([42; 16]); - - let spec = Arc::new(ChainSpec::foundation()); - let slot_clock = Arc::new(TestingSlotClock::new(0)); - let beacon_node = Arc::new(SimulatedBeaconNode::default()); - let signer = Arc::new(LocalSigner::new(Keypair::random())); - - let mut epoch_map = EpochMap::new(spec.slots_per_epoch); - let produce_slot = Slot::new(100); - let produce_epoch = produce_slot.epoch(spec.slots_per_epoch); - epoch_map.map.insert(produce_epoch, produce_slot); - let epoch_map = Arc::new(epoch_map); - - let mut block_proposer = BlockProducer::new( - spec.clone(), - epoch_map.clone(), - slot_clock.clone(), - beacon_node.clone(), - signer.clone(), - ); - - // Configure responses from the BeaconNode. - beacon_node.set_next_produce_result(Ok(Some(BeaconBlock::random_for_test(&mut rng)))); - beacon_node.set_next_publish_result(Ok(PublishOutcome::ValidBlock)); - - // One slot before production slot... - slot_clock.set_slot(produce_slot.as_u64() - 1); - assert_eq!( - block_proposer.poll(), - Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1)) - ); - - // On the produce slot... - slot_clock.set_slot(produce_slot.as_u64()); - assert_eq!( - block_proposer.poll(), - Ok(PollOutcome::BlockProduced(produce_slot.into())) - ); - - // Trying the same produce slot again... - slot_clock.set_slot(produce_slot.as_u64()); - assert_eq!( - block_proposer.poll(), - Ok(PollOutcome::SlotAlreadyProcessed(produce_slot)) - ); - - // One slot after the produce slot... - slot_clock.set_slot(produce_slot.as_u64() + 1); - assert_eq!( - block_proposer.poll(), - Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1)) - ); - - // In an epoch without known duties... - let slot = (produce_epoch.as_u64() + 1) * spec.slots_per_epoch; - slot_clock.set_slot(slot); - assert_eq!( - block_proposer.poll(), - Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot))) - ); - } -} -*/ diff --git a/validator_client/src/block_producer/grpc.rs b/validator_client/src/block_producer/grpc.rs new file mode 100644 index 0000000000..23477ece14 --- /dev/null +++ b/validator_client/src/block_producer/grpc.rs @@ -0,0 +1,86 @@ +use super::beacon_block_node::*; +use protos::services::{ + BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, +}; +use protos::services_grpc::BeaconBlockServiceClient; +use ssz::{ssz_encode, Decodable}; +use std::sync::Arc; +use types::{BeaconBlock, Signature, Slot}; + +/// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be +/// implemented upon it. +pub struct BeaconBlockGrpcClient { + client: Arc, +} + +impl BeaconBlockGrpcClient { + pub fn new(client: Arc) -> Self { + Self { client } + } +} + +impl BeaconBlockNode for BeaconBlockGrpcClient { + /// Request a Beacon Node (BN) to produce a new block at the supplied slot. + /// + /// Returns `None` if it is not possible to produce at the supplied slot. For example, if the + /// BN is unable to find a parent block. + fn produce_beacon_block( + &self, + slot: Slot, + randao_reveal: &Signature, + ) -> Result, BeaconBlockNodeError> { + // request a beacon block from the node + let mut req = ProduceBeaconBlockRequest::new(); + req.set_slot(slot.as_u64()); + req.set_randao_reveal(ssz_encode(randao_reveal)); + + //TODO: Determine if we want an explicit timeout + let reply = self + .client + .produce_beacon_block(&req) + .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; + + // format the reply + if reply.has_block() { + let block = reply.get_block(); + let ssz = block.get_ssz(); + + let (block, _i) = BeaconBlock::ssz_decode(&ssz, 0) + .map_err(|_| BeaconBlockNodeError::DecodeFailure)?; + + Ok(Some(block)) + } else { + Ok(None) + } + } + + /// Request a Beacon Node (BN) to publish a block. + /// + /// Generally, this will be called after a `produce_beacon_block` call with a block that has + /// been completed (signed) by the validator client. + fn publish_beacon_block( + &self, + block: BeaconBlock, + ) -> Result { + let mut req = PublishBeaconBlockRequest::new(); + + let ssz = ssz_encode(&block); + + let mut grpc_block = GrpcBeaconBlock::new(); + grpc_block.set_ssz(ssz); + + req.set_block(grpc_block); + + let reply = self + .client + .publish_beacon_block(&req) + .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; + + if reply.get_success() { + Ok(PublishOutcome::ValidBlock) + } else { + // TODO: distinguish between different errors + Ok(PublishOutcome::InvalidBlock("Publish failed".to_string())) + } + } +} diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index fe34f627d8..8297469baf 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -1,89 +1,223 @@ mod beacon_block_node; -mod block_producer; +mod grpc; -use self::beacon_block_node::*; -use protos::services::{ - BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, -}; -use protos::services_grpc::BeaconBlockServiceClient; -use ssz::{ssz_encode, Decodable}; +use self::beacon_block_node::{BeaconBlockNode, BeaconBlockNodeError}; +pub use self::grpc::BeaconBlockGrpcClient; +use crate::signer::Signer; +use ssz::{SignedRoot, TreeHash}; use std::sync::Arc; -use types::{BeaconBlock, Signature, Slot}; +use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; -/// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be -/// implemented upon it. -pub struct BeaconBlockGrpcClient { - client: Arc, +#[derive(Debug, PartialEq)] +pub enum Error { + SlotClockError, + SlotUnknowable, + EpochMapPoisoned, + SlotClockPoisoned, + EpochLengthIsZero, + BeaconBlockNodeError(BeaconBlockNodeError), } -impl BeaconBlockGrpcClient { - pub fn new(client: Arc) -> Self { - Self { client } - } +#[derive(Debug, PartialEq)] +pub enum ValidatorEvent { + /// A new block was produced. + BlockProduced(Slot), + /// A block was not produced as it would have been slashable. + SlashableBlockNotProduced(Slot), + /// The Beacon Node was unable to produce a block at that slot. + BeaconNodeUnableToProduceBlock(Slot), + /// The signer failed to sign the message. + SignerRejection(Slot), + /// The public key for this validator is not an active validator. + ValidatorIsUnknown(Slot), } -impl BeaconBlockNode for BeaconBlockGrpcClient { - /// Request a Beacon Node (BN) to produce a new block at the supplied slot. +/// This struct contains the logic for requesting and signing beacon blocks for a validator. The +/// validator can abstractly sign via the Signer trait object. +pub struct BlockProducer { + /// The current fork. + pub fork: Fork, + /// The current slot to produce a block for. + pub slot: Slot, + /// The current epoch. + pub spec: Arc, + /// The beacon node to connect to. + pub beacon_node: Arc, + /// The signer to sign the block. + pub signer: Arc, +} + +impl BlockProducer { + /// Produce a block at some slot. /// - /// Returns `None` if it is not possible to produce at the supplied slot. For example, if the - /// BN is unable to find a parent block. - fn produce_beacon_block( - &self, - slot: Slot, - randao_reveal: &Signature, - ) -> Result, BeaconBlockNodeError> { - // request a beacon block from the node - let mut req = ProduceBeaconBlockRequest::new(); - req.set_slot(slot.as_u64()); - req.set_randao_reveal(ssz_encode(randao_reveal)); + /// Assumes that a block is required at this slot (does not check the duties). + /// + /// Ensures the message is not slashable. + /// + /// !!! UNSAFE !!! + /// + /// The slash-protection code is not yet implemented. There is zero protection against + /// slashing. + fn produce_block(&mut self) -> Result { + let epoch = self.slot.epoch(self.spec.slots_per_epoch); - //TODO: Determine if we want an explicit timeout - let reply = self - .client - .produce_beacon_block(&req) - .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; + let randao_reveal = { + let message = epoch.hash_tree_root(); + let randao_reveal = match self.signer.sign_randao_reveal( + &message, + self.spec.get_domain(epoch, Domain::Randao, &self.fork), + ) { + None => return Ok(ValidatorEvent::SignerRejection(self.slot)), + Some(signature) => signature, + }; + randao_reveal + }; - // format the reply - if reply.has_block() { - let block = reply.get_block(); - let ssz = block.get_ssz(); - - let (block, _i) = BeaconBlock::ssz_decode(&ssz, 0) - .map_err(|_| BeaconBlockNodeError::DecodeFailure)?; - - Ok(Some(block)) + if let Some(block) = self + .beacon_node + .produce_beacon_block(self.slot, &randao_reveal)? + { + if self.safe_to_produce(&block) { + let domain = self.spec.get_domain(epoch, Domain::BeaconBlock, &self.fork); + if let Some(block) = self.sign_block(block, domain) { + self.beacon_node.publish_beacon_block(block)?; + Ok(ValidatorEvent::BlockProduced(self.slot)) + } else { + Ok(ValidatorEvent::SignerRejection(self.slot)) + } + } else { + Ok(ValidatorEvent::SlashableBlockNotProduced(self.slot)) + } } else { - Ok(None) + Ok(ValidatorEvent::BeaconNodeUnableToProduceBlock(self.slot)) } } - /// Request a Beacon Node (BN) to publish a block. + /// Consumes a block, returning that block signed by the validators private key. /// - /// Generally, this will be called after a `produce_beacon_block` call with a block that has - /// been completed (signed) by the validator client. - fn publish_beacon_block( - &self, - block: BeaconBlock, - ) -> Result { - let mut req = PublishBeaconBlockRequest::new(); + /// Important: this function will not check to ensure the block is not slashable. This must be + /// done upstream. + fn sign_block(&mut self, mut block: BeaconBlock, domain: u64) -> Option { + self.store_produce(&block); - let ssz = ssz_encode(&block); - - let mut grpc_block = GrpcBeaconBlock::new(); - grpc_block.set_ssz(ssz); - - req.set_block(grpc_block); - - let reply = self - .client - .publish_beacon_block(&req) - .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; - - if reply.get_success() { - Ok(PublishOutcome::ValidBlock) - } else { - // TODO: distinguish between different errors - Ok(PublishOutcome::InvalidBlock("Publish failed".to_string())) + match self + .signer + .sign_block_proposal(&block.signed_root()[..], domain) + { + None => None, + Some(signature) => { + block.signature = signature; + Some(block) + } } } + + /// Returns `true` if signing a block is safe (non-slashable). + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn safe_to_produce(&self, _block: &BeaconBlock) -> bool { + // TODO: ensure the producer doesn't produce slashable blocks. + // https://github.com/sigp/lighthouse/issues/160 + true + } + + /// Record that a block was produced so that slashable votes may not be made in the future. + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn store_produce(&mut self, _block: &BeaconBlock) { + // TODO: record this block production to prevent future slashings. + // https://github.com/sigp/lighthouse/issues/160 + } } + +impl From for Error { + fn from(e: BeaconBlockNodeError) -> Error { + Error::BeaconBlockNodeError(e) + } +} + +/* Old tests - Re-work for new logic +#[cfg(test)] +mod tests { + use super::test_utils::{EpochMap, LocalSigner, SimulatedBeaconNode}; + use super::*; + use slot_clock::TestingSlotClock; + use types::{ + test_utils::{SeedableRng, TestRandom, XorShiftRng}, + Keypair, + }; + + // TODO: implement more thorough testing. + // https://github.com/sigp/lighthouse/issues/160 + // + // These tests should serve as a good example for future tests. + + #[test] + pub fn polling() { + let mut rng = XorShiftRng::from_seed([42; 16]); + + let spec = Arc::new(ChainSpec::foundation()); + let slot_clock = Arc::new(TestingSlotClock::new(0)); + let beacon_node = Arc::new(SimulatedBeaconNode::default()); + let signer = Arc::new(LocalSigner::new(Keypair::random())); + + let mut epoch_map = EpochMap::new(spec.slots_per_epoch); + let produce_slot = Slot::new(100); + let produce_epoch = produce_slot.epoch(spec.slots_per_epoch); + epoch_map.map.insert(produce_epoch, produce_slot); + let epoch_map = Arc::new(epoch_map); + + let mut block_proposer = BlockProducer::new( + spec.clone(), + epoch_map.clone(), + slot_clock.clone(), + beacon_node.clone(), + signer.clone(), + ); + + // Configure responses from the BeaconNode. + beacon_node.set_next_produce_result(Ok(Some(BeaconBlock::random_for_test(&mut rng)))); + beacon_node.set_next_publish_result(Ok(PublishOutcome::ValidBlock)); + + // One slot before production slot... + slot_clock.set_slot(produce_slot.as_u64() - 1); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1)) + ); + + // On the produce slot... + slot_clock.set_slot(produce_slot.as_u64()); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::BlockProduced(produce_slot.into())) + ); + + // Trying the same produce slot again... + slot_clock.set_slot(produce_slot.as_u64()); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::SlotAlreadyProcessed(produce_slot)) + ); + + // One slot after the produce slot... + slot_clock.set_slot(produce_slot.as_u64() + 1); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1)) + ); + + // In an epoch without known duties... + let slot = (produce_epoch.as_u64() + 1) * spec.slots_per_epoch; + slot_clock.set_slot(slot); + assert_eq!( + block_proposer.poll(), + Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot))) + ); + } +} +*/ diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 6f02cf6eed..b3a488fe08 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -8,6 +8,7 @@ mod signer; use crate::config::Config as ValidatorClientConfig; use clap::{App, Arg}; +use protos::services_grpc::ValidatorServiceClient; use service::Service as ValidatorService; use slog::{error, info, o, Drain}; @@ -53,7 +54,8 @@ fn main() { .expect("Unable to build a configuration for the validator client."); // start the validator service. - match ValidatorService::start(config, log.clone()) { + // this specifies the GRPC type to use as the duty manager beacon node. + match ValidatorService::::start(config, log.clone()) { Ok(_) => info!(log, "Validator client shutdown successfully."), 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 398f6d7770..c8874a1f62 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -9,8 +9,7 @@ /// data from the beacon node and performs the signing before publishing the block to the beacon /// node. use crate::attester_service::{AttestationGrpcClient, AttesterService}; -use crate::block_producer::BlockProducer; -use crate::block_producer_service::BeaconBlockGrpcClient; +use crate::block_producer::{BeaconBlockGrpcClient, BlockProducer}; use crate::config::Config as ValidatorConfig; use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap, UpdateOutcome}; use crate::error as error_chain; @@ -40,6 +39,7 @@ use types::{ChainSpec, Epoch, Fork, Slot}; /// The validator service. This is the main thread that executes and maintains validator /// duties. +//TODO: Generalize the BeaconNode types to use testing pub struct Service { /// The node we currently connected to. connected_node_version: String, From 1840248af8497e9704948bf01d862676ebd25956 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 12:00:31 +1100 Subject: [PATCH 135/191] Remove old queues from BeaconChain --- beacon_node/beacon_chain/src/beacon_chain.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index d3b9e2bdcf..41fcb1128b 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -90,11 +90,6 @@ pub struct BeaconChain { pub slot_clock: U, pub attestation_aggregator: RwLock, pub op_pool: RwLock, - pub deposits_for_inclusion: RwLock>, - pub exits_for_inclusion: RwLock>, - pub transfers_for_inclusion: RwLock>, - pub proposer_slashings_for_inclusion: RwLock>, - pub attester_slashings_for_inclusion: RwLock>, canonical_head: RwLock, finalized_head: RwLock, pub state: RwLock, @@ -149,11 +144,6 @@ where slot_clock, attestation_aggregator, op_pool: RwLock::new(OperationPool::new()), - deposits_for_inclusion: RwLock::new(vec![]), - exits_for_inclusion: RwLock::new(vec![]), - transfers_for_inclusion: RwLock::new(vec![]), - proposer_slashings_for_inclusion: RwLock::new(vec![]), - attester_slashings_for_inclusion: RwLock::new(vec![]), state: RwLock::new(genesis_state), finalized_head, canonical_head, From 97bb61371c666deae630fe6eb9006119168f8b9e Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 12:14:56 +1100 Subject: [PATCH 136/191] Correct compiler issues, re-introduce validator library --- validator_client/Cargo.toml | 4 ++++ validator_client/src/lib.rs | 3 +++ validator_client/src/service.rs | 25 ++++++++++++++----------- 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 validator_client/src/lib.rs diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index 209ebf25e7..80477c8eaa 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -8,6 +8,10 @@ edition = "2018" name = "validator_client" path = "src/main.rs" +[lib] +name = "validator_client" +path = "src/lib.rs" + [dependencies] block_proposer = { path = "../eth2/block_proposer" } attester = { path = "../eth2/attester" } diff --git a/validator_client/src/lib.rs b/validator_client/src/lib.rs new file mode 100644 index 0000000000..470a070e87 --- /dev/null +++ b/validator_client/src/lib.rs @@ -0,0 +1,3 @@ +pub mod config; + +pub use crate::config::Config; diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index c8874a1f62..bd1053a342 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -41,9 +41,7 @@ use types::{ChainSpec, Epoch, Fork, Slot}; /// duties. //TODO: Generalize the BeaconNode types to use testing pub struct Service { - /// The node we currently connected to. - connected_node_version: String, - /// The chain id we are processing on. + /// The node's current fork version we are processing on. fork: Fork, /// The slot clock for this service. slot_clock: SystemTimeSlotClock, @@ -67,7 +65,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) -> error_chain::Result { + 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()); @@ -162,8 +163,6 @@ impl Service { .map_err(|e| ErrorKind::SlotClockError(e))? .expect("Genesis must be in the future"); - let spec = Arc::new(config.spec); - /* Generate the duties manager */ // generate keypairs @@ -185,8 +184,9 @@ impl Service { beacon_node: validator_client, }); - Ok(Self { - connected_node_version: node_info.version, + let spec = Arc::new(config.spec); + + Ok(Service { fork, slot_clock, current_slot, @@ -199,9 +199,10 @@ impl Service { } /// Initialise the service then run the core thread. + // TODO: Improve handling of generic BeaconNode types, to stub grpcClient pub fn start(config: ValidatorConfig, log: slog::Logger) -> error_chain::Result<()> { // connect to the node and retrieve its properties and initialize the gRPC clients - let service = Service::initialize_service(config, log)?; + let mut service = Service::::initialize_service(config, log)?; // we have connected to a node and established its parameters. Spin up the core service @@ -221,7 +222,7 @@ impl Service { // 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); + let slot_duration = Duration::from_secs(service.spec.seconds_per_slot); //TODO: Handle checked add correctly Interval::new(Instant::now() + duration_to_next_slot, slot_duration) }; @@ -236,7 +237,7 @@ impl Service { Ok(()) }) .map_err(|e| format!("Service thread failed: {:?}", e)), - ); + )?; // validator client exited Ok(()) } @@ -300,11 +301,13 @@ impl Service { if work_type.produce_block { // spawns a thread to produce a beacon block std::thread::spawn(move || { + /* let block_producer = BlockProducer { fork: self.fork, slot: self.current_slot, spec: self.spec.clone(), }; + */ }); // TODO: Produce a beacon block in a new thread From cd9494181c80d5c1af40246ff9033674d2246e64 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 12:26:25 +1100 Subject: [PATCH 137/191] Push RwLock down into OperationPool There used to be one massive lock on `BeaconChain.op_pool`, however that would cause unnecessary blocking. --- beacon_node/beacon_chain/src/beacon_chain.rs | 51 ++------- eth2/operation_pool/Cargo.toml | 1 + eth2/operation_pool/src/lib.rs | 113 ++++++++++--------- 3 files changed, 75 insertions(+), 90 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 41fcb1128b..5e83fdd81b 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -89,7 +89,7 @@ pub struct BeaconChain { pub state_store: Arc>, pub slot_clock: U, pub attestation_aggregator: RwLock, - pub op_pool: RwLock, + pub op_pool: OperationPool, canonical_head: RwLock, finalized_head: RwLock, pub state: RwLock, @@ -143,7 +143,7 @@ where state_store, slot_clock, attestation_aggregator, - op_pool: RwLock::new(OperationPool::new()), + op_pool: OperationPool::new(), state: RwLock::new(genesis_state), finalized_head, canonical_head, @@ -545,7 +545,6 @@ where attestation: Attestation, ) -> Result<(), AttestationValidationError> { self.op_pool - .write() .insert_attestation(attestation, &*self.state.read(), &self.spec) } @@ -555,21 +554,18 @@ where deposit: Deposit, ) -> Result { self.op_pool - .write() .insert_deposit(deposit, &*self.state.read(), &self.spec) } /// Accept some exit and queue it for inclusion in an appropriate block. pub fn process_voluntary_exit(&self, exit: VoluntaryExit) -> Result<(), ExitValidationError> { self.op_pool - .write() .insert_voluntary_exit(exit, &*self.state.read(), &self.spec) } /// Accept some transfer and queue it for inclusion in an appropriate block. pub fn process_transfer(&self, transfer: Transfer) -> Result<(), TransferValidationError> { self.op_pool - .write() .insert_transfer(transfer, &*self.state.read(), &self.spec) } @@ -578,11 +574,8 @@ where &self, proposer_slashing: ProposerSlashing, ) -> Result<(), ProposerSlashingValidationError> { - self.op_pool.write().insert_proposer_slashing( - proposer_slashing, - &*self.state.read(), - &self.spec, - ) + self.op_pool + .insert_proposer_slashing(proposer_slashing, &*self.state.read(), &self.spec) } /// Accept some attester slashing and queue it for inclusion in an appropriate block. @@ -590,11 +583,8 @@ where &self, attester_slashing: AttesterSlashing, ) -> Result<(), AttesterSlashingValidationError> { - self.op_pool.write().insert_attester_slashing( - attester_slashing, - &*self.state.read(), - &self.spec, - ) + self.op_pool + .insert_attester_slashing(attester_slashing, &*self.state.read(), &self.spec) } /// Accept some block and attempt to add it to block DAG. @@ -705,24 +695,12 @@ where trace!("Finding attestations for new block..."); - let attestations = self - .op_pool - .read() - .get_attestations(&*self.state.read(), &self.spec); - - trace!( - "Inserting {} attestation(s) into new block.", - attestations.len() - ); - let previous_block_root = *state .get_block_root(state.slot - 1, &self.spec) .map_err(|_| BlockProductionError::UnableToGetBlockRootFromState)?; - let (proposer_slashings, attester_slashings) = self - .op_pool - .read() - .get_slashings(&*self.state.read(), &self.spec); + let (proposer_slashings, attester_slashings) = + self.op_pool.get_slashings(&*self.state.read(), &self.spec); let mut block = BeaconBlock { slot: state.slot, @@ -738,19 +716,14 @@ where }, proposer_slashings, attester_slashings, - attestations, - deposits: self + attestations: self .op_pool - .read() - .get_deposits(&*self.state.read(), &self.spec), + .get_attestations(&*self.state.read(), &self.spec), + deposits: self.op_pool.get_deposits(&*self.state.read(), &self.spec), voluntary_exits: self .op_pool - .read() .get_voluntary_exits(&*self.state.read(), &self.spec), - transfers: self - .op_pool - .read() - .get_transfers(&*self.state.read(), &self.spec), + transfers: self.op_pool.get_transfers(&*self.state.read(), &self.spec), }, }; diff --git a/eth2/operation_pool/Cargo.toml b/eth2/operation_pool/Cargo.toml index 07cb618649..67d13013ca 100644 --- a/eth2/operation_pool/Cargo.toml +++ b/eth2/operation_pool/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] int_to_bytes = { path = "../utils/int_to_bytes" } itertools = "0.8" +parking_lot = "0.7" types = { path = "../types" } state_processing = { path = "../state_processing" } ssz = { path = "../utils/ssz" } diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index c3de95b48d..c42527b608 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -1,5 +1,6 @@ use int_to_bytes::int_to_bytes8; use itertools::Itertools; +use parking_lot::RwLock; use ssz::ssz_encode; use state_processing::per_block_processing::errors::{ AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, @@ -26,21 +27,21 @@ const VERIFY_DEPOSIT_PROOFS: bool = false; // TODO: enable this #[derive(Default)] pub struct OperationPool { /// Map from attestation ID (see below) to vectors of attestations. - attestations: HashMap>, + attestations: RwLock>>, /// Map from deposit index to deposit data. // NOTE: We assume that there is only one deposit per index // because the Eth1 data is updated (at most) once per epoch, // and the spec doesn't seem to accomodate for re-orgs on a time-frame // longer than an epoch - deposits: BTreeMap, + deposits: RwLock>, /// Map from two attestation IDs to a slashing for those IDs. - attester_slashings: HashMap<(AttestationId, AttestationId), AttesterSlashing>, + attester_slashings: RwLock>, /// Map from proposer index to slashing. - proposer_slashings: HashMap, + proposer_slashings: RwLock>, /// Map from exiting validator to their exit data. - voluntary_exits: HashMap, + voluntary_exits: RwLock>, /// Set of transfers. - transfers: HashSet, + transfers: RwLock>, } /// Serialized `AttestationData` augmented with a domain to encode the fork info. @@ -109,7 +110,7 @@ impl OperationPool { /// Insert an attestation into the pool, aggregating it with existing attestations if possible. pub fn insert_attestation( - &mut self, + &self, attestation: Attestation, state: &BeaconState, spec: &ChainSpec, @@ -119,7 +120,10 @@ impl OperationPool { let id = AttestationId::from_data(&attestation.data, state, spec); - let existing_attestations = match self.attestations.entry(id) { + // Take a write lock on the attestations map. + let mut attestations = self.attestations.write(); + + let existing_attestations = match attestations.entry(id) { hash_map::Entry::Vacant(entry) => { entry.insert(vec![attestation]); return Ok(()); @@ -146,7 +150,11 @@ impl OperationPool { /// Total number of attestations in the pool, including attestations for the same data. pub fn num_attestations(&self) -> usize { - self.attestations.values().map(|atts| atts.len()).sum() + self.attestations + .read() + .values() + .map(|atts| atts.len()) + .sum() } /// Get a list of attestations for inclusion in a block. @@ -157,6 +165,7 @@ impl OperationPool { let prev_domain_bytes = AttestationId::compute_domain_bytes(prev_epoch, state, spec); let curr_domain_bytes = AttestationId::compute_domain_bytes(current_epoch, state, spec); self.attestations + .read() .iter() .filter(|(key, _)| { key.domain_bytes_match(&prev_domain_bytes) @@ -180,8 +189,8 @@ impl OperationPool { // TODO: we could probably prune other attestations here: // - ones that are completely covered by attestations included in the state // - maybe ones invalidated by the confirmation of one fork over another - pub fn prune_attestations(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { - self.attestations.retain(|_, attestations| { + pub fn prune_attestations(&self, finalized_state: &BeaconState, spec: &ChainSpec) { + self.attestations.write().retain(|_, attestations| { // All the attestations in this bucket have the same data, so we only need to // check the first one. attestations.first().map_or(false, |att| { @@ -194,14 +203,14 @@ impl OperationPool { /// /// No two distinct deposits should be added with the same index. pub fn insert_deposit( - &mut self, + &self, deposit: Deposit, state: &BeaconState, spec: &ChainSpec, ) -> Result { use DepositInsertStatus::*; - match self.deposits.entry(deposit.index) { + match self.deposits.write().entry(deposit.index) { Entry::Vacant(entry) => { verify_deposit(state, &deposit, VERIFY_DEPOSIT_PROOFS, spec)?; entry.insert(deposit); @@ -224,27 +233,26 @@ impl OperationPool { pub fn get_deposits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { let start_idx = state.deposit_index; (start_idx..start_idx + spec.max_deposits) - .map(|idx| self.deposits.get(&idx)) + .map(|idx| self.deposits.read().get(&idx).cloned()) .take_while(Option::is_some) .flatten() - .cloned() .collect() } /// Remove all deposits with index less than the deposit index of the latest finalised block. - pub fn prune_deposits(&mut self, state: &BeaconState) -> BTreeMap { - let deposits_keep = self.deposits.split_off(&state.deposit_index); - std::mem::replace(&mut self.deposits, deposits_keep) + pub fn prune_deposits(&self, state: &BeaconState) -> BTreeMap { + let deposits_keep = self.deposits.write().split_off(&state.deposit_index); + std::mem::replace(&mut self.deposits.write(), deposits_keep) } /// The number of deposits stored in the pool. pub fn num_deposits(&self) -> usize { - self.deposits.len() + self.deposits.read().len() } /// Insert a proposer slashing into the pool. pub fn insert_proposer_slashing( - &mut self, + &self, slashing: ProposerSlashing, state: &BeaconState, spec: &ChainSpec, @@ -253,6 +261,7 @@ impl OperationPool { // because they could *become* known later verify_proposer_slashing(&slashing, state, spec)?; self.proposer_slashings + .write() .insert(slashing.proposer_index, slashing); Ok(()) } @@ -273,14 +282,14 @@ impl OperationPool { /// Insert an attester slashing into the pool. pub fn insert_attester_slashing( - &mut self, + &self, slashing: AttesterSlashing, state: &BeaconState, spec: &ChainSpec, ) -> Result<(), AttesterSlashingValidationError> { verify_attester_slashing(state, &slashing, true, spec)?; let id = Self::attester_slashing_id(&slashing, state, spec); - self.attester_slashings.insert(id, slashing); + self.attester_slashings.write().insert(id, slashing); Ok(()) } @@ -295,7 +304,7 @@ impl OperationPool { spec: &ChainSpec, ) -> (Vec, Vec) { let proposer_slashings = filter_limit_operations( - self.proposer_slashings.values(), + self.proposer_slashings.read().values(), |slashing| { state .validator_registry @@ -314,6 +323,7 @@ impl OperationPool { let attester_slashings = self .attester_slashings + .read() .iter() .filter(|(id, slashing)| { // Check the fork. @@ -345,9 +355,9 @@ impl OperationPool { } /// Prune proposer slashings for all slashed or withdrawn validators. - pub fn prune_proposer_slashings(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + pub fn prune_proposer_slashings(&self, finalized_state: &BeaconState, spec: &ChainSpec) { prune_validator_hash_map( - &mut self.proposer_slashings, + &mut self.proposer_slashings.write(), |validator| { validator.slashed || validator.is_withdrawable_at(finalized_state.current_epoch(spec)) @@ -358,8 +368,8 @@ impl OperationPool { /// Prune attester slashings for all slashed or withdrawn validators, or attestations on another /// fork. - pub fn prune_attester_slashings(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { - self.attester_slashings.retain(|id, slashing| { + pub fn prune_attester_slashings(&self, finalized_state: &BeaconState, spec: &ChainSpec) { + self.attester_slashings.write().retain(|id, slashing| { let fork_ok = &Self::attester_slashing_id(slashing, finalized_state, spec) == id; let curr_epoch = finalized_state.current_epoch(spec); let slashing_ok = gather_attester_slashing_indices_modular( @@ -375,29 +385,31 @@ impl OperationPool { /// Insert a voluntary exit, validating it almost-entirely (future exits are permitted). pub fn insert_voluntary_exit( - &mut self, + &self, exit: VoluntaryExit, state: &BeaconState, spec: &ChainSpec, ) -> Result<(), ExitValidationError> { verify_exit_time_independent_only(state, &exit, spec)?; - self.voluntary_exits.insert(exit.validator_index, exit); + self.voluntary_exits + .write() + .insert(exit.validator_index, exit); Ok(()) } /// Get a list of voluntary exits for inclusion in a block. pub fn get_voluntary_exits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { filter_limit_operations( - self.voluntary_exits.values(), + self.voluntary_exits.read().values(), |exit| verify_exit(state, exit, spec).is_ok(), spec.max_voluntary_exits, ) } /// Prune if validator has already exited at the last finalized state. - pub fn prune_voluntary_exits(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + pub fn prune_voluntary_exits(&self, finalized_state: &BeaconState, spec: &ChainSpec) { prune_validator_hash_map( - &mut self.voluntary_exits, + &mut self.voluntary_exits.write(), |validator| validator.is_exited_at(finalized_state.current_epoch(spec)), finalized_state, ); @@ -405,7 +417,7 @@ impl OperationPool { /// Insert a transfer into the pool, checking it for validity in the process. pub fn insert_transfer( - &mut self, + &self, transfer: Transfer, state: &BeaconState, spec: &ChainSpec, @@ -414,7 +426,7 @@ impl OperationPool { // it before we insert into the HashSet, we can't end up with duplicate // transactions. verify_transfer_time_independent_only(state, &transfer, spec)?; - self.transfers.insert(transfer); + self.transfers.write().insert(transfer); Ok(()) } @@ -423,6 +435,7 @@ impl OperationPool { // dependencies between transfers in the same block e.g. A pays B, B pays C pub fn get_transfers(&self, state: &BeaconState, spec: &ChainSpec) -> Vec { self.transfers + .read() .iter() .filter(|transfer| verify_transfer(state, transfer, spec).is_ok()) .sorted_by_key(|transfer| std::cmp::Reverse(transfer.fee)) @@ -432,16 +445,14 @@ impl OperationPool { } /// Prune the set of transfers by removing all those whose slot has already passed. - pub fn prune_transfers(&mut self, finalized_state: &BeaconState) { - self.transfers = self - .transfers - .drain() - .filter(|transfer| transfer.slot > finalized_state.slot) - .collect(); + pub fn prune_transfers(&self, finalized_state: &BeaconState) { + self.transfers + .write() + .retain(|transfer| transfer.slot > finalized_state.slot) } /// Prune all types of transactions given the latest finalized state. - pub fn prune_all(&mut self, finalized_state: &BeaconState, spec: &ChainSpec) { + pub fn prune_all(&self, finalized_state: &BeaconState, spec: &ChainSpec) { self.prune_attestations(finalized_state, spec); self.prune_deposits(finalized_state); self.prune_proposer_slashings(finalized_state, spec); @@ -497,7 +508,7 @@ mod tests { fn insert_deposit() { let rng = &mut XorShiftRng::from_seed([42; 16]); let (ref spec, ref state) = test_state(rng); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let deposit1 = make_deposit(rng, state, spec); let mut deposit2 = make_deposit(rng, state, spec); deposit2.index = deposit1.index; @@ -520,7 +531,7 @@ mod tests { fn get_deposits_max() { let rng = &mut XorShiftRng::from_seed([42; 16]); let (spec, mut state) = test_state(rng); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let start = 10000; let max_deposits = spec.max_deposits; let extra = 5; @@ -550,7 +561,7 @@ mod tests { fn prune_deposits() { let rng = &mut XorShiftRng::from_seed([42; 16]); let (spec, state) = test_state(rng); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let start1 = 100; // test is super slow in debug mode if this parameter is too high @@ -734,7 +745,7 @@ mod tests { fn attestation_aggregation_insert_get_prune() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let slot = state.slot - 1; let committees = state @@ -765,7 +776,7 @@ mod tests { } } - assert_eq!(op_pool.attestations.len(), committees.len()); + assert_eq!(op_pool.attestations.read().len(), committees.len()); assert_eq!(op_pool.num_attestations(), committees.len()); // Before the min attestation inclusion delay, get_attestations shouldn't return anything. @@ -799,7 +810,7 @@ mod tests { fn attestation_duplicate() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let slot = state.slot - 1; let committees = state @@ -825,7 +836,7 @@ mod tests { fn attestation_pairwise_overlapping() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let slot = state.slot - 1; let committees = state @@ -854,7 +865,7 @@ mod tests { // The attestations should get aggregated into two attestations that comprise all // validators. - assert_eq!(op_pool.attestations.len(), committees.len()); + assert_eq!(op_pool.attestations.read().len(), committees.len()); assert_eq!(op_pool.num_attestations(), 2 * committees.len()); } @@ -869,7 +880,7 @@ mod tests { let small_step_size = 2; let big_step_size = 4; let (ref mut state, ref keypairs) = attestation_test_state(spec, big_step_size); - let mut op_pool = OperationPool::new(); + let op_pool = OperationPool::new(); let slot = state.slot - 1; let committees = state @@ -907,7 +918,7 @@ mod tests { let num_small = target_committee_size / small_step_size; let num_big = target_committee_size / big_step_size; - assert_eq!(op_pool.attestations.len(), committees.len()); + assert_eq!(op_pool.attestations.read().len(), committees.len()); assert_eq!( op_pool.num_attestations(), (num_small + num_big) * committees.len() From 89cc92572a1764be16c12dcdd8b205fea1770b23 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 13:03:05 +1100 Subject: [PATCH 138/191] Add `test_harness` tests for attestation count --- .../specs/validator_registry.yaml | 2 ++ .../test_harness/src/test_case/state_check.rs | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml b/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml index 0c4f5004bb..1674ecffc7 100644 --- a/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml +++ b/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml @@ -47,6 +47,8 @@ test_cases: states: - slot: 63 num_validators: 1003 + num_previous_epoch_attestations: 0 + num_current_epoch_attestations: 10 slashed_validators: [11, 12, 13, 14, 42] exited_validators: [] exit_initiated_validators: [50] diff --git a/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs b/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs index 4d2bfd07d6..7ac33c86c9 100644 --- a/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs +++ b/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs @@ -16,6 +16,10 @@ pub struct StateCheck { pub slot: Slot, /// Checked against `beacon_state.validator_registry.len()`. pub num_validators: Option, + /// The number of pending attestations from the previous epoch that should be in the state. + pub num_previous_epoch_attestations: Option, + /// The number of pending attestations from the current epoch that should be in the state. + pub num_current_epoch_attestations: Option, /// A list of validator indices which have been penalized. Must be in ascending order. pub slashed_validators: Option>, /// A list of validator indices which have been fully exited. Must be in ascending order. @@ -34,6 +38,8 @@ impl StateCheck { Self { slot: Slot::from(as_u64(&yaml, "slot").expect("State must specify slot")), num_validators: as_usize(&yaml, "num_validators"), + num_previous_epoch_attestations: as_usize(&yaml, "num_previous_epoch_attestations"), + num_current_epoch_attestations: as_usize(&yaml, "num_current_epoch_attestations"), slashed_validators: as_vec_u64(&yaml, "slashed_validators"), exited_validators: as_vec_u64(&yaml, "exited_validators"), exit_initiated_validators: as_vec_u64(&yaml, "exit_initiated_validators"), @@ -58,6 +64,7 @@ impl StateCheck { "State slot is invalid." ); + // Check the validator count if let Some(num_validators) = self.num_validators { assert_eq!( state.validator_registry.len(), @@ -67,6 +74,26 @@ impl StateCheck { info!("OK: num_validators = {}.", num_validators); } + // Check the previous epoch attestations + if let Some(n) = self.num_previous_epoch_attestations { + assert_eq!( + state.previous_epoch_attestations.len(), + n, + "previous epoch attestations count != expected." + ); + info!("OK: num_previous_epoch_attestations = {}.", n); + } + + // Check the current epoch attestations + if let Some(n) = self.num_current_epoch_attestations { + assert_eq!( + state.current_epoch_attestations.len(), + n, + "current epoch attestations count != expected." + ); + info!("OK: num_current_epoch_attestations = {}.", n); + } + // Check for slashed validators. if let Some(ref slashed_validators) = self.slashed_validators { let actually_slashed_validators: Vec = state From d3a6d73153eb6de71ed6eb5d2a8ad08324bf5927 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 14:27:37 +1100 Subject: [PATCH 139/191] Implements Signer generic for validator client and epoch duties --- eth2/utils/bls/src/keypair.rs | 7 +++++ .../src/duties/beacon_node_duties.rs | 4 +-- validator_client/src/duties/epoch_duties.rs | 6 ++-- validator_client/src/duties/grpc.rs | 12 ++++---- validator_client/src/duties/mod.rs | 26 +++++++++-------- validator_client/src/main.rs | 5 ++-- validator_client/src/service.rs | 17 +++++------ validator_client/src/signer.rs | 29 +++++++++++++++++-- 8 files changed, 70 insertions(+), 36 deletions(-) diff --git a/eth2/utils/bls/src/keypair.rs b/eth2/utils/bls/src/keypair.rs index c91b13bad3..2f0e794a61 100644 --- a/eth2/utils/bls/src/keypair.rs +++ b/eth2/utils/bls/src/keypair.rs @@ -1,5 +1,6 @@ use super::{PublicKey, SecretKey}; use serde_derive::{Deserialize, Serialize}; +use std::fmt; use std::hash::{Hash, Hasher}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -32,3 +33,9 @@ impl Hash for Keypair { self.pk.as_uncompressed_bytes().hash(state) } } + +impl fmt::Display for Keypair { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.pk) + } +} diff --git a/validator_client/src/duties/beacon_node_duties.rs b/validator_client/src/duties/beacon_node_duties.rs index b66b5f704f..af1fab60bf 100644 --- a/validator_client/src/duties/beacon_node_duties.rs +++ b/validator_client/src/duties/beacon_node_duties.rs @@ -1,5 +1,5 @@ use super::EpochDuties; -use types::{Epoch, Keypair}; +use types::{Epoch, PublicKey}; #[derive(Debug, PartialEq, Clone)] pub enum BeaconNodeDutiesError { @@ -15,6 +15,6 @@ pub trait BeaconNodeDuties: Send + Sync { fn request_duties( &self, epoch: Epoch, - signers: &[Keypair], + pub_keys: &[PublicKey], ) -> Result; } diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 8e710ba9aa..0a4f73f723 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::fmt; use std::ops::{Deref, DerefMut}; -use types::{AttestationDuty, Epoch, Keypair, Slot}; +use types::{AttestationDuty, Epoch, PublicKey, 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. @@ -72,7 +72,7 @@ impl fmt::Display for EpochDuty { } /// Maps a list of keypairs (many validators) to an EpochDuty. -pub type EpochDuties = HashMap>; +pub type EpochDuties = HashMap>; pub enum EpochDutiesMapError { UnknownEpoch, @@ -113,7 +113,7 @@ impl EpochDutiesMap { pub fn is_work_slot( &self, slot: Slot, - signer: &Keypair, + signer: &PublicKey, ) -> Result, EpochDutiesMapError> { let epoch = slot.epoch(self.slots_per_epoch); diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index d6e2e5238e..66bf368a21 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -6,21 +6,21 @@ use protos::services_grpc::ValidatorServiceClient; use ssz::ssz_encode; use std::collections::HashMap; use std::time::Duration; -use types::{Epoch, Keypair, Slot}; +use types::{Epoch, PublicKey, Slot}; impl BeaconNodeDuties for ValidatorServiceClient { /// Requests all duties (block signing and committee attesting) from the Beacon Node (BN). fn request_duties( &self, epoch: Epoch, - signers: &[Keypair], + pub_keys: &[PublicKey], ) -> Result { // Get the required duties from all validators // build the request let mut req = GetDutiesRequest::new(); req.set_epoch(epoch.as_u64()); let mut validators = Validators::new(); - validators.set_public_keys(signers.iter().map(|v| ssz_encode(&v.pk)).collect()); + validators.set_public_keys(pub_keys.iter().map(|v| ssz_encode(v)).collect()); req.set_validators(validators); // set a timeout for requests @@ -31,11 +31,11 @@ impl BeaconNodeDuties for ValidatorServiceClient { .get_validator_duties(&req) .map_err(|err| BeaconNodeDutiesError::RemoteFailure(format!("{:?}", err)))?; - let mut epoch_duties: HashMap> = HashMap::new(); + 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(signers[index].clone(), None); + epoch_duties.insert(pub_keys[index].clone(), None); continue; } // active validator @@ -53,7 +53,7 @@ impl BeaconNodeDuties for ValidatorServiceClient { attestation_shard: active_duty.get_attestation_shard(), committee_index: active_duty.get_committee_index(), }; - epoch_duties.insert(signers[index].clone(), Some(epoch_duty)); + epoch_duties.insert(pub_keys[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 1019f489dc..596d314ed4 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -8,11 +8,13 @@ mod grpc; pub use self::beacon_node_duties::{BeaconNodeDuties, BeaconNodeDutiesError}; use self::epoch_duties::{EpochDuties, EpochDutiesMapError}; pub use self::epoch_duties::{EpochDutiesMap, WorkInfo}; +use super::signer::Signer; use futures::Async; use slog::{debug, error, info}; +use std::fmt::Display; use std::sync::Arc; use std::sync::RwLock; -use types::{Epoch, Keypair, Slot}; +use types::{Epoch, PublicKey, Slot}; #[derive(Debug, PartialEq, Clone)] pub enum UpdateOutcome { @@ -38,20 +40,20 @@ pub enum Error { /// Node. /// /// This keeps track of all validator keys and required voting slots. -pub struct DutiesManager { +pub struct DutiesManager { pub duties_map: RwLock, /// A list of all signer objects known to the validator service. - // TODO: Generalise the signers, so that they're not just keypairs - pub signers: Arc>, + pub signers: Arc>, pub beacon_node: Arc, } -impl DutiesManager { +impl DutiesManager { /// Check the Beacon Node for `EpochDuties`. /// /// 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.signers)?; + let public_keys: Vec = self.signers.iter().map(|s| s.to_public()).collect(); + let duties = self.beacon_node.request_duties(epoch, &public_keys)?; { // 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) { @@ -90,17 +92,17 @@ impl DutiesManager { Ok(Async::Ready(())) } - /// Returns a list of (Public, WorkInfo) indicating all the validators that have work to perform + /// Returns a list of (index, 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<(Keypair, WorkInfo)> = Vec::new(); + pub fn get_current_work(&self, slot: Slot) -> Option> { + let mut current_work: Vec<(usize, WorkInfo)> = Vec::new(); // if the map is poisoned, return None let duties = self.duties_map.read().ok()?; - for validator_signer in self.signers.iter() { - match duties.is_work_slot(slot, &validator_signer) { - Ok(Some(work_type)) => current_work.push((validator_signer.clone(), work_type)), + for (index, validator_signer) in self.signers.iter().enumerate() { + match duties.is_work_slot(slot, &validator_signer.to_public()) { + Ok(Some(work_type)) => current_work.push((index, work_type)), Ok(None) => {} // No work for this validator //TODO: This should really log an error, as we shouldn't end up with an err here. Err(_) => {} // Unknown epoch or validator, no work diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index b3a488fe08..4817667348 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -11,6 +11,7 @@ use clap::{App, Arg}; use protos::services_grpc::ValidatorServiceClient; use service::Service as ValidatorService; use slog::{error, info, o, Drain}; +use types::Keypair; fn main() { // Logging @@ -54,8 +55,8 @@ fn main() { .expect("Unable to build a configuration for the validator client."); // start the validator service. - // this specifies the GRPC type to use as the duty manager beacon node. - match ValidatorService::::start(config, log.clone()) { + // this specifies the GRPC and signer type to use as the duty manager beacon node. + match ValidatorService::::start(config, log.clone()) { Ok(_) => info!(log, "Validator client shutdown successfully."), 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 d92d944cb7..d6c0e6638b 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -14,6 +14,7 @@ use crate::config::Config as ValidatorConfig; use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap, UpdateOutcome}; use crate::error as error_chain; use crate::error::ErrorKind; +use crate::signer::Signer; use attester::test_utils::EpochMap; use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use bls::Keypair; @@ -36,14 +37,10 @@ use tokio_timer::clock::Clock; use types::test_utils::generate_deterministic_keypairs; use types::{ChainSpec, Epoch, Fork, Slot}; -//TODO: This service should be simplified in the future. Can be made more steamlined. - -const POLL_INTERVAL_MILLIS: u64 = 100; - /// The validator service. This is the main thread that executes and maintains validator /// duties. //TODO: Generalize the BeaconNode types to use testing -pub struct Service { +pub struct Service { /// The node's current fork version we are processing on. fork: Fork, /// The slot clock for this service. @@ -53,7 +50,7 @@ pub struct Service { /// The chain specification for this clients instance. spec: Arc, /// The duties manager which maintains the state of when to perform actions. - duties_manager: Arc>, + duties_manager: Arc>, // GRPC Clients /// The beacon block GRPC client. beacon_block_client: Arc, @@ -63,7 +60,7 @@ pub struct Service { log: slog::Logger, } -impl Service { +impl Service { /// 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 @@ -71,7 +68,7 @@ impl Service { fn initialize_service( config: ValidatorConfig, log: slog::Logger, - ) -> error_chain::Result> { + ) -> error_chain::Result> { // initialise the beacon node client to check for a connection let env = Arc::new(EnvBuilder::new().build()); @@ -183,6 +180,7 @@ impl Service { // and can check when a validator needs to perform a task. let duties_manager = Arc::new(DutiesManager { duties_map, + // these are abstract objects capable of signing signers: keypairs, beacon_node: validator_client, }); @@ -205,7 +203,8 @@ impl Service { // TODO: Improve handling of generic BeaconNode types, to stub grpcClient pub fn start(config: ValidatorConfig, log: slog::Logger) -> error_chain::Result<()> { // connect to the node and retrieve its properties and initialize the gRPC clients - let mut service = Service::::initialize_service(config, log)?; + let mut service = + Service::::initialize_service(config, log)?; // we have connected to a node and established its parameters. Spin up the core service diff --git a/validator_client/src/signer.rs b/validator_client/src/signer.rs index 85bf35b167..49dedbb33f 100644 --- a/validator_client/src/signer.rs +++ b/validator_client/src/signer.rs @@ -1,7 +1,32 @@ -use types::Signature; +use std::fmt::Display; +use types::{Keypair, PublicKey, Signature}; /// Signs message using an internally-maintained private key. -pub trait Signer { +pub trait Signer: Display + Send + Sync { fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option; fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option; + /// Returns a public key for the signer object. + fn to_public(&self) -> PublicKey; +} + +/* Implements Display and Signer for Keypair */ + +impl Signer for Keypair { + fn to_public(&self) -> PublicKey { + self.pk.clone() + } + + fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option { + Some(Signature::new(message, domain, &self.sk)) + } + + fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option { + Some(Signature::new(message, domain, &self.sk)) + } + + /* + fn sign_attestation_message(&self, message: &[u8], domain: u64) -> Option { + Some(Signature::new(message, domain, &self.sk)) + } + */ } From deb0abd4a811a3c33491a9476755f5466953f010 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 14:28:42 +1100 Subject: [PATCH 140/191] Restores display for validator keys --- validator_client/src/duties/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 596d314ed4..9f0998567f 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -140,9 +140,9 @@ impl From for Error { fn print_duties(log: &slog::Logger, duties: EpochDuties) { for (pk, duty) in duties.iter() { if let Some(display_duty) = duty { - info!(log, "Validator: {:?}",pk; "Duty" => format!("{}",display_duty)); + info!(log, "Validator: {}",pk; "Duty" => format!("{}",display_duty)); } else { - info!(log, "Validator: {:?}",pk; "Duty" => "None"); + info!(log, "Validator: {}",pk; "Duty" => "None"); } } } From ba9090173023e48d36844ea224bcfafabec57ce5 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 14:48:43 +1100 Subject: [PATCH 141/191] Referenced signer passed to block producer --- validator_client/src/block_producer/mod.rs | 11 +++-------- validator_client/src/service.rs | 18 ++++++++++++------ validator_client/src/signer.rs | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index 8297469baf..77b7196667 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -10,11 +10,6 @@ use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; #[derive(Debug, PartialEq)] pub enum Error { - SlotClockError, - SlotUnknowable, - EpochMapPoisoned, - SlotClockPoisoned, - EpochLengthIsZero, BeaconBlockNodeError(BeaconBlockNodeError), } @@ -34,7 +29,7 @@ pub enum ValidatorEvent { /// This struct contains the logic for requesting and signing beacon blocks for a validator. The /// validator can abstractly sign via the Signer trait object. -pub struct BlockProducer { +pub struct BlockProducer<'a, B: BeaconBlockNode, S: Signer> { /// The current fork. pub fork: Fork, /// The current slot to produce a block for. @@ -44,10 +39,10 @@ pub struct BlockProducer { /// The beacon node to connect to. pub beacon_node: Arc, /// The signer to sign the block. - pub signer: Arc, + pub signer: &'a S, } -impl BlockProducer { +impl<'a, B: BeaconBlockNode, S: Signer> BlockProducer<'a, B, S> { /// Produce a block at some slot. /// /// Assumes that a block is required at this slot (does not check the duties). diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index d6c0e6638b..4c01d89671 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -299,17 +299,23 @@ impl Service { /// If there are any duties to process, spawn a separate thread and perform required actions. fn process_duties(&mut self) { if let Some(work) = self.duties_manager.get_current_work(self.current_slot) { - for (_public_key, work_type) in work { + for (signer_index, work_type) in work { if work_type.produce_block { // spawns a thread to produce a beacon block + let signers = self.duties_manager.signers.clone(); + let fork = self.fork.clone(); + let slot = self.current_slot.clone(); + let spec = self.spec.clone(); + let beacon_node = self.beacon_block_client.clone(); std::thread::spawn(move || { - /* + let signer = &signers[signer_index]; let block_producer = BlockProducer { - fork: self.fork, - slot: self.current_slot, - spec: self.spec.clone(), + fork, + slot, + spec, + beacon_node, + signer, }; - */ }); // TODO: Produce a beacon block in a new thread diff --git a/validator_client/src/signer.rs b/validator_client/src/signer.rs index 49dedbb33f..4bbada08ee 100644 --- a/validator_client/src/signer.rs +++ b/validator_client/src/signer.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use types::{Keypair, PublicKey, Signature}; /// Signs message using an internally-maintained private key. -pub trait Signer: Display + Send + Sync { +pub trait Signer: Display + Send + Sync + Clone { fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option; fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option; /// Returns a public key for the signer object. From 6e254551af043f39dadbb7070220f81dc3d0f7f5 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 15:58:31 +1100 Subject: [PATCH 142/191] Implement produce beacon block on gRPC beacon node server --- beacon_node/beacon_chain/src/lib.rs | 2 +- beacon_node/rpc/src/beacon_block.rs | 58 +++++++++++++++---- beacon_node/rpc/src/beacon_chain.rs | 15 ++++- .../testing_beacon_state_builder.rs | 2 +- validator_client/src/block_producer/mod.rs | 24 +++++++- validator_client/src/service.rs | 7 ++- 6 files changed, 90 insertions(+), 18 deletions(-) diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 48a42b941e..234a960945 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -7,7 +7,7 @@ pub mod test_utils; pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; -pub use self::errors::BeaconChainError; +pub use self::errors::{BeaconChainError, BlockProductionError}; pub use attestation_aggregator::Outcome as AggregationOutcome; pub use db; pub use fork_choice; diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index f6b426c18f..e8b3cb01b4 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -3,7 +3,7 @@ use crossbeam_channel; use eth2_libp2p::rpc::methods::BlockRootSlot; use eth2_libp2p::PubsubMessage; use futures::Future; -use grpcio::{RpcContext, UnarySink}; +use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use network::NetworkMessage; use protos::services::{ BeaconBlock as BeaconBlockProto, ProduceBeaconBlockRequest, ProduceBeaconBlockResponse, @@ -11,10 +11,10 @@ use protos::services::{ }; use protos::services_grpc::BeaconBlockService; use slog::Logger; -use slog::{debug, error, info, warn}; -use ssz::{Decodable, TreeHash}; +use slog::{error, info, trace, warn}; +use ssz::{ssz_encode, Decodable, TreeHash}; use std::sync::Arc; -use types::{BeaconBlock, Hash256, Slot}; +use types::{BeaconBlock, Hash256, Signature, Slot}; #[derive(Clone)] pub struct BeaconBlockServiceInstance { @@ -31,11 +31,44 @@ impl BeaconBlockService for BeaconBlockServiceInstance { req: ProduceBeaconBlockRequest, sink: UnarySink, ) { - println!("producing at slot {}", req.get_slot()); + trace!(self.log, "Generating a beacon block"; "req" => format!("{:?}", req)); + + // decode the request + // TODO: requested slot currently unused, see: https://github.com/sigp/lighthouse/issues/336 + let _requested_slot = Slot::from(req.get_slot()); + let (randao_reveal, _index) = match Signature::ssz_decode(req.get_randao_reveal(), 0) { + Ok(v) => v, + Err(_) => { + // decode error, incorrect signature + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some(format!("Invalid randao reveal signature")), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; + + let produced_block = match self.chain.produce_block(randao_reveal) { + Ok((block, _state)) => block, + Err(e) => { + // could not produce a block + let log_clone = self.log.clone(); + warn!(self.log, "RPC Error"; "Error" => format!("Could not produce a block:{:?}",e)); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::Unknown, + Some(format!("Could not produce a block: {:?}", e)), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; - // TODO: build a legit block. let mut block = BeaconBlockProto::new(); - block.set_ssz(b"cats".to_vec()); + block.set_ssz(ssz_encode(&produced_block)); let mut resp = ProduceBeaconBlockResponse::new(); resp.set_block(block); @@ -81,11 +114,16 @@ impl BeaconBlockService for BeaconBlockServiceInstance { slot: block.slot, }); - println!("Sending beacon block to gossipsub"); - self.network_chan.send(NetworkMessage::Publish { + match self.network_chan.send(NetworkMessage::Publish { topics: vec![topic], message, - }); + }) { + Ok(_) => {} + Err(_) => warn!( + self.log, + "Could not send published block to the network service" + ), + } resp.set_success(true); } else if outcome.is_invalid() { diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs index 0551a80246..f21b8df7b4 100644 --- a/beacon_node/rpc/src/beacon_chain.rs +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -4,7 +4,8 @@ use beacon_chain::{ fork_choice::ForkChoice, parking_lot::RwLockReadGuard, slot_clock::SlotClock, - types::{BeaconState, ChainSpec}, + types::{BeaconState, ChainSpec, Signature}, + BlockProductionError, }; pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; use types::BeaconBlock; @@ -17,6 +18,11 @@ pub trait BeaconChain: Send + Sync { fn process_block(&self, block: BeaconBlock) -> Result; + + fn produce_block( + &self, + randao_reveal: Signature, + ) -> Result<(BeaconBlock, BeaconState), BlockProductionError>; } impl BeaconChain for RawBeaconChain @@ -39,4 +45,11 @@ where ) -> Result { self.process_block(block) } + + fn produce_block( + &self, + randao_reveal: Signature, + ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { + self.produce_block(randao_reveal) + } } 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 5e4cebd576..7c231b20b0 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 = 1553776331; // arbitrary + let genesis_time = 1553918534; // arbitrary let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index 77b7196667..7def97e03c 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -4,6 +4,7 @@ mod grpc; use self::beacon_block_node::{BeaconBlockNode, BeaconBlockNodeError}; pub use self::grpc::BeaconBlockGrpcClient; use crate::signer::Signer; +use slog::{error, info}; use ssz::{SignedRoot, TreeHash}; use std::sync::Arc; use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; @@ -23,8 +24,6 @@ pub enum ValidatorEvent { BeaconNodeUnableToProduceBlock(Slot), /// The signer failed to sign the message. SignerRejection(Slot), - /// The public key for this validator is not an active validator. - ValidatorIsUnknown(Slot), } /// This struct contains the logic for requesting and signing beacon blocks for a validator. The @@ -43,6 +42,25 @@ pub struct BlockProducer<'a, B: BeaconBlockNode, S: Signer> { } impl<'a, B: BeaconBlockNode, S: Signer> BlockProducer<'a, B, S> { + /// Handle outputs and results from block production. + pub fn handle_produce_block(&mut self, log: slog::Logger) { + match self.produce_block() { + Ok(ValidatorEvent::BlockProduced(_slot)) => { + info!(log, "Block produced"; "Validator" => format!("{}", self.signer)) + } + Err(e) => error!(log, "Block production error"; "Error" => format!("{:?}", e)), + Ok(ValidatorEvent::SignerRejection(_slot)) => { + error!(log, "Block production error"; "Error" => format!("Signer Could not sign the block")) + } + Ok(ValidatorEvent::SlashableBlockNotProduced(_slot)) => { + error!(log, "Block production error"; "Error" => format!("Rejected the block as it could have been slashed")) + } + Ok(ValidatorEvent::BeaconNodeUnableToProduceBlock(_slot)) => { + error!(log, "Block production error"; "Error" => format!("Beacon node was unable to produce a block")) + } + } + } + /// Produce a block at some slot. /// /// Assumes that a block is required at this slot (does not check the duties). @@ -53,7 +71,7 @@ impl<'a, B: BeaconBlockNode, S: Signer> BlockProducer<'a, B, S> { /// /// The slash-protection code is not yet implemented. There is zero protection against /// slashing. - fn produce_block(&mut self) -> Result { + pub fn produce_block(&mut self) -> Result { let epoch = self.slot.epoch(self.spec.slots_per_epoch); let randao_reveal = { diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 4c01d89671..621fb03a31 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -302,20 +302,23 @@ impl Service { for (signer_index, work_type) in work { if work_type.produce_block { // spawns a thread to produce a beacon block - let signers = self.duties_manager.signers.clone(); + let signers = self.duties_manager.signers.clone(); // this is an arc let fork = self.fork.clone(); let slot = self.current_slot.clone(); let spec = self.spec.clone(); let beacon_node = self.beacon_block_client.clone(); + let log = self.log.clone(); std::thread::spawn(move || { + info!(log, "Producing a block"; "Validator"=> format!("{}", signers[signer_index])); let signer = &signers[signer_index]; - let block_producer = BlockProducer { + let mut block_producer = BlockProducer { fork, slot, spec, beacon_node, signer, }; + block_producer.handle_produce_block(log); }); // TODO: Produce a beacon block in a new thread From 397e104f9b4b65649ef60922059d3a4b0f375b3e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 16:02:09 +1100 Subject: [PATCH 143/191] Implement `Attestation` building in test harness --- beacon_node/beacon_chain/src/beacon_chain.rs | 33 +---- .../test_harness/src/beacon_chain_harness.rs | 114 ++++++++++-------- .../validator_harness/direct_beacon_node.rs | 2 +- eth2/types/src/attestation_duty.rs | 2 +- .../generate_deterministic_keypairs.rs | 2 +- 5 files changed, 69 insertions(+), 84 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 5e83fdd81b..614cc46d81 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1,4 +1,3 @@ -use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome}; use crate::checkpoint::CheckPoint; use crate::errors::{BeaconChainError as Error, BlockProductionError}; use db::{ @@ -88,7 +87,6 @@ pub struct BeaconChain { pub block_store: Arc>, pub state_store: Arc>, pub slot_clock: U, - pub attestation_aggregator: RwLock, pub op_pool: OperationPool, canonical_head: RwLock, finalized_head: RwLock, @@ -131,7 +129,6 @@ where genesis_state.clone(), state_root, )); - let attestation_aggregator = RwLock::new(AttestationAggregator::new()); genesis_state.build_epoch_cache(RelativeEpoch::Previous, &spec)?; genesis_state.build_epoch_cache(RelativeEpoch::Current, &spec)?; @@ -142,7 +139,6 @@ where block_store, state_store, slot_clock, - attestation_aggregator, op_pool: OperationPool::new(), state: RwLock::new(genesis_state), finalized_head, @@ -477,7 +473,7 @@ where } /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. - pub fn produce_attestation(&self, shard: u64) -> Result { + pub fn produce_attestation_data(&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( @@ -509,33 +505,6 @@ where }) } - /// Validate a `FreeAttestation` and either: - /// - /// - Create a new `Attestation`. - /// - Aggregate it to an existing `Attestation`. - pub fn process_free_attestation( - &self, - free_attestation: FreeAttestation, - ) -> Result { - let aggregation_outcome = self - .attestation_aggregator - .write() - .process_free_attestation(&self.state.read(), &free_attestation, &self.spec)?; - - // return if the attestation is invalid - if !aggregation_outcome.valid { - return Ok(aggregation_outcome); - } - - // valid attestation, proceed with fork-choice logic - self.fork_choice.write().add_attestation( - free_attestation.validator_index, - &free_attestation.data.beacon_block_root, - &self.spec, - )?; - Ok(aggregation_outcome) - } - /// Accept a new attestation from the network. /// /// If valid, the attestation is added to the `op_pool` and aggregated with another attestation 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 7d6a690e09..b7acac9e11 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 @@ -10,8 +10,6 @@ use log::debug; use rayon::prelude::*; use slot_clock::TestingSlotClock; use ssz::TreeHash; -use std::collections::HashSet; -use std::iter::FromIterator; use std::sync::Arc; use types::{test_utils::TestingBeaconStateBuilder, *}; @@ -137,51 +135,64 @@ impl BeaconChainHarness { slot } - /// Gather the `FreeAttestation`s from the valiators. - /// - /// Note: validators will only produce attestations _once per slot_. So, if you call this twice - /// you'll only get attestations on the first run. - pub fn gather_free_attesations(&mut self) -> Vec { + pub fn gather_attesations(&mut self) -> Vec { let present_slot = self.beacon_chain.present_slot(); + let state = self.beacon_chain.state.read(); - let attesting_validators = self - .beacon_chain - .state - .read() + let mut attestations = vec![]; + + for committee in state .get_crosslink_committees_at_slot(present_slot, &self.spec) .unwrap() - .iter() - .fold(vec![], |mut acc, c| { - acc.append(&mut c.committee.clone()); - acc - }); - let attesting_validators: HashSet = - HashSet::from_iter(attesting_validators.iter().cloned()); + { + for &validator in &committee.committee { + let duties = state + .get_attestation_duties(validator, &self.spec) + .unwrap() + .expect("Attesting validators by definition have duties"); - let free_attestations: Vec = self - .validators - .par_iter_mut() - .enumerate() - .filter_map(|(i, validator)| { - if attesting_validators.contains(&i) { - // Advance the validator slot. - validator.set_slot(present_slot); + // Obtain `AttestationData` from the beacon chain. + let data = self + .beacon_chain + .produce_attestation_data(duties.shard) + .unwrap(); - // Prompt the validator to produce an attestation (if required). - validator.produce_free_attestation().ok() - } else { - None - } - }) - .collect(); + // Produce an aggregate signature with a single signature. + let aggregate_signature = { + let message = AttestationDataAndCustodyBit { + data: data.clone(), + custody_bit: false, + } + .hash_tree_root(); + let domain = self.spec.get_domain( + state.slot.epoch(self.spec.slots_per_epoch), + Domain::Attestation, + &state.fork, + ); + let sig = + Signature::new(&message, domain, &self.validators[validator].keypair.sk); - debug!( - "Gathered {} FreeAttestations for slot {}.", - free_attestations.len(), - present_slot - ); + let mut agg_sig = AggregateSignature::new(); + agg_sig.add(&sig); - free_attestations + agg_sig + }; + + let mut aggregation_bitfield = Bitfield::with_capacity(committee.committee.len()); + let custody_bitfield = Bitfield::with_capacity(committee.committee.len()); + + aggregation_bitfield.set(duties.committee_index, true); + + attestations.push(Attestation { + aggregation_bitfield, + data, + custody_bitfield, + aggregate_signature, + }) + } + } + + attestations } /// Get the block from the proposer for the slot. @@ -200,7 +211,9 @@ impl BeaconChainHarness { // Ensure the validators slot clock is accurate. self.validators[proposer].set_slot(present_slot); - self.validators[proposer].produce_block().unwrap() + let block = self.validators[proposer].produce_block().unwrap(); + + block } /// Advances the chain with a BeaconBlock and attestations from all validators. @@ -219,20 +232,23 @@ impl BeaconChainHarness { }; debug!("...block processed by BeaconChain."); - debug!("Producing free attestations..."); + debug!("Producing attestations..."); // Produce new attestations. - let free_attestations = self.gather_free_attesations(); + let attestations = self.gather_attesations(); - debug!("Processing free attestations..."); + debug!("Processing {} attestations...", attestations.len()); - free_attestations.par_iter().for_each(|free_attestation| { - self.beacon_chain - .process_free_attestation(free_attestation.clone()) - .unwrap(); - }); + attestations + .par_iter() + .enumerate() + .for_each(|(i, attestation)| { + self.beacon_chain + .process_attestation(attestation.clone()) + .expect(&format!("Attestation {} invalid: {:?}", i, attestation)); + }); - debug!("Free attestations processed."); + debug!("Attestations processed."); block } 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 fde8211ab5..7853459d7e 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 @@ -55,7 +55,7 @@ impl AttesterBeaconNode for DirectBeac _slot: Slot, shard: u64, ) -> Result, NodeError> { - match self.beacon_chain.produce_attestation(shard) { + match self.beacon_chain.produce_attestation_data(shard) { Ok(attestation_data) => Ok(Some(attestation_data)), Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))), } diff --git a/eth2/types/src/attestation_duty.rs b/eth2/types/src/attestation_duty.rs index f6e86d2632..80d912a83f 100644 --- a/eth2/types/src/attestation_duty.rs +++ b/eth2/types/src/attestation_duty.rs @@ -1,7 +1,7 @@ use crate::*; use serde_derive::{Deserialize, Serialize}; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)] pub struct AttestationDuty { pub slot: Slot, pub shard: Shard, diff --git a/eth2/types/src/test_utils/generate_deterministic_keypairs.rs b/eth2/types/src/test_utils/generate_deterministic_keypairs.rs index f2ce8709e8..37880a988e 100644 --- a/eth2/types/src/test_utils/generate_deterministic_keypairs.rs +++ b/eth2/types/src/test_utils/generate_deterministic_keypairs.rs @@ -19,7 +19,7 @@ pub fn generate_deterministic_keypairs(validator_count: usize) -> Vec { .collect::>() .par_iter() .map(|&i| { - let secret = int_to_bytes48(i as u64 + 1); + let secret = int_to_bytes48(i as u64 + 1000); let sk = SecretKey::from_bytes(&secret).unwrap(); let pk = PublicKey::from_secret_key(&sk); Keypair { sk, pk } From 25d1ddfbb0c50155cab86eb42db542eee2fe076a Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 16:34:43 +1100 Subject: [PATCH 144/191] Renames BeaconBlockNode to BeaconNodeBlock for future consistency --- .../attestation_grpc_client.rs | 45 ------------------- validator_client/src/attester_service/mod.rs | 7 ++- .../src/block_producer/beacon_block_node.rs | 33 -------------- validator_client/src/block_producer/grpc.rs | 21 ++++----- validator_client/src/block_producer/mod.rs | 16 +++---- validator_client/src/service.rs | 2 +- 6 files changed, 21 insertions(+), 103 deletions(-) delete mode 100644 validator_client/src/attester_service/attestation_grpc_client.rs delete mode 100644 validator_client/src/block_producer/beacon_block_node.rs diff --git a/validator_client/src/attester_service/attestation_grpc_client.rs b/validator_client/src/attester_service/attestation_grpc_client.rs deleted file mode 100644 index 502e51cac9..0000000000 --- a/validator_client/src/attester_service/attestation_grpc_client.rs +++ /dev/null @@ -1,45 +0,0 @@ -use protos::services_grpc::AttestationServiceClient; -use std::sync::Arc; - -use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; -use protos::services::ProduceAttestationDataRequest; -use types::{Attestation, AttestationData, 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> { - 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 Attestation - Err(BeaconNodeError::DecodeFailure) - } - - fn publish_attestation( - &self, - attestation: Attestation, - ) -> Result { - // TODO: return correct PublishOutcome - Err(BeaconNodeError::DecodeFailure) - } -} -*/ diff --git a/validator_client/src/attester_service/mod.rs b/validator_client/src/attester_service/mod.rs index c146694458..1695ec0fb6 100644 --- a/validator_client/src/attester_service/mod.rs +++ b/validator_client/src/attester_service/mod.rs @@ -1,4 +1,5 @@ -mod attestation_grpc_client; +mod grpc; +/* use attester::{Attester, BeaconNode, DutiesReader, PollOutcome as AttesterPollOutcome, Signer}; use slog::{error, info, warn, Logger}; use slot_clock::SlotClock; @@ -6,10 +7,8 @@ use std::time::Duration; pub use self::attestation_grpc_client::AttestationGrpcClient; -pub struct AttesterService {} -/* pub struct AttesterService { - // pub attester: Attester, + pub attester: Attester, pub poll_interval_millis: u64, pub log: Logger, } diff --git a/validator_client/src/block_producer/beacon_block_node.rs b/validator_client/src/block_producer/beacon_block_node.rs deleted file mode 100644 index 5a581a4a7b..0000000000 --- a/validator_client/src/block_producer/beacon_block_node.rs +++ /dev/null @@ -1,33 +0,0 @@ -use types::{BeaconBlock, Signature, Slot}; -#[derive(Debug, PartialEq, Clone)] -pub enum BeaconBlockNodeError { - RemoteFailure(String), - DecodeFailure, -} - -#[derive(Debug, PartialEq, Clone)] -pub enum PublishOutcome { - ValidBlock, - InvalidBlock(String), -} - -/// Defines the methods required to produce and publish blocks on a Beacon Node. Abstracts the -/// actual beacon node. -pub trait BeaconBlockNode: Send + Sync { - /// Request that the node produces a block. - /// - /// Returns Ok(None) if the Beacon Node is unable to produce at the given slot. - fn produce_beacon_block( - &self, - slot: Slot, - randao_reveal: &Signature, - ) -> Result, BeaconBlockNodeError>; - - /// Request that the node publishes a block. - /// - /// Returns `true` if the publish was successful. - fn publish_beacon_block( - &self, - block: BeaconBlock, - ) -> Result; -} diff --git a/validator_client/src/block_producer/grpc.rs b/validator_client/src/block_producer/grpc.rs index 23477ece14..ab0d5d4210 100644 --- a/validator_client/src/block_producer/grpc.rs +++ b/validator_client/src/block_producer/grpc.rs @@ -1,4 +1,4 @@ -use super::beacon_block_node::*; +use super::beacon_node_block::*; use protos::services::{ BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, }; @@ -19,7 +19,7 @@ impl BeaconBlockGrpcClient { } } -impl BeaconBlockNode for BeaconBlockGrpcClient { +impl BeaconNodeBlock for BeaconBlockGrpcClient { /// Request a Beacon Node (BN) to produce a new block at the supplied slot. /// /// Returns `None` if it is not possible to produce at the supplied slot. For example, if the @@ -28,7 +28,7 @@ impl BeaconBlockNode for BeaconBlockGrpcClient { &self, slot: Slot, randao_reveal: &Signature, - ) -> Result, BeaconBlockNodeError> { + ) -> Result, BeaconNodeError> { // request a beacon block from the node let mut req = ProduceBeaconBlockRequest::new(); req.set_slot(slot.as_u64()); @@ -38,15 +38,15 @@ impl BeaconBlockNode for BeaconBlockGrpcClient { let reply = self .client .produce_beacon_block(&req) - .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; // format the reply if reply.has_block() { let block = reply.get_block(); let ssz = block.get_ssz(); - let (block, _i) = BeaconBlock::ssz_decode(&ssz, 0) - .map_err(|_| BeaconBlockNodeError::DecodeFailure)?; + let (block, _i) = + BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| BeaconNodeError::DecodeFailure)?; Ok(Some(block)) } else { @@ -58,10 +58,7 @@ impl BeaconBlockNode for BeaconBlockGrpcClient { /// /// Generally, this will be called after a `produce_beacon_block` call with a block that has /// been completed (signed) by the validator client. - fn publish_beacon_block( - &self, - block: BeaconBlock, - ) -> Result { + fn publish_beacon_block(&self, block: BeaconBlock) -> Result { let mut req = PublishBeaconBlockRequest::new(); let ssz = ssz_encode(&block); @@ -74,10 +71,10 @@ impl BeaconBlockNode for BeaconBlockGrpcClient { let reply = self .client .publish_beacon_block(&req) - .map_err(|err| BeaconBlockNodeError::RemoteFailure(format!("{:?}", err)))?; + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; if reply.get_success() { - Ok(PublishOutcome::ValidBlock) + Ok(PublishOutcome::Valid) } else { // TODO: distinguish between different errors Ok(PublishOutcome::InvalidBlock("Publish failed".to_string())) diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index 7def97e03c..09ce9bffae 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -1,7 +1,7 @@ -mod beacon_block_node; +mod beacon_node_block; mod grpc; -use self::beacon_block_node::{BeaconBlockNode, BeaconBlockNodeError}; +use self::beacon_node_block::{BeaconNodeBlock, BeaconNodeError}; pub use self::grpc::BeaconBlockGrpcClient; use crate::signer::Signer; use slog::{error, info}; @@ -11,7 +11,7 @@ use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; #[derive(Debug, PartialEq)] pub enum Error { - BeaconBlockNodeError(BeaconBlockNodeError), + BeaconNodeError(BeaconNodeError), } #[derive(Debug, PartialEq)] @@ -28,7 +28,7 @@ pub enum ValidatorEvent { /// This struct contains the logic for requesting and signing beacon blocks for a validator. The /// validator can abstractly sign via the Signer trait object. -pub struct BlockProducer<'a, B: BeaconBlockNode, S: Signer> { +pub struct BlockProducer<'a, B: BeaconNodeBlock, S: Signer> { /// The current fork. pub fork: Fork, /// The current slot to produce a block for. @@ -41,7 +41,7 @@ pub struct BlockProducer<'a, B: BeaconBlockNode, S: Signer> { pub signer: &'a S, } -impl<'a, B: BeaconBlockNode, S: Signer> BlockProducer<'a, B, S> { +impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> { /// Handle outputs and results from block production. pub fn handle_produce_block(&mut self, log: slog::Logger) { match self.produce_block() { @@ -147,9 +147,9 @@ impl<'a, B: BeaconBlockNode, S: Signer> BlockProducer<'a, B, S> { } } -impl From for Error { - fn from(e: BeaconBlockNodeError) -> Error { - Error::BeaconBlockNodeError(e) +impl From for Error { + fn from(e: BeaconNodeError) -> Error { + Error::BeaconNodeError(e) } } diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 621fb03a31..fd24744ace 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -8,7 +8,7 @@ /// When a validator needs to either produce a block or sign an attestation, it requests the /// data from the beacon node and performs the signing before publishing the block to the beacon /// node. -use crate::attester_service::{AttestationGrpcClient, AttesterService}; +//use crate::attester_service::{AttestationGrpcClient, AttesterService}; use crate::block_producer::{BeaconBlockGrpcClient, BlockProducer}; use crate::config::Config as ValidatorConfig; use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap, UpdateOutcome}; From c107ebf9aa353f74559643d32295584eb43662fd Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 17:06:43 +1100 Subject: [PATCH 145/191] Initial implementation of AttestationProducer --- .../beacon_node_attestation.rs | 22 +++ validator_client/src/attester_service/grpc.rs | 45 +++++ validator_client/src/attester_service/mod.rs | 172 +++++++++++++----- .../src/block_producer/beacon_node_block.rs | 31 ++++ validator_client/src/signer.rs | 15 +- 5 files changed, 222 insertions(+), 63 deletions(-) create mode 100644 validator_client/src/attester_service/beacon_node_attestation.rs create mode 100644 validator_client/src/attester_service/grpc.rs create mode 100644 validator_client/src/block_producer/beacon_node_block.rs diff --git a/validator_client/src/attester_service/beacon_node_attestation.rs b/validator_client/src/attester_service/beacon_node_attestation.rs new file mode 100644 index 0000000000..fa8d540319 --- /dev/null +++ b/validator_client/src/attester_service/beacon_node_attestation.rs @@ -0,0 +1,22 @@ +//TODO: generalise these enums to the crate +use super::block_producer::{BeaconNodeError, PublishOutcome}; + +/// Defines the methods required to produce and publish attestations on a Beacon Node. Abstracts the +/// actual beacon node. +pub trait BeaconNodeAttestation: Send + Sync { + /// Request that the node produces the required attestation data. + /// + fn produce_attestation_data( + &self, + slot: Slot, + shard: u64, + ) -> Result; + + /// Request that the node publishes a attestation. + /// + /// Returns `true` if the publish was successful. + fn publish_attestation( + &self, + attestation: Attestation, + ) -> Result; +} diff --git a/validator_client/src/attester_service/grpc.rs b/validator_client/src/attester_service/grpc.rs new file mode 100644 index 0000000000..502e51cac9 --- /dev/null +++ b/validator_client/src/attester_service/grpc.rs @@ -0,0 +1,45 @@ +use protos::services_grpc::AttestationServiceClient; +use std::sync::Arc; + +use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; +use protos::services::ProduceAttestationDataRequest; +use types::{Attestation, AttestationData, 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> { + 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 Attestation + Err(BeaconNodeError::DecodeFailure) + } + + fn publish_attestation( + &self, + attestation: Attestation, + ) -> Result { + // TODO: return correct PublishOutcome + Err(BeaconNodeError::DecodeFailure) + } +} +*/ diff --git a/validator_client/src/attester_service/mod.rs b/validator_client/src/attester_service/mod.rs index 1695ec0fb6..7178e28d73 100644 --- a/validator_client/src/attester_service/mod.rs +++ b/validator_client/src/attester_service/mod.rs @@ -1,59 +1,131 @@ mod grpc; -/* -use attester::{Attester, BeaconNode, DutiesReader, PollOutcome as AttesterPollOutcome, Signer}; -use slog::{error, info, warn, Logger}; -use slot_clock::SlotClock; -use std::time::Duration; +mod beacon_node_attestation; -pub use self::attestation_grpc_client::AttestationGrpcClient; +use std::sync::Arc; +use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; -pub struct AttesterService { - pub attester: Attester, - pub poll_interval_millis: u64, - pub log: Logger, +#[derive(Debug, PartialEq)] +pub enum Error { + BeaconNodeError(BeaconNodeError), } +/// This struct contains the logic for requesting and signing beacon attestations for a validator. The +/// validator can abstractly sign via the Signer trait object. +pub struct AttestationProducer<'a, B: BeaconNodeAttestation, S: Signer> { + /// The current fork. + pub fork: Fork, + /// The current slot to produce an attestation for. + pub slot: Slot, + /// The current epoch. + pub spec: Arc, + /// The beacon node to connect to. + pub beacon_node: Arc, + /// The signer to sign the block. + pub signer: &'a S, +} -impl AttesterService { - /// Run a loop which polls the Attester each `poll_interval_millis` millseconds. +impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { + /// Handle outputs and results from attestation production. + pub fn handle_produce_attestation(&mut self, log: slog::Logger) { + match self.produce_attestation() { + Ok(ValidatorEvent::AttestationProduced(_slot)) => { + info!(log, "Attestation produced"; "Validator" => format!("{}", self.signer)) + } + Err(e) => error!(log, "Attestation production error"; "Error" => format!("{:?}", e)), + Ok(ValidatorEvent::SignerRejection(_slot)) => { + error!(log, "Attestation production error"; "Error" => format!("Signer could not sign the attestation")) + } + Ok(ValidatorEvent::SlashableAttestationNotProduced(_slot)) => { + error!(log, "Attestation production error"; "Error" => format!("Rejected the attestation as it could have been slashed")) + } + Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(_slot)) => { + error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to produce an attestation")) + } + } + } + + /// Produce an attestation, sign it and send it back /// - /// Logs the results of the polls. - pub fn run(&mut self) { - loop { - /* We don't do the polling any more... - match self.attester.poll() { - Err(error) => { - error!(self.log, "Attester poll error"; "error" => format!("{:?}", error)) + /// Assumes that an attestation is required at this slot (does not check the duties). + /// + /// Ensures the message is not slashable. + /// + /// !!! UNSAFE !!! + /// + /// The slash-protection code is not yet implemented. There is zero protection against + /// slashing. + pub fn produce_attestation(&mut self) -> Result { + let epoch = self.slot.epoch(self.spec.slots_per_epoch); + + if let Some(attestation) = self + .beacon_node + .produce_attestation_data(self.slot, self.shard)? + { + if self.safe_to_produce(&attestation) { + let domain = self.spec.get_domain(epoch, Domain::Attestation, &self.fork); + if let Some(attestation) = self.sign_attestation(attestation, domain) { + self.beacon_node.publish_attestation(attestation)?; + Ok(ValidatorEvent::AttestationProduced(self.slot)) + } else { + Ok(ValidatorEvent::SignerRejection(self.slot)) } - 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) - } - }; - */ -println!("Legacy polling still happening..."); -std::thread::sleep(Duration::from_millis(self.poll_interval_millis)); + } else { + Ok(ValidatorEvent::SlashableAttestationNotProduced(self.slot)) + } + } else { + Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(self.slot)) + } + } + + /// Consumes an attestation, returning the attestation signed by the validators private key. + /// + /// Important: this function will not check to ensure the attestation is not slashable. This must be + /// done upstream. + fn sign_attestation(&mut self, mut attestation: Attestation, duties: AttestationDuties, domain: u64) -> Option { + self.store_produce(&attestation); + + // build the aggregate signature + let aggregate_sig = { + let message = AttestationDataAndCustodyBit { + data: attestation.clone(), + custody_bit: false, + }.hash_tree_root(); + + let sig = self.signer.sign_message(&message, domain)?; + + let mut agg_sig = AggregateSignature::new(); + agg_sig.add(&sig); + agg_sig + } + + let mut aggregation_bitfield = Bitfield::with_capacity(duties.comitee_size); + let custody_bitfield = Bitfield::with_capacity(duties.committee_size); + aggregation_bitfield.set(duties.committee_index, true); + + Attestation { + aggregation_bitfield, + data, + custody_bitfield, + aggregate_signature, + } + } + + /// Returns `true` if signing an attestation is safe (non-slashable). + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn safe_to_produce(&self, _block: &Attestation) -> bool { + //TODO: Implement slash protection + true + } + + /// Record that an attestation was produced so that slashable votes may not be made in the future. + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn store_produce(&mut self, _block: &BeaconBlock) { + // TODO: Implement slash protection + } } -} -} -*/ diff --git a/validator_client/src/block_producer/beacon_node_block.rs b/validator_client/src/block_producer/beacon_node_block.rs new file mode 100644 index 0000000000..65ccb21047 --- /dev/null +++ b/validator_client/src/block_producer/beacon_node_block.rs @@ -0,0 +1,31 @@ +use types::{BeaconBlock, Signature, Slot}; +#[derive(Debug, PartialEq, Clone)] +pub enum BeaconNodeError { + RemoteFailure(String), + DecodeFailure, +} + +#[derive(Debug, PartialEq, Clone)] +pub enum PublishOutcome { + Valid, + InvalidBlock(String), + InvalidAttestation(String), +} + +/// Defines the methods required to produce and publish blocks on a Beacon Node. Abstracts the +/// actual beacon node. +pub trait BeaconNodeBlock: Send + Sync { + /// Request that the node produces a block. + /// + /// Returns Ok(None) if the Beacon Node is unable to produce at the given slot. + fn produce_beacon_block( + &self, + slot: Slot, + randao_reveal: &Signature, + ) -> Result, BeaconNodeError>; + + /// Request that the node publishes a block. + /// + /// Returns `true` if the publish was successful. + fn publish_beacon_block(&self, block: BeaconBlock) -> Result; +} diff --git a/validator_client/src/signer.rs b/validator_client/src/signer.rs index 4bbada08ee..018142322f 100644 --- a/validator_client/src/signer.rs +++ b/validator_client/src/signer.rs @@ -3,8 +3,7 @@ use types::{Keypair, PublicKey, Signature}; /// Signs message using an internally-maintained private key. pub trait Signer: Display + Send + Sync + Clone { - fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option; - fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option; + fn sign_message(&self, message: &[u8], domain: u64) -> Option; /// Returns a public key for the signer object. fn to_public(&self) -> PublicKey; } @@ -16,17 +15,7 @@ impl Signer for Keypair { self.pk.clone() } - fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option { + fn sign_message(&self, message: &[u8], domain: u64) -> Option { Some(Signature::new(message, domain, &self.sk)) } - - fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option { - Some(Signature::new(message, domain, &self.sk)) - } - - /* - fn sign_attestation_message(&self, message: &[u8], domain: u64) -> Option { - Some(Signature::new(message, domain, &self.sk)) - } - */ } From 7b3f317abfe3439e20f3148596fec58d42c33016 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 17:12:43 +1100 Subject: [PATCH 146/191] Fix bug with attestation production It was being produced with the wrong source root. I will raise an issue on the spec as it's a tricky one. --- beacon_node/beacon_chain/src/beacon_chain.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 614cc46d81..45a28b7822 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -475,11 +475,7 @@ 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: 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), - &self.spec, - )?; + let state = self.state.read(); let target_root = *self.state.read().get_block_root( self.state @@ -500,8 +496,8 @@ where epoch: self.state.read().slot.epoch(self.spec.slots_per_epoch), crosslink_data_root: Hash256::zero(), }, - source_epoch, - source_root, + source_epoch: state.current_justified_epoch, + source_root: state.current_justified_root, }) } From dbcc88ad67aff419a9af00037809d599dc3dc1e9 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 17:13:23 +1100 Subject: [PATCH 147/191] Ensure BitVec is initialized using a multiple of 8 I found it was panic-ing when supplied a non-power-of-zero len. --- beacon_node/beacon_chain/src/beacon_chain.rs | 5 ++++- eth2/utils/boolean-bitfield/src/lib.rs | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 45a28b7822..7c2336a28b 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -692,7 +692,10 @@ where }, }; - trace!("BeaconChain::produce_block: updating state for new block.",); + debug!( + "Produced block with {} attestations, updating state.", + block.body.attestations.len() + ); per_block_processing_without_verifying_block_signature(&mut state, &block, &self.spec)?; diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index cdd0bc3d77..d90b28dc5b 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -33,9 +33,11 @@ impl BooleanBitfield { } /// Create a new bitfield with the given length `initial_len` and all values set to `bit`. - pub fn from_elem(inital_len: usize, bit: bool) -> Self { + pub fn from_elem(initial_len: usize, bit: bool) -> Self { + // BitVec can panic if we don't set the len to be a multiple of 8. + let len = ((initial_len + 7) / 8) * 8; Self { - 0: BitVec::from_elem(inital_len, bit), + 0: BitVec::from_elem(len, bit), } } From bb8938c564d77a1ca5e8d18958f3ce6d9a75f7ab Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 17:14:38 +1100 Subject: [PATCH 148/191] Use AttestationDuty in epoch duties --- eth2/types/src/attestation_duty.rs | 1 + validator_client/src/duties/epoch_duties.rs | 12 +++--------- validator_client/src/duties/grpc.rs | 12 +++++++++--- validator_client/src/duties/mod.rs | 1 - 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/eth2/types/src/attestation_duty.rs b/eth2/types/src/attestation_duty.rs index 80d912a83f..9612dd868c 100644 --- a/eth2/types/src/attestation_duty.rs +++ b/eth2/types/src/attestation_duty.rs @@ -6,4 +6,5 @@ pub struct AttestationDuty { pub slot: Slot, pub shard: Shard, pub committee_index: usize, + pub comittee_size: usize, } diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 0a4f73f723..d0753a2a6a 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -23,9 +23,7 @@ pub struct WorkInfo { #[derive(Debug, PartialEq, Clone, Copy, Default)] pub struct EpochDuty { pub block_production_slot: Option, - pub attestation_slot: Slot, - pub attestation_shard: u64, - pub committee_index: u64, + pub attestation_duty: AttestationDuty, } impl EpochDuty { @@ -39,12 +37,8 @@ impl EpochDuty { // if the validator is required to attest to a shard, create the data let mut attestation_duty = None; - if self.attestation_slot == slot { - attestation_duty = Some(AttestationDuty { - slot, - shard: self.attestation_shard, - committee_index: self.committee_index as usize, - }); + if self.attestation_duty.slot == slot { + attestation_duty = self.attestation_duty } if produce_block | attestation_duty.is_some() { diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 66bf368a21..f0d892e98f 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -47,11 +47,17 @@ impl BeaconNodeDuties for ValidatorServiceClient { None } }; + + let attestation_duty = AttestationDuty { + slot: Slot::from(active_duty.get_attestation_slot()), + shard: active_duty.get_attestation_shard(), + committee_index: active_duty.get_committee_index(), + comittee_size: 10, + } + let epoch_duty = EpochDuty { block_production_slot, - attestation_slot: Slot::from(active_duty.get_attestation_slot()), - attestation_shard: active_duty.get_attestation_shard(), - committee_index: active_duty.get_committee_index(), + attestation_duty, }; epoch_duties.insert(pub_keys[index].clone(), Some(epoch_duty)); } diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index 9f0998567f..d52cc32544 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -30,7 +30,6 @@ pub enum UpdateOutcome { #[derive(Debug, PartialEq)] pub enum Error { DutiesMapPoisoned, - EpochMapPoisoned, BeaconNodeDutiesError(BeaconNodeDutiesError), UnknownEpoch, UnknownValidator, From ed6d0b46d03b088f5074b3b17c723723c19d9437 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 17:16:04 +1100 Subject: [PATCH 149/191] Add committee len to AttesterDuties --- eth2/types/src/attestation_duty.rs | 1 + eth2/types/src/beacon_state/epoch_cache.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/eth2/types/src/attestation_duty.rs b/eth2/types/src/attestation_duty.rs index 80d912a83f..299fdd44cf 100644 --- a/eth2/types/src/attestation_duty.rs +++ b/eth2/types/src/attestation_duty.rs @@ -6,4 +6,5 @@ pub struct AttestationDuty { pub slot: Slot, pub shard: Shard, pub committee_index: usize, + pub committee_len: usize, } diff --git a/eth2/types/src/beacon_state/epoch_cache.rs b/eth2/types/src/beacon_state/epoch_cache.rs index 32d9a643e9..62df902714 100644 --- a/eth2/types/src/beacon_state/epoch_cache.rs +++ b/eth2/types/src/beacon_state/epoch_cache.rs @@ -92,6 +92,7 @@ impl EpochCache { slot, shard, committee_index: k, + committee_len: crosslink_committee.committee.len(), }; attestation_duties[*validator_index] = Some(attestation_duty) } From 64507950dd1193d3c5032c33afe496b83975e227 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 17:31:58 +1100 Subject: [PATCH 150/191] Use committe_len in test_harness --- .../beacon_chain/test_harness/src/beacon_chain_harness.rs | 4 ++-- 1 file changed, 2 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 b7acac9e11..33c12d7c70 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 @@ -178,8 +178,8 @@ impl BeaconChainHarness { agg_sig }; - let mut aggregation_bitfield = Bitfield::with_capacity(committee.committee.len()); - let custody_bitfield = Bitfield::with_capacity(committee.committee.len()); + let mut aggregation_bitfield = Bitfield::with_capacity(duties.committee_len); + let custody_bitfield = Bitfield::with_capacity(duties.committee_len); aggregation_bitfield.set(duties.committee_index, true); From 145cabc427da8fe460375aa11e37e988d868802e Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 17:56:43 +1100 Subject: [PATCH 151/191] Build validator client AttestationProducer --- .../beacon_node_attestation.rs | 3 +- validator_client/src/attester_service/mod.rs | 95 ++++++++++++------- validator_client/src/block_producer/mod.rs | 21 ++-- validator_client/src/duties/epoch_duties.rs | 4 +- validator_client/src/duties/grpc.rs | 12 +-- 5 files changed, 83 insertions(+), 52 deletions(-) diff --git a/validator_client/src/attester_service/beacon_node_attestation.rs b/validator_client/src/attester_service/beacon_node_attestation.rs index fa8d540319..b5ff777de8 100644 --- a/validator_client/src/attester_service/beacon_node_attestation.rs +++ b/validator_client/src/attester_service/beacon_node_attestation.rs @@ -1,5 +1,6 @@ //TODO: generalise these enums to the crate -use super::block_producer::{BeaconNodeError, PublishOutcome}; +use crate::block_producer::{BeaconNodeError, PublishOutcome}; +use types::{Attestation, AttestationData, Slot}; /// Defines the methods required to produce and publish attestations on a Beacon Node. Abstracts the /// actual beacon node. diff --git a/validator_client/src/attester_service/mod.rs b/validator_client/src/attester_service/mod.rs index 20488b1c19..7b2174b0cd 100644 --- a/validator_client/src/attester_service/mod.rs +++ b/validator_client/src/attester_service/mod.rs @@ -1,22 +1,38 @@ -mod grpc; mod beacon_node_attestation; +mod grpc; use std::sync::Arc; use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; -use super::block_proposer::beacon_node_block::BeaconNodeError; +//TODO: Move these higher up in the crate +use super::block_producer::{BeaconNodeError, ValidatorEvent}; +use crate::signer::Signer; +use beacon_node_attestation::BeaconNodeAttestation; +use slog::{error, info, warn}; +use ssz::TreeHash; +use types::{ + AggregateSignature, Attestation, AttestationData, AttestationDataAndCustodyBit, + AttestationDuty, Bitfield, +}; +//TODO: Group these errors at a crate level #[derive(Debug, PartialEq)] pub enum Error { BeaconNodeError(BeaconNodeError), } +impl From for Error { + fn from(e: BeaconNodeError) -> Error { + Error::BeaconNodeError(e) + } +} + /// This struct contains the logic for requesting and signing beacon attestations for a validator. The /// validator can abstractly sign via the Signer trait object. pub struct AttestationProducer<'a, B: BeaconNodeAttestation, S: Signer> { /// The current fork. pub fork: Fork, - /// The current slot to produce an attestation for. - pub slot: Slot, + /// The attestation duty to perform. + pub duty: AttestationDuty, /// The current epoch. pub spec: Arc, /// The beacon node to connect to. @@ -42,6 +58,9 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(_slot)) => { error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to produce an attestation")) } + Ok(v) => { + warn!(log, "Unknown result for attestation production"; "Error" => format!("{:?}",v)) + } } } @@ -56,25 +75,23 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { /// The slash-protection code is not yet implemented. There is zero protection against /// slashing. pub fn produce_attestation(&mut self) -> Result { - let epoch = self.slot.epoch(self.spec.slots_per_epoch); + let epoch = self.duty.slot.epoch(self.spec.slots_per_epoch); - if let Some(attestation) = self + let attestation = self .beacon_node - .produce_attestation_data(self.slot, self.shard)? - { - if self.safe_to_produce(&attestation) { - let domain = self.spec.get_domain(epoch, Domain::Attestation, &self.fork); - if let Some(attestation) = self.sign_attestation(attestation, domain) { - self.beacon_node.publish_attestation(attestation)?; - Ok(ValidatorEvent::AttestationProduced(self.slot)) - } else { - Ok(ValidatorEvent::SignerRejection(self.slot)) - } + .produce_attestation_data(self.duty.slot, self.duty.shard)?; + if self.safe_to_produce(&attestation) { + let domain = self.spec.get_domain(epoch, Domain::Attestation, &self.fork); + if let Some(attestation) = self.sign_attestation(attestation, self.duty, domain) { + self.beacon_node.publish_attestation(attestation)?; + Ok(ValidatorEvent::AttestationProduced(self.duty.slot)) } else { - Ok(ValidatorEvent::SlashableAttestationNotProduced(self.slot)) + Ok(ValidatorEvent::SignerRejection(self.duty.slot)) } } else { - Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(self.slot)) + Ok(ValidatorEvent::SlashableAttestationNotProduced( + self.duty.slot, + )) } } @@ -82,33 +99,39 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { /// /// Important: this function will not check to ensure the attestation is not slashable. This must be /// done upstream. - fn sign_attestation(&mut self, mut attestation: Attestation, duties: AttestationDuties, domain: u64) -> Option { + fn sign_attestation( + &mut self, + mut attestation: AttestationData, + duties: AttestationDuty, + domain: u64, + ) -> Option { self.store_produce(&attestation); // build the aggregate signature - let aggregate_sig = { + let aggregate_signature = { let message = AttestationDataAndCustodyBit { - data: attestation.clone(), - custody_bit: false, - }.hash_tree_root(); + data: attestation.clone(), + custody_bit: false, + } + .hash_tree_root(); let sig = self.signer.sign_message(&message, domain)?; let mut agg_sig = AggregateSignature::new(); agg_sig.add(&sig); agg_sig - } + }; - let mut aggregation_bitfield = Bitfield::with_capacity(duties.comitee_size); - let custody_bitfield = Bitfield::with_capacity(duties.committee_size); - aggregation_bitfield.set(duties.committee_index, true); + let mut aggregation_bitfield = Bitfield::with_capacity(duties.committee_len); + let custody_bitfield = Bitfield::with_capacity(duties.committee_len); + aggregation_bitfield.set(duties.committee_index, true); - Attestation { - aggregation_bitfield, - data, - custody_bitfield, - aggregate_signature, - } + Some(Attestation { + aggregation_bitfield, + data: attestation, + custody_bitfield, + aggregate_signature, + }) } /// Returns `true` if signing an attestation is safe (non-slashable). @@ -116,8 +139,8 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { /// !!! UNSAFE !!! /// /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. - fn safe_to_produce(&self, _block: &Attestation) -> bool { - //TODO: Implement slash protection + fn safe_to_produce(&self, _attestation: &AttestationData) -> bool { + //TODO: Implement slash protection true } @@ -126,7 +149,7 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { /// !!! UNSAFE !!! /// /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. - fn store_produce(&mut self, _block: &BeaconBlock) { + fn store_produce(&mut self, _attestation: &AttestationData) { // TODO: Implement slash protection } } diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index 09ce9bffae..592c0b9192 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -1,10 +1,11 @@ mod beacon_node_block; mod grpc; -use self::beacon_node_block::{BeaconNodeBlock, BeaconNodeError}; +use self::beacon_node_block::BeaconNodeBlock; +pub use self::beacon_node_block::{BeaconNodeError, PublishOutcome}; pub use self::grpc::BeaconBlockGrpcClient; use crate::signer::Signer; -use slog::{error, info}; +use slog::{error, info, warn}; use ssz::{SignedRoot, TreeHash}; use std::sync::Arc; use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; @@ -18,10 +19,16 @@ pub enum Error { pub enum ValidatorEvent { /// A new block was produced. BlockProduced(Slot), + /// A new attestation was produced. + AttestationProduced(Slot), /// A block was not produced as it would have been slashable. SlashableBlockNotProduced(Slot), + /// An attestation was not produced as it would have been slashable. + SlashableAttestationNotProduced(Slot), /// The Beacon Node was unable to produce a block at that slot. BeaconNodeUnableToProduceBlock(Slot), + /// The Beacon Node was unable to produce an attestation at that slot. + BeaconNodeUnableToProduceAttestation(Slot), /// The signer failed to sign the message. SignerRejection(Slot), } @@ -58,6 +65,9 @@ impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> { Ok(ValidatorEvent::BeaconNodeUnableToProduceBlock(_slot)) => { error!(log, "Block production error"; "Error" => format!("Beacon node was unable to produce a block")) } + Ok(v) => { + warn!(log, "Unknown result for block production"; "Error" => format!("{:?}",v)) + } } } @@ -76,7 +86,7 @@ impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> { let randao_reveal = { let message = epoch.hash_tree_root(); - let randao_reveal = match self.signer.sign_randao_reveal( + let randao_reveal = match self.signer.sign_message( &message, self.spec.get_domain(epoch, Domain::Randao, &self.fork), ) { @@ -113,10 +123,7 @@ impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> { fn sign_block(&mut self, mut block: BeaconBlock, domain: u64) -> Option { self.store_produce(&block); - match self - .signer - .sign_block_proposal(&block.signed_root()[..], domain) - { + match self.signer.sign_message(&block.signed_root()[..], domain) { None => None, Some(signature) => { block.signature = signature; diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index d0753a2a6a..692a8d6a62 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -38,7 +38,7 @@ impl EpochDuty { // if the validator is required to attest to a shard, create the data let mut attestation_duty = None; if self.attestation_duty.slot == slot { - attestation_duty = self.attestation_duty + attestation_duty = Some(self.attestation_duty) } if produce_block | attestation_duty.is_some() { @@ -60,7 +60,7 @@ impl fmt::Display for EpochDuty { write!( f, "produce block slot: {}, attestation slot: {}, attestation shard: {}", - display_block, self.attestation_slot, self.attestation_shard + display_block, self.attestation_duty.slot, self.attestation_duty.shard ) } } diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index f0d892e98f..ab87b602e0 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -6,7 +6,7 @@ use protos::services_grpc::ValidatorServiceClient; use ssz::ssz_encode; use std::collections::HashMap; use std::time::Duration; -use types::{Epoch, PublicKey, Slot}; +use types::{AttestationDuty, Epoch, PublicKey, Slot}; impl BeaconNodeDuties for ValidatorServiceClient { /// Requests all duties (block signing and committee attesting) from the Beacon Node (BN). @@ -49,11 +49,11 @@ impl BeaconNodeDuties for ValidatorServiceClient { }; let attestation_duty = AttestationDuty { - slot: Slot::from(active_duty.get_attestation_slot()), - shard: active_duty.get_attestation_shard(), - committee_index: active_duty.get_committee_index(), - comittee_size: 10, - } + slot: Slot::from(active_duty.get_attestation_slot()), + shard: active_duty.get_attestation_shard(), + committee_index: active_duty.get_committee_index() as usize, + committee_len: 10, + }; let epoch_duty = EpochDuty { block_production_slot, From d12ddae24763683eac3f9844719de8bcbc9e5ecb Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 18:14:04 +1100 Subject: [PATCH 152/191] Builds attestation grpc implemention --- protos/src/services.proto | 28 +--- .../beacon_node_attestation.rs | 23 --- validator_client/src/attester_service/grpc.rs | 45 ----- validator_client/src/attester_service/mod.rs | 155 ------------------ validator_client/src/block_producer/grpc.rs | 1 + validator_client/src/main.rs | 2 +- 6 files changed, 8 insertions(+), 246 deletions(-) delete mode 100644 validator_client/src/attester_service/beacon_node_attestation.rs delete mode 100644 validator_client/src/attester_service/grpc.rs delete mode 100644 validator_client/src/attester_service/mod.rs diff --git a/protos/src/services.proto b/protos/src/services.proto index 7cd653a600..c5bd8a91d3 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -141,7 +141,11 @@ message ProduceAttestationDataResponse { } message PublishAttestationRequest { - FreeAttestation free_attestation = 1; + Attestation attestation = 1; +} + +message Attestation { + bytes ssz = 1; } message PublishAttestationResponse { @@ -149,26 +153,6 @@ message PublishAttestationResponse { bytes msg = 2; } -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 data = 1; - bytes signature = 2; - uint64 validator_index = 3; + bytes ssz = 1; } diff --git a/validator_client/src/attester_service/beacon_node_attestation.rs b/validator_client/src/attester_service/beacon_node_attestation.rs deleted file mode 100644 index b5ff777de8..0000000000 --- a/validator_client/src/attester_service/beacon_node_attestation.rs +++ /dev/null @@ -1,23 +0,0 @@ -//TODO: generalise these enums to the crate -use crate::block_producer::{BeaconNodeError, PublishOutcome}; -use types::{Attestation, AttestationData, Slot}; - -/// Defines the methods required to produce and publish attestations on a Beacon Node. Abstracts the -/// actual beacon node. -pub trait BeaconNodeAttestation: Send + Sync { - /// Request that the node produces the required attestation data. - /// - fn produce_attestation_data( - &self, - slot: Slot, - shard: u64, - ) -> Result; - - /// Request that the node publishes a attestation. - /// - /// Returns `true` if the publish was successful. - fn publish_attestation( - &self, - attestation: Attestation, - ) -> Result; -} diff --git a/validator_client/src/attester_service/grpc.rs b/validator_client/src/attester_service/grpc.rs deleted file mode 100644 index 502e51cac9..0000000000 --- a/validator_client/src/attester_service/grpc.rs +++ /dev/null @@ -1,45 +0,0 @@ -use protos::services_grpc::AttestationServiceClient; -use std::sync::Arc; - -use attester::{BeaconNode, BeaconNodeError, PublishOutcome}; -use protos::services::ProduceAttestationDataRequest; -use types::{Attestation, AttestationData, 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> { - 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 Attestation - Err(BeaconNodeError::DecodeFailure) - } - - fn publish_attestation( - &self, - attestation: Attestation, - ) -> Result { - // TODO: return correct PublishOutcome - Err(BeaconNodeError::DecodeFailure) - } -} -*/ diff --git a/validator_client/src/attester_service/mod.rs b/validator_client/src/attester_service/mod.rs deleted file mode 100644 index 7b2174b0cd..0000000000 --- a/validator_client/src/attester_service/mod.rs +++ /dev/null @@ -1,155 +0,0 @@ -mod beacon_node_attestation; -mod grpc; - -use std::sync::Arc; -use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; -//TODO: Move these higher up in the crate -use super::block_producer::{BeaconNodeError, ValidatorEvent}; -use crate::signer::Signer; -use beacon_node_attestation::BeaconNodeAttestation; -use slog::{error, info, warn}; -use ssz::TreeHash; -use types::{ - AggregateSignature, Attestation, AttestationData, AttestationDataAndCustodyBit, - AttestationDuty, Bitfield, -}; - -//TODO: Group these errors at a crate level -#[derive(Debug, PartialEq)] -pub enum Error { - BeaconNodeError(BeaconNodeError), -} - -impl From for Error { - fn from(e: BeaconNodeError) -> Error { - Error::BeaconNodeError(e) - } -} - -/// This struct contains the logic for requesting and signing beacon attestations for a validator. The -/// validator can abstractly sign via the Signer trait object. -pub struct AttestationProducer<'a, B: BeaconNodeAttestation, S: Signer> { - /// The current fork. - pub fork: Fork, - /// The attestation duty to perform. - pub duty: AttestationDuty, - /// The current epoch. - pub spec: Arc, - /// The beacon node to connect to. - pub beacon_node: Arc, - /// The signer to sign the block. - pub signer: &'a S, -} - -impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { - /// Handle outputs and results from attestation production. - pub fn handle_produce_attestation(&mut self, log: slog::Logger) { - match self.produce_attestation() { - Ok(ValidatorEvent::AttestationProduced(_slot)) => { - info!(log, "Attestation produced"; "Validator" => format!("{}", self.signer)) - } - Err(e) => error!(log, "Attestation production error"; "Error" => format!("{:?}", e)), - Ok(ValidatorEvent::SignerRejection(_slot)) => { - error!(log, "Attestation production error"; "Error" => format!("Signer could not sign the attestation")) - } - Ok(ValidatorEvent::SlashableAttestationNotProduced(_slot)) => { - error!(log, "Attestation production error"; "Error" => format!("Rejected the attestation as it could have been slashed")) - } - Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(_slot)) => { - error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to produce an attestation")) - } - Ok(v) => { - warn!(log, "Unknown result for attestation production"; "Error" => format!("{:?}",v)) - } - } - } - - /// Produce an attestation, sign it and send it back - /// - /// Assumes that an attestation is required at this slot (does not check the duties). - /// - /// Ensures the message is not slashable. - /// - /// !!! UNSAFE !!! - /// - /// The slash-protection code is not yet implemented. There is zero protection against - /// slashing. - pub fn produce_attestation(&mut self) -> Result { - let epoch = self.duty.slot.epoch(self.spec.slots_per_epoch); - - let attestation = self - .beacon_node - .produce_attestation_data(self.duty.slot, self.duty.shard)?; - if self.safe_to_produce(&attestation) { - let domain = self.spec.get_domain(epoch, Domain::Attestation, &self.fork); - if let Some(attestation) = self.sign_attestation(attestation, self.duty, domain) { - self.beacon_node.publish_attestation(attestation)?; - Ok(ValidatorEvent::AttestationProduced(self.duty.slot)) - } else { - Ok(ValidatorEvent::SignerRejection(self.duty.slot)) - } - } else { - Ok(ValidatorEvent::SlashableAttestationNotProduced( - self.duty.slot, - )) - } - } - - /// Consumes an attestation, returning the attestation signed by the validators private key. - /// - /// Important: this function will not check to ensure the attestation is not slashable. This must be - /// done upstream. - fn sign_attestation( - &mut self, - mut attestation: AttestationData, - duties: AttestationDuty, - domain: u64, - ) -> Option { - self.store_produce(&attestation); - - // build the aggregate signature - let aggregate_signature = { - let message = AttestationDataAndCustodyBit { - data: attestation.clone(), - custody_bit: false, - } - .hash_tree_root(); - - let sig = self.signer.sign_message(&message, domain)?; - - let mut agg_sig = AggregateSignature::new(); - agg_sig.add(&sig); - agg_sig - }; - - let mut aggregation_bitfield = Bitfield::with_capacity(duties.committee_len); - let custody_bitfield = Bitfield::with_capacity(duties.committee_len); - aggregation_bitfield.set(duties.committee_index, true); - - Some(Attestation { - aggregation_bitfield, - data: attestation, - custody_bitfield, - aggregate_signature, - }) - } - - /// Returns `true` if signing an attestation is safe (non-slashable). - /// - /// !!! UNSAFE !!! - /// - /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. - fn safe_to_produce(&self, _attestation: &AttestationData) -> bool { - //TODO: Implement slash protection - true - } - - /// Record that an attestation was produced so that slashable votes may not be made in the future. - /// - /// !!! UNSAFE !!! - /// - /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. - fn store_produce(&mut self, _attestation: &AttestationData) { - // TODO: Implement slash protection - } -} diff --git a/validator_client/src/block_producer/grpc.rs b/validator_client/src/block_producer/grpc.rs index ab0d5d4210..fd1a70fa2f 100644 --- a/validator_client/src/block_producer/grpc.rs +++ b/validator_client/src/block_producer/grpc.rs @@ -7,6 +7,7 @@ use ssz::{ssz_encode, Decodable}; use std::sync::Arc; use types::{BeaconBlock, Signature, Slot}; +//TODO: Remove this new type. Do not need to wrap /// A newtype designed to wrap the gRPC-generated service so the `BeaconNode` trait may be /// implemented upon it. pub struct BeaconBlockGrpcClient { diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 4817667348..7a353e0dcc 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,4 +1,4 @@ -mod attester_service; +mod attestation_producer; mod block_producer; mod config; mod duties; From c2b6f949c0e6ab0693f5cf036078d1f7a4b16408 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 18:25:32 +1100 Subject: [PATCH 153/191] Restrict blop pool from re-including attestations --- .../specs/validator_registry.yaml | 3 ++- eth2/operation_pool/src/lib.rs | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml b/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml index 1674ecffc7..ad9c899cfe 100644 --- a/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml +++ b/beacon_node/beacon_chain/test_harness/specs/validator_registry.yaml @@ -48,7 +48,8 @@ test_cases: - slot: 63 num_validators: 1003 num_previous_epoch_attestations: 0 - num_current_epoch_attestations: 10 + # slots_per_epoch - attestation_inclusion_delay - skip_slots + num_current_epoch_attestations: 57 slashed_validators: [11, 12, 13, 14, 42] exited_validators: [] exit_initiated_validators: [50] diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index c42527b608..9d4d0091ab 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -172,6 +172,8 @@ impl OperationPool { || key.domain_bytes_match(&curr_domain_bytes) }) .flat_map(|(_, attestations)| attestations) + // That are not superseded by an attestation included in the state... + .filter(|attestation| !superior_attestation_exists_in_state(state, attestation)) // That are valid... .filter(|attestation| validate_attestation(state, attestation, spec).is_ok()) // Scored by the number of new attestations they introduce (descending) @@ -462,6 +464,31 @@ impl OperationPool { } } +/// Returns `true` if the state already contains a `PendingAttestation` that is superior to the +/// given `attestation`. +/// +/// A validator has nothing to gain from re-including an attestation and it adds load to the +/// network. +/// +/// An existing `PendingAttestation` is superior to an existing `attestation` if: +/// +/// - Their `AttestationData` is equal. +/// - `attestation` does not contain any signatures that `PendingAttestation` does not have. +fn superior_attestation_exists_in_state(state: &BeaconState, attestation: &Attestation) -> bool { + state + .current_epoch_attestations + .iter() + .chain(state.previous_epoch_attestations.iter()) + .any(|existing_attestation| { + let bitfield = &attestation.aggregation_bitfield; + let existing_bitfield = &existing_attestation.aggregation_bitfield; + + existing_attestation.data == attestation.data + && bitfield.intersection(existing_bitfield).num_set_bits() + == bitfield.num_set_bits() + }) +} + /// Filter up to a maximum number of operations out of an iterator. fn filter_limit_operations<'a, T: 'a, I, F>(operations: I, filter: F, limit: u64) -> Vec where From d8fd7c8803f3ed780177463a060fb0e3fa44a7d8 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 18:36:38 +1100 Subject: [PATCH 154/191] Implement beacon node side of attestation production gRPC --- beacon_node/rpc/src/beacon_attester.rs | 100 +++++++++++++++++++------ 1 file changed, 76 insertions(+), 24 deletions(-) diff --git a/beacon_node/rpc/src/beacon_attester.rs b/beacon_node/rpc/src/beacon_attester.rs index 4166c30d4d..d787d1e5f0 100644 --- a/beacon_node/rpc/src/beacon_attester.rs +++ b/beacon_node/rpc/src/beacon_attester.rs @@ -7,9 +7,7 @@ use protos::services::{ PublishAttestation }; use protos::services_grpc::BeaconBlockService; -use slog::{Logger, info, warn, error}; - -const TEST_SHARD_PHASE_ZERO: u8 = 0; +use slog::{Logger, info, warn, error, trace}; #[derive(Clone)] pub struct AttestationServiceInstance { @@ -25,30 +23,47 @@ impl AttestationService for AttestationServiceInstance { req: ProduceAttestationDataRequest, sink: UnarySink, ) { - info!(&self.log, "Attempting to produce attestation at slot {}", req.get_slot()); + trace!(&self.log, "Attempting to produce attestation at slot {}", req.get_slot()); - // get the chain spec & state - let spec = self.chain.get_spec(); - let state = self.chain.get_state(); + // verify the slot, drop lock on state afterwards + { + let slot_requested = req.get_slot(); + let state = self.chain.get_state(); - let slot_requested = req.get_slot(); - - // Start by performing some checks - // Check that the the AttestionData is for the current slot (otherwise it will not be valid) - if slot_requested != state.slot { - let f = sink - .fail(RpcStatus::new( - RpcStatusCode::OutOfRange, - "AttestationData request for a slot that is not the current slot." - )) - .map_err(move |e| error!(&self.log, "Failed to reply with failure {:?}: {:?}", req, e)); + // Start by performing some checks + // Check that the the AttestionData is for the current slot (otherwise it will not be valid) + if slot_requested != state.slot { + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::OutOfRange, + "AttestationData request for a slot that is not the current slot." + )) + .map_err(move |e| error!(&self.log, "Failed to reply with failure {:?}: {:?}", req, e)); + } } - // Then get the AttestationData from the beacon chain (for shard 0 for now) - let attestation_data = self.chain.produce_attestation_data(TEST_SHARD_PHASE_ZERO); + // Then get the AttestationData from the beacon chain + let attestation_data = match self.chain.produce_attestation_data(req.get_shard()){ + Ok(v) => v, + Err(e) => { + // Could not produce an attestation + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::Unknown + Some(format!("Could not produce an attestation: {:?}",e)), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; + + + let mut attestation_data_proto = AttestationDataProto::new(); + attestation_data_proto.set_ssz(ssz_encode(&attestation_data)); let mut resp = ProduceAttestationDataResponse::new(); - resp.set_attestation_data(attestation_data); + resp.set_attestation_data(attestation_data_proto); let f = sink .success(resp) @@ -64,12 +79,49 @@ impl AttestationService for AttestationServiceInstance { req: PublishAttestationRequest, sink: UnarySink, ) { - println!("publishing attestation {:?}", req.get_block()); + trace!(self.log, "Publishing attestation"); - // TODO: actually process the block. let mut resp = PublishAttestationResponse::new(); + let ssz_serialized_attestation = req.get_attestation().get_ssz(); - resp.set_success(true); + let attestation = match Attestation::ssz_decode(ssz_serialized_attestation, 0) { + Ok((v, _index)) => v, + Err(_) => { + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some("Invalid attestation".to_string()), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?}", req)); + return ctx.spawn(f); + } + }; + + match self.chain.process_attestation(attestation) { + Ok(_) => { + // Attestation was successfully processed. + info!( + self.log, + "PublishAttestation"; + "type" => "valid_attestation", + ); + + resp.set_success(true); + }, + Err(e)=> { + // Attestation was invalid + warn!( + self.log, + "PublishAttestation"; + "type" => "invalid_attestation", + ); + resp.set_success(false); + resp.set_msg( + format!("InvalidAttestation: {:?}", e).as_bytes().to_vec(), + ); + } + }; let f = sink .success(resp) From e1befe9d3a22fe8d1c2414757ad6be0672221ae6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 18:46:06 +1100 Subject: [PATCH 155/191] Adds attestation producer to the validation client --- validator_client/src/service.rs | 53 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index fd24744ace..ba0795ec10 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -8,15 +8,13 @@ /// When a validator needs to either produce a block or sign an attestation, it requests the /// data from the beacon node and performs the signing before publishing the block to the beacon /// node. -//use crate::attester_service::{AttestationGrpcClient, AttesterService}; +use crate::attestation_producer::AttestationProducer; use crate::block_producer::{BeaconBlockGrpcClient, BlockProducer}; use crate::config::Config as ValidatorConfig; -use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap, UpdateOutcome}; +use crate::duties::{BeaconNodeDuties, DutiesManager, EpochDutiesMap}; use crate::error as error_chain; use crate::error::ErrorKind; use crate::signer::Signer; -use attester::test_utils::EpochMap; -use attester::{test_utils::LocalSigner as AttesterLocalSigner, Attester}; use bls::Keypair; use grpcio::{ChannelBuilder, EnvBuilder}; use protos::services::Empty; @@ -24,11 +22,10 @@ use protos::services_grpc::{ AttestationServiceClient, BeaconBlockServiceClient, BeaconNodeServiceClient, ValidatorServiceClient, }; -use slog::{debug, error, info, warn}; +use slog::{error, info, warn}; use slot_clock::{SlotClock, SystemTimeSlotClock}; use std::sync::Arc; use std::sync::RwLock; -use std::thread; use std::time::{Duration, Instant, SystemTime}; use tokio::prelude::*; use tokio::runtime::Builder; @@ -55,7 +52,7 @@ pub struct Service { /// The beacon block GRPC client. beacon_block_client: Arc, /// The attester GRPC client. - attester_client: Arc, + attestation_client: Arc, /// The validator client logger. log: slog::Logger, } @@ -148,7 +145,7 @@ impl Service { }; //Beacon node gRPC attester endpoints. - let attester_client = { + let attestation_client = { let ch = ChannelBuilder::new(env.clone()).connect(&config.server); Arc::new(AttestationServiceClient::new(ch)) }; @@ -194,7 +191,7 @@ impl Service { spec, duties_manager, beacon_block_client, - attester_client, + attestation_client, log, }) } @@ -301,6 +298,7 @@ impl Service { if let Some(work) = self.duties_manager.get_current_work(self.current_slot) { for (signer_index, work_type) in work { if work_type.produce_block { + // we need to produce a block // spawns a thread to produce a beacon block let signers = self.duties_manager.signers.clone(); // this is an arc let fork = self.fork.clone(); @@ -320,26 +318,27 @@ impl Service { }; block_producer.handle_produce_block(log); }); - - // 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"); - let attester_grpc_client = Arc::new(AttestationGrpcClient::new( - service.attester_client.clone(), - )); - let signer = Arc::new(AttesterLocalSigner::new(keypair.clone())); - let attester = Attester::new(attester_grpc_client, signer); - let mut attester_service = AttesterService { - attester, - poll_interval_millis: POLL_INTERVAL_MILLIS, - log: log.clone(), - }; - attester_service.run(); - */ + // we need to produce an attestation + // spawns a thread to produce and sign an attestation + let signers = self.duties_manager.signers.clone(); // this is an arc + let fork = self.fork.clone(); + let spec = self.spec.clone(); + let beacon_node = self.attestation_client.clone(); + let log = self.log.clone(); + std::thread::spawn(move || { + info!(log, "Producing an attestation"; "Validator"=> format!("{}", signers[signer_index])); + let signer = &signers[signer_index]; + let mut attestation_producer = AttestationProducer { + fork, + duty: work_type.attestation_duty.expect("Should never be none"), + spec, + beacon_node, + signer, + }; + attestation_producer.handle_produce_attestation(log); + }); } } } From 935c64deef97431d71e20272c9411e99ecfd2722 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 19:11:52 +1100 Subject: [PATCH 156/191] Adds attestation validation to `SimpleSync` --- beacon_node/beacon_chain/src/beacon_chain.rs | 2 +- beacon_node/beacon_chain/src/lib.rs | 4 ++++ beacon_node/network/src/beacon_chain.rs | 15 +++++---------- beacon_node/network/src/sync/simple_sync.rs | 7 ++----- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 7c2336a28b..6ca0bff733 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -11,7 +11,7 @@ use operation_pool::OperationPool; use parking_lot::{RwLock, RwLockReadGuard}; use slot_clock::SlotClock; use ssz::ssz_encode; -pub use state_processing::per_block_processing::errors::{ +use state_processing::per_block_processing::errors::{ AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, ExitValidationError, ProposerSlashingValidationError, TransferValidationError, }; diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 48a42b941e..409b86e028 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -13,4 +13,8 @@ pub use db; pub use fork_choice; pub use parking_lot; pub use slot_clock; +pub use state_processing::per_block_processing::errors::{ + AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, + ExitValidationError, ProposerSlashingValidationError, TransferValidationError, +}; pub use types; diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 8ec8162ff7..7a8efb2540 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -5,7 +5,7 @@ use beacon_chain::{ parking_lot::RwLockReadGuard, slot_clock::SlotClock, types::{BeaconState, ChainSpec}, - AggregationOutcome, CheckPoint, + AttestationValidationError, CheckPoint, }; use eth2_libp2p::rpc::HelloMessage; use types::{Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; @@ -40,7 +40,7 @@ pub trait BeaconChain: Send + Sync { fn process_attestation( &self, attestation: Attestation, - ) -> Result; + ) -> Result<(), AttestationValidationError>; fn get_block_roots( &self, @@ -126,14 +126,9 @@ where fn process_attestation( &self, - _attestation: Attestation, - ) -> Result { - // Awaiting a proper operations pool before we can import attestations. - // - // Returning a useless error for now. - // - // https://github.com/sigp/lighthouse/issues/281 - return Err(BeaconChainError::DBInconsistent("CANNOT PROCESS".into())); + attestation: Attestation, + ) -> Result<(), AttestationValidationError> { + self.process_attestation(attestation) } fn get_block_roots( diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 85949fa98e..1afba018d9 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -563,12 +563,9 @@ impl SimpleSync { "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) { - Ok(_) => panic!("Impossible, method not implemented."), - Err(_) => error!(self.log, "Attestation processing not implemented!"), + Ok(()) => info!(self.log, "ImportedAttestation"), + Err(e) => warn!(self.log, "InvalidAttestation"; "error" => format!("{:?}", e)), } } From 65ae8fda47cc323d7ed591037e3968b97ee978ec Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Mar 2019 19:15:15 +1100 Subject: [PATCH 157/191] Remove old attestation aggregator --- .../src/attestation_aggregator.rs | 218 ------------------ beacon_node/beacon_chain/src/lib.rs | 2 - 2 files changed, 220 deletions(-) delete mode 100644 beacon_node/beacon_chain/src/attestation_aggregator.rs diff --git a/beacon_node/beacon_chain/src/attestation_aggregator.rs b/beacon_node/beacon_chain/src/attestation_aggregator.rs deleted file mode 100644 index 9b4e5a6874..0000000000 --- a/beacon_node/beacon_chain/src/attestation_aggregator.rs +++ /dev/null @@ -1,218 +0,0 @@ -use ssz::TreeHash; -use state_processing::per_block_processing::validate_attestation_without_signature; -use std::collections::{HashMap, HashSet}; -use types::*; - -const PHASE_0_CUSTODY_BIT: bool = false; - -/// Provides the functionality to: -/// -/// - Recieve a `FreeAttestation` and aggregate it into an `Attestation` (or create a new if it -/// doesn't exist). -/// - Store all aggregated or created `Attestation`s. -/// - Produce a list of attestations that would be valid for inclusion in some `BeaconState` (and -/// therefore valid for inclusion in a `BeaconBlock`. -/// -/// Note: `Attestations` are stored in memory and never deleted. This is not scalable and must be -/// rectified in a future revision. -#[derive(Default)] -pub struct AttestationAggregator { - store: HashMap, Attestation>, -} - -pub struct Outcome { - pub valid: bool, - pub message: Message, -} - -pub enum Message { - /// The free attestation was added to an existing attestation. - Aggregated, - /// The free attestation has already been aggregated to an existing attestation. - AggregationNotRequired, - /// The free attestation was transformed into a new attestation. - NewAttestationCreated, - /// The supplied `validator_index` is not in the committee for the given `shard` and `slot`. - BadValidatorIndex, - /// The given `signature` did not match the `pubkey` in the given - /// `state.validator_registry`. - BadSignature, - /// The given `slot` does not match the validators committee assignment. - BadSlot, - /// The given `shard` does not match the validators committee assignment, or is not included in - /// a committee for the given slot. - BadShard, - /// Attestation is from the epoch prior to this, ignoring. - TooOld, -} - -macro_rules! valid_outcome { - ($error: expr) => { - return Ok(Outcome { - valid: true, - message: $error, - }); - }; -} - -macro_rules! invalid_outcome { - ($error: expr) => { - return Ok(Outcome { - valid: false, - message: $error, - }); - }; -} - -impl AttestationAggregator { - /// Instantiates a new AttestationAggregator with an empty database. - pub fn new() -> Self { - Self { - store: HashMap::new(), - } - } - - /// Accepts some `FreeAttestation`, validates it and either aggregates it upon some existing - /// `Attestation` or produces a new `Attestation`. - /// - /// The "validation" provided is not complete, instead the following points are checked: - /// - The given `validator_index` is in the committee for the given `shard` for the given - /// `slot`. - /// - The signature is verified against that of the validator at `validator_index`. - pub fn process_free_attestation( - &mut self, - state: &BeaconState, - free_attestation: &FreeAttestation, - spec: &ChainSpec, - ) -> Result { - let duties = - match state.get_attestation_duties(free_attestation.validator_index as usize, spec) { - Err(BeaconStateError::EpochCacheUninitialized(e)) => { - panic!("Attempted to access unbuilt cache {:?}.", e) - } - Err(BeaconStateError::EpochOutOfBounds) => invalid_outcome!(Message::TooOld), - Err(BeaconStateError::ShardOutOfBounds) => invalid_outcome!(Message::BadShard), - Err(e) => return Err(e), - Ok(None) => invalid_outcome!(Message::BadValidatorIndex), - Ok(Some(attestation_duties)) => attestation_duties, - }; - - if free_attestation.data.slot != duties.slot { - invalid_outcome!(Message::BadSlot); - } - if free_attestation.data.shard != duties.shard { - invalid_outcome!(Message::BadShard); - } - - let signable_message = AttestationDataAndCustodyBit { - data: free_attestation.data.clone(), - custody_bit: PHASE_0_CUSTODY_BIT, - } - .hash_tree_root(); - - let validator_record = match state - .validator_registry - .get(free_attestation.validator_index as usize) - { - None => invalid_outcome!(Message::BadValidatorIndex), - Some(validator_record) => validator_record, - }; - - if !free_attestation.signature.verify( - &signable_message, - spec.get_domain(state.current_epoch(spec), Domain::Attestation, &state.fork), - &validator_record.pubkey, - ) { - invalid_outcome!(Message::BadSignature); - } - - if let Some(existing_attestation) = self.store.get(&signable_message) { - if let Some(updated_attestation) = aggregate_attestation( - existing_attestation, - &free_attestation.signature, - duties.committee_index as usize, - ) { - self.store.insert(signable_message, updated_attestation); - valid_outcome!(Message::Aggregated); - } else { - valid_outcome!(Message::AggregationNotRequired); - } - } else { - let mut aggregate_signature = AggregateSignature::new(); - aggregate_signature.add(&free_attestation.signature); - let mut aggregation_bitfield = Bitfield::new(); - aggregation_bitfield.set(duties.committee_index as usize, true); - let new_attestation = Attestation { - data: free_attestation.data.clone(), - aggregation_bitfield, - custody_bitfield: Bitfield::new(), - aggregate_signature, - }; - self.store.insert(signable_message, new_attestation); - valid_outcome!(Message::NewAttestationCreated); - } - } - - /// Returns all known attestations which are: - /// - /// - Valid for the given state - /// - Not already in `state.latest_attestations`. - pub fn get_attestations_for_state( - &self, - state: &BeaconState, - spec: &ChainSpec, - ) -> Vec { - let mut known_attestation_data: HashSet = HashSet::new(); - - state - .previous_epoch_attestations - .iter() - .chain(state.current_epoch_attestations.iter()) - .for_each(|attestation| { - known_attestation_data.insert(attestation.data.clone()); - }); - - self.store - .values() - .filter_map(|attestation| { - if validate_attestation_without_signature(&state, attestation, spec).is_ok() - && !known_attestation_data.contains(&attestation.data) - { - Some(attestation.clone()) - } else { - None - } - }) - .collect() - } -} - -/// Produces a new `Attestation` where: -/// -/// - `signature` is added to `Attestation.aggregate_signature` -/// - Attestation.aggregation_bitfield[committee_index]` is set to true. -fn aggregate_attestation( - existing_attestation: &Attestation, - signature: &Signature, - committee_index: usize, -) -> Option { - let already_signed = existing_attestation - .aggregation_bitfield - .get(committee_index) - .unwrap_or(false); - - if already_signed { - None - } else { - let mut aggregation_bitfield = existing_attestation.aggregation_bitfield.clone(); - aggregation_bitfield.set(committee_index, true); - let mut aggregate_signature = existing_attestation.aggregate_signature.clone(); - aggregate_signature.add(&signature); - - Some(Attestation { - aggregation_bitfield, - aggregate_signature, - ..existing_attestation.clone() - }) - } -} diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 409b86e028..8b8fcb5e62 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -1,4 +1,3 @@ -mod attestation_aggregator; mod beacon_chain; mod checkpoint; mod errors; @@ -8,7 +7,6 @@ pub mod test_utils; pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; pub use self::errors::BeaconChainError; -pub use attestation_aggregator::Outcome as AggregationOutcome; pub use db; pub use fork_choice; pub use parking_lot; From fc5142c09abbe11597f1ac302c13125a554e42a6 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 19:32:32 +1100 Subject: [PATCH 158/191] Registers the attester service to the beacon node RPC client --- beacon_node/beacon_chain/src/lib.rs | 4 +- beacon_node/rpc/src/attestation.rs | 143 ++++++++++++++++ beacon_node/rpc/src/beacon_attester.rs | 131 --------------- beacon_node/rpc/src/beacon_block.rs | 6 +- beacon_node/rpc/src/beacon_chain.rs | 22 ++- beacon_node/rpc/src/lib.rs | 17 +- .../testing_beacon_state_builder.rs | 2 +- .../beacon_node_attestation.rs | 23 +++ .../src/attestation_producer/grpc.rs | 59 +++++++ .../src/attestation_producer/mod.rs | 155 ++++++++++++++++++ validator_client/src/service.rs | 3 + 11 files changed, 425 insertions(+), 140 deletions(-) create mode 100644 beacon_node/rpc/src/attestation.rs delete mode 100644 beacon_node/rpc/src/beacon_attester.rs create mode 100644 validator_client/src/attestation_producer/beacon_node_attestation.rs create mode 100644 validator_client/src/attestation_producer/grpc.rs create mode 100644 validator_client/src/attestation_producer/mod.rs diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 234a960945..69ff14671b 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -5,7 +5,9 @@ mod errors; pub mod initialise; pub mod test_utils; -pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; +pub use self::beacon_chain::{ + AttestationValidationError, BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock, +}; pub use self::checkpoint::CheckPoint; pub use self::errors::{BeaconChainError, BlockProductionError}; pub use attestation_aggregator::Outcome as AggregationOutcome; diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs new file mode 100644 index 0000000000..f9423df615 --- /dev/null +++ b/beacon_node/rpc/src/attestation.rs @@ -0,0 +1,143 @@ +use crate::beacon_chain::BeaconChain; +use futures::Future; +use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; +use protos::services::{ + AttestationData as AttestationDataProto, ProduceAttestationDataRequest, + ProduceAttestationDataResponse, PublishAttestationRequest, PublishAttestationResponse, +}; +use protos::services_grpc::AttestationService; +use slog::{error, info, trace, warn, Logger}; +use ssz::{ssz_encode, Decodable}; +use std::sync::Arc; +use types::Attestation; + +#[derive(Clone)] +pub struct AttestationServiceInstance { + pub chain: Arc, + pub log: slog::Logger, +} + +impl AttestationService for AttestationServiceInstance { + /// Produce the `AttestationData` for signing by a validator. + fn produce_attestation_data( + &mut self, + ctx: RpcContext, + req: ProduceAttestationDataRequest, + sink: UnarySink, + ) { + warn!( + &self.log, + "Attempting to produce attestation at slot {}", + req.get_slot() + ); + + // verify the slot, drop lock on state afterwards + { + let slot_requested = req.get_slot(); + let state = self.chain.get_state(); + + // Start by performing some checks + // Check that the the AttestionData is for the current slot (otherwise it will not be valid) + if slot_requested != state.slot.as_u64() { + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::OutOfRange, + Some(format!( + "AttestationData request for a slot that is not the current slot." + )), + )) + .map_err(move |e| { + error!(log_clone, "Failed to reply with failure {:?}: {:?}", req, e) + }); + return ctx.spawn(f); + } + } + + // Then get the AttestationData from the beacon chain + let shard = req.get_shard(); + let attestation_data = match self.chain.produce_attestation_data(shard) { + Ok(v) => v, + Err(e) => { + // Could not produce an attestation + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::Unknown, + Some(format!("Could not produce an attestation: {:?}", e)), + )) + .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); + return ctx.spawn(f); + } + }; + + let mut attestation_data_proto = AttestationDataProto::new(); + attestation_data_proto.set_ssz(ssz_encode(&attestation_data)); + + let mut resp = ProduceAttestationDataResponse::new(); + resp.set_attestation_data(attestation_data_proto); + + let error_log = self.log.clone(); + let f = sink + .success(resp) + .map_err(move |e| error!(error_log, "Failed to reply with success {:?}: {:?}", req, e)); + ctx.spawn(f) + } + + /// Accept some fully-formed `FreeAttestation` from the validator, + /// store it, and aggregate it into an `Attestation`. + fn publish_attestation( + &mut self, + ctx: RpcContext, + req: PublishAttestationRequest, + sink: UnarySink, + ) { + warn!(self.log, "Publishing attestation"); + + let mut resp = PublishAttestationResponse::new(); + let ssz_serialized_attestation = req.get_attestation().get_ssz(); + + let attestation = match Attestation::ssz_decode(ssz_serialized_attestation, 0) { + Ok((v, _index)) => v, + Err(_) => { + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some("Invalid attestation".to_string()), + )) + .map_err(move |_| warn!(log_clone, "failed to reply {:?}", req)); + return ctx.spawn(f); + } + }; + + match self.chain.process_attestation(attestation) { + Ok(_) => { + // Attestation was successfully processed. + info!( + self.log, + "PublishAttestation"; + "type" => "valid_attestation", + ); + + resp.set_success(true); + } + Err(e) => { + // Attestation was invalid + warn!( + self.log, + "PublishAttestation"; + "type" => "invalid_attestation", + ); + resp.set_success(false); + resp.set_msg(format!("InvalidAttestation: {:?}", e).as_bytes().to_vec()); + } + }; + + let error_log = self.log.clone(); + let f = sink + .success(resp) + .map_err(move |e| error!(error_log, "failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) + } +} diff --git a/beacon_node/rpc/src/beacon_attester.rs b/beacon_node/rpc/src/beacon_attester.rs deleted file mode 100644 index d787d1e5f0..0000000000 --- a/beacon_node/rpc/src/beacon_attester.rs +++ /dev/null @@ -1,131 +0,0 @@ -use crate::beacon_chain::BeaconChain; -use futures::Future; -use grpcio::{RpcContext, UnarySink, RpcStatus, RpcStatusCode}; -use protos::services::{ - AttestationData as AttestationDataProto, ProduceAttestationData, ProduceAttestationDataResponse, - ProduceAttestationDataRequest, PublishAttestationResponse, PublishAttestationRequest, - PublishAttestation -}; -use protos::services_grpc::BeaconBlockService; -use slog::{Logger, info, warn, error, trace}; - -#[derive(Clone)] -pub struct AttestationServiceInstance { - pub chain: Arc, - pub log: Logger, -} - -impl AttestationService for AttestationServiceInstance { - /// Produce the `AttestationData` for signing by a validator. - fn produce_attestation_data( - &mut self, - ctx: RpcContext, - req: ProduceAttestationDataRequest, - sink: UnarySink, - ) { - trace!(&self.log, "Attempting to produce attestation at slot {}", req.get_slot()); - - // verify the slot, drop lock on state afterwards - { - let slot_requested = req.get_slot(); - let state = self.chain.get_state(); - - // Start by performing some checks - // Check that the the AttestionData is for the current slot (otherwise it will not be valid) - if slot_requested != state.slot { - let f = sink - .fail(RpcStatus::new( - RpcStatusCode::OutOfRange, - "AttestationData request for a slot that is not the current slot." - )) - .map_err(move |e| error!(&self.log, "Failed to reply with failure {:?}: {:?}", req, e)); - } - } - - // Then get the AttestationData from the beacon chain - let attestation_data = match self.chain.produce_attestation_data(req.get_shard()){ - Ok(v) => v, - Err(e) => { - // Could not produce an attestation - let log_clone = self.log.clone(); - let f = sink - .fail(RpcStatus::new( - RpcStatusCode::Unknown - Some(format!("Could not produce an attestation: {:?}",e)), - )) - .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); - return ctx.spawn(f); - } - }; - - - let mut attestation_data_proto = AttestationDataProto::new(); - attestation_data_proto.set_ssz(ssz_encode(&attestation_data)); - - let mut resp = ProduceAttestationDataResponse::new(); - resp.set_attestation_data(attestation_data_proto); - - let f = sink - .success(resp) - .map_err(move |e| error!("Failed to reply with success {:?}: {:?}", req, e)); - ctx.spawn(f) - } - - /// Accept some fully-formed `FreeAttestation` from the validator, - /// store it, and aggregate it into an `Attestation`. - fn publish_attestation( - &mut self, - ctx: RpcContext, - req: PublishAttestationRequest, - sink: UnarySink, - ) { - trace!(self.log, "Publishing attestation"); - - let mut resp = PublishAttestationResponse::new(); - let ssz_serialized_attestation = req.get_attestation().get_ssz(); - - let attestation = match Attestation::ssz_decode(ssz_serialized_attestation, 0) { - Ok((v, _index)) => v, - Err(_) => { - let log_clone = self.log.clone(); - let f = sink - .fail(RpcStatus::new( - RpcStatusCode::InvalidArgument, - Some("Invalid attestation".to_string()), - )) - .map_err(move |e| warn!(log_clone, "failed to reply {:?}", req)); - return ctx.spawn(f); - } - }; - - match self.chain.process_attestation(attestation) { - Ok(_) => { - // Attestation was successfully processed. - info!( - self.log, - "PublishAttestation"; - "type" => "valid_attestation", - ); - - resp.set_success(true); - }, - Err(e)=> { - // Attestation was invalid - warn!( - self.log, - "PublishAttestation"; - "type" => "invalid_attestation", - ); - resp.set_success(false); - resp.set_msg( - format!("InvalidAttestation: {:?}", e).as_bytes().to_vec(), - ); - } - }; - - 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_block.rs b/beacon_node/rpc/src/beacon_block.rs index e8b3cb01b4..adb0a3c22f 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -36,8 +36,8 @@ impl BeaconBlockService for BeaconBlockServiceInstance { // decode the request // TODO: requested slot currently unused, see: https://github.com/sigp/lighthouse/issues/336 let _requested_slot = Slot::from(req.get_slot()); - let (randao_reveal, _index) = match Signature::ssz_decode(req.get_randao_reveal(), 0) { - Ok(v) => v, + let randao_reveal = match Signature::ssz_decode(req.get_randao_reveal(), 0) { + Ok((reveal, _index)) => reveal, Err(_) => { // decode error, incorrect signature let log_clone = self.log.clone(); @@ -86,6 +86,8 @@ impl BeaconBlockService for BeaconBlockServiceInstance { req: PublishBeaconBlockRequest, sink: UnarySink, ) { + trace!(&self.log, "Attempting to publish a block"); + let mut resp = PublishBeaconBlockResponse::new(); let ssz_serialized_block = req.get_block().get_ssz(); diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs index f21b8df7b4..7de48efa1c 100644 --- a/beacon_node/rpc/src/beacon_chain.rs +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -5,10 +5,10 @@ use beacon_chain::{ parking_lot::RwLockReadGuard, slot_clock::SlotClock, types::{BeaconState, ChainSpec, Signature}, - BlockProductionError, + AttestationValidationError, BlockProductionError, }; pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; -use types::BeaconBlock; +use types::{Attestation, AttestationData, BeaconBlock}; /// The RPC's API to the beacon chain. pub trait BeaconChain: Send + Sync { @@ -23,6 +23,13 @@ pub trait BeaconChain: Send + Sync { &self, randao_reveal: Signature, ) -> Result<(BeaconBlock, BeaconState), BlockProductionError>; + + fn produce_attestation_data(&self, shard: u64) -> Result; + + fn process_attestation( + &self, + attestation: Attestation, + ) -> Result<(), AttestationValidationError>; } impl BeaconChain for RawBeaconChain @@ -52,4 +59,15 @@ where ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { self.produce_block(randao_reveal) } + + fn produce_attestation_data(&self, shard: u64) -> Result { + self.produce_attestation_data(shard) + } + + fn process_attestation( + &self, + attestation: Attestation, + ) -> Result<(), AttestationValidationError> { + self.process_attestation(attestation) + } } diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 2d47b4a69a..5aac4ce558 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -1,19 +1,22 @@ +mod attestation; mod beacon_block; pub mod beacon_chain; mod beacon_node; pub mod config; mod validator; +use self::attestation::AttestationServiceInstance; use self::beacon_block::BeaconBlockServiceInstance; 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 futures::Future; +use grpcio::{Environment, ServerBuilder}; use network::NetworkMessage; use protos::services_grpc::{ - create_beacon_block_service, create_beacon_node_service, create_validator_service, + create_attestation_service, create_beacon_block_service, create_beacon_node_service, + create_validator_service, }; use slog::{info, o, warn}; use std::sync::Arc; @@ -56,11 +59,19 @@ pub fn start_server( }; create_validator_service(instance) }; + let attestation_service = { + let instance = AttestationServiceInstance { + chain: beacon_chain.clone(), + log: log.clone(), + }; + create_attestation_service(instance) + }; let mut server = ServerBuilder::new(env) .register_service(beacon_block_service) .register_service(validator_service) .register_service(beacon_node_service) + .register_service(attestation_service) .bind(config.listen_address.to_string(), config.port) .build() .unwrap(); 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 7c231b20b0..8d0aa6af62 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 = 1553918534; // arbitrary + let genesis_time = 1553932445; // arbitrary let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/attestation_producer/beacon_node_attestation.rs b/validator_client/src/attestation_producer/beacon_node_attestation.rs new file mode 100644 index 0000000000..b5ff777de8 --- /dev/null +++ b/validator_client/src/attestation_producer/beacon_node_attestation.rs @@ -0,0 +1,23 @@ +//TODO: generalise these enums to the crate +use crate::block_producer::{BeaconNodeError, PublishOutcome}; +use types::{Attestation, AttestationData, Slot}; + +/// Defines the methods required to produce and publish attestations on a Beacon Node. Abstracts the +/// actual beacon node. +pub trait BeaconNodeAttestation: Send + Sync { + /// Request that the node produces the required attestation data. + /// + fn produce_attestation_data( + &self, + slot: Slot, + shard: u64, + ) -> Result; + + /// Request that the node publishes a attestation. + /// + /// Returns `true` if the publish was successful. + fn publish_attestation( + &self, + attestation: Attestation, + ) -> Result; +} diff --git a/validator_client/src/attestation_producer/grpc.rs b/validator_client/src/attestation_producer/grpc.rs new file mode 100644 index 0000000000..49c577e242 --- /dev/null +++ b/validator_client/src/attestation_producer/grpc.rs @@ -0,0 +1,59 @@ +use super::beacon_node_attestation::BeaconNodeAttestation; +use crate::block_producer::{BeaconNodeError, PublishOutcome}; +use protos::services_grpc::AttestationServiceClient; +use ssz::{ssz_encode, Decodable}; + +use protos::services::{ + Attestation as GrpcAttestation, ProduceAttestationDataRequest, PublishAttestationRequest, +}; +use types::{Attestation, AttestationData, Slot}; + +impl BeaconNodeAttestation for AttestationServiceClient { + fn produce_attestation_data( + &self, + slot: Slot, + shard: u64, + ) -> Result { + let mut req = ProduceAttestationDataRequest::new(); + req.set_slot(slot.as_u64()); + req.set_shard(shard); + + let reply = self + .produce_attestation_data(&req) + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + + dbg!("Produced Attestation Data"); + + let (attestation_data, _index) = + AttestationData::ssz_decode(reply.get_attestation_data().get_ssz(), 0) + .map_err(|_| BeaconNodeError::DecodeFailure)?; + Ok(attestation_data) + } + + fn publish_attestation( + &self, + attestation: Attestation, + ) -> Result { + let mut req = PublishAttestationRequest::new(); + + let ssz = ssz_encode(&attestation); + + let mut grpc_attestation = GrpcAttestation::new(); + grpc_attestation.set_ssz(ssz); + + req.set_attestation(grpc_attestation); + + let reply = self + .publish_attestation(&req) + .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; + + if reply.get_success() { + Ok(PublishOutcome::Valid) + } else { + // TODO: distinguish between different errors + Ok(PublishOutcome::InvalidAttestation( + "Publish failed".to_string(), + )) + } + } +} diff --git a/validator_client/src/attestation_producer/mod.rs b/validator_client/src/attestation_producer/mod.rs new file mode 100644 index 0000000000..7b2174b0cd --- /dev/null +++ b/validator_client/src/attestation_producer/mod.rs @@ -0,0 +1,155 @@ +mod beacon_node_attestation; +mod grpc; + +use std::sync::Arc; +use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; +//TODO: Move these higher up in the crate +use super::block_producer::{BeaconNodeError, ValidatorEvent}; +use crate::signer::Signer; +use beacon_node_attestation::BeaconNodeAttestation; +use slog::{error, info, warn}; +use ssz::TreeHash; +use types::{ + AggregateSignature, Attestation, AttestationData, AttestationDataAndCustodyBit, + AttestationDuty, Bitfield, +}; + +//TODO: Group these errors at a crate level +#[derive(Debug, PartialEq)] +pub enum Error { + BeaconNodeError(BeaconNodeError), +} + +impl From for Error { + fn from(e: BeaconNodeError) -> Error { + Error::BeaconNodeError(e) + } +} + +/// This struct contains the logic for requesting and signing beacon attestations for a validator. The +/// validator can abstractly sign via the Signer trait object. +pub struct AttestationProducer<'a, B: BeaconNodeAttestation, S: Signer> { + /// The current fork. + pub fork: Fork, + /// The attestation duty to perform. + pub duty: AttestationDuty, + /// The current epoch. + pub spec: Arc, + /// The beacon node to connect to. + pub beacon_node: Arc, + /// The signer to sign the block. + pub signer: &'a S, +} + +impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { + /// Handle outputs and results from attestation production. + pub fn handle_produce_attestation(&mut self, log: slog::Logger) { + match self.produce_attestation() { + Ok(ValidatorEvent::AttestationProduced(_slot)) => { + info!(log, "Attestation produced"; "Validator" => format!("{}", self.signer)) + } + Err(e) => error!(log, "Attestation production error"; "Error" => format!("{:?}", e)), + Ok(ValidatorEvent::SignerRejection(_slot)) => { + error!(log, "Attestation production error"; "Error" => format!("Signer could not sign the attestation")) + } + Ok(ValidatorEvent::SlashableAttestationNotProduced(_slot)) => { + error!(log, "Attestation production error"; "Error" => format!("Rejected the attestation as it could have been slashed")) + } + Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(_slot)) => { + error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to produce an attestation")) + } + Ok(v) => { + warn!(log, "Unknown result for attestation production"; "Error" => format!("{:?}",v)) + } + } + } + + /// Produce an attestation, sign it and send it back + /// + /// Assumes that an attestation is required at this slot (does not check the duties). + /// + /// Ensures the message is not slashable. + /// + /// !!! UNSAFE !!! + /// + /// The slash-protection code is not yet implemented. There is zero protection against + /// slashing. + pub fn produce_attestation(&mut self) -> Result { + let epoch = self.duty.slot.epoch(self.spec.slots_per_epoch); + + let attestation = self + .beacon_node + .produce_attestation_data(self.duty.slot, self.duty.shard)?; + if self.safe_to_produce(&attestation) { + let domain = self.spec.get_domain(epoch, Domain::Attestation, &self.fork); + if let Some(attestation) = self.sign_attestation(attestation, self.duty, domain) { + self.beacon_node.publish_attestation(attestation)?; + Ok(ValidatorEvent::AttestationProduced(self.duty.slot)) + } else { + Ok(ValidatorEvent::SignerRejection(self.duty.slot)) + } + } else { + Ok(ValidatorEvent::SlashableAttestationNotProduced( + self.duty.slot, + )) + } + } + + /// Consumes an attestation, returning the attestation signed by the validators private key. + /// + /// Important: this function will not check to ensure the attestation is not slashable. This must be + /// done upstream. + fn sign_attestation( + &mut self, + mut attestation: AttestationData, + duties: AttestationDuty, + domain: u64, + ) -> Option { + self.store_produce(&attestation); + + // build the aggregate signature + let aggregate_signature = { + let message = AttestationDataAndCustodyBit { + data: attestation.clone(), + custody_bit: false, + } + .hash_tree_root(); + + let sig = self.signer.sign_message(&message, domain)?; + + let mut agg_sig = AggregateSignature::new(); + agg_sig.add(&sig); + agg_sig + }; + + let mut aggregation_bitfield = Bitfield::with_capacity(duties.committee_len); + let custody_bitfield = Bitfield::with_capacity(duties.committee_len); + aggregation_bitfield.set(duties.committee_index, true); + + Some(Attestation { + aggregation_bitfield, + data: attestation, + custody_bitfield, + aggregate_signature, + }) + } + + /// Returns `true` if signing an attestation is safe (non-slashable). + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn safe_to_produce(&self, _attestation: &AttestationData) -> bool { + //TODO: Implement slash protection + true + } + + /// Record that an attestation was produced so that slashable votes may not be made in the future. + /// + /// !!! UNSAFE !!! + /// + /// Important: this function is presently stubbed-out. It provides ZERO SAFETY. + fn store_produce(&mut self, _attestation: &AttestationData) { + // TODO: Implement slash protection + } +} diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index ba0795ec10..91c91e0b10 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -290,6 +290,7 @@ impl Service { // the return value is a future which returns ready. // built to be compatible with the tokio runtime. let _empty = cloned_manager.run_update(current_epoch.clone(), cloned_log.clone()); + dbg!("Duties Thread Ended"); }); } @@ -317,6 +318,7 @@ impl Service { signer, }; block_producer.handle_produce_block(log); + dbg!("Block produce Thread Ended"); }); } if work_type.attestation_duty.is_some() { @@ -338,6 +340,7 @@ impl Service { signer, }; attestation_producer.handle_produce_attestation(log); + dbg!("Attestation Thread Ended"); }); } } From 51ffbc07d25644ef7a8ef0cea64a5d316ec8ec18 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 19:48:45 +1100 Subject: [PATCH 159/191] Correct attestation error handling --- beacon_node/rpc/src/attestation.rs | 5 +++-- .../src/attestation_producer/mod.rs | 21 +++++++++++++++---- validator_client/src/block_producer/mod.rs | 4 ++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index f9423df615..aec948abbd 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -25,7 +25,7 @@ impl AttestationService for AttestationServiceInstance { req: ProduceAttestationDataRequest, sink: UnarySink, ) { - warn!( + trace!( &self.log, "Attempting to produce attestation at slot {}", req.get_slot() @@ -92,7 +92,7 @@ impl AttestationService for AttestationServiceInstance { req: PublishAttestationRequest, sink: UnarySink, ) { - warn!(self.log, "Publishing attestation"); + trace!(self.log, "Publishing attestation"); let mut resp = PublishAttestationResponse::new(); let ssz_serialized_attestation = req.get_attestation().get_ssz(); @@ -128,6 +128,7 @@ impl AttestationService for AttestationServiceInstance { self.log, "PublishAttestation"; "type" => "invalid_attestation", + "error" => format!("{:?}", e), ); resp.set_success(false); resp.set_msg(format!("InvalidAttestation: {:?}", e).as_bytes().to_vec()); diff --git a/validator_client/src/attestation_producer/mod.rs b/validator_client/src/attestation_producer/mod.rs index 7b2174b0cd..db9028c403 100644 --- a/validator_client/src/attestation_producer/mod.rs +++ b/validator_client/src/attestation_producer/mod.rs @@ -4,7 +4,7 @@ mod grpc; use std::sync::Arc; use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; //TODO: Move these higher up in the crate -use super::block_producer::{BeaconNodeError, ValidatorEvent}; +use super::block_producer::{BeaconNodeError, PublishOutcome, ValidatorEvent}; use crate::signer::Signer; use beacon_node_attestation::BeaconNodeAttestation; use slog::{error, info, warn}; @@ -58,6 +58,12 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(_slot)) => { error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to produce an attestation")) } + Ok(ValidatorEvent::PublishAttestationFailed) => { + error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to publish an attestation")) + } + Ok(ValidatorEvent::InvalidAttestation) => { + error!(log, "Attestation production error"; "Error" => format!("The signed attestation was invalid")) + } Ok(v) => { warn!(log, "Unknown result for attestation production"; "Error" => format!("{:?}",v)) } @@ -83,8 +89,15 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { if self.safe_to_produce(&attestation) { let domain = self.spec.get_domain(epoch, Domain::Attestation, &self.fork); if let Some(attestation) = self.sign_attestation(attestation, self.duty, domain) { - self.beacon_node.publish_attestation(attestation)?; - Ok(ValidatorEvent::AttestationProduced(self.duty.slot)) + match self.beacon_node.publish_attestation(attestation) { + Ok(PublishOutcome::InvalidAttestation(_string)) => { + Ok(ValidatorEvent::InvalidAttestation) + } + Ok(PublishOutcome::Valid) => { + Ok(ValidatorEvent::AttestationProduced(self.duty.slot)) + } + Err(_) | Ok(_) => Ok(ValidatorEvent::PublishAttestationFailed), + } } else { Ok(ValidatorEvent::SignerRejection(self.duty.slot)) } @@ -101,7 +114,7 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { /// done upstream. fn sign_attestation( &mut self, - mut attestation: AttestationData, + attestation: AttestationData, duties: AttestationDuty, domain: u64, ) -> Option { diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index 592c0b9192..dc7f6c3eed 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -31,6 +31,10 @@ pub enum ValidatorEvent { BeaconNodeUnableToProduceAttestation(Slot), /// The signer failed to sign the message. SignerRejection(Slot), + /// Publishing an attestation failed. + PublishAttestationFailed, + /// Beacon node rejected the attestation. + InvalidAttestation, } /// This struct contains the logic for requesting and signing beacon blocks for a validator. The From 9a6ecc46657305f4f07e1974e896918244b2a9e7 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sat, 30 Mar 2019 19:58:19 +1100 Subject: [PATCH 160/191] Add clippy suggestions --- .../src/attestation_producer/mod.rs | 13 ++++------ validator_client/src/block_producer/mod.rs | 25 ++++++++----------- validator_client/src/duties/grpc.rs | 5 ++-- validator_client/src/duties/mod.rs | 2 +- validator_client/src/service.rs | 9 +++---- 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/validator_client/src/attestation_producer/mod.rs b/validator_client/src/attestation_producer/mod.rs index db9028c403..0fbc7bcbaa 100644 --- a/validator_client/src/attestation_producer/mod.rs +++ b/validator_client/src/attestation_producer/mod.rs @@ -2,7 +2,7 @@ mod beacon_node_attestation; mod grpc; use std::sync::Arc; -use types::{BeaconBlock, ChainSpec, Domain, Fork, Slot}; +use types::{ChainSpec, Domain, Fork}; //TODO: Move these higher up in the crate use super::block_producer::{BeaconNodeError, PublishOutcome, ValidatorEvent}; use crate::signer::Signer; @@ -50,19 +50,16 @@ impl<'a, B: BeaconNodeAttestation, S: Signer> AttestationProducer<'a, B, S> { } Err(e) => error!(log, "Attestation production error"; "Error" => format!("{:?}", e)), Ok(ValidatorEvent::SignerRejection(_slot)) => { - error!(log, "Attestation production error"; "Error" => format!("Signer could not sign the attestation")) + error!(log, "Attestation production error"; "Error" => "Signer could not sign the attestation".to_string()) } Ok(ValidatorEvent::SlashableAttestationNotProduced(_slot)) => { - error!(log, "Attestation production error"; "Error" => format!("Rejected the attestation as it could have been slashed")) - } - Ok(ValidatorEvent::BeaconNodeUnableToProduceAttestation(_slot)) => { - error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to produce an attestation")) + error!(log, "Attestation production error"; "Error" => "Rejected the attestation as it could have been slashed".to_string()) } Ok(ValidatorEvent::PublishAttestationFailed) => { - error!(log, "Attestation production error"; "Error" => format!("Beacon node was unable to publish an attestation")) + error!(log, "Attestation production error"; "Error" => "Beacon node was unable to publish an attestation".to_string()) } Ok(ValidatorEvent::InvalidAttestation) => { - error!(log, "Attestation production error"; "Error" => format!("The signed attestation was invalid")) + error!(log, "Attestation production error"; "Error" => "The signed attestation was invalid".to_string()) } Ok(v) => { warn!(log, "Unknown result for attestation production"; "Error" => format!("{:?}",v)) diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index dc7f6c3eed..8b4f5abda0 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -27,8 +27,6 @@ pub enum ValidatorEvent { SlashableAttestationNotProduced(Slot), /// The Beacon Node was unable to produce a block at that slot. BeaconNodeUnableToProduceBlock(Slot), - /// The Beacon Node was unable to produce an attestation at that slot. - BeaconNodeUnableToProduceAttestation(Slot), /// The signer failed to sign the message. SignerRejection(Slot), /// Publishing an attestation failed. @@ -61,13 +59,13 @@ impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> { } Err(e) => error!(log, "Block production error"; "Error" => format!("{:?}", e)), Ok(ValidatorEvent::SignerRejection(_slot)) => { - error!(log, "Block production error"; "Error" => format!("Signer Could not sign the block")) + error!(log, "Block production error"; "Error" => "Signer Could not sign the block".to_string()) } Ok(ValidatorEvent::SlashableBlockNotProduced(_slot)) => { - error!(log, "Block production error"; "Error" => format!("Rejected the block as it could have been slashed")) + error!(log, "Block production error"; "Error" => "Rejected the block as it could have been slashed".to_string()) } Ok(ValidatorEvent::BeaconNodeUnableToProduceBlock(_slot)) => { - error!(log, "Block production error"; "Error" => format!("Beacon node was unable to produce a block")) + error!(log, "Block production error"; "Error" => "Beacon node was unable to produce a block".to_string()) } Ok(v) => { warn!(log, "Unknown result for block production"; "Error" => format!("{:?}",v)) @@ -88,16 +86,13 @@ impl<'a, B: BeaconNodeBlock, S: Signer> BlockProducer<'a, B, S> { pub fn produce_block(&mut self) -> Result { let epoch = self.slot.epoch(self.spec.slots_per_epoch); - let randao_reveal = { - let message = epoch.hash_tree_root(); - let randao_reveal = match self.signer.sign_message( - &message, - self.spec.get_domain(epoch, Domain::Randao, &self.fork), - ) { - None => return Ok(ValidatorEvent::SignerRejection(self.slot)), - Some(signature) => signature, - }; - randao_reveal + let message = epoch.hash_tree_root(); + let randao_reveal = match self.signer.sign_message( + &message, + self.spec.get_domain(epoch, Domain::Randao, &self.fork), + ) { + None => return Ok(ValidatorEvent::SignerRejection(self.slot)), + Some(signature) => signature, }; if let Some(block) = self diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index ab87b602e0..954ee194e7 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -1,11 +1,12 @@ use super::beacon_node_duties::{BeaconNodeDuties, BeaconNodeDutiesError}; use super::epoch_duties::{EpochDuties, EpochDuty}; -use grpcio::CallOption; +// to use if we manually specify a timeout +//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 std::time::Duration; use types::{AttestationDuty, Epoch, PublicKey, Slot}; impl BeaconNodeDuties for ValidatorServiceClient { diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index d52cc32544..7db4672e30 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -69,7 +69,7 @@ impl DutiesManager { // 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)); + 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 91c91e0b10..ce9e352665 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -157,7 +157,7 @@ impl Service { let current_slot = slot_clock .present_slot() - .map_err(|e| ErrorKind::SlotClockError(e))? + .map_err(ErrorKind::SlotClockError)? .expect("Genesis must be in the future"); /* Generate the duties manager */ @@ -289,8 +289,7 @@ impl Service { std::thread::spawn(move || { // the return value is a future which returns ready. // built to be compatible with the tokio runtime. - let _empty = cloned_manager.run_update(current_epoch.clone(), cloned_log.clone()); - dbg!("Duties Thread Ended"); + let _empty = cloned_manager.run_update(current_epoch, cloned_log.clone()); }); } @@ -303,7 +302,7 @@ impl Service { // spawns a thread to produce a beacon block let signers = self.duties_manager.signers.clone(); // this is an arc let fork = self.fork.clone(); - let slot = self.current_slot.clone(); + let slot = self.current_slot; let spec = self.spec.clone(); let beacon_node = self.beacon_block_client.clone(); let log = self.log.clone(); @@ -318,7 +317,6 @@ impl Service { signer, }; block_producer.handle_produce_block(log); - dbg!("Block produce Thread Ended"); }); } if work_type.attestation_duty.is_some() { @@ -340,7 +338,6 @@ impl Service { signer, }; attestation_producer.handle_produce_attestation(log); - dbg!("Attestation Thread Ended"); }); } } From 4fb95d06d1062e1779f24d4cda23a89c4f9a6f9f Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 31 Mar 2019 00:08:55 +1100 Subject: [PATCH 161/191] Correct cache race condition --- beacon_node/rpc/src/beacon_chain.rs | 8 +++++++- beacon_node/rpc/src/validator.rs | 18 ++++++++++++++---- .../test_utils/testing_beacon_state_builder.rs | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs index 7de48efa1c..ddc91b73cc 100644 --- a/beacon_node/rpc/src/beacon_chain.rs +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -2,7 +2,7 @@ use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::{ db::ClientDB, fork_choice::ForkChoice, - parking_lot::RwLockReadGuard, + parking_lot::{RwLockReadGuard, RwLockWriteGuard}, slot_clock::SlotClock, types::{BeaconState, ChainSpec, Signature}, AttestationValidationError, BlockProductionError, @@ -16,6 +16,8 @@ pub trait BeaconChain: Send + Sync { fn get_state(&self) -> RwLockReadGuard; + fn get_mut_state(&self) -> RwLockWriteGuard; + fn process_block(&self, block: BeaconBlock) -> Result; @@ -46,6 +48,10 @@ where self.state.read() } + fn get_mut_state(&self) -> RwLockWriteGuard { + self.state.write() + } + fn process_block( &self, block: BeaconBlock, diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 936c95f52f..a60cb43942 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, info, warn, Logger}; +use slog::{trace, warn}; use ssz::Decodable; use std::sync::Arc; use types::{Epoch, RelativeEpoch}; @@ -12,7 +12,7 @@ use types::{Epoch, RelativeEpoch}; #[derive(Clone)] pub struct ValidatorServiceInstance { pub chain: Arc, - pub log: Logger, + pub log: slog::Logger, } //TODO: Refactor Errors @@ -27,13 +27,23 @@ impl ValidatorService for ValidatorServiceInstance { sink: UnarySink, ) { let validators = req.get_validators(); - debug!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch()); + trace!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch()); + + let spec = self.chain.get_spec(); + // update the caches if necessary + { + let mut mut_state = self.chain.get_mut_state(); + + let _ = mut_state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec); + + let _ = mut_state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec); + let _ = mut_state.update_pubkey_cache(); + } 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(); let relative_epoch = 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 8d0aa6af62..c041889208 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 = 1553932445; // arbitrary + let genesis_time = 1553950542; // arbitrary let mut state = BeaconState::genesis( genesis_time, From ee693fb3e3942ad175cc5f441903795afdedcf16 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 31 Mar 2019 00:34:35 +1100 Subject: [PATCH 162/191] Add committe_len to gRPC parameters --- beacon_node/rpc/src/attestation.rs | 2 +- beacon_node/rpc/src/validator.rs | 1 + protos/src/services.proto | 1 + validator_client/src/duties/grpc.rs | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index aec948abbd..c6e5c68eeb 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -6,7 +6,7 @@ use protos::services::{ ProduceAttestationDataResponse, PublishAttestationRequest, PublishAttestationResponse, }; use protos::services_grpc::AttestationService; -use slog::{error, info, trace, warn, Logger}; +use slog::{error, info, trace, warn}; use ssz::{ssz_encode, Decodable}; use std::sync::Arc; use types::Attestation; diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index a60cb43942..3dbbbdd177 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -167,6 +167,7 @@ impl ValidatorService for ValidatorServiceInstance { 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); + duty.set_committee_len(attestation_duties.committee_len as u64); active_validator.set_duty(duty); resp_validators.push(active_validator); diff --git a/protos/src/services.proto b/protos/src/services.proto index c5bd8a91d3..ecc75ee264 100644 --- a/protos/src/services.proto +++ b/protos/src/services.proto @@ -125,6 +125,7 @@ message ValidatorDuty { uint64 attestation_slot = 3; uint64 attestation_shard = 4; uint64 committee_index = 5; + uint64 committee_len = 6; } /* diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index 954ee194e7..58fb5c992d 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -53,7 +53,7 @@ impl BeaconNodeDuties for ValidatorServiceClient { slot: Slot::from(active_duty.get_attestation_slot()), shard: active_duty.get_attestation_shard(), committee_index: active_duty.get_committee_index() as usize, - committee_len: 10, + committee_len: active_duty.get_committee_len() as usize, }; let epoch_duty = EpochDuty { From d2b5cf5a32eeee3ff69dbcf436f2dfba23600891 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 09:44:58 +1100 Subject: [PATCH 163/191] Improve queueing in SimpleSync --- beacon_node/eth2-libp2p/src/rpc/methods.rs | 13 ++++ beacon_node/network/src/sync/import_queue.rs | 54 ++++++++++--- beacon_node/network/src/sync/simple_sync.rs | 78 +++++++++++++------ .../testing_beacon_state_builder.rs | 2 +- 4 files changed, 115 insertions(+), 32 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index ad3233be71..f9adb93c17 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -179,6 +179,19 @@ pub struct BeaconBlockRootsResponse { pub roots: Vec, } +impl BeaconBlockRootsResponse { + /// Returns `true` if each `self.roots.slot[i]` is higher than the preceeding `i`. + pub fn slots_are_ascending(&self) -> bool { + for i in 1..self.roots.len() { + if self.roots[i - 1].slot >= self.roots[i].slot { + return false; + } + } + + true + } +} + /// Contains a block root and associated slot. #[derive(Encode, Decode, Clone, Debug, PartialEq)] pub struct BlockRootSlot { diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs index 17cbd2f12e..8680993aa4 100644 --- a/beacon_node/network/src/sync/import_queue.rs +++ b/beacon_node/network/src/sync/import_queue.rs @@ -5,7 +5,7 @@ use slog::{debug, error}; use ssz::TreeHash; use std::sync::Arc; use std::time::{Duration, Instant}; -use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Hash256}; +use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Hash256, Slot}; /// Provides a queue for fully and partially built `BeaconBlock`s. /// @@ -120,6 +120,38 @@ impl ImportQueue { .position(|brs| self.is_new_block(&brs.block_root)) } + /// Adds the `block_roots` to the partials queue. + /// + /// If a `block_root` is not in the queue and has not been processed by the chain it is added + /// to the queue and it's block root is included in the output. + pub fn enqueue_block_roots( + &mut self, + block_roots: &[BlockRootSlot], + sender: PeerId, + ) -> Vec { + let new_roots: Vec = block_roots + .iter() + // Ignore any roots already processed by the chain. + .filter(|brs| self.is_new_block(&brs.block_root)) + // Ignore any roots already stored in the queue. + .filter(|brs| !self.partials.iter().any(|p| p.block_root == brs.block_root)) + .cloned() + .collect(); + + new_roots.iter().for_each(|brs| { + self.partials.push(PartialBeaconBlock { + slot: brs.slot, + block_root: brs.block_root, + sender: sender.clone(), + header: None, + body: None, + inserted: Instant::now(), + }) + }); + + new_roots + } + /// Adds the `headers` to the `partials` queue. Returns a list of `Hash256` block roots for /// which we should use to request `BeaconBlockBodies`. /// @@ -174,8 +206,9 @@ impl ImportQueue { self.partials[i].inserted = Instant::now(); } else { self.partials.push(PartialBeaconBlock { + slot: header.slot, block_root, - header, + header: Some(header), body: None, inserted: Instant::now(), sender, @@ -192,12 +225,14 @@ impl ImportQueue { let body_root = Hash256::from_slice(&body.hash_tree_root()[..]); self.partials.iter_mut().for_each(|mut p| { - if body_root == p.header.block_body_root { - p.inserted = Instant::now(); + if let Some(header) = &mut p.header { + if body_root == header.block_body_root { + p.inserted = Instant::now(); - if p.body.is_none() { - p.body = Some(body.clone()); - p.sender = sender.clone(); + if p.body.is_none() { + p.body = Some(body.clone()); + p.sender = sender.clone(); + } } } }); @@ -208,9 +243,10 @@ impl ImportQueue { /// `BeaconBlock`. #[derive(Clone, Debug)] pub struct PartialBeaconBlock { + pub slot: Slot, /// `BeaconBlock` root. pub block_root: Hash256, - pub header: BeaconBlockHeader, + pub header: Option, pub body: Option, /// The instant at which this record was created or last meaningfully modified. Used to /// determine if an entry is stale and should be removed. @@ -225,7 +261,7 @@ impl PartialBeaconBlock { pub fn complete(self) -> Option<(Hash256, BeaconBlock, PeerId)> { Some(( self.block_root, - self.header.into_block(self.body?), + self.header?.into_block(self.body?), self.sender, )) } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 1afba018d9..21b2612689 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -358,31 +358,48 @@ impl SimpleSync { if res.roots.is_empty() { warn!( self.log, - "Peer returned empty block roots response. PeerId: {:?}", peer_id + "Peer returned empty block roots response"; + "peer_id" => format!("{:?}", peer_id) ); return; } - let new_root_index = self.import_queue.first_new_root(&res.roots); - - // If a new block root is found, request it and all the headers following it. - // - // We make an assumption here that if we don't know a block then we don't know of all - // it's parents. This might not be the case if syncing becomes more sophisticated. - if let Some(i) = new_root_index { - let new = &res.roots[i]; - - self.request_block_headers( - peer_id, - BeaconBlockHeadersRequest { - start_root: new.block_root, - start_slot: new.slot, - max_headers: (res.roots.len() - i) as u64, - skip_slots: 0, - }, - network, - ) + // The wire protocol specifies that slots must be in ascending order. + if !res.slots_are_ascending() { + warn!( + self.log, + "Peer returned block roots response with bad slot ordering"; + "peer_id" => format!("{:?}", peer_id) + ); + return; } + + let new_roots = self.import_queue.enqueue_block_roots(&res.roots, peer_id.clone()); + + // No new roots means nothing to do. + // + // This check protects against future panics. + if new_roots.is_empty() { + return; + } + + // Determine the first (earliest) and last (latest) `BlockRootSlot` items. + // + // This logic relies upon slots to be in ascending order, which is enforced earlier. + let first = new_roots.first().expect("Non-empty list must have first"); + let last = new_roots.last().expect("Non-empty list must have last"); + + // Request all headers between the earliest and latest new `BlockRootSlot` items. + self.request_block_headers( + peer_id, + BeaconBlockHeadersRequest { + start_root: first.block_root, + start_slot: first.slot, + max_headers: (last.slot - first.slot + 1).as_u64(), + skip_slots: 0, + }, + network, + ) } /// Handle a `BeaconBlockHeaders` request from the peer. @@ -528,8 +545,17 @@ impl SimpleSync { "NewGossipBlock"; "peer" => format!("{:?}", peer_id), ); - // TODO: filter out messages that a prior to the finalized slot. - // + + // Ignore any block from a finalized slot. + if self.slot_is_finalized(msg.slot) { + warn!( + self.log, "NewGossipBlock"; + "msg" => "new block slot is finalized.", + "slot" => msg.slot, + ); + return; + } + // TODO: if the block is a few more slots ahead, try to get all block roots from then until // now. // @@ -675,6 +701,14 @@ impl SimpleSync { network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockBodies(req)); } + fn slot_is_finalized(&self, slot: Slot) -> bool { + slot <= self + .chain + .hello_message() + .latest_finalized_epoch + .start_slot(self.chain.get_spec().slots_per_epoch) + } + /// Generates our current state in the form of a HELLO RPC message. pub fn generate_hello(&self) -> HelloMessage { self.chain.hello_message() 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 b38e8b5273..f437240dc6 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 = 1553753928; // arbitrary + let genesis_time = 1553977336; // arbitrary let mut state = BeaconState::genesis( genesis_time, From 5cc2fdd3d4bb932196da410f852082442c5e43d0 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 10:15:01 +1100 Subject: [PATCH 164/191] Change `beacon_node` and `network` slog features Allows printing debug messages in --release --- beacon_node/Cargo.toml | 2 +- beacon_node/network/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index a090c1cc54..37d96a4974 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -9,7 +9,7 @@ types = { path = "../eth2/types" } client = { path = "client" } version = { path = "version" } clap = "2.32.0" -slog = "^2.2.3" +slog = { version = "^2.2.3" , features = ["max_level_trace", "release_max_level_debug"] } slog-term = "^2.4.0" slog-async = "^2.3.0" ctrlc = { version = "3.1.1", features = ["termination"] } diff --git a/beacon_node/network/Cargo.toml b/beacon_node/network/Cargo.toml index c6411a0205..cd2c2269a6 100644 --- a/beacon_node/network/Cargo.toml +++ b/beacon_node/network/Cargo.toml @@ -13,7 +13,7 @@ beacon_chain = { path = "../beacon_chain" } eth2-libp2p = { path = "../eth2-libp2p" } version = { path = "../version" } types = { path = "../../eth2/types" } -slog = "2.4.1" +slog = { version = "^2.2.3" , features = ["max_level_trace", "release_max_level_debug"] } ssz = { path = "../../eth2/utils/ssz" } futures = "0.1.25" error-chain = "0.12.0" From c99a742aae515564a575fc65279dca18cb92d5fe Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 10:15:42 +1100 Subject: [PATCH 165/191] Fix bug in SimpleSync queue. It was not completing partials with bodies. --- beacon_node/network/src/sync/import_queue.rs | 16 +++++++++------- beacon_node/network/src/sync/simple_sync.rs | 4 +++- beacon_node/network/tests/tests.rs | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs index 8680993aa4..b9280440bf 100644 --- a/beacon_node/network/src/sync/import_queue.rs +++ b/beacon_node/network/src/sync/import_queue.rs @@ -113,13 +113,6 @@ impl ImportQueue { }) } - /// Returns the index of the first new root in the list of block roots. - pub fn first_new_root(&mut self, roots: &[BlockRootSlot]) -> Option { - roots - .iter() - .position(|brs| self.is_new_block(&brs.block_root)) - } - /// Adds the `block_roots` to the partials queue. /// /// If a `block_root` is not in the queue and has not been processed by the chain it is added @@ -203,8 +196,17 @@ impl ImportQueue { .iter() .position(|p| p.block_root == block_root) { + // Case 1: there already exists a partial with a matching block root. + // + // The `inserted` time is set to now and the header is replaced, regardless of whether + // it existed or not. + self.partials[i].header = Some(header); self.partials[i].inserted = Instant::now(); } else { + // Case 2: there was no partial with a matching block root. + // + // A new partial is added. This case permits adding a header without already known the + // root -- this is not possible in the wire protocol however we support it anyway. self.partials.push(PartialBeaconBlock { slot: header.slot, block_root, diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 21b2612689..39fe772b45 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -374,7 +374,9 @@ impl SimpleSync { return; } - let new_roots = self.import_queue.enqueue_block_roots(&res.roots, peer_id.clone()); + let new_roots = self + .import_queue + .enqueue_block_roots(&res.roots, peer_id.clone()); // No new roots means nothing to do. // diff --git a/beacon_node/network/tests/tests.rs b/beacon_node/network/tests/tests.rs index 9cead1b557..47d5482d3e 100644 --- a/beacon_node/network/tests/tests.rs +++ b/beacon_node/network/tests/tests.rs @@ -543,7 +543,7 @@ fn sync_two_nodes() { // A provides block bodies to B. node_a.tee_block_body_response(&node_b); - std::thread::sleep(Duration::from_secs(10)); + std::thread::sleep(Duration::from_secs(20)); node_b.harness.run_fork_choice(); From e0680f97711b64b97e477e0d948ca968d59d6ab7 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 31 Mar 2019 11:04:50 +1100 Subject: [PATCH 166/191] Correct compile error --- beacon_node/beacon_chain/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 7605ced156..d8d85a8a6c 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -4,9 +4,7 @@ mod errors; pub mod initialise; pub mod test_utils; -pub use self::beacon_chain::{ - AttestationValidationError, BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock, -}; +pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; pub use self::errors::{BeaconChainError, BlockProductionError}; pub use db; From 2c1fa86cd317b970aed03fbebe23329051f54062 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 12:28:35 +1100 Subject: [PATCH 167/191] Swap to gossiping whole block. Processing for gossiped blocks is broken in `SimpleSync`, will be fixed next. --- beacon_node/eth2-libp2p/src/behaviour.rs | 13 +++----- beacon_node/network/src/sync/simple_sync.rs | 35 ++++++++++++++++----- beacon_node/rpc/src/beacon_block.rs | 33 ++++++++++--------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 2865971838..fdf12f0bff 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -1,4 +1,3 @@ -use crate::rpc::methods::BlockRootSlot; use crate::rpc::{RPCEvent, RPCMessage, Rpc}; use crate::NetworkConfig; use futures::prelude::*; @@ -15,8 +14,7 @@ use libp2p::{ }; use slog::{debug, o, warn}; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; -use ssz_derive::{Decode, Encode}; -use types::Attestation; +use types::{Attestation, BeaconBlock}; use types::{Topic, TopicHash}; /// Builds the network behaviour for the libp2p Swarm. @@ -198,7 +196,7 @@ pub enum BehaviourEvent { #[derive(Debug, Clone, PartialEq)] pub enum PubsubMessage { /// Gossipsub message providing notification of a new block. - Block(BlockRootSlot), + Block(BeaconBlock), /// Gossipsub message providing notification of a new attestation. Attestation(Attestation), } @@ -224,7 +222,7 @@ impl Decodable for PubsubMessage { let (id, index) = u32::ssz_decode(bytes, index)?; match id { 0 => { - let (block, index) = BlockRootSlot::ssz_decode(bytes, index)?; + let (block, index) = BeaconBlock::ssz_decode(bytes, index)?; Ok((PubsubMessage::Block(block), index)) } 1 => { @@ -243,10 +241,7 @@ mod test { #[test] fn ssz_encoding() { - let original = PubsubMessage::Block(BlockRootSlot { - block_root: Hash256::from_slice(&[42; 32]), - slot: Slot::new(4), - }); + let original = PubsubMessage::Block(BeaconBlock::empty(&ChainSpec::foundation())); let encoded = ssz_encode(&original); diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 39fe772b45..e8a3da656d 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -8,7 +8,7 @@ use slog::{debug, error, info, o, warn}; use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use types::{Attestation, Epoch, Hash256, Slot}; +use types::{Attestation, BeaconBlock, 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; @@ -539,7 +539,7 @@ impl SimpleSync { pub fn on_block_gossip( &mut self, peer_id: PeerId, - msg: BlockRootSlot, + block: BeaconBlock, network: &mut NetworkContext, ) { info!( @@ -548,6 +548,7 @@ impl SimpleSync { "peer" => format!("{:?}", peer_id), ); + /* // Ignore any block from a finalized slot. if self.slot_is_finalized(msg.slot) { warn!( @@ -558,11 +559,13 @@ impl SimpleSync { return; } - // TODO: if the block is a few more slots ahead, try to get all block roots from then until - // now. - // - // Note: only requests the new block -- will fail if we don't have its parents. - if self.import_queue.is_new_block(&msg.block_root) { + // Ignore any block that the chain already knows about. + if self.chain_has_seen_block(&msg.block_root) { + return; + } + + // k + if msg.slot == self.chain.hello_message().best_slot + 1 { self.request_block_headers( peer_id, BeaconBlockHeadersRequest { @@ -574,6 +577,24 @@ impl SimpleSync { network, ) } + + // TODO: if the block is a few more slots ahead, try to get all block roots from then until + // now. + // + // Note: only requests the new block -- will fail if we don't have its parents. + if !self.chain_has_seen_block(&msg.block_root) { + self.request_block_headers( + peer_id, + BeaconBlockHeadersRequest { + start_root: msg.block_root, + start_slot: msg.slot, + max_headers: 1, + skip_slots: 0, + }, + network, + ) + } + */ } /// Process a gossip message declaring a new attestation. diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index f6b426c18f..6978e0f0e8 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -1,6 +1,5 @@ use crate::beacon_chain::BeaconChain; use crossbeam_channel; -use eth2_libp2p::rpc::methods::BlockRootSlot; use eth2_libp2p::PubsubMessage; use futures::Future; use grpcio::{RpcContext, UnarySink}; @@ -11,10 +10,10 @@ use protos::services::{ }; use protos::services_grpc::BeaconBlockService; use slog::Logger; -use slog::{debug, error, info, warn}; -use ssz::{Decodable, TreeHash}; +use slog::{error, info, warn}; +use ssz::Decodable; use std::sync::Arc; -use types::{BeaconBlock, Hash256, Slot}; +use types::BeaconBlock; #[derive(Clone)] pub struct BeaconBlockServiceInstance { @@ -59,8 +58,6 @@ impl BeaconBlockService for BeaconBlockServiceInstance { match BeaconBlock::ssz_decode(ssz_serialized_block, 0) { Ok((block, _i)) => { - let block_root = Hash256::from_slice(&block.hash_tree_root()[..]); - match self.chain.process_block(block.clone()) { Ok(outcome) => { if outcome.sucessfully_processed() { @@ -76,16 +73,22 @@ impl BeaconBlockService for BeaconBlockServiceInstance { // 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 message = PubsubMessage::Block(block); - println!("Sending beacon block to gossipsub"); - self.network_chan.send(NetworkMessage::Publish { - topics: vec![topic], - message, - }); + // Publish the block to the p2p network via gossipsub. + self.network_chan + .send(NetworkMessage::Publish { + topics: vec![topic], + message, + }) + .unwrap_or_else(|e| { + error!( + self.log, + "PublishBeaconBlock"; + "type" => "failed to publish to gossipsub", + "error" => format!("{:?}", e) + ); + }); resp.set_success(true); } else if outcome.is_invalid() { From 4e71ed69721dd7d665169a043389e7c3062679b9 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 12:54:42 +1100 Subject: [PATCH 168/191] Fix `produce_attestation` bug. It was referencing the wrong crosslink. --- beacon_node/beacon_chain/src/beacon_chain.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 6ca0bff733..efa83bdd39 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -492,10 +492,7 @@ where beacon_block_root: self.head().beacon_block_root, target_root, crosslink_data_root: Hash256::zero(), - previous_crosslink: Crosslink { - epoch: self.state.read().slot.epoch(self.spec.slots_per_epoch), - crosslink_data_root: Hash256::zero(), - }, + previous_crosslink: state.latest_crosslinks[shard as usize].clone(), source_epoch: state.current_justified_epoch, source_root: state.current_justified_root, }) From b26f1f8e1cfd357f2991d1e8230a52305f491a44 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 13:40:12 +1100 Subject: [PATCH 169/191] Add `build_all_caches` method to `BeaconState` Also adds a few more cache builds in BeaconChain. --- beacon_node/beacon_chain/src/beacon_chain.rs | 23 ++++++++++++-------- eth2/types/src/beacon_state.rs | 11 ++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index efa83bdd39..046f37a81a 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -130,10 +130,7 @@ where state_root, )); - genesis_state.build_epoch_cache(RelativeEpoch::Previous, &spec)?; - genesis_state.build_epoch_cache(RelativeEpoch::Current, &spec)?; - genesis_state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, &spec)?; - genesis_state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, &spec)?; + genesis_state.build_all_caches(&spec)?; Ok(Self { block_store, @@ -318,6 +315,8 @@ where per_slot_processing(&mut state, &latest_block_header, &self.spec)?; } + state.build_all_caches(&self.spec)?; + *self.state.write() = state; Ok(()) @@ -342,11 +341,17 @@ 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()?; + + state.build_all_caches(&self.spec)?; + + Ok(()) + } + + /// Build all of the caches on the current state. + /// + /// Ideally this shouldn't be required, however we leave it here for testing. + pub fn ensure_state_caches_are_built(&self) -> Result<(), Error> { + self.state.write().build_all_caches(&self.spec)?; Ok(()) } diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 1e52781243..774e8eb761 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -661,6 +661,17 @@ impl BeaconState { }) } + /// Build all the caches, if they need to be built. + pub fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), Error> { + self.build_epoch_cache(RelativeEpoch::Previous, spec)?; + self.build_epoch_cache(RelativeEpoch::Current, spec)?; + self.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec)?; + self.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec)?; + self.update_pubkey_cache()?; + + Ok(()) + } + /// Build an epoch cache, unless it is has already been built. pub fn build_epoch_cache( &mut self, From 33473892f27df37fdec79fb4a7d76c8fe241083c Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 31 Mar 2019 14:26:58 +1100 Subject: [PATCH 170/191] Validator client fixes. Hack fix for genesis start time --- beacon_node/rpc/src/attestation.rs | 24 ++++++++++++++++--- beacon_node/rpc/src/validator.rs | 13 +--------- .../validate_attestation.rs | 3 +++ .../testing_beacon_state_builder.rs | 10 +++++++- validator_client/src/service.rs | 8 ++++++- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index c6e5c68eeb..494b240678 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -37,14 +37,29 @@ impl AttestationService for AttestationServiceInstance { let state = self.chain.get_state(); // Start by performing some checks - // Check that the the AttestionData is for the current slot (otherwise it will not be valid) - if slot_requested != state.slot.as_u64() { + // Check that the AttestionData is for the current slot (otherwise it will not be valid) + if slot_requested > state.slot.as_u64() { let log_clone = self.log.clone(); let f = sink .fail(RpcStatus::new( RpcStatusCode::OutOfRange, Some(format!( - "AttestationData request for a slot that is not the current slot." + "AttestationData request for a slot that is in the future." + )), + )) + .map_err(move |e| { + error!(log_clone, "Failed to reply with failure {:?}: {:?}", req, e) + }); + return ctx.spawn(f); + } + // currently cannot handle past slots. TODO: Handle this case + else if slot_requested < state.slot.as_u64() { + let log_clone = self.log.clone(); + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some(format!( + "AttestationData request for a slot that is in the past." )), )) .map_err(move |e| { @@ -71,6 +86,9 @@ impl AttestationService for AttestationServiceInstance { } }; + dbg!("Produced attestation"); + dbg!(attestation_data.clone()); + let mut attestation_data_proto = AttestationDataProto::new(); attestation_data_proto.set_ssz(ssz_encode(&attestation_data)); diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 3dbbbdd177..0a9d7015c9 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -30,22 +30,11 @@ impl ValidatorService for ValidatorServiceInstance { trace!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch()); let spec = self.chain.get_spec(); - // update the caches if necessary - { - let mut mut_state = self.chain.get_mut_state(); - - let _ = mut_state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec); - - let _ = mut_state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec); - let _ = mut_state.update_pubkey_cache(); - } - + let state = self.chain.get_state(); let epoch = Epoch::from(req.get_epoch()); let mut resp = GetDutiesResponse::new(); let resp_validators = resp.mut_active_validators(); - let state = self.chain.get_state(); - let relative_epoch = match RelativeEpoch::from_epoch(state.slot.epoch(spec.slots_per_epoch), epoch) { Ok(v) => v, diff --git a/eth2/state_processing/src/per_block_processing/validate_attestation.rs b/eth2/state_processing/src/per_block_processing/validate_attestation.rs index 3b89bec99c..76b4157968 100644 --- a/eth2/state_processing/src/per_block_processing/validate_attestation.rs +++ b/eth2/state_processing/src/per_block_processing/validate_attestation.rs @@ -99,6 +99,9 @@ fn validate_attestation_parametric( crosslink_data_root: attestation.data.crosslink_data_root, epoch: attestation.data.slot.epoch(spec.slots_per_epoch), }; + dbg!(attestation.clone()); + dbg!(state.latest_crosslinks[attestation.data.shard as usize].clone()); + dbg!(potential_crosslink.clone()); verify!( (attestation.data.previous_crosslink == state.latest_crosslinks[attestation.data.shard as usize]) 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 f437240dc6..518f55e3c1 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -6,6 +6,8 @@ use dirs; use log::debug; use rayon::prelude::*; use std::path::{Path, PathBuf}; +//TODO: testing only +use std::time::{Duration, SystemTime}; pub const KEYPAIRS_FILE: &str = "keypairs.raw_keypairs"; @@ -120,7 +122,13 @@ impl TestingBeaconStateBuilder { }) .collect(); - let genesis_time = 1553977336; // arbitrary + //TODO: Testing only + let now = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() + - 30; + let genesis_time = now; // arbitrary let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index ce9e352665..38883e68fa 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -29,11 +29,15 @@ use std::sync::RwLock; use std::time::{Duration, Instant, SystemTime}; use tokio::prelude::*; use tokio::runtime::Builder; -use tokio::timer::Interval; +use tokio::timer::{Delay, Interval}; use tokio_timer::clock::Clock; use types::test_utils::generate_deterministic_keypairs; use types::{ChainSpec, Epoch, Fork, Slot}; +/// A fixed amount of time after a slot to perform operations. This gives the node time to complete +/// per-slot processes. +const TIME_DELAY_FROM_SLOT: Duration = Duration::from_millis(200); + /// The validator service. This is the main thread that executes and maintains validator /// duties. //TODO: Generalize the BeaconNode types to use testing @@ -230,6 +234,8 @@ impl Service { runtime.block_on( interval .for_each(move |_| { + // wait for node to process + std::thread::sleep(TIME_DELAY_FROM_SLOT); // if a non-fatal error occurs, proceed to the next slot. let _ignore_error = service.per_slot_execution(); // completed a slot process From bd860eb3e155177ca47714e6f9356d42c39eb960 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 15:30:09 +1100 Subject: [PATCH 171/191] Fixes bug in epoch processing. - Was using the wrong slot to determine relative epoch. - Added a non-related test I build during the search --- .../src/per_epoch_processing/validator_statuses.rs | 2 +- eth2/types/src/slot_epoch.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs index 50f3ec3727..02149cc5a1 100644 --- a/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs +++ b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs @@ -227,7 +227,7 @@ impl ValidatorStatuses { status.is_previous_epoch_attester = true; // The inclusion slot and distance are only required for previous epoch attesters. - let relative_epoch = RelativeEpoch::from_slot(state.slot, a.data.slot, spec)?; + let relative_epoch = RelativeEpoch::from_slot(state.slot, a.inclusion_slot, spec)?; status.inclusion_info = Some(InclusionInfo { slot: a.inclusion_slot, distance: inclusion_distance(a), diff --git a/eth2/types/src/slot_epoch.rs b/eth2/types/src/slot_epoch.rs index c40b6badfc..a8c32ef859 100644 --- a/eth2/types/src/slot_epoch.rs +++ b/eth2/types/src/slot_epoch.rs @@ -113,6 +113,16 @@ mod epoch_tests { all_tests!(Epoch); + #[test] + fn epoch_start_end() { + let slots_per_epoch = 8; + + let epoch = Epoch::new(0); + + assert_eq!(epoch.start_slot(slots_per_epoch), Slot::new(0)); + assert_eq!(epoch.end_slot(slots_per_epoch), Slot::new(7)); + } + #[test] fn slot_iter() { let slots_per_epoch = 8; From c6fc4f0769ae423f38cc3a46d162d79bd4425908 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 15:35:27 +1100 Subject: [PATCH 172/191] Fix bug in attestation production --- beacon_node/beacon_chain/src/beacon_chain.rs | 37 ++++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 046f37a81a..b272520c59 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -482,14 +482,37 @@ where trace!("BeaconChain::produce_attestation: shard: {}", shard); let state = self.state.read(); - let target_root = *self.state.read().get_block_root( - self.state + let current_epoch_start_slot = self + .state + .read() + .slot + .epoch(self.spec.slots_per_epoch) + .start_slot(self.spec.slots_per_epoch); + + let target_root = if state.slot == current_epoch_start_slot { + // If we're on the first slot of the state's epoch. + if self.head().beacon_block.slot == state.slot { + // If the current head block is from the current slot, use its block root. + self.head().beacon_block_root + } else { + // If the current head block is not from this slot, use the slot from the previous + // epoch. + let root = *self.state.read().get_block_root( + current_epoch_start_slot - self.spec.slots_per_epoch, + &self.spec, + )?; + + root + } + } else { + // If we're not on the first slot of the epoch. + let root = *self + .state .read() - .slot - .epoch(self.spec.slots_per_epoch) - .start_slot(self.spec.slots_per_epoch), - &self.spec, - )?; + .get_block_root(current_epoch_start_slot, &self.spec)?; + + root + }; Ok(AttestationData { slot: self.state.read().slot, From c85da612f64c16640cbcfab2c8fbc3a9d6de12f1 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 31 Mar 2019 15:35:54 +1100 Subject: [PATCH 173/191] Remove debugging statements --- beacon_node/beacon_chain/src/beacon_chain.rs | 2 +- beacon_node/rpc/src/attestation.rs | 3 --- validator_client/src/attestation_producer/grpc.rs | 2 -- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 046f37a81a..f600fb1ac4 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -290,7 +290,7 @@ where /// fork-choice rule). /// /// It is important to note that the `beacon_state` returned may not match the present slot. It - /// is the state as it was when the head block was recieved, which could be some slots prior to + /// is the state as it was when the head block was received, which could be some slots prior to /// now. pub fn head(&self) -> RwLockReadGuard { self.canonical_head.read() diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index 494b240678..abef49df1d 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -86,9 +86,6 @@ impl AttestationService for AttestationServiceInstance { } }; - dbg!("Produced attestation"); - dbg!(attestation_data.clone()); - let mut attestation_data_proto = AttestationDataProto::new(); attestation_data_proto.set_ssz(ssz_encode(&attestation_data)); diff --git a/validator_client/src/attestation_producer/grpc.rs b/validator_client/src/attestation_producer/grpc.rs index 49c577e242..900a92f321 100644 --- a/validator_client/src/attestation_producer/grpc.rs +++ b/validator_client/src/attestation_producer/grpc.rs @@ -22,8 +22,6 @@ impl BeaconNodeAttestation for AttestationServiceClient { .produce_attestation_data(&req) .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; - dbg!("Produced Attestation Data"); - let (attestation_data, _index) = AttestationData::ssz_decode(reply.get_attestation_data().get_ssz(), 0) .map_err(|_| BeaconNodeError::DecodeFailure)?; From e0b5e74e7cb269b75db4ec2469bbf8fd29bfa45c Mon Sep 17 00:00:00 2001 From: Age Manning Date: Sun, 31 Mar 2019 15:45:52 +1100 Subject: [PATCH 174/191] Removes further unneccessary debug output --- beacon_node/client/src/notifier.rs | 4 ++-- beacon_node/network/src/service.rs | 2 +- .../src/per_block_processing/validate_attestation.rs | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/beacon_node/client/src/notifier.rs b/beacon_node/client/src/notifier.rs index 91a9f3a261..1a5ecbb53b 100644 --- a/beacon_node/client/src/notifier.rs +++ b/beacon_node/client/src/notifier.rs @@ -22,13 +22,13 @@ pub fn run(client: &Client, executor: TaskExecutor, exit: Exi // build heartbeat logic here let heartbeat = move |_| { - debug!(log, "Temp heartbeat output"); + //debug!(log, "Temp heartbeat output"); //TODO: Remove this logic. Testing only let mut count = counter.lock().unwrap(); *count += 1; if *count % 5 == 0 { - debug!(log, "Sending Message"); + // debug!(log, "Sending Message"); network.send_message(); } diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index b2d2b5a246..aee7eb4660 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -161,7 +161,7 @@ fn network_service( libp2p_service.swarm.send_rpc(peer_id, rpc_event); } OutgoingMessage::NotifierTest => { - debug!(log, "Received message from notifier"); + // debug!(log, "Received message from notifier"); } }; } diff --git a/eth2/state_processing/src/per_block_processing/validate_attestation.rs b/eth2/state_processing/src/per_block_processing/validate_attestation.rs index 76b4157968..3b89bec99c 100644 --- a/eth2/state_processing/src/per_block_processing/validate_attestation.rs +++ b/eth2/state_processing/src/per_block_processing/validate_attestation.rs @@ -99,9 +99,6 @@ fn validate_attestation_parametric( crosslink_data_root: attestation.data.crosslink_data_root, epoch: attestation.data.slot.epoch(spec.slots_per_epoch), }; - dbg!(attestation.clone()); - dbg!(state.latest_crosslinks[attestation.data.shard as usize].clone()); - dbg!(potential_crosslink.clone()); verify!( (attestation.data.previous_crosslink == state.latest_crosslinks[attestation.data.shard as usize]) From c596e3f7d77fb04b7881b012019f66e5bf55b213 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 17:26:28 +1100 Subject: [PATCH 175/191] Change log levels of gossipsub events --- beacon_node/eth2-libp2p/src/behaviour.rs | 4 ++-- beacon_node/eth2-libp2p/src/service.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index fdf12f0bff..88bfd0042a 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -12,7 +12,7 @@ use libp2p::{ tokio_io::{AsyncRead, AsyncWrite}, NetworkBehaviour, PeerId, }; -use slog::{debug, o, warn}; +use slog::{debug, o, trace, warn}; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use types::{Attestation, BeaconBlock}; use types::{Topic, TopicHash}; @@ -47,7 +47,7 @@ impl NetworkBehaviourEventProcess { - debug!(self.log, "Received GossipEvent"; "msg" => format!("{:?}", gs_msg)); + trace!(self.log, "Received GossipEvent"; "msg" => format!("{:?}", gs_msg)); let pubsub_message = match PubsubMessage::ssz_decode(&gs_msg.data, 0) { //TODO: Punish peer on error diff --git a/beacon_node/eth2-libp2p/src/service.rs b/beacon_node/eth2-libp2p/src/service.rs index 0dc30cf420..f52d11ef1e 100644 --- a/beacon_node/eth2-libp2p/src/service.rs +++ b/beacon_node/eth2-libp2p/src/service.rs @@ -113,7 +113,7 @@ impl Stream for Service { topics, message, } => { - debug!(self.log, "Pubsub message received: {:?}", message); + trace!(self.log, "Pubsub message received: {:?}", message); return Ok(Async::Ready(Some(Libp2pEvent::PubsubMessage { source, topics, From a93f89894627a0b1276617965940abf91672b2db Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 31 Mar 2019 17:27:04 +1100 Subject: [PATCH 176/191] Improve gossipsub block processing --- beacon_node/network/src/beacon_chain.rs | 2 +- beacon_node/network/src/message_handler.rs | 5 +- beacon_node/network/src/sync/import_queue.rs | 38 ++++- beacon_node/network/src/sync/simple_sync.rs | 158 +++++++++++++----- .../testing_beacon_state_builder.rs | 2 +- 5 files changed, 160 insertions(+), 45 deletions(-) diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 7a8efb2540..827adeb3c9 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -10,7 +10,7 @@ use beacon_chain::{ use eth2_libp2p::rpc::HelloMessage; use types::{Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; -pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; +pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome, InvalidBlock}; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 0efa6b96fd..098a5b4bfb 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -208,8 +208,9 @@ impl MessageHandler { fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: PubsubMessage) { match gossip_message { PubsubMessage::Block(message) => { - self.sync - .on_block_gossip(peer_id, message, &mut self.network_context) + let _should_foward_on = + self.sync + .on_block_gossip(peer_id, message, &mut self.network_context); } PubsubMessage::Attestation(message) => { self.sync diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs index b9280440bf..0026347eb2 100644 --- a/beacon_node/network/src/sync/import_queue.rs +++ b/beacon_node/network/src/sync/import_queue.rs @@ -104,7 +104,7 @@ impl ImportQueue { } /// Returns `true` if `self.chain` has not yet processed this block. - pub fn is_new_block(&self, block_root: &Hash256) -> bool { + pub fn chain_has_not_seen_block(&self, block_root: &Hash256) -> bool { self.chain .is_new_block_root(&block_root) .unwrap_or_else(|_| { @@ -125,7 +125,7 @@ impl ImportQueue { let new_roots: Vec = block_roots .iter() // Ignore any roots already processed by the chain. - .filter(|brs| self.is_new_block(&brs.block_root)) + .filter(|brs| self.chain_has_not_seen_block(&brs.block_root)) // Ignore any roots already stored in the queue. .filter(|brs| !self.partials.iter().any(|p| p.block_root == brs.block_root)) .cloned() @@ -168,7 +168,7 @@ impl ImportQueue { for header in headers { let block_root = Hash256::from_slice(&header.hash_tree_root()[..]); - if self.is_new_block(&block_root) { + if self.chain_has_not_seen_block(&block_root) { self.insert_header(block_root, header, sender.clone()); required_bodies.push(block_root) } @@ -186,6 +186,12 @@ impl ImportQueue { } } + pub fn enqueue_full_blocks(&mut self, blocks: Vec, sender: PeerId) { + for block in blocks { + self.insert_full_block(block, sender.clone()); + } + } + /// Inserts a header to the queue. /// /// If the header already exists, the `inserted` time is set to `now` and not other @@ -239,6 +245,32 @@ impl ImportQueue { } }); } + + /// Updates an existing `partial` with the completed block, or adds a new (complete) partial. + /// + /// If the partial already existed, the `inserted` time is set to `now`. + fn insert_full_block(&mut self, block: BeaconBlock, sender: PeerId) { + let block_root = Hash256::from_slice(&block.hash_tree_root()[..]); + + let partial = PartialBeaconBlock { + slot: block.slot, + block_root, + header: Some(block.block_header()), + body: Some(block.body), + inserted: Instant::now(), + sender, + }; + + if let Some(i) = self + .partials + .iter() + .position(|p| p.block_root == block_root) + { + self.partials[i] = partial; + } else { + self.partials.push(partial) + } + } } /// Individual components of a `BeaconBlock`, potentially all that are required to form a full diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index e8a3da656d..6a78dc57d8 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,10 +1,11 @@ use super::import_queue::ImportQueue; -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock}; use crate::message_handler::NetworkContext; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId}; use eth2_libp2p::PeerId; use slog::{debug, error, info, o, warn}; +use ssz::TreeHash; use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; @@ -16,6 +17,10 @@ const SLOT_IMPORT_TOLERANCE: u64 = 100; /// The amount of seconds a block (or partial block) may exist in the import queue. const QUEUE_STALE_SECS: u64 = 60; +/// If a block is more than `FUTURE_SLOT_TOLERANCE` slots ahead of our slot clock, we drop it. +/// Otherwise we queue it. +const FUTURE_SLOT_TOLERANCE: u64 = 1; + /// Keeps track of syncing information for known connected peers. #[derive(Clone, Copy, Debug)] pub struct PeerSyncInfo { @@ -536,65 +541,130 @@ impl SimpleSync { } /// Process a gossip message declaring a new block. + /// + /// Returns a `bool` which, if `true`, indicates we should forward the block to our peers. pub fn on_block_gossip( &mut self, peer_id: PeerId, block: BeaconBlock, network: &mut NetworkContext, - ) { + ) -> bool { info!( self.log, "NewGossipBlock"; "peer" => format!("{:?}", peer_id), ); - /* // Ignore any block from a finalized slot. - if self.slot_is_finalized(msg.slot) { + if self.slot_is_finalized(block.slot) { warn!( self.log, "NewGossipBlock"; "msg" => "new block slot is finalized.", - "slot" => msg.slot, + "block_slot" => block.slot, ); - return; + return false; } + let block_root = Hash256::from_slice(&block.hash_tree_root()); + // Ignore any block that the chain already knows about. - if self.chain_has_seen_block(&msg.block_root) { - return; + if self.chain_has_seen_block(&block_root) { + println!("this happened"); + // TODO: Age confirm that we shouldn't forward a block if we already know of it. + return false; } - // k - if msg.slot == self.chain.hello_message().best_slot + 1 { - self.request_block_headers( - peer_id, - BeaconBlockHeadersRequest { - start_root: msg.block_root, - start_slot: msg.slot, - max_headers: 1, - skip_slots: 0, - }, - network, - ) + debug!( + self.log, + "NewGossipBlock"; + "peer" => format!("{:?}", peer_id), + "msg" => "processing block", + ); + match self.chain.process_block(block.clone()) { + Ok(BlockProcessingOutcome::InvalidBlock(InvalidBlock::ParentUnknown)) => { + // get the parent. + true + } + Ok(BlockProcessingOutcome::InvalidBlock(InvalidBlock::FutureSlot { + present_slot, + block_slot, + })) => { + if block_slot - present_slot > FUTURE_SLOT_TOLERANCE { + // The block is too far in the future, drop it. + warn!( + self.log, "NewGossipBlock"; + "msg" => "future block rejected", + "present_slot" => present_slot, + "block_slot" => block_slot, + "FUTURE_SLOT_TOLERANCE" => FUTURE_SLOT_TOLERANCE, + "peer" => format!("{:?}", peer_id), + ); + // Do not forward the block around to peers. + false + } else { + // The block is in the future, but not too far. + warn!( + self.log, "NewGossipBlock"; + "msg" => "queuing future block", + "present_slot" => present_slot, + "block_slot" => block_slot, + "FUTURE_SLOT_TOLERANCE" => FUTURE_SLOT_TOLERANCE, + "peer" => format!("{:?}", peer_id), + ); + // Queue the block for later processing. + self.import_queue.enqueue_full_blocks(vec![block], peer_id); + // Forward the block around to peers. + true + } + } + Ok(outcome) => { + if outcome.is_invalid() { + // The peer has sent a block which is fundamentally invalid. + warn!( + self.log, "NewGossipBlock"; + "msg" => "invalid block from peer", + "outcome" => format!("{:?}", outcome), + "peer" => format!("{:?}", peer_id), + ); + // Disconnect the peer + network.disconnect(peer_id, GoodbyeReason::Fault); + // Do not forward the block to peers. + false + } else if outcome.sucessfully_processed() { + // The block was valid and we processed it successfully. + info!( + self.log, "NewGossipBlock"; + "msg" => "block import successful", + "peer" => format!("{:?}", peer_id), + ); + // Forward the block to peers + true + } else { + // The block wasn't necessarily invalid but we didn't process it successfully. + // This condition shouldn't be reached. + error!( + self.log, "NewGossipBlock"; + "msg" => "unexpected condition in processing block.", + "outcome" => format!("{:?}", outcome), + ); + // Do not forward the block on. + false + } + } + Err(e) => { + // We encountered an error whilst processing the block. + // + // Blocks should not be able to trigger errors, instead they should be flagged as + // invalid. + error!( + self.log, "NewGossipBlock"; + "msg" => "internal error in processing block.", + "error" => format!("{:?}", e), + ); + // Do not forward the block to peers. + false + } } - - // TODO: if the block is a few more slots ahead, try to get all block roots from then until - // now. - // - // Note: only requests the new block -- will fail if we don't have its parents. - if !self.chain_has_seen_block(&msg.block_root) { - self.request_block_headers( - peer_id, - BeaconBlockHeadersRequest { - start_root: msg.block_root, - start_slot: msg.slot, - max_headers: 1, - skip_slots: 0, - }, - network, - ) - } - */ } /// Process a gossip message declaring a new attestation. @@ -724,6 +794,18 @@ impl SimpleSync { network.send_rpc_request(peer_id.clone(), RPCRequest::BeaconBlockBodies(req)); } + /// Returns `true` if `self.chain` has not yet processed this block. + pub fn chain_has_seen_block(&self, block_root: &Hash256) -> bool { + !self + .chain + .is_new_block_root(&block_root) + .unwrap_or_else(|_| { + error!(self.log, "Unable to determine if block is new."); + false + }) + } + + /// Returns `true` if the given slot is finalized in our chain. fn slot_is_finalized(&self, slot: Slot) -> bool { slot <= self .chain 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 f437240dc6..e25da37e7e 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 = 1553977336; // arbitrary + let genesis_time = 1554013000; // arbitrary let mut state = BeaconState::genesis( genesis_time, From 170f993032002f0d50fdb8bed25564fbad678622 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 1 Apr 2019 12:14:23 +1100 Subject: [PATCH 177/191] Clean up warnings --- beacon_node/client/src/lib.rs | 2 -- beacon_node/eth2-libp2p/src/rpc/methods.rs | 2 +- beacon_node/rpc/src/validator.rs | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 807fd9301e..6b4277c264 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -15,13 +15,11 @@ use futures::{future::Future, Stream}; use network::Service as NetworkService; 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. diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index f9adb93c17..dc0be19a92 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -1,7 +1,7 @@ use ssz::{Decodable, DecodeError, Encodable, SszStream}; /// Available RPC methods types and ids. use ssz_derive::{Decode, Encode}; -use types::{Attestation, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; +use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; #[derive(Debug)] /// Available Serenity Libp2p RPC methods diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 0a9d7015c9..4511a8913e 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -83,7 +83,7 @@ impl ValidatorService for ValidatorServiceInstance { RpcStatusCode::InvalidArgument, Some("Invalid public_key".to_string()), )) - .map_err(move |e| warn!(log_clone, "failed to reply {:?}", req)); + .map_err(move |_| warn!(log_clone, "failed to reply {:?}", req)); return ctx.spawn(f); } }; From 9f8850d0a18bbdefd9fc5d8eefadb42dc8bbb327 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 1 Apr 2019 12:14:44 +1100 Subject: [PATCH 178/191] Adds bootnode CLI parameter --- beacon_node/client/src/client_config.rs | 12 ++++++++++++ beacon_node/src/main.rs | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/beacon_node/client/src/client_config.rs b/beacon_node/client/src/client_config.rs index cad287f2cc..f7a257a3a9 100644 --- a/beacon_node/client/src/client_config.rs +++ b/beacon_node/client/src/client_config.rs @@ -10,6 +10,7 @@ use std::path::PathBuf; use types::multiaddr::Protocol; use types::multiaddr::ToMultiaddr; use types::ChainSpec; +use types::Multiaddr; /// Stores the client configuration for this Lighthouse instance. #[derive(Debug, Clone)] @@ -88,6 +89,17 @@ impl ClientConfig { } } + // Custom bootnodes + // TODO: Handle list of addresses + if let Some(boot_addresses_str) = args.value_of("boot_nodes") { + if let Ok(boot_address) = boot_addresses_str.parse::() { + config.net_conf.boot_nodes.append(&mut vec![boot_address]); + } else { + error!(log, "Invalid Bootnode multiaddress"; "Multiaddr" => boot_addresses_str); + return Err("Invalid IP Address"); + } + } + /* Filesystem related arguments */ // Custom datadir diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index ea74c73766..a4e1ee1308 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -16,6 +16,7 @@ fn main() { .version(version::version().as_str()) .author("Sigma Prime ") .about("Eth 2.0 Client") + // file system related arguments .arg( Arg::with_name("datadir") .long("datadir") @@ -23,6 +24,7 @@ fn main() { .help("Data directory for keys and databases.") .takes_value(true), ) + // network related arguments .arg( Arg::with_name("listen_address") .long("listen-address") @@ -37,6 +39,14 @@ fn main() { .help("Network listen port for p2p connections.") .takes_value(true), ) + .arg( + Arg::with_name("boot-nodes") + .long("boot-nodes") + .value_name("BOOTNODES") + .help("A list of comma separated multi addresses representing bootnodes to connect to.") + .takes_value(true), + ) + // rpc related arguments .arg( Arg::with_name("rpc") .long("rpc") From ddd9654f7001adb1ac254a9cefa76c297af91a45 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 1 Apr 2019 13:34:43 +1100 Subject: [PATCH 179/191] op-pool: fix bug in attestation_score The attestation scoring function was looking only at the previous epoch, but should really look at whichever epoch is appropriate for a given attestation. We also avoid including attestations that don't pay us any reward, as they simply bloat the chain. --- eth2/operation_pool/src/lib.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index c42527b608..0c5d78fe49 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -74,15 +74,26 @@ impl AttestationId { /// the aggregate attestation introduces, and is proportional to the size of the reward we will /// receive for including it in a block. // TODO: this could be optimised with a map from validator index to whether that validator has -// attested in the *current* epoch. Alternatively, we could cache an index that allows us to -// quickly look up the attestations in the current epoch for a given shard. -fn attestation_score(attestation: &Attestation, state: &BeaconState) -> usize { +// attested in each of the current and previous epochs. Currently quadractic in number of validators. +fn attestation_score(attestation: &Attestation, state: &BeaconState, spec: &ChainSpec) -> usize { // Bitfield of validators whose attestations are new/fresh. let mut new_validators = attestation.aggregation_bitfield.clone(); - state - .current_epoch_attestations + let attestation_epoch = attestation.data.slot.epoch(spec.slots_per_epoch); + + let state_attestations = if attestation_epoch == state.current_epoch(spec) { + &state.current_epoch_attestations + } else if attestation_epoch == state.previous_epoch(spec) { + &state.previous_epoch_attestations + } else { + return 0; + }; + + state_attestations .iter() + // In a single epoch, an attester should only be attesting for one shard. + // TODO: we avoid including slashable attestations in the state here, + // but maybe we should do something else with them (like construct slashings). .filter(|current_attestation| current_attestation.data.shard == attestation.data.shard) .for_each(|current_attestation| { // Remove the validators who have signed the existing attestation (they are not new) @@ -176,7 +187,9 @@ impl OperationPool { .filter(|attestation| validate_attestation(state, attestation, spec).is_ok()) // Scored by the number of new attestations they introduce (descending) // TODO: need to consider attestations introduced in THIS block - .map(|att| (att, attestation_score(att, state))) + .map(|att| (att, attestation_score(att, state, spec))) + // Don't include any useless attestations (score 0) + .filter(|&(_, score)| score != 0) .sorted_by_key(|&(_, score)| std::cmp::Reverse(score)) // Limited to the maximum number of attestations per block .take(spec.max_attestations as usize) @@ -695,7 +708,7 @@ mod tests { num_committees * (spec.slots_per_epoch * spec.target_committee_size) as usize; let mut state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(num_validators, spec); - let slot_offset = 100000; + let slot_offset = 1000 * spec.slots_per_epoch + spec.slots_per_epoch / 2; let slot = spec.genesis_slot + slot_offset; state_builder.teleport_to_slot(slot, spec); state_builder.build_caches(spec).unwrap(); @@ -726,7 +739,7 @@ mod tests { assert_eq!( att1.aggregation_bitfield.num_set_bits(), - attestation_score(&att1, state) + attestation_score(&att1, state, spec) ); state @@ -735,7 +748,7 @@ mod tests { assert_eq!( committee.committee.len() - 2, - attestation_score(&att2, &state) + attestation_score(&att2, state, spec) ); } } From 111c81f42836f215a4e6190cd95f139c83328b8c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 1 Apr 2019 15:23:38 +1100 Subject: [PATCH 180/191] Add ParentUnknown block processing to `SimpleSync` --- beacon_node/network/src/sync/simple_sync.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 6a78dc57d8..e7ef301bb7 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -582,7 +582,23 @@ impl SimpleSync { ); match self.chain.process_block(block.clone()) { Ok(BlockProcessingOutcome::InvalidBlock(InvalidBlock::ParentUnknown)) => { - // get the parent. + // The block was valid and we processed it successfully. + debug!( + self.log, "NewGossipBlock"; + "msg" => "parent block unknown", + "parent_root" => format!("{}", block.previous_block_root), + "peer" => format!("{:?}", peer_id), + ); + // Send a hello to learn of the clients best slot so we can then sync the require + // parent(s). + network.send_rpc_request( + peer_id.clone(), + RPCRequest::Hello(self.chain.hello_message()), + ); + // Forward the block onto our peers. + // + // Note: this may need to be changed if we decide to only forward blocks if we have + // all required info. true } Ok(BlockProcessingOutcome::InvalidBlock(InvalidBlock::FutureSlot { From a3ca3ec50d1058a45368918fe8313cabadf3bb72 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 1 Apr 2019 15:32:04 +1100 Subject: [PATCH 181/191] Break block proc. loop if we get a bad block --- beacon_node/network/src/sync/simple_sync.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index e7ef301bb7..841ae37ac5 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -726,6 +726,7 @@ impl SimpleSync { "reason" => format!("{:?}", outcome), ); network.disconnect(sender, GoodbyeReason::Fault); + break; } // If this results to true, the item will be removed from the queue. From a7df4f180081aa26a58bc3c4098f5365bce9f26c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 1 Apr 2019 15:38:22 +1100 Subject: [PATCH 182/191] Add log when block not processed in queue --- beacon_node/network/src/sync/simple_sync.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 841ae37ac5..038975f05a 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -733,6 +733,14 @@ impl SimpleSync { if outcome.sucessfully_processed() { successful += 1; self.import_queue.remove(block_root); + } else { + debug!( + self.log, + "ProcessImportQueue"; + "msg" => "Block not imported", + "outcome" => format!("{:?}", outcome), + "peer" => format!("{:?}", sender), + ); } } Err(e) => { From 5e80b90301d1abab663180d7d13278987b6597e1 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 1 Apr 2019 15:51:48 +1100 Subject: [PATCH 183/191] Extend queue stale time, queue more blocks --- beacon_node/network/src/sync/simple_sync.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 038975f05a..9a1e51bdd4 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -15,7 +15,7 @@ use types::{Attestation, BeaconBlock, Epoch, Hash256, Slot}; const SLOT_IMPORT_TOLERANCE: u64 = 100; /// The amount of seconds a block (or partial block) may exist in the import queue. -const QUEUE_STALE_SECS: u64 = 60; +const QUEUE_STALE_SECS: u64 = 600; /// If a block is more than `FUTURE_SLOT_TOLERANCE` slots ahead of our slot clock, we drop it. /// Otherwise we queue it. @@ -589,6 +589,9 @@ impl SimpleSync { "parent_root" => format!("{}", block.previous_block_root), "peer" => format!("{:?}", peer_id), ); + // Queue the block for later processing. + self.import_queue + .enqueue_full_blocks(vec![block], peer_id.clone()); // Send a hello to learn of the clients best slot so we can then sync the require // parent(s). network.send_rpc_request( From 3f160d3b995f848c035f1ab7c5d46da3dcc208c2 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Mon, 1 Apr 2019 16:29:11 +1100 Subject: [PATCH 184/191] Correct bootnodes cli parameter --- beacon_node/client/src/client_config.rs | 4 ++-- beacon_node/src/main.rs | 2 +- .../src/test_utils/testing_beacon_state_builder.rs | 11 ++++++++--- validator_client/src/service.rs | 12 +++++++----- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/beacon_node/client/src/client_config.rs b/beacon_node/client/src/client_config.rs index f7a257a3a9..407171ff58 100644 --- a/beacon_node/client/src/client_config.rs +++ b/beacon_node/client/src/client_config.rs @@ -77,7 +77,7 @@ impl ClientConfig { } // Custom listening address ipv4/ipv6 // TODO: Handle list of addresses - if let Some(listen_address_str) = args.value_of("listen_address") { + if let Some(listen_address_str) = args.value_of("listen-address") { if let Ok(listen_address) = listen_address_str.parse::() { let multiaddr = SocketAddr::new(listen_address, config.net_conf.listen_port) .to_multiaddr() @@ -91,7 +91,7 @@ impl ClientConfig { // Custom bootnodes // TODO: Handle list of addresses - if let Some(boot_addresses_str) = args.value_of("boot_nodes") { + if let Some(boot_addresses_str) = args.value_of("boot-nodes") { if let Ok(boot_address) = boot_addresses_str.parse::() { config.net_conf.boot_nodes.append(&mut vec![boot_address]); } else { diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index a4e1ee1308..45aafb3ce5 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -26,7 +26,7 @@ fn main() { ) // network related arguments .arg( - Arg::with_name("listen_address") + Arg::with_name("listen-address") .long("listen-address") .value_name("Listen Address") .help("The Network address to listen for p2p connections.") 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 def58b0d74..445debae7c 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -122,12 +122,17 @@ impl TestingBeaconStateBuilder { }) .collect(); + // TODO: Testing only. Burn with fire later. + // set genesis to the last 30 minute block. + // this is used for testing only. Allows multiple nodes to connect within a 30min window + // and agree on a genesis let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() - .as_secs() - - 30; - let genesis_time = now; // arbitrary + .as_secs(); + let secs_after_last_period = now.checked_rem(30 * 60).unwrap_or(0); + // genesis is now the last 30 minute block. + let genesis_time = now - secs_after_last_period; let mut state = BeaconState::genesis( genesis_time, diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index 38883e68fa..ce19c23e93 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -292,11 +292,13 @@ impl Service { let current_epoch = self.current_slot.epoch(self.spec.slots_per_epoch); // spawn a new thread separate to the runtime // TODO: Handle thread termination/timeout - std::thread::spawn(move || { - // the return value is a future which returns ready. - // built to be compatible with the tokio runtime. - let _empty = cloned_manager.run_update(current_epoch, cloned_log.clone()); - }); + // TODO: Add duties thread back in, with channel to process duties in duty change. + // leave sequential for now. + //std::thread::spawn(move || { + // the return value is a future which returns ready. + // built to be compatible with the tokio runtime. + let _empty = cloned_manager.run_update(current_epoch, cloned_log.clone()); + //}); } /// If there are any duties to process, spawn a separate thread and perform required actions. From 38f2cb955580d6ebee405e9086d2181d9058a9a1 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 2 Apr 2019 14:30:32 +1100 Subject: [PATCH 185/191] Run rustfmt --- eth2/state_processing/tests/tests.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/eth2/state_processing/tests/tests.rs b/eth2/state_processing/tests/tests.rs index 0363da8101..12b78c08f2 100644 --- a/eth2/state_processing/tests/tests.rs +++ b/eth2/state_processing/tests/tests.rs @@ -1,6 +1,9 @@ -use state_processing::{per_block_processing, per_block_processing_without_verifying_block_signature, per_slot_processing}; use serde_derive::Deserialize; use serde_yaml; +use state_processing::{ + per_block_processing, per_block_processing_without_verifying_block_signature, + per_slot_processing, +}; use std::{fs::File, io::prelude::*, path::PathBuf}; use types::*; #[allow(unused_imports)] @@ -79,17 +82,22 @@ fn run_state_transition_tests_small() { for block in test_case.blocks.iter() { while block.slot > state.slot { let latest_block_header = state.latest_block_header.clone(); - let res = per_slot_processing(&mut state, &latest_block_header, &test_case.config).unwrap(); + let res = per_slot_processing(&mut state, &latest_block_header, &test_case.config) + .unwrap(); } if test_case.verify_signatures { let res = per_block_processing(&mut state, &block, &test_case.config); - if res.is_err() { + if res.is_err() { println!("{:?}", i); println!("{:?}", res); }; } else { - let res = per_block_processing_without_verifying_block_signature(&mut state, &block, &test_case.config); - if res.is_err() { + let res = per_block_processing_without_verifying_block_signature( + &mut state, + &block, + &test_case.config, + ); + if res.is_err() { println!("{:?}", i); println!("{:?}", res); } From 3a42d7fa6ebbd5679c675b8edfdb663eccd18e6b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 2 Apr 2019 15:33:18 +1100 Subject: [PATCH 186/191] Disable some blop pool tests during debug --- eth2/operation_pool/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 41c41c3ff7..69a1ccc0bc 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -695,6 +695,7 @@ mod tests { /// Create a signed attestation for use in tests. /// Signed by all validators in `committee[signing_range]` and `committee[extra_signer]`. + #[cfg(not(debug_assertions))] fn signed_attestation>( committee: &CrosslinkCommittee, keypairs: &[Keypair], @@ -727,6 +728,7 @@ mod tests { } /// Test state for attestation-related tests. + #[cfg(not(debug_assertions))] fn attestation_test_state( spec: &ChainSpec, num_committees: usize, @@ -743,6 +745,7 @@ mod tests { } /// Set the latest crosslink in the state to match the attestation. + #[cfg(not(debug_assertions))] fn fake_latest_crosslink(att: &Attestation, state: &mut BeaconState, spec: &ChainSpec) { state.latest_crosslinks[att.data.shard as usize] = Crosslink { crosslink_data_root: att.data.crosslink_data_root, @@ -751,6 +754,7 @@ mod tests { } #[test] + #[cfg(not(debug_assertions))] fn test_attestation_score() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); @@ -782,6 +786,7 @@ mod tests { /// End-to-end test of basic attestation handling. #[test] + #[cfg(not(debug_assertions))] fn attestation_aggregation_insert_get_prune() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); @@ -847,6 +852,7 @@ mod tests { /// Adding an attestation already in the pool should not increase the size of the pool. #[test] + #[cfg(not(debug_assertions))] fn attestation_duplicate() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); @@ -873,6 +879,7 @@ mod tests { /// Adding lots of attestations that only intersect pairwise should lead to two aggregate /// attestations. #[test] + #[cfg(not(debug_assertions))] fn attestation_pairwise_overlapping() { let spec = &ChainSpec::foundation(); let (ref mut state, ref keypairs) = attestation_test_state(spec, 1); @@ -915,6 +922,7 @@ mod tests { /// high-quality attestations. To ensure that no aggregation occurs, ALL attestations /// are also signed by the 0th member of the committee. #[test] + #[cfg(not(debug_assertions))] fn attestation_get_max() { let spec = &ChainSpec::foundation(); let small_step_size = 2; From f61db9cac880194b55021c851808e4a8bf868463 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 2 Apr 2019 15:33:38 +1100 Subject: [PATCH 187/191] Fix merge conflict in `bls` Issue came about from Michael and Kirk doing simultaneous work. --- eth2/utils/bls/src/aggregate_signature.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eth2/utils/bls/src/aggregate_signature.rs b/eth2/utils/bls/src/aggregate_signature.rs index f446ce71ee..8c7ae5222e 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -38,7 +38,8 @@ impl AggregateSignature { /// Add (aggregate) another `AggregateSignature`. pub fn add_aggregate(&mut self, agg_signature: &AggregateSignature) { - self.0.add_aggregate(&agg_signature.0) + self.aggregate_signature + .add_aggregate(&agg_signature.aggregate_signature) } /// Verify the `AggregateSignature` against an `AggregatePublicKey`. From 1d34e2b2a5bd245a7c4e96fc40b261deee91814f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 2 Apr 2019 15:34:18 +1100 Subject: [PATCH 188/191] Fix bug in bitfield. --- eth2/utils/boolean-bitfield/src/lib.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index 0634490a58..d04516dbac 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -33,12 +33,21 @@ impl BooleanBitfield { } /// Create a new bitfield with the given length `initial_len` and all values set to `bit`. + /// + /// Note: if `initial_len` is not a multiple of 8, the remaining bits will be set to `false` + /// regardless of `bit`. pub fn from_elem(initial_len: usize, bit: bool) -> Self { // BitVec can panic if we don't set the len to be a multiple of 8. - let len = ((initial_len + 7) / 8) * 8; - Self { - 0: BitVec::from_elem(len, bit), + let full_len = ((initial_len + 7) / 8) * 8; + let mut bitfield = BitVec::from_elem(full_len, false); + + if bit { + for i in 0..initial_len { + bitfield.set(i, true); + } } + + Self { 0: bitfield } } /// Create a new bitfield using the supplied `bytes` as input From 8442a8ff203ffc5d613a4b0c044543cd3171182b Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Wed, 3 Apr 2019 13:40:06 +1100 Subject: [PATCH 189/191] Listed Usage cmake [options] cmake [options] cmake [options] -S -B Specify a source directory to (re-)generate a build system for it in the current working directory. Specify an existing build directory to re-generate its build system. Run 'cmake --help' for more information. as a requirement for building lighthouse, as it is a dependency that must be installed on a fresh arch system. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6da6732ad2..7727154e71 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ A few basic steps are needed to get set up: 5. Install build dependencies (Arch packages are listed here, your distribution will likely be similar): - `clang`: required by RocksDB. - `protobuf`: required for protobuf serialization (gRPC). + - `cmake`: required for building protobuf 6. Navigate to the working directory. 7. Run the test by using command `cargo test --all`. By running, it will pass all the required test cases. If you are doing it for the first time, then you can grab a coffee in the meantime. Usually, it takes time From 1913be0c6fc923378a14f41ad968a0433f5e2635 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 3 Apr 2019 16:23:09 +1100 Subject: [PATCH 190/191] Fix clippy lints --- beacon_node/beacon_chain/src/beacon_chain.rs | 12 +++------ .../test_harness/src/beacon_chain_harness.rs | 5 ++-- .../test_harness/src/test_case/state_check.rs | 1 + .../validator_harness/direct_beacon_node.rs | 8 ------ .../test_harness/src/validator_harness/mod.rs | 26 ++----------------- beacon_node/client/src/lib.rs | 19 +++++++------- beacon_node/client/src/notifier.rs | 2 +- beacon_node/eth2-libp2p/src/behaviour.rs | 19 +++++--------- beacon_node/eth2-libp2p/src/rpc/mod.rs | 6 ++--- beacon_node/eth2-libp2p/src/rpc/protocol.rs | 16 +++++++----- beacon_node/eth2-libp2p/src/service.rs | 13 ++++++---- beacon_node/network/src/message_handler.rs | 4 +-- beacon_node/network/src/service.rs | 12 ++++----- beacon_node/network/src/sync/simple_sync.rs | 2 +- beacon_node/rpc/src/attestation.rs | 10 +++---- beacon_node/rpc/src/beacon_block.rs | 4 +-- beacon_node/rpc/src/beacon_node.rs | 4 +-- eth2/state_processing/tests/tests.rs | 3 +-- .../testing_beacon_state_builder.rs | 3 +-- eth2/utils/bls/src/keypair.rs | 8 +++++- validator_client/src/config.rs | 2 ++ validator_client/src/service.rs | 2 +- 22 files changed, 76 insertions(+), 105 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 600c453fdf..a22f4179e2 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -497,21 +497,17 @@ where } else { // If the current head block is not from this slot, use the slot from the previous // epoch. - let root = *self.state.read().get_block_root( + *self.state.read().get_block_root( current_epoch_start_slot - self.spec.slots_per_epoch, &self.spec, - )?; - - root + )? } } else { // If we're not on the first slot of the epoch. - let root = *self + *self .state .read() - .get_block_root(current_epoch_start_slot, &self.spec)?; - - root + .get_block_root(current_epoch_start_slot, &self.spec)? }; Ok(AttestationData { 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 33c12d7c70..aeb734a4e4 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 @@ -211,9 +211,8 @@ impl BeaconChainHarness { // Ensure the validators slot clock is accurate. self.validators[proposer].set_slot(present_slot); - let block = self.validators[proposer].produce_block().unwrap(); - block + self.validators[proposer].produce_block().unwrap() } /// Advances the chain with a BeaconBlock and attestations from all validators. @@ -245,7 +244,7 @@ impl BeaconChainHarness { .for_each(|(i, attestation)| { self.beacon_chain .process_attestation(attestation.clone()) - .expect(&format!("Attestation {} invalid: {:?}", i, attestation)); + .unwrap_or_else(|_| panic!("Attestation {} invalid: {:?}", i, attestation)); }); debug!("Attestations processed."); diff --git a/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs b/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs index 7ac33c86c9..c6bdf8978f 100644 --- a/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs +++ b/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs @@ -52,6 +52,7 @@ impl StateCheck { /// # Panics /// /// Panics with an error message if any test fails. + #[allow(clippy::cyclomatic_complexity)] pub fn assert_valid(&self, state: &BeaconState, spec: &ChainSpec) { let state_epoch = state.slot.epoch(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 2d2b9e84dc..d47fd44b93 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 @@ -14,9 +14,6 @@ use slot_clock::SlotClock; use std::sync::Arc; use types::{AttestationData, BeaconBlock, FreeAttestation, Signature, Slot}; -// mod attester; -// mod producer; - /// Connect directly to a borrowed `BeaconChain` instance so an attester/producer can request/submit /// blocks/attestations. /// @@ -42,11 +39,6 @@ impl DirectBeaconNode { pub fn last_published_block(&self) -> Option { Some(self.published_blocks.read().last()?.clone()) } - - /// Get the last published attestation (if any). - pub fn last_published_free_attestation(&self) -> Option { - Some(self.published_attestations.read().last()?.clone()) - } } impl AttesterBeaconNode for DirectBeaconNode { diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs index 91a6794637..815d4b23b5 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs @@ -2,8 +2,7 @@ mod direct_beacon_node; mod direct_duties; mod local_signer; -use attester::PollOutcome as AttestationPollOutcome; -use attester::{Attester, Error as AttestationPollError}; +use attester::Attester; use beacon_chain::BeaconChain; use block_proposer::PollOutcome as BlockPollOutcome; use block_proposer::{BlockProducer, Error as BlockPollError}; @@ -14,7 +13,7 @@ use fork_choice::BitwiseLMDGhost; use local_signer::LocalSigner; use slot_clock::TestingSlotClock; use std::sync::Arc; -use types::{BeaconBlock, ChainSpec, FreeAttestation, Keypair, Slot}; +use types::{BeaconBlock, ChainSpec, Keypair, Slot}; #[derive(Debug, PartialEq)] pub enum BlockProduceError { @@ -22,12 +21,6 @@ pub enum BlockProduceError { PollError(BlockPollError), } -#[derive(Debug, PartialEq)] -pub enum AttestationProduceError { - DidNotProduce(AttestationPollOutcome), - PollError(AttestationPollError), -} - type TestingBlockProducer = BlockProducer< TestingSlotClock, DirectBeaconNode>, @@ -117,21 +110,6 @@ impl ValidatorHarness { .expect("Unable to obtain produced block.")) } - /// Run the `poll` function on the `Attester` and produce a `FreeAttestation`. - /// - /// An error is returned if the attester refuses to attest. - pub fn produce_free_attestation(&mut self) -> Result { - match self.attester.poll() { - Ok(AttestationPollOutcome::AttestationProduced(_)) => {} - Ok(outcome) => return Err(AttestationProduceError::DidNotProduce(outcome)), - Err(error) => return Err(AttestationProduceError::PollError(error)), - }; - Ok(self - .beacon_node - .last_published_free_attestation() - .expect("Unable to obtain produced attestation.")) - } - /// Set the validators slot clock to the specified slot. /// /// The validators slot clock will always read this value until it is set to something else. diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 6b4277c264..6a21493b13 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -25,9 +25,9 @@ use tokio::timer::Interval; /// sub-services in multiple threads. pub struct Client { /// Configuration for the lighthouse client. - config: ClientConfig, + _config: ClientConfig, /// The beacon chain for the running client. - beacon_chain: Arc>, + _beacon_chain: Arc>, /// Reference to the network service. pub network: Arc, /// Signal to terminate the RPC server. @@ -90,17 +90,18 @@ impl Client { network_logger, )?; - let mut rpc_exit_signal = None; // spawn the RPC server - if config.rpc_conf.enabled { - rpc_exit_signal = Some(rpc::start_server( + let rpc_exit_signal = if config.rpc_conf.enabled { + Some(rpc::start_server( &config.rpc_conf, executor, network_send, beacon_chain.clone(), &log, - )); - } + )) + } else { + None + }; 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() { @@ -129,8 +130,8 @@ impl Client { } Ok(Client { - config, - beacon_chain, + _config: config, + _beacon_chain: beacon_chain, rpc_exit_signal, slot_timer_exit_signal: Some(slot_timer_exit_signal), log, diff --git a/beacon_node/client/src/notifier.rs b/beacon_node/client/src/notifier.rs index 1a5ecbb53b..aa1e43c3cd 100644 --- a/beacon_node/client/src/notifier.rs +++ b/beacon_node/client/src/notifier.rs @@ -14,7 +14,7 @@ pub fn run(client: &Client, executor: TaskExecutor, exit: Exi // notification heartbeat let interval = Interval::new(Instant::now(), Duration::from_secs(5)); - let log = client.log.new(o!("Service" => "Notifier")); + let _log = client.log.new(o!("Service" => "Notifier")); // TODO: Debugging only let counter = Arc::new(Mutex::new(0)); diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 88bfd0042a..e1112e6ff9 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -65,17 +65,11 @@ impl NetworkBehaviourEventProcess {} + GossipsubEvent::Subscribed { .. } => {} + GossipsubEvent::Unsubscribed { .. } => {} } } } @@ -110,7 +104,8 @@ impl NetworkBehaviourEventProcess {} IdentifyEvent::SendBack { .. } => {} @@ -183,12 +178,12 @@ impl Behaviour { pub enum BehaviourEvent { RPC(PeerId, RPCEvent), PeerDialed(PeerId), - Identified(PeerId, IdentifyInfo), + Identified(PeerId, Box), // TODO: This is a stub at the moment GossipMessage { source: PeerId, topics: Vec, - message: PubsubMessage, + message: Box, }, } diff --git a/beacon_node/eth2-libp2p/src/rpc/mod.rs b/beacon_node/eth2-libp2p/src/rpc/mod.rs index 08573aa52b..57d7dadbe5 100644 --- a/beacon_node/eth2-libp2p/src/rpc/mod.rs +++ b/beacon_node/eth2-libp2p/src/rpc/mod.rs @@ -26,7 +26,7 @@ pub struct Rpc { /// Pins the generic substream. marker: PhantomData, /// Slog logger for RPC behaviour. - log: slog::Logger, + _log: slog::Logger, } impl Rpc { @@ -35,7 +35,7 @@ impl Rpc { Rpc { events: Vec::new(), marker: PhantomData, - log, + _log: log, } } @@ -65,7 +65,7 @@ where fn inject_connected(&mut self, peer_id: PeerId, connected_point: ConnectedPoint) { // if initialised the connection, report this upwards to send the HELLO request - if let ConnectedPoint::Dialer { address: _ } = connected_point { + if let ConnectedPoint::Dialer { .. } = connected_point { self.events.push(NetworkBehaviourAction::GenerateEvent( RPCMessage::PeerDialed(peer_id), )); diff --git a/beacon_node/eth2-libp2p/src/rpc/protocol.rs b/beacon_node/eth2-libp2p/src/rpc/protocol.rs index 314be1037c..5c1c47fbf7 100644 --- a/beacon_node/eth2-libp2p/src/rpc/protocol.rs +++ b/beacon_node/eth2-libp2p/src/rpc/protocol.rs @@ -31,7 +31,7 @@ impl Default for RPCProtocol { } /// A monotonic counter for ordering `RPCRequest`s. -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, Default)] pub struct RequestId(u64); impl RequestId { @@ -48,6 +48,12 @@ impl RequestId { impl Eq for RequestId {} +impl PartialEq for RequestId { + fn eq(&self, other: &RequestId) -> bool { + self.0 == other.0 + } +} + impl Hash for RequestId { fn hash(&self, state: &mut H) { self.0.hash(state); @@ -104,17 +110,15 @@ impl UpgradeInfo for RPCEvent { } } +type FnDecodeRPCEvent = fn(Vec, ()) -> Result; + impl InboundUpgrade for RPCProtocol where TSocket: AsyncRead + AsyncWrite, { type Output = RPCEvent; type Error = DecodeError; - type Future = upgrade::ReadOneThen< - upgrade::Negotiated, - (), - fn(Vec, ()) -> Result, - >; + type Future = upgrade::ReadOneThen, (), FnDecodeRPCEvent>; fn upgrade_inbound(self, socket: upgrade::Negotiated, _: Self::Info) -> Self::Future { upgrade::read_one_then(socket, MAX_READ_SIZE, (), |packet, ()| Ok(decode(packet)?)) diff --git a/beacon_node/eth2-libp2p/src/service.rs b/beacon_node/eth2-libp2p/src/service.rs index f52d11ef1e..07a36e408c 100644 --- a/beacon_node/eth2-libp2p/src/service.rs +++ b/beacon_node/eth2-libp2p/src/service.rs @@ -19,13 +19,16 @@ use std::io::{Error, ErrorKind}; use std::time::Duration; use types::{TopicBuilder, TopicHash}; +type Libp2pStream = Boxed<(PeerId, StreamMuxerBox), Error>; +type Libp2pBehaviour = Behaviour>; + /// The configuration and state of the libp2p components for the beacon node. pub struct Service { /// The libp2p Swarm handler. //TODO: Make this private - pub swarm: Swarm, Behaviour>>, + pub swarm: Swarm, /// This node's PeerId. - local_peer_id: PeerId, + _local_peer_id: PeerId, /// The libp2p logger handle. pub log: slog::Logger, } @@ -89,7 +92,7 @@ impl Service { info!(log, "Subscribed to topics: {:?}", subscribed_topics); Ok(Service { - local_peer_id, + _local_peer_id: local_peer_id, swarm, log, }) @@ -179,11 +182,11 @@ pub enum Libp2pEvent { /// Initiated the connection to a new peer. PeerDialed(PeerId), /// Received information about a peer on the network. - Identified(PeerId, IdentifyInfo), + Identified(PeerId, Box), /// Received pubsub message. PubsubMessage { source: PeerId, topics: Vec, - message: PubsubMessage, + message: Box, }, } diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 098a5b4bfb..c5ba25f823 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -41,7 +41,7 @@ pub enum HandlerMessage { /// An RPC response/request has been received. RPC(PeerId, RPCEvent), /// A gossip message has been received. - PubsubMessage(PeerId, PubsubMessage), + PubsubMessage(PeerId, Box), } impl MessageHandler { @@ -93,7 +93,7 @@ impl MessageHandler { } // we have received an RPC message request/response HandlerMessage::PubsubMessage(peer_id, gossip) => { - self.handle_gossip(peer_id, gossip); + self.handle_gossip(peer_id, *gossip); } //TODO: Handle all messages _ => {} diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index aee7eb4660..06e3f7af9d 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -17,7 +17,7 @@ use types::Topic; /// Service that handles communication between internal services and the eth2_libp2p network service. pub struct Service { //libp2p_service: Arc>, - libp2p_exit: oneshot::Sender<()>, + _libp2p_exit: oneshot::Sender<()>, network_send: crossbeam_channel::Sender, //message_handler: MessageHandler, //message_handler_send: Sender, @@ -54,7 +54,7 @@ impl Service { log, )?; let network_service = Service { - libp2p_exit, + _libp2p_exit: libp2p_exit, network_send: network_send.clone(), }; @@ -131,9 +131,7 @@ fn network_service( ); } Libp2pEvent::PubsubMessage { - source, - topics: _, - message, + source, message, .. } => { //TODO: Decide if we need to propagate the topic upwards. (Potentially for //attestations) @@ -167,7 +165,7 @@ fn network_service( } Ok(NetworkMessage::Publish { topics, message }) => { debug!(log, "Sending pubsub message on topics {:?}", topics); - libp2p_service.swarm.publish(topics, message); + libp2p_service.swarm.publish(topics, *message); } Err(TryRecvError::Empty) => break, Err(TryRecvError::Disconnected) => { @@ -190,7 +188,7 @@ pub enum NetworkMessage { /// Publish a message to pubsub mechanism. Publish { topics: Vec, - message: PubsubMessage, + message: Box, }, } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 9a1e51bdd4..824458b894 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -65,7 +65,7 @@ pub enum PeerStatus { } impl PeerStatus { - pub fn should_handshake(&self) -> bool { + pub fn should_handshake(self) -> bool { match self { PeerStatus::DifferentNetworkId => false, PeerStatus::FinalizedEpochNotInChain => false, diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index abef49df1d..3abfdac594 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -43,9 +43,9 @@ impl AttestationService for AttestationServiceInstance { let f = sink .fail(RpcStatus::new( RpcStatusCode::OutOfRange, - Some(format!( - "AttestationData request for a slot that is in the future." - )), + Some( + "AttestationData request for a slot that is in the future.".to_string(), + ), )) .map_err(move |e| { error!(log_clone, "Failed to reply with failure {:?}: {:?}", req, e) @@ -58,9 +58,7 @@ impl AttestationService for AttestationServiceInstance { let f = sink .fail(RpcStatus::new( RpcStatusCode::InvalidArgument, - Some(format!( - "AttestationData request for a slot that is in the past." - )), + Some("AttestationData request for a slot that is in the past.".to_string()), )) .map_err(move |e| { error!(log_clone, "Failed to reply with failure {:?}: {:?}", req, e) diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index 0b90f774a5..450bcbca15 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -43,7 +43,7 @@ impl BeaconBlockService for BeaconBlockServiceInstance { let f = sink .fail(RpcStatus::new( RpcStatusCode::InvalidArgument, - Some(format!("Invalid randao reveal signature")), + Some("Invalid randao reveal signature".to_string()), )) .map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e)); return ctx.spawn(f); @@ -114,7 +114,7 @@ impl BeaconBlockService for BeaconBlockServiceInstance { self.network_chan .send(NetworkMessage::Publish { topics: vec![topic], - message, + message: Box::new(message), }) .unwrap_or_else(|e| { error!( diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index c92ab66162..a9b8df3435 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -24,7 +24,7 @@ impl BeaconNodeService for BeaconNodeServiceInstance { // get the chain state let state = self.chain.get_state(); let state_fork = state.fork.clone(); - let genesis_time = state.genesis_time.clone(); + let genesis_time = state.genesis_time; // build the rpc fork struct let mut fork = Fork::new(); @@ -35,7 +35,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); + node_info.set_chain_id(u32::from(self.chain.get_spec().chain_id)); // send the node_info the requester let error_log = self.log.clone(); diff --git a/eth2/state_processing/tests/tests.rs b/eth2/state_processing/tests/tests.rs index 12b78c08f2..1394eeba05 100644 --- a/eth2/state_processing/tests/tests.rs +++ b/eth2/state_processing/tests/tests.rs @@ -82,8 +82,7 @@ fn run_state_transition_tests_small() { for block in test_case.blocks.iter() { while block.slot > state.slot { let latest_block_header = state.latest_block_header.clone(); - let res = per_slot_processing(&mut state, &latest_block_header, &test_case.config) - .unwrap(); + per_slot_processing(&mut state, &latest_block_header, &test_case.config).unwrap(); } if test_case.verify_signatures { let res = per_block_processing(&mut state, &block, &test_case.config); 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 445debae7c..9bdd9e149b 100644 --- a/eth2/types/src/test_utils/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -6,8 +6,7 @@ use dirs; use log::debug; use rayon::prelude::*; use std::path::{Path, PathBuf}; -//TODO: testing only -use std::time::{Duration, SystemTime}; +use std::time::SystemTime; pub const KEYPAIRS_FILE: &str = "keypairs.raw_keypairs"; diff --git a/eth2/utils/bls/src/keypair.rs b/eth2/utils/bls/src/keypair.rs index 2f0e794a61..75960a47d4 100644 --- a/eth2/utils/bls/src/keypair.rs +++ b/eth2/utils/bls/src/keypair.rs @@ -3,7 +3,7 @@ use serde_derive::{Deserialize, Serialize}; use std::fmt; use std::hash::{Hash, Hasher}; -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, Eq, Serialize, Deserialize)] pub struct Keypair { pub sk: SecretKey, pub pk: PublicKey, @@ -22,6 +22,12 @@ impl Keypair { } } +impl PartialEq for Keypair { + fn eq(&self, other: &Keypair) -> bool { + self == other + } +} + impl Hash for Keypair { /// Note: this is distinct from consensus serialization, it will produce a different hash. /// diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 3d426e8c70..903da047e7 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -81,6 +81,7 @@ impl Config { } /// Try to load keys from validator_dir, returning None if none are found or an error. + #[allow(dead_code)] pub fn fetch_keys(&self, log: &slog::Logger) -> Option> { let key_pairs: Vec = fs::read_dir(&self.data_dir) .unwrap() @@ -144,6 +145,7 @@ impl Config { } /// Saves a keypair to a file inside the appropriate validator directory. Returns the saved path filename. + #[allow(dead_code)] pub fn save_key(&self, key: &Keypair) -> Result { let validator_config_path = self.data_dir.join(key.identifier()); let key_path = validator_config_path.join(DEFAULT_PRIVATE_KEY_FILENAME); diff --git a/validator_client/src/service.rs b/validator_client/src/service.rs index ce19c23e93..a8a8325dd2 100644 --- a/validator_client/src/service.rs +++ b/validator_client/src/service.rs @@ -29,7 +29,7 @@ use std::sync::RwLock; use std::time::{Duration, Instant, SystemTime}; use tokio::prelude::*; use tokio::runtime::Builder; -use tokio::timer::{Delay, Interval}; +use tokio::timer::Interval; use tokio_timer::clock::Clock; use types::test_utils::generate_deterministic_keypairs; use types::{ChainSpec, Epoch, Fork, Slot}; From 914e0cf1fc523dfcdfdb30f10d3452d0a8bde040 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 3 Apr 2019 17:27:32 +1100 Subject: [PATCH 191/191] Skip long-running tests in debug --- eth2/state_processing/tests/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/eth2/state_processing/tests/tests.rs b/eth2/state_processing/tests/tests.rs index 1394eeba05..1359508dcd 100644 --- a/eth2/state_processing/tests/tests.rs +++ b/eth2/state_processing/tests/tests.rs @@ -1,5 +1,6 @@ use serde_derive::Deserialize; use serde_yaml; +#[cfg(not(debug_assertions))] use state_processing::{ per_block_processing, per_block_processing_without_verifying_block_signature, per_slot_processing, @@ -62,6 +63,7 @@ fn test_read_yaml() { } #[test] +#[cfg(not(debug_assertions))] fn run_state_transition_tests_small() { // Test sanity-check_small-config_32-vals.yaml let mut file = {