From 992f2101c259627cb85c188a612a4afa722592d0 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 22 Jan 2019 09:05:34 +1100 Subject: [PATCH] Add short ID to logging for BLS public keys --- beacon_node/Cargo.toml | 2 ++ beacon_node/src/rpc/validator.rs | 32 ++++++++++++++++++-------- validator_client/src/duties/grpc.rs | 4 +--- validator_client/src/duties/mod.rs | 22 ++++++++++-------- validator_client/src/duties/service.rs | 2 +- validator_client/src/main.rs | 1 + 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index 002a266b8a..ca78a47677 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Paul Hauner "] edition = "2018" [dependencies] +bls = { path = "../beacon_chain/utils/bls" } grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] } protobuf = "2.0.2" protos = { path = "../protos" } @@ -15,4 +16,5 @@ futures = "0.1.23" slog = "^2.2.3" slog-term = "^2.4.0" slog-async = "^2.3.0" +ssz = { path = "../beacon_chain/utils/ssz" } tokio = "0.1" diff --git a/beacon_node/src/rpc/validator.rs b/beacon_node/src/rpc/validator.rs index f0c828872f..f894deca6b 100644 --- a/beacon_node/src/rpc/validator.rs +++ b/beacon_node/src/rpc/validator.rs @@ -1,10 +1,12 @@ +use bls::PublicKey; use futures::Future; -use grpcio::{RpcContext, UnarySink}; +use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use protos::services::{ IndexResponse, ProposeBlockSlotRequest, ProposeBlockSlotResponse, PublicKey as PublicKeyRequest, }; use protos::services_grpc::ValidatorService; use slog::{debug, Logger}; +use ssz::Decodable; #[derive(Clone)] pub struct ValidatorServiceInstance { @@ -18,17 +20,27 @@ impl ValidatorService for ValidatorServiceInstance { req: PublicKeyRequest, sink: UnarySink, ) { - debug!(self.log, "RPC got ValidatorIndex"; "public_key" => format!("{:x?}", req.get_public_key())); + 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 mut resp = IndexResponse::new(); + let mut resp = IndexResponse::new(); - // TODO: return a legit value. - resp.set_index(1); + // TODO: return a legit value. + resp.set_index(1); - let f = sink - .success(resp) - .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); - ctx.spawn(f) + let f = sink + .success(resp) + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) + } else { + let f = sink + .fail(RpcStatus::new( + RpcStatusCode::InvalidArgument, + Some("Invalid public_key".to_string()), + )) + .map_err(move |e| println!("failed to reply {:?}: {:?}", req, e)); + ctx.spawn(f) + } } fn propose_block_slot( @@ -37,7 +49,7 @@ impl ValidatorService for ValidatorServiceInstance { req: ProposeBlockSlotRequest, sink: UnarySink, ) { - debug!(self.log, "RPC got ProposeBlockSlot"; "epoch" => req.get_epoch(), "validator_index" => req.get_validator_index()); + debug!(self.log, "RPC request"; "endpoint" => "ProposeBlockSlot", "epoch" => req.get_epoch(), "validator_index" => req.get_validator_index()); let mut resp = ProposeBlockSlotResponse::new(); diff --git a/validator_client/src/duties/grpc.rs b/validator_client/src/duties/grpc.rs index fc552516d1..7b4e1e6173 100644 --- a/validator_client/src/duties/grpc.rs +++ b/validator_client/src/duties/grpc.rs @@ -1,8 +1,6 @@ use super::traits::{BeaconNode, BeaconNodeError}; use super::EpochDuties; -use protos::services::{ - IndexResponse, ProposeBlockSlotRequest, ProposeBlockSlotResponse, PublicKey as IndexRequest, -}; +use protos::services::{ProposeBlockSlotRequest, PublicKey as IndexRequest}; use protos::services_grpc::ValidatorServiceClient; use ssz::ssz_encode; use types::PublicKey; diff --git a/validator_client/src/duties/mod.rs b/validator_client/src/duties/mod.rs index acfe108eef..e8438b3dbb 100644 --- a/validator_client/src/duties/mod.rs +++ b/validator_client/src/duties/mod.rs @@ -33,7 +33,7 @@ pub type EpochDutiesMap = HashMap; #[derive(Debug, PartialEq, Clone, Copy)] pub enum PollOutcome { - NoChange(u64, EpochDuties), + NoChange(u64), NewDuties(u64, EpochDuties), DutiesChanged(u64, EpochDuties), UnknownValidatorOrEpoch(u64), @@ -80,7 +80,7 @@ impl DutiesManager { // If these duties were known, check to see if they're updates or identical. let result = if let Some(known_duties) = map.get(&epoch) { if *known_duties == duties { - Ok(PollOutcome::NoChange(epoch, duties)) + Ok(PollOutcome::NoChange(epoch)) } else { Ok(PollOutcome::DutiesChanged(epoch, duties)) } @@ -129,25 +129,27 @@ mod tests { }; // Configure response from the BeaconNode. - beacon_node.set_next_shuffling_result(Ok(Some(EpochDuties { + let duties = EpochDuties { validator_index: 0, block_production_slot: Some(10), - }))); + }; + beacon_node.set_next_shuffling_result(Ok(Some(duties))); // Get the duties for the first time... - assert_eq!(manager.poll(), Ok(PollOutcome::NewDuties)); + assert_eq!(manager.poll(), Ok(PollOutcome::NewDuties(0, duties))); // Get the same duties again... - assert_eq!(manager.poll(), Ok(PollOutcome::NoChange)); + assert_eq!(manager.poll(), Ok(PollOutcome::NoChange(0))); // Return new duties. - beacon_node.set_next_shuffling_result(Ok(Some(EpochDuties { + let duties = EpochDuties { validator_index: 0, block_production_slot: Some(11), - }))); - assert_eq!(manager.poll(), Ok(PollOutcome::DutiesChanged)); + }; + beacon_node.set_next_shuffling_result(Ok(Some(duties))); + assert_eq!(manager.poll(), Ok(PollOutcome::DutiesChanged(0, duties))); // Return no duties. beacon_node.set_next_shuffling_result(Ok(None)); - assert_eq!(manager.poll(), Ok(PollOutcome::UnknownValidatorOrEpoch)); + assert_eq!(manager.poll(), Ok(PollOutcome::UnknownValidatorOrEpoch(0))); } } diff --git a/validator_client/src/duties/service.rs b/validator_client/src/duties/service.rs index c51db09609..401b7dddd4 100644 --- a/validator_client/src/duties/service.rs +++ b/validator_client/src/duties/service.rs @@ -17,7 +17,7 @@ impl DutiesManagerService { Err(error) => { error!(self.log, "Epoch duties poll error"; "error" => format!("{:?}", error)) } - Ok(PollOutcome::NoChange(epoch, _)) => { + Ok(PollOutcome::NoChange(epoch)) => { debug!(self.log, "No change in duties"; "epoch" => epoch) } Ok(PollOutcome::DutiesChanged(epoch, duties)) => { diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 6210557dd2..a52ffe1241 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -103,6 +103,7 @@ fn main() { let mut threads = vec![]; for keypair in keypairs { + info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); let duties_map = Arc::new(RwLock::new(EpochDutiesMap::new())); let duties_manager_thread = {