2019-03-04 07:31:01 +00:00
|
|
|
use crate::error;
|
|
|
|
use crate::message_handler::{HandlerMessage, MessageHandler};
|
|
|
|
use crate::NetworkConfig;
|
2019-05-30 08:27:19 +00:00
|
|
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8
* state_processing: epoch processing v0.8.0
* state_processing: block processing v0.8.0
* tree_hash_derive: support generics in SignedRoot
* types v0.8: update to use ssz_types
* state_processing v0.8: use ssz_types
* ssz_types: add bitwise methods and from_elem
* types: fix v0.8 FIXMEs
* ssz_types: add bitfield shift_up
* ssz_types: iterators and DerefMut for VariableList
* types,state_processing: use VariableList
* ssz_types: fix BitVector Decode impl
Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it
to be considered variable length!
* types: fix test modules for v0.8 update
* types: remove slow type-level arithmetic
* state_processing: fix tests for v0.8
* op_pool: update for v0.8
* ssz_types: Bitfield difference length-independent
Allow computing the difference of two bitfields of different lengths.
* Implement compact committee support
* epoch_processing: committee & active index roots
* state_processing: genesis state builder v0.8
* state_processing: implement v0.8.1
* Further improve tree_hash
* Strip examples, tests from cached_tree_hash
* Update TreeHash, un-impl CachedTreeHash
* Update bitfield TreeHash, un-impl CachedTreeHash
* Update FixedLenVec TreeHash, unimpl CachedTreeHash
* Update update tree_hash_derive for new TreeHash
* Fix TreeHash, un-impl CachedTreeHash for ssz_types
* Remove fixed_len_vec, ssz benches
SSZ benches relied upon fixed_len_vec -- it is easier to just delete
them and rebuild them later (when necessary)
* Remove boolean_bitfield crate
* Fix fake_crypto BLS compile errors
* Update ef_tests for new v.8 type params
* Update ef_tests submodule to v0.8.1 tag
* Make fixes to support parsing ssz ef_tests
* `compact_committee...` to `compact_committees...`
* Derive more traits for `CompactCommittee`
* Flip bitfield byte-endianness
* Fix tree_hash for bitfields
* Modify CLI output for ef_tests
* Bump ssz crate version
* Update ssz_types doc comment
* Del cached tree hash tests from ssz_static tests
* Tidy SSZ dependencies
* Rename ssz_types crate to eth2_ssz_types
* validator_client: update for v0.8
* ssz_types: update union/difference for bit order swap
* beacon_node: update for v0.8, EthSpec
* types: disable cached tree hash, update min spec
* state_processing: fix slot bug in committee update
* tests: temporarily disable fork choice harness test
See #447
* committee cache: prevent out-of-bounds access
In the case where we tried to access the committee of a shard that didn't have a committee in the
current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This
commit adds a check to make the failure safe and explicit.
* fix bug in get_indexed_attestation and simplify
There was a bug in our implementation of get_indexed_attestation whereby
incorrect "committee indices" were used to index into the custody bitfield. The
bug was only observable in the case where some bits of the custody bitfield were
set to 1. The implementation has been simplified to remove the bug, and a test
added.
* state_proc: workaround for compact committees bug
https://github.com/ethereum/eth2.0-specs/issues/1315
* v0.8: updates to make the EF tests pass
* Remove redundant max operation checks.
* Always supply both messages when checking attestation signatures -- allowing
verification of an attestation with no signatures.
* Swap the order of the fork and domain constant in `get_domain`, to match
the spec.
* rustfmt
* ef_tests: add new epoch processing tests
* Integrate v0.8 into master (compiles)
* Remove unused crates, fix clippy lints
* Replace v0.6.3 tags w/ v0.8.1
* Remove old comment
* Ensure lmd ghost tests only run in release
* Update readme
2019-07-30 02:44:51 +00:00
|
|
|
use core::marker::PhantomData;
|
2019-03-20 04:09:24 +00:00
|
|
|
use eth2_libp2p::Service as LibP2PService;
|
2019-04-03 05:00:09 +00:00
|
|
|
use eth2_libp2p::Topic;
|
2019-08-23 05:53:53 +00:00
|
|
|
use eth2_libp2p::{Enr, Libp2pEvent, Multiaddr, PeerId, Swarm};
|
2019-03-25 12:02:51 +00:00
|
|
|
use eth2_libp2p::{PubsubMessage, RPCEvent};
|
2019-03-12 06:28:11 +00:00
|
|
|
use futures::prelude::*;
|
|
|
|
use futures::Stream;
|
2019-07-24 07:45:31 +00:00
|
|
|
use parking_lot::Mutex;
|
2019-03-18 12:34:44 +00:00
|
|
|
use slog::{debug, info, o, trace};
|
2019-03-18 06:38:23 +00:00
|
|
|
use std::sync::Arc;
|
2019-03-12 06:28:11 +00:00
|
|
|
use tokio::runtime::TaskExecutor;
|
2019-07-16 07:28:15 +00:00
|
|
|
use tokio::sync::{mpsc, oneshot};
|
2019-03-04 07:31:01 +00:00
|
|
|
|
2019-03-20 04:09:24 +00:00
|
|
|
/// Service that handles communication between internal services and the eth2_libp2p network service.
|
2019-05-25 10:51:15 +00:00
|
|
|
pub struct Service<T: BeaconChainTypes> {
|
2019-08-10 01:44:17 +00:00
|
|
|
libp2p_service: Arc<Mutex<LibP2PService>>,
|
2019-08-23 05:53:53 +00:00
|
|
|
libp2p_port: u16,
|
2019-04-03 05:23:09 +00:00
|
|
|
_libp2p_exit: oneshot::Sender<()>,
|
2019-08-10 01:44:17 +00:00
|
|
|
_network_send: mpsc::UnboundedSender<NetworkMessage>,
|
2019-08-29 11:23:28 +00:00
|
|
|
_phantom: PhantomData<T>,
|
2019-03-04 07:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 10:51:15 +00:00
|
|
|
impl<T: BeaconChainTypes + 'static> Service<T> {
|
2019-03-04 07:31:01 +00:00
|
|
|
pub fn new(
|
2019-05-25 10:51:15 +00:00
|
|
|
beacon_chain: Arc<BeaconChain<T>>,
|
2019-03-18 05:16:54 +00:00
|
|
|
config: &NetworkConfig,
|
2019-03-17 10:49:56 +00:00
|
|
|
executor: &TaskExecutor,
|
2019-03-04 07:31:01 +00:00
|
|
|
log: slog::Logger,
|
2019-08-10 01:44:17 +00:00
|
|
|
) -> error::Result<(Arc<Self>, mpsc::UnboundedSender<NetworkMessage>)> {
|
2019-03-18 12:18:25 +00:00
|
|
|
// build the network channel
|
2019-08-10 01:44:17 +00:00
|
|
|
let (network_send, network_recv) = mpsc::unbounded_channel::<NetworkMessage>();
|
2019-03-04 07:31:01 +00:00
|
|
|
// launch message handler thread
|
|
|
|
let message_handler_log = log.new(o!("Service" => "MessageHandler"));
|
2019-03-19 12:20:39 +00:00
|
|
|
let message_handler_send = MessageHandler::spawn(
|
2019-03-18 12:18:25 +00:00
|
|
|
beacon_chain,
|
|
|
|
network_send.clone(),
|
|
|
|
executor,
|
|
|
|
message_handler_log,
|
|
|
|
)?;
|
2019-03-04 07:31:01 +00:00
|
|
|
|
2019-08-29 11:23:28 +00:00
|
|
|
let network_log = log.new(o!("Service" => "Network"));
|
2019-03-21 02:15:14 +00:00
|
|
|
// launch libp2p service
|
2019-08-29 11:23:28 +00:00
|
|
|
let libp2p_service = Arc::new(Mutex::new(LibP2PService::new(
|
|
|
|
config.clone(),
|
|
|
|
network_log.clone(),
|
|
|
|
)?));
|
2019-03-04 07:31:01 +00:00
|
|
|
|
2019-03-21 02:15:14 +00:00
|
|
|
let libp2p_exit = spawn_service(
|
2019-07-24 07:45:31 +00:00
|
|
|
libp2p_service.clone(),
|
2019-03-18 12:18:25 +00:00
|
|
|
network_recv,
|
|
|
|
message_handler_send,
|
|
|
|
executor,
|
2019-08-29 11:23:28 +00:00
|
|
|
network_log,
|
2019-03-18 12:18:25 +00:00
|
|
|
)?;
|
2019-03-19 11:53:51 +00:00
|
|
|
let network_service = Service {
|
2019-07-24 07:45:31 +00:00
|
|
|
libp2p_service,
|
2019-08-23 05:53:53 +00:00
|
|
|
libp2p_port: config.libp2p_port,
|
2019-04-03 05:23:09 +00:00
|
|
|
_libp2p_exit: libp2p_exit,
|
2019-07-24 07:45:31 +00:00
|
|
|
_network_send: network_send.clone(),
|
2019-05-08 09:38:18 +00:00
|
|
|
_phantom: PhantomData,
|
2019-03-12 06:28:11 +00:00
|
|
|
};
|
2019-03-04 07:31:01 +00:00
|
|
|
|
2019-03-19 11:53:51 +00:00
|
|
|
Ok((Arc::new(network_service), network_send))
|
2019-03-04 07:31:01 +00:00
|
|
|
}
|
2019-03-12 06:28:11 +00:00
|
|
|
|
2019-08-23 05:53:53 +00:00
|
|
|
/// Returns the local ENR from the underlying Discv5 behaviour that external peers may connect
|
|
|
|
/// to.
|
|
|
|
pub fn local_enr(&self) -> Enr {
|
|
|
|
self.libp2p_service
|
|
|
|
.lock()
|
|
|
|
.swarm
|
|
|
|
.discovery()
|
|
|
|
.local_enr()
|
|
|
|
.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the local libp2p PeerID.
|
|
|
|
pub fn local_peer_id(&self) -> PeerId {
|
|
|
|
self.libp2p_service.lock().local_peer_id.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the list of `Multiaddr` that the underlying libp2p instance is listening on.
|
|
|
|
pub fn listen_multiaddrs(&self) -> Vec<Multiaddr> {
|
|
|
|
Swarm::listeners(&self.libp2p_service.lock().swarm)
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the libp2p port that this node has been configured to listen using.
|
|
|
|
pub fn listen_port(&self) -> u16 {
|
|
|
|
self.libp2p_port
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of libp2p connected peers.
|
|
|
|
pub fn connected_peers(&self) -> usize {
|
|
|
|
self.libp2p_service.lock().swarm.connected_peers()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the set of `PeerId` that are connected via libp2p.
|
|
|
|
pub fn connected_peer_set(&self) -> Vec<PeerId> {
|
|
|
|
self.libp2p_service
|
|
|
|
.lock()
|
|
|
|
.swarm
|
|
|
|
.discovery()
|
|
|
|
.connected_peer_set()
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides a reference to the underlying libp2p service.
|
2019-08-10 01:44:17 +00:00
|
|
|
pub fn libp2p_service(&self) -> Arc<Mutex<LibP2PService>> {
|
2019-07-24 07:45:31 +00:00
|
|
|
self.libp2p_service.clone()
|
2019-03-12 06:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:44:17 +00:00
|
|
|
fn spawn_service(
|
|
|
|
libp2p_service: Arc<Mutex<LibP2PService>>,
|
|
|
|
network_recv: mpsc::UnboundedReceiver<NetworkMessage>,
|
|
|
|
message_handler_send: mpsc::UnboundedSender<HandlerMessage>,
|
2019-03-17 10:49:56 +00:00
|
|
|
executor: &TaskExecutor,
|
2019-03-12 06:28:11 +00:00
|
|
|
log: slog::Logger,
|
2019-07-16 07:28:15 +00:00
|
|
|
) -> error::Result<tokio::sync::oneshot::Sender<()>> {
|
|
|
|
let (network_exit, exit_rx) = tokio::sync::oneshot::channel();
|
2019-03-12 06:28:11 +00:00
|
|
|
|
|
|
|
// spawn on the current executor
|
|
|
|
executor.spawn(
|
|
|
|
network_service(
|
2019-03-21 02:15:14 +00:00
|
|
|
libp2p_service,
|
2019-03-12 06:28:11 +00:00
|
|
|
network_recv,
|
|
|
|
message_handler_send,
|
|
|
|
log.clone(),
|
|
|
|
)
|
|
|
|
// allow for manual termination
|
|
|
|
.select(exit_rx.then(|_| Ok(())))
|
|
|
|
.then(move |_| {
|
2019-03-18 07:22:01 +00:00
|
|
|
info!(log.clone(), "Network service shutdown");
|
2019-03-12 06:28:11 +00:00
|
|
|
Ok(())
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2019-03-18 12:18:25 +00:00
|
|
|
Ok(network_exit)
|
2019-03-12 06:28:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 12:02:51 +00:00
|
|
|
//TODO: Potentially handle channel errors
|
2019-08-10 01:44:17 +00:00
|
|
|
fn network_service(
|
|
|
|
libp2p_service: Arc<Mutex<LibP2PService>>,
|
|
|
|
mut network_recv: mpsc::UnboundedReceiver<NetworkMessage>,
|
|
|
|
mut message_handler_send: mpsc::UnboundedSender<HandlerMessage>,
|
2019-03-12 06:28:11 +00:00
|
|
|
log: slog::Logger,
|
2019-03-20 04:09:24 +00:00
|
|
|
) -> impl futures::Future<Item = (), Error = eth2_libp2p::error::Error> {
|
|
|
|
futures::future::poll_fn(move || -> Result<_, eth2_libp2p::error::Error> {
|
2019-07-30 10:33:37 +00:00
|
|
|
// if the network channel is not ready, try the swarm
|
|
|
|
loop {
|
2019-07-16 07:28:15 +00:00
|
|
|
// poll the network channel
|
|
|
|
match network_recv.poll() {
|
2019-07-24 07:45:31 +00:00
|
|
|
Ok(Async::Ready(Some(message))) => match message {
|
|
|
|
NetworkMessage::Send(peer_id, outgoing_message) => match outgoing_message {
|
|
|
|
OutgoingMessage::RPC(rpc_event) => {
|
|
|
|
trace!(log, "Sending RPC Event: {:?}", rpc_event);
|
|
|
|
libp2p_service.lock().swarm.send_rpc(peer_id, rpc_event);
|
2019-07-16 07:28:15 +00:00
|
|
|
}
|
2019-07-24 07:45:31 +00:00
|
|
|
},
|
|
|
|
NetworkMessage::Publish { topics, message } => {
|
|
|
|
debug!(log, "Sending pubsub message"; "topics" => format!("{:?}",topics));
|
2019-08-29 11:23:28 +00:00
|
|
|
libp2p_service.lock().swarm.publish(&topics, message);
|
2019-07-16 07:28:15 +00:00
|
|
|
}
|
2019-07-24 07:45:31 +00:00
|
|
|
},
|
2019-07-30 10:33:37 +00:00
|
|
|
Ok(Async::NotReady) => break,
|
2019-07-16 07:28:15 +00:00
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
return Err(eth2_libp2p::error::Error::from("Network channel closed"));
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
return Err(eth2_libp2p::error::Error::from("Network channel error"));
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 10:33:37 +00:00
|
|
|
}
|
2019-07-16 07:28:15 +00:00
|
|
|
|
2019-07-30 10:33:37 +00:00
|
|
|
loop {
|
2019-07-16 07:28:15 +00:00
|
|
|
// poll the swarm
|
2019-07-24 07:45:31 +00:00
|
|
|
match libp2p_service.lock().poll() {
|
2019-03-21 02:15:14 +00:00
|
|
|
Ok(Async::Ready(Some(event))) => match event {
|
|
|
|
Libp2pEvent::RPC(peer_id, rpc_event) => {
|
|
|
|
trace!(log, "RPC Event: RPC message received: {:?}", rpc_event);
|
|
|
|
message_handler_send
|
2019-07-16 07:28:15 +00:00
|
|
|
.try_send(HandlerMessage::RPC(peer_id, rpc_event))
|
2019-07-16 13:26:31 +00:00
|
|
|
.map_err(|_| "Failed to send RPC to handler")?;
|
2019-03-21 02:15:14 +00:00
|
|
|
}
|
|
|
|
Libp2pEvent::PeerDialed(peer_id) => {
|
2019-08-29 11:23:28 +00:00
|
|
|
debug!(log, "Peer Dialed"; "PeerID" => format!("{:?}", peer_id));
|
2019-03-21 02:15:14 +00:00
|
|
|
message_handler_send
|
2019-07-16 07:28:15 +00:00
|
|
|
.try_send(HandlerMessage::PeerDialed(peer_id))
|
2019-07-16 12:32:37 +00:00
|
|
|
.map_err(|_| "Failed to send PeerDialed to handler")?;
|
|
|
|
}
|
|
|
|
Libp2pEvent::PeerDisconnected(peer_id) => {
|
2019-08-29 11:23:28 +00:00
|
|
|
debug!(log, "Peer Disconnected"; "PeerID" => format!("{:?}", peer_id));
|
2019-07-16 12:32:37 +00:00
|
|
|
message_handler_send
|
2019-07-16 13:26:31 +00:00
|
|
|
.try_send(HandlerMessage::PeerDisconnected(peer_id))
|
2019-07-16 12:32:37 +00:00
|
|
|
.map_err(|_| "Failed to send PeerDisconnected to handler")?;
|
2019-03-21 02:15:14 +00:00
|
|
|
}
|
2019-03-25 12:02:51 +00:00
|
|
|
Libp2pEvent::PubsubMessage {
|
2019-04-03 05:23:09 +00:00
|
|
|
source, message, ..
|
2019-03-25 12:02:51 +00:00
|
|
|
} => {
|
|
|
|
//TODO: Decide if we need to propagate the topic upwards. (Potentially for
|
|
|
|
//attestations)
|
|
|
|
message_handler_send
|
2019-07-16 07:28:15 +00:00
|
|
|
.try_send(HandlerMessage::PubsubMessage(source, message))
|
2019-03-25 12:02:51 +00:00
|
|
|
.map_err(|_| " failed to send pubsub message to handler")?;
|
|
|
|
}
|
2019-03-21 02:15:14 +00:00
|
|
|
},
|
|
|
|
Ok(Async::Ready(None)) => unreachable!("Stream never ends"),
|
2019-07-30 10:33:37 +00:00
|
|
|
Ok(Async::NotReady) => break,
|
|
|
|
Err(_) => break,
|
2019-03-12 06:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-24 07:45:31 +00:00
|
|
|
|
2019-03-12 06:28:11 +00:00
|
|
|
Ok(Async::NotReady)
|
|
|
|
})
|
2019-03-04 07:31:01 +00:00
|
|
|
}
|
2019-03-18 12:18:25 +00:00
|
|
|
|
|
|
|
/// Types of messages that the network service can receive.
|
2019-07-16 12:32:37 +00:00
|
|
|
#[derive(Debug)]
|
2019-08-10 01:44:17 +00:00
|
|
|
pub enum NetworkMessage {
|
2019-03-21 02:15:14 +00:00
|
|
|
/// Send a message to libp2p service.
|
2019-03-18 12:18:25 +00:00
|
|
|
//TODO: Define typing for messages across the wire
|
2019-03-18 12:34:44 +00:00
|
|
|
Send(PeerId, OutgoingMessage),
|
2019-03-25 12:02:51 +00:00
|
|
|
/// Publish a message to pubsub mechanism.
|
2019-03-25 12:39:39 +00:00
|
|
|
Publish {
|
|
|
|
topics: Vec<Topic>,
|
2019-08-10 01:44:17 +00:00
|
|
|
message: PubsubMessage,
|
2019-03-25 12:39:39 +00:00
|
|
|
},
|
2019-03-18 12:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Type of outgoing messages that can be sent through the network service.
|
2019-07-16 12:32:37 +00:00
|
|
|
#[derive(Debug)]
|
2019-03-18 12:34:44 +00:00
|
|
|
pub enum OutgoingMessage {
|
|
|
|
/// Send an RPC request/response.
|
|
|
|
RPC(RPCEvent),
|
2019-03-18 12:18:25 +00:00
|
|
|
}
|