Network sync refactoring (part 1) (#11303)

* Remove unnecessary imports, move one internal re-export into where it is actually used, make one import explicit

* Move a few data structures down into modules

* Use generic parameters in `sc-network` instead of `chain::Client` trait

* Remove unnecessary bound
This commit is contained in:
Nazar Mokrynskyi
2022-04-29 17:02:03 +03:00
committed by GitHub
parent 887acda7d0
commit af6773aba9
15 changed files with 380 additions and 206 deletions
+97 -15
View File
@@ -40,8 +40,10 @@ use libp2p::{
};
use log::debug;
use prost::Message;
use sc_client_api::{BlockBackend, ProofProvider};
use sc_consensus::import_queue::{IncomingBlock, Origin};
use sc_peerset::PeersetHandle;
use sp_blockchain::{HeaderBackend, HeaderMetadata};
use sp_consensus::BlockOrigin;
use sp_runtime::{
traits::{Block as BlockT, NumberFor},
@@ -62,17 +64,27 @@ pub use crate::request_responses::{
/// General behaviour of the network. Combines all protocols together.
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourOut<B>", poll_method = "poll", event_process = true)]
pub struct Behaviour<B: BlockT> {
pub struct Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
/// All the substrate-specific protocols.
substrate: Protocol<B>,
substrate: Protocol<B, Client>,
/// Periodically pings and identifies the nodes we are connected to, and store information in a
/// cache.
peer_info: peer_info::PeerInfoBehaviour,
/// Discovers nodes of the network.
discovery: DiscoveryBehaviour,
/// Bitswap server for blockchain data.
bitswap: Toggle<Bitswap<B>>,
/// Generic request-reponse protocols.
bitswap: Toggle<Bitswap<B, Client>>,
/// Generic request-response protocols.
request_responses: request_responses::RequestResponsesBehaviour,
/// Queue of events to produce for the outside.
@@ -191,17 +203,27 @@ pub enum BehaviourOut<B: BlockT> {
Dht(DhtEvent, Duration),
}
impl<B: BlockT> Behaviour<B> {
impl<B, Client> Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
/// Builds a new `Behaviour`.
pub fn new(
substrate: Protocol<B>,
substrate: Protocol<B, Client>,
user_agent: String,
local_public_key: PublicKey,
disco_config: DiscoveryConfig,
block_request_protocol_config: request_responses::ProtocolConfig,
state_request_protocol_config: request_responses::ProtocolConfig,
warp_sync_protocol_config: Option<request_responses::ProtocolConfig>,
bitswap: Option<Bitswap<B>>,
bitswap: Option<Bitswap<B, Client>>,
light_client_request_protocol_config: request_responses::ProtocolConfig,
// All remaining request protocol configs.
mut request_response_protocols: Vec<request_responses::ProtocolConfig>,
@@ -293,12 +315,12 @@ impl<B: BlockT> Behaviour<B> {
}
/// Returns a shared reference to the user protocol.
pub fn user_protocol(&self) -> &Protocol<B> {
pub fn user_protocol(&self) -> &Protocol<B, Client> {
&self.substrate
}
/// Returns a mutable reference to the user protocol.
pub fn user_protocol_mut(&mut self) -> &mut Protocol<B> {
pub fn user_protocol_mut(&mut self) -> &mut Protocol<B, Client> {
&mut self.substrate
}
@@ -325,13 +347,33 @@ fn reported_roles_to_observed_role(roles: Roles) -> ObservedRole {
}
}
impl<B: BlockT> NetworkBehaviourEventProcess<void::Void> for Behaviour<B> {
impl<B, Client> NetworkBehaviourEventProcess<void::Void> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
fn inject_event(&mut self, event: void::Void) {
void::unreachable(event)
}
}
impl<B: BlockT> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for Behaviour<B> {
impl<B, Client> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
fn inject_event(&mut self, event: CustomMessageOutcome<B>) {
match event {
CustomMessageOutcome::BlockImport(origin, blocks) =>
@@ -435,7 +477,17 @@ impl<B: BlockT> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for Behavi
}
}
impl<B: BlockT> NetworkBehaviourEventProcess<request_responses::Event> for Behaviour<B> {
impl<B, Client> NetworkBehaviourEventProcess<request_responses::Event> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
fn inject_event(&mut self, event: request_responses::Event) {
match event {
request_responses::Event::InboundRequest { peer, protocol, result } => {
@@ -457,7 +509,17 @@ impl<B: BlockT> NetworkBehaviourEventProcess<request_responses::Event> for Behav
}
}
impl<B: BlockT> NetworkBehaviourEventProcess<peer_info::PeerInfoEvent> for Behaviour<B> {
impl<B, Client> NetworkBehaviourEventProcess<peer_info::PeerInfoEvent> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
fn inject_event(&mut self, event: peer_info::PeerInfoEvent) {
let peer_info::PeerInfoEvent::Identified {
peer_id,
@@ -480,7 +542,17 @@ impl<B: BlockT> NetworkBehaviourEventProcess<peer_info::PeerInfoEvent> for Behav
}
}
impl<B: BlockT> NetworkBehaviourEventProcess<DiscoveryOut> for Behaviour<B> {
impl<B, Client> NetworkBehaviourEventProcess<DiscoveryOut> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
fn inject_event(&mut self, out: DiscoveryOut) {
match out {
DiscoveryOut::UnroutablePeer(_peer_id) => {
@@ -514,7 +586,17 @@ impl<B: BlockT> NetworkBehaviourEventProcess<DiscoveryOut> for Behaviour<B> {
}
}
impl<B: BlockT> Behaviour<B> {
impl<B, Client> Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
{
fn poll(
&mut self,
_cx: &mut Context,