Implement skeleton network/sync framework.

This commit is contained in:
Age Manning
2019-03-04 18:31:01 +11:00
parent 3b8f29a914
commit b68adc1ae3
16 changed files with 147 additions and 39 deletions

View File

@@ -5,6 +5,11 @@ authors = ["Age Manning <Age@AgeManning.com>"]
edition = "2018"
[dependencies]
libp2p = { git = "../libp2p" }
libp2p = { path = "../libp2p" }
version = { path = "../version" }
types = { path = "../../eth2/types" }
sync = { path = "../sync" }
slog = "2.4.1"
futures = "0.1.25"
error-chain = "0.12.0"
crossbeam-channel = "0.3.8"

View File

@@ -1,6 +1,9 @@
/// This crate provides the network server for Lighthouse.
pub mod error;
mod message_handler;
mod messages;
mod network_config;
mod service;
pub use network_config::NetworkConfig;
pub use service::NetworkService;
pub use service::Service;

View File

@@ -1,8 +1,14 @@
use crate::node_message::NodeMessage;
use crate::error;
use crate::messages::NodeMessage;
use crossbeam_channel::{unbounded as channel, Sender};
use libp2p::PeerId;
use slog::debug;
use sync::SimpleSync;
use types::Hash256;
/// Handles messages received from the network and client and organises syncing.
pub struct MessageHandler {
sync: Syncer,
sync: SimpleSync,
//TODO: Implement beacon chain
//chain: BeaconChain
}
@@ -14,5 +20,25 @@ pub enum HandlerMessage {
/// Peer has disconnected,
PeerDisconnected(PeerId),
/// A Node message has been received.
Message(Peer, NodeMessage),
Message(PeerId, NodeMessage),
}
impl MessageHandler {
/// Initializes and runs the MessageHandler.
pub fn new(log: slog::Logger) -> error::Result<Sender<HandlerMessage>> {
debug!(log, "Service starting");
let (handler_send, handler_recv) = channel();
// Initialise sync and begin processing in thread
//TODO: Load genesis from BeaconChain
let temp_genesis = Hash256::zero();
let sync = SimpleSync::new(temp_genesis);
let handler = MessageHandler { sync };
// spawn handler thread
Ok(handler_send)
}
}

View File

@@ -1,27 +1,26 @@
use types::{H256,Slot}
use libp2p::PeerId;
use types::{Hash256, Slot};
/// Messages between nodes across the network.
pub enum NodeMessage {
Status(Status),
BlockRequest,
}
pub struct Status {
/// Current node version.
version: u8
/// Genesis Hash.
genesis_hash: H256
/// Best known slot number.
best_slot: Slot
/// Best known slot hash.
best_slot_hash: H256
/// Current node version.
version: u8,
/// Genesis Hash.
genesis_hash: Hash256,
/// Best known slot number.
best_slot: Slot,
/// Best known slot hash.
best_slot_hash: Hash256,
}
/// Types of messages that the network service can receive.
pub enum NetworkMessage {
/// Send a message to libp2p service.
//TODO: Define typing for messages accross the wire
Send(Node, Message),
//TODO: Define typing for messages across the wire
Send(PeerId, NodeMessage),
}

View File

@@ -1,4 +1,4 @@
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
use libp2p::{GossipsubConfig, GossipsubConfigBuilder};
use std::net::IpAddr;
use version;

View File

@@ -0,0 +1,41 @@
use crate::error;
use crate::message_handler::{HandlerMessage, MessageHandler};
use crate::messages::{NetworkMessage, NodeMessage};
use crate::NetworkConfig;
use crossbeam_channel::{unbounded as channel, Sender};
use futures::sync::oneshot;
use libp2p::Service as LibP2PService;
use slog::{debug, info, o, trace, warn, Logger};
use std::sync::{Arc, Mutex};
/// Service that handles communication between internal services and the libp2p network service.
pub struct Service {
//libp2p_service: Arc<Mutex<LibP2PService>>,
//libp2p_thread: oneshot::Sender<()>,
//message_handler: MessageHandler,
//message_handler_send: Sender<HandlerMessage>,
}
impl Service {
pub fn new(
config: NetworkConfig,
log: slog::Logger,
) -> error::Result<(Arc<Self>, Sender<NetworkMessage>)> {
debug!(log, "Service starting");
let (network_send, network_recv) = channel::<NetworkMessage>();
// launch message handler thread
let message_handler_log = log.new(o!("Service" => "MessageHandler"));
let message_handler_send = MessageHandler::new(message_handler_log);
// launch libp2p service
let libp2p_log = log.new(o!("Service" => "Libp2p"));
let libp2p_service = LibP2PService::new(libp2p_log);
// TODO: Spawn thread to handle libp2p messages and pass to message handler thread.
let network = Service {};
Ok((Arc::new(network), network_send))
}
}