From d4fecd8a84e92060ce2da6f2102b6dc9c5449d0a Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 26 Mar 2019 12:32:38 +1100 Subject: [PATCH] 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 4e1875665..6317414b0 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 80d512c54..38c9d0b24 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 04a02a221..ba2acfffb 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);