2019-03-18 06:38:23 +00:00
|
|
|
use crate::beacon_chain::BeaconChain;
|
2019-03-04 07:31:01 +00:00
|
|
|
use crate::error;
|
|
|
|
use crate::messages::NodeMessage;
|
2019-03-18 06:38:23 +00:00
|
|
|
use crossbeam_channel::{unbounded as channel, Sender};
|
2019-03-17 10:49:56 +00:00
|
|
|
use futures::future;
|
|
|
|
use futures::prelude::*;
|
|
|
|
use libp2p::rpc;
|
2019-03-17 12:14:28 +00:00
|
|
|
use libp2p::{PeerId, RPCEvent};
|
2019-03-04 07:31:01 +00:00
|
|
|
use slog::debug;
|
2019-03-17 12:14:28 +00:00
|
|
|
use std::collections::HashMap;
|
2019-03-18 06:38:23 +00:00
|
|
|
use std::sync::Arc;
|
2019-03-17 12:14:28 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2019-03-04 07:31:01 +00:00
|
|
|
use sync::SimpleSync;
|
|
|
|
use types::Hash256;
|
2019-03-04 05:39:37 +00:00
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
/// Timeout for establishing a HELLO handshake.
|
|
|
|
const HELLO_TIMEOUT: Duration = Duration::from_secs(30);
|
|
|
|
|
2019-03-04 05:39:37 +00:00
|
|
|
/// Handles messages received from the network and client and organises syncing.
|
2019-03-18 06:38:23 +00:00
|
|
|
pub struct MessageHandler {
|
2019-03-18 05:16:54 +00:00
|
|
|
/// Currently loaded and initialised beacon chain.
|
2019-03-18 06:38:23 +00:00
|
|
|
chain: Arc<BeaconChain>,
|
2019-03-18 05:16:54 +00:00
|
|
|
/// The syncing framework.
|
2019-03-04 07:31:01 +00:00
|
|
|
sync: SimpleSync,
|
2019-03-18 05:16:54 +00:00
|
|
|
/// A mapping of peers we have sent a HELLO rpc request to.
|
2019-03-17 12:14:28 +00:00
|
|
|
hello_requests: HashMap<PeerId, Instant>,
|
2019-03-18 05:16:54 +00:00
|
|
|
/// The `MessageHandler` logger.
|
2019-03-17 10:49:56 +00:00
|
|
|
log: slog::Logger,
|
2019-03-04 05:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Types of messages the handler can receive.
|
2019-03-17 10:49:56 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-04 05:39:37 +00:00
|
|
|
pub enum HandlerMessage {
|
2019-03-17 12:14:28 +00:00
|
|
|
/// We have initiated a connection to a new peer.
|
|
|
|
PeerDialed(PeerId),
|
2019-03-04 05:39:37 +00:00
|
|
|
/// Peer has disconnected,
|
|
|
|
PeerDisconnected(PeerId),
|
|
|
|
/// A Node message has been received.
|
2019-03-04 07:31:01 +00:00
|
|
|
Message(PeerId, NodeMessage),
|
2019-03-17 10:49:56 +00:00
|
|
|
/// An RPC response/request has been received.
|
2019-03-17 12:14:28 +00:00
|
|
|
RPC(RPCEvent),
|
2019-03-04 07:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 06:38:23 +00:00
|
|
|
impl MessageHandler {
|
2019-03-04 07:31:01 +00:00
|
|
|
/// Initializes and runs the MessageHandler.
|
2019-03-17 10:49:56 +00:00
|
|
|
pub fn new(
|
2019-03-18 06:38:23 +00:00
|
|
|
beacon_chain: Arc<BeaconChain>,
|
2019-03-17 10:49:56 +00:00
|
|
|
executor: &tokio::runtime::TaskExecutor,
|
|
|
|
log: slog::Logger,
|
|
|
|
) -> error::Result<Sender<HandlerMessage>> {
|
2019-03-04 07:31:01 +00:00
|
|
|
debug!(log, "Service starting");
|
|
|
|
|
|
|
|
let (handler_send, handler_recv) = channel();
|
|
|
|
|
|
|
|
// Initialise sync and begin processing in thread
|
|
|
|
//TODO: Load genesis from BeaconChain
|
2019-03-18 05:16:54 +00:00
|
|
|
//TODO: Initialise beacon chain
|
2019-03-04 07:31:01 +00:00
|
|
|
let temp_genesis = Hash256::zero();
|
|
|
|
|
2019-03-17 10:49:56 +00:00
|
|
|
// generate the Message handler
|
|
|
|
let sync = SimpleSync::new(temp_genesis);
|
|
|
|
let mut handler = MessageHandler {
|
2019-03-18 06:38:23 +00:00
|
|
|
chain: beacon_chain.clone(),
|
2019-03-17 10:49:56 +00:00
|
|
|
sync,
|
2019-03-17 12:14:28 +00:00
|
|
|
hello_requests: HashMap::new(),
|
2019-03-17 10:49:56 +00:00
|
|
|
log: log.clone(),
|
|
|
|
};
|
2019-03-04 07:31:01 +00:00
|
|
|
|
2019-03-17 10:49:56 +00:00
|
|
|
// 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.");
|
2019-03-17 10:49:56 +00:00
|
|
|
})?);
|
|
|
|
}
|
|
|
|
}));
|
2019-03-04 07:31:01 +00:00
|
|
|
|
|
|
|
Ok(handler_send)
|
|
|
|
}
|
2019-03-17 10:49:56 +00:00
|
|
|
|
|
|
|
fn handle_message(&mut self, message: HandlerMessage) {
|
2019-03-18 05:16:54 +00:00
|
|
|
match message {
|
|
|
|
HandlerMessage::PeerDialed(peer_id) => self.send_hello(peer_id),
|
|
|
|
//TODO: Handle all messages
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-03-17 10:49:56 +00:00
|
|
|
}
|
2019-03-18 05:16:54 +00:00
|
|
|
|
|
|
|
/// Sends a HELLO RPC request to a newly connected peer.
|
|
|
|
fn send_hello(&self, peer_id: PeerId) {}
|
2019-03-04 05:39:37 +00:00
|
|
|
}
|