Extract syncing protocol from sc-network (#12828)

* Move import queue out of `sc-network`

Add supplementary asynchronous API for the import queue which means
it can be run as an independent task and communicated with through
the `ImportQueueService`.

This commit removes removes block and justification imports from
`sc-network` and provides `ChainSync` with a handle to import queue so
it can import blocks and justifications. Polling of the import queue is
moved complete out of `sc-network` and `sc_consensus::Link` is
implemented for `ChainSyncInterfaceHandled` so the import queue
can still influence the syncing process.

* Move stuff to SyncingEngine

* Move `ChainSync` instanation to `SyncingEngine`

Some of the tests have to be rewritten

* Move peer hashmap to `SyncingEngine`

* Let `SyncingEngine` to implement `ChainSyncInterface`

* Introduce `SyncStatusProvider`

* Move `sync_peer_(connected|disconnected)` to `SyncingEngine`

* Implement `SyncEventStream`

Remove `SyncConnected`/`SyncDisconnected` events from
`NetworkEvenStream` and provide those events through
`ChainSyncInterface` instead.

Modify BEEFY/GRANDPA/transactions protocol and `NetworkGossip` to take
`SyncEventStream` object which they listen to for incoming sync peer
events.

* Introduce `ChainSyncInterface`

This interface provides a set of miscellaneous functions that other
subsystems can use to query, for example, the syncing status.

* Move event stream polling to `SyncingEngine`

Subscribe to `NetworkStreamEvent` and poll the incoming notifications
and substream events from `SyncingEngine`.

The code needs refactoring.

* Make `SyncingEngine` into an asynchronous runner

This commits removes the last hard dependency of syncing from
`sc-network` meaning the protocol now lives completely outside of
`sc-network`, ignoring the hardcoded peerset entry which will be
addressed in the future.

Code needs a lot of refactoring.

* Fix warnings

* Code refactoring

* Use `SyncingService` for BEEFY

* Use `SyncingService` for GRANDPA

* Remove call delegation from `NetworkService`

* Remove `ChainSyncService`

* Remove `ChainSync` service tests

They were written for the sole purpose of verifying that `NetworWorker`
continues to function while the calls are being dispatched to
`ChainSync`.

* Refactor code

* Refactor code

* Update client/finality-grandpa/src/communication/tests.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* Fix warnings

* Apply review comments

* Fix docs

* Fix test

* cargo-fmt

* Update client/network/sync/src/engine.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* Update client/network/sync/src/engine.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* Add missing docs

* Refactor code

---------

Co-authored-by: Anton <anton.kalyaev@gmail.com>
This commit is contained in:
Aaro Altonen
2023-03-06 18:33:38 +02:00
committed by GitHub
parent 8adde84330
commit 1a7f5be07f
57 changed files with 2904 additions and 2877 deletions
+44 -34
View File
@@ -38,18 +38,19 @@ use sc_client_db::{Backend, DatabaseSettings};
use sc_consensus::import_queue::ImportQueue;
use sc_executor::RuntimeVersionOf;
use sc_keystore::LocalKeystore;
use sc_network::{config::SyncMode, NetworkService};
use sc_network::NetworkService;
use sc_network_bitswap::BitswapRequestHandler;
use sc_network_common::{
config::SyncMode,
protocol::role::Roles,
service::{NetworkStateInfo, NetworkStatusProvider},
service::{NetworkEventStream, NetworkStateInfo, NetworkStatusProvider},
sync::warp::WarpSyncParams,
};
use sc_network_light::light_client_requests::handler::LightClientRequestHandler;
use sc_network_sync::{
block_request_handler::BlockRequestHandler, service::network::NetworkServiceProvider,
state_request_handler::StateRequestHandler,
warp_request_handler::RequestHandler as WarpSyncRequestHandler, ChainSync,
block_request_handler::BlockRequestHandler, engine::SyncingEngine,
service::network::NetworkServiceProvider, state_request_handler::StateRequestHandler,
warp_request_handler::RequestHandler as WarpSyncRequestHandler, SyncingService,
};
use sc_rpc::{
author::AuthorApiServer,
@@ -349,12 +350,7 @@ where
/// Shared network instance implementing a set of mandatory traits.
pub trait SpawnTaskNetwork<Block: BlockT>:
sc_offchain::NetworkProvider
+ NetworkStateInfo
+ NetworkStatusProvider<Block>
+ Send
+ Sync
+ 'static
sc_offchain::NetworkProvider + NetworkStateInfo + NetworkStatusProvider + Send + Sync + 'static
{
}
@@ -363,7 +359,7 @@ where
Block: BlockT,
T: sc_offchain::NetworkProvider
+ NetworkStateInfo
+ NetworkStatusProvider<Block>
+ NetworkStatusProvider
+ Send
+ Sync
+ 'static,
@@ -394,6 +390,8 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> {
/// Controller for transactions handlers
pub tx_handler_controller:
sc_network_transactions::TransactionsHandlerController<<TBl as BlockT>::Hash>,
/// Syncing service.
pub sync_service: Arc<SyncingService<TBl>>,
/// Telemetry instance for this node.
pub telemetry: Option<&'a mut Telemetry>,
}
@@ -471,6 +469,7 @@ where
network,
system_rpc_tx,
tx_handler_controller,
sync_service,
telemetry,
} = params;
@@ -533,7 +532,12 @@ where
spawn_handle.spawn(
"telemetry-periodic-send",
None,
metrics_service.run(client.clone(), transaction_pool.clone(), network.clone()),
metrics_service.run(
client.clone(),
transaction_pool.clone(),
network.clone(),
sync_service.clone(),
),
);
let rpc_id_provider = config.rpc_id_provider.take();
@@ -560,7 +564,12 @@ where
spawn_handle.spawn(
"informant",
None,
sc_informant::build(client.clone(), network, config.informant_output_format),
sc_informant::build(
client.clone(),
network,
sync_service.clone(),
config.informant_output_format,
),
);
task_manager.keep_alive((config.base_path, rpc));
@@ -771,6 +780,7 @@ pub fn build_network<TBl, TExPool, TImpQu, TCl>(
TracingUnboundedSender<sc_rpc::system::Request<TBl>>,
sc_network_transactions::TransactionsHandlerController<<TBl as BlockT>::Hash>,
NetworkStarter,
Arc<SyncingService<TBl>>,
),
Error,
>
@@ -876,27 +886,23 @@ where
};
let (chain_sync_network_provider, chain_sync_network_handle) = NetworkServiceProvider::new();
let (chain_sync, chain_sync_service, block_announce_config) = ChainSync::new(
match config.network.sync_mode {
SyncMode::Full => sc_network_common::sync::SyncMode::Full,
SyncMode::Fast { skip_proofs, storage_chain_mode } =>
sc_network_common::sync::SyncMode::LightState { skip_proofs, storage_chain_mode },
SyncMode::Warp => sc_network_common::sync::SyncMode::Warp,
},
let (engine, sync_service, block_announce_config) = SyncingEngine::new(
Roles::from(&config.role),
client.clone(),
config.prometheus_config.as_ref().map(|config| config.registry.clone()).as_ref(),
&config.network,
protocol_id.clone(),
&config.chain_spec.fork_id().map(ToOwned::to_owned),
Roles::from(&config.role),
block_announce_validator,
config.network.max_parallel_downloads,
warp_sync_params,
config.prometheus_config.as_ref().map(|config| config.registry.clone()).as_ref(),
chain_sync_network_handle,
import_queue.service(),
block_request_protocol_config.name.clone(),
state_request_protocol_config.name.clone(),
warp_sync_protocol_config.as_ref().map(|config| config.name.clone()),
)?;
let sync_service_import_queue = sync_service.clone();
let sync_service = Arc::new(sync_service);
request_response_protocol_configs.push(config.network.ipfs_server.then(|| {
let (handler, protocol_config) = BitswapRequestHandler::new(client.clone());
@@ -916,8 +922,6 @@ where
chain: client.clone(),
protocol_id: protocol_id.clone(),
fork_id: config.chain_spec.fork_id().map(ToOwned::to_owned),
chain_sync: Box::new(chain_sync),
chain_sync_service: Box::new(chain_sync_service.clone()),
metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()),
block_announce_config,
request_response_protocol_configs: request_response_protocol_configs
@@ -953,6 +957,7 @@ where
let (tx_handler, tx_handler_controller) = transactions_handler_proto.build(
network.clone(),
sync_service.clone(),
Arc::new(TransactionPoolAdapter { pool: transaction_pool, client: client.clone() }),
config.prometheus_config.as_ref().map(|config| &config.registry),
)?;
@@ -963,11 +968,10 @@ where
Some("networking"),
chain_sync_network_provider.run(network.clone()),
);
spawn_handle.spawn(
"import-queue",
None,
import_queue.run(Box::new(chain_sync_service.clone())),
);
spawn_handle.spawn("import-queue", None, import_queue.run(Box::new(sync_service_import_queue)));
let event_stream = network.event_stream("syncing");
spawn_handle.spawn("syncing", None, engine.run(event_stream));
let (system_rpc_tx, system_rpc_rx) = tracing_unbounded("mpsc_system_rpc", 10_000);
spawn_handle.spawn(
@@ -976,7 +980,7 @@ where
build_system_rpc_future(
config.role.clone(),
network_mut.service().clone(),
chain_sync_service.clone(),
sync_service.clone(),
client.clone(),
system_rpc_rx,
has_bootnodes,
@@ -984,7 +988,7 @@ where
);
let future =
build_network_future(network_mut, client, chain_sync_service, config.announce_block);
build_network_future(network_mut, client, sync_service.clone(), config.announce_block);
// TODO: Normally, one is supposed to pass a list of notifications protocols supported by the
// node through the `NetworkConfiguration` struct. But because this function doesn't know in
@@ -1022,7 +1026,13 @@ where
future.await
});
Ok((network, system_rpc_tx, tx_handler_controller, NetworkStarter(network_start_tx)))
Ok((
network,
system_rpc_tx,
tx_handler_controller,
NetworkStarter(network_start_tx),
sync_service.clone(),
))
}
/// Object used to start the network.