0c3fdcd57c
* Renamed fork_choice::process_attestation_from_block * Processing attestation in fork choice * Retrieving state from store and checking signature * Looser check on beacon state validity. * Cleaned up get_attestation_state * Expanded fork choice api to provide latest validator message. * Checking if the an attestation contains a latest message * Correct process_attestation error handling. * Copy paste error in comment fixed. * Tidy ancestor iterators * Getting attestation slot via helper method * Refactored attestation creation in test utils * Revert "Refactored attestation creation in test utils" This reverts commit 4d277fe4239a7194758b18fb5c00dfe0b8231306. * Integration tests for free attestation processing * Implicit conflicts resolved. * formatting * Do first pass on Grants code * Add another attestation processing test * Tidy attestation processing * Remove old code fragment * Add non-compiling half finished changes * Simplify, fix bugs, add tests for chain iters * Remove attestation processing from op pool * Fix bug with fork choice, tidy * Fix overly restrictive check in fork choice. * Ensure committee cache is build during attn proc * Ignore unknown blocks at fork choice * Various minor fixes * Make fork choice write lock in to read lock * Remove unused method * Tidy comments * Fix attestation prod. target roots change * Fix compile error in store iters * Reject any attestation prior to finalization * Begin metrics refactor * Move beacon_chain to new metrics structure. * Make metrics not panic if already defined * Use global prometheus gather at rest api * Unify common metric fns into a crate * Add heavy metering to block processing * Remove hypen from prometheus metric name * Add more beacon chain metrics * Add beacon chain persistence metric * Prune op pool on finalization * Add extra prom beacon chain metrics * Prefix BeaconChain metrics with "beacon_" * Add more store metrics * Add basic metrics to libp2p * Add metrics to HTTP server * Remove old `http_server` crate * Update metrics names to be more like standard * Fix broken beacon chain metrics, add slot clock metrics * Add lighthouse_metrics gather fn * Remove http args * Fix wrong state given to op pool prune * Make prom metric names more consistent * Add more metrics, tidy existing metrics * Fix store block read metrics * Tidy attestation metrics * Fix minor PR comments * Fix minor PR comments * Remove duplicated attestation finalization check * Remove awkward `let` statement * Add first attempts at HTTP bootstrap * Add beacon_block methods to rest api * Fix serde for block.body.grafitti * Allow travis failures on beta (see desc) There's a non-backward compatible change in `cargo fmt`. Stable and beta do not agree. * Add network routes to API * Fix rustc warnings * Add best_slot method * Add --bootstrap arg to beacon node * Get bootstrapper working for ENR address * Store intermediate states during block processing * Allow bootstrapper to scrape libp2p address * Update bootstrapper libp2p address finding * Add comments * Tidy API to be more consistent with recent decisions * Address some review comments * Make BeaconChainTypes Send + Sync + 'static * Add `/network/listen_port` API endpoint * Abandon starting the node if libp2p doesn't start * Update bootstrapper for API changes * Remove unnecessary trait bounds
273 lines
9.1 KiB
Rust
273 lines
9.1 KiB
Rust
use crate::discovery::Discovery;
|
|
use crate::rpc::{RPCEvent, RPCMessage, RPC};
|
|
use crate::{error, NetworkConfig};
|
|
use crate::{Topic, TopicHash};
|
|
use crate::{BEACON_ATTESTATION_TOPIC, BEACON_BLOCK_TOPIC};
|
|
use futures::prelude::*;
|
|
use libp2p::{
|
|
core::identity::Keypair,
|
|
discv5::Discv5Event,
|
|
gossipsub::{Gossipsub, GossipsubEvent},
|
|
identify::{Identify, IdentifyEvent},
|
|
ping::{Ping, PingConfig, PingEvent},
|
|
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
|
|
tokio_io::{AsyncRead, AsyncWrite},
|
|
NetworkBehaviour, PeerId,
|
|
};
|
|
use slog::{debug, o, trace};
|
|
use ssz::{ssz_encode, Encode};
|
|
use std::num::NonZeroU32;
|
|
use std::time::Duration;
|
|
|
|
const MAX_IDENTIFY_ADDRESSES: usize = 20;
|
|
|
|
/// Builds the network behaviour that manages the core protocols of eth2.
|
|
/// This core behaviour is managed by `Behaviour` which adds peer management to all core
|
|
/// behaviours.
|
|
#[derive(NetworkBehaviour)]
|
|
#[behaviour(out_event = "BehaviourEvent", poll_method = "poll")]
|
|
pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
|
|
/// The routing pub-sub mechanism for eth2.
|
|
gossipsub: Gossipsub<TSubstream>,
|
|
/// The Eth2 RPC specified in the wire-0 protocol.
|
|
eth2_rpc: RPC<TSubstream>,
|
|
/// Keep regular connection to peers and disconnect if absent.
|
|
// TODO: Remove Libp2p ping in favour of discv5 ping.
|
|
ping: Ping<TSubstream>,
|
|
// TODO: Using id for initial interop. This will be removed by mainnet.
|
|
/// Provides IP addresses and peer information.
|
|
identify: Identify<TSubstream>,
|
|
/// Discovery behaviour.
|
|
discovery: Discovery<TSubstream>,
|
|
#[behaviour(ignore)]
|
|
/// The events generated by this behaviour to be consumed in the swarm poll.
|
|
events: Vec<BehaviourEvent>,
|
|
/// Logger for behaviour actions.
|
|
#[behaviour(ignore)]
|
|
log: slog::Logger,
|
|
}
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
|
pub fn new(
|
|
local_key: &Keypair,
|
|
net_conf: &NetworkConfig,
|
|
log: &slog::Logger,
|
|
) -> error::Result<Self> {
|
|
let local_peer_id = local_key.public().clone().into_peer_id();
|
|
let behaviour_log = log.new(o!());
|
|
|
|
let ping_config = PingConfig::new()
|
|
.with_timeout(Duration::from_secs(30))
|
|
.with_interval(Duration::from_secs(20))
|
|
.with_max_failures(NonZeroU32::new(2).expect("2 != 0"))
|
|
.with_keep_alive(false);
|
|
|
|
let identify = Identify::new(
|
|
"lighthouse/libp2p".into(),
|
|
version::version(),
|
|
local_key.public(),
|
|
);
|
|
|
|
Ok(Behaviour {
|
|
eth2_rpc: RPC::new(log),
|
|
gossipsub: Gossipsub::new(local_peer_id.clone(), net_conf.gs_config.clone()),
|
|
discovery: Discovery::new(local_key, net_conf, log)?,
|
|
ping: Ping::new(ping_config),
|
|
identify,
|
|
events: Vec::new(),
|
|
log: behaviour_log,
|
|
})
|
|
}
|
|
|
|
pub fn discovery(&self) -> &Discovery<TSubstream> {
|
|
&self.discovery
|
|
}
|
|
}
|
|
|
|
// Implement the NetworkBehaviourEventProcess trait so that we can derive NetworkBehaviour for Behaviour
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<GossipsubEvent>
|
|
for Behaviour<TSubstream>
|
|
{
|
|
fn inject_event(&mut self, event: GossipsubEvent) {
|
|
match event {
|
|
GossipsubEvent::Message(gs_msg) => {
|
|
trace!(self.log, "Received GossipEvent"; "msg" => format!("{:?}", gs_msg));
|
|
|
|
let msg = PubsubMessage::from_topics(&gs_msg.topics, gs_msg.data);
|
|
|
|
self.events.push(BehaviourEvent::GossipMessage {
|
|
source: gs_msg.source,
|
|
topics: gs_msg.topics,
|
|
message: msg,
|
|
});
|
|
}
|
|
GossipsubEvent::Subscribed { .. } => {}
|
|
GossipsubEvent::Unsubscribed { .. } => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<RPCMessage>
|
|
for Behaviour<TSubstream>
|
|
{
|
|
fn inject_event(&mut self, event: RPCMessage) {
|
|
match event {
|
|
RPCMessage::PeerDialed(peer_id) => {
|
|
self.events.push(BehaviourEvent::PeerDialed(peer_id))
|
|
}
|
|
RPCMessage::PeerDisconnected(peer_id) => {
|
|
self.events.push(BehaviourEvent::PeerDisconnected(peer_id))
|
|
}
|
|
RPCMessage::RPC(peer_id, rpc_event) => {
|
|
self.events.push(BehaviourEvent::RPC(peer_id, rpc_event))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<PingEvent>
|
|
for Behaviour<TSubstream>
|
|
{
|
|
fn inject_event(&mut self, _event: PingEvent) {
|
|
// not interested in ping responses at the moment.
|
|
}
|
|
}
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
|
/// Consumes the events list when polled.
|
|
fn poll<TBehaviourIn>(
|
|
&mut self,
|
|
) -> Async<NetworkBehaviourAction<TBehaviourIn, BehaviourEvent>> {
|
|
if !self.events.is_empty() {
|
|
return Async::Ready(NetworkBehaviourAction::GenerateEvent(self.events.remove(0)));
|
|
}
|
|
|
|
Async::NotReady
|
|
}
|
|
}
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<IdentifyEvent>
|
|
for Behaviour<TSubstream>
|
|
{
|
|
fn inject_event(&mut self, event: IdentifyEvent) {
|
|
match event {
|
|
IdentifyEvent::Identified {
|
|
peer_id, mut info, ..
|
|
} => {
|
|
if info.listen_addrs.len() > MAX_IDENTIFY_ADDRESSES {
|
|
debug!(
|
|
self.log,
|
|
"More than 20 addresses have been identified, truncating"
|
|
);
|
|
info.listen_addrs.truncate(MAX_IDENTIFY_ADDRESSES);
|
|
}
|
|
debug!(self.log, "Identified Peer"; "Peer" => format!("{}", peer_id),
|
|
"Protocol Version" => info.protocol_version,
|
|
"Agent Version" => info.agent_version,
|
|
"Listening Addresses" => format!("{:?}", info.listen_addrs),
|
|
"Protocols" => format!("{:?}", info.protocols)
|
|
);
|
|
}
|
|
IdentifyEvent::Error { .. } => {}
|
|
IdentifyEvent::SendBack { .. } => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<Discv5Event>
|
|
for Behaviour<TSubstream>
|
|
{
|
|
fn inject_event(&mut self, _event: Discv5Event) {
|
|
// discv5 has no events to inject
|
|
}
|
|
}
|
|
|
|
/// Implements the combined behaviour for the libp2p service.
|
|
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
|
/* Pubsub behaviour functions */
|
|
|
|
/// Subscribes to a gossipsub topic.
|
|
pub fn subscribe(&mut self, topic: Topic) -> bool {
|
|
self.gossipsub.subscribe(topic)
|
|
}
|
|
|
|
/// Publishes a message on the pubsub (gossipsub) behaviour.
|
|
pub fn publish(&mut self, topics: Vec<Topic>, message: PubsubMessage) {
|
|
let message_bytes = ssz_encode(&message);
|
|
for topic in topics {
|
|
self.gossipsub.publish(topic, message_bytes.clone());
|
|
}
|
|
}
|
|
|
|
/* Eth2 RPC behaviour functions */
|
|
|
|
/// Sends an RPC Request/Response via the RPC protocol.
|
|
pub fn send_rpc(&mut self, peer_id: PeerId, rpc_event: RPCEvent) {
|
|
self.eth2_rpc.send_rpc(peer_id, rpc_event);
|
|
}
|
|
|
|
/* Discovery / Peer management functions */
|
|
pub fn connected_peers(&self) -> usize {
|
|
self.discovery.connected_peers()
|
|
}
|
|
}
|
|
|
|
/// The types of events than can be obtained from polling the behaviour.
|
|
pub enum BehaviourEvent {
|
|
RPC(PeerId, RPCEvent),
|
|
PeerDialed(PeerId),
|
|
PeerDisconnected(PeerId),
|
|
GossipMessage {
|
|
source: PeerId,
|
|
topics: Vec<TopicHash>,
|
|
message: PubsubMessage,
|
|
},
|
|
}
|
|
|
|
/// Messages that are passed to and from the pubsub (Gossipsub) behaviour.
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum PubsubMessage {
|
|
/// Gossipsub message providing notification of a new block.
|
|
Block(Vec<u8>),
|
|
/// Gossipsub message providing notification of a new attestation.
|
|
Attestation(Vec<u8>),
|
|
/// Gossipsub message from an unknown topic.
|
|
Unknown(Vec<u8>),
|
|
}
|
|
|
|
impl PubsubMessage {
|
|
/* Note: This is assuming we are not hashing topics. If we choose to hash topics, these will
|
|
* need to be modified.
|
|
*
|
|
* Also note that a message can be associated with many topics. As soon as one of the topics is
|
|
* known we match. If none of the topics are known we return an unknown state.
|
|
*/
|
|
fn from_topics(topics: &Vec<TopicHash>, data: Vec<u8>) -> Self {
|
|
for topic in topics {
|
|
match topic.as_str() {
|
|
BEACON_BLOCK_TOPIC => return PubsubMessage::Block(data),
|
|
BEACON_ATTESTATION_TOPIC => return PubsubMessage::Attestation(data),
|
|
_ => {}
|
|
}
|
|
}
|
|
PubsubMessage::Unknown(data)
|
|
}
|
|
}
|
|
|
|
impl Encode for PubsubMessage {
|
|
fn is_ssz_fixed_len() -> bool {
|
|
false
|
|
}
|
|
|
|
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
|
match self {
|
|
PubsubMessage::Block(inner)
|
|
| PubsubMessage::Attestation(inner)
|
|
| PubsubMessage::Unknown(inner) => {
|
|
// Encode the gossip as a Vec<u8>;
|
|
buf.append(&mut inner.as_ssz_bytes());
|
|
}
|
|
}
|
|
}
|
|
}
|