Skip to content

Commit

Permalink
Preparing for async backing support (#1491)
Browse files Browse the repository at this point in the history
* Preparing for async backing support

* Preparing for async backing support(Phase 2)

* Increase authoring_duration from 500 milliseconds to 1500
  • Loading branch information
hqwangningbo authored Nov 9, 2024
1 parent f251aac commit 18328f8
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 87 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ cumulus-pallet-aura-ext = { git = "https://github.com/parityt
cumulus-pallet-parachain-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
cumulus-pallet-xcm = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
cumulus-pallet-xcmp-queue = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
cumulus-primitives-aura = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
cumulus-primitives-core = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
cumulus-primitives-parachain-inherent = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
cumulus-primitives-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.13.0", default-features = false }
Expand Down
33 changes: 19 additions & 14 deletions node/service/src/collator_kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@
//! Service implementation. Specialized wrapper over substrate service.
use std::{sync::Arc, time::Duration};

use crate::eth::EthConfiguration;
#[cfg(any(feature = "with-bifrost-kusama-runtime", feature = "with-bifrost-runtime"))]
pub use bifrost_kusama_runtime;
use bifrost_kusama_runtime::RuntimeApi;
use bifrost_primitives::Block;
use cumulus_client_cli::CollatorOptions;
use cumulus_client_collator::service::CollatorService;
use cumulus_client_consensus_aura::collators::basic::{
self as basic_aura, Params as BasicAuraParams,
};

use cumulus_client_consensus_aura::collators::lookahead::{self as aura, Params as AuraParams};
use cumulus_client_consensus_common::ParachainBlockImport as TParachainBlockImport;
use cumulus_client_consensus_proposer::Proposer;

use crate::eth::EthConfiguration;
use bifrost_primitives::Block;
use cumulus_client_service::{
build_network, build_relay_chain_interface, prepare_node_config, start_relay_chain_tasks,
BuildNetworkParams, CollatorSybilResistance, DARecoveryProfile, StartRelayChainTasksParams,
};
use cumulus_primitives_core::{relay_chain::Hash, ParaId};
use cumulus_primitives_core::{
relay_chain::{Hash, ValidationCode},
ParaId,
};
use cumulus_relay_chain_interface::{OverseerHandle, RelayChainInterface};
use polkadot_primitives::CollatorPair;
use sc_client_api::backend::Backend;
Expand Down Expand Up @@ -199,6 +198,7 @@ fn build_import_queue(

fn start_consensus(
client: Arc<FullClient>,
backend: Arc<FullBackend>,
block_import: ParachainBlockImport,
prometheus_registry: Option<&Registry>,
telemetry: Option<TelemetryHandle>,
Expand Down Expand Up @@ -230,11 +230,15 @@ fn start_consensus(
client.clone(),
);

let params = BasicAuraParams {
let params = AuraParams {
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
block_import,
para_client: client,
para_client: client.clone(),
para_backend: backend.clone(),
relay_client: relay_chain_interface,
code_hash_provider: move |block_hash| {
client.code_at(block_hash).ok().map(|c| ValidationCode::from(c).hash())
},
sync_oracle,
keystore,
collator_key,
Expand All @@ -244,12 +248,12 @@ fn start_consensus(
proposer,
collator_service,
// Very limited proposal time.
authoring_duration: Duration::from_millis(500),
collation_request_receiver: None,
authoring_duration: Duration::from_millis(1500),
reinitialize: false,
};

let fut =
basic_aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _>(
aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _, _, _>(
params,
);
task_manager.spawn_essential_handle().spawn("aura", None, fut);
Expand Down Expand Up @@ -359,7 +363,7 @@ where
task_manager: &mut task_manager,
config: parachain_config,
keystore: params.keystore_container.keystore(),
backend,
backend: backend.clone(),
network: network.clone(),
sync_service: sync_service.clone(),
system_rpc_tx,
Expand Down Expand Up @@ -411,6 +415,7 @@ where
if validator {
start_consensus(
client.clone(),
backend.clone(),
block_import,
prometheus_registry.as_ref(),
telemetry.as_ref().map(|t| t.handle()),
Expand Down
39 changes: 23 additions & 16 deletions node/service/src/collator_polkadot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ use std::{
time::Duration,
};

use crate::{
chain_spec, eth,
eth::{EthConfiguration, FrontierBackend},
IdentifyVariant,
};
#[cfg(any(feature = "with-bifrost-polkadot-runtime", feature = "with-bifrost-runtime"))]
pub use bifrost_polkadot_runtime;
use bifrost_polkadot_runtime::{RuntimeApi, TransactionConverter};
use bifrost_primitives::Block;
use cumulus_client_cli::CollatorOptions;
use cumulus_client_collator::service::CollatorService;
use cumulus_client_consensus_aura::collators::basic::{
self as basic_aura, Params as BasicAuraParams,
};

use cumulus_client_consensus_aura::collators::lookahead::{self as aura, Params as AuraParams};
use cumulus_client_consensus_common::ParachainBlockImport as TParachainBlockImport;
use cumulus_client_consensus_proposer::Proposer;

use crate::{chain_spec, eth, IdentifyVariant};
use bifrost_primitives::Block;
use cumulus_client_service::{
build_network, build_relay_chain_interface, prepare_node_config, start_relay_chain_tasks,
BuildNetworkParams, CollatorSybilResistance, DARecoveryProfile, StartRelayChainTasksParams,
};
use cumulus_primitives_core::{relay_chain::Hash, ParaId};
use cumulus_primitives_core::{
relay_chain::{Hash, ValidationCode},
ParaId,
};
use cumulus_relay_chain_interface::{OverseerHandle, RelayChainInterface};
use fc_rpc_core::types::{FeeHistoryCache, FilterPool};
use fc_storage::StorageOverrideHandler;
Expand All @@ -57,8 +60,6 @@ use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sp_keystore::KeystorePtr;
use substrate_prometheus_endpoint::Registry;

use crate::eth::{EthConfiguration, FrontierBackend};

#[cfg(not(feature = "runtime-benchmarks"))]
type HostFunctions = cumulus_client_service::ParachainHostFunctions;

Expand Down Expand Up @@ -239,6 +240,7 @@ fn build_import_queue(

fn start_consensus(
client: Arc<FullClient>,
backend: Arc<FullBackend>,
block_import: eth::BlockImport<Block, ParachainBlockImport, FullClient>,
prometheus_registry: Option<&Registry>,
telemetry: Option<TelemetryHandle>,
Expand Down Expand Up @@ -270,11 +272,15 @@ fn start_consensus(
client.clone(),
);

let params = BasicAuraParams {
let params = AuraParams {
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
block_import,
para_client: client,
para_client: client.clone(),
para_backend: backend.clone(),
relay_client: relay_chain_interface,
code_hash_provider: move |block_hash| {
client.code_at(block_hash).ok().map(|c| ValidationCode::from(c).hash())
},
sync_oracle,
keystore,
collator_key,
Expand All @@ -284,12 +290,12 @@ fn start_consensus(
proposer,
collator_service,
// Very limited proposal time.
authoring_duration: Duration::from_millis(500),
collation_request_receiver: None,
authoring_duration: Duration::from_millis(1500),
reinitialize: false,
};

let fut =
basic_aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _>(
aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _, _, _>(
params,
);
task_manager.spawn_essential_handle().spawn("aura", None, fut);
Expand Down Expand Up @@ -473,7 +479,7 @@ where
eth::spawn_frontier_tasks(
&task_manager,
client.clone(),
backend,
backend.clone(),
frontier_backend,
filter_pool,
storage_override,
Expand Down Expand Up @@ -527,6 +533,7 @@ where
if validator {
start_consensus(
client.clone(),
backend.clone(),
block_import,
prometheus_registry.as_ref(),
telemetry.as_ref().map(|t| t.handle()),
Expand Down
2 changes: 2 additions & 0 deletions runtime/bifrost-kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ cumulus-pallet-aura-ext = { workspace = true }
cumulus-pallet-parachain-system = { workspace = true }
cumulus-pallet-xcm = { workspace = true }
cumulus-pallet-xcmp-queue = { workspace = true }
cumulus-primitives-aura = { workspace = true }
cumulus-primitives-core = { workspace = true }
cumulus-primitives-timestamp = { workspace = true }
cumulus-primitives-utility = { workspace = true }
Expand Down Expand Up @@ -213,6 +214,7 @@ std = [
"cumulus-pallet-parachain-system/std",
"cumulus-pallet-xcm/std",
"cumulus-pallet-xcmp-queue/std",
"cumulus-primitives-aura/std",
"cumulus-primitives-core/std",
"cumulus-primitives-timestamp/std",
"cumulus-primitives-utility/std",
Expand Down
50 changes: 21 additions & 29 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub use bifrost_runtime_common::{
};
use bifrost_slp::QueryId;
use constants::currency::*;
use cumulus_pallet_parachain_system::{RelayNumberStrictlyIncreases, RelaychainDataProvider};
use cumulus_pallet_parachain_system::{RelayNumberMonotonicallyIncreases, RelaychainDataProvider};
use cumulus_primitives_core::AggregateMessageOrigin;
use frame_support::{
dispatch::DispatchClass,
Expand Down Expand Up @@ -999,6 +999,13 @@ parameter_types! {
pub const RelayOrigin: AggregateMessageOrigin = AggregateMessageOrigin::Parent;
}

type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
Runtime,
RELAY_CHAIN_SLOT_DURATION_MILLIS,
BLOCK_PROCESSING_VELOCITY,
UNINCLUDED_SEGMENT_CAPACITY,
>;

impl cumulus_pallet_parachain_system::Config for Runtime {
type DmpQueue = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>;
type RuntimeEvent = RuntimeEvent;
Expand All @@ -1008,8 +1015,8 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type ReservedXcmpWeight = ReservedXcmpWeight;
type SelfParaId = parachain_info::Pallet<Runtime>;
type XcmpMessageHandler = XcmpQueue;
type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases;
type ConsensusHook = cumulus_pallet_parachain_system::ExpectParentIncluded;
type CheckAssociatedRelayNumber = RelayNumberMonotonicallyIncreases;
type ConsensusHook = ConsensusHook;
type WeightInfo = cumulus_pallet_parachain_system::weights::SubstrateWeight<Runtime>;
}

Expand Down Expand Up @@ -2197,14 +2204,23 @@ impl_runtime_apis! {

impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())
sp_consensus_aura::SlotDuration::from_millis(SLOT_DURATION)
}

fn authorities() -> Vec<AuraId> {
pallet_aura::Authorities::<Runtime>::get().into_inner()
}
}

impl cumulus_primitives_aura::AuraUnincludedSegmentApi<Block> for Runtime {
fn can_build_upon(
included_hash: <Block as BlockT>::Hash,
slot: cumulus_primitives_aura::Slot,
) -> bool {
ConsensusHook::can_build_upon(included_hash, slot)
}
}

impl xcm_fee_payment_runtime_api::fees::XcmPaymentApi<Block> for Runtime {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
let acceptable_assets = AssetRegistry::asset_ids();
Expand Down Expand Up @@ -2522,31 +2538,7 @@ impl_runtime_apis! {
}
}

struct CheckInherents;
#[allow(deprecated)]
impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {
fn check_inherents(
block: &Block,
relay_state_proof: &cumulus_pallet_parachain_system::RelayChainStateProof,
) -> sp_inherents::CheckInherentsResult {
let relay_chain_slot = relay_state_proof
.read_slot()
.expect("Could not read the relay chain slot from the proof");

let inherent_data =
cumulus_primitives_timestamp::InherentDataProvider::from_relay_chain_slot_and_duration(
relay_chain_slot,
sp_std::time::Duration::from_secs(6),
)
.create_inherent_data()
.expect("Could not create the timestamp inherent data");

inherent_data.check_extrinsics(&block)
}
}

cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
CheckInherents = CheckInherents,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>
}
2 changes: 2 additions & 0 deletions runtime/bifrost-polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ cumulus-pallet-aura-ext = { workspace = true }
cumulus-pallet-parachain-system = { workspace = true }
cumulus-pallet-xcm = { workspace = true }
cumulus-pallet-xcmp-queue = { workspace = true }
cumulus-primitives-aura = { workspace = true }
cumulus-primitives-core = { workspace = true }
cumulus-primitives-timestamp = { workspace = true }
cumulus-primitives-utility = { workspace = true }
Expand Down Expand Up @@ -233,6 +234,7 @@ std = [
"cumulus-pallet-parachain-system/std",
"cumulus-pallet-xcm/std",
"cumulus-pallet-xcmp-queue/std",
"cumulus-primitives-aura/std",
"cumulus-primitives-core/std",
"cumulus-primitives-timestamp/std",
"cumulus-primitives-utility/std",
Expand Down
Loading

0 comments on commit 18328f8

Please sign in to comment.