Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow skipping block history download in warp sync #2710

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions cumulus/client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use sc_consensus::{
import_queue::{ImportQueue, ImportQueueService},
BlockImport,
};
use sc_network::{config::SyncMode, NetworkService};
use sc_network::NetworkService;
use sc_network_sync::SyncingService;
use sc_network_transactions::TransactionsHandlerController;
use sc_service::{Configuration, NetworkStarter, SpawnTaskHandle, TaskManager, WarpSyncParams};
Expand Down Expand Up @@ -453,16 +453,15 @@ where
RCInterface: RelayChainInterface + Clone + 'static,
IQ: ImportQueue<Block> + 'static,
{
let warp_sync_params = match parachain_config.network.sync_mode {
SyncMode::Warp => {
let target_block = warp_sync_get::<Block, RCInterface>(
para_id,
relay_chain_interface.clone(),
spawn_handle.clone(),
);
Some(WarpSyncParams::WaitForTarget(target_block))
},
_ => None,
let warp_sync_params = if parachain_config.network.sync_mode.is_warp() {
let target_block = warp_sync_get::<Block, RCInterface>(
para_id,
relay_chain_interface.clone(),
spawn_handle.clone(),
);
Some(WarpSyncParams::WaitForTarget(target_block))
} else {
None
};

let block_announce_validator = match sybil_resistance_level {
Expand Down
8 changes: 6 additions & 2 deletions substrate/client/cli/src/arg_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ pub enum SyncMode {
Fast,
/// Download blocks without executing them. Download latest state without proofs.
FastUnsafe,
/// Prove finality and download the latest state.
/// Prove finality and download the latest state. Download block history as well.
Warp,
/// Prove finality and download the latest state. Does not download block history.
WarpNoBlockHistory,
}

impl Into<sc_network::config::SyncMode> for SyncMode {
Expand All @@ -247,7 +249,9 @@ impl Into<sc_network::config::SyncMode> for SyncMode {
skip_proofs: true,
storage_chain_mode: false,
},
SyncMode::Warp => sc_network::config::SyncMode::Warp,
SyncMode::Warp => sc_network::config::SyncMode::Warp { block_history: true },
SyncMode::WarpNoBlockHistory =>
sc_network::config::SyncMode::Warp { block_history: false },
}
}
}
7 changes: 5 additions & 2 deletions substrate/client/network/common/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ pub enum SyncMode {
storage_chain_mode: bool,
},
/// Warp sync - verify authority set transitions and the latest state.
Warp,
Warp {
/// Download block history as well
block_history: bool,
},
}

impl SyncMode {
/// Returns `true` if `self` is [`Self::Warp`].
pub fn is_warp(&self) -> bool {
matches!(self, Self::Warp)
matches!(self, Self::Warp { .. })
}

/// Returns `true` if `self` is [`Self::LightState`].
Expand Down
47 changes: 31 additions & 16 deletions substrate/client/network/sync/src/chain_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ pub struct ChainSync<B: BlockT, Client> {
import_existing: bool,
/// Gap download process.
gap_sync: Option<GapSync<B>>,
/// Flag to remember that block history has been disabled
skipped_gap_sync: bool,
/// Pending actions.
actions: Vec<ChainSyncAction<B>>,
}
Expand Down Expand Up @@ -367,6 +369,7 @@ where
warp_sync: None,
import_existing: false,
gap_sync: None,
skipped_gap_sync: false,
warp_sync_config,
warp_sync_target_block_header: None,
actions: Vec::new(),
Expand Down Expand Up @@ -412,7 +415,7 @@ where
phase: WarpSyncPhase::DownloadingBlocks(gap_sync.best_queued_number),
total_bytes: 0,
}),
(None, SyncMode::Warp, _) => Some(WarpSyncProgress {
(None, SyncMode::Warp { .. }, _) => Some(WarpSyncProgress {
phase: WarpSyncPhase::AwaitingPeers {
required_peers: MIN_PEERS_TO_START_WARP_SYNC,
},
Expand Down Expand Up @@ -554,7 +557,7 @@ where
},
);

if let SyncMode::Warp = self.mode {
if self.mode.is_warp() {
if self.peers.len() >= MIN_PEERS_TO_START_WARP_SYNC && self.warp_sync.is_none()
{
log::debug!(target: LOG_TARGET, "Starting warp state sync.");
Expand Down Expand Up @@ -1204,7 +1207,7 @@ where
match self.mode {
SyncMode::Full =>
BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION | BlockAttributes::BODY,
SyncMode::LightState { storage_chain_mode: false, .. } | SyncMode::Warp =>
SyncMode::LightState { storage_chain_mode: false, .. } | SyncMode::Warp { .. } =>
BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION | BlockAttributes::BODY,
SyncMode::LightState { storage_chain_mode: true, .. } =>
BlockAttributes::HEADER |
Expand All @@ -1217,7 +1220,7 @@ where
match self.mode {
SyncMode::Full => false,
SyncMode::LightState { .. } => true,
SyncMode::Warp => true,
SyncMode::Warp { .. } => true,
}
}

Expand Down Expand Up @@ -1360,7 +1363,7 @@ where
);
self.mode = SyncMode::Full;
}
if matches!(self.mode, SyncMode::Warp) && info.finalized_state.is_some() {
if self.mode.is_warp() && info.finalized_state.is_some() {
warn!(
target: LOG_TARGET,
"Can't use warp sync mode with a partially synced database. Reverting to full sync mode."
Expand Down Expand Up @@ -1388,12 +1391,17 @@ where
}

if let Some((start, end)) = info.block_gap {
debug!(target: LOG_TARGET, "Starting gap sync #{start} - #{end}");
self.gap_sync = Some(GapSync {
best_queued_number: start - One::one(),
target: end,
blocks: BlockCollection::new(),
});
if let SyncMode::Warp { block_history: false } = self.mode {
// Skip block history
self.skipped_gap_sync = true;
} else {
debug!(target: LOG_TARGET, "Starting gap sync #{start} - #{end}");
self.gap_sync = Some(GapSync {
best_queued_number: start - One::one(),
target: end,
blocks: BlockCollection::new(),
});
}
}
trace!(
target: LOG_TARGET,
Expand Down Expand Up @@ -1564,7 +1572,7 @@ where

/// Get block requests scheduled by sync to be sent out.
fn block_requests(&mut self) -> Vec<(PeerId, BlockRequest<B>)> {
if self.mode == SyncMode::Warp {
if self.mode.is_warp() {
return self
.warp_target_block_request()
.map_or_else(|| Vec::new(), |req| Vec::from([req]))
Expand Down Expand Up @@ -1962,10 +1970,17 @@ where
let gap_sync_complete =
self.gap_sync.as_ref().map_or(false, |s| s.target == number);
if gap_sync_complete {
info!(
target: LOG_TARGET,
"Block history download is complete."
);
if self.skipped_gap_sync {
info!(
target: LOG_TARGET,
"Block history download has been skipped."
);
} else {
info!(
target: LOG_TARGET,
"Block history download is complete."
);
}
self.gap_sync = None;
}
},
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ pub trait TestNetFactory: Default + Sized + Send {
*genesis_extra_storage = storage;
}

if matches!(config.sync_mode, SyncMode::LightState { .. } | SyncMode::Warp) {
if matches!(config.sync_mode, SyncMode::LightState { .. } | SyncMode::Warp { .. }) {
test_client_builder = test_client_builder.set_no_genesis();
}
let backend = test_client_builder.backend();
Expand Down
4 changes: 2 additions & 2 deletions substrate/client/network/test/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ async fn warp_sync() {
net.add_full_peer_with_config(Default::default());
net.add_full_peer_with_config(Default::default());
net.add_full_peer_with_config(FullPeerConfig {
sync_mode: SyncMode::Warp,
sync_mode: SyncMode::Warp { block_history: true },
..Default::default()
});
let gap_end = net.peer(0).push_blocks(63, false).pop().unwrap();
Expand Down Expand Up @@ -1266,7 +1266,7 @@ async fn warp_sync_to_target_block() {
let target_block = net.peer(0).client.header(target).unwrap().unwrap();

net.add_full_peer_with_config(FullPeerConfig {
sync_mode: SyncMode::Warp,
sync_mode: SyncMode::Warp { block_history: true },
target_block: Some(target_block),
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ where
match config.network.sync_mode {
SyncMode::LightState { .. } =>
return Err("Fast sync doesn't work for archive nodes".into()),
SyncMode::Warp => return Err("Warp sync doesn't work for archive nodes".into()),
SyncMode::Warp { .. } => return Err("Warp sync doesn't work for archive nodes".into()),
SyncMode::Full => {},
}
}
Expand Down
Loading