Skip to content

Commit 7c9ebfa

Browse files
author
ordian
authored
Merge branch 'ao-fix-parainclusion-weight-overestimation' into ao-enact-candidate-weight
2 parents 26621c4 + 46c7965 commit 7c9ebfa

File tree

16 files changed

+115
-307
lines changed

16 files changed

+115
-307
lines changed

cumulus/client/service/src/lib.rs

Lines changed: 32 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ use cumulus_relay_chain_interface::{RelayChainInterface, RelayChainResult};
2828
use cumulus_relay_chain_minimal_node::{
2929
build_minimal_relay_chain_node_light_client, build_minimal_relay_chain_node_with_rpc,
3030
};
31-
use futures::{
32-
channel::{mpsc, oneshot},
33-
FutureExt, StreamExt,
34-
};
31+
use futures::{channel::mpsc, StreamExt};
3532
use polkadot_primitives::{CollatorPair, OccupiedCoreAssumption};
3633
use sc_client_api::{
3734
Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, ProofProvider, UsageProvider,
@@ -43,7 +40,7 @@ use sc_consensus::{
4340
use sc_network::{config::SyncMode, service::traits::NetworkService, NetworkBackend};
4441
use sc_network_sync::SyncingService;
4542
use sc_network_transactions::TransactionsHandlerController;
46-
use sc_service::{Configuration, NetworkStarter, SpawnTaskHandle, TaskManager, WarpSyncParams};
43+
use sc_service::{Configuration, NetworkStarter, SpawnTaskHandle, TaskManager, WarpSyncConfig};
4744
use sc_telemetry::{log, TelemetryWorkerHandle};
4845
use sc_utils::mpsc::TracingUnboundedSender;
4946
use sp_api::ProvideRuntimeApi;
@@ -467,12 +464,19 @@ where
467464
{
468465
let warp_sync_params = match parachain_config.network.sync_mode {
469466
SyncMode::Warp => {
470-
let target_block = warp_sync_get::<Block, RCInterface>(
471-
para_id,
472-
relay_chain_interface.clone(),
473-
spawn_handle.clone(),
474-
);
475-
Some(WarpSyncParams::WaitForTarget(target_block))
467+
log::debug!(target: LOG_TARGET_SYNC, "waiting for announce block...");
468+
469+
let target_block =
470+
wait_for_finalized_para_head::<Block, _>(para_id, relay_chain_interface.clone())
471+
.await
472+
.inspect_err(|e| {
473+
log::error!(
474+
target: LOG_TARGET_SYNC,
475+
"Unable to determine parachain target block {:?}",
476+
e
477+
);
478+
})?;
479+
Some(WarpSyncConfig::WithTarget(target_block))
476480
},
477481
_ => None,
478482
};
@@ -500,67 +504,37 @@ where
500504
spawn_handle,
501505
import_queue,
502506
block_announce_validator_builder: Some(Box::new(move |_| block_announce_validator)),
503-
warp_sync_params,
507+
warp_sync_config: warp_sync_params,
504508
block_relay: None,
505509
metrics,
506510
})
507511
}
508512

509-
/// Creates a new background task to wait for the relay chain to sync up and retrieve the parachain
510-
/// header
511-
fn warp_sync_get<B, RCInterface>(
512-
para_id: ParaId,
513-
relay_chain_interface: RCInterface,
514-
spawner: SpawnTaskHandle,
515-
) -> oneshot::Receiver<<B as BlockT>::Header>
516-
where
517-
B: BlockT + 'static,
518-
RCInterface: RelayChainInterface + 'static,
519-
{
520-
let (sender, receiver) = oneshot::channel::<B::Header>();
521-
spawner.spawn(
522-
"cumulus-parachain-wait-for-target-block",
523-
None,
524-
async move {
525-
log::debug!(
526-
target: LOG_TARGET_SYNC,
527-
"waiting for announce block in a background task...",
528-
);
529-
530-
let _ = wait_for_finalized_para_head::<B, _>(sender, para_id, relay_chain_interface)
531-
.await
532-
.map_err(|e| {
533-
log::error!(
534-
target: LOG_TARGET_SYNC,
535-
"Unable to determine parachain target block {:?}",
536-
e
537-
)
538-
});
539-
}
540-
.boxed(),
541-
);
542-
543-
receiver
544-
}
545-
546513
/// Waits for the relay chain to have finished syncing and then gets the parachain header that
547514
/// corresponds to the last finalized relay chain block.
548515
async fn wait_for_finalized_para_head<B, RCInterface>(
549-
sender: oneshot::Sender<<B as BlockT>::Header>,
550516
para_id: ParaId,
551517
relay_chain_interface: RCInterface,
552-
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
518+
) -> sc_service::error::Result<<B as BlockT>::Header>
553519
where
554520
B: BlockT + 'static,
555521
RCInterface: RelayChainInterface + Send + 'static,
556522
{
557-
let mut imported_blocks = relay_chain_interface.import_notification_stream().await?.fuse();
558-
while imported_blocks.next().await.is_some() {
559-
let is_syncing = relay_chain_interface.is_major_syncing().await.map_err(|e| {
560-
Box::<dyn std::error::Error + Send + Sync>::from(format!(
561-
"Unable to determine sync status. {e}"
523+
let mut imported_blocks = relay_chain_interface
524+
.import_notification_stream()
525+
.await
526+
.map_err(|error| {
527+
sc_service::Error::Other(format!(
528+
"Relay chain import notification stream error when waiting for parachain head: \
529+
{error}"
562530
))
563-
})?;
531+
})?
532+
.fuse();
533+
while imported_blocks.next().await.is_some() {
534+
let is_syncing = relay_chain_interface
535+
.is_major_syncing()
536+
.await
537+
.map_err(|e| format!("Unable to determine sync status: {e}"))?;
564538

565539
if !is_syncing {
566540
let relay_chain_best_hash = relay_chain_interface
@@ -586,8 +560,7 @@ where
586560
finalized_header.number(),
587561
finalized_header.hash()
588562
);
589-
let _ = sender.send(finalized_header);
590-
return Ok(())
563+
return Ok(finalized_header)
591564
}
592565
}
593566

docs/sdk/src/polkadot_sdk/templates.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@
4040
//!
4141
//! In June 2023, OpenZeppelin was awarded a grant from the [Polkadot
4242
//! treasury](https://polkadot.polkassembly.io/treasury/406) for building a number of Polkadot-sdk
43-
//! based templates. These templates are expected to be a great starting point for developers.
44-
//!
45-
//! - <https://github.com/OpenZeppelin/polkadot-runtime-template/>
43+
//! based templates. These templates are a great starting point for developers and newcomers.
44+
//! So far OpenZeppelin has released two templates, which have been fully [audited](https://github.com/OpenZeppelin/polkadot-runtime-templates/tree/main/audits):
45+
//! - [`generic-runtime-template`](https://github.com/OpenZeppelin/polkadot-runtime-templates?tab=readme-ov-file#generic-runtime-template):
46+
//! A minimal template that has all the common pallets that parachains use with secure defaults.
47+
//! - [`evm-runtime-template`](https://github.com/OpenZeppelin/polkadot-runtime-templates/tree/main?tab=readme-ov-file#evm-template):
48+
//! This template has EVM compatibility out of the box and allows migrating your solidity contracts
49+
//! or EVM compatible dapps easily. It also uses 20 byte addresses like Ethereum and has some
50+
//! Account Abstraction support.
4651
//!
4752
//! ## POP-CLI
4853
//!

polkadot/node/service/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ pub fn new_full<
764764
) -> Result<NewFull, Error> {
765765
use polkadot_availability_recovery::FETCH_CHUNKS_THRESHOLD;
766766
use polkadot_node_network_protocol::request_response::IncomingRequest;
767-
use sc_network_sync::WarpSyncParams;
767+
use sc_network_sync::WarpSyncConfig;
768768

769769
let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
770770
let role = config.role.clone();
@@ -1037,7 +1037,7 @@ pub fn new_full<
10371037
spawn_handle: task_manager.spawn_handle(),
10381038
import_queue,
10391039
block_announce_validator_builder: None,
1040-
warp_sync_params: Some(WarpSyncParams::WithProvider(warp_sync)),
1040+
warp_sync_config: Some(WarpSyncConfig::WithProvider(warp_sync)),
10411041
block_relay: None,
10421042
metrics,
10431043
})?;

prdoc/pr_5431.prdoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
title: Remove the need to wait for target block header in warp sync implementation
2+
3+
doc:
4+
- audience: Node Dev
5+
description: |
6+
Previously warp sync needed to wait for target block header of the relay chain to become available before warp
7+
sync can start, which resulted in cumbersome APIs. Parachain initialization was refactored to initialize warp sync
8+
with target block header from the very beginning, improving and simplifying sync API.
9+
10+
crates:
11+
- name: sc-service
12+
bump: major
13+
- name: sc-network-sync
14+
bump: major
15+
- name: polkadot-service
16+
bump: major
17+
- name: cumulus-client-service
18+
bump: major
19+
- name: sc-informant
20+
bump: major

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use sc_consensus_babe::{self, SlotProportion};
3737
use sc_network::{
3838
event::Event, service::traits::NetworkService, NetworkBackend, NetworkEventStream,
3939
};
40-
use sc_network_sync::{strategy::warp::WarpSyncParams, SyncingService};
40+
use sc_network_sync::{strategy::warp::WarpSyncConfig, SyncingService};
4141
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
4242
use sc_statement_store::Store as StatementStore;
4343
use sc_telemetry::{Telemetry, TelemetryWorker};
@@ -517,7 +517,7 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
517517
spawn_handle: task_manager.spawn_handle(),
518518
import_queue,
519519
block_announce_validator_builder: None,
520-
warp_sync_params: Some(WarpSyncParams::WithProvider(warp_sync)),
520+
warp_sync_config: Some(WarpSyncConfig::WithProvider(warp_sync)),
521521
block_relay: None,
522522
metrics,
523523
})?;

substrate/client/informant/src/display.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,9 @@ impl<B: BlockT> InformantDisplay<B> {
101101
_,
102102
Some(WarpSyncProgress { phase: WarpSyncPhase::DownloadingBlocks(n), .. }),
103103
) if !sync_status.is_major_syncing() => ("⏩", "Block history".into(), format!(", #{}", n)),
104-
(
105-
_,
106-
_,
107-
Some(WarpSyncProgress { phase: WarpSyncPhase::AwaitingTargetBlock, .. }),
108-
) => ("⏩", "Waiting for pending target block".into(), "".into()),
109104
// Handle all phases besides the two phases we already handle above.
110105
(_, _, Some(warp))
111-
if !matches!(
112-
warp.phase,
113-
WarpSyncPhase::AwaitingTargetBlock | WarpSyncPhase::DownloadingBlocks(_)
114-
) =>
106+
if !matches!(warp.phase, WarpSyncPhase::DownloadingBlocks(_)) =>
115107
(
116108
"⏩",
117109
"Warping".into(),

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

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
syncing_service::{SyncingService, ToServiceCommand},
3333
},
3434
strategy::{
35-
warp::{EncodedProof, WarpProofRequest, WarpSyncParams},
35+
warp::{EncodedProof, WarpProofRequest, WarpSyncConfig},
3636
StrategyKey, SyncingAction, SyncingConfig, SyncingStrategy,
3737
},
3838
types::{
@@ -42,11 +42,7 @@ use crate::{
4242
};
4343

4444
use codec::{Decode, DecodeAll, Encode};
45-
use futures::{
46-
channel::oneshot,
47-
future::{BoxFuture, Fuse},
48-
FutureExt, StreamExt,
49-
};
45+
use futures::{channel::oneshot, FutureExt, StreamExt};
5046
use libp2p::request_response::OutboundFailure;
5147
use log::{debug, error, trace, warn};
5248
use prometheus_endpoint::{
@@ -257,10 +253,6 @@ pub struct SyncingEngine<B: BlockT, Client> {
257253
/// The `PeerId`'s of all boot nodes.
258254
boot_node_ids: HashSet<PeerId>,
259255

260-
/// A channel to get target block header if we skip over proofs downloading during warp sync.
261-
warp_sync_target_block_header_rx_fused:
262-
Fuse<BoxFuture<'static, Result<B::Header, oneshot::Canceled>>>,
263-
264256
/// Protocol name used for block announcements
265257
block_announce_protocol_name: ProtocolName,
266258

@@ -309,7 +301,7 @@ where
309301
protocol_id: ProtocolId,
310302
fork_id: &Option<String>,
311303
block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
312-
warp_sync_params: Option<WarpSyncParams<B>>,
304+
warp_sync_config: Option<WarpSyncConfig<B>>,
313305
network_service: service::network::NetworkServiceHandle,
314306
import_queue: Box<dyn ImportQueueService<B>>,
315307
block_downloader: Arc<dyn BlockDownloader<B>>,
@@ -404,19 +396,6 @@ where
404396
Arc::clone(&peer_store_handle),
405397
);
406398

407-
// Split warp sync params into warp sync config and a channel to retrieve target block
408-
// header.
409-
let (warp_sync_config, warp_sync_target_block_header_rx) =
410-
warp_sync_params.map_or((None, None), |params| {
411-
let (config, target_block_rx) = params.split();
412-
(Some(config), target_block_rx)
413-
});
414-
415-
// Make sure polling of the target block channel is a no-op if there is no block to
416-
// retrieve.
417-
let warp_sync_target_block_header_rx_fused = warp_sync_target_block_header_rx
418-
.map_or(futures::future::pending().boxed().fuse(), |rx| rx.boxed().fuse());
419-
420399
// Initialize syncing strategy.
421400
let strategy = SyncingStrategy::new(syncing_config, client.clone(), warp_sync_config)?;
422401

@@ -460,7 +439,6 @@ where
460439
genesis_hash,
461440
important_peers,
462441
default_peers_set_no_slot_connected_peers: HashSet::new(),
463-
warp_sync_target_block_header_rx_fused,
464442
boot_node_ids,
465443
default_peers_set_no_slot_peers,
466444
default_peers_set_num_full,
@@ -635,17 +613,6 @@ where
635613
Some(event) => self.process_notification_event(event),
636614
None => return,
637615
},
638-
// TODO: setting of warp sync target block should be moved to the initialization of
639-
// `SyncingEngine`, see https://github.com/paritytech/polkadot-sdk/issues/3537.
640-
warp_target_block_header = &mut self.warp_sync_target_block_header_rx_fused => {
641-
if let Err(_) = self.pass_warp_sync_target_block_header(warp_target_block_header) {
642-
error!(
643-
target: LOG_TARGET,
644-
"Failed to set warp sync target block header, terminating `SyncingEngine`.",
645-
);
646-
return
647-
}
648-
},
649616
response_event = self.pending_responses.select_next_some() =>
650617
self.process_response_event(response_event),
651618
validation_result = self.block_announce_validator.select_next_some() =>
@@ -898,22 +865,6 @@ where
898865
}
899866
}
900867

901-
fn pass_warp_sync_target_block_header(
902-
&mut self,
903-
header: Result<B::Header, oneshot::Canceled>,
904-
) -> Result<(), ()> {
905-
match header {
906-
Ok(header) => self.strategy.set_warp_sync_target_block_header(header),
907-
Err(err) => {
908-
error!(
909-
target: LOG_TARGET,
910-
"Failed to get target block for warp sync. Error: {err:?}",
911-
);
912-
Err(())
913-
},
914-
}
915-
}
916-
917868
/// Called by peer when it is disconnecting.
918869
///
919870
/// Returns a result if the handshake of this peer was indeed accepted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! Blockchain syncing implementation in Substrate.
2020
2121
pub use service::syncing_service::SyncingService;
22-
pub use strategy::warp::{WarpSyncParams, WarpSyncPhase, WarpSyncProgress};
22+
pub use strategy::warp::{WarpSyncConfig, WarpSyncPhase, WarpSyncProgress};
2323
pub use types::{SyncEvent, SyncEventStream, SyncState, SyncStatus, SyncStatusProvider};
2424

2525
mod block_announce_validator;

0 commit comments

Comments
 (0)