Skip to content

Commit 5208bed

Browse files
Extract warp sync strategy from ChainSync (#2467)
Extract `WarpSync` (and `StateSync` as part of warp sync) from `ChainSync` as independent syncing strategy called by `SyncingEngine`. Introduce `SyncingStrategy` enum as a proxy between `SyncingEngine` and specific syncing strategies. ## Limitations Gap sync is kept in `ChainSync` for now because it shares the same set of peers as block syncing implementation in `ChainSync`. Extraction of a common context responsible for peer management in syncing strategies able to run in parallel is planned for a follow-up PR. ## Further improvements A possibility of conversion of `SyncingStartegy` into a trait should be evaluated. The main stopper for this is that different strategies need to communicate different actions to `SyncingEngine` and respond to different events / provide different APIs (e.g., requesting justifications is only possible via `ChainSync` and not through `WarpSync`; `SendWarpProofRequest` action is only relevant to `WarpSync`, etc.) --------- Co-authored-by: Aaro Altonen <[email protected]>
1 parent 5ed0a75 commit 5208bed

File tree

30 files changed

+3227
-1016
lines changed

30 files changed

+3227
-1016
lines changed

polkadot/node/service/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ pub fn new_full<OverseerGenerator: OverseerGen>(
734734
}: NewFullParams<OverseerGenerator>,
735735
) -> Result<NewFull, Error> {
736736
use polkadot_node_network_protocol::request_response::IncomingRequest;
737-
use sc_network_sync::warp::WarpSyncParams;
737+
use sc_network_sync::WarpSyncParams;
738738

739739
let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
740740
let role = config.role.clone();

prdoc/pr_2467.prdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Extract warp sync strategy from `ChainSync`
5+
6+
doc:
7+
- audience: Node Dev
8+
description: |
9+
`WarpSync`, and `StateSync` as the logical part of warp sync, are extracted from `ChainSync`
10+
as independent syncing strategies. `SyncingStrategy` enum is introduced as a proxy between
11+
`SyncingEngine` and specific strategies. `SyncingStrategy` may be replaced by a trait in a
12+
follow-up PRs.
13+
14+
crates:
15+
- name: sc-network-sync

substrate/bin/node/cli/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use node_primitives::Block;
3030
use sc_client_api::{Backend, BlockBackend};
3131
use sc_consensus_babe::{self, SlotProportion};
3232
use sc_network::{event::Event, NetworkEventStream, NetworkService};
33-
use sc_network_sync::{warp::WarpSyncParams, SyncingService};
33+
use sc_network_sync::{strategy::warp::WarpSyncParams, SyncingService};
3434
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
3535
use sc_statement_store::Store as StatementStore;
3636
use sc_telemetry::{Telemetry, TelemetryWorker};

substrate/client/consensus/grandpa/src/warp_proof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
BlockNumberOps, GrandpaJustification, SharedAuthoritySet,
2424
};
2525
use sc_client_api::Backend as ClientBackend;
26-
use sc_network_sync::warp::{EncodedProof, VerificationResult, WarpSyncProvider};
26+
use sc_network_sync::strategy::warp::{EncodedProof, VerificationResult, WarpSyncProvider};
2727
use sp_blockchain::{Backend as BlockchainBackend, HeaderBackend};
2828
use sp_consensus_grandpa::{AuthorityList, SetId, GRANDPA_ENGINE_ID};
2929
use sp_runtime::{

substrate/client/informant/src/display.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ use ansi_term::Colour;
2121
use log::info;
2222
use sc_client_api::ClientInfo;
2323
use sc_network::NetworkStatus;
24-
use sc_network_sync::{
25-
warp::{WarpSyncPhase, WarpSyncProgress},
26-
SyncState, SyncStatus,
27-
};
24+
use sc_network_sync::{SyncState, SyncStatus, WarpSyncPhase, WarpSyncProgress};
2825
use sp_runtime::traits::{Block as BlockT, CheckedDiv, NumberFor, Saturating, Zero};
2926
use std::{fmt, time::Instant};
3027

@@ -130,9 +127,10 @@ impl<B: BlockT> InformantDisplay<B> {
130127
),
131128
(_, Some(state), _) => (
132129
"⚙️ ",
133-
"Downloading state".into(),
130+
"State sync".into(),
134131
format!(
135-
", {}%, {:.2} Mib",
132+
", {}, {}%, {:.2} Mib",
133+
state.phase,
136134
state.percentage,
137135
(state.size as f32) / (1024f32 * 1024f32)
138136
),

substrate/client/network/sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa" }
4949
sp-runtime = { path = "../../../primitives/runtime" }
5050

5151
[dev-dependencies]
52-
tokio = { version = "1.22.0", features = ["macros"] }
52+
mockall = "0.11.3"
5353
quickcheck = { version = "1.0.3", default-features = false }
5454
sc-block-builder = { path = "../../block-builder" }
5555
sp-test-primitives = { path = "../../../primitives/test-primitives" }

substrate/client/network/sync/src/block_announce_validator.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! [`BlockAnnounceValidator`] is responsible for async validation of block announcements.
2020
//! [`Stream`] implemented by [`BlockAnnounceValidator`] never terminates.
2121
22-
use crate::futures_stream::FuturesStream;
22+
use crate::{futures_stream::FuturesStream, LOG_TARGET};
2323
use futures::{stream::FusedStream, Future, FutureExt, Stream, StreamExt};
2424
use libp2p::PeerId;
2525
use log::{debug, error, trace, warn};
@@ -33,9 +33,6 @@ use std::{
3333
task::{Context, Poll},
3434
};
3535

36-
/// Log target for this file.
37-
const LOG_TARGET: &str = "sync";
38-
3936
/// Maximum number of concurrent block announce validations.
4037
///
4138
/// If the queue reaches the maximum, we drop any new block

substrate/client/network/sync/src/block_request_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::{
2424
BlockResponse as BlockResponseSchema, BlockResponse, Direction,
2525
},
2626
service::network::NetworkServiceHandle,
27+
LOG_TARGET,
2728
};
2829

2930
use codec::{Decode, DecodeAll, Encode};
@@ -56,7 +57,6 @@ use std::{
5657
/// Maximum blocks per response.
5758
pub(crate) const MAX_BLOCKS_IN_RESPONSE: usize = 128;
5859

59-
const LOG_TARGET: &str = "sync";
6060
const MAX_BODY_BYTES: usize = 8 * 1024 * 1024;
6161
const MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER: usize = 2;
6262

substrate/client/network/sync/src/blocks.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// You should have received a copy of the GNU General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19+
use crate::LOG_TARGET;
1920
use libp2p::PeerId;
2021
use log::trace;
2122
use sc_network_common::sync::message;
@@ -87,10 +88,10 @@ impl<B: BlockT> BlockCollection<B> {
8788

8889
match self.blocks.get(&start) {
8990
Some(&BlockRangeState::Downloading { .. }) => {
90-
trace!(target: "sync", "Inserting block data still marked as being downloaded: {}", start);
91+
trace!(target: LOG_TARGET, "Inserting block data still marked as being downloaded: {}", start);
9192
},
9293
Some(BlockRangeState::Complete(existing)) if existing.len() >= blocks.len() => {
93-
trace!(target: "sync", "Ignored block data already downloaded: {}", start);
94+
trace!(target: LOG_TARGET, "Ignored block data already downloaded: {}", start);
9495
return
9596
},
9697
_ => (),
@@ -162,7 +163,7 @@ impl<B: BlockT> BlockCollection<B> {
162163
};
163164
// crop to peers best
164165
if range.start > peer_best {
165-
trace!(target: "sync", "Out of range for peer {} ({} vs {})", who, range.start, peer_best);
166+
trace!(target: LOG_TARGET, "Out of range for peer {} ({} vs {})", who, range.start, peer_best);
166167
return None
167168
}
168169
range.end = cmp::min(peer_best + One::one(), range.end);
@@ -173,7 +174,7 @@ impl<B: BlockT> BlockCollection<B> {
173174
.next()
174175
.map_or(false, |(n, _)| range.start > *n + max_ahead.into())
175176
{
176-
trace!(target: "sync", "Too far ahead for peer {} ({})", who, range.start);
177+
trace!(target: LOG_TARGET, "Too far ahead for peer {} ({})", who, range.start);
177178
return None
178179
}
179180

@@ -224,7 +225,7 @@ impl<B: BlockT> BlockCollection<B> {
224225
};
225226
*range_data = BlockRangeState::Queued { len };
226227
}
227-
trace!(target: "sync", "{} blocks ready for import", ready.len());
228+
trace!(target: LOG_TARGET, "{} blocks ready for import", ready.len());
228229
ready
229230
}
230231

@@ -235,7 +236,7 @@ impl<B: BlockT> BlockCollection<B> {
235236
self.blocks.remove(&block_num);
236237
block_num += One::one();
237238
}
238-
trace!(target: "sync", "Cleared blocks from {:?} to {:?}", from, to);
239+
trace!(target: LOG_TARGET, "Cleared blocks from {:?} to {:?}", from, to);
239240
}
240241
}
241242

0 commit comments

Comments
 (0)