Introduce ChainSyncInterface (#12489)

* Introduce `ChainSyncInterface`

`ChainSyncInterface` provides an asynchronous interface for other
subsystems to submit calls to `ChainSync`. This allows `NetworkService`
to delegate calls to `ChainSync` while still providing the same API
for other subsystems (for now). This makes it possible to move the
syncing code in piecemeal fashion out of `protocol.rs` as the calls
are just forwarded to `ChainSync`.

* Apply review comments

* Fix tests
This commit is contained in:
Aaro Altonen
2022-10-17 10:25:25 +03:00
committed by GitHub
parent 68e2513265
commit ce4cad8b8b
17 changed files with 301 additions and 45 deletions
+35 -10
View File
@@ -32,14 +32,18 @@ pub mod block_request_handler;
pub mod blocks;
pub mod mock;
mod schema;
pub mod service;
pub mod state;
pub mod state_request_handler;
#[cfg(test)]
mod tests;
pub mod warp;
pub mod warp_request_handler;
use crate::{
blocks::BlockCollection,
schema::v1::{StateRequest, StateResponse},
service::chain_sync::{ChainSyncInterfaceHandle, ToServiceCommand},
state::StateSync,
warp::{WarpProofImportResult, WarpSync},
};
@@ -67,6 +71,7 @@ use sc_network_common::{
PollBlockAnnounceValidation, SyncMode, SyncState, SyncStatus,
},
};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver};
use sp_arithmetic::traits::Saturating;
use sp_blockchain::{Error as ClientError, HeaderBackend, HeaderMetadata};
use sp_consensus::{
@@ -264,6 +269,8 @@ pub struct ChainSync<B: BlockT, Client> {
import_existing: bool,
/// Gap download process.
gap_sync: Option<GapSync<B>>,
/// Channel for receiving service commands
service_rx: TracingUnboundedReceiver<ToServiceCommand<B>>,
}
/// All the data we have about a Peer that we are trying to sync with
@@ -1725,6 +1732,21 @@ where
Ok(OpaqueStateResponse(Box::new(response)))
}
fn poll(
&mut self,
cx: &mut std::task::Context,
) -> Poll<PollBlockAnnounceValidation<B::Header>> {
while let Poll::Ready(Some(event)) = self.service_rx.poll_next_unpin(cx) {
match event {
ToServiceCommand::SetSyncForkRequest(peers, hash, number) => {
self.set_sync_fork_request(peers, &hash, number);
},
}
}
self.poll_block_announce_validation(cx)
}
}
impl<B, Client> ChainSync<B, Client>
@@ -1746,7 +1768,9 @@ where
block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
max_parallel_downloads: u32,
warp_sync_provider: Option<Arc<dyn WarpSyncProvider<B>>>,
) -> Result<Self, ClientError> {
) -> Result<(Self, Box<ChainSyncInterfaceHandle<B>>), ClientError> {
let (tx, service_rx) = tracing_unbounded("mpsc_chain_sync");
let mut sync = Self {
client,
peers: HashMap::new(),
@@ -1768,9 +1792,10 @@ where
warp_sync_provider,
import_existing: false,
gap_sync: None,
service_rx,
};
sync.reset_sync_start_point()?;
Ok(sync)
Ok((sync, Box::new(ChainSyncInterfaceHandle::new(tx))))
}
/// Returns the best seen block number if we don't have that block yet, `None` otherwise.
@@ -2664,7 +2689,7 @@ mod test {
let block_announce_validator = Box::new(DefaultBlockAnnounceValidator);
let peer_id = PeerId::random();
let mut sync =
let (mut sync, _) =
ChainSync::new(SyncMode::Full, client.clone(), block_announce_validator, 1, None)
.unwrap();
@@ -2712,7 +2737,7 @@ mod test {
#[test]
fn restart_doesnt_affect_peers_downloading_finality_data() {
let mut client = Arc::new(TestClientBuilder::new().build());
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
client.clone(),
Box::new(DefaultBlockAnnounceValidator),
@@ -2879,7 +2904,7 @@ mod test {
let mut client = Arc::new(TestClientBuilder::new().build());
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
client.clone(),
Box::new(DefaultBlockAnnounceValidator),
@@ -2994,7 +3019,7 @@ mod test {
let mut client = Arc::new(TestClientBuilder::new().build());
let info = client.info();
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
client.clone(),
Box::new(DefaultBlockAnnounceValidator),
@@ -3137,7 +3162,7 @@ mod test {
let info = client.info();
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
client.clone(),
Box::new(DefaultBlockAnnounceValidator),
@@ -3268,7 +3293,7 @@ mod test {
let info = client.info();
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
client.clone(),
Box::new(DefaultBlockAnnounceValidator),
@@ -3399,7 +3424,7 @@ mod test {
let mut client = Arc::new(TestClientBuilder::new().build());
let blocks = (0..3).map(|_| build_block(&mut client, None, false)).collect::<Vec<_>>();
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
client.clone(),
Box::new(DefaultBlockAnnounceValidator),
@@ -3432,7 +3457,7 @@ mod test {
let empty_client = Arc::new(TestClientBuilder::new().build());
let mut sync = ChainSync::new(
let (mut sync, _) = ChainSync::new(
SyncMode::Full,
empty_client.clone(),
Box::new(DefaultBlockAnnounceValidator),