From 0e09ad448bce27fcd255370cc2827ea5d2cf3892 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 8 Nov 2024 00:45:38 +0800 Subject: [PATCH] Expose more syncing types to enable custom syncing strategy (#6163) This PR exposes additional syncing types to facilitate the development of a custom syncing strategy based on the existing Polkadot syncing strategy. Specifically, my goal is to isolate the state sync and chain sync components, allowing the state to be downloaded from the P2P network without running a full regular Substrate node. I also need to intercept the state responses during the state sync process. The newly exposed types are necessary to implement this custom syncing strategy. --- prdoc/pr_6163.prdoc | 13 +++++++++++++ substrate/client/network/sync/src/lib.rs | 1 + substrate/client/network/sync/src/strategy.rs | 5 +++-- substrate/client/network/sync/src/strategy/state.rs | 11 ++++++----- 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 prdoc/pr_6163.prdoc diff --git a/prdoc/pr_6163.prdoc b/prdoc/pr_6163.prdoc new file mode 100644 index 000000000000..c8571f80ed52 --- /dev/null +++ b/prdoc/pr_6163.prdoc @@ -0,0 +1,13 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Expose more syncing types to enable custom syncing strategy + +doc: + - audience: Node Dev + description: | + Exposes additional syncing types to facilitate the development of a custom syncing strategy. + +crates: + - name: sc-network-sync + bump: patch diff --git a/substrate/client/network/sync/src/lib.rs b/substrate/client/network/sync/src/lib.rs index c458c7a5da49..e503a1cbdb18 100644 --- a/substrate/client/network/sync/src/lib.rs +++ b/substrate/client/network/sync/src/lib.rs @@ -18,6 +18,7 @@ //! Blockchain syncing implementation in Substrate. +pub use schema::v1::*; pub use service::syncing_service::SyncingService; pub use strategy::warp::{WarpSyncConfig, WarpSyncPhase, WarpSyncProgress}; pub use types::{SyncEvent, SyncEventStream, SyncState, SyncStatus, SyncStatusProvider}; diff --git a/substrate/client/network/sync/src/strategy.rs b/substrate/client/network/sync/src/strategy.rs index cdc6de1f8c65..2ac6674231e5 100644 --- a/substrate/client/network/sync/src/strategy.rs +++ b/substrate/client/network/sync/src/strategy.rs @@ -25,7 +25,7 @@ pub mod chain_sync; mod disconnected_peers; pub mod polkadot; -mod state; +pub mod state; pub mod state_sync; pub mod warp; @@ -177,7 +177,8 @@ pub enum SyncingAction { } impl SyncingAction { - fn is_finished(&self) -> bool { + /// Returns `true` if the syncing action has completed. + pub fn is_finished(&self) -> bool { matches!(self, SyncingAction::Finished) } diff --git a/substrate/client/network/sync/src/strategy/state.rs b/substrate/client/network/sync/src/strategy/state.rs index 93125fe8f66a..1abbb96ccd90 100644 --- a/substrate/client/network/sync/src/strategy/state.rs +++ b/substrate/client/network/sync/src/strategy/state.rs @@ -118,10 +118,11 @@ impl StateStrategy { } } - // Create a new instance with a custom state sync provider. - // Used in tests. - #[cfg(test)] - fn new_with_provider( + /// Create a new instance with a custom state sync provider. + /// + /// Note: In most cases, users should use [`StateStrategy::new`]. + /// This method is intended for custom sync strategies and advanced use cases. + pub fn new_with_provider( state_sync_provider: Box>, initial_peers: impl Iterator)>, protocol_name: ProtocolName, @@ -348,7 +349,7 @@ impl StateStrategy { } } - /// Get actions that should be performed by the owner on [`WarpSync`]'s behalf + /// Get actions that should be performed. #[must_use] pub fn actions( &mut self,