From 41abdb7599168ba760d78e4c36b76ea8c991392b Mon Sep 17 00:00:00 2001 From: Age Manning Date: Tue, 19 Mar 2019 00:05:06 +1100 Subject: [PATCH] Remove sync crate, move into network crate --- Cargo.toml | 1 - beacon_node/beacon_chain/src/lib.rs | 1 + beacon_node/client/Cargo.toml | 1 - beacon_node/network/Cargo.toml | 1 - beacon_node/network/src/beacon_chain.rs | 8 +++++++- beacon_node/network/src/lib.rs | 1 + beacon_node/network/src/message_handler.rs | 11 ++++------- .../{sync/src/lib.rs => network/src/sync/mod.rs} | 0 .../src => network/src/sync}/simple_sync.rs | 16 +++++++++++----- beacon_node/sync/Cargo.toml | 9 --------- 10 files changed, 24 insertions(+), 25 deletions(-) rename beacon_node/{sync/src/lib.rs => network/src/sync/mod.rs} (100%) rename beacon_node/{sync/src => network/src/sync}/simple_sync.rs (54%) delete mode 100644 beacon_node/sync/Cargo.toml diff --git a/Cargo.toml b/Cargo.toml index 89158542e..d34f6fd30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ members = [ "beacon_node/client", "beacon_node/network", "beacon_node/rpc", - "beacon_node/sync", "beacon_node/version", "beacon_node/beacon_chain", "beacon_node/beacon_chain/test_harness", diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 5acac6ff2..2137c0edf 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -11,3 +11,4 @@ pub use db; pub use fork_choice; pub use parking_lot; pub use slot_clock; +pub use types; diff --git a/beacon_node/client/Cargo.toml b/beacon_node/client/Cargo.toml index 8914a9e7e..11453e4b8 100644 --- a/beacon_node/client/Cargo.toml +++ b/beacon_node/client/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] beacon_chain = { path = "../beacon_chain" } network = { path = "../network" } -sync = { path = "../sync" } db = { path = "../db" } fork_choice = { path = "../../eth2/fork_choice" } types = { path = "../../eth2/types" } diff --git a/beacon_node/network/Cargo.toml b/beacon_node/network/Cargo.toml index f1a7ed258..8b87a9d50 100644 --- a/beacon_node/network/Cargo.toml +++ b/beacon_node/network/Cargo.toml @@ -9,7 +9,6 @@ beacon_chain = { path = "../beacon_chain" } 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" diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 5e0857f47..5e9857c09 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -1,11 +1,13 @@ use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::{ db::ClientDB, fork_choice::ForkChoice, parking_lot::RwLockReadGuard, slot_clock::SlotClock, - CheckPoint, + types::ChainSpec, CheckPoint, }; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { + fn get_spec(&self) -> &ChainSpec; + fn head(&self) -> RwLockReadGuard; fn finalized_head(&self) -> RwLockReadGuard; @@ -17,6 +19,10 @@ where U: SlotClock, F: ForkChoice, { + fn get_spec(&self) -> &ChainSpec { + &self.spec + } + fn head(&self) -> RwLockReadGuard { self.head() } diff --git a/beacon_node/network/src/lib.rs b/beacon_node/network/src/lib.rs index dca83bb77..c1840f592 100644 --- a/beacon_node/network/src/lib.rs +++ b/beacon_node/network/src/lib.rs @@ -4,6 +4,7 @@ pub mod error; mod message_handler; mod messages; mod service; +pub mod sync; pub use libp2p::NetworkConfig; pub use messages::NodeMessage; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 02234f326..7e5a74a10 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -2,16 +2,15 @@ use crate::beacon_chain::BeaconChain; use crate::error; use crate::messages::NodeMessage; use crate::service::NetworkMessage; +use crate::sync::SimpleSync; use crossbeam_channel::{unbounded as channel, Sender}; use futures::future; use futures::prelude::*; -use libp2p::rpc; use libp2p::{PeerId, RPCEvent}; use slog::debug; use std::collections::HashMap; use std::sync::Arc; use std::time::{Duration, Instant}; -use sync::SimpleSync; use types::Hash256; /// Timeout for establishing a HELLO handshake. @@ -57,13 +56,11 @@ impl MessageHandler { let (handler_send, handler_recv) = channel(); // 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); + let sync = SimpleSync::new(beacon_chain.clone()); + let mut handler = MessageHandler { + // TODO: The handler may not need a chain, perhaps only sync? chain: beacon_chain.clone(), sync, network_send, diff --git a/beacon_node/sync/src/lib.rs b/beacon_node/network/src/sync/mod.rs similarity index 100% rename from beacon_node/sync/src/lib.rs rename to beacon_node/network/src/sync/mod.rs diff --git a/beacon_node/sync/src/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs similarity index 54% rename from beacon_node/sync/src/simple_sync.rs rename to beacon_node/network/src/sync/simple_sync.rs index 01a6a1adf..4034c63c9 100644 --- a/beacon_node/sync/src/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,11 +1,15 @@ +use crate::beacon_chain::BeaconChain; use libp2p::PeerId; use std::collections::HashMap; -use types::{Hash256, Slot}; +use std::sync::Arc; +use types::{Epoch, Hash256, Slot}; /// Keeps track of syncing information for known connected peers. pub struct PeerSyncInfo { + latest_finalized_root: Hash256, + latest_finalized_epoch: Epoch, + best_root: Hash256, best_slot: Slot, - best_slot_hash: Hash256, } /// The current syncing state. @@ -16,18 +20,20 @@ pub enum SyncState { } /// Simple Syncing protocol. +//TODO: Decide for HELLO messages whether its better to keep current in RAM or build on the fly +//when asked. pub struct SimpleSync { - genesis_hash: Hash256, known_peers: HashMap, state: SyncState, + network_id: u8, } impl SimpleSync { - pub fn new(genesis_hash: Hash256) -> Self { + pub fn new(beacon_chain: Arc) -> Self { SimpleSync { - genesis_hash, known_peers: HashMap::new(), state: SyncState::Idle, + network_id: beacon_chain.get_spec().network_id, } } } diff --git a/beacon_node/sync/Cargo.toml b/beacon_node/sync/Cargo.toml deleted file mode 100644 index a4ebe3eed..000000000 --- a/beacon_node/sync/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "sync" -version = "0.1.0" -authors = ["Age Manning "] -edition = "2018" - -[dependencies] -types = { path = "../../eth2/types" } -libp2p = { path = "../libp2p" }