Make NetworkService callable for ChainSync (#12542)

Introduce a middleware called `NetworkServiceProvider` which the
`ChainSync` can use to communicate with `NetworkService`. `ChainSync` is
given a `NetworkServiceHandle` which it uses to call `NetworkServiceProvider`
which then dispatches the calls to `NetworkService` on behalf of `ChainSync`.

This change will allow `ChainSync` to disconnect and report peers and
in the future it'll be possible to send requests and notifications
through the `NetworkServiceProvider`.

`NetworkServiceProvider` is needed only until the `ChainSync` object
has been removed from `Protocol`. After that, a normal `NetworkService`
handle can be passed onto `ChainSync` and these changes can be
deprecated.

Co-authored-by: parity-processbot <>
This commit is contained in:
Aaro Altonen
2022-10-22 15:36:26 +03:00
committed by GitHub
parent 932e1a334b
commit a877b0ccec
9 changed files with 443 additions and 13 deletions
+10 -2
View File
@@ -61,8 +61,8 @@ use sc_network_common::{
};
use sc_network_light::light_client_requests::handler::LightClientRequestHandler;
use sc_network_sync::{
block_request_handler::BlockRequestHandler, state_request_handler::StateRequestHandler,
warp_request_handler, ChainSync,
block_request_handler::BlockRequestHandler, service::network::NetworkServiceProvider,
state_request_handler::StateRequestHandler, warp_request_handler, ChainSync,
};
use sc_service::client::Client;
use sp_blockchain::{
@@ -864,6 +864,8 @@ where
let block_announce_validator = config
.block_announce_validator
.unwrap_or_else(|| Box::new(DefaultBlockAnnounceValidator));
let (chain_sync_network_provider, chain_sync_network_handle) =
NetworkServiceProvider::new();
let (chain_sync, chain_sync_service) = ChainSync::new(
match network_config.sync_mode {
SyncMode::Full => sc_network_common::sync::SyncMode::Full,
@@ -878,6 +880,7 @@ where
block_announce_validator,
network_config.max_parallel_downloads,
Some(warp_sync),
chain_sync_network_handle,
)
.unwrap();
let block_announce_config = chain_sync.get_block_announce_proto_config(
@@ -915,6 +918,11 @@ where
trace!(target: "test_network", "Peer identifier: {}", network.service().local_peer_id());
let service = network.service().clone();
async_std::task::spawn(async move {
chain_sync_network_provider.run(service).await;
});
self.mut_peers(move |peers| {
for peer in peers.iter_mut() {
peer.network