From a70c62d0a72a592ad7470c0eb113a244a38b297f Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 26 May 2023 14:47:44 +0300 Subject: [PATCH 1/3] Move `sc-peerset` to `sc-network` --- Cargo.lock | 28 +-- Cargo.toml | 1 - client/network-gossip/Cargo.toml | 1 - client/network-gossip/src/bridge.rs | 3 +- client/network-gossip/src/state_machine.rs | 5 +- client/network/Cargo.toml | 4 +- client/network/common/Cargo.toml | 1 - client/network/common/src/lib.rs | 1 + client/network/common/src/sync.rs | 4 +- client/network/common/src/types.rs | 38 ++++ client/network/light/Cargo.toml | 1 - .../src/light_client_requests/handler.rs | 2 +- client/network/src/behaviour.rs | 3 +- client/network/src/lib.rs | 6 +- client/{peerset => network}/src/peer_store.rs | 8 +- .../src/lib.rs => network/src/peerset.rs} | 50 +---- client/network/src/protocol.rs | 31 +-- .../src/protocol/notifications/behaviour.rs | 191 +++++++++--------- .../src/protocol/notifications/tests.rs | 8 +- .../src/protocol_controller.rs | 147 ++++++++------ client/network/src/request_responses.rs | 8 +- client/network/src/service.rs | 2 +- client/network/src/service/traits.rs | 3 +- client/network/statement/Cargo.toml | 1 - client/network/statement/src/lib.rs | 2 +- client/network/sync/Cargo.toml | 1 - .../network/sync/src/block_request_handler.rs | 2 +- client/network/sync/src/engine.rs | 6 +- client/network/sync/src/lib.rs | 2 +- client/network/sync/src/service/mock.rs | 3 +- client/network/sync/src/service/network.rs | 5 +- .../network/sync/src/state_request_handler.rs | 2 +- .../fuzz.rs => network/test/src/peerset.rs} | 0 client/network/transactions/Cargo.toml | 1 - client/network/transactions/src/lib.rs | 2 +- client/offchain/Cargo.toml | 1 - client/offchain/src/api.rs | 2 +- client/offchain/src/lib.rs | 3 +- client/peerset/Cargo.toml | 30 --- client/peerset/README.md | 4 - 40 files changed, 287 insertions(+), 326 deletions(-) create mode 100644 client/network/common/src/types.rs rename client/{peerset => network}/src/peer_store.rs (98%) rename client/{peerset/src/lib.rs => network/src/peerset.rs} (89%) rename client/{peerset => network}/src/protocol_controller.rs (91%) rename client/{peerset/tests/fuzz.rs => network/test/src/peerset.rs} (100%) delete mode 100644 client/peerset/Cargo.toml delete mode 100644 client/peerset/README.md diff --git a/Cargo.lock b/Cargo.lock index 7bffd9e169159..236e70c33a8a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9390,6 +9390,7 @@ dependencies = [ "multistream-select", "parity-scale-codec", "parking_lot 0.12.1", + "partial_sort", "pin-project", "rand 0.8.5", "sc-block-builder", @@ -9398,7 +9399,6 @@ dependencies = [ "sc-network-common", "sc-network-light", "sc-network-sync", - "sc-peerset", "sc-utils", "serde", "serde_json", @@ -9420,6 +9420,7 @@ dependencies = [ "tokio-test", "tokio-util", "unsigned-varint", + "wasm-timer", "zeroize", ] @@ -9464,7 +9465,6 @@ dependencies = [ "parity-scale-codec", "prost-build", "sc-consensus", - "sc-peerset", "sc-utils", "serde", "smallvec", @@ -9491,7 +9491,6 @@ dependencies = [ "quickcheck", "sc-network", "sc-network-common", - "sc-peerset", "sp-runtime", "substrate-prometheus-endpoint", "substrate-test-runtime-client", @@ -9514,7 +9513,6 @@ dependencies = [ "sc-client-api", "sc-network", "sc-network-common", - "sc-peerset", "sp-blockchain", "sp-core", "sp-runtime", @@ -9534,7 +9532,6 @@ dependencies = [ "pin-project", "sc-network", "sc-network-common", - "sc-peerset", "sp-consensus", "sp-runtime", "sp-statement-store", @@ -9564,7 +9561,6 @@ dependencies = [ "sc-consensus", "sc-network", "sc-network-common", - "sc-peerset", "sc-utils", "smallvec", "sp-arithmetic", @@ -9624,7 +9620,6 @@ dependencies = [ "pin-project", "sc-network", "sc-network-common", - "sc-peerset", "sc-utils", "sp-consensus", "sp-runtime", @@ -9654,7 +9649,6 @@ dependencies = [ "sc-client-db", "sc-network", "sc-network-common", - "sc-peerset", "sc-transaction-pool", "sc-transaction-pool-api", "sc-utils", @@ -9670,24 +9664,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "sc-peerset" -version = "4.0.0-dev" -dependencies = [ - "futures", - "libp2p-identity", - "log", - "mockall", - "parking_lot 0.12.1", - "partial_sort", - "rand 0.8.5", - "sc-utils", - "serde_json", - "sp-arithmetic", - "sp-tracing", - "wasm-timer", -] - [[package]] name = "sc-proposer-metrics" version = "0.10.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 0ac6a9dfb57e4..39005a690899b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,6 @@ members = [ "client/network/sync", "client/network/test", "client/offchain", - "client/peerset", "client/allocator", "client/proposer-metrics", "client/rpc", diff --git a/client/network-gossip/Cargo.toml b/client/network-gossip/Cargo.toml index 40277c946a1d7..4eb51d6d23652 100644 --- a/client/network-gossip/Cargo.toml +++ b/client/network-gossip/Cargo.toml @@ -24,7 +24,6 @@ tracing = "0.1.29" prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../utils/prometheus" } sc-network = { version = "0.10.0-dev", path = "../network/" } sc-network-common = { version = "0.10.0-dev", path = "../network/common" } -sc-peerset = { version = "4.0.0-dev", path = "../peerset" } sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" } [dev-dependencies] diff --git a/client/network-gossip/src/bridge.rs b/client/network-gossip/src/bridge.rs index baf11c9e8649b..2cd4e18171568 100644 --- a/client/network-gossip/src/bridge.rs +++ b/client/network-gossip/src/bridge.rs @@ -21,9 +21,8 @@ use crate::{ Network, Syncing, Validator, }; -use sc_network::{event::Event, types::ProtocolName}; +use sc_network::{event::Event, types::ProtocolName, ReputationChange}; use sc_network_common::sync::SyncEvent; -use sc_peerset::ReputationChange; use futures::{ channel::mpsc::{channel, Receiver, Sender}, diff --git a/client/network-gossip/src/state_machine.rs b/client/network-gossip/src/state_machine.rs index ee65bd890e15b..24373cd402513 100644 --- a/client/network-gossip/src/state_machine.rs +++ b/client/network-gossip/src/state_machine.rs @@ -43,7 +43,7 @@ const REBROADCAST_INTERVAL: time::Duration = time::Duration::from_millis(750); pub(crate) const PERIODIC_MAINTENANCE_INTERVAL: time::Duration = time::Duration::from_millis(1100); mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Reputation change when a peer sends us a gossip message that we didn't know about. pub const GOSSIP_SUCCESS: Rep = Rep::new(1 << 4, "Successful gossip"); /// Reputation change when a peer sends us a gossip message that we already knew about. @@ -529,9 +529,8 @@ mod tests { use sc_network::{ config::MultiaddrWithPeerId, event::Event, NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers, NotificationSenderError, - NotificationSenderT as NotificationSender, + NotificationSenderT as NotificationSender, ReputationChange, }; - use sc_peerset::ReputationChange; use sp_runtime::{ testing::{Block as RawBlock, ExtrinsicWrapper, H256}, traits::NumberFor, diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index de4c4c14a2587..d2343c58c31f4 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -31,6 +31,7 @@ log = "0.4.17" lru = "0.8.1" mockall = "0.11.3" parking_lot = "0.12.1" +partial_sort = "0.2.0" pin-project = "1.0.12" rand = "0.8.5" serde = { version = "1.0.136", features = ["derive"] } @@ -44,7 +45,6 @@ sc-block-builder = { version = "0.10.0-dev", path = "../block-builder" } sc-client-api = { version = "4.0.0-dev", path = "../api" } sc-consensus = { version = "0.10.0-dev", path = "../consensus/common" } sc-network-common = { version = "0.10.0-dev", path = "./common" } -sc-peerset = { version = "4.0.0-dev", path = "../peerset" } sc-utils = { version = "4.0.0-dev", path = "../utils" } sp-arithmetic = { version = "6.0.0", path = "../../primitives/arithmetic" } sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" } @@ -55,9 +55,11 @@ sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" } # # When libp2p also enforces this version, we can get rid off this extra dep here. snow = "0.9.2" +wasm-timer = "0.2" [dev-dependencies] assert_matches = "1.3" +mockall = "0.11.3" multistream-select = "0.12.1" rand = "0.8.5" tempfile = "3.1.0" diff --git a/client/network/common/Cargo.toml b/client/network/common/Cargo.toml index d9769413b857f..4a8bdf00a215d 100644 --- a/client/network/common/Cargo.toml +++ b/client/network/common/Cargo.toml @@ -29,7 +29,6 @@ libp2p-identity = { version = "0.1.2", features = ["peerid"] } prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../../utils/prometheus" } smallvec = "1.8.0" sc-consensus = { version = "0.10.0-dev", path = "../../consensus/common" } -sc-peerset = { version = "4.0.0-dev", path = "../../peerset" } sc-utils = { version = "4.0.0-dev", path = "../../utils" } serde = { version = "1.0.136", features = ["derive"] } sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" } diff --git a/client/network/common/src/lib.rs b/client/network/common/src/lib.rs index f53590efd4c84..8149f5ea708cd 100644 --- a/client/network/common/src/lib.rs +++ b/client/network/common/src/lib.rs @@ -21,6 +21,7 @@ pub mod message; pub mod role; pub mod sync; +pub mod types; /// Minimum Requirements for a Hash within Networking pub trait ExHashT: std::hash::Hash + Eq + std::fmt::Debug + Clone + Send + Sync + 'static {} diff --git a/client/network/common/src/sync.rs b/client/network/common/src/sync.rs index 404a1aff91153..d84400c30e14d 100644 --- a/client/network/common/src/sync.rs +++ b/client/network/common/src/sync.rs @@ -22,7 +22,7 @@ pub mod message; pub mod metrics; pub mod warp; -use crate::role::Roles; +use crate::{role::Roles, types::ReputationChange}; use futures::Stream; use libp2p_identity::PeerId; @@ -106,7 +106,7 @@ pub struct SyncStatus { /// A peer did not behave as expected and should be reported. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct BadPeer(pub PeerId, pub sc_peerset::ReputationChange); +pub struct BadPeer(pub PeerId, pub ReputationChange); impl fmt::Display for BadPeer { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/client/network/common/src/types.rs b/client/network/common/src/types.rs new file mode 100644 index 0000000000000..d23a2236d556a --- /dev/null +++ b/client/network/common/src/types.rs @@ -0,0 +1,38 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +/// Description of a reputation adjustment for a node. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ReputationChange { + /// Reputation delta. + pub value: i32, + /// Reason for reputation change. + pub reason: &'static str, +} + +impl ReputationChange { + /// New reputation change with given delta and reason. + pub const fn new(value: i32, reason: &'static str) -> ReputationChange { + Self { value, reason } + } + + /// New reputation change that forces minimum possible reputation. + pub const fn new_fatal(reason: &'static str) -> ReputationChange { + Self { value: i32::MIN, reason } + } +} diff --git a/client/network/light/Cargo.toml b/client/network/light/Cargo.toml index ec2c2c077fc8c..40ba1aefc986a 100644 --- a/client/network/light/Cargo.toml +++ b/client/network/light/Cargo.toml @@ -29,7 +29,6 @@ sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" sc-client-api = { version = "4.0.0-dev", path = "../../api" } sc-network = { version = "0.10.0-dev", path = "../" } sc-network-common = { version = "0.10.0-dev", path = "../common" } -sc-peerset = { version = "4.0.0-dev", path = "../../peerset" } sp-core = { version = "7.0.0", path = "../../../primitives/core" } sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" } thiserror = "1.0" diff --git a/client/network/light/src/light_client_requests/handler.rs b/client/network/light/src/light_client_requests/handler.rs index 2a0047b40e784..8f2bcc7384b33 100644 --- a/client/network/light/src/light_client_requests/handler.rs +++ b/client/network/light/src/light_client_requests/handler.rs @@ -32,8 +32,8 @@ use sc_client_api::{BlockBackend, ProofProvider}; use sc_network::{ config::ProtocolId, request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig}, + ReputationChange, }; -use sc_peerset::ReputationChange; use sp_core::{ hexdisplay::HexDisplay, storage::{ChildInfo, ChildType, PrefixedStorageKey}, diff --git a/client/network/src/behaviour.rs b/client/network/src/behaviour.rs index ef967eee92686..a4c19c47c3a80 100644 --- a/client/network/src/behaviour.rs +++ b/client/network/src/behaviour.rs @@ -20,9 +20,11 @@ use crate::{ discovery::{DiscoveryBehaviour, DiscoveryConfig, DiscoveryOut}, event::DhtEvent, peer_info, + peerset::PeersetHandle, protocol::{CustomMessageOutcome, NotificationsSink, Protocol}, request_responses::{self, IfDisconnected, ProtocolConfig, RequestFailure}, types::ProtocolName, + ReputationChange, }; use bytes::Bytes; @@ -33,7 +35,6 @@ use libp2p::{ }; use sc_network_common::role::{ObservedRole, Roles}; -use sc_peerset::{PeersetHandle, ReputationChange}; use sp_runtime::traits::Block as BlockT; use std::{collections::HashSet, time::Duration}; diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index a66c187cacf7b..9f528b8bec38c 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -243,7 +243,10 @@ //! More precise usage details are still being worked on and will likely change in the future. mod behaviour; +mod peer_store; +mod peerset; mod protocol; +mod protocol_controller; mod service; pub mod config; @@ -267,6 +270,7 @@ pub use sc_network_common::{ warp::{WarpSyncPhase, WarpSyncProgress}, ExtendedPeerInfo, StateDownloadProgress, SyncEventStream, SyncState, SyncStatusProvider, }, + types::ReputationChange, }; pub use service::{ signature::Signature, @@ -281,8 +285,6 @@ pub use service::{ }; pub use types::ProtocolName; -pub use sc_peerset::ReputationChange; - /// The maximum allowed number of established connections per peer. /// /// Typically, and by design of the network behaviours in this crate, diff --git a/client/peerset/src/peer_store.rs b/client/network/src/peer_store.rs similarity index 98% rename from client/peerset/src/peer_store.rs rename to client/network/src/peer_store.rs index 4255118213209..59886c335784b 100644 --- a/client/peerset/src/peer_store.rs +++ b/client/network/src/peer_store.rs @@ -16,10 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use libp2p_identity::PeerId; +use libp2p::PeerId; use log::trace; use parking_lot::Mutex; use partial_sort::PartialSort; +use sc_network_common::types::ReputationChange; use std::{ cmp::{Ord, Ordering, PartialOrd}, collections::{hash_map::Entry, HashMap, HashSet}, @@ -29,7 +30,10 @@ use std::{ }; use wasm_timer::Delay; -use crate::{protocol_controller::ProtocolHandle, ReputationChange, LOG_TARGET}; +use crate::protocol_controller::ProtocolHandle; + +/// Log target for this file. +pub const LOG_TARGET: &str = "peerset"; /// We don't accept nodes whose reputation is under this value. pub const BANNED_THRESHOLD: i32 = 82 * (i32::MIN / 100); diff --git a/client/peerset/src/lib.rs b/client/network/src/peerset.rs similarity index 89% rename from client/peerset/src/lib.rs rename to client/network/src/peerset.rs index 8a5ed7ddcd134..fd57175dd77cd 100644 --- a/client/peerset/src/lib.rs +++ b/client/network/src/peerset.rs @@ -32,11 +32,10 @@ //! In addition, for each, set, the peerset also holds a list of reserved nodes towards which it //! will at all time try to maintain a connection with. -mod peer_store; -mod protocol_controller; - -use peer_store::{PeerStore, PeerStoreHandle, PeerStoreProvider}; -use protocol_controller::{ProtocolController, ProtocolHandle}; +use crate::{ + peer_store::{PeerStore, PeerStoreHandle, PeerStoreProvider}, + protocol_controller::{ProtocolController, ProtocolHandle}, +}; use futures::{ channel::oneshot, @@ -45,6 +44,7 @@ use futures::{ stream::Stream, }; use log::debug; +use sc_network_common::types::ReputationChange; use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use serde_json::json; use std::{ @@ -53,9 +53,7 @@ use std::{ task::{Context, Poll}, }; -pub use libp2p_identity::PeerId; - -pub use peer_store::BANNED_THRESHOLD; +use libp2p::PeerId; pub const LOG_TARGET: &str = "peerset"; @@ -97,27 +95,6 @@ impl From for usize { } } -/// Description of a reputation adjustment for a node. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ReputationChange { - /// Reputation delta. - pub value: i32, - /// Reason for reputation change. - pub reason: &'static str, -} - -impl ReputationChange { - /// New reputation change with given delta and reason. - pub const fn new(value: i32, reason: &'static str) -> ReputationChange { - Self { value, reason } - } - - /// New reputation change that forces minimum possible reputation. - pub const fn new_fatal(reason: &'static str) -> ReputationChange { - Self { value: i32::MIN, reason } - } -} - /// Shared handle to the peer set manager (PSM). Distributed around the code. #[derive(Debug, Clone)] pub struct PeersetHandle { @@ -262,8 +239,6 @@ pub struct Peerset { from_controllers: TracingUnboundedReceiver, /// Receiver for messages from the `PeersetHandle` and from `to_self`. from_handle: TracingUnboundedReceiver, - /// Sending side of `from_handle`. - to_self: TracingUnboundedSender, } impl Peerset { @@ -292,9 +267,9 @@ impl Peerset { let (protocol_handles, protocol_controllers): (Vec, Vec<_>) = controllers.into_iter().unzip(); - let (to_self, from_handle) = tracing_unbounded("mpsc_peerset_messages", 10_000); + let (tx, from_handle) = tracing_unbounded("mpsc_peerset_messages", 10_000); - let handle = PeersetHandle { tx: to_self.clone() }; + let handle = PeersetHandle { tx }; let protocol_controller_futures = join_all(protocol_controllers.into_iter().map(|c| c.run().boxed())); @@ -306,7 +281,6 @@ impl Peerset { protocol_controller_futures, from_controllers, from_handle, - to_self, }; (peerset, handle) @@ -338,14 +312,6 @@ impl Peerset { self.protocol_handles[set_id.0].dropped(peer_id); } - /// Reports an adjustment to the reputation of the given peer. - pub fn report_peer(&mut self, peer_id: PeerId, score_diff: ReputationChange) { - // We don't immediately perform the adjustments in order to have state consistency. We - // don't want the reporting here to take priority over messages sent using the - // `PeersetHandle`. - let _ = self.to_self.unbounded_send(Action::ReportPeer(peer_id, score_diff)); - } - /// Produces a JSON object containing the state of the peerset manager, for debugging purposes. pub fn debug_info(&mut self) -> serde_json::Value { // TODO: Check what info we can include here. diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index 5fbb518ce02ea..e57bc3e520be4 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -20,6 +20,7 @@ use crate::{ config::{self, NonReservedPeerMode}, error, types::ProtocolName, + ReputationChange, }; use bytes::Bytes; @@ -61,13 +62,13 @@ pub mod message; pub(crate) const BLOCK_ANNOUNCES_TRANSACTIONS_SUBSTREAM_SIZE: u64 = 16 * 1024 * 1024; /// Identifier of the peerset for the block announces protocol. -const HARDCODED_PEERSETS_SYNC: sc_peerset::SetId = sc_peerset::SetId::from(0); +const HARDCODED_PEERSETS_SYNC: crate::peerset::SetId = crate::peerset::SetId::from(0); /// Number of hardcoded peersets (the constants right above). Any set whose identifier is equal or /// superior to this value corresponds to a user-defined protocol. const NUM_HARDCODED_PEERSETS: usize = 1; mod rep { - use sc_peerset::ReputationChange as Rep; + use crate::ReputationChange as Rep; /// We received a message that failed to decode. pub const BAD_MESSAGE: Rep = Rep::new(-(1 << 12), "Bad message"); } @@ -78,7 +79,7 @@ type PendingSyncSubstreamValidation = // Lock must always be taken in order declared here. pub struct Protocol { /// Used to report reputation changes. - peerset_handle: sc_peerset::PeersetHandle, + peerset_handle: crate::peerset::PeersetHandle, /// Handles opening the unique substream and sending and receiving raw messages. behaviour: Notifications, /// List of notifications protocols that have been registered. @@ -89,7 +90,7 @@ pub struct Protocol { /// event to the outer layers, we also shouldn't propagate this "substream closed" event. To /// solve this, an entry is added to this map whenever an invalid handshake is received. /// Entries are removed when the corresponding "substream closed" is later received. - bad_handshake_substreams: HashSet<(PeerId, sc_peerset::SetId)>, + bad_handshake_substreams: HashSet<(PeerId, crate::peerset::SetId)>, /// Connected peers. peers: HashMap, sync_substream_validations: FuturesUnordered, @@ -105,7 +106,7 @@ impl Protocol { notification_protocols: Vec, block_announces_protocol: config::NonDefaultSetConfig, tx: TracingUnboundedSender>, - ) -> error::Result<(Self, sc_peerset::PeersetHandle, Vec<(PeerId, Multiaddr)>)> { + ) -> error::Result<(Self, crate::peerset::PeersetHandle, Vec<(PeerId, Multiaddr)>)> { let mut known_addresses = Vec::new(); let (peerset, peerset_handle) = { @@ -127,7 +128,7 @@ impl Protocol { } // Set number 0 is used for block announces. - sets.push(sc_peerset::SetConfig { + sets.push(crate::peerset::SetConfig { in_peers: network_config.default_peers_set.in_peers, out_peers: network_config.default_peers_set.out_peers, bootnodes, @@ -146,7 +147,7 @@ impl Protocol { let reserved_only = set_cfg.set_config.non_reserved_mode == NonReservedPeerMode::Deny; - sets.push(sc_peerset::SetConfig { + sets.push(crate::peerset::SetConfig { in_peers: set_cfg.set_config.in_peers, out_peers: set_cfg.set_config.out_peers, bootnodes: Vec::new(), @@ -155,7 +156,7 @@ impl Protocol { }); } - sc_peerset::Peerset::from_config(sc_peerset::PeersetConfig { sets }) + crate::peerset::Peerset::from_config(crate::peerset::PeersetConfig { sets }) }; let behaviour = { @@ -210,7 +211,7 @@ impl Protocol { pub fn disconnect_peer(&mut self, peer_id: &PeerId, protocol_name: ProtocolName) { if let Some(position) = self.notification_protocols.iter().position(|p| *p == protocol_name) { - self.behaviour.disconnect_peer(peer_id, sc_peerset::SetId::from(position)); + self.behaviour.disconnect_peer(peer_id, crate::peerset::SetId::from(position)); self.peers.remove(peer_id); } else { warn!(target: "sub-libp2p", "disconnect_peer() with invalid protocol name") @@ -228,7 +229,7 @@ impl Protocol { } /// Adjusts the reputation of a node. - pub fn report_peer(&self, who: PeerId, reputation: sc_peerset::ReputationChange) { + pub fn report_peer(&self, who: PeerId, reputation: ReputationChange) { self.peerset_handle.report_peer(who, reputation) } @@ -236,7 +237,7 @@ impl Protocol { pub fn set_notification_handshake(&mut self, protocol: ProtocolName, handshake: Vec) { if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) { self.behaviour - .set_notif_protocol_handshake(sc_peerset::SetId::from(index), handshake); + .set_notif_protocol_handshake(crate::peerset::SetId::from(index), handshake); } else { error!( target: "sub-libp2p", @@ -274,7 +275,8 @@ impl Protocol { /// Sets the list of reserved peers for the given protocol/peerset. pub fn set_reserved_peerset_peers(&self, protocol: ProtocolName, peers: HashSet) { if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) { - self.peerset_handle.set_reserved_peers(sc_peerset::SetId::from(index), peers); + self.peerset_handle + .set_reserved_peers(crate::peerset::SetId::from(index), peers); } else { error!( target: "sub-libp2p", @@ -287,7 +289,8 @@ impl Protocol { /// Removes a `PeerId` from the list of reserved peers. pub fn remove_set_reserved_peer(&self, protocol: ProtocolName, peer: PeerId) { if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) { - self.peerset_handle.remove_reserved_peer(sc_peerset::SetId::from(index), peer); + self.peerset_handle + .remove_reserved_peer(crate::peerset::SetId::from(index), peer); } else { error!( target: "sub-libp2p", @@ -300,7 +303,7 @@ impl Protocol { /// Adds a `PeerId` to the list of reserved peers. pub fn add_set_reserved_peer(&self, protocol: ProtocolName, peer: PeerId) { if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) { - self.peerset_handle.add_reserved_peer(sc_peerset::SetId::from(index), peer); + self.peerset_handle.add_reserved_peer(crate::peerset::SetId::from(index), peer); } else { error!( target: "sub-libp2p", diff --git a/client/network/src/protocol/notifications/behaviour.rs b/client/network/src/protocol/notifications/behaviour.rs index 8f1324dfa8c06..2861e3e0416a2 100644 --- a/client/network/src/protocol/notifications/behaviour.rs +++ b/client/network/src/protocol/notifications/behaviour.rs @@ -23,6 +23,7 @@ use crate::{ types::ProtocolName, }; +use crate::peerset::DropReason; use bytes::BytesMut; use fnv::FnvHashMap; use futures::{channel::oneshot, prelude::*}; @@ -38,7 +39,6 @@ use libp2p::{ use log::{debug, error, info, trace, warn}; use parking_lot::RwLock; use rand::distributions::{Distribution as _, Uniform}; -use sc_peerset::DropReason; use smallvec::SmallVec; use std::{ cmp, @@ -108,10 +108,10 @@ pub struct Notifications { notif_protocols: Vec, /// Receiver for instructions about who to connect to or disconnect from. - peerset: sc_peerset::Peerset, + peerset: crate::peerset::Peerset, /// List of peers in our state. - peers: FnvHashMap<(PeerId, sc_peerset::SetId), PeerState>, + peers: FnvHashMap<(PeerId, crate::peerset::SetId), PeerState>, /// The elements in `peers` occasionally contain `Delay` objects that we would normally have /// to be polled one by one. In order to avoid doing so, as an optimization, every `Delay` is @@ -121,7 +121,7 @@ pub struct Notifications { /// By design, we never remove elements from this list. Elements are removed only when the /// `Delay` triggers. As such, this stream may produce obsolete elements. delays: stream::FuturesUnordered< - Pin + Send>>, + Pin + Send>>, >, /// [`DelayId`] to assign to the next delay. @@ -133,7 +133,7 @@ pub struct Notifications { /// We generate indices to identify incoming connections. This is the next value for the index /// to use when a connection is incoming. - next_incoming_index: sc_peerset::IncomingIndex, + next_incoming_index: crate::peerset::IncomingIndex, /// Events to produce from `poll()`. events: VecDeque>, @@ -232,7 +232,7 @@ enum PeerState { backoff_until: Option, /// Incoming index tracking this connection. - incoming_index: sc_peerset::IncomingIndex, + incoming_index: crate::peerset::IncomingIndex, /// List of connections with this peer, and their state. connections: SmallVec<[(ConnectionId, ConnectionState); crate::MAX_CONNECTIONS_PER_PEER]>, @@ -296,12 +296,12 @@ struct IncomingPeer { /// Id of the remote peer of the incoming substream. peer_id: PeerId, /// Id of the set the incoming substream would belong to. - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, /// If true, this "incoming" still corresponds to an actual connection. If false, then the /// connection corresponding to it has been closed or replaced already. alive: bool, /// Id that the we sent to the peerset. - incoming_id: sc_peerset::IncomingIndex, + incoming_id: crate::peerset::IncomingIndex, } /// Event that can be emitted by the `Notifications`. @@ -312,7 +312,7 @@ pub enum NotificationsOut { /// Id of the peer we are connected to. peer_id: PeerId, /// Peerset set ID the substream is tied to. - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, /// If `Some`, a fallback protocol name has been used rather the main protocol name. /// Always matches one of the fallback names passed at initialization. negotiated_fallback: Option, @@ -332,7 +332,7 @@ pub enum NotificationsOut { /// Id of the peer we are connected to. peer_id: PeerId, /// Peerset set ID the substream is tied to. - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, /// Replacement for the previous [`NotificationsSink`]. notifications_sink: NotificationsSink, }, @@ -343,7 +343,7 @@ pub enum NotificationsOut { /// Id of the peer we were connected to. peer_id: PeerId, /// Peerset set ID the substream was tied to. - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, }, /// Receives a message on a custom protocol substream. @@ -353,7 +353,7 @@ pub enum NotificationsOut { /// Id of the peer the message came from. peer_id: PeerId, /// Peerset set ID the substream is tied to. - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, /// Message that has been received. message: BytesMut, }, @@ -362,7 +362,7 @@ pub enum NotificationsOut { impl Notifications { /// Creates a `CustomProtos`. pub fn new( - peerset: sc_peerset::Peerset, + peerset: crate::peerset::Peerset, notif_protocols: impl Iterator, ) -> Self { let notif_protocols = notif_protocols @@ -383,7 +383,7 @@ impl Notifications { delays: Default::default(), next_delay_id: DelayId(0), incoming: SmallVec::new(), - next_incoming_index: sc_peerset::IncomingIndex(0), + next_incoming_index: crate::peerset::IncomingIndex(0), events: VecDeque::new(), } } @@ -391,7 +391,7 @@ impl Notifications { /// Modifies the handshake of the given notifications protocol. pub fn set_notif_protocol_handshake( &mut self, - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, handshake_message: impl Into>, ) { if let Some(p) = self.notif_protocols.get_mut(usize::from(set_id)) { @@ -413,18 +413,18 @@ impl Notifications { } /// Returns true if we have an open substream to the given peer. - pub fn is_open(&self, peer_id: &PeerId, set_id: sc_peerset::SetId) -> bool { + pub fn is_open(&self, peer_id: &PeerId, set_id: crate::peerset::SetId) -> bool { self.peers.get(&(*peer_id, set_id)).map(|p| p.is_open()).unwrap_or(false) } /// Disconnects the given peer if we are connected to it. - pub fn disconnect_peer(&mut self, peer_id: &PeerId, set_id: sc_peerset::SetId) { + pub fn disconnect_peer(&mut self, peer_id: &PeerId, set_id: crate::peerset::SetId) { trace!(target: "sub-libp2p", "External API => Disconnect({}, {:?})", peer_id, set_id); self.disconnect_peer_inner(peer_id, set_id); } /// Inner implementation of `disconnect_peer`. - fn disconnect_peer_inner(&mut self, peer_id: &PeerId, set_id: sc_peerset::SetId) { + fn disconnect_peer_inner(&mut self, peer_id: &PeerId, set_id: crate::peerset::SetId) { let mut entry = if let Entry::Occupied(entry) = self.peers.entry((*peer_id, set_id)) { entry } else { @@ -541,7 +541,7 @@ impl Notifications { /// Returns the list of reserved peers. pub fn reserved_peers( &self, - set_id: sc_peerset::SetId, + set_id: crate::peerset::SetId, pending_response: oneshot::Sender>, ) { self.peerset.reserved_peers(set_id, pending_response); @@ -553,7 +553,7 @@ impl Notifications { } /// Function that is called when the peerset wants us to connect to a peer. - fn peerset_report_connect(&mut self, peer_id: PeerId, set_id: sc_peerset::SetId) { + fn peerset_report_connect(&mut self, peer_id: PeerId, set_id: crate::peerset::SetId) { // If `PeerId` is unknown to us, insert an entry, start dialing, and return early. let mut occ_entry = match self.peers.entry((peer_id, set_id)) { Entry::Occupied(entry) => entry, @@ -731,7 +731,7 @@ impl Notifications { } /// Function that is called when the peerset wants us to disconnect from a peer. - fn peerset_report_disconnect(&mut self, peer_id: PeerId, set_id: sc_peerset::SetId) { + fn peerset_report_disconnect(&mut self, peer_id: PeerId, set_id: crate::peerset::SetId) { let mut entry = match self.peers.entry((peer_id, set_id)) { Entry::Occupied(entry) => entry, Entry::Vacant(entry) => { @@ -839,7 +839,7 @@ impl Notifications { /// Function that is called when the peerset wants us to accept a connection /// request from a peer. - fn peerset_report_accept(&mut self, index: sc_peerset::IncomingIndex) { + fn peerset_report_accept(&mut self, index: crate::peerset::IncomingIndex) { let incoming = if let Some(pos) = self.incoming.iter().position(|i| i.incoming_id == index) { self.incoming.remove(pos) @@ -925,7 +925,7 @@ impl Notifications { } /// Function that is called when the peerset wants us to reject an incoming peer. - fn peerset_report_reject(&mut self, index: sc_peerset::IncomingIndex) { + fn peerset_report_reject(&mut self, index: crate::peerset::IncomingIndex) { let incoming = if let Some(pos) = self.incoming.iter().position(|i| i.incoming_id == index) { self.incoming.remove(pos) @@ -1059,7 +1059,7 @@ impl NetworkBehaviour for Notifications { connection_id, .. }) => { - for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) { + for set_id in (0..self.notif_protocols.len()).map(crate::peerset::SetId::from) { match self.peers.entry((peer_id, set_id)).or_insert(PeerState::Poisoned) { // Requested | PendingRequest => Enabled st @ &mut PeerState::Requested | @@ -1113,7 +1113,7 @@ impl NetworkBehaviour for Notifications { } }, FromSwarm::ConnectionClosed(ConnectionClosed { peer_id, connection_id, .. }) => { - for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) { + for set_id in (0..self.notif_protocols.len()).map(crate::peerset::SetId::from) { let mut entry = if let Entry::Occupied(entry) = self.peers.entry((peer_id, set_id)) { @@ -1405,7 +1405,7 @@ impl NetworkBehaviour for Notifications { if let Some(peer_id) = peer_id { trace!(target: "sub-libp2p", "Libp2p => Dial failure for {:?}", peer_id); - for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) { + for set_id in (0..self.notif_protocols.len()).map(crate::peerset::SetId::from) { if let Entry::Occupied(mut entry) = self.peers.entry((peer_id, set_id)) { match mem::replace(entry.get_mut(), PeerState::Poisoned) { // The peer is not in our list. @@ -1484,7 +1484,7 @@ impl NetworkBehaviour for Notifications { ) { match event { NotifsHandlerOut::OpenDesiredByRemote { protocol_index } => { - let set_id = sc_peerset::SetId::from(protocol_index); + let set_id = crate::peerset::SetId::from(protocol_index); trace!(target: "sub-libp2p", "Handler({:?}, {:?}]) => OpenDesiredByRemote({:?})", @@ -1674,7 +1674,7 @@ impl NetworkBehaviour for Notifications { }, NotifsHandlerOut::CloseDesired { protocol_index } => { - let set_id = sc_peerset::SetId::from(protocol_index); + let set_id = crate::peerset::SetId::from(protocol_index); trace!(target: "sub-libp2p", "Handler({}, {:?}) => CloseDesired({:?})", @@ -1774,7 +1774,7 @@ impl NetworkBehaviour for Notifications { }, NotifsHandlerOut::CloseResult { protocol_index } => { - let set_id = sc_peerset::SetId::from(protocol_index); + let set_id = crate::peerset::SetId::from(protocol_index); trace!(target: "sub-libp2p", "Handler({}, {:?}) => CloseResult({:?})", @@ -1813,7 +1813,7 @@ impl NetworkBehaviour for Notifications { notifications_sink, .. } => { - let set_id = sc_peerset::SetId::from(protocol_index); + let set_id = crate::peerset::SetId::from(protocol_index); trace!(target: "sub-libp2p", "Handler({}, {:?}) => OpenResultOk({:?})", peer_id, connection_id, set_id); @@ -1879,7 +1879,7 @@ impl NetworkBehaviour for Notifications { }, NotifsHandlerOut::OpenResultErr { protocol_index } => { - let set_id = sc_peerset::SetId::from(protocol_index); + let set_id = crate::peerset::SetId::from(protocol_index); trace!(target: "sub-libp2p", "Handler({:?}, {:?}) => OpenResultErr({:?})", peer_id, connection_id, set_id); @@ -1968,7 +1968,7 @@ impl NetworkBehaviour for Notifications { }, NotifsHandlerOut::Notification { protocol_index, message } => { - let set_id = sc_peerset::SetId::from(protocol_index); + let set_id = crate::peerset::SetId::from(protocol_index); if self.is_open(&peer_id, set_id) { trace!( target: "sub-libp2p", @@ -2014,16 +2014,16 @@ impl NetworkBehaviour for Notifications { // Note that the peerset is a *best effort* crate, and we have to use defensive programming. loop { match futures::Stream::poll_next(Pin::new(&mut self.peerset), cx) { - Poll::Ready(Some(sc_peerset::Message::Accept(index))) => { + Poll::Ready(Some(crate::peerset::Message::Accept(index))) => { self.peerset_report_accept(index); }, - Poll::Ready(Some(sc_peerset::Message::Reject(index))) => { + Poll::Ready(Some(crate::peerset::Message::Reject(index))) => { self.peerset_report_reject(index); }, - Poll::Ready(Some(sc_peerset::Message::Connect { peer_id, set_id, .. })) => { + Poll::Ready(Some(crate::peerset::Message::Connect { peer_id, set_id, .. })) => { self.peerset_report_connect(peer_id, set_id); }, - Poll::Ready(Some(sc_peerset::Message::Drop { peer_id, set_id, .. })) => { + Poll::Ready(Some(crate::peerset::Message::Drop { peer_id, set_id, .. })) => { self.peerset_report_disconnect(peer_id, set_id); }, Poll::Ready(None) => { @@ -2104,9 +2104,8 @@ impl NetworkBehaviour for Notifications { #[allow(deprecated)] mod tests { use super::*; - use crate::protocol::notifications::handler::tests::*; + use crate::{peerset::IncomingIndex, protocol::notifications::handler::tests::*}; use libp2p::swarm::AddressRecord; - use sc_peerset::IncomingIndex; use std::{collections::HashSet, iter}; impl PartialEq for ConnectionState { @@ -2152,11 +2151,11 @@ mod tests { } } - fn development_notifs() -> (Notifications, sc_peerset::PeersetHandle) { + fn development_notifs() -> (Notifications, crate::peerset::PeersetHandle) { let (peerset, peerset_handle) = { let mut sets = Vec::with_capacity(1); - sets.push(sc_peerset::SetConfig { + sets.push(crate::peerset::SetConfig { in_peers: 25, out_peers: 25, bootnodes: Vec::new(), @@ -2164,7 +2163,7 @@ mod tests { reserved_only: false, }); - sc_peerset::Peerset::from_config(sc_peerset::PeersetConfig { sets }) + crate::peerset::Peerset::from_config(crate::peerset::PeersetConfig { sets }) }; ( @@ -2310,7 +2309,7 @@ mod tests { assert!(std::matches!( notif.incoming.pop(), - Some(IncomingPeer { alive: true, incoming_id: sc_peerset::IncomingIndex(0), .. }), + Some(IncomingPeer { alive: true, incoming_id: crate::peerset::IncomingIndex(0), .. }), )); } @@ -2353,7 +2352,7 @@ mod tests { #[test] fn peerset_report_connect_backoff() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -2420,7 +2419,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2454,7 +2453,7 @@ mod tests { #[test] fn peerset_disconnect_disable_pending_enable() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -2503,7 +2502,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2538,7 +2537,7 @@ mod tests { fn peerset_disconnect_requested() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); // Set peer into `Requested` state. notif.peerset_report_connect(peer, set_id); @@ -2552,7 +2551,7 @@ mod tests { #[test] fn peerset_disconnect_pending_request() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -2607,7 +2606,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2634,17 +2633,17 @@ mod tests { assert!(std::matches!( notif.incoming[0], - IncomingPeer { alive: true, incoming_id: sc_peerset::IncomingIndex(0), .. }, + IncomingPeer { alive: true, incoming_id: crate::peerset::IncomingIndex(0), .. }, )); notif.disconnect_peer(&peer, set_id); assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Disabled { .. }))); assert!(std::matches!( notif.incoming[0], - IncomingPeer { alive: false, incoming_id: sc_peerset::IncomingIndex(0), .. }, + IncomingPeer { alive: false, incoming_id: crate::peerset::IncomingIndex(0), .. }, )); - notif.peerset_report_accept(sc_peerset::IncomingIndex(0)); + notif.peerset_report_accept(crate::peerset::IncomingIndex(0)); assert_eq!(notif.incoming.len(), 0); assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(PeerState::Disabled { .. }))); } @@ -2655,7 +2654,7 @@ mod tests { let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2709,7 +2708,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2743,7 +2742,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2779,7 +2778,7 @@ mod tests { assert!(notif.peers.get(&(peer, set_id)).is_none()); assert!(std::matches!( notif.incoming[0], - IncomingPeer { alive: false, incoming_id: sc_peerset::IncomingIndex(0), .. }, + IncomingPeer { alive: false, incoming_id: crate::peerset::IncomingIndex(0), .. }, )); } @@ -2789,7 +2788,7 @@ mod tests { let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let conn1 = ConnectionId::new_unchecked(1); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2857,7 +2856,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -2885,7 +2884,7 @@ mod tests { // We rely on the implementation detail that incoming indices are counted // from 0 to not mock the `Peerset`. - notif.peerset_report_accept(sc_peerset::IncomingIndex(0)); + notif.peerset_report_accept(crate::peerset::IncomingIndex(0)); assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Enabled { .. }))); // open new substream @@ -2912,7 +2911,7 @@ mod tests { let peer = PeerId::random(); let conn1 = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3005,7 +3004,7 @@ mod tests { fn dial_failure_for_requested_peer() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); // Set peer into `Requested` state. notif.peerset_report_connect(peer, set_id); @@ -3029,7 +3028,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3076,7 +3075,7 @@ mod tests { #[test] fn peerset_report_connect_backoff_expired() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -3125,7 +3124,7 @@ mod tests { fn peerset_report_disconnect_disabled() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), @@ -3150,7 +3149,7 @@ mod tests { #[test] fn peerset_report_disconnect_backoff() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -3196,7 +3195,7 @@ mod tests { #[test] fn peer_is_backed_off_if_both_connections_get_closed_while_peer_is_disabled_with_back_off() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn1 = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); @@ -3270,7 +3269,7 @@ mod tests { fn inject_connection_closed_incoming_with_backoff() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), @@ -3323,7 +3322,7 @@ mod tests { let peer = PeerId::random(); let conn1 = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3378,7 +3377,7 @@ mod tests { let peer = PeerId::random(); let conn1 = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3436,7 +3435,7 @@ mod tests { let peer = PeerId::random(); let conn1 = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3501,7 +3500,7 @@ mod tests { #[test] fn inject_dial_failure_for_pending_request() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -3565,7 +3564,7 @@ mod tests { fn peerstate_incoming_open_desired_by_remote() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let conn1 = ConnectionId::new_unchecked(0); let conn2 = ConnectionId::new_unchecked(1); let connected = ConnectedPoint::Listener { @@ -3619,7 +3618,7 @@ mod tests { async fn remove_backoff_peer_after_timeout() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), @@ -3698,7 +3697,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3816,7 +3815,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -3864,7 +3863,7 @@ mod tests { #[cfg(debug_assertions)] fn peerset_report_connect_with_disabled_pending_enable_peer() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -3911,7 +3910,7 @@ mod tests { fn peerset_report_connect_with_requested_peer() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); // Set peer into `Requested` state. notif.peerset_report_connect(peer, set_id); @@ -3926,7 +3925,7 @@ mod tests { #[cfg(debug_assertions)] fn peerset_report_connect_with_pending_requested() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -3984,7 +3983,7 @@ mod tests { fn peerset_report_connect_with_incoming_peer() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), @@ -4019,7 +4018,7 @@ mod tests { fn peerset_report_disconnect_with_incoming_peer() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), @@ -4056,7 +4055,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4083,11 +4082,11 @@ mod tests { assert!(std::matches!( notif.incoming[0], - IncomingPeer { alive: true, incoming_id: sc_peerset::IncomingIndex(0), .. }, + IncomingPeer { alive: true, incoming_id: crate::peerset::IncomingIndex(0), .. }, )); notif.peers.remove(&(peer, set_id)); - notif.peerset_report_accept(sc_peerset::IncomingIndex(0)); + notif.peerset_report_accept(crate::peerset::IncomingIndex(0)); } #[test] @@ -4097,7 +4096,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4125,7 +4124,7 @@ mod tests { assert!(std::matches!( notif.incoming[0], - IncomingPeer { alive: true, incoming_id: sc_peerset::IncomingIndex(0), .. }, + IncomingPeer { alive: true, incoming_id: crate::peerset::IncomingIndex(0), .. }, )); notif.peerset_report_connect(peer, set_id); @@ -4136,7 +4135,7 @@ mod tests { assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Enabled { .. }))); notif.incoming[0].alive = true; - notif.peerset_report_accept(sc_peerset::IncomingIndex(0)); + notif.peerset_report_accept(crate::peerset::IncomingIndex(0)); } #[test] @@ -4165,7 +4164,7 @@ mod tests { fn disconnect_non_existent_peer() { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); notif.peerset_report_disconnect(peer, set_id); @@ -4198,7 +4197,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4236,7 +4235,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4262,7 +4261,7 @@ mod tests { assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. }))); assert!(std::matches!( notif.incoming[0], - IncomingPeer { alive: true, incoming_id: sc_peerset::IncomingIndex(0), .. }, + IncomingPeer { alive: true, incoming_id: crate::peerset::IncomingIndex(0), .. }, )); notif.peers.remove(&(peer, set_id)); @@ -4276,7 +4275,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4317,7 +4316,7 @@ mod tests { #[cfg(debug_assertions)] fn inject_non_existent_connection_closed_for_disabled_peer() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -4352,7 +4351,7 @@ mod tests { #[cfg(debug_assertions)] fn inject_non_existent_connection_closed_for_disabled_pending_enable() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { @@ -4405,7 +4404,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4449,7 +4448,7 @@ mod tests { let (mut notif, _peerset) = development_notifs(); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let connected = ConnectedPoint::Listener { local_addr: Multiaddr::empty(), send_back_addr: Multiaddr::empty(), @@ -4494,7 +4493,7 @@ mod tests { #[cfg(debug_assertions)] fn inject_connection_closed_for_backoff_peer() { let (mut notif, _peerset) = development_notifs(); - let set_id = sc_peerset::SetId::from(0); + let set_id = crate::peerset::SetId::from(0); let peer = PeerId::random(); let conn = ConnectionId::new_unchecked(0); let connected = ConnectedPoint::Listener { diff --git a/client/network/src/protocol/notifications/tests.rs b/client/network/src/protocol/notifications/tests.rs index 998ec79f81f88..ddef29a4ea6ac 100644 --- a/client/network/src/protocol/notifications/tests.rs +++ b/client/network/src/protocol/notifications/tests.rs @@ -65,8 +65,8 @@ fn build_nodes() -> (Swarm, Swarm) { .timeout(Duration::from_secs(20)) .boxed(); - let (peerset, _) = sc_peerset::Peerset::from_config(sc_peerset::PeersetConfig { - sets: vec![sc_peerset::SetConfig { + let (peerset, _) = crate::peerset::Peerset::from_config(crate::peerset::PeersetConfig { + sets: vec![crate::peerset::SetConfig { in_peers: 25, out_peers: 25, bootnodes: if index == 0 { @@ -266,7 +266,7 @@ fn reconnect_after_disconnect() { if service2_state == ServiceState::FirstConnec { service1.behaviour_mut().disconnect_peer( Swarm::local_peer_id(&service2), - sc_peerset::SetId::from(0), + crate::peerset::SetId::from(0), ); } }, @@ -289,7 +289,7 @@ fn reconnect_after_disconnect() { if service1_state == ServiceState::FirstConnec { service1.behaviour_mut().disconnect_peer( Swarm::local_peer_id(&service2), - sc_peerset::SetId::from(0), + crate::peerset::SetId::from(0), ); } }, diff --git a/client/peerset/src/protocol_controller.rs b/client/network/src/protocol_controller.rs similarity index 91% rename from client/peerset/src/protocol_controller.rs rename to client/network/src/protocol_controller.rs index ce3961f130af0..72373974a44ff 100644 --- a/client/peerset/src/protocol_controller.rs +++ b/client/network/src/protocol_controller.rs @@ -42,7 +42,7 @@ //! view of the peers' states at any given moment, the eventual consistency is maintained. use futures::{channel::oneshot, future::Either, FutureExt, StreamExt}; -use libp2p_identity::PeerId; +use libp2p::PeerId; use log::{error, trace, warn}; use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use sp_arithmetic::traits::SaturatedConversion; @@ -52,7 +52,13 @@ use std::{ }; use wasm_timer::Delay; -use crate::{peer_store::PeerStoreProvider, IncomingIndex, Message, SetConfig, SetId, LOG_TARGET}; +use crate::{ + peer_store::PeerStoreProvider, + peerset::{IncomingIndex, Message, SetConfig, SetId}, +}; + +/// Log target for this file. +pub const LOG_TARGET: &str = "peerset"; /// External API actions. #[derive(Debug)] @@ -738,9 +744,11 @@ impl ProtocolController { mod tests { use super::{Direction, PeerState, ProtocolController, ProtocolHandle}; use crate::{ - peer_store::PeerStoreProvider, IncomingIndex, Message, ReputationChange, SetConfig, SetId, + peer_store::PeerStoreProvider, + peerset::{IncomingIndex, Message, SetConfig, SetId}, + ReputationChange, }; - use libp2p_identity::PeerId; + use libp2p::PeerId; use sc_utils::mpsc::{tracing_unbounded, TryRecvError}; use std::collections::HashSet; @@ -779,7 +787,7 @@ mod tests { peer_store.expect_report_disconnect().times(2).return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Add second reserved node at runtime (this currently calls `alloc_slots` internally). controller.on_add_reserved_peer(reserved2); @@ -793,8 +801,8 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); // Reserved peers do not occupy slots. assert_eq!(controller.num_out, 0); @@ -841,7 +849,7 @@ mod tests { peer_store.expect_is_banned().times(6).return_const(true); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Add second reserved node at runtime (this currently calls `alloc_slots` internally). controller.on_add_reserved_peer(reserved2); @@ -894,7 +902,7 @@ mod tests { peer_store.expect_report_disconnect().times(2).return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Add second reserved node at runtime (this calls `alloc_slots` internally). controller.on_add_reserved_peer(reserved2); @@ -908,8 +916,8 @@ mod tests { } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); // Drop both reserved nodes. controller.on_peer_dropped(reserved1); @@ -924,8 +932,8 @@ mod tests { } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); // No slots occupied. assert_eq!(controller.num_out, 0); @@ -953,7 +961,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Initiate connections. controller.alloc_slots(); @@ -965,8 +973,8 @@ mod tests { // Only first two peers are connected (we only have 2 slots). assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer2 })); // Outgoing slots occupied. assert_eq!(controller.num_out, 2); @@ -1005,7 +1013,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Initiate connections. controller.alloc_slots(); @@ -1015,10 +1023,10 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 4); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: regular1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: regular2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: regular1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: regular2 })); assert_eq!(controller.num_out, 2); assert_eq!(controller.num_in, 0); } @@ -1048,7 +1056,7 @@ mod tests { peer_store.expect_report_disconnect().times(2).return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Initiate connections. controller.alloc_slots(); @@ -1060,8 +1068,8 @@ mod tests { // Only first two peers are connected (we only have 2 slots). assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer2 })); // Outgoing slots occupied. assert_eq!(controller.num_out, 2); @@ -1093,7 +1101,7 @@ mod tests { // Peers are connected. assert_eq!(messages.len(), 1); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer3 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer3 })); // Outgoing slots occupied. assert_eq!(controller.num_out, 1); @@ -1116,7 +1124,7 @@ mod tests { peer_store.expect_register_protocol().once().return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Initiate connections. controller.alloc_slots(); @@ -1143,7 +1151,7 @@ mod tests { peer_store.expect_register_protocol().once().return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); let peer = PeerId::random(); let incoming_index = IncomingIndex(1); @@ -1182,7 +1190,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Initiate connections. controller.alloc_slots(); @@ -1201,8 +1209,8 @@ mod tests { } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer2 })); assert_eq!(controller.num_out, 2); assert_eq!(controller.num_in, 0); } @@ -1230,7 +1238,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert_eq!(controller.num_out, 0); assert_eq!(controller.num_in, 0); @@ -1242,9 +1250,9 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 3); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: regular1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: regular1 })); assert_eq!(controller.num_out, 1); assert_eq!(controller.num_in, 0); @@ -1264,8 +1272,8 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Drop { set_id: SetId(0), peer_id: regular1 })); - assert!(messages.contains(&Message::Drop { set_id: SetId(0), peer_id: regular2 })); + assert!(messages.contains(&Message::Drop { set_id: SetId::from(0), peer_id: regular1 })); + assert!(messages.contains(&Message::Drop { set_id: SetId::from(0), peer_id: regular2 })); assert_eq!(controller.nodes.len(), 0); assert_eq!(controller.num_out, 0); assert_eq!(controller.num_in, 0); @@ -1289,7 +1297,7 @@ mod tests { peer_store.expect_register_protocol().once().return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert_eq!(controller.reserved_nodes.len(), 2); assert_eq!(controller.nodes.len(), 0); assert_eq!(controller.num_out, 0); @@ -1323,7 +1331,7 @@ mod tests { peer_store.expect_is_banned().times(2).return_const(false); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Initiate connections. controller.alloc_slots(); @@ -1332,8 +1340,8 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved1 })); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); assert_eq!(controller.reserved_nodes.len(), 2); assert!(controller.reserved_nodes.contains_key(&reserved1)); assert!(controller.reserved_nodes.contains_key(&reserved2)); @@ -1341,7 +1349,10 @@ mod tests { // Remove reserved node controller.on_remove_reserved_peer(reserved1); - assert_eq!(rx.try_recv().unwrap(), Message::Drop { set_id: SetId(0), peer_id: reserved1 }); + assert_eq!( + rx.try_recv().unwrap(), + Message::Drop { set_id: SetId::from(0), peer_id: reserved1 } + ); assert_eq!(rx.try_recv().unwrap_err(), TryRecvError::Empty); assert_eq!(controller.reserved_nodes.len(), 1); assert!(controller.reserved_nodes.contains_key(&reserved2)); @@ -1368,7 +1379,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(Vec::new()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `peer1` as inbound, `peer2` as outbound. controller.on_incoming_connection(peer1, IncomingIndex(1)); @@ -1379,7 +1390,7 @@ mod tests { } assert_eq!(messages.len(), 2); assert!(messages.contains(&Message::Accept(IncomingIndex(1)))); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer2 })); assert_eq!(controller.num_out, 0); assert_eq!(controller.num_in, 0); @@ -1415,7 +1426,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `peer1` as outbound & `peer2` as inbound. controller.alloc_slots(); @@ -1425,7 +1436,7 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer1 })); assert!(messages.contains(&Message::Accept(IncomingIndex(1)))); assert_eq!(controller.num_in, 1); assert_eq!(controller.num_out, 1); @@ -1458,7 +1469,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `peer1` as outbound & `peer2` as inbound. controller.alloc_slots(); @@ -1468,7 +1479,7 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer1 })); assert!(messages.contains(&Message::Accept(IncomingIndex(1)))); assert_eq!(controller.nodes.len(), 2); assert!(matches!(controller.nodes.get(&peer1), Some(Direction::Outbound))); @@ -1477,7 +1488,10 @@ mod tests { assert_eq!(controller.num_out, 1); controller.on_disconnect_peer(peer1); - assert_eq!(rx.try_recv().unwrap(), Message::Drop { set_id: SetId(0), peer_id: peer1 }); + assert_eq!( + rx.try_recv().unwrap(), + Message::Drop { set_id: SetId::from(0), peer_id: peer1 } + ); assert_eq!(rx.try_recv().unwrap_err(), TryRecvError::Empty); assert_eq!(controller.nodes.len(), 1); assert!(!controller.nodes.contains_key(&peer1)); @@ -1485,7 +1499,10 @@ mod tests { assert_eq!(controller.num_out, 0); controller.on_disconnect_peer(peer2); - assert_eq!(rx.try_recv().unwrap(), Message::Drop { set_id: SetId(0), peer_id: peer2 }); + assert_eq!( + rx.try_recv().unwrap(), + Message::Drop { set_id: SetId::from(0), peer_id: peer2 } + ); assert_eq!(rx.try_recv().unwrap_err(), TryRecvError::Empty); assert_eq!(controller.nodes.len(), 0); assert_eq!(controller.num_in, 0); @@ -1512,7 +1529,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(Vec::new()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `reserved1` as inbound & `reserved2` as outbound. controller.on_incoming_connection(reserved1, IncomingIndex(1)); @@ -1523,7 +1540,7 @@ mod tests { } assert_eq!(messages.len(), 2); assert!(messages.contains(&Message::Accept(IncomingIndex(1)))); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); assert!(matches!( controller.reserved_nodes.get(&reserved1), Some(PeerState::Connected(Direction::Inbound)) @@ -1570,7 +1587,7 @@ mod tests { peer_store.expect_report_disconnect().times(2).return_const(()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `peer1` as outbound & `peer2` as inbound. controller.alloc_slots(); @@ -1580,7 +1597,7 @@ mod tests { messages.push(message); } assert_eq!(messages.len(), 2); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: peer1 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: peer1 })); assert!(messages.contains(&Message::Accept(IncomingIndex(1)))); assert_eq!(controller.nodes.len(), 2); assert!(matches!(controller.nodes.get(&peer1), Some(Direction::Outbound))); @@ -1622,7 +1639,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(Vec::new()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `reserved1` as inbound & `reserved2` as outbound. controller.on_incoming_connection(reserved1, IncomingIndex(1)); @@ -1633,7 +1650,7 @@ mod tests { } assert_eq!(messages.len(), 2); assert!(messages.contains(&Message::Accept(IncomingIndex(1)))); - assert!(messages.contains(&Message::Connect { set_id: SetId(0), peer_id: reserved2 })); + assert!(messages.contains(&Message::Connect { set_id: SetId::from(0), peer_id: reserved2 })); assert!(matches!( controller.reserved_nodes.get(&reserved1), Some(PeerState::Connected(Direction::Inbound)) @@ -1683,7 +1700,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert_eq!(controller.num_out, 0); assert_eq!(controller.num_in, 0); @@ -1691,7 +1708,7 @@ mod tests { controller.alloc_slots(); assert_eq!( rx.try_recv().ok().unwrap(), - Message::Connect { set_id: SetId(0), peer_id: regular1 } + Message::Connect { set_id: SetId::from(0), peer_id: regular1 } ); assert_eq!(rx.try_recv().unwrap_err(), TryRecvError::Empty); assert!(matches!(controller.nodes.get(®ular1).unwrap(), Direction::Outbound,)); @@ -1737,7 +1754,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert_eq!(controller.num_out, 0); assert_eq!(controller.num_in, 0); @@ -1745,7 +1762,7 @@ mod tests { controller.alloc_slots(); assert_eq!( rx.try_recv().ok().unwrap(), - Message::Connect { set_id: SetId(0), peer_id: regular1 } + Message::Connect { set_id: SetId::from(0), peer_id: regular1 } ); assert_eq!(rx.try_recv().unwrap_err(), TryRecvError::Empty); assert!(matches!(controller.nodes.get(®ular1).unwrap(), Direction::Outbound,)); @@ -1790,7 +1807,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(outgoing_candidates); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert_eq!(controller.num_out, 0); assert_eq!(controller.num_in, 0); @@ -1798,7 +1815,7 @@ mod tests { controller.alloc_slots(); assert_eq!( rx.try_recv().ok().unwrap(), - Message::Connect { set_id: SetId(0), peer_id: regular1 } + Message::Connect { set_id: SetId::from(0), peer_id: regular1 } ); assert_eq!(rx.try_recv().unwrap_err(), TryRecvError::Empty); assert!(matches!(controller.nodes.get(®ular1).unwrap(), Direction::Outbound,)); @@ -1843,7 +1860,7 @@ mod tests { peer_store.expect_is_banned().once().return_const(false); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Connect `peer1` as inbound. controller.on_incoming_connection(peer1, IncomingIndex(1)); @@ -1874,7 +1891,7 @@ mod tests { peer_store.expect_is_banned().once().return_const(true); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); // Incoming request. controller.on_incoming_connection(peer1, IncomingIndex(1)); @@ -1900,7 +1917,7 @@ mod tests { peer_store.expect_is_banned().once().return_const(true); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert!(controller.reserved_nodes.contains_key(&reserved1)); // Incoming request. @@ -1928,7 +1945,7 @@ mod tests { peer_store.expect_outgoing_candidates().once().return_const(Vec::new()); let (_handle, mut controller) = - ProtocolController::new(SetId(0), config, tx, Box::new(peer_store)); + ProtocolController::new(SetId::from(0), config, tx, Box::new(peer_store)); assert!(matches!(controller.reserved_nodes.get(&reserved1), Some(PeerState::NotConnected))); // Initiate connectios diff --git a/client/network/src/request_responses.rs b/client/network/src/request_responses.rs index e21ff3a3412d9..44e6f588ab520 100644 --- a/client/network/src/request_responses.rs +++ b/client/network/src/request_responses.rs @@ -34,7 +34,9 @@ //! - If provided, a ["requests processing"](ProtocolConfig::inbound_queue) channel //! is used to handle incoming requests. -use crate::{types::ProtocolName, ReputationChange}; +use crate::{ + peer_store::BANNED_THRESHOLD, peerset::PeersetHandle, types::ProtocolName, ReputationChange, +}; use futures::{channel::oneshot, prelude::*}; use libp2p::{ @@ -49,8 +51,6 @@ use libp2p::{ PeerId, }; -use sc_peerset::{PeersetHandle, BANNED_THRESHOLD}; - use std::{ collections::{hash_map::Entry, HashMap}, io, iter, @@ -1040,6 +1040,7 @@ impl Codec for GenericCodec { mod tests { use super::*; + use crate::peerset::{Peerset, PeersetConfig, SetConfig}; use futures::{channel::oneshot, executor::LocalPool, task::Spawn}; use libp2p::{ core::{ @@ -1051,7 +1052,6 @@ mod tests { swarm::{Executor, Swarm, SwarmBuilder, SwarmEvent}, Multiaddr, }; - use sc_peerset::{Peerset, PeersetConfig, SetConfig}; use std::{iter, time::Duration}; struct TokioExecutor(tokio::runtime::Runtime); diff --git a/client/network/src/service.rs b/client/network/src/service.rs index 34d97ca07cf03..62ff5ae42466b 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -36,6 +36,7 @@ use crate::{ network_state::{ NetworkState, NotConnectedPeer as NetworkStateNotConnectedPeer, Peer as NetworkStatePeer, }, + peerset::PeersetHandle, protocol::{self, NotifsHandlerError, Protocol, Ready}, request_responses::{IfDisconnected, RequestFailure}, service::{ @@ -73,7 +74,6 @@ use metrics::{Histogram, HistogramVec, MetricSources, Metrics}; use parking_lot::Mutex; use sc_network_common::ExHashT; -use sc_peerset::PeersetHandle; use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use sp_runtime::traits::Block as BlockT; diff --git a/client/network/src/service/traits.rs b/client/network/src/service/traits.rs index 22af3816bd15d..97680a9eb73b7 100644 --- a/client/network/src/service/traits.rs +++ b/client/network/src/service/traits.rs @@ -24,13 +24,12 @@ use crate::{ request_responses::{IfDisconnected, RequestFailure}, service::signature::Signature, types::ProtocolName, + ReputationChange, }; use futures::{channel::oneshot, Stream}; use libp2p::{Multiaddr, PeerId}; -use sc_peerset::ReputationChange; - use std::{collections::HashSet, future::Future, pin::Pin, sync::Arc}; pub use libp2p::{identity::SigningError, kad::record::Key as KademliaKey}; diff --git a/client/network/statement/Cargo.toml b/client/network/statement/Cargo.toml index a81e6a916c05c..7b2bbb2552d22 100644 --- a/client/network/statement/Cargo.toml +++ b/client/network/statement/Cargo.toml @@ -23,7 +23,6 @@ pin-project = "1.0.12" prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../../utils/prometheus" } sc-network-common = { version = "0.10.0-dev", path = "../common" } sc-network = { version = "0.10.0-dev", path = "../" } -sc-peerset = { version = "4.0.0-dev", path = "../../peerset" } sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" } sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" } sp-statement-store = { version = "4.0.0-dev", path = "../../../primitives/statement-store" } diff --git a/client/network/statement/src/lib.rs b/client/network/statement/src/lib.rs index 02cbab27a6a15..2c966a346ad87 100644 --- a/client/network/statement/src/lib.rs +++ b/client/network/statement/src/lib.rs @@ -62,7 +62,7 @@ pub type Statements = Vec; pub type StatementImportFuture = oneshot::Receiver; mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Reputation change when a peer sends us any statement. /// /// This forces node to verify it, thus the negative value here. Once statement is verified, diff --git a/client/network/sync/Cargo.toml b/client/network/sync/Cargo.toml index 81d39d87f54f3..7dbc774844a29 100644 --- a/client/network/sync/Cargo.toml +++ b/client/network/sync/Cargo.toml @@ -35,7 +35,6 @@ sc-client-api = { version = "4.0.0-dev", path = "../../api" } sc-consensus = { version = "0.10.0-dev", path = "../../consensus/common" } sc-network = { version = "0.10.0-dev", path = "../" } sc-network-common = { version = "0.10.0-dev", path = "../common" } -sc-peerset = { version = "4.0.0-dev", path = "../../peerset" } sc-utils = { version = "4.0.0-dev", path = "../../utils" } sp-arithmetic = { version = "6.0.0", path = "../../../primitives/arithmetic" } sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" } diff --git a/client/network/sync/src/block_request_handler.rs b/client/network/sync/src/block_request_handler.rs index df17d56ba59f7..4fa5de3ca3c84 100644 --- a/client/network/sync/src/block_request_handler.rs +++ b/client/network/sync/src/block_request_handler.rs @@ -54,7 +54,7 @@ const MAX_BODY_BYTES: usize = 8 * 1024 * 1024; const MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER: usize = 2; mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Reputation change when a peer sent us the same request multiple times. pub const SAME_REQUEST: Rep = Rep::new_fatal("Same block request multiple times"); diff --git a/client/network/sync/src/engine.rs b/client/network/sync/src/engine.rs index a6db5a5d54c8c..f92cd585680b1 100644 --- a/client/network/sync/src/engine.rs +++ b/client/network/sync/src/engine.rs @@ -40,7 +40,7 @@ use sc_network::{ FullNetworkConfiguration, NonDefaultSetConfig, ProtocolId, SyncMode as SyncOperationMode, }, utils::LruHashSet, - NotificationsSink, ProtocolName, + NotificationsSink, ProtocolName, ReputationChange, }; use sc_network_common::{ role::Roles, @@ -82,7 +82,7 @@ const MAX_KNOWN_BLOCKS: usize = 1024; // ~32kb per peer + LruHashSet overhead const INACTIVITY_EVICT_THRESHOLD: Duration = Duration::from_secs(30); mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Peer has different genesis. pub const GENESIS_MISMATCH: Rep = Rep::new_fatal("Genesis mismatch"); /// Peer send us a block announcement that failed at validation. @@ -681,7 +681,7 @@ where .disconnect_peer(peer, self.block_announce_protocol_name.clone()); self.network_service.report_peer( peer, - sc_peerset::ReputationChange::new_fatal("Invalid justification"), + ReputationChange::new_fatal("Invalid justification"), ); } }, diff --git a/client/network/sync/src/lib.rs b/client/network/sync/src/lib.rs index fbdb275a444cd..e9655ddd4c999 100644 --- a/client/network/sync/src/lib.rs +++ b/client/network/sync/src/lib.rs @@ -148,7 +148,7 @@ const MAX_BLOCK_ANNOUNCE_SIZE: u64 = 1024 * 1024; pub(crate) const MAX_BLOCKS_IN_RESPONSE: usize = 128; mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Reputation change when a peer sent us a message that led to a /// database read error. pub const BLOCKCHAIN_READ_ERROR: Rep = Rep::new(-(1 << 16), "DB Error"); diff --git a/client/network/sync/src/service/mock.rs b/client/network/sync/src/service/mock.rs index 9bce9f91b1d24..b3ef0f328140b 100644 --- a/client/network/sync/src/service/mock.rs +++ b/client/network/sync/src/service/mock.rs @@ -25,9 +25,8 @@ use sc_network::{ request_responses::{IfDisconnected, RequestFailure}, types::ProtocolName, NetworkNotification, NetworkPeers, NetworkRequest, NetworkSyncForkRequest, - NotificationSenderError, NotificationSenderT, + NotificationSenderError, NotificationSenderT, ReputationChange, }; -use sc_peerset::ReputationChange; use sp_runtime::traits::{Block as BlockT, NumberFor}; use std::collections::HashSet; diff --git a/client/network/sync/src/service/network.rs b/client/network/sync/src/service/network.rs index f87de1c4c3ecc..12a47d6a9b544 100644 --- a/client/network/sync/src/service/network.rs +++ b/client/network/sync/src/service/network.rs @@ -22,9 +22,8 @@ use libp2p::PeerId; use sc_network::{ request_responses::{IfDisconnected, RequestFailure}, types::ProtocolName, - NetworkNotification, NetworkPeers, NetworkRequest, + NetworkNotification, NetworkPeers, NetworkRequest, ReputationChange, }; -use sc_peerset::ReputationChange; use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use std::sync::Arc; @@ -159,7 +158,7 @@ mod tests { let peer = PeerId::random(); let proto = ProtocolName::from("test-protocol"); let proto_clone = proto.clone(); - let change = sc_peerset::ReputationChange::new_fatal("test-change"); + let change = sc_network::ReputationChange::new_fatal("test-change"); let mut mock_network = MockNetwork::new(); mock_network diff --git a/client/network/sync/src/state_request_handler.rs b/client/network/sync/src/state_request_handler.rs index e5b0da6ce1501..5e2d0ae48f62b 100644 --- a/client/network/sync/src/state_request_handler.rs +++ b/client/network/sync/src/state_request_handler.rs @@ -45,7 +45,7 @@ const MAX_RESPONSE_BYTES: usize = 2 * 1024 * 1024; // Actual reponse may be bigg const MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER: usize = 2; mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Reputation change when a peer sent us the same request multiple times. pub const SAME_REQUEST: Rep = Rep::new(i32::MIN, "Same state request multiple times"); diff --git a/client/peerset/tests/fuzz.rs b/client/network/test/src/peerset.rs similarity index 100% rename from client/peerset/tests/fuzz.rs rename to client/network/test/src/peerset.rs diff --git a/client/network/transactions/Cargo.toml b/client/network/transactions/Cargo.toml index 3ae1dc5908df4..6d939a3648431 100644 --- a/client/network/transactions/Cargo.toml +++ b/client/network/transactions/Cargo.toml @@ -22,7 +22,6 @@ pin-project = "1.0.12" prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../../utils/prometheus" } sc-network = { version = "0.10.0-dev", path = "../" } sc-network-common = { version = "0.10.0-dev", path = "../common" } -sc-peerset = { version = "4.0.0-dev", path = "../../peerset" } sc-utils = { version = "4.0.0-dev", path = "../../utils" } sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" } sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" } diff --git a/client/network/transactions/src/lib.rs b/client/network/transactions/src/lib.rs index f57556d3986b0..36c49b68669f8 100644 --- a/client/network/transactions/src/lib.rs +++ b/client/network/transactions/src/lib.rs @@ -65,7 +65,7 @@ pub mod config; pub type Transactions = Vec; mod rep { - use sc_peerset::ReputationChange as Rep; + use sc_network::ReputationChange as Rep; /// Reputation change when a peer sends us any transaction. /// /// This forces node to verify it, thus the negative value here. Once transaction is verified, diff --git a/client/offchain/Cargo.toml b/client/offchain/Cargo.toml index a2ab54ba5e638..46690e392e8ba 100644 --- a/client/offchain/Cargo.toml +++ b/client/offchain/Cargo.toml @@ -31,7 +31,6 @@ tracing = "0.1.29" sc-client-api = { version = "4.0.0-dev", path = "../api" } sc-network = { version = "0.10.0-dev", path = "../network" } sc-network-common = { version = "0.10.0-dev", path = "../network/common" } -sc-peerset = { version = "4.0.0-dev", path = "../peerset" } sc-utils = { version = "4.0.0-dev", path = "../utils" } sp-api = { version = "4.0.0-dev", path = "../../primitives/api" } sp-core = { version = "7.0.0", path = "../../primitives/core" } diff --git a/client/offchain/src/api.rs b/client/offchain/src/api.rs index f6cfdcd536bb5..3171fb133945f 100644 --- a/client/offchain/src/api.rs +++ b/client/offchain/src/api.rs @@ -328,8 +328,8 @@ mod tests { use sc_client_db::offchain::LocalStorage; use sc_network::{ config::MultiaddrWithPeerId, types::ProtocolName, NetworkPeers, NetworkStateInfo, + ReputationChange, }; - use sc_peerset::ReputationChange; use sp_core::offchain::{DbExternalities, Externalities}; use std::time::SystemTime; diff --git a/client/offchain/src/lib.rs b/client/offchain/src/lib.rs index 013bb27980e29..27f7df3837050 100644 --- a/client/offchain/src/lib.rs +++ b/client/offchain/src/lib.rs @@ -246,8 +246,7 @@ mod tests { use libp2p::{Multiaddr, PeerId}; use sc_block_builder::BlockBuilderProvider as _; use sc_client_api::Backend as _; - use sc_network::{config::MultiaddrWithPeerId, types::ProtocolName}; - use sc_peerset::ReputationChange; + use sc_network::{config::MultiaddrWithPeerId, types::ProtocolName, ReputationChange}; use sc_transaction_pool::{BasicPool, FullChainApi}; use sc_transaction_pool_api::{InPoolTransaction, TransactionPool}; use sp_consensus::BlockOrigin; diff --git a/client/peerset/Cargo.toml b/client/peerset/Cargo.toml deleted file mode 100644 index b07a6ffa09c7c..0000000000000 --- a/client/peerset/Cargo.toml +++ /dev/null @@ -1,30 +0,0 @@ -[package] -description = "Connectivity manager based on reputation" -homepage = "https://substrate.io" -license = "GPL-3.0-or-later WITH Classpath-exception-2.0" -name = "sc-peerset" -version = "4.0.0-dev" -authors = ["Parity Technologies "] -edition = "2021" -repository = "https://github.com/paritytech/substrate/" -documentation = "https://docs.rs/sc-peerset" -readme = "README.md" - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - -[dependencies] -futures = "0.3.21" -libp2p-identity = { version = "0.1.2", features = ["peerid", "ed25519"] } -log = "0.4.17" -parking_lot = "0.12.1" -partial_sort = "0.2.0" -serde_json = "1.0.85" -wasm-timer = "0.2" -sc-utils = { version = "4.0.0-dev", path = "../utils" } -sp-arithmetic = { version = "6.0.0", path = "../../primitives/arithmetic" } - -[dev-dependencies] -sp-tracing = { version = "6.0.0", path = "../../primitives/tracing" } -mockall = "0.11.3" -rand = "0.8.5" diff --git a/client/peerset/README.md b/client/peerset/README.md deleted file mode 100644 index 1b54c52001caf..0000000000000 --- a/client/peerset/README.md +++ /dev/null @@ -1,4 +0,0 @@ -Peer Set Manager (PSM). Contains the strategy for choosing which nodes the network should be -connected to. - -License: GPL-3.0-or-later WITH Classpath-exception-2.0 \ No newline at end of file From fd91ecbb0275da4a6f0ffcff14695ab169bf543d Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 26 May 2023 16:13:42 +0300 Subject: [PATCH 2/3] Apply review suggestions --- client/network/src/protocol/notifications/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/network/src/protocol/notifications/behaviour.rs b/client/network/src/protocol/notifications/behaviour.rs index 2861e3e0416a2..c14e0313155a9 100644 --- a/client/network/src/protocol/notifications/behaviour.rs +++ b/client/network/src/protocol/notifications/behaviour.rs @@ -17,13 +17,13 @@ // along with this program. If not, see . use crate::{ + peerset::DropReason, protocol::notifications::handler::{ self, NotificationsSink, NotifsHandler, NotifsHandlerIn, NotifsHandlerOut, }, types::ProtocolName, }; -use crate::peerset::DropReason; use bytes::BytesMut; use fnv::FnvHashMap; use futures::{channel::oneshot, prelude::*}; From 8769e10ea18e82756b4be1ca61a95681b590de6a Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Mon, 29 May 2023 19:41:10 +0300 Subject: [PATCH 3/3] Fix `reconnect_after_disconnect` test --- .../src/protocol/notifications/tests.rs | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/client/network/src/protocol/notifications/tests.rs b/client/network/src/protocol/notifications/tests.rs index ddef29a4ea6ac..0c2eb89262f93 100644 --- a/client/network/src/protocol/notifications/tests.rs +++ b/client/network/src/protocol/notifications/tests.rs @@ -65,19 +65,24 @@ fn build_nodes() -> (Swarm, Swarm) { .timeout(Duration::from_secs(20)) .boxed(); - let (peerset, _) = crate::peerset::Peerset::from_config(crate::peerset::PeersetConfig { - sets: vec![crate::peerset::SetConfig { - in_peers: 25, - out_peers: 25, - bootnodes: if index == 0 { - keypairs.iter().skip(1).map(|keypair| keypair.public().to_peer_id()).collect() - } else { - vec![] - }, - reserved_nodes: Default::default(), - reserved_only: false, - }], - }); + let (peerset, handle) = + crate::peerset::Peerset::from_config(crate::peerset::PeersetConfig { + sets: vec![crate::peerset::SetConfig { + in_peers: 25, + out_peers: 25, + bootnodes: if index == 0 { + keypairs + .iter() + .skip(1) + .map(|keypair| keypair.public().to_peer_id()) + .collect() + } else { + vec![] + }, + reserved_nodes: Default::default(), + reserved_only: false, + }], + }); let behaviour = CustomProtoWithAddr { inner: Notifications::new( @@ -89,6 +94,7 @@ fn build_nodes() -> (Swarm, Swarm) { max_notification_size: 1024 * 1024, }), ), + _peerset_handle: handle, addrs: addrs .iter() .enumerate() @@ -124,6 +130,8 @@ fn build_nodes() -> (Swarm, Swarm) { /// Wraps around the `CustomBehaviour` network behaviour, and adds hardcoded node addresses to it. struct CustomProtoWithAddr { inner: Notifications, + // We need to keep `PeersetHandle` for `Peerset` not to shut down. + _peerset_handle: crate::peerset::PeersetHandle, addrs: Vec<(PeerId, Multiaddr)>, }