From 9077e402cc3d0ba8354f23326be87b4dd69d01e0 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Tue, 19 Sep 2023 01:24:01 +0300 Subject: [PATCH] Improve sync start speed on restart --- crates/subspace-service/src/sync_from_dsn.rs | 28 +++++++++++++++++-- .../src/sync_from_dsn/import_blocks.rs | 5 ++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/crates/subspace-service/src/sync_from_dsn.rs b/crates/subspace-service/src/sync_from_dsn.rs index e613e40feb..a2bc95bb89 100644 --- a/crates/subspace-service/src/sync_from_dsn.rs +++ b/crates/subspace-service/src/sync_from_dsn.rs @@ -13,8 +13,10 @@ use sc_consensus::import_queue::ImportQueueService; use sc_consensus_subspace::archiver::SegmentHeadersStore; use sc_network::config::SyncMode; use sc_network::{NetworkPeers, NetworkService}; -use sp_api::BlockT; +use sp_api::{BlockT, ProvideRuntimeApi}; use sp_blockchain::HeaderBackend; +use sp_consensus_subspace::{FarmerPublicKey, SubspaceApi}; +use sp_runtime::Saturating; use std::future::Future; use std::sync::atomic::Ordering; use std::sync::Arc; @@ -61,9 +63,11 @@ where Client: HeaderBackend + BlockBackend + BlockchainEvents + + ProvideRuntimeApi + Send + Sync + 'static, + Client::Api: SubspaceApi, { let (tx, rx) = mpsc::channel(0); let observer_fut = { @@ -217,13 +221,31 @@ async fn create_worker( where Block: BlockT, AS: AuxStore + Send + Sync + 'static, - Client: HeaderBackend + BlockBackend + Send + Sync + 'static, + Client: HeaderBackend + + BlockBackend + + ProvideRuntimeApi + + Send + + Sync + + 'static, + Client::Api: SubspaceApi, IQS: ImportQueueService + ?Sized, { // Corresponds to contents of block one, everyone has it, so we consider it being processed // right away let mut last_processed_segment_index = SegmentIndex::ZERO; - let mut last_processed_block_number = client.info().finalized_number; + // TODO: We'll be able to just take finalized block once we are able to decouple pruning from + // finality: https://github.com/paritytech/polkadot-sdk/issues/1570 + let mut last_processed_block_number = { + let info = client.info(); + info.best_number.saturating_sub( + client + .runtime_api() + .chain_constants(info.best_hash) + .map_err(|error| error.to_string())? + .confirmation_depth_k() + .into(), + ) + }; let segment_header_downloader = SegmentHeaderDownloader::new(node); let piece_provider = PieceProvider::new( node.clone(), diff --git a/crates/subspace-service/src/sync_from_dsn/import_blocks.rs b/crates/subspace-service/src/sync_from_dsn/import_blocks.rs index 934842cf75..7f8306dc4b 100644 --- a/crates/subspace-service/src/sync_from_dsn/import_blocks.rs +++ b/crates/subspace-service/src/sync_from_dsn/import_blocks.rs @@ -117,10 +117,9 @@ where .is_some(); let info = client.info(); - // We already have this block imported and it is finalized, so can't change - if last_archived_block <= info.finalized_number { + // We have already processed this block, it can't change + if last_archived_block <= *last_processed_block_number { *last_processed_segment_index = segment_index; - *last_processed_block_number = last_archived_block; // Reset reconstructor instance reconstructor = Reconstructor::new().map_err(|error| error.to_string())?; continue;