Add patch for attestation crosslinks

This commit is contained in:
Age Manning 2019-03-31 13:06:01 +11:00
commit ae8e470d21
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
4 changed files with 50 additions and 39 deletions

View File

@ -492,10 +492,7 @@ where
beacon_block_root: self.head().beacon_block_root, beacon_block_root: self.head().beacon_block_root,
target_root, target_root,
crosslink_data_root: Hash256::zero(), crosslink_data_root: Hash256::zero(),
previous_crosslink: Crosslink { previous_crosslink: state.latest_crosslinks[shard as usize].clone(),
epoch: self.state.read().slot.epoch(self.spec.slots_per_epoch),
crosslink_data_root: Hash256::zero(),
},
source_epoch: state.current_justified_epoch, source_epoch: state.current_justified_epoch,
source_root: state.current_justified_root, source_root: state.current_justified_root,
}) })

View File

@ -1,4 +1,3 @@
use crate::rpc::methods::BlockRootSlot;
use crate::rpc::{RPCEvent, RPCMessage, Rpc}; use crate::rpc::{RPCEvent, RPCMessage, Rpc};
use crate::NetworkConfig; use crate::NetworkConfig;
use futures::prelude::*; use futures::prelude::*;
@ -15,8 +14,7 @@ use libp2p::{
}; };
use slog::{debug, o, warn}; use slog::{debug, o, warn};
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use ssz_derive::{Decode, Encode}; use types::{Attestation, BeaconBlock};
use types::Attestation;
use types::{Topic, TopicHash}; use types::{Topic, TopicHash};
/// Builds the network behaviour for the libp2p Swarm. /// Builds the network behaviour for the libp2p Swarm.
@ -198,7 +196,7 @@ pub enum BehaviourEvent {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum PubsubMessage { pub enum PubsubMessage {
/// Gossipsub message providing notification of a new block. /// Gossipsub message providing notification of a new block.
Block(BlockRootSlot), Block(BeaconBlock),
/// Gossipsub message providing notification of a new attestation. /// Gossipsub message providing notification of a new attestation.
Attestation(Attestation), Attestation(Attestation),
} }
@ -224,7 +222,7 @@ impl Decodable for PubsubMessage {
let (id, index) = u32::ssz_decode(bytes, index)?; let (id, index) = u32::ssz_decode(bytes, index)?;
match id { match id {
0 => { 0 => {
let (block, index) = BlockRootSlot::ssz_decode(bytes, index)?; let (block, index) = BeaconBlock::ssz_decode(bytes, index)?;
Ok((PubsubMessage::Block(block), index)) Ok((PubsubMessage::Block(block), index))
} }
1 => { 1 => {
@ -243,10 +241,7 @@ mod test {
#[test] #[test]
fn ssz_encoding() { fn ssz_encoding() {
let original = PubsubMessage::Block(BlockRootSlot { let original = PubsubMessage::Block(BeaconBlock::empty(&ChainSpec::foundation()));
block_root: Hash256::from_slice(&[42; 32]),
slot: Slot::new(4),
});
let encoded = ssz_encode(&original); let encoded = ssz_encode(&original);

View File

@ -8,7 +8,7 @@ use slog::{debug, error, info, o, warn};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; 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. /// The number of slots that we can import blocks ahead of us, before going into full Sync mode.
const SLOT_IMPORT_TOLERANCE: u64 = 100; const SLOT_IMPORT_TOLERANCE: u64 = 100;
@ -539,7 +539,7 @@ impl SimpleSync {
pub fn on_block_gossip( pub fn on_block_gossip(
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
msg: BlockRootSlot, block: BeaconBlock,
network: &mut NetworkContext, network: &mut NetworkContext,
) { ) {
info!( info!(
@ -548,6 +548,7 @@ impl SimpleSync {
"peer" => format!("{:?}", peer_id), "peer" => format!("{:?}", peer_id),
); );
/*
// Ignore any block from a finalized slot. // Ignore any block from a finalized slot.
if self.slot_is_finalized(msg.slot) { if self.slot_is_finalized(msg.slot) {
warn!( warn!(
@ -558,11 +559,13 @@ impl SimpleSync {
return; return;
} }
// TODO: if the block is a few more slots ahead, try to get all block roots from then until // Ignore any block that the chain already knows about.
// now. if self.chain_has_seen_block(&msg.block_root) {
// return;
// 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) {
// k
if msg.slot == self.chain.hello_message().best_slot + 1 {
self.request_block_headers( self.request_block_headers(
peer_id, peer_id,
BeaconBlockHeadersRequest { BeaconBlockHeadersRequest {
@ -574,6 +577,24 @@ impl SimpleSync {
network, 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. /// Process a gossip message declaring a new attestation.

View File

@ -1,6 +1,5 @@
use crate::beacon_chain::BeaconChain; use crate::beacon_chain::BeaconChain;
use crossbeam_channel; use crossbeam_channel;
use eth2_libp2p::rpc::methods::BlockRootSlot;
use eth2_libp2p::PubsubMessage; use eth2_libp2p::PubsubMessage;
use futures::Future; use futures::Future;
use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink};
@ -12,9 +11,9 @@ use protos::services::{
use protos::services_grpc::BeaconBlockService; use protos::services_grpc::BeaconBlockService;
use slog::Logger; use slog::Logger;
use slog::{error, info, trace, warn}; use slog::{error, info, trace, warn};
use ssz::{ssz_encode, Decodable, TreeHash}; use ssz::{ssz_encode, Decodable};
use std::sync::Arc; use std::sync::Arc;
use types::{BeaconBlock, Hash256, Signature, Slot}; use types::{BeaconBlock, Signature, Slot};
#[derive(Clone)] #[derive(Clone)]
pub struct BeaconBlockServiceInstance { pub struct BeaconBlockServiceInstance {
@ -94,8 +93,6 @@ impl BeaconBlockService for BeaconBlockServiceInstance {
match BeaconBlock::ssz_decode(ssz_serialized_block, 0) { match BeaconBlock::ssz_decode(ssz_serialized_block, 0) {
Ok((block, _i)) => { Ok((block, _i)) => {
let block_root = Hash256::from_slice(&block.hash_tree_root()[..]);
match self.chain.process_block(block.clone()) { match self.chain.process_block(block.clone()) {
Ok(outcome) => { Ok(outcome) => {
if outcome.sucessfully_processed() { if outcome.sucessfully_processed() {
@ -111,21 +108,22 @@ impl BeaconBlockService for BeaconBlockServiceInstance {
// TODO: Obtain topics from the network service properly. // TODO: Obtain topics from the network service properly.
let topic = let topic =
types::TopicBuilder::new("beacon_chain".to_string()).build(); types::TopicBuilder::new("beacon_chain".to_string()).build();
let message = PubsubMessage::Block(BlockRootSlot { let message = PubsubMessage::Block(block);
block_root,
slot: block.slot,
});
match self.network_chan.send(NetworkMessage::Publish { // Publish the block to the p2p network via gossipsub.
topics: vec![topic], self.network_chan
message, .send(NetworkMessage::Publish {
}) { topics: vec![topic],
Ok(_) => {} message,
Err(_) => warn!( })
self.log, .unwrap_or_else(|e| {
"Could not send published block to the network service" error!(
), self.log,
} "PublishBeaconBlock";
"type" => "failed to publish to gossipsub",
"error" => format!("{:?}", e)
);
});
resp.set_success(true); resp.set_success(true);
} else if outcome.is_invalid() { } else if outcome.is_invalid() {