diff --git a/Cargo.lock b/Cargo.lock index e9145532e4..adbe678b92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -623,6 +623,7 @@ dependencies = [ "cumulus-client-consensus-relay-chain", "cumulus-client-network", "cumulus-client-service", + "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-relay-chain-inprocess-interface", @@ -762,6 +763,7 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcm", "cumulus-pallet-xcmp-queue", + "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-timestamp", "cumulus-primitives-utility", @@ -14150,6 +14152,7 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcm", "cumulus-pallet-xcmp-queue", + "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-timestamp", "cumulus-primitives-utility", @@ -14268,6 +14271,7 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcm", "cumulus-pallet-xcmp-queue", + "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-timestamp", "cumulus-primitives-utility", diff --git a/Cargo.toml b/Cargo.toml index 87562d6292..396a02fcbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -227,6 +227,7 @@ cumulus-pallet-aura-ext = { git = "https://github.com/paritytech/polkadot-sdk", cumulus-pallet-dmp-queue = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } cumulus-pallet-parachain-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } cumulus-pallet-xcmp-queue = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } +cumulus-primitives-aura = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } cumulus-primitives-core = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } cumulus-primitives-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } cumulus-primitives-utility = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.9.0", default-features = false } diff --git a/bin/collator/Cargo.toml b/bin/collator/Cargo.toml index 1e876683f6..3669fa198b 100644 --- a/bin/collator/Cargo.toml +++ b/bin/collator/Cargo.toml @@ -117,6 +117,7 @@ cumulus-client-consensus-proposer = { workspace = true } cumulus-client-consensus-relay-chain = { workspace = true } cumulus-client-network = { workspace = true } cumulus-client-service = { workspace = true } +cumulus-primitives-aura = { workspace = true } cumulus-primitives-core = { workspace = true, features = ["std"] } cumulus-primitives-parachain-inherent = { workspace = true } cumulus-relay-chain-inprocess-interface = { workspace = true } diff --git a/bin/collator/src/parachain/service.rs b/bin/collator/src/parachain/service.rs index 14547ea4bc..122826d9e7 100644 --- a/bin/collator/src/parachain/service.rs +++ b/bin/collator/src/parachain/service.rs @@ -20,21 +20,24 @@ use astar_primitives::*; use cumulus_client_cli::CollatorOptions; -use cumulus_client_consensus_aura::collators::basic as basic_aura; +use cumulus_client_consensus_aura::collators::lookahead::{self as aura, Params as AuraParams}; use cumulus_client_consensus_common::ParachainBlockImport; use cumulus_client_consensus_relay_chain::Verifier as RelayChainVerifier; use cumulus_client_service::{ prepare_node_config, start_relay_chain_tasks, BuildNetworkParams, DARecoveryProfile, StartRelayChainTasksParams, }; -use cumulus_primitives_core::ParaId; +use cumulus_primitives_aura::AuraUnincludedSegmentApi; +use cumulus_primitives_core::{ + relay_chain::{CollatorPair, ValidationCode}, + ParaId, +}; use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain; use cumulus_relay_chain_interface::{RelayChainInterface, RelayChainResult}; use cumulus_relay_chain_minimal_node::build_minimal_relay_chain_node_with_rpc; use fc_consensus::FrontierBlockImport; use fc_rpc_core::types::{FeeHistoryCache, FilterPool}; use futures::StreamExt; -use polkadot_service::CollatorPair; use sc_client_api::BlockchainEvents; use sc_consensus::{import_queue::BasicQueue, ImportQueue}; use sc_executor::NativeElseWasmExecutor; @@ -352,6 +355,7 @@ where ) -> sc_consensus::DefaultImportQueue, SC: FnOnce( Arc>>, + Arc>, ParachainBlockImport< Block, FrontierBlockImport< @@ -580,6 +584,7 @@ where if is_authority { start_consensus( client.clone(), + backend, parachain_block_import, prometheus_registry.as_ref(), telemetry.map(|t| t.handle()), @@ -675,6 +680,7 @@ where ) -> sc_consensus::DefaultImportQueue, SC: FnOnce( Arc>>, + Arc>, ParachainBlockImport< Block, FrontierBlockImport< @@ -935,6 +941,7 @@ where if is_authority { start_consensus( client.clone(), + backend, parachain_block_import, prometheus_registry.as_ref(), telemetry.map(|t| t.handle()), @@ -1064,6 +1071,7 @@ where sc_client_api::StateBackend, Executor: sc_executor::NativeExecutionDispatch + 'static, { + let cidp_client = client.clone(); let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client) .expect("AuraApi slot_duration failed!"); @@ -1076,16 +1084,21 @@ where >( client, block_import, - move |_, _| async move { - let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); - - let slot = - sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, - slot_duration, - ); - - Ok((slot, timestamp)) + move |parent_hash, _| { + let cidp_client = cidp_client.clone(); + async move { + let slot_duration = + sc_consensus_aura::standalone::slot_duration_at(&*cidp_client, parent_hash)?; + let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); + + let slot = + sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( + *timestamp, + slot_duration, + ); + + Ok((slot, timestamp)) + } }, slot_duration, &task_manager.spawn_essential_handle(), @@ -1097,6 +1110,7 @@ where /// Start collating with the `shell` runtime while waiting for an upgrade to an Aura compatible runtime. fn start_aura_consensus_fallback( client: Arc>>, + backend: Arc>, parachain_block_import: ParachainBlockImport< Block, FrontierBlockImport< @@ -1135,6 +1149,7 @@ where + sp_block_builder::BlockBuilder + fp_rpc::EthereumRuntimeRPCApi + AuraApi + + AuraUnincludedSegmentApi + cumulus_primitives_core::CollectCollationInfo, sc_client_api::StateBackendFor, Block>: sc_client_api::StateBackend, @@ -1202,9 +1217,6 @@ where } // Move to Aura consensus. - let slot_duration = - cumulus_client_consensus_aura::slot_duration(&*client).expect("aura is present; qed"); - let announce_block = { let sync_service = sync_oracle.clone(); Arc::new(move |hash, data| sync_service.announce_block(hash, data)) @@ -1217,23 +1229,28 @@ where client.clone(), ); - basic_aura::run::(basic_aura::Params { + aura::run::(AuraParams { create_inherent_data_providers: move |_, ()| async move { Ok(()) }, block_import: parachain_block_import.clone(), para_client: client.clone(), + para_backend: backend, relay_client: relay_chain_interface.clone(), + code_hash_provider: move |block_hash| { + client + .code_at(block_hash) + .ok() + .map(|c| ValidationCode::from(c).hash()) + }, sync_oracle: sync_oracle.clone(), keystore, collator_key, para_id, overseer_handle, - slot_duration, relay_chain_slot_duration: Duration::from_secs(6), proposer: cumulus_client_consensus_proposer::Proposer::new(proposer_factory), collator_service, - // We got around 500ms for proposing - authoring_duration: Duration::from_millis(500), - collation_request_receiver: Some(request_stream), + authoring_duration: Duration::from_millis(1500), + reinitialize: true, }) .await }); @@ -1246,6 +1263,7 @@ where fn start_aura_consensus( client: Arc>>, + backend: Arc>, parachain_block_import: ParachainBlockImport< Block, FrontierBlockImport< @@ -1284,6 +1302,7 @@ where + sp_block_builder::BlockBuilder + fp_rpc::EthereumRuntimeRPCApi + AuraApi + + AuraUnincludedSegmentApi + cumulus_primitives_core::CollectCollationInfo, sc_client_api::StateBackendFor, Block>: sc_client_api::StateBackend, @@ -1318,23 +1337,28 @@ where client.clone(), ); - let fut = basic_aura::run::(basic_aura::Params { + let fut = aura::run::(AuraParams { create_inherent_data_providers: move |_, ()| async move { Ok(()) }, block_import: parachain_block_import.clone(), para_client: client.clone(), + para_backend: backend, relay_client: relay_chain_interface.clone(), + code_hash_provider: move |block_hash| { + client + .code_at(block_hash) + .ok() + .map(|c| ValidationCode::from(c).hash()) + }, sync_oracle: sync_oracle.clone(), keystore, collator_key, para_id, overseer_handle, - slot_duration: cumulus_client_consensus_aura::slot_duration(&*client)?, relay_chain_slot_duration: Duration::from_secs(6), proposer: cumulus_client_consensus_proposer::Proposer::new(proposer_factory), collator_service, - // We got around 500ms for proposing - authoring_duration: Duration::from_millis(500), - collation_request_receiver: None, + authoring_duration: Duration::from_millis(1500), + reinitialize: false, }); task_manager diff --git a/runtime/astar/Cargo.toml b/runtime/astar/Cargo.toml index 9edbe67e9d..5c3df5d917 100644 --- a/runtime/astar/Cargo.toml +++ b/runtime/astar/Cargo.toml @@ -73,9 +73,10 @@ pallet-xcm-benchmarks = { workspace = true, optional = true } # cumulus dependencies cumulus-pallet-aura-ext = { workspace = true } cumulus-pallet-dmp-queue = { workspace = true } -cumulus-pallet-parachain-system = { workspace = true } +cumulus-pallet-parachain-system = { workspace = true, features = ["parameterized-consensus-hook"] } 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 } @@ -203,6 +204,7 @@ std = [ "polkadot-parachain/std", "polkadot-primitives/std", "polkadot-runtime-common/std", + "cumulus-primitives-aura/std", "cumulus-primitives-core/std", "cumulus-primitives-utility/std", "cumulus-primitives-timestamp/std", diff --git a/runtime/astar/src/lib.rs b/runtime/astar/src/lib.rs index b860540ab9..21922b03a2 100644 --- a/runtime/astar/src/lib.rs +++ b/runtime/astar/src/lib.rs @@ -22,7 +22,6 @@ // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256. #![recursion_limit = "256"] -use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::AggregateMessageOrigin; use frame_support::{ construct_runtime, @@ -131,11 +130,21 @@ pub const fn contracts_deposit(items: u32, bytes: u32) -> Balance { /// Change this to adjust the block time. pub const MILLISECS_PER_BLOCK: u64 = 12000; +pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK; // Time is measured by number of blocks. pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); pub const HOURS: BlockNumber = MINUTES * 60; pub const DAYS: BlockNumber = HOURS * 24; +/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the +/// relay chain. +pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1; +/// How many parachain blocks are processed by the relay chain per parent. Limits the number of +/// blocks authored per slot. +pub const BLOCK_PROCESSING_VELOCITY: u32 = 1; +/// Relay chain slot duration, in milliseconds. +pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000; + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); @@ -461,22 +470,24 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type ReservedDmpWeight = ReservedDmpWeight; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases; + type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; + type ConsensusHook = ConsensusHook; type WeightInfo = cumulus_pallet_parachain_system::weights::SubstrateWeight; } -impl parachain_info::Config for Runtime {} +type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook< + Runtime, + RELAY_CHAIN_SLOT_DURATION_MILLIS, + BLOCK_PROCESSING_VELOCITY, + UNINCLUDED_SEGMENT_CAPACITY, +>; -parameter_types! { - pub const MaxAuthorities: u32 = 250; -} +impl parachain_info::Config for Runtime {} impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; type DisabledValidators = (); - type MaxAuthorities = MaxAuthorities; - // Should be only enabled (`true`) when async backing is enabled - // otherwise set to `false` + type MaxAuthorities = ConstU32<250>; type AllowMultipleBlocksPerSlot = ConstBool; } @@ -1387,7 +1398,7 @@ impl_runtime_apis! { impl sp_consensus_aura::AuraApi 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 { @@ -1395,6 +1406,15 @@ impl_runtime_apis! { } } + impl cumulus_primitives_aura::AuraUnincludedSegmentApi for Runtime { + fn can_build_upon( + included_hash: ::Hash, + slot: cumulus_primitives_aura::Slot, + ) -> bool { + ConsensusHook::can_build_upon(included_hash, slot) + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) @@ -2185,29 +2205,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -impl cumulus_pallet_parachain_system::CheckInherents 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::, - CheckInherents = CheckInherents, } diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index e9bc2a3655..ee32d7a9f2 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -265,16 +265,11 @@ impl frame_system::Config for Runtime { type PostTransactions = (); } -parameter_types! { - pub const MaxAuthorities: u32 = 50; - pub const AllowMultipleBlocksPerSlot: bool = false; -} - impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; type DisabledValidators = (); type MaxAuthorities = ConstU32<50>; - type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot; + type AllowMultipleBlocksPerSlot = ConstBool; } impl pallet_grandpa::Config for Runtime { @@ -284,7 +279,7 @@ impl pallet_grandpa::Config for Runtime { type EquivocationReportSystem = (); type WeightInfo = (); - type MaxAuthorities = MaxAuthorities; + type MaxAuthorities = ConstU32<50>; type MaxSetIdSessionEntries = ConstU64<0>; type MaxNominators = ConstU32<0>; } @@ -1364,7 +1359,7 @@ impl_runtime_apis! { impl sp_consensus_aura::AuraApi 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 { diff --git a/runtime/shibuya/Cargo.toml b/runtime/shibuya/Cargo.toml index a4a80a48f4..a68ec0fd6f 100644 --- a/runtime/shibuya/Cargo.toml +++ b/runtime/shibuya/Cargo.toml @@ -79,9 +79,10 @@ pallet-vesting = { workspace = true } # cumulus dependencies cumulus-pallet-aura-ext = { workspace = true } cumulus-pallet-dmp-queue = { workspace = true } -cumulus-pallet-parachain-system = { workspace = true } +cumulus-pallet-parachain-system = { workspace = true, features = ["parameterized-consensus-hook"] } 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 } @@ -227,6 +228,7 @@ std = [ "polkadot-parachain/std", "polkadot-primitives/std", "polkadot-runtime-common/std", + "cumulus-primitives-aura/std", "cumulus-primitives-core/std", "cumulus-primitives-utility/std", "cumulus-primitives-timestamp/std", diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index e00ad7bb26..9dba42c6a9 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -22,7 +22,6 @@ // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 512. #![recursion_limit = "512"] -use cumulus_pallet_parachain_system::AnyRelayNumber; use cumulus_primitives_core::AggregateMessageOrigin; use frame_support::{ construct_runtime, @@ -32,9 +31,9 @@ use frame_support::{ traits::{ fungible::HoldConsideration, tokens::{PayFromAccount, UnityAssetBalanceConversion}, - AsEnsureOriginWithArg, ConstU128, ConstU32, Contains, Currency, EqualPrivilegeOnly, - FindAuthor, Get, Imbalance, InstanceFilter, LinearStoragePrice, Nothing, OnFinalize, - OnUnbalanced, WithdrawReasons, + AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, Contains, Currency, + EqualPrivilegeOnly, FindAuthor, Get, Imbalance, InstanceFilter, LinearStoragePrice, + Nothing, OnFinalize, OnUnbalanced, WithdrawReasons, }, weights::{ constants::{ @@ -58,7 +57,7 @@ use pallet_transaction_payment::{ use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen}; use polkadot_runtime_common::BlockHashCount; use sp_api::impl_runtime_apis; -use sp_core::{ConstBool, OpaqueMetadata, H160, H256, U256}; +use sp_core::{OpaqueMetadata, H160, H256, U256}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, @@ -144,11 +143,21 @@ pub const fn contracts_deposit(items: u32, bytes: u32) -> Balance { /// Change this to adjust the block time. pub const MILLISECS_PER_BLOCK: u64 = 12000; +pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK; // Time is measured by number of blocks. pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); pub const HOURS: BlockNumber = MINUTES * 60; pub const DAYS: BlockNumber = HOURS * 24; +/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the +/// relay chain. +pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1; +/// How many parachain blocks are processed by the relay chain per parent. Limits the number of +/// blocks authored per slot. +pub const BLOCK_PROCESSING_VELOCITY: u32 = 1; +/// Relay chain slot duration, in milliseconds. +pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000; + impl AddressToAssetId for Runtime { fn address_to_asset_id(address: H160) -> Option { let mut data = [0u8; 16]; @@ -538,24 +547,25 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; // Shibuya is subject to relay changes so we don't enforce increasing relay number - type CheckAssociatedRelayNumber = AnyRelayNumber; + type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::AnyRelayNumber; + type ConsensusHook = ConsensusHook; type WeightInfo = cumulus_pallet_parachain_system::weights::SubstrateWeight; } -impl parachain_info::Config for Runtime {} +type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook< + Runtime, + RELAY_CHAIN_SLOT_DURATION_MILLIS, + BLOCK_PROCESSING_VELOCITY, + UNINCLUDED_SEGMENT_CAPACITY, +>; -parameter_types! { - pub const MaxAuthorities: u32 = 250; - // Should be only enabled (`true`) when async backing is enabled - // otherwise set to `false` - pub const AllowMultipleBlocksPerSlot: bool = false; -} +impl parachain_info::Config for Runtime {} impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; type DisabledValidators = (); - type MaxAuthorities = MaxAuthorities; - type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot; + type MaxAuthorities = ConstU32<250>; + type AllowMultipleBlocksPerSlot = ConstBool; } impl cumulus_pallet_aura_ext::Config for Runtime {} @@ -1735,7 +1745,7 @@ impl_runtime_apis! { impl sp_consensus_aura::AuraApi 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 { @@ -1743,6 +1753,15 @@ impl_runtime_apis! { } } + impl cumulus_primitives_aura::AuraUnincludedSegmentApi for Runtime { + fn can_build_upon( + included_hash: ::Hash, + slot: cumulus_primitives_aura::Slot, + ) -> bool { + ConsensusHook::can_build_upon(included_hash, slot) + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) @@ -2533,29 +2552,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -impl cumulus_pallet_parachain_system::CheckInherents 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::, - CheckInherents = CheckInherents, } diff --git a/runtime/shiden/Cargo.toml b/runtime/shiden/Cargo.toml index bb1ac94d32..56fddfdeef 100644 --- a/runtime/shiden/Cargo.toml +++ b/runtime/shiden/Cargo.toml @@ -75,9 +75,10 @@ pallet-xcm-benchmarks = { workspace = true, optional = true } # cumulus dependencies cumulus-pallet-aura-ext = { workspace = true } cumulus-pallet-dmp-queue = { workspace = true } -cumulus-pallet-parachain-system = { workspace = true } +cumulus-pallet-parachain-system = { workspace = true, features = ["parameterized-consensus-hook"] } 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 } @@ -207,6 +208,7 @@ std = [ "polkadot-parachain/std", "polkadot-primitives/std", "polkadot-runtime-common/std", + "cumulus-primitives-aura/std", "cumulus-primitives-core/std", "cumulus-primitives-utility/std", "cumulus-primitives-timestamp/std", diff --git a/runtime/shiden/src/lib.rs b/runtime/shiden/src/lib.rs index d171fd0752..6432aaf765 100644 --- a/runtime/shiden/src/lib.rs +++ b/runtime/shiden/src/lib.rs @@ -22,7 +22,6 @@ // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256. #![recursion_limit = "256"] -use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::AggregateMessageOrigin; use frame_support::{ construct_runtime, @@ -30,7 +29,7 @@ use frame_support::{ genesis_builder_helper::{build_config, create_default_config}, parameter_types, traits::{ - AsEnsureOriginWithArg, ConstU32, Contains, Currency, FindAuthor, Get, Imbalance, + AsEnsureOriginWithArg, ConstBool, ConstU32, Contains, Currency, FindAuthor, Get, Imbalance, InstanceFilter, Nothing, OnFinalize, OnUnbalanced, WithdrawReasons, }, weights::{ @@ -55,7 +54,7 @@ use pallet_transaction_payment::{ use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen}; use polkadot_runtime_common::BlockHashCount; use sp_api::impl_runtime_apis; -use sp_core::{ConstBool, OpaqueMetadata, H160, H256, U256}; +use sp_core::{OpaqueMetadata, H160, H256, U256}; use sp_inherents::{CheckInherentsResult, InherentData}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, @@ -133,11 +132,21 @@ pub const fn contracts_deposit(items: u32, bytes: u32) -> Balance { /// Change this to adjust the block time. pub const MILLISECS_PER_BLOCK: u64 = 12000; +pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK; // Time is measured by number of blocks. pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); pub const HOURS: BlockNumber = MINUTES * 60; pub const DAYS: BlockNumber = HOURS * 24; +/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included into the +/// relay chain. +pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1; +/// How many parachain blocks are processed by the relay chain per parent. Limits the number of +/// blocks authored per slot. +pub const BLOCK_PROCESSING_VELOCITY: u32 = 1; +/// Relay chain slot duration, in milliseconds. +pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000; + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); @@ -500,24 +509,25 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type ReservedDmpWeight = ReservedDmpWeight; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases; + type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; + type ConsensusHook = ConsensusHook; type WeightInfo = cumulus_pallet_parachain_system::weights::SubstrateWeight; } -impl parachain_info::Config for Runtime {} +type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook< + Runtime, + RELAY_CHAIN_SLOT_DURATION_MILLIS, + BLOCK_PROCESSING_VELOCITY, + UNINCLUDED_SEGMENT_CAPACITY, +>; -parameter_types! { - pub const MaxAuthorities: u32 = 250; - // Should be only enabled (`true`) when async backing is enabled - // otherwise set to `false` - pub const AllowMultipleBlocksPerSlot: bool = false; -} +impl parachain_info::Config for Runtime {} impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; type DisabledValidators = (); - type MaxAuthorities = MaxAuthorities; - type AllowMultipleBlocksPerSlot = AllowMultipleBlocksPerSlot; + type MaxAuthorities = ConstU32<250>; + type AllowMultipleBlocksPerSlot = ConstBool; } impl cumulus_pallet_aura_ext::Config for Runtime {} @@ -1391,7 +1401,7 @@ impl_runtime_apis! { impl sp_consensus_aura::AuraApi 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 { @@ -1399,6 +1409,15 @@ impl_runtime_apis! { } } + impl cumulus_primitives_aura::AuraUnincludedSegmentApi for Runtime { + fn can_build_upon( + included_hash: ::Hash, + slot: cumulus_primitives_aura::Slot, + ) -> bool { + ConsensusHook::can_build_upon(included_hash, slot) + } + } + impl sp_block_builder::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ::Extrinsic) -> ApplyExtrinsicResult { Executive::apply_extrinsic(extrinsic) @@ -2191,29 +2210,7 @@ impl_runtime_apis! { } } -struct CheckInherents; - -impl cumulus_pallet_parachain_system::CheckInherents 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::, - CheckInherents = CheckInherents, } diff --git a/tests/integration/src/setup.rs b/tests/integration/src/setup.rs index a6d08bb828..a11b7a5a3b 100644 --- a/tests/integration/src/setup.rs +++ b/tests/integration/src/setup.rs @@ -245,11 +245,9 @@ impl ExtBuilder { pallet_dapp_staking_v3::Safeguard::::put(false); // Ensure the initial state is set for the first block - assert_ok!(Timestamp::set( - RuntimeOrigin::none(), - ::MinimumPeriod::get() - )); AllPalletsWithSystem::on_initialize(1); + set_timestamp(); + set_validation_data(); }); ext } @@ -265,15 +263,23 @@ pub fn new_test_ext() -> sp_io::TestExternalities { .build() } +fn set_timestamp() { + assert_ok!(Timestamp::set( + RuntimeOrigin::none(), + pallet_timestamp::Now::::get() + SLOT_DURATION + )); +} + fn set_validation_data() { let block_number = System::block_number(); let para_id = ::SelfParaId::get(); + let parent_head = HeadData(b"deadbeef".into()); let sproof_builder = RelayStateSproofBuilder { para_id, + included_para_head: Some(parent_head.clone()), ..Default::default() }; - let parent_head = HeadData(System::parent_hash().encode()); let (relay_parent_storage_root, relay_chain_state) = sproof_builder.into_state_root_and_proof(); let para_inherent_data = ParachainInherentData { validation_data: PersistedValidationData { @@ -295,21 +301,30 @@ fn set_validation_data() { pub fn run_to_block(n: BlockNumber) { while System::block_number() < n { - set_validation_data(); - let block_number = System::block_number(); + + // finalize block + AllPalletsWithSystem::on_idle(block_number, Weight::MAX.div(2)); AllPalletsWithSystem::on_finalize(block_number); - System::set_block_number(block_number + 1); - assert_ok!(Timestamp::set( - RuntimeOrigin::none(), - pallet_timestamp::Now::::get() - + ::MinimumPeriod::get() - + 1 + // Mock some storage to make consensus hook happy + sp_io::storage::clear(&frame_support::storage::storage_prefix( + b"ParachainSystem", + b"UnincludedSegment", )); + if let Some((slot, _authored)) = AuraExt::slot_info() { + sp_io::storage::set( + &frame_support::storage::storage_prefix(b"AuraExt", b"SlotInfo"), + &(slot, 0u32).encode(), + ); + } + // initialize block + System::set_block_number(block_number + 1); AllPalletsWithSystem::on_initialize(block_number + 1); - AllPalletsWithSystem::on_idle(block_number + 1, Weight::MAX); + // apply inherent + set_timestamp(); + set_validation_data(); } } diff --git a/third-party/zombienet/single_parachain.toml b/third-party/zombienet/single_parachain.toml index fb592d5e8b..9f4a890b02 100644 --- a/third-party/zombienet/single_parachain.toml +++ b/third-party/zombienet/single_parachain.toml @@ -5,24 +5,32 @@ timeout = 1000 [relaychain] default_command = "./polkadot" -default_args = [ "-l=parachain=debug,xcm=trace" ] +default_args = ["-l=parachain=debug,xcm=trace"] chain = "rococo-local" - [[relaychain.nodes]] - name = "alice" - validator = true +[relaychain.genesis.runtimeGenesis.patch.configuration.config.async_backing_params] +max_candidate_depth = 3 +allowed_ancestry_len = 2 - [[relaychain.nodes]] - name = "bob" - validator = true +[relaychain.genesis.runtimeGenesis.patch.configuration.config.scheduler_params] +lookahead = 2 - [[relaychain.nodes]] - name = "charlie" - validator = true +[[relaychain.nodes]] +name = "alice" +validator = true +ws_port = 9933 - [[relaychain.nodes]] - name = "dave" - validator = true +[[relaychain.nodes]] +name = "bob" +validator = true + +[[relaychain.nodes]] +name = "charlie" +validator = true + +[[relaychain.nodes]] +name = "dave" +validator = true [[parachains]] # Right now this has to be 2000 but soon we might be able to use arbitrary para-id @@ -31,13 +39,19 @@ id = 2000 chain = "shibuya-dev" cumulus_based = true - [[parachains.collators]] - name = "collator1" - command = "./astar-collator" - args = [ "-l=xcm=trace", "--enable-evm-rpc" ] - - - [[parachains.collators]] - name = "collator2" - command = "./astar-collator" - args = [ "--enable-evm-rpc" ] +[[parachains.collators]] +name = "collator1" +command = "./astar-collator" +args = ["-l=xcm=trace,parachain=debug", "--enable-evm-rpc"] +ws_port = 9944 + +[[parachains.collators]] +name = "collator2" +command = "./astar-collator" +args = ["--enable-evm-rpc"] +ws_port = 9945 + +[[parachains.collators]] +name = "collator3" +command = "./astar-collator" +ws_port = 9946