From 031aade0f0ec76d1dc63f266280af6f762b25d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Sement=C3=A9=20Fern=C3=A1ndez?= Date: Fri, 20 Oct 2023 10:51:05 +0200 Subject: [PATCH 1/3] * Phase 1 adjustments for async-backing --- Cargo.lock | 58 +++++++++++++++++++------------------ Cargo.toml | 1 + runtime/ajuna/Cargo.toml | 4 ++- runtime/ajuna/src/lib.rs | 59 ++++++++++++++++++++------------------ runtime/bajun/Cargo.toml | 4 ++- runtime/bajun/src/lib.rs | 57 +++++++++++++++++++----------------- runtime/solo/src/consts.rs | 6 ++-- 7 files changed, 101 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7760eaa3..7e1eb79b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -265,6 +265,7 @@ dependencies = [ "cumulus-pallet-session-benchmarking", "cumulus-pallet-xcm", "cumulus-pallet-xcmp-queue", + "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-timestamp", "cumulus-primitives-utility", @@ -903,7 +904,7 @@ dependencies = [ "log", "parking", "polling", - "rustix 0.37.25", + "rustix 0.37.26", "slab", "socket2 0.4.9", "waker-fn", @@ -942,7 +943,7 @@ dependencies = [ "cfg-if", "event-listener 3.0.0", "futures-lite", - "rustix 0.38.19", + "rustix 0.38.20", "windows-sys 0.48.0", ] @@ -969,7 +970,7 @@ dependencies = [ "cfg-if", "futures-core", "futures-io", - "rustix 0.38.19", + "rustix 0.38.20", "signal-hook-registry", "slab", "windows-sys 0.48.0", @@ -1060,6 +1061,7 @@ dependencies = [ "cumulus-pallet-session-benchmarking", "cumulus-pallet-xcm", "cumulus-pallet-xcmp-queue", + "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-timestamp", "cumulus-primitives-utility", @@ -3246,7 +3248,7 @@ checksum = "7d9ce6874da5d4415896cd45ffbc4d1cfc0c4f9c079427bd870742c30f2f65a9" dependencies = [ "curve25519-dalek 4.1.1", "ed25519", - "hashbrown 0.14.1", + "hashbrown 0.14.2", "hex", "rand_core 0.6.4", "sha2 0.10.8", @@ -3975,7 +3977,7 @@ version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eeb4ed9e12f43b7fa0baae3f9cdda28352770132ef2e09a23760c29cae8bd47" dependencies = [ - "rustix 0.38.19", + "rustix 0.38.20", "windows-sys 0.48.0", ] @@ -4314,9 +4316,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.1" +version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" dependencies = [ "ahash 0.8.3", "allocator-api2", @@ -4657,7 +4659,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ "equivalent", - "hashbrown 0.14.1", + "hashbrown 0.14.2", ] [[package]] @@ -4773,7 +4775,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ "hermit-abi 0.3.3", - "rustix 0.38.19", + "rustix 0.38.20", "windows-sys 0.48.0", ] @@ -5812,7 +5814,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2cffa4ad52c6f791f4f8b15f0c05f9824b2ced1160e88cc393d64fff9a8ac64" dependencies = [ - "rustix 0.38.19", + "rustix 0.38.20", ] [[package]] @@ -10129,9 +10131,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.37.25" +version = "0.37.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4eb579851244c2c03e7c24f501c3432bed80b8f720af1d6e5b0e0f01555a035" +checksum = "84f3f8f960ed3b5a59055428714943298bf3fa2d4a1d53135084e0544829d995" dependencies = [ "bitflags 1.3.2", "errno", @@ -10143,9 +10145,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.19" +version = "0.38.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" +checksum = "67ce50cb2e16c2903e30d1cbccfd8387a74b9d4c938b6a4c5ec6cc7556f7a8a0" dependencies = [ "bitflags 2.4.1", "errno", @@ -11384,9 +11386,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35c0a159d0c45c12b20c5a844feb1fe4bea86e28f17b92a5f0c42193634d3782" +checksum = "7f7d66a1128282b7ef025a8ead62a4a9fcf017382ec53b8ffbf4d7bf77bd3c60" dependencies = [ "bitvec", "cfg-if", @@ -11398,9 +11400,9 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "912e55f6d20e0e80d63733872b40e1227c0bce1e1ab81ba67d696339bfd7fd29" +checksum = "abf2c68b89cafb3b8d918dd07b42be0da66ff202cf1155c5739a4e0c1ea0dc19" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -11871,7 +11873,7 @@ dependencies = [ "fnv", "futures-lite", "futures-util", - "hashbrown 0.14.1", + "hashbrown 0.14.2", "hex", "hmac 0.12.1", "itertools 0.11.0", @@ -11920,7 +11922,7 @@ dependencies = [ "futures-channel", "futures-lite", "futures-util", - "hashbrown 0.14.1", + "hashbrown 0.14.2", "hex", "itertools 0.11.0", "log", @@ -13220,9 +13222,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "target-lexicon" -version = "0.12.11" +version = "0.12.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" +checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" [[package]] name = "tempfile" @@ -13233,7 +13235,7 @@ dependencies = [ "cfg-if", "fastrand 2.0.1", "redox_syscall 0.3.5", - "rustix 0.38.19", + "rustix 0.38.20", "windows-sys 0.48.0", ] @@ -13254,9 +13256,9 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "thiserror" -version = "1.0.49" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" dependencies = [ "thiserror-impl", ] @@ -13283,9 +13285,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "1.0.49" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", @@ -14809,7 +14811,7 @@ dependencies = [ "either", "home", "once_cell", - "rustix 0.38.19", + "rustix 0.38.20", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d1db2dc9..1a657008 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -135,6 +135,7 @@ cumulus-pallet-parachain-system = { git = "https://github.com/paritytech cumulus-pallet-session-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-pallet-xcm = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-pallet-xcmp-queue = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } +cumulus-primitives-aura = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-primitives-core = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-primitives-parachain-inherent = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-primitives-timestamp = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } diff --git a/runtime/ajuna/Cargo.toml b/runtime/ajuna/Cargo.toml index 06c77a9f..b5466017 100644 --- a/runtime/ajuna/Cargo.toml +++ b/runtime/ajuna/Cargo.toml @@ -71,10 +71,11 @@ staging-xcm-executor = { workspace = true } # Cumulus 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-session-benchmarking = { 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 } @@ -100,6 +101,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", diff --git a/runtime/ajuna/src/lib.rs b/runtime/ajuna/src/lib.rs index 4dae7f42..38a265a9 100644 --- a/runtime/ajuna/src/lib.rs +++ b/runtime/ajuna/src/lib.rs @@ -26,7 +26,6 @@ mod proxy_type; mod weights; pub mod xcm_config; -use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use smallvec::smallvec; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; @@ -202,6 +201,15 @@ const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64, ); +/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included +/// into the relay chain. +const UNINCLUDED_SEGMENT_CAPACITY: u32 = 3; +/// How many parachain blocks are processed by the relay chain per parent. Limits the +/// number of blocks authored per slot. +const BLOCK_PROCESSING_VELOCITY: u32 = 2; +/// Relay chain slot duration, in milliseconds. +const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000; + /// The version information used to identify this runtime when compiled natively. #[cfg(feature = "std")] pub fn native_version() -> NativeVersion { @@ -468,16 +476,24 @@ parameter_types! { pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4); } +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 RuntimeEvent = RuntimeEvent; type OnSystemEvent = (); type SelfParaId = parachain_info::Pallet; + type OutboundXcmpMessageSource = XcmpQueue; type DmpMessageHandler = DmpQueue; type ReservedDmpWeight = ReservedDmpWeight; - type OutboundXcmpMessageSource = XcmpQueue; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases; + type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; + type ConsensusHook = ConsensusHook; } impl parachain_info::Config for Runtime {} @@ -527,6 +543,8 @@ impl pallet_aura::Config for Runtime { type DisabledValidators = (); type MaxAuthorities = MaxAuthorities; type AllowMultipleBlocksPerSlot = ConstBool; + #[cfg(feature = "experimental")] + type SlotDuration = ConstU64; } parameter_types! { @@ -740,7 +758,7 @@ mod benches { 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 { @@ -748,6 +766,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_api::Core for Runtime { fn version() -> RuntimeVersion { VERSION @@ -930,31 +957,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/bajun/Cargo.toml b/runtime/bajun/Cargo.toml index 0e20a0f5..c6a86b70 100644 --- a/runtime/bajun/Cargo.toml +++ b/runtime/bajun/Cargo.toml @@ -73,10 +73,11 @@ staging-xcm-executor = { workspace = true } # Cumulus 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-session-benchmarking = { 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 } @@ -106,6 +107,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", diff --git a/runtime/bajun/src/lib.rs b/runtime/bajun/src/lib.rs index 1cd99580..cc502663 100644 --- a/runtime/bajun/src/lib.rs +++ b/runtime/bajun/src/lib.rs @@ -26,7 +26,6 @@ mod proxy_type; mod weights; pub mod xcm_config; -use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use smallvec::smallvec; use sp_api::impl_runtime_apis; @@ -210,6 +209,15 @@ const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64, ); +/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included +/// into the relay chain. +const UNINCLUDED_SEGMENT_CAPACITY: u32 = 3; +/// How many parachain blocks are processed by the relay chain per parent. Limits the +/// number of blocks authored per slot. +const BLOCK_PROCESSING_VELOCITY: u32 = 2; +/// Relay chain slot duration, in milliseconds. +const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000; + /// The version information used to identify this runtime when compiled natively. #[cfg(feature = "std")] pub fn native_version() -> NativeVersion { @@ -448,6 +456,13 @@ parameter_types! { pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4); } +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 RuntimeEvent = RuntimeEvent; type OnSystemEvent = (); @@ -457,7 +472,8 @@ impl cumulus_pallet_parachain_system::Config for Runtime { type OutboundXcmpMessageSource = XcmpQueue; type XcmpMessageHandler = XcmpQueue; type ReservedXcmpWeight = ReservedXcmpWeight; - type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases; + type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; + type ConsensusHook = ConsensusHook; } impl parachain_info::Config for Runtime {} @@ -507,6 +523,8 @@ impl pallet_aura::Config for Runtime { type DisabledValidators = (); type MaxAuthorities = MaxAuthorities; type AllowMultipleBlocksPerSlot = ConstBool; + #[cfg(feature = "experimental")] + type SlotDuration = ConstU64; } parameter_types! { @@ -838,7 +856,7 @@ impl, ItemId: From<[u8; 32]>> 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 { @@ -846,6 +864,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_api::Core for Runtime { fn version() -> RuntimeVersion { VERSION @@ -1032,31 +1059,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/solo/src/consts.rs b/runtime/solo/src/consts.rs index 47b6d068..fa0cdc5c 100644 --- a/runtime/solo/src/consts.rs +++ b/runtime/solo/src/consts.rs @@ -34,14 +34,14 @@ pub mod time { /// up by `pallet_aura` to implement `fn slot_duration()`. /// /// Change this to adjust the block time. - pub const BLOCK_TIME_MS: Moment = 6_000; + pub const MILLISECS_PER_BLOCK: Moment = 6_000; // NOTE: Currently it is not possible to change the slot duration after the chain has started. // Attempting to do so will brick block production. - pub const SLOT_DURATION: Moment = BLOCK_TIME_MS; + pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; // Time is measured by number of blocks. - pub const MINUTES: BlockNumber = 60_000 / (BLOCK_TIME_MS as BlockNumber); + pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); pub const HOURS: BlockNumber = 60 * MINUTES; pub const DAYS: BlockNumber = 24 * HOURS; } From d866fbee5469640e4a55273ce4ed27046fab9711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Sement=C3=A9=20Fern=C3=A1ndez?= Date: Fri, 20 Oct 2023 13:26:24 +0200 Subject: [PATCH 2/3] * Adapted node for async-backing --- Cargo.lock | 27 ++ Cargo.toml | 2 + node/cli/src/para/command.rs | 54 ++-- node/service/Cargo.toml | 11 +- node/service/src/para.rs | 549 ++++++++++++++++++++--------------- 5 files changed, 382 insertions(+), 261 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e1eb79b..16290c58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -332,14 +332,17 @@ dependencies = [ "ajuna-rpc", "ajuna-runtime", "ajuna-solo-runtime", + "async-trait", "bajun-runtime", "cumulus-client-cli", "cumulus-client-collator", "cumulus-client-consensus-aura", "cumulus-client-consensus-common", "cumulus-client-consensus-proposer", + "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", @@ -351,6 +354,7 @@ dependencies = [ "hex-literal", "jsonrpsee", "pallet-transaction-payment-rpc", + "parity-scale-codec", "polkadot-service", "sc-basic-authorship", "sc-chain-spec", @@ -2280,6 +2284,29 @@ dependencies = [ "thiserror", ] +[[package]] +name = "cumulus-client-consensus-relay-chain" +version = "0.1.0" +source = "git+https://github.com/paritytech/polkadot-sdk.git?branch=release-polkadot-v1.1.0#f60318f68687e601c47de5ad5ca88e2c3f8139a7" +dependencies = [ + "async-trait", + "cumulus-client-consensus-common", + "cumulus-primitives-core", + "cumulus-relay-chain-interface", + "futures", + "parking_lot 0.12.1", + "sc-consensus", + "sp-api", + "sp-block-builder", + "sp-blockchain", + "sp-consensus", + "sp-core", + "sp-inherents", + "sp-runtime", + "substrate-prometheus-endpoint", + "tracing", +] + [[package]] name = "cumulus-client-network" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1a657008..534d1c02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ [workspace.dependencies] # General +async-trait = { version = "0.1.73", default-features = false} clap = { version = "4.4.6", default-features = false } futures = { version = "0.3.28", default-features = false } hex-literal = { version = "0.4.1", default-features = false } @@ -126,6 +127,7 @@ cumulus-client-cli = { git = "https://github.com/paritytech cumulus-client-collator = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-client-consensus-aura = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-client-consensus-common = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } +cumulus-client-consensus-relay-chain = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-client-consensus-proposer = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-client-network = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } cumulus-client-service = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false } diff --git a/node/cli/src/para/command.rs b/node/cli/src/para/command.rs index 1bcbbb35..e4578515 100644 --- a/node/cli/src/para/command.rs +++ b/node/cli/src/para/command.rs @@ -144,10 +144,16 @@ macro_rules! construct_async_run { let runner = $cli.create_runner($cmd)?; runner.async_run(|$config| { #[cfg(feature = "bajun")] - let $components = service::new_partial::(&$config)?; + let $components = service::new_partial::( + &$config, + service::aura_build_import_queue:: + )?; #[cfg(feature = "ajuna")] - let $components = service::new_partial::(&$config)?; + let $components = service::new_partial::( + &$config, + service::aura_build_import_queue:: + )?; let task_manager = $components.task_manager; { $( $code )* }.map(|v| (v, task_manager)) @@ -160,10 +166,16 @@ macro_rules! construct_sync_run { let runner = $cli.create_runner($cmd)?; runner.sync_run(|$config| { #[cfg(feature = "bajun")] - let $components = service::new_partial::(&$config)?; + let $components = service::new_partial::( + &$config, + service::aura_build_import_queue:: + )?; #[cfg(feature = "ajuna")] - let $components = service::new_partial::(&$config)?; + let $components = service::new_partial::( + &$config, + service::aura_build_import_queue:: + )?; { $( $code )* } }) @@ -229,14 +241,18 @@ pub fn run() -> Result<()> { runner.sync_run(|config| { #[cfg(feature = "ajuna")] { - let partials = - service::new_partial::(&config)?; + let partials = service::new_partial::( + &config, + service::aura_build_import_queue::, + )?; return cmd.run::(&*config.chain_spec, &*partials.client) } #[cfg(feature = "bajun")] { - let partials = - service::new_partial::(&config)?; + let partials = service::new_partial::( + &config, + service::aura_build_import_queue::, + )?; cmd.run::(&*config.chain_spec, &*partials.client) } #[cfg(not(feature = "bajun"))] @@ -383,25 +399,19 @@ pub fn run() -> Result<()> { match &config.chain_spec { #[cfg(feature = "ajuna")] spec if spec.id().starts_with("ajuna") => - service::start_parachain_node::( - config, - polkadot_config, - collator_options, - id, - hwbench, - ) + service::start_lookahead_parachain_node::< + AjunaRuntimeApi, + AjunaRuntimeExecutor, + >(config, polkadot_config, collator_options, id, hwbench) .await .map(|r| r.0) .map_err(Into::into), #[cfg(feature = "bajun")] spec if spec.id().starts_with("bajun") => - service::start_parachain_node::( - config, - polkadot_config, - collator_options, - id, - hwbench, - ) + service::start_lookahead_parachain_node::< + BajunRuntimeApi, + BajunRuntimeExecutor, + >(config, polkadot_config, collator_options, id, hwbench) .await .map(|r| r.0) .map_err(Into::into), diff --git a/node/service/Cargo.toml b/node/service/Cargo.toml index d6407867..3d9acbf8 100644 --- a/node/service/Cargo.toml +++ b/node/service/Cargo.toml @@ -9,10 +9,14 @@ version.workspace = true [dependencies] # General +async-trait = { workspace = true } hex-literal = { workspace = true } futures = { workspace = true } jsonrpsee = { workspace = true, features = [ "server" ] } -serde = { workspace = true, features = [ "derive" ] } +serde = { workspace = true, features = [ "derive" ] }C + +# Parity codec +parity-scale-codec = { workspace = true } # Substrate frame-benchmarking = { workspace = true } @@ -61,9 +65,11 @@ cumulus-client-collator = { workspace = true, optional = true } cumulus-client-consensus-aura = { workspace = true, optional = true } cumulus-client-consensus-common = { workspace = true, optional = true } cumulus-client-consensus-proposer = { workspace = true, optional = true } +cumulus-client-consensus-relay-chain = { workspace = true, optional = true } cumulus-client-network = { workspace = true, optional = true } cumulus-client-service = { workspace = true, optional = true } cumulus-primitives-core = { workspace = true, optional = true } +cumulus-primitives-aura = { workspace = true, optional = true } cumulus-primitives-parachain-inherent = { workspace = true, optional = true } cumulus-relay-chain-inprocess-interface = { workspace = true, optional = true } cumulus-relay-chain-interface = { workspace = true, optional = true } @@ -106,13 +112,14 @@ para = [ "cumulus-client-network", "cumulus-client-service", "cumulus-primitives-core", + "cumulus-primitives-aura", "cumulus-primitives-parachain-inherent", "cumulus-relay-chain-inprocess-interface", "cumulus-relay-chain-interface", "cumulus-relay-chain-minimal-node", "cumulus-relay-chain-rpc-interface", ] -solo = [ "ajuna-solo-runtime" ] +solo = [ "ajuna-solo-runtime", "sc-network" ] kusama-native = [ "polkadot-service/kusama-native" ] polkadot-native = [ "polkadot-service/polkadot-native" ] diff --git a/node/service/src/para.rs b/node/service/src/para.rs index ae8ecbd9..f89a9e81 100644 --- a/node/service/src/para.rs +++ b/node/service/src/para.rs @@ -16,32 +16,40 @@ #![allow(deprecated)] -use std::{sync::Arc, time::Duration}; +use std::{marker::PhantomData, sync::Arc, time::Duration}; -use ajuna_primitives::{AccountId, Balance, Block, Index as Nonce}; +use ajuna_primitives::{AccountId, Balance, Block, Hash, Index as Nonce}; use cumulus_client_cli::CollatorOptions; -use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion}; -use cumulus_client_consensus_common::{ - ParachainBlockImport as TParachainBlockImport, ParachainConsensus, -}; +use cumulus_client_collator::service::CollatorService; +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 cumulus_client_consensus_relay_chain::Verifier as RelayChainVerifier; use cumulus_client_service::{ - build_network, build_relay_chain_interface, prepare_node_config, start_collator, - start_full_node, BuildNetworkParams, CollatorSybilResistance, StartCollatorParams, - StartFullNodeParams, + build_network, build_relay_chain_interface, prepare_node_config, start_relay_chain_tasks, + BuildNetworkParams, CollatorSybilResistance, DARecoveryProfile, StartRelayChainTasksParams, +}; +use cumulus_primitives_core::{ + relay_chain::{CollatorPair, ValidationCode}, + ParaId, }; -use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::RelayChainInterface; +use cumulus_relay_chain_interface::{OverseerHandle, RelayChainInterface}; +use parity_scale_codec::Codec; use sc_client_api::Backend; -use sc_consensus::ImportQueue; +use sc_consensus::{ + import_queue::{BasicQueue, Verifier as VerifierT}, + BlockImportParams, ImportQueue, +}; use sc_executor::{ HeapAllocStrategy, NativeElseWasmExecutor, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY, }; -use sc_network::NetworkBlock; +use sc_network::{config::FullNetworkConfiguration, NetworkBlock}; use sc_network_sync::SyncingService; use sc_service::{Configuration, PartialComponents, TFullBackend, TFullClient, TaskManager}; use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle}; use sc_transaction_pool_api::OffchainTransactionPoolFactory; -use sp_api::ConstructRuntimeApi; +use sp_api::{ApiExt, ConstructRuntimeApi, HeaderT}; +use sp_consensus_aura::AuraApi; use sp_keystore::KeystorePtr; use sp_runtime::traits::BlakeTwo256; use substrate_prometheus_endpoint::Registry; @@ -91,8 +99,9 @@ type ParachainBlockImport = /// Use this macro if you don't actually need the full service, but just the builder in order to /// be able to perform chain operations. #[allow(clippy::type_complexity)] -pub fn new_partial( +pub fn new_partial( config: &Configuration, + build_import_queue: BIQ, ) -> Result< PartialComponents< ParachainClient, @@ -123,6 +132,13 @@ where + sp_consensus_aura::AuraApi, sc_client_api::StateBackendFor: sp_api::StateBackend, Executor: sc_executor::NativeExecutionDispatch + 'static, + BIQ: FnOnce( + Arc>, + ParachainBlockImport, + &Configuration, + Option, + &TaskManager, + ) -> Result, sc_service::Error>, { let telemetry = config .telemetry_endpoints @@ -195,16 +211,237 @@ where }) } -/// Start a node with the given parachain `Configuration` and relay chain `Configuration`. -/// -/// This is the actual implementation that is abstract over the executor and the runtime api. +enum BuildOnAccess { + Uninitialized(Option R + Send + Sync>>), + Initialized(R), +} + +impl BuildOnAccess { + fn get_mut(&mut self) -> &mut R { + loop { + match self { + Self::Uninitialized(f) => { + *self = Self::Initialized((f.take().unwrap())()); + }, + Self::Initialized(ref mut r) => return r, + } + } + } +} + +struct Verifier { + client: Arc, + aura_verifier: BuildOnAccess>>, + relay_chain_verifier: Box>, + _phantom: PhantomData, +} + +#[async_trait::async_trait] +impl VerifierT for Verifier +where + Client: sp_api::ProvideRuntimeApi + Send + Sync, + Client::Api: AuraApi, + AuraId: Send + Sync + Codec, +{ + async fn verify( + &mut self, + block_import: BlockImportParams, + ) -> Result, String> { + if self + .client + .runtime_api() + .has_api::>(*block_import.header.parent_hash()) + .unwrap_or(false) + { + self.aura_verifier.get_mut().verify(block_import).await + } else { + self.relay_chain_verifier.verify(block_import).await + } + } +} + +/// Build the import queue for Aura-based runtimes. +pub fn aura_build_import_queue( + client: Arc>, + block_import: ParachainBlockImport, + config: &Configuration, + telemetry_handle: Option, + task_manager: &TaskManager, +) -> Result, sc_service::Error> +where + RuntimeApi: + ConstructRuntimeApi> + Send + Sync + 'static, + RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + + sp_api::Metadata + + sp_session::SessionKeys + + sp_api::ApiExt + + sp_offchain::OffchainWorkerApi + + sp_block_builder::BlockBuilder + + sp_consensus_aura::AuraApi, + Executor: sc_executor::NativeExecutionDispatch + 'static, +{ + let client2 = client.clone(); + + let aura_verifier = move || { + let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client2).unwrap(); + + Box::new(cumulus_client_consensus_aura::build_verifier::< + sp_consensus_aura::sr25519::AuthorityPair, + _, + _, + _, + >(cumulus_client_consensus_aura::BuildVerifierParams { + client: client2.clone(), + create_inherent_data_providers: 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)) + }, + telemetry: telemetry_handle, + })) as Box<_> + }; + + let relay_chain_verifier = + Box::new(RelayChainVerifier::new(client.clone(), |_, _| async { Ok(()) })) as Box<_>; + + let verifier = Verifier { + client, + relay_chain_verifier, + aura_verifier: BuildOnAccess::Uninitialized(Some(Box::new(aura_verifier))), + _phantom: PhantomData, + }; + + let registry = config.prometheus_registry(); + let spawner = task_manager.spawn_essential_handle(); + + Ok(BasicQueue::new(verifier, Box::new(block_import), None, &spawner, registry)) +} + +/// Start an aura powered parachain node which uses the lookahead collator to support async backing. +/// This node is basic in the sense that its runtime api doesn't include common contents such as +/// transaction payment. Used for aura glutton. +pub async fn start_lookahead_parachain_node( + parachain_config: Configuration, + polkadot_config: Configuration, + collator_options: CollatorOptions, + para_id: ParaId, + hwbench: Option, +) -> sc_service::error::Result<(TaskManager, Arc>)> +where + RuntimeApi: + ConstructRuntimeApi> + Send + Sync + 'static, + RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue + + sp_api::Metadata + + sp_session::SessionKeys + + sp_api::ApiExt + + sp_offchain::OffchainWorkerApi + + sp_block_builder::BlockBuilder + + cumulus_primitives_core::CollectCollationInfo + + sp_consensus_aura::AuraApi + + substrate_frame_rpc_system::AccountNonceApi + + cumulus_primitives_aura::AuraUnincludedSegmentApi + + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, + Executor: sc_executor::NativeExecutionDispatch + 'static, +{ + start_parachain_lookahead_node_impl::( + parachain_config, + polkadot_config, + collator_options, + CollatorSybilResistance::Resistant, // Aura + para_id, + aura_build_import_queue::, + |client, + block_import, + prometheus_registry, + telemetry, + task_manager, + relay_chain_interface, + transaction_pool, + sync_oracle, + keystore, + relay_chain_slot_duration, + para_id, + collator_key, + overseer_handle, + announce_block, + backend| { + let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; + + let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording( + task_manager.spawn_handle(), + client.clone(), + transaction_pool, + prometheus_registry, + telemetry, + ); + let proposer = Proposer::new(proposer_factory); + + let collator_service = CollatorService::new( + client.clone(), + Arc::new(task_manager.spawn_handle()), + announce_block, + client.clone(), + ); + + let params = AuraParams { + create_inherent_data_providers: move |_, ()| async move { Ok(()) }, + block_import, + para_client: client.clone(), + para_backend: backend, + relay_client: relay_chain_interface, + code_hash_provider: move |block_hash| { + client.code_at(block_hash).ok().map(ValidationCode).map(|c| c.hash()) + }, + sync_oracle, + keystore, + collator_key, + para_id, + overseer_handle, + slot_duration, + relay_chain_slot_duration, + proposer, + collator_service, + authoring_duration: Duration::from_millis(1500), + }; + + let fut = aura::run::< + Block, + sp_consensus_aura::sr25519::AuthorityPair, + _, + _, + _, + _, + _, + _, + _, + _, + _, + >(params); + task_manager.spawn_essential_handle().spawn("aura", None, fut); + + Ok(()) + }, + hwbench, + ) + .await +} + +/// Start a parachain node, with lookahead implementation. #[sc_tracing::logging::prefix_logs_with("Parachain")] -#[allow(clippy::too_many_arguments)] -async fn start_node_impl( +async fn start_parachain_lookahead_node_impl( parachain_config: Configuration, polkadot_config: Configuration, collator_options: CollatorOptions, + sybil_resistance_level: CollatorSybilResistance, para_id: ParaId, + build_import_queue: BIQ, + start_consensus: SC, hwbench: Option, ) -> sc_service::error::Result<(TaskManager, Arc>)> where @@ -217,22 +454,44 @@ where + sp_offchain::OffchainWorkerApi + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + substrate_frame_rpc_system::AccountNonceApi + + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + sp_consensus_aura::AuraApi, - sc_client_api::StateBackendFor: sp_api::StateBackend, + BIQ: FnOnce( + Arc>, + ParachainBlockImport, + &Configuration, + Option, + &TaskManager, + ) -> Result, sc_service::Error>, + SC: FnOnce( + Arc>, + ParachainBlockImport, + Option<&Registry>, + Option, + &TaskManager, + Arc, + Arc>>, + Arc>, + KeystorePtr, + Duration, + ParaId, + CollatorPair, + OverseerHandle, + Arc>) + Send + Sync>, + Arc, + ) -> Result<(), sc_service::Error>, Executor: sc_executor::NativeExecutionDispatch + 'static, { let parachain_config = prepare_node_config(parachain_config); - let params = new_partial::(¶chain_config)?; + let params = new_partial::(¶chain_config, build_import_queue)?; let (block_import, mut telemetry, telemetry_worker_handle) = params.other; - let net_config = sc_network::config::FullNetworkConfiguration::new(¶chain_config.network); let client = params.client.clone(); let backend = params.backend.clone(); - let mut task_manager = params.task_manager; + let mut task_manager = params.task_manager; let (relay_chain_interface, collator_key) = build_relay_chain_interface( polkadot_config, ¶chain_config, @@ -244,11 +503,11 @@ where .await .map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?; - let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); let prometheus_registry = parachain_config.prometheus_registry().cloned(); let transaction_pool = params.transaction_pool.clone(); let import_queue_service = params.import_queue.service(); + let net_config = FullNetworkConfiguration::new(¶chain_config.network); let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) = build_network(BuildNetworkParams { @@ -260,7 +519,7 @@ where spawn_handle: task_manager.spawn_handle(), relay_chain_interface: relay_chain_interface.clone(), import_queue: params.import_queue, - sybil_resistance_level: CollatorSybilResistance::Resistant, + sybil_resistance_level, }) .await?; @@ -309,8 +568,8 @@ where task_manager: &mut task_manager, config: parachain_config, keystore: params.keystore_container.keystore(), - backend, - network: network.clone(), + backend: backend.clone(), + network, sync_service: sync_service.clone(), system_rpc_tx, tx_handler_controller, @@ -341,8 +600,25 @@ where .overseer_handle() .map_err(|e| sc_service::Error::Application(Box::new(e)))?; + start_relay_chain_tasks(StartRelayChainTasksParams { + client: client.clone(), + announce_block: announce_block.clone(), + para_id, + relay_chain_interface: relay_chain_interface.clone(), + task_manager: &mut task_manager, + da_recovery_profile: if validator { + DARecoveryProfile::Collator + } else { + DARecoveryProfile::FullNode + }, + import_queue: import_queue_service, + relay_chain_slot_duration, + recovery_handle: Box::new(overseer_handle.clone()), + sync_service: sync_service.clone(), + })?; + if validator { - let parachain_consensus = build_consensus( + start_consensus( client.clone(), block_import, prometheus_registry.as_ref(), @@ -350,219 +626,18 @@ where &task_manager, relay_chain_interface.clone(), transaction_pool, - sync_service.clone(), + sync_service, params.keystore_container.keystore(), - force_authoring, - para_id, - )?; - - let spawner = task_manager.spawn_handle(); - let params = StartCollatorParams { - para_id, - block_status: client.clone(), - announce_block, - client: client.clone(), - task_manager: &mut task_manager, - relay_chain_interface, - spawner, - parachain_consensus, - import_queue: import_queue_service, - collator_key: collator_key.expect("Command line arguments do not allow this. qed"), relay_chain_slot_duration, - recovery_handle: Box::new(overseer_handle), - sync_service, - }; - - start_collator(params).await?; - } else { - let params = StartFullNodeParams { - client: client.clone(), - announce_block, - task_manager: &mut task_manager, para_id, - relay_chain_interface, - relay_chain_slot_duration, - import_queue: import_queue_service, - recovery_handle: Box::new(overseer_handle), - sync_service, - }; - - start_full_node(params)?; + collator_key.expect("Command line arguments do not allow this. qed"), + overseer_handle, + announce_block, + backend.clone(), + )?; } start_network.start_network(); Ok((task_manager, client)) } - -/// Build the import queue for the parachain runtime. -pub fn build_import_queue( - client: Arc>, - block_import: ParachainBlockImport, - config: &Configuration, - telemetry: Option, - task_manager: &TaskManager, -) -> Result, sc_service::Error> -where - RuntimeApi: - ConstructRuntimeApi> + Send + Sync + 'static, - RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue - + sp_api::Metadata - + sp_session::SessionKeys - + sp_api::ApiExt - + sp_offchain::OffchainWorkerApi - + sp_block_builder::BlockBuilder - + cumulus_primitives_core::CollectCollationInfo - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + substrate_frame_rpc_system::AccountNonceApi - + sp_consensus_aura::AuraApi, - Executor: sc_executor::NativeExecutionDispatch + 'static, -{ - let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; - - cumulus_client_consensus_aura::import_queue::< - sp_consensus_aura::sr25519::AuthorityPair, - _, - _, - _, - _, - _, - >(cumulus_client_consensus_aura::ImportQueueParams { - block_import, - client, - create_inherent_data_providers: 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)) - }, - registry: config.prometheus_registry(), - spawner: &task_manager.spawn_essential_handle(), - telemetry, - }) - .map_err(Into::into) -} - -fn build_consensus( - client: Arc>, - block_import: ParachainBlockImport, - prometheus_registry: Option<&Registry>, - telemetry: Option, - task_manager: &TaskManager, - relay_chain_interface: Arc, - transaction_pool: Arc< - sc_transaction_pool::FullPool>, - >, - sync_oracle: Arc>, - keystore: KeystorePtr, - force_authoring: bool, - para_id: ParaId, -) -> Result>, sc_service::Error> -where - RuntimeApi: - ConstructRuntimeApi> + Send + Sync + 'static, - RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue - + sp_api::Metadata - + sp_session::SessionKeys - + sp_api::ApiExt - + sp_offchain::OffchainWorkerApi - + sp_block_builder::BlockBuilder - + cumulus_primitives_core::CollectCollationInfo - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + substrate_frame_rpc_system::AccountNonceApi - + sp_consensus_aura::AuraApi, - Executor: sc_executor::NativeExecutionDispatch + 'static, -{ - let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; - - let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording( - task_manager.spawn_handle(), - client.clone(), - transaction_pool, - prometheus_registry, - telemetry.clone(), - ); - - let params = BuildAuraConsensusParams { - proposer_factory, - create_inherent_data_providers: move |_, (relay_parent, validation_data)| { - let relay_chain_interface = relay_chain_interface.clone(); - async move { - let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( - relay_parent, - &relay_chain_interface, - &validation_data, - para_id, - ) - .await; - let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); - - let slot = - sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, - slot_duration, - ); - - let parachain_inherent = parachain_inherent.ok_or_else(|| { - Box::::from( - "Failed to create parachain inherent", - ) - })?; - Ok((slot, timestamp, parachain_inherent)) - } - }, - block_import, - para_client: client, - backoff_authoring_blocks: Option::<()>::None, - sync_oracle, - keystore, - force_authoring, - slot_duration, - // We got around 500ms for proposing - block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), - // And a maximum of 750ms if slots are skipped - max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), - telemetry, - }; - - Ok(AuraConsensus::build::(params)) -} - -/// Start a parachain node. -pub async fn start_parachain_node( - parachain_config: Configuration, - polkadot_config: Configuration, - collator_options: CollatorOptions, - para_id: ParaId, - hwbench: Option, -) -> sc_service::error::Result<(TaskManager, Arc>)> -where - RuntimeApi: - ConstructRuntimeApi> + Send + Sync + 'static, - RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue - + sp_api::Metadata - + sp_session::SessionKeys - + sp_api::ApiExt - + sp_offchain::OffchainWorkerApi - + sp_block_builder::BlockBuilder - + cumulus_primitives_core::CollectCollationInfo - + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + substrate_frame_rpc_system::AccountNonceApi - + sp_consensus_aura::AuraApi, - Executor: sc_executor::NativeExecutionDispatch + 'static, -{ - start_node_impl::( - parachain_config, - polkadot_config, - collator_options, - para_id, - hwbench, - ) - .await -} From 0c958da42f6e916f1218014569e2539338296f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Sement=C3=A9=20Fern=C3=A1ndez?= Date: Fri, 20 Oct 2023 15:52:46 +0200 Subject: [PATCH 3/3] * Finished code adaptations --- node/service/Cargo.toml | 8 +++++--- runtime/ajuna/Cargo.toml | 2 ++ runtime/ajuna/src/lib.rs | 18 +++++++----------- runtime/bajun/Cargo.toml | 2 ++ runtime/bajun/src/lib.rs | 14 +++++++------- runtime/solo/Cargo.toml | 2 ++ runtime/solo/src/lib.rs | 2 ++ 7 files changed, 27 insertions(+), 21 deletions(-) diff --git a/node/service/Cargo.toml b/node/service/Cargo.toml index 3d9acbf8..724ef689 100644 --- a/node/service/Cargo.toml +++ b/node/service/Cargo.toml @@ -13,7 +13,7 @@ async-trait = { workspace = true } hex-literal = { workspace = true } futures = { workspace = true } jsonrpsee = { workspace = true, features = [ "server" ] } -serde = { workspace = true, features = [ "derive" ] }C +serde = { workspace = true, features = [ "derive" ] } # Parity codec parity-scale-codec = { workspace = true } @@ -86,8 +86,8 @@ bajun-runtime = { workspace = true, optional = true } [features] default = [ "solo" ] -ajuna = [ "ajuna-runtime", "para" ] -bajun = [ "bajun-runtime", "para" ] +ajuna = [ "ajuna-runtime", "para", "ajuna-runtime/experimental" ] +bajun = [ "bajun-runtime", "para", "bajun-runtime/experimental" ] para = [ "pallet-transaction-payment-rpc", "sc-network", @@ -106,9 +106,11 @@ para = [ "polkadot-service/default", "staging-xcm", "cumulus-client-cli", + "cumulus-client-collator", "cumulus-client-consensus-aura", "cumulus-client-consensus-common", "cumulus-client-consensus-proposer", + "cumulus-client-consensus-relay-chain", "cumulus-client-network", "cumulus-client-service", "cumulus-primitives-core", diff --git a/runtime/ajuna/Cargo.toml b/runtime/ajuna/Cargo.toml index b5466017..a32467ef 100644 --- a/runtime/ajuna/Cargo.toml +++ b/runtime/ajuna/Cargo.toml @@ -206,3 +206,5 @@ try-runtime = [ "pallet-xcm/try-runtime", "parachain-info/try-runtime", ] + +experimental = [ "pallet-aura/experimental" ] diff --git a/runtime/ajuna/src/lib.rs b/runtime/ajuna/src/lib.rs index 38a265a9..e679bbd3 100644 --- a/runtime/ajuna/src/lib.rs +++ b/runtime/ajuna/src/lib.rs @@ -28,7 +28,7 @@ pub mod xcm_config; use smallvec::smallvec; use sp_api::impl_runtime_apis; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, ConstU64, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdLookup, BlakeTwo256, Block as BlockT}, @@ -166,7 +166,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { /// up by `pallet_aura` to implement `fn slot_duration()`. /// /// Change this to adjust the block time. -pub const MILLISECS_PER_BLOCK: u64 = 12000; +pub const MILLISECS_PER_BLOCK: u64 = 6000; // NOTE: Currently it is not possible to change the slot duration after the chain has started. // Attempting to do so will brick block production. @@ -195,9 +195,9 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); /// `Operational` extrinsics. const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); -/// We allow for 0.5 of a second of compute with a 12 second average block time. +/// We allow for 2 of a second of compute with a 6 second average block time. const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( - WEIGHT_REF_TIME_PER_SECOND.saturating_div(2), + WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64, ); @@ -336,15 +336,11 @@ impl pallet_sudo::Config for Runtime { type WeightInfo = (); // TODO: Add weight calc } -parameter_types! { - pub const MinimumPeriod: u64 = SLOT_DURATION / 2; -} - impl pallet_timestamp::Config for Runtime { /// A timestamp: milliseconds since the unix epoch. type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; + type OnTimestampSet = Aura; + type MinimumPeriod = ConstU64<0>; type WeightInfo = weights::pallet_timestamp::WeightInfo; } @@ -542,7 +538,7 @@ impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; type DisabledValidators = (); type MaxAuthorities = MaxAuthorities; - type AllowMultipleBlocksPerSlot = ConstBool; + type AllowMultipleBlocksPerSlot = ConstBool; #[cfg(feature = "experimental")] type SlotDuration = ConstU64; } diff --git a/runtime/bajun/Cargo.toml b/runtime/bajun/Cargo.toml index c6a86b70..e42367af 100644 --- a/runtime/bajun/Cargo.toml +++ b/runtime/bajun/Cargo.toml @@ -224,3 +224,5 @@ try-runtime = [ "pallet-ajuna-awesome-avatars/try-runtime", "pallet-ajuna-nft-transfer/try-runtime", ] + +experimental = [ "pallet-aura/experimental" ] diff --git a/runtime/bajun/src/lib.rs b/runtime/bajun/src/lib.rs index cc502663..59d9efcd 100644 --- a/runtime/bajun/src/lib.rs +++ b/runtime/bajun/src/lib.rs @@ -29,7 +29,7 @@ pub mod xcm_config; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use smallvec::smallvec; use sp_api::impl_runtime_apis; -use sp_core::{crypto::KeyTypeId, Get, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, ConstU64, Get, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdLookup, BlakeTwo256, Block as BlockT}, @@ -174,7 +174,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { /// up by `pallet_aura` to implement `fn slot_duration()`. /// /// Change this to adjust the block time. -pub const MILLISECS_PER_BLOCK: u64 = 12000; +pub const MILLISECS_PER_BLOCK: u64 = 6000; // NOTE: Currently it is not possible to change the slot duration after the chain has started. // Attempting to do so will brick block production. @@ -203,9 +203,9 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); /// `Operational` extrinsics. const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); -/// We allow for 0.5 of a second of compute with a 12 second average block time. +/// We allow for 2 of a second of compute with a 6 second average block time. const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( - WEIGHT_REF_TIME_PER_SECOND.saturating_div(2), + WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64, ); @@ -323,8 +323,8 @@ parameter_types! { impl pallet_timestamp::Config for Runtime { /// A timestamp: milliseconds since the unix epoch. type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; + type OnTimestampSet = Aura; + type MinimumPeriod = ConstU64<0>; type WeightInfo = weights::pallet_timestamp::WeightInfo; } @@ -522,7 +522,7 @@ impl pallet_aura::Config for Runtime { type AuthorityId = AuraId; type DisabledValidators = (); type MaxAuthorities = MaxAuthorities; - type AllowMultipleBlocksPerSlot = ConstBool; + type AllowMultipleBlocksPerSlot = ConstBool; #[cfg(feature = "experimental")] type SlotDuration = ConstU64; } diff --git a/runtime/solo/Cargo.toml b/runtime/solo/Cargo.toml index 373dc76a..08b84f4f 100644 --- a/runtime/solo/Cargo.toml +++ b/runtime/solo/Cargo.toml @@ -156,3 +156,5 @@ std = [ "pallet-ajuna-nft-staking/std", "pallet-ajuna-nft-transfer/std", ] + +experimental = [ "pallet-aura/experimental" ] diff --git a/runtime/solo/src/lib.rs b/runtime/solo/src/lib.rs index 7f05b2ee..0ccf07bc 100644 --- a/runtime/solo/src/lib.rs +++ b/runtime/solo/src/lib.rs @@ -186,6 +186,8 @@ impl pallet_aura::Config for Runtime { type MaxAuthorities = MaxAuthorities; type DisabledValidators = (); type AllowMultipleBlocksPerSlot = ConstBool; + #[cfg(feature = "experimental")] + type SlotDuration = frame_support::traits::ConstU64; } impl pallet_grandpa::Config for Runtime {