diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 286597183..fdf12f0bf 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 39fe772b4..e8a3da656 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 f6b426c18..6978e0f0e 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() {