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
+16 -2
View File
@@ -27,6 +27,7 @@ use sc_network_common::{
config::{MultiaddrWithPeerId, TransportConfig},
service::{NetworkBlock, NetworkPeers, NetworkStateInfo},
};
use sc_network_sync::SyncingService;
use sc_service::{
client::Client,
config::{BasePath, DatabaseSource, KeystoreConfig},
@@ -79,6 +80,7 @@ pub trait TestNetNode:
fn network(
&self,
) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>>;
fn sync(&self) -> &Arc<SyncingService<Self::Block>>;
fn spawn_handle(&self) -> SpawnTaskHandle;
}
@@ -87,6 +89,7 @@ pub struct TestNetComponents<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> {
client: Arc<Client<TBackend, TExec, TBl, TRtApi>>,
transaction_pool: Arc<TExPool>,
network: Arc<sc_network::NetworkService<TBl, <TBl as BlockT>::Hash>>,
sync: Arc<SyncingService<TBl>>,
}
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool>
@@ -96,9 +99,16 @@ impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool>
task_manager: TaskManager,
client: Arc<Client<TBackend, TExec, TBl, TRtApi>>,
network: Arc<sc_network::NetworkService<TBl, <TBl as BlockT>::Hash>>,
sync: Arc<SyncingService<TBl>>,
transaction_pool: Arc<TExPool>,
) -> Self {
Self { client, transaction_pool, network, task_manager: Arc::new(Mutex::new(task_manager)) }
Self {
client,
sync,
transaction_pool,
network,
task_manager: Arc::new(Mutex::new(task_manager)),
}
}
}
@@ -111,6 +121,7 @@ impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Clone
client: self.client.clone(),
transaction_pool: self.transaction_pool.clone(),
network: self.network.clone(),
sync: self.sync.clone(),
}
}
}
@@ -151,6 +162,9 @@ where
) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>> {
self.network.clone()
}
fn sync(&self) -> &Arc<SyncingService<Self::Block>> {
&self.sync
}
fn spawn_handle(&self) -> SpawnTaskHandle {
self.task_manager.lock().spawn_handle()
}
@@ -477,7 +491,7 @@ pub fn sync<G, E, Fb, F, B, ExF, U>(
let info = network.full_nodes[0].1.client().info();
network.full_nodes[0]
.1
.network()
.sync()
.new_best_block_imported(info.best_hash, info.best_number);
network.full_nodes[0].3.clone()
};