diff --git a/substrate/core/cli/src/lib.rs b/substrate/core/cli/src/lib.rs index fc4ab029aa..8aec489457 100644 --- a/substrate/core/cli/src/lib.rs +++ b/substrate/core/cli/src/lib.rs @@ -60,7 +60,7 @@ use service::{ ServiceFactory, FactoryFullConfiguration, RuntimeGenesis, FactoryGenesis, PruningMode, ChainSpec, }; -use network::{NonReservedPeerMode, Protocol}; +use network::{Protocol, config::NonReservedPeerMode}; use primitives::H256; use std::io::{Write, Read, stdin, stdout}; diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index e55756eacf..6e993f1879 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -405,7 +405,7 @@ mod tests { use network::test::*; use network::test::{Block as TestBlock, PeersClient}; use runtime_primitives::traits::Block as BlockT; - use network::ProtocolConfig; + use network::config::ProtocolConfig; use parking_lot::Mutex; use tokio::runtime::current_thread; use keyring::Keyring; diff --git a/substrate/core/network/src/config.rs b/substrate/core/network/src/config.rs index fc6a1e374b..a7936ce491 100644 --- a/substrate/core/network/src/config.rs +++ b/substrate/core/network/src/config.rs @@ -14,9 +14,34 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -pub use service::Roles; +//! Configuration for the networking layer of Substrate. -/// Protocol configuration +pub use network_libp2p::{NonReservedPeerMode, NetworkConfiguration}; + +use chain::Client; +use codec; +use on_demand::OnDemandService; +use runtime_primitives::traits::{Block as BlockT}; +use service::{ExHashT, TransactionPool}; +use std::sync::Arc; + +/// Service initialization parameters. +pub struct Params { + /// Configuration. + pub config: ProtocolConfig, + /// Network layer configuration. + pub network_config: NetworkConfiguration, + /// Substrate relay chain access point. + pub chain: Arc>, + /// On-demand service reference. + pub on_demand: Option>>, + /// Transaction pool. + pub transaction_pool: Arc>, + /// Protocol specialization. + pub specialization: S, +} + +/// Configuration for the Substrate-specific part of the networking layer. #[derive(Clone)] pub struct ProtocolConfig { /// Assigned roles. @@ -30,3 +55,29 @@ impl Default for ProtocolConfig { } } } + +bitflags! { + /// Bitmask of the roles that a node fulfills. + pub struct Roles: u8 { + /// No network. + const NONE = 0b00000000; + /// Full node, does not participate in consensus. + const FULL = 0b00000001; + /// Light client node. + const LIGHT = 0b00000010; + /// Act as an authority + const AUTHORITY = 0b00000100; + } +} + +impl codec::Encode for Roles { + fn encode_to(&self, dest: &mut T) { + dest.push_byte(self.bits()) + } +} + +impl codec::Decode for Roles { + fn decode(input: &mut I) -> Option { + Self::from_bits(input.read_byte()?) + } +} diff --git a/substrate/core/network/src/consensus_gossip.rs b/substrate/core/network/src/consensus_gossip.rs index 0ecb0e5e68..882d2f02fd 100644 --- a/substrate/core/network/src/consensus_gossip.rs +++ b/substrate/core/network/src/consensus_gossip.rs @@ -26,8 +26,8 @@ use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Hash, HashF use runtime_primitives::generic::BlockId; use message::generic::{Message, ConsensusMessage}; use protocol::Context; -use service::Roles; -use specialization::Specialization; +use config::Roles; +use specialization::NetworkSpecialization; use StatusMessage; use generic_message; @@ -262,7 +262,7 @@ where } } -impl Specialization for ConsensusGossip where +impl NetworkSpecialization for ConsensusGossip where Block::Header: HeaderT { fn status(&self) -> Vec { diff --git a/substrate/core/network/src/lib.rs b/substrate/core/network/src/lib.rs index fa9232970f..a8ad20496f 100644 --- a/substrate/core/network/src/lib.rs +++ b/substrate/core/network/src/lib.rs @@ -51,10 +51,10 @@ mod sync; #[macro_use] mod protocol; mod io; -mod config; mod chain; mod blocks; mod on_demand; +pub mod config; pub mod import_queue; pub mod consensus_gossip; pub mod error; @@ -65,13 +65,12 @@ pub mod specialization; pub mod test; pub use chain::Client as ClientHandle; -pub use service::{Service, FetchFuture, TransactionPool, Params, ManageNetwork, SyncProvider}; +pub use service::{Service, FetchFuture, TransactionPool, ManageNetwork, SyncProvider}; pub use protocol::{ProtocolStatus, PeerInfo, Context}; pub use sync::{Status as SyncStatus, SyncState}; -pub use network_libp2p::{NonReservedPeerMode, NetworkConfiguration, NodeIndex, ProtocolId, Severity, Protocol}; +pub use network_libp2p::{NodeIndex, ProtocolId, Severity, Protocol}; pub use message::{generic as generic_message, RequestId, Status as StatusMessage}; pub use error::Error; -pub use config::{Roles, ProtocolConfig}; pub use on_demand::{OnDemand, OnDemandService, RemoteResponse}; #[doc(hidden)] pub use runtime_primitives::traits::Block as BlockT; diff --git a/substrate/core/network/src/message.rs b/substrate/core/network/src/message.rs index f08fce3368..40cd9a435d 100644 --- a/substrate/core/network/src/message.rs +++ b/substrate/core/network/src/message.rs @@ -125,7 +125,7 @@ pub struct RemoteReadResponse { /// Generic types. pub mod generic { use runtime_primitives::Justification; - use service::Roles; + use config::Roles; use super::{ BlockAttributes, RemoteCallResponse, RemoteReadResponse, RequestId, Transactions, Direction diff --git a/substrate/core/network/src/on_demand.rs b/substrate/core/network/src/on_demand.rs index bc0f3aec9c..5fbf893714 100644 --- a/substrate/core/network/src/on_demand.rs +++ b/substrate/core/network/src/on_demand.rs @@ -30,6 +30,7 @@ use client::light::fetcher::{Fetcher, FetchChecker, RemoteHeaderRequest, use io::SyncIo; use message; use network_libp2p::{Severity, NodeIndex}; +use config::Roles; use service; use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor}; @@ -41,7 +42,7 @@ const RETRY_COUNT: usize = 1; /// On-demand service API. pub trait OnDemandService: Send + Sync { /// When new node is connected. - fn on_connect(&self, peer: NodeIndex, role: service::Roles, best_number: NumberFor); + fn on_connect(&self, peer: NodeIndex, role: Roles, best_number: NumberFor); /// When block is announced by the peer. fn on_block_announce(&self, peer: NodeIndex, best_number: NumberFor); @@ -211,8 +212,8 @@ impl OnDemandService for OnDemand where E: service::ExecuteInContext, B::Header: HeaderT, { - fn on_connect(&self, peer: NodeIndex, role: service::Roles, best_number: NumberFor) { - if !role.intersects(service::Roles::FULL | service::Roles::AUTHORITY) { // TODO: correct? + fn on_connect(&self, peer: NodeIndex, role: Roles, best_number: NumberFor) { + if !role.intersects(Roles::FULL | Roles::AUTHORITY) { // TODO: correct? return; } @@ -511,9 +512,10 @@ pub mod tests { use client::{self, error::{ErrorKind as ClientErrorKind, Result as ClientResult}}; use client::light::fetcher::{Fetcher, FetchChecker, RemoteHeaderRequest, RemoteCallRequest, RemoteReadRequest, RemoteChangesRequest}; + use config::Roles; use message; use network_libp2p::NodeIndex; - use service::{Roles, ExecuteInContext}; + use service::ExecuteInContext; use test::TestIo; use super::{REQUEST_TIMEOUT, OnDemand, OnDemandService}; use test_client::runtime::{changes_trie_config, Block, Header}; diff --git a/substrate/core/network/src/protocol.rs b/substrate/core/network/src/protocol.rs index 4e98ee8f80..af20a6b1bc 100644 --- a/substrate/core/network/src/protocol.rs +++ b/substrate/core/network/src/protocol.rs @@ -27,11 +27,11 @@ use codec::{Encode, Decode}; use message::{self, Message}; use message::generic::Message as GenericMessage; -use specialization::Specialization; +use specialization::NetworkSpecialization; use sync::{ChainSync, Status as SyncStatus, SyncState}; -use service::{Roles, TransactionPool, ExHashT}; +use service::{TransactionPool, ExHashT}; use import_queue::ImportQueue; -use config::ProtocolConfig; +use config::{ProtocolConfig, Roles}; use chain::Client; use on_demand::OnDemandService; use io::SyncIo; @@ -50,7 +50,7 @@ const MAX_BLOCK_DATA_RESPONSE: u32 = 128; const LIGHT_MAXIMAL_BLOCKS_DIFFERENCE: u64 = 8192; // Lock must always be taken in order declared here. -pub struct Protocol, H: ExHashT> { +pub struct Protocol, H: ExHashT> { config: ProtocolConfig, on_demand: Option>>, genesis_hash: B::Hash, @@ -184,7 +184,7 @@ pub(crate) struct ContextData { pub chain: Arc>, } -impl, H: ExHashT> Protocol { +impl, H: ExHashT> Protocol { /// Create a new instance. pub fn new>( config: ProtocolConfig, @@ -761,7 +761,7 @@ macro_rules! construct_simple_protocol { } } - impl $crate::specialization::Specialization<$block> for $protocol { + impl $crate::specialization::NetworkSpecialization<$block> for $protocol { fn status(&self) -> Vec { $( let status = self.$status_protocol_name.status(); diff --git a/substrate/core/network/src/service.rs b/substrate/core/network/src/service.rs index 6d38d4484f..92c9beea94 100644 --- a/substrate/core/network/src/service.rs +++ b/substrate/core/network/src/service.rs @@ -25,11 +25,9 @@ use network_libp2p::{start_service, Service as NetworkService, ServiceEvent as N use network_libp2p::{RegisteredProtocol, parse_str_addr, Protocol as Libp2pProtocol}; use io::NetSyncIo; use protocol::{self, Protocol, ProtocolContext, Context, ProtocolStatus}; -use config::{ProtocolConfig}; +use config::Params; use error::Error; -use chain::Client; -use specialization::Specialization; -use on_demand::OnDemandService; +use specialization::NetworkSpecialization; use import_queue::ImportQueue; use runtime_primitives::traits::{Block as BlockT}; use tokio::{runtime::Runtime, timer::Interval}; @@ -40,38 +38,10 @@ pub type FetchFuture = oneshot::Receiver>; const TICK_TIMEOUT: Duration = Duration::from_millis(1000); const PROPAGATE_TIMEOUT: Duration = Duration::from_millis(5000); -bitflags! { - /// Node roles bitmask. - pub struct Roles: u8 { - /// No network. - const NONE = 0b00000000; - /// Full node, does not participate in consensus. - const FULL = 0b00000001; - /// Light client node. - const LIGHT = 0b00000010; - /// Act as an authority - const AUTHORITY = 0b00000100; - } -} - -impl ::codec::Encode for Roles { - fn encode_to(&self, dest: &mut T) { - dest.push_byte(self.bits()) - } -} - -impl ::codec::Decode for Roles { - fn decode(input: &mut I) -> Option { - Self::from_bits(input.read_byte()?) - } -} - /// Sync status pub trait SyncProvider: Send + Sync { /// Get sync status fn status(&self) -> ProtocolStatus; - /// Get this node id if available. - fn node_id(&self) -> Option; } pub trait ExHashT: ::std::hash::Hash + Eq + ::std::fmt::Debug + Clone + Send + Sync + 'static {} @@ -93,24 +63,8 @@ pub trait ExecuteInContext: Send + Sync { fn execute_in_context)>(&self, closure: F); } -/// Service initialization parameters. -pub struct Params { - /// Configuration. - pub config: ProtocolConfig, - /// Network layer configuration. - pub network_config: NetworkConfiguration, - /// Substrate relay chain access point. - pub chain: Arc>, - /// On-demand service reference. - pub on_demand: Option>>, - /// Transaction pool. - pub transaction_pool: Arc>, - /// Protocol specialization. - pub specialization: S, -} - /// Substrate network service. Handles network IO and manages connectivity. -pub struct Service, H: ExHashT> { +pub struct Service, H: ExHashT> { /// Network service network: Arc>, /// Protocol handler @@ -123,7 +77,7 @@ pub struct Service, H: ExHashT> { bg_thread: Option<(oneshot::Sender<()>, thread::JoinHandle<()>)>, } -impl, H: ExHashT> Service { +impl, H: ExHashT> Service { /// Creates and register protocol with the network service pub fn new>( params: Params, @@ -178,13 +132,13 @@ impl, H: ExHashT> Service { } } -impl, H: ExHashT> ::consensus::SyncOracle for Service { +impl, H: ExHashT> ::consensus::SyncOracle for Service { fn is_major_syncing(&self) -> bool { self.handler.sync().read().status().is_major_syncing() } } -impl, H:ExHashT> Drop for Service { +impl, H:ExHashT> Drop for Service { fn drop(&mut self) { self.handler.stop(); if let Some((sender, join)) = self.bg_thread.take() { @@ -196,30 +150,17 @@ impl, H:ExHashT> Drop for Service, H: ExHashT> ExecuteInContext for Service { +impl, H: ExHashT> ExecuteInContext for Service { fn execute_in_context)>(&self, closure: F) { closure(&mut ProtocolContext::new(self.handler.context_data(), &mut NetSyncIo::new(&self.network, self.protocol_id))) } } -impl, H: ExHashT> SyncProvider for Service { +impl, H: ExHashT> SyncProvider for Service { /// Get sync status fn status(&self) -> ProtocolStatus { self.handler.status() } - - fn node_id(&self) -> Option { - let network = self.network.lock(); - let ret = network - .listeners() - .next() - .map(|addr| { - let mut addr = addr.clone(); - addr.append(Libp2pProtocol::P2p(network.peer_id().clone().into())); - addr.to_string() - }); - ret - } } /// Trait for managing network @@ -232,9 +173,11 @@ pub trait ManageNetwork: Send + Sync { fn remove_reserved_peer(&self, peer: PeerId); /// Add reserved peer fn add_reserved_peer(&self, peer: String) -> Result<(), String>; + /// Returns a user-friendly identifier of our node. + fn node_id(&self) -> Option; } -impl, H: ExHashT> ManageNetwork for Service { +impl, H: ExHashT> ManageNetwork for Service { fn accept_unreserved_peers(&self) { self.network.lock().accept_unreserved_peers(); } @@ -264,10 +207,23 @@ impl, H: ExHashT> ManageNetwork for Se self.network.lock().add_reserved_peer(addr, peer_id); Ok(()) } + + fn node_id(&self) -> Option { + let network = self.network.lock(); + let ret = network + .listeners() + .next() + .map(|addr| { + let mut addr = addr.clone(); + addr.append(Libp2pProtocol::P2p(network.peer_id().clone().into())); + addr.to_string() + }); + ret + } } /// Starts the background thread that handles the networking. -fn start_thread, H: ExHashT>( +fn start_thread, H: ExHashT>( config: NetworkConfiguration, protocol: Arc>, registered: RegisteredProtocol, @@ -308,7 +264,7 @@ fn start_thread, H: ExHashT>( } /// Runs the background thread that handles the networking. -fn run_thread, H: ExHashT>( +fn run_thread, H: ExHashT>( network_service: Arc>, protocol: Arc>, protocol_id: ProtocolId, diff --git a/substrate/core/network/src/specialization.rs b/substrate/core/network/src/specialization.rs index 70ad9e9b2e..d1cde8b33b 100644 --- a/substrate/core/network/src/specialization.rs +++ b/substrate/core/network/src/specialization.rs @@ -21,7 +21,7 @@ use runtime_primitives::traits::Block as BlockT; use protocol::Context; /// A specialization of the substrate network protocol. Handles events and sends messages. -pub trait Specialization: Send + Sync + 'static { +pub trait NetworkSpecialization: Send + Sync + 'static { /// Get the current specialization-status. fn status(&self) -> Vec; diff --git a/substrate/core/network/src/sync.rs b/substrate/core/network/src/sync.rs index e69b22fc57..1287beffe9 100644 --- a/substrate/core/network/src/sync.rs +++ b/substrate/core/network/src/sync.rs @@ -25,7 +25,7 @@ use blocks::{self, BlockCollection}; use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As, NumberFor}; use runtime_primitives::generic::BlockId; use message::{self, generic::Message as GenericMessage}; -use service::Roles; +use config::Roles; use import_queue::ImportQueue; // Maximum blocks to request in a single packet. diff --git a/substrate/core/network/src/test/mod.rs b/substrate/core/network/src/test/mod.rs index f5b70f2e9a..458f659949 100644 --- a/substrate/core/network/src/test/mod.rs +++ b/substrate/core/network/src/test/mod.rs @@ -35,7 +35,7 @@ use keyring::Keyring; use codec::Encode; use import_queue::{SyncImportQueue, PassThroughVerifier, Verifier}; use consensus::BlockOrigin; -use specialization::Specialization; +use specialization::NetworkSpecialization; use consensus_gossip::ConsensusGossip; use import_queue::ImportQueue; use service::ExecuteInContext; @@ -62,7 +62,7 @@ pub struct DummySpecialization { pub gossip: ConsensusGossip, } -impl Specialization for DummySpecialization { +impl NetworkSpecialization for DummySpecialization { fn status(&self) -> Vec { vec![] } fn on_connect(&mut self, ctx: &mut Context, peer_id: NodeIndex, status: ::message::Status) { diff --git a/substrate/core/network/src/test/sync.rs b/substrate/core/network/src/test/sync.rs index 0f9e407828..479b96ecc0 100644 --- a/substrate/core/network/src/test/sync.rs +++ b/substrate/core/network/src/test/sync.rs @@ -16,9 +16,9 @@ use client::backend::Backend; use client::blockchain::HeaderBackend as BlockchainHeaderBackend; +use config::Roles; use consensus::BlockOrigin; use sync::SyncState; -use Roles; use super::*; #[test] diff --git a/substrate/core/service/src/components.rs b/substrate/core/service/src/components.rs index cde3a5aecb..0325b75e9e 100644 --- a/substrate/core/service/src/components.rs +++ b/substrate/core/service/src/components.rs @@ -175,12 +175,12 @@ impl StartRPC for T where pub trait CreateNetworkParams { fn create_network_params( client: Arc, C::RuntimeApi>>, - roles: network::Roles, - network_config: network::NetworkConfiguration, + roles: network::config::Roles, + network_config: network::config::NetworkConfiguration, on_demand: Option, NetworkService>>>, transaction_pool_adapter: TransactionPoolAdapter, specialization: S, - ) -> network::Params, S, ComponentExHash>; + ) -> network::config::Params, S, ComponentExHash>; } impl CreateNetworkParams for T where @@ -188,14 +188,14 @@ impl CreateNetworkParams for T where { fn create_network_params( client: Arc, T::RuntimeApi>>, - roles: network::Roles, - network_config: network::NetworkConfiguration, + roles: network::config::Roles, + network_config: network::config::NetworkConfiguration, on_demand: Option, NetworkService>>>, transaction_pool_adapter: TransactionPoolAdapter, specialization: S, - ) -> network::Params, S, ComponentExHash> { - network::Params { - config: network::ProtocolConfig { roles }, + ) -> network::config::Params, S, ComponentExHash> { + network::config::Params { + config: network::config::ProtocolConfig { roles }, network_config, chain: client, on_demand: on_demand.map(|d| d as Arc>>), @@ -225,7 +225,7 @@ pub trait ServiceFactory: 'static + Sized { /// The type that implements the runtime API. type RuntimeApi: Send + Sync; /// Network protocol extensions. - type NetworkProtocol: network::specialization::Specialization; + type NetworkProtocol: network::specialization::NetworkSpecialization; /// Chain runtime. type RuntimeDispatch: NativeExecutionDispatch + Send + Sync + 'static; /// Extrinsic pool backend type for the full client. diff --git a/substrate/core/service/src/config.rs b/substrate/core/service/src/config.rs index 00866d9563..b91c26ed04 100644 --- a/substrate/core/service/src/config.rs +++ b/substrate/core/service/src/config.rs @@ -20,9 +20,8 @@ use std::net::SocketAddr; use transaction_pool; use chain_spec::ChainSpec; pub use client::ExecutionStrategy; -pub use network::Roles; -pub use network::NetworkConfiguration; pub use client_db::PruningMode; +pub use network::config::{NetworkConfiguration, Roles}; use runtime_primitives::BuildStorage; use serde::{Serialize, de::DeserializeOwned}; use target_info::Target; diff --git a/substrate/core/service/test/src/lib.rs b/substrate/core/service/test/src/lib.rs index a155b18b37..7ffa783e97 100644 --- a/substrate/core/service/test/src/lib.rs +++ b/substrate/core/service/test/src/lib.rs @@ -47,7 +47,8 @@ use service::{ Roles, FactoryExtrinsic, }; -use network::{NetworkConfiguration, NonReservedPeerMode, Protocol, SyncProvider, ManageNetwork}; +use network::{Protocol, SyncProvider, ManageNetwork}; +use network::config::{NetworkConfiguration, NonReservedPeerMode}; use sr_primitives::traits::As; use sr_primitives::generic::BlockId; use consensus::{ImportBlock, BlockImport};