Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Get rid of Peerset compatibility layer #14337

Merged
merged 44 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6e38b7e
Move bootnodes from individual `SetConfig`s to `PeersetConfig`
dmitry-markin Jun 6, 2023
dae075d
Move `SetId` & `SetConfig` from `peerset` to `protocol_controller`
dmitry-markin Jun 6, 2023
5a4f6a4
Remove unused `DropReason`
dmitry-markin Jun 6, 2023
7acf0e3
Move `Message` & `IncomingIndex` from `peerset` to `protocol_controller`
dmitry-markin Jun 6, 2023
037ead4
Restore running fuzz test
dmitry-markin Jun 6, 2023
efba474
Get rid of `Peerset` in `fuzz` test
dmitry-markin Jun 6, 2023
911d26f
Spawn runners instead of manual polling in `fuzz` test
dmitry-markin Jun 7, 2023
25b63cd
Migrate `Protocol` from `Peerset` to `PeerStore` & `ProtocolController`
dmitry-markin Jun 7, 2023
1fbff99
Migrate `NetworkService` from `Peerset` to `PeerStore` & `ProtocolCon…
dmitry-markin Jun 7, 2023
8923fb5
Migrate `Notifications` from `Peerset` to `ProtocolController`s
dmitry-markin Jun 8, 2023
1350193
Migrate `Notifications` tests from `Peerset` to `ProtocolController`
dmitry-markin Jun 8, 2023
e0cae7e
Fix compilation of `NetworkService` & `Protocol`
dmitry-markin Jun 8, 2023
10ec8b4
Fix borrowing issues in `Notifications`
dmitry-markin Jun 8, 2023
b92ed07
Migrate `RequestResponse`from `Peerset` to `PeerStore`
dmitry-markin Jun 8, 2023
b265eb8
rustfmt
dmitry-markin Jun 8, 2023
5339b2e
Migrate request-response tests from `Peerset` to `PeerStore`
dmitry-markin Jun 8, 2023
71ca009
Migrate `reconnect_after_disconnect` test to `PeerStore` & `ProtocolC…
dmitry-markin Jun 8, 2023
8b64997
Fix `Notifications` tests
dmitry-markin Jun 8, 2023
eb2693e
Remove `Peerset` completely
dmitry-markin Jun 8, 2023
cb3b12c
Fix bug with counting sync peers in `Protocol`
dmitry-markin Jun 9, 2023
649b6c1
Eliminate indirect calls to `PeerStore` via `Protocol`
dmitry-markin Jun 9, 2023
094ffe5
Eliminate indirect calls to `ProtocolController` via `Protocol`
dmitry-markin Jun 9, 2023
dd77fba
Handle `Err` outcome from `remove_peers_from_reserved_set`
dmitry-markin Jun 9, 2023
5cddda0
Add note about disconnecting sync peers in `Protocol`
dmitry-markin Jun 9, 2023
84fb33d
minor: remove unneeded `clone()`
dmitry-markin Jun 9, 2023
7588e32
minor: extra comma removed
dmitry-markin Jun 9, 2023
7a84efc
minor: use `Stream` API of `from_protocol_controllers` channel
dmitry-markin Jun 9, 2023
7b39689
minor: remove TODO
dmitry-markin Jun 9, 2023
a9b65dc
minor: replace `.map().flatten()` with `.flat_map()`
dmitry-markin Jun 9, 2023
ac22673
minor: update `ProtocolController` docs
dmitry-markin Jun 9, 2023
c637260
rustfmt
dmitry-markin Jun 9, 2023
ab05cf0
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Jun 12, 2023
6f17714
Apply suggestions from code review
dmitry-markin Jun 13, 2023
b5ec102
Extract `MockPeerStore` to `mock.rs`
dmitry-markin Jun 13, 2023
1e89b2a
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Jun 14, 2023
0520230
Move `PeerStore` initialization to `build_network`
dmitry-markin Jun 14, 2023
e46fa49
minor: remove unused import
dmitry-markin Jun 14, 2023
2d336a5
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Jun 19, 2023
02334a8
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Jul 21, 2023
bbea1d4
minor: clarify error message
dmitry-markin Jul 24, 2023
c516194
Convert `syncs_header_only_forks` test into single-threaded
dmitry-markin Jul 24, 2023
3c37275
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Jul 24, 2023
78f8884
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Jul 26, 2023
70d5a11
Merge remote-tracking branch 'origin/master' into dm-get-rid-of-peers…
dmitry-markin Aug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/consensus/grandpa/src/communication/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ impl NetworkPeers for TestNetwork {
unimplemented!();
}

fn remove_peers_from_reserved_set(&self, _protocol: ProtocolName, _peers: Vec<PeerId>) {}
fn remove_peers_from_reserved_set(
&self,
_protocol: ProtocolName,
_peers: Vec<PeerId>,
) -> Result<(), String> {
unimplemented!();
}

fn sync_num_connected(&self) -> usize {
unimplemented!();
Expand Down
13 changes: 8 additions & 5 deletions client/network-gossip/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,7 @@ impl<B: BlockT> Future for GossipEngine<B> {
SyncEvent::PeerConnected(remote) =>
this.network.add_set_reserved(remote, this.protocol.clone()),
SyncEvent::PeerDisconnected(remote) =>
this.network.remove_peers_from_reserved_set(
this.protocol.clone(),
vec![remote],
),
this.network.remove_set_reserved(remote, this.protocol.clone()),
},
// The sync event stream closed. Do the same for [`GossipValidator`].
Poll::Ready(None) => {
Expand Down Expand Up @@ -414,7 +411,13 @@ mod tests {
unimplemented!();
}

fn remove_peers_from_reserved_set(&self, _protocol: ProtocolName, _peers: Vec<PeerId>) {}
fn remove_peers_from_reserved_set(
&self,
_protocol: ProtocolName,
_peers: Vec<PeerId>,
) -> Result<(), String> {
unimplemented!();
}

fn sync_num_connected(&self) -> usize {
unimplemented!();
Expand Down
6 changes: 6 additions & 0 deletions client/network-gossip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ pub trait Network<B: BlockT>: NetworkPeers + NetworkEventStream + NetworkNotific
log::error!(target: "gossip", "add_set_reserved failed: {}", err);
}
}
fn remove_set_reserved(&self, who: PeerId, protocol: ProtocolName) {
let result = self.remove_peers_from_reserved_set(protocol, iter::once(who).collect());
if let Err(err) = result {
log::error!(target: "gossip", "remove_set_reserved failed: {}", err);
}
}
}

impl<T, B: BlockT> Network<B> for T where T: NetworkPeers + NetworkEventStream + NetworkNotification {}
Expand Down
8 changes: 7 additions & 1 deletion client/network-gossip/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,13 @@ mod tests {
unimplemented!();
}

fn remove_peers_from_reserved_set(&self, _protocol: ProtocolName, _peers: Vec<PeerId>) {}
fn remove_peers_from_reserved_set(
&self,
_protocol: ProtocolName,
_peers: Vec<PeerId>,
) -> Result<(), String> {
unimplemented!();
}

fn sync_num_connected(&self) -> usize {
unimplemented!();
Expand Down
6 changes: 3 additions & 3 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
discovery::{DiscoveryBehaviour, DiscoveryConfig, DiscoveryOut},
event::DhtEvent,
peer_info,
peerset::PeersetHandle,
peer_store::PeerStoreHandle,
protocol::{CustomMessageOutcome, NotificationsSink, Protocol},
request_responses::{self, IfDisconnected, ProtocolConfig, RequestFailure},
types::ProtocolName,
Expand Down Expand Up @@ -173,7 +173,7 @@ impl<B: BlockT> Behaviour<B> {
local_public_key: PublicKey,
disco_config: DiscoveryConfig,
request_response_protocols: Vec<ProtocolConfig>,
peerset: PeersetHandle,
peer_store_handle: PeerStoreHandle,
connection_limits: ConnectionLimits,
external_addresses: Arc<Mutex<HashSet<Multiaddr>>>,
) -> Result<Self, request_responses::RegisterError> {
Expand All @@ -188,7 +188,7 @@ impl<B: BlockT> Behaviour<B> {
connection_limits: libp2p::connection_limits::Behaviour::new(connection_limits),
request_responses: request_responses::RequestResponsesBehaviour::new(
request_response_protocols.into_iter(),
peerset,
Box::new(peer_store_handle),
)?,
})
}
Expand Down
9 changes: 9 additions & 0 deletions client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use crate::{

pub use libp2p::{identity::Keypair, multiaddr, Multiaddr, PeerId};

use crate::peer_store::PeerStoreHandle;
use codec::Encode;
use prometheus_endpoint::Registry;
use zeroize::Zeroize;
Expand Down Expand Up @@ -270,6 +271,11 @@ impl NonReservedPeerMode {
_ => None,
}
}

/// If we are in "reserved-only" peer mode.
pub fn is_reserved_only(&self) -> bool {
matches!(self, NonReservedPeerMode::Deny)
}
}

/// The configuration of a node's secret key, describing the type of key
Expand Down Expand Up @@ -674,6 +680,9 @@ pub struct Params<Block: BlockT> {
/// Network layer configuration.
pub network_config: FullNetworkConfiguration,

/// Peer store with known nodes, peer reputations, etc.
pub peer_store: PeerStoreHandle,

/// Legacy name of the protocol to use on the wire. Should be different for each chain.
pub protocol_id: ProtocolId,

Expand Down
8 changes: 5 additions & 3 deletions client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,20 @@
//! 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;

#[cfg(test)]
mod mock;

pub mod config;
pub mod discovery;
pub mod error;
pub mod event;
pub mod network_state;
pub mod peer_info;
pub mod peer_store;
pub mod protocol_controller;
pub mod request_responses;
pub mod transport;
pub mod types;
Expand Down
55 changes: 55 additions & 0 deletions client/network/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 <https://www.gnu.org/licenses/>.

//! Mocked components for tests.

use crate::{peer_store::PeerStoreProvider, protocol_controller::ProtocolHandle, ReputationChange};
use libp2p::PeerId;
use std::collections::HashSet;

/// No-op `PeerStore`.
#[derive(Debug)]
pub struct MockPeerStore {}

impl PeerStoreProvider for MockPeerStore {
fn is_banned(&self, _peer_id: &PeerId) -> bool {
// Make sure that the peer is not banned.
false
}

fn register_protocol(&self, _protocol_handle: ProtocolHandle) {
// Make sure not to fail.
}

fn report_disconnect(&mut self, _peer_id: PeerId) {
// Make sure not to fail.
}

fn report_peer(&mut self, _peer_id: PeerId, _change: ReputationChange) {
// Make sure not to fail.
}

fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
// Make sure that the peer is not banned.
0
}

fn outgoing_candidates(&self, _count: usize, _ignored: HashSet<&PeerId>) -> Vec<PeerId> {
unimplemented!()
}
}
6 changes: 6 additions & 0 deletions client/network/src/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! [`PeerStore`] manages peer reputations and provides connection candidates to
//! [`crate::protocol_controller::ProtocolController`].

use libp2p::PeerId;
use log::trace;
use parking_lot::Mutex;
Expand Down Expand Up @@ -49,6 +52,7 @@ const INVERSE_DECREMENT: i32 = 50;
/// remove it, once the reputation value reaches 0.
const FORGET_AFTER: Duration = Duration::from_secs(3600);

/// Trait providing peer reputation management and connection candidates.
pub trait PeerStoreProvider: Debug + Send {
/// Check whether the peer is banned.
fn is_banned(&self, peer_id: &PeerId) -> bool;
Expand All @@ -69,6 +73,7 @@ pub trait PeerStoreProvider: Debug + Send {
fn outgoing_candidates(&self, count: usize, ignored: HashSet<&PeerId>) -> Vec<PeerId>;
}

/// Actual implementation of peer reputations and connection candidates provider.
#[derive(Debug, Clone)]
pub struct PeerStoreHandle {
inner: Arc<Mutex<PeerStoreInner>>,
Expand Down Expand Up @@ -289,6 +294,7 @@ impl PeerStoreInner {
}
}

/// Worker part of [`PeerStoreHandle`]
#[derive(Debug)]
pub struct PeerStore {
inner: Arc<Mutex<PeerStoreInner>>,
Expand Down
Loading