Add validator duties gRPC service to val. client

This commit is contained in:
Paul Hauner 2019-01-21 18:29:50 +11:00
parent d63d3c908b
commit ab502de8ec
5 changed files with 49 additions and 55 deletions

View File

@ -1,11 +1,11 @@
mod grpc; mod grpc;
mod service; mod service;
#[cfg(test)]
mod test_node; mod test_node;
mod traits; mod traits;
use self::traits::{BeaconNode, BeaconNodeError}; use self::traits::{BeaconNode, BeaconNodeError};
use super::EpochDutiesMap; use super::EpochDutiesMap;
use crate::duties::EpochDuties;
use slot_clock::SlotClock; use slot_clock::SlotClock;
use spec::ChainSpec; use spec::ChainSpec;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -139,6 +139,7 @@ mod tests {
use super::*; use super::*;
use slot_clock::TestingSlotClock; use slot_clock::TestingSlotClock;
use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use types::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use crate::duties::EpochDuties;
// TODO: implement more thorough testing. // TODO: implement more thorough testing.
// //

View File

@ -1,63 +1,34 @@
use super::EpochDuties;
use super::traits::{BeaconNode, BeaconNodeError}; use super::traits::{BeaconNode, BeaconNodeError};
use protos::services::{ use protos::services::ValidatorAssignmentRequest;
BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest,
};
use protos::services_grpc::BeaconBlockServiceClient; use protos::services_grpc::BeaconBlockServiceClient;
use ssz::{ssz_encode, Decodable}; use ssz::ssz_encode;
use types::{BeaconBlock, BeaconBlockBody, Hash256, Signature}; use types::{PublicKey};
impl BeaconNode for BeaconBlockServiceClient { impl BeaconNode for BeaconBlockServiceClient {
fn produce_beacon_block(&self, slot: u64) -> Result<Option<BeaconBlock>, BeaconNodeError> { fn request_shuffling(&self, epoch: u64, public_key: &PublicKey) -> Result<Option<EpochDuties>, BeaconNodeError> {
let mut req = ProduceBeaconBlockRequest::new(); let mut req = ValidatorAssignmentRequest::new();
req.set_slot(slot); req.set_epoch(epoch);
req.set_public_key(ssz_encode(public_key).to_vec());
let reply = self let reply = self
.produce_beacon_block(&req) .validator_assignment(&req)
.map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?; .map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?;
if reply.has_block() { if reply.has_validator_assignment() {
let block = reply.get_block(); let assignment = reply.get_validator_assignment();
let (signature, _) = Signature::ssz_decode(block.get_signature(), 0) let block_production_slot = if assignment.has_block_production_slot() {
.map_err(|_| BeaconNodeError::DecodeFailure)?; Some(assignment.get_block_production_slot())
} else {
None
};
// TODO: this conversion is incomplete; fix it. let duties = EpochDuties { block_production_slot };
Ok(Some(BeaconBlock {
slot: block.get_slot(), Ok(Some(duties))
parent_root: Hash256::zero(),
state_root: Hash256::zero(),
randao_reveal: Hash256::from(block.get_randao_reveal()),
candidate_pow_receipt_root: Hash256::zero(),
signature,
body: BeaconBlockBody {
proposer_slashings: vec![],
casper_slashings: vec![],
attestations: vec![],
deposits: vec![],
exits: vec![],
},
}))
} else { } else {
Ok(None) Ok(None)
} }
} }
fn publish_beacon_block(&self, block: BeaconBlock) -> Result<bool, BeaconNodeError> {
let mut req = PublishBeaconBlockRequest::new();
// TODO: this conversion is incomplete; fix it.
let mut grpc_block = GrpcBeaconBlock::new();
grpc_block.set_slot(block.slot);
grpc_block.set_block_root(vec![0]);
grpc_block.set_randao_reveal(block.randao_reveal.to_vec());
grpc_block.set_signature(ssz_encode(&block.signature));
req.set_block(grpc_block);
let reply = self
.publish_beacon_block(&req)
.map_err(|err| BeaconNodeError::RemoteFailure(format!("{:?}", err)))?;
Ok(reply.get_success())
}
} }

View File

@ -1,3 +1,9 @@
mod grpc;
mod service;
#[cfg(test)]
mod test_node;
mod traits;
use self::traits::{BeaconNode, BeaconNodeError}; use self::traits::{BeaconNode, BeaconNodeError};
use bls::PublicKey; use bls::PublicKey;
use slot_clock::SlotClock; use slot_clock::SlotClock;
@ -5,10 +11,6 @@ use spec::ChainSpec;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
mod service;
mod test_node;
mod traits;
pub use self::service::DutiesManagerService; pub use self::service::DutiesManagerService;
#[derive(Debug, PartialEq, Clone, Copy, Default)] #[derive(Debug, PartialEq, Clone, Copy, Default)]

View File

@ -4,7 +4,6 @@ use bls::PublicKey;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum BeaconNodeError { pub enum BeaconNodeError {
RemoteFailure(String), RemoteFailure(String),
DecodeFailure,
} }
pub trait BeaconNode: Send + Sync { pub trait BeaconNode: Send + Sync {

View File

@ -96,6 +96,25 @@ fn main() {
for keypair in keypairs { for keypair in keypairs {
let duties_map = Arc::new(RwLock::new(EpochDutiesMap::new())); let duties_map = Arc::new(RwLock::new(EpochDutiesMap::new()));
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 = 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();
})
};
let producer_thread = { let producer_thread = {
let spec = spec.clone(); let spec = spec.clone();
let duties_map = duties_map.clone(); let duties_map = duties_map.clone();
@ -113,11 +132,13 @@ fn main() {
block_producer_service.run(); block_producer_service.run();
}) })
}; };
threads.push(((), producer_thread));
threads.push((duties_manager_thread, producer_thread));
} }
for tuple in threads { for tuple in threads {
let (manager, producer) = tuple; let (manager, producer) = tuple;
let _ = producer.join(); let _ = producer.join();
let _ = manager.join();
} }
} }