lighthouse/beacon_node/network/src/message_handler.rs

307 lines
11 KiB
Rust
Raw Normal View History

use crate::beacon_chain::BeaconChain;
use crate::error;
2019-03-19 01:19:07 +00:00
use crate::service::{NetworkMessage, OutgoingMessage};
use crate::sync::SimpleSync;
use crossbeam_channel::{unbounded as channel, Sender};
2019-03-20 04:09:24 +00:00
use eth2_libp2p::{
2019-03-25 07:59:50 +00:00
behaviour::IncomingGossip,
rpc::{methods::GoodbyeReason, RPCRequest, RPCResponse, RequestId},
2019-03-21 07:21:50 +00:00
PeerId, RPCEvent,
};
2019-03-20 04:09:24 +00:00
use futures::future;
2019-03-24 02:59:27 +00:00
use slog::{debug, warn};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
/// Timeout for RPC requests.
// const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
2019-03-19 02:25:25 +00:00
/// Timeout before banning a peer for non-identification.
// const HELLO_TIMEOUT: Duration = Duration::from_secs(30);
/// Handles messages received from the network and client and organises syncing.
pub struct MessageHandler {
/// Currently loaded and initialised beacon chain.
_chain: Arc<BeaconChain>,
/// The syncing framework.
sync: SimpleSync,
/// The context required to send messages to, and process messages from peers.
network_context: NetworkContext,
/// The `MessageHandler` logger.
log: slog::Logger,
}
/// Types of messages the handler can receive.
#[derive(Debug, Clone)]
pub enum HandlerMessage {
/// We have initiated a connection to a new peer.
PeerDialed(PeerId),
/// Peer has disconnected,
PeerDisconnected(PeerId),
/// An RPC response/request has been received.
RPC(PeerId, RPCEvent),
2019-03-25 03:27:20 +00:00
/// A gossip message has been received.
IncomingGossip(PeerId, IncomingGossip),
}
impl MessageHandler {
/// Initializes and runs the MessageHandler.
2019-03-19 12:20:39 +00:00
pub fn spawn(
beacon_chain: Arc<BeaconChain>,
network_send: crossbeam_channel::Sender<NetworkMessage>,
executor: &tokio::runtime::TaskExecutor,
log: slog::Logger,
) -> error::Result<Sender<HandlerMessage>> {
debug!(log, "Service starting");
let (handler_send, handler_recv) = channel();
// Initialise sync and begin processing in thread
// generate the Message handler
2019-03-19 10:55:57 +00:00
let sync = SimpleSync::new(beacon_chain.clone(), &log);
let mut handler = MessageHandler {
_chain: beacon_chain.clone(),
sync,
network_context: NetworkContext::new(network_send, log.clone()),
log: log.clone(),
};
// spawn handler task
// TODO: Handle manual termination of thread
executor.spawn(future::poll_fn(move || -> Result<_, _> {
loop {
handler.handle_message(handler_recv.recv().map_err(|_| {
2019-03-18 07:22:01 +00:00
debug!(log, "Network message handler terminated.");
})?);
}
}));
Ok(handler_send)
}
2019-03-19 10:44:52 +00:00
/// Handle all messages incoming from the network service.
fn handle_message(&mut self, message: HandlerMessage) {
match message {
// we have initiated a connection to a peer
HandlerMessage::PeerDialed(peer_id) => {
2019-03-21 07:21:50 +00:00
self.sync.on_connect(peer_id, &mut self.network_context);
}
// we have received an RPC message request/response
HandlerMessage::RPC(peer_id, rpc_event) => {
self.handle_rpc_message(peer_id, rpc_event);
}
2019-03-25 03:27:20 +00:00
// we have received an RPC message request/response
HandlerMessage::IncomingGossip(peer_id, gossip) => {
self.handle_gossip(peer_id, gossip);
}
//TODO: Handle all messages
_ => {}
}
}
2019-03-19 10:44:52 +00:00
/* RPC - Related functionality */
/// Handle RPC messages
fn handle_rpc_message(&mut self, peer_id: PeerId, rpc_message: RPCEvent) {
match rpc_message {
2019-03-19 12:20:39 +00:00
RPCEvent::Request { id, body, .. // TODO: Clean up RPC Message types, have a cleaner type by this point.
} => self.handle_rpc_request(peer_id, id, body),
2019-03-19 12:20:39 +00:00
RPCEvent::Response { id, result, .. } => self.handle_rpc_response(peer_id, id, result),
}
}
2019-03-19 02:03:12 +00:00
/// A new RPC request has been received from the network.
2019-03-25 05:48:44 +00:00
fn handle_rpc_request(&mut self, peer_id: PeerId, request_id: RequestId, request: RPCRequest) {
// TODO: process the `id`.
2019-03-19 02:03:12 +00:00
match request {
2019-03-25 05:48:44 +00:00
RPCRequest::Hello(hello_message) => self.sync.on_hello_request(
peer_id,
request_id,
hello_message,
&mut self.network_context,
),
RPCRequest::Goodbye(goodbye_reason) => self.sync.on_goodbye(peer_id, goodbye_reason),
2019-03-25 05:48:44 +00:00
RPCRequest::BeaconBlockRoots(request) => self.sync.on_beacon_block_roots_request(
peer_id,
request_id,
request,
&mut self.network_context,
),
2019-03-24 01:50:23 +00:00
RPCRequest::BeaconBlockHeaders(request) => self.sync.on_beacon_block_headers_request(
peer_id,
2019-03-25 05:48:44 +00:00
request_id,
2019-03-24 01:50:23 +00:00
request,
&mut self.network_context,
),
RPCRequest::BeaconBlockBodies(request) => self.sync.on_beacon_block_bodies_request(
peer_id,
2019-03-25 05:48:44 +00:00
request_id,
2019-03-24 01:50:23 +00:00
request,
&mut self.network_context,
),
RPCRequest::BeaconChainState(_) => {
// We do not implement this endpoint, it is not required and will only likely be
// useful for light-client support in later phases.
warn!(self.log, "BeaconChainState RPC call is not supported.");
}
2019-03-19 02:03:12 +00:00
}
}
2019-03-19 02:03:12 +00:00
/// An RPC response has been received from the network.
// we match on id and ignore responses past the timeout.
2019-03-25 05:48:44 +00:00
fn handle_rpc_response(&mut self, peer_id: PeerId, id: RequestId, response: RPCResponse) {
// if response id is not related to a request, ignore (likely RPC timeout)
if self
.network_context
2019-03-25 05:48:44 +00:00
.outstanding_outgoing_request_ids
.remove(&(peer_id.clone(), id.clone()))
.is_none()
{
2019-03-25 05:48:44 +00:00
warn!(
self.log,
"Unknown ResponseId for incoming RPCRequest";
"peer" => format!("{:?}", peer_id),
"request_id" => format!("{:?}", id)
);
2019-03-19 02:25:25 +00:00
return;
}
2019-03-25 05:48:44 +00:00
2019-03-24 02:59:27 +00:00
match response {
RPCResponse::Hello(hello_message) => {
2019-03-21 07:21:50 +00:00
self.sync
2019-03-24 01:50:23 +00:00
.on_hello_response(peer_id, hello_message, &mut self.network_context);
}
RPCResponse::BeaconBlockRoots(response) => {
self.sync.on_beacon_block_roots_response(
peer_id,
response,
&mut self.network_context,
2019-03-24 01:50:23 +00:00
);
}
2019-03-23 02:23:44 +00:00
RPCResponse::BeaconBlockHeaders(response) => {
self.sync.on_beacon_block_headers_response(
peer_id,
response,
&mut self.network_context,
2019-03-24 01:50:23 +00:00
);
2019-03-23 02:23:44 +00:00
}
2019-03-23 07:48:09 +00:00
RPCResponse::BeaconBlockBodies(response) => {
self.sync.on_beacon_block_bodies_response(
peer_id,
response,
&mut self.network_context,
2019-03-24 01:50:23 +00:00
);
2019-03-23 07:48:09 +00:00
}
RPCResponse::BeaconChainState(_) => {
// We do not implement this endpoint, it is not required and will only likely be
// useful for light-client support in later phases.
//
// Theoretically, we shouldn't reach this code because we should never send a
// beacon state RPC request.
warn!(self.log, "BeaconChainState RPC call is not supported.");
}
2019-03-24 01:50:23 +00:00
};
}
2019-03-25 03:27:20 +00:00
/// Handle RPC messages
fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: IncomingGossip) {
match gossip_message {
IncomingGossip::Block(message) => {
self.sync
.on_block_gossip(peer_id, message, &mut self.network_context)
}
IncomingGossip::Attestation(message) => {
2019-03-25 04:30:46 +00:00
self.sync
.on_attestation_gossip(peer_id, message, &mut self.network_context)
2019-03-25 03:27:20 +00:00
}
}
}
}
2019-03-19 02:03:12 +00:00
pub struct NetworkContext {
/// The network channel to relay messages to the Network service.
network_send: crossbeam_channel::Sender<NetworkMessage>,
/// A mapping of peers and the RPC id we have sent an RPC request to.
2019-03-25 05:48:44 +00:00
outstanding_outgoing_request_ids: HashMap<(PeerId, RequestId), Instant>,
/// Stores the next `RequestId` we should include on an outgoing `RPCRequest` to a `PeerId`.
outgoing_request_ids: HashMap<PeerId, RequestId>,
/// The `MessageHandler` logger.
log: slog::Logger,
}
impl NetworkContext {
pub fn new(network_send: crossbeam_channel::Sender<NetworkMessage>, log: slog::Logger) -> Self {
Self {
network_send,
2019-03-25 05:48:44 +00:00
outstanding_outgoing_request_ids: HashMap::new(),
outgoing_request_ids: HashMap::new(),
log,
}
}
2019-03-25 04:30:46 +00:00
pub fn disconnect(&mut self, peer_id: PeerId, reason: GoodbyeReason) {
self.send_rpc_request(peer_id, RPCRequest::Goodbye(reason))
2019-03-21 07:21:50 +00:00
// TODO: disconnect peers.
}
pub fn send_rpc_request(&mut self, peer_id: PeerId, rpc_request: RPCRequest) {
let id = self.generate_request_id(&peer_id);
2019-03-25 05:48:44 +00:00
self.outstanding_outgoing_request_ids
.insert((peer_id.clone(), id.clone()), Instant::now());
self.send_rpc_event(
peer_id,
RPCEvent::Request {
id,
method_id: rpc_request.method_id(),
body: rpc_request,
},
);
}
2019-03-25 05:48:44 +00:00
pub fn send_rpc_response(
&mut self,
peer_id: PeerId,
request_id: RequestId,
rpc_response: RPCResponse,
) {
self.send_rpc_event(
peer_id,
RPCEvent::Response {
2019-03-25 05:48:44 +00:00
id: request_id,
method_id: rpc_response.method_id(),
result: rpc_response,
},
);
}
fn send_rpc_event(&self, peer_id: PeerId, rpc_event: RPCEvent) {
self.send(peer_id, OutgoingMessage::RPC(rpc_event))
}
fn send(&self, peer_id: PeerId, outgoing_message: OutgoingMessage) {
self.network_send
.send(NetworkMessage::Send(peer_id, outgoing_message))
.unwrap_or_else(|_| {
warn!(
self.log,
"Could not send RPC message to the network service"
)
});
//
}
2019-03-19 10:44:52 +00:00
2019-03-25 05:48:44 +00:00
/// Returns the next `RequestId` for sending an `RPCRequest` to the `peer_id`.
fn generate_request_id(&mut self, peer_id: &PeerId) -> RequestId {
let next_id = self
.outgoing_request_ids
.entry(peer_id.clone())
.and_modify(|id| id.increment())
.or_insert_with(|| RequestId::from(1));
next_id.previous()
}
}