Network sync refactoring (part 6) (#11940)

* Extract `NetworkKVProvider` trait in `sc-authority-discovery` and remove unnecessary dependency

* Extract `NetworkSyncForkRequest` trait in `sc-finality-grandpa`

* Relax requirements on `SyncOracle` trait, remove extra native methods from `NetworkService` that are already provided by trait impls

* Move `NetworkSigner` trait from `sc-authority-discovery` into `sc-network-common` and de-duplicate methods on `NetworkService`

* Move `NetworkKVProvider` trait from `sc-authority-discovery` into `sc-network-common` and de-duplicate methods on `NetworkService`

* Minimize `sc-authority-discovery` dependency on `sc-network`

* Move `NetworkSyncForkRequest` trait from `sc-finality-grandpa` to `sc-network-common` and de-duplicate methods in `NetworkService`

* Extract `NetworkStatusProvider` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkPeers` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkEventStream` trait and de-duplicate methods on `NetworkService`

* Move more methods from `NetworkService` into `NetworkPeers` trait

* Move `NetworkStateInfo` trait into `sc-network-common`

* Extract `NetworkNotification` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkRequest` trait and de-duplicate methods on `NetworkService`

* Remove `NetworkService::local_peer_id()`, it is already provided by `NetworkStateInfo` impl

* Extract `NetworkTransaction` trait and de-duplicate methods on `NetworkService`

* Extract `NetworkBlock` trait and de-duplicate methods on `NetworkService`

* Remove dependencies on `NetworkService` from most of the methods of `sc-service`

* Address simple review comments
This commit is contained in:
Nazar Mokrynskyi
2022-08-09 21:28:32 +03:00
committed by GitHub
parent 9c56e79c43
commit a685582bfd
49 changed files with 1889 additions and 1029 deletions
+21 -60
View File
@@ -41,9 +41,9 @@
//! messages.
//!
//! The [`GossipEngine`] will automatically use [`Network::add_set_reserved`] and
//! [`Network::remove_set_reserved`] to maintain a set of peers equal to the set of peers the
//! node is syncing from. See the documentation of `sc-network` for more explanations about the
//! concepts of peer sets.
//! [`NetworkPeers::remove_peers_from_reserved_set`] to maintain a set of peers equal to the set of
//! peers the node is syncing from. See the documentation of `sc-network` for more explanations
//! about the concepts of peer sets.
//!
//! # What is a validator?
//!
@@ -67,74 +67,35 @@ pub use self::{
validator::{DiscardAll, MessageIntent, ValidationResult, Validator, ValidatorContext},
};
use futures::prelude::*;
use sc_network::{multiaddr, Event, ExHashT, NetworkService, PeerId, ReputationChange};
use sp_runtime::traits::Block as BlockT;
use std::{borrow::Cow, iter, pin::Pin, sync::Arc};
use sc_network::{multiaddr, PeerId};
use sc_network_common::service::{
NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{borrow::Cow, iter};
mod bridge;
mod state_machine;
mod validator;
/// Abstraction over a network.
pub trait Network<B: BlockT> {
/// Returns a stream of events representing what happens on the network.
fn event_stream(&self) -> Pin<Box<dyn Stream<Item = Event> + Send>>;
/// Adjust the reputation of a node.
fn report_peer(&self, peer_id: PeerId, reputation: ReputationChange);
/// Adds the peer to the set of peers to be connected to with this protocol.
fn add_set_reserved(&self, who: PeerId, protocol: Cow<'static, str>);
/// Removes the peer from the set of peers to be connected to with this protocol.
fn remove_set_reserved(&self, who: PeerId, protocol: Cow<'static, str>);
/// Force-disconnect a peer.
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>);
/// Send a notification to a peer.
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>);
/// Notify everyone we're connected to that we have the given block.
///
/// Note: this method isn't strictly related to gossiping and should eventually be moved
/// somewhere else.
fn announce(&self, block: B::Hash, associated_data: Option<Vec<u8>>);
}
impl<B: BlockT, H: ExHashT> Network<B> for Arc<NetworkService<B, H>> {
fn event_stream(&self) -> Pin<Box<dyn Stream<Item = Event> + Send>> {
Box::pin(NetworkService::event_stream(self, "network-gossip"))
}
fn report_peer(&self, peer_id: PeerId, reputation: ReputationChange) {
NetworkService::report_peer(self, peer_id, reputation);
}
pub trait Network<B: BlockT>:
NetworkPeers + NetworkEventStream + NetworkNotification + NetworkBlock<B::Hash, NumberFor<B>>
{
fn add_set_reserved(&self, who: PeerId, protocol: Cow<'static, str>) {
let addr =
iter::once(multiaddr::Protocol::P2p(who.into())).collect::<multiaddr::Multiaddr>();
let result =
NetworkService::add_peers_to_reserved_set(self, protocol, iter::once(addr).collect());
let result = self.add_peers_to_reserved_set(protocol, iter::once(addr).collect());
if let Err(err) = result {
log::error!(target: "gossip", "add_set_reserved failed: {}", err);
}
}
fn remove_set_reserved(&self, who: PeerId, protocol: Cow<'static, str>) {
NetworkService::remove_peers_from_reserved_set(self, protocol, iter::once(who).collect());
}
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
NetworkService::disconnect_peer(self, who, protocol)
}
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
NetworkService::write_notification(self, who, protocol, message)
}
fn announce(&self, block: B::Hash, associated_data: Option<Vec<u8>>) {
NetworkService::announce_block(self, block, associated_data)
}
}
impl<T, B: BlockT> Network<B> for T where
T: NetworkPeers
+ NetworkEventStream
+ NetworkNotification
+ NetworkBlock<B::Hash, NumberFor<B>>
{
}