Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 5d0fd14

Browse files
authored
Unify SyncMode data structures under one (#14465)
1 parent fe2d513 commit 5d0fd14

File tree

10 files changed

+50
-152
lines changed

10 files changed

+50
-152
lines changed

client/cli/src/arg_enums.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,14 @@ impl Into<sc_network::config::SyncMode> for SyncMode {
258258
fn into(self) -> sc_network::config::SyncMode {
259259
match self {
260260
SyncMode::Full => sc_network::config::SyncMode::Full,
261-
SyncMode::Fast =>
262-
sc_network::config::SyncMode::Fast { skip_proofs: false, storage_chain_mode: false },
263-
SyncMode::FastUnsafe =>
264-
sc_network::config::SyncMode::Fast { skip_proofs: true, storage_chain_mode: false },
261+
SyncMode::Fast => sc_network::config::SyncMode::LightState {
262+
skip_proofs: false,
263+
storage_chain_mode: false,
264+
},
265+
SyncMode::FastUnsafe => sc_network::config::SyncMode::LightState {
266+
skip_proofs: true,
267+
storage_chain_mode: false,
268+
},
265269
SyncMode::Warp => sc_network::config::SyncMode::Warp,
266270
}
267271
}

client/network/common/src/sync.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,43 @@ pub enum PollBlockAnnounceValidation<H> {
185185
/// The announcement.
186186
announce: BlockAnnounce<H>,
187187
},
188-
/// The announcement header should be imported.
189-
ImportHeader {
190-
/// Who sent the processed block announcement?
191-
who: PeerId,
192-
/// Was this their new best block?
193-
is_best: bool,
194-
/// The announcement.
195-
announce: BlockAnnounce<H>,
196-
},
197188
/// The block announcement should be skipped.
198189
Skip,
199190
}
200191

201-
/// Operation mode.
202-
#[derive(Debug, PartialEq, Eq)]
192+
/// Sync operation mode.
193+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
203194
pub enum SyncMode {
204-
// Sync headers only
205-
Light,
206-
// Sync headers and block bodies
195+
/// Full block download and verification.
207196
Full,
208-
// Sync headers and the last finalied state
209-
LightState { storage_chain_mode: bool, skip_proofs: bool },
210-
// Warp sync mode.
197+
/// Download blocks and the latest state.
198+
LightState {
199+
/// Skip state proof download and verification.
200+
skip_proofs: bool,
201+
/// Download indexed transactions for recent blocks.
202+
storage_chain_mode: bool,
203+
},
204+
/// Warp sync - verify authority set transitions and the latest state.
211205
Warp,
212206
}
213207

208+
impl SyncMode {
209+
/// Returns `true` if `self` is [`Self::Warp`].
210+
pub fn is_warp(&self) -> bool {
211+
matches!(self, Self::Warp)
212+
}
213+
214+
/// Returns `true` if `self` is [`Self::LightState`].
215+
pub fn light_state(&self) -> bool {
216+
matches!(self, Self::LightState { .. })
217+
}
218+
}
219+
220+
impl Default for SyncMode {
221+
fn default() -> Self {
222+
Self::Full
223+
}
224+
}
214225
#[derive(Debug)]
215226
pub struct Metrics {
216227
pub queued_blocks: u32,
@@ -376,12 +387,6 @@ pub trait ChainSync<Block: BlockT>: Send {
376387
response: BlockResponse<Block>,
377388
) -> Result<OnBlockData<Block>, BadPeer>;
378389

379-
/// Procss received block data.
380-
fn process_block_response_data(
381-
&mut self,
382-
blocks_to_import: Result<OnBlockData<Block>, BadPeer>,
383-
);
384-
385390
/// Handle a response from the remote to a justification request that we made.
386391
///
387392
/// `request` must be the original request that triggered `response`.
@@ -421,9 +426,6 @@ pub trait ChainSync<Block: BlockT>: Send {
421426
/// [`ChainSync::push_block_announce_validation`].
422427
///
423428
/// This should be polled until it returns [`Poll::Pending`].
424-
///
425-
/// If [`PollBlockAnnounceValidation::ImportHeader`] is returned, then the caller MUST try to
426-
/// import passed header (call `on_block_data`). The network request isn't sent in this case.
427429
fn poll_block_announce_validation(
428430
&mut self,
429431
cx: &mut std::task::Context<'_>,

client/network/src/config.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use zeroize::Zeroize;
3838

3939
pub use sc_network_common::{
4040
role::{Role, Roles},
41-
sync::warp::WarpSyncProvider,
41+
sync::{warp::WarpSyncProvider, SyncMode},
4242
ExHashT,
4343
};
4444
use sc_utils::mpsc::TracingUnboundedSender;
@@ -277,40 +277,6 @@ impl NonReservedPeerMode {
277277
}
278278
}
279279

280-
/// Sync operation mode.
281-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
282-
pub enum SyncMode {
283-
/// Full block download and verification.
284-
Full,
285-
/// Download blocks and the latest state.
286-
Fast {
287-
/// Skip state proof download and verification.
288-
skip_proofs: bool,
289-
/// Download indexed transactions for recent blocks.
290-
storage_chain_mode: bool,
291-
},
292-
/// Warp sync - verify authority set transitions and the latest state.
293-
Warp,
294-
}
295-
296-
impl SyncMode {
297-
/// Returns if `self` is [`Self::Warp`].
298-
pub fn is_warp(&self) -> bool {
299-
matches!(self, Self::Warp)
300-
}
301-
302-
/// Returns if `self` is [`Self::Fast`].
303-
pub fn is_fast(&self) -> bool {
304-
matches!(self, Self::Fast { .. })
305-
}
306-
}
307-
308-
impl Default for SyncMode {
309-
fn default() -> Self {
310-
Self::Full
311-
}
312-
}
313-
314280
/// The configuration of a node's secret key, describing the type of key
315281
/// and how it is obtained. A node's identity keypair is the result of
316282
/// the evaluation of the node key configuration.

client/network/sync/src/engine.rs

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,16 @@ use prometheus_endpoint::{
3636
use sc_client_api::{BlockBackend, HeaderBackend, ProofProvider};
3737
use sc_consensus::import_queue::ImportQueueService;
3838
use sc_network::{
39-
config::{
40-
FullNetworkConfiguration, NonDefaultSetConfig, ProtocolId, SyncMode as SyncOperationMode,
41-
},
39+
config::{FullNetworkConfiguration, NonDefaultSetConfig, ProtocolId},
4240
utils::LruHashSet,
4341
NotificationsSink, ProtocolName, ReputationChange,
4442
};
4543
use sc_network_common::{
4644
role::Roles,
4745
sync::{
48-
message::{
49-
generic::{BlockData, BlockResponse},
50-
BlockAnnounce, BlockAnnouncesHandshake, BlockState,
51-
},
46+
message::{BlockAnnounce, BlockAnnouncesHandshake, BlockState},
5247
warp::WarpSyncParams,
5348
BadPeer, ChainSync as ChainSyncT, ExtendedPeerInfo, PollBlockAnnounceValidation, SyncEvent,
54-
SyncMode,
5549
},
5650
};
5751
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
@@ -290,12 +284,7 @@ where
290284
warp_sync_protocol_name: Option<ProtocolName>,
291285
rx: sc_utils::mpsc::TracingUnboundedReceiver<sc_network::SyncEvent<B>>,
292286
) -> Result<(Self, SyncingService<B>, NonDefaultSetConfig), ClientError> {
293-
let mode = match net_config.network_config.sync_mode {
294-
SyncOperationMode::Full => SyncMode::Full,
295-
SyncOperationMode::Fast { skip_proofs, storage_chain_mode } =>
296-
SyncMode::LightState { skip_proofs, storage_chain_mode },
297-
SyncOperationMode::Warp => SyncMode::Warp,
298-
};
287+
let mode = net_config.network_config.sync_mode;
299288
let max_parallel_downloads = net_config.network_config.max_parallel_downloads;
300289
let max_blocks_per_request = if net_config.network_config.max_blocks_per_request >
301290
crate::MAX_BLOCKS_IN_RESPONSE as u32
@@ -469,8 +458,8 @@ where
469458
&mut self,
470459
validation_result: PollBlockAnnounceValidation<B::Header>,
471460
) {
472-
let (header, _is_best, who) = match validation_result {
473-
PollBlockAnnounceValidation::Skip => return,
461+
match validation_result {
462+
PollBlockAnnounceValidation::Skip => {},
474463
PollBlockAnnounceValidation::Nothing { is_best: _, who, announce } => {
475464
self.update_peer_info(&who);
476465

@@ -479,19 +468,6 @@ where
479468
self.block_announce_data_cache.put(announce.header.hash(), data);
480469
}
481470
}
482-
483-
return
484-
},
485-
PollBlockAnnounceValidation::ImportHeader { announce, is_best, who } => {
486-
self.update_peer_info(&who);
487-
488-
if let Some(data) = announce.data {
489-
if !data.is_empty() {
490-
self.block_announce_data_cache.put(announce.header.hash(), data);
491-
}
492-
}
493-
494-
(announce.header, is_best, who)
495471
},
496472
PollBlockAnnounceValidation::Failure { who, disconnect } => {
497473
if disconnect {
@@ -500,31 +476,8 @@ where
500476
}
501477

502478
self.network_service.report_peer(who, rep::BAD_BLOCK_ANNOUNCEMENT);
503-
return
504479
},
505-
};
506-
507-
// to import header from announced block let's construct response to request that normally
508-
// would have been sent over network (but it is not in our case)
509-
let blocks_to_import = self.chain_sync.on_block_data(
510-
&who,
511-
None,
512-
BlockResponse {
513-
id: 0,
514-
blocks: vec![BlockData {
515-
hash: header.hash(),
516-
header: Some(header),
517-
body: None,
518-
indexed_body: None,
519-
receipt: None,
520-
message_queue: None,
521-
justification: None,
522-
justifications: None,
523-
}],
524-
},
525-
);
526-
527-
self.chain_sync.process_block_response_data(blocks_to_import);
480+
}
528481
}
529482

530483
/// Push a block announce validation.

client/network/sync/src/lib.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,19 +1003,6 @@ where
10031003
Ok(self.validate_and_queue_blocks(new_blocks, gap))
10041004
}
10051005

1006-
fn process_block_response_data(&mut self, blocks_to_import: Result<OnBlockData<B>, BadPeer>) {
1007-
match blocks_to_import {
1008-
Ok(OnBlockData::Import(origin, blocks)) => self.import_blocks(origin, blocks),
1009-
Ok(OnBlockData::Request(peer, req)) => self.send_block_request(peer, req),
1010-
Ok(OnBlockData::Continue) => {},
1011-
Err(BadPeer(id, repu)) => {
1012-
self.network_service
1013-
.disconnect_peer(id, self.block_announce_protocol_name.clone());
1014-
self.network_service.report_peer(id, repu);
1015-
},
1016-
}
1017-
}
1018-
10191006
fn on_block_justification(
10201007
&mut self,
10211008
who: PeerId,
@@ -1499,7 +1486,6 @@ where
14991486
match self.mode {
15001487
SyncMode::Full =>
15011488
BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION | BlockAttributes::BODY,
1502-
SyncMode::Light => BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION,
15031489
SyncMode::LightState { storage_chain_mode: false, .. } | SyncMode::Warp =>
15041490
BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION | BlockAttributes::BODY,
15051491
SyncMode::LightState { storage_chain_mode: true, .. } =>
@@ -1512,7 +1498,6 @@ where
15121498
fn skip_execution(&self) -> bool {
15131499
match self.mode {
15141500
SyncMode::Full => false,
1515-
SyncMode::Light => true,
15161501
SyncMode::LightState { .. } => true,
15171502
SyncMode::Warp => true,
15181503
}
@@ -1759,18 +1744,6 @@ where
17591744
return PollBlockAnnounceValidation::Nothing { is_best, who, announce }
17601745
}
17611746

1762-
let requires_additional_data = self.mode != SyncMode::Light || !known_parent;
1763-
if !requires_additional_data {
1764-
trace!(
1765-
target: "sync",
1766-
"Importing new header announced from {}: {} {:?}",
1767-
who,
1768-
hash,
1769-
announce.header,
1770-
);
1771-
return PollBlockAnnounceValidation::ImportHeader { is_best, announce, who }
1772-
}
1773-
17741747
if self.status().state == SyncState::Idle {
17751748
trace!(
17761749
target: "sync",

client/network/sync/src/mock.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ mockall::mock! {
5959
request: Option<BlockRequest<Block>>,
6060
response: BlockResponse<Block>,
6161
) -> Result<OnBlockData<Block>, BadPeer>;
62-
fn process_block_response_data(&mut self, blocks_to_import: Result<OnBlockData<Block>, BadPeer>);
6362
fn on_block_justification(
6463
&mut self,
6564
who: PeerId,

client/network/test/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ where
770770
*genesis_extra_storage = storage;
771771
}
772772

773-
if matches!(config.sync_mode, SyncMode::Fast { .. } | SyncMode::Warp) {
773+
if matches!(config.sync_mode, SyncMode::LightState { .. } | SyncMode::Warp) {
774774
test_client_builder = test_client_builder.set_no_genesis();
775775
}
776776
let backend = test_client_builder.backend();

client/network/test/src/sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ async fn syncs_state() {
11321132
let mut config_two = FullPeerConfig::default();
11331133
config_two.extra_storage = Some(genesis_storage);
11341134
config_two.sync_mode =
1135-
SyncMode::Fast { skip_proofs: *skip_proofs, storage_chain_mode: false };
1135+
SyncMode::LightState { skip_proofs: *skip_proofs, storage_chain_mode: false };
11361136
net.add_full_peer_with_config(config_two);
11371137
let hashes = net.peer(0).push_blocks(64, false);
11381138
// Wait for peer 1 to sync header chain.
@@ -1175,7 +1175,7 @@ async fn syncs_indexed_blocks() {
11751175
net.add_full_peer_with_config(FullPeerConfig { storage_chain: true, ..Default::default() });
11761176
net.add_full_peer_with_config(FullPeerConfig {
11771177
storage_chain: true,
1178-
sync_mode: SyncMode::Fast { skip_proofs: false, storage_chain_mode: true },
1178+
sync_mode: SyncMode::LightState { skip_proofs: false, storage_chain_mode: true },
11791179
..Default::default()
11801180
});
11811181
net.peer(0).generate_blocks_at(

client/service/src/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ where
226226
wasm_runtime_overrides: config.wasm_runtime_overrides.clone(),
227227
no_genesis: matches!(
228228
config.network.sync_mode,
229-
SyncMode::Fast { .. } | SyncMode::Warp { .. }
229+
SyncMode::LightState { .. } | SyncMode::Warp { .. }
230230
),
231231
wasm_runtime_substitutes,
232232
},
@@ -794,7 +794,8 @@ where
794794

795795
if client.requires_full_sync() {
796796
match config.network.sync_mode {
797-
SyncMode::Fast { .. } => return Err("Fast sync doesn't work for archive nodes".into()),
797+
SyncMode::LightState { .. } =>
798+
return Err("Fast sync doesn't work for archive nodes".into()),
798799
SyncMode::Warp => return Err("Warp sync doesn't work for archive nodes".into()),
799800
SyncMode::Full => {},
800801
}

client/service/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl Configuration {
234234
/// Returns true if the genesis state writting will be skipped while initializing the genesis
235235
/// block.
236236
pub fn no_genesis(&self) -> bool {
237-
matches!(self.network.sync_mode, SyncMode::Fast { .. } | SyncMode::Warp { .. })
237+
matches!(self.network.sync_mode, SyncMode::LightState { .. } | SyncMode::Warp { .. })
238238
}
239239

240240
/// Returns the database config for creating the backend.

0 commit comments

Comments
 (0)