Starts initialisation of beacon chain in the client

This commit is contained in:
Age Manning
2019-03-18 16:16:54 +11:00
parent 2e0c8e2e47
commit bbad4bfa19
10 changed files with 207 additions and 80 deletions

View File

@@ -5,6 +5,7 @@ authors = ["Age Manning <Age@AgeManning.com>"]
edition = "2018"
[dependencies]
beacon_chain = { path = "../beacon_chain" }
libp2p = { path = "../libp2p" }
version = { path = "../version" }
types = { path = "../../eth2/types" }

View File

@@ -1,5 +1,6 @@
use crate::error;
use crate::messages::NodeMessage;
use beacon_chain::BeaconChain;
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
use futures::future;
use futures::prelude::*;
@@ -7,6 +8,7 @@ use libp2p::rpc;
use libp2p::{PeerId, RPCEvent};
use slog::debug;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use sync::SimpleSync;
use types::Hash256;
@@ -15,12 +17,14 @@ use types::Hash256;
const HELLO_TIMEOUT: Duration = Duration::from_secs(30);
/// Handles messages received from the network and client and organises syncing.
pub struct MessageHandler {
pub struct MessageHandler<T: ClientTypes> {
/// Currently loaded and initialised beacon chain.
chain: BeaconChain<T::DB, T::SlotClock, T::ForkChoice>,
/// The syncing framework.
sync: SimpleSync,
//TODO: Implement beacon chain
//chain: BeaconChain
/// A mapping of peers we have sent a HELLO rpc request to
/// A mapping of peers we have sent a HELLO rpc request to.
hello_requests: HashMap<PeerId, Instant>,
/// The `MessageHandler` logger.
log: slog::Logger,
}
@@ -37,9 +41,10 @@ pub enum HandlerMessage {
RPC(RPCEvent),
}
impl MessageHandler {
impl<T: ClientTypes> MessageHandler<T> {
/// Initializes and runs the MessageHandler.
pub fn new(
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>>,
executor: &tokio::runtime::TaskExecutor,
log: slog::Logger,
) -> error::Result<Sender<HandlerMessage>> {
@@ -49,12 +54,13 @@ impl MessageHandler {
// Initialise sync and begin processing in thread
//TODO: Load genesis from BeaconChain
//TODO: Initialise beacon chain
let temp_genesis = Hash256::zero();
// generate the Message handler
let sync = SimpleSync::new(temp_genesis);
//TODO: Initialise beacon chain
let mut handler = MessageHandler {
chain: beacon_chain,
sync,
hello_requests: HashMap::new(),
log: log.clone(),
@@ -74,6 +80,13 @@ impl MessageHandler {
}
fn handle_message(&mut self, message: HandlerMessage) {
debug!(self.log, "Message received {:?}", message);
match message {
HandlerMessage::PeerDialed(peer_id) => self.send_hello(peer_id),
//TODO: Handle all messages
_ => {}
}
}
/// Sends a HELLO RPC request to a newly connected peer.
fn send_hello(&self, peer_id: PeerId) {}
}

View File

@@ -15,25 +15,28 @@ use libp2p::{Libp2pEvent, PeerId};
use slog::{debug, info, o, trace, warn, Logger};
use std::sync::{Arc, Mutex};
use tokio::runtime::TaskExecutor;
use client::ClientTypes;
/// Service that handles communication between internal services and the libp2p network service.
pub struct Service {
pub struct Service<T: ClientTypes> {
//libp2p_service: Arc<Mutex<LibP2PService>>,
libp2p_exit: oneshot::Sender<()>,
network_send: crossbeam_channel::Sender<NetworkMessage>,
//message_handler: MessageHandler,
//message_handler_send: Sender<HandlerMessage>,
PhantomData: T,
}
impl Service {
impl<T: ClientTypes> Service<T> {
pub fn new(
config: NetworkConfig,
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>,
config: &NetworkConfig,
executor: &TaskExecutor,
log: slog::Logger,
) -> error::Result<(Arc<Self>, Sender<NetworkMessage>)> {
// launch message handler thread
let message_handler_log = log.new(o!("Service" => "MessageHandler"));
let message_handler_send = MessageHandler::new(executor, message_handler_log)?;
let message_handler_send = MessageHandler::new(beacon_chain, executor, message_handler_log)?;
// launch libp2p service
let libp2p_log = log.new(o!("Service" => "Libp2p"));