From 80965417c7ec2eb7bf925ac54bd34471f5dd4e5c Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Mon, 7 Apr 2025 12:53:24 +0200 Subject: [PATCH 1/3] BACKPORT-CONFLICT --- Cargo.lock | 1 + cumulus/xcm/xcm-emulator/Cargo.toml | 30 +++++++++++++ cumulus/xcm/xcm-emulator/src/lib.rs | 67 +++++++++++++++++++++-------- prdoc/pr_8083.prdoc | 13 ++++++ 4 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 prdoc/pr_8083.prdoc diff --git a/Cargo.lock b/Cargo.lock index ac936dbb80c08..c77c8f169cb14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27639,6 +27639,7 @@ dependencies = [ "log", "pallet-balances", "pallet-message-queue", + "pallet-timestamp", "parachains-common", "parity-scale-codec", "paste", diff --git a/cumulus/xcm/xcm-emulator/Cargo.toml b/cumulus/xcm/xcm-emulator/Cargo.toml index ba1ecb29f8834..c9817e89744a5 100644 --- a/cumulus/xcm/xcm-emulator/Cargo.toml +++ b/cumulus/xcm/xcm-emulator/Cargo.toml @@ -17,6 +17,7 @@ codec = { workspace = true, default-features = true } impl-trait-for-tuples = { workspace = true } log = { workspace = true } paste = { workspace = true, default-features = true } +<<<<<<< HEAD frame-support.workspace = true frame-support.default-features = true frame-system.workspace = true @@ -57,3 +58,32 @@ xcm.workspace = true xcm.default-features = true xcm-executor.workspace = true xcm-executor.default-features = true +======= + +# Substrate +frame-support = { workspace = true, default-features = true } +frame-system = { workspace = true, default-features = true } +pallet-balances = { workspace = true, default-features = true } +pallet-message-queue = { workspace = true, default-features = true } +pallet-timestamp = { workspace = true, default-features = true } +sp-arithmetic = { workspace = true, default-features = true } +sp-core = { workspace = true, default-features = true } +sp-crypto-hashing = { workspace = true, default-features = true } +sp-io = { workspace = true, default-features = true } +sp-runtime = { workspace = true, default-features = true } +sp-tracing = { workspace = true, default-features = true } + +# Cumulus +cumulus-pallet-parachain-system = { workspace = true, default-features = true } +cumulus-primitives-core = { workspace = true, default-features = true } +cumulus-primitives-parachain-inherent = { workspace = true, default-features = true } +cumulus-test-relay-sproof-builder = { workspace = true, default-features = true } +parachains-common = { workspace = true, default-features = true } + +# Polkadot +polkadot-parachain-primitives = { workspace = true, default-features = true } +polkadot-primitives = { workspace = true, default-features = true } +polkadot-runtime-parachains = { workspace = true, default-features = true } +xcm = { workspace = true, default-features = true } +xcm-executor = { workspace = true, default-features = true } +>>>>>>> 4962d65a (`xcm-emulator` improved callback triggering (`on_initialize`, `on_finalize`, `on_idle`, `OnSystemEvent`). (#8083)) diff --git a/cumulus/xcm/xcm-emulator/src/lib.rs b/cumulus/xcm/xcm-emulator/src/lib.rs index 28719c8654ece..2387a935c9919 100644 --- a/cumulus/xcm/xcm-emulator/src/lib.rs +++ b/cumulus/xcm/xcm-emulator/src/lib.rs @@ -37,20 +37,22 @@ pub use cumulus_primitives_core::AggregateMessageOrigin as CumulusAggregateMessa pub use frame_support::{ assert_ok, sp_runtime::{ - traits::{Convert, Dispatchable, Header as HeaderT}, + traits::{Convert, Dispatchable, Header as HeaderT, Zero}, Digest, DispatchResult, }, traits::{ - EnqueueMessage, ExecuteOverweightError, Get, Hooks, OnInitialize, OriginTrait, - ProcessMessage, ProcessMessageError, ServiceQueues, + EnqueueMessage, ExecuteOverweightError, Get, Hooks, OnFinalize, OnIdle, OnInitialize, + OriginTrait, ProcessMessage, ProcessMessageError, ServiceQueues, }, weights::{Weight, WeightMeter}, }; pub use frame_system::{ - pallet_prelude::BlockNumberFor, Config as SystemConfig, Pallet as SystemPallet, + limits::BlockWeights as BlockWeightsLimits, pallet_prelude::BlockNumberFor, + Config as SystemConfig, Pallet as SystemPallet, }; pub use pallet_balances::AccountData; pub use pallet_message_queue; +pub use pallet_timestamp::Call as TimestampCall; pub use sp_arithmetic::traits::Bounded; pub use sp_core::{ crypto::get_public_from_string_or_panic, parameter_types, sr25519, storage::Storage, Pair, @@ -61,7 +63,9 @@ pub use sp_runtime::BoundedSlice; pub use sp_tracing; // Cumulus -pub use cumulus_pallet_parachain_system::Pallet as ParachainSystemPallet; +pub use cumulus_pallet_parachain_system::{ + Call as ParachainSystemCall, Pallet as ParachainSystemPallet, +}; pub use cumulus_primitives_core::{ relay_chain::{BlockNumber as RelayBlockNumber, HeadData, HrmpChannelId}, AbridgedHrmpChannel, DmpMessageHandler, ParaId, PersistedValidationData, XcmpMessageHandler, @@ -263,7 +267,7 @@ pub trait Parachain: Chain { type ParachainInfo: Get; type ParachainSystem; type MessageProcessor: ProcessMessage + ServiceQueues; - type DigestProvider: sp_runtime::traits::Convert, Digest>; + type DigestProvider: Convert, Digest>; fn init(); @@ -645,7 +649,8 @@ macro_rules! decl_test_parachains { // We run an empty block during initialisation to open HRMP channels // and have them ready for the next block fn init() { - use $crate::{Chain, HeadData, Network, Hooks, Encode, Parachain, TestExt}; + use $crate::{Chain, TestExt}; + // Initialize the thread local variable $crate::paste::paste! { [].with(|v| *v.borrow_mut() = Self::build_new_ext($genesis)); @@ -659,7 +664,9 @@ macro_rules! decl_test_parachains { } fn new_block() { - use $crate::{Chain, Convert, HeadData, Network, Hooks, Encode, Parachain, TestExt}; + use $crate::{ + Dispatchable, Chain, Convert, TestExt, Zero, + }; let para_id = Self::para_id().into(); @@ -680,24 +687,50 @@ macro_rules! decl_test_parachains { .clone() ); + // Initialze `System`. let digest = ::DigestProvider::convert(block_number); - ::System::initialize(&block_number, &parent_head_data.hash(), &digest); - <::ParachainSystem as Hooks<$crate::BlockNumberFor>>::on_initialize(block_number); - let _ = ::ParachainSystem::set_validation_data( - ::RuntimeOrigin::none(), - N::hrmp_channel_parachain_inherent_data(para_id, relay_block_number, parent_head_data), + // Process `on_initialize` for all pallets except `System`. + let _ = $runtime::AllPalletsWithoutSystem::on_initialize(block_number); + + // Process parachain inherents: + + // 1. inherent: cumulus_pallet_parachain_system::Call::set_validation_data + let set_validation_data: ::RuntimeCall = $crate::ParachainSystemCall::set_validation_data { + data: N::hrmp_channel_parachain_inherent_data(para_id, relay_block_number, parent_head_data), + }.into(); + $crate::assert_ok!( + set_validation_data.dispatch(::RuntimeOrigin::none()) + ); + + // 2. inherent: pallet_timestamp::Call::set (we expect the parachain has `pallet_timestamp`) + let timestamp_set: ::RuntimeCall = $crate::TimestampCall::set { + // We need to satisfy `pallet_timestamp::on_finalize`. + now: Zero::zero(), + }.into(); + $crate::assert_ok!( + timestamp_set.dispatch(::RuntimeOrigin::none()) ); }); } fn finalize_block() { - use $crate::{Chain, Encode, Hooks, Network, Parachain, TestExt}; + use $crate::{BlockWeightsLimits, Chain, OnFinalize, OnIdle, SystemConfig, TestExt, Weight}; Self::ext_wrapper(|| { let block_number = ::System::block_number(); - ::ParachainSystem::on_finalize(block_number); + + // Process `on_idle` for all pallets. + let weight = ::System::block_weight(); + let max_weight: Weight = <<::Runtime as SystemConfig>::BlockWeights as frame_support::traits::Get>::get().max_block; + let remaining_weight = max_weight.saturating_sub(weight.total()); + if remaining_weight.all_gt(Weight::zero()) { + let _ = $runtime::AllPalletsWithSystem::on_idle(block_number, remaining_weight); + } + + // Process `on_finalize` for all pallets except `System`. + $runtime::AllPalletsWithoutSystem::on_finalize(block_number); }); Self::set_last_head(); @@ -705,7 +738,7 @@ macro_rules! decl_test_parachains { fn set_last_head() { - use $crate::{Chain, Encode, HeadData, Network, Parachain, TestExt}; + use $crate::{Chain, Encode, HeadData, TestExt}; let para_id = Self::para_id().into(); @@ -1157,7 +1190,7 @@ macro_rules! decl_test_networks { $crate::ParachainInherentData { validation_data: $crate::PersistedValidationData { - parent_head: Default::default(), + parent_head: parent_head_data.clone(), relay_parent_number, relay_parent_storage_root: relay_storage_root, max_pov_size: Default::default(), diff --git a/prdoc/pr_8083.prdoc b/prdoc/pr_8083.prdoc new file mode 100644 index 0000000000000..7cd34b8afd4a7 --- /dev/null +++ b/prdoc/pr_8083.prdoc @@ -0,0 +1,13 @@ +title: '`xcm-emulator` improved callback triggering (`on_initialize`, `on_finalize`, + `on_idle`, `OnSystemEvent`).' +doc: +- audience: Runtime Dev + description: |- + This PR fixes several issues related to `xcm-emulator`: + - Corrects parent head propagation to the `PersistedValidationData` and `OnSystemEvent` callback. + - Triggers `on_initialize` for all pallets, not just `ParachainSystem`. + - Triggers `on_finalize` for all pallets, not just `ParachainSystem`. + - Triggers `on_idle` for all parachain pallets. +crates: +- name: xcm-emulator + bump: minor From 2f602cc0640831e7b643dd92dd5a97234356aa05 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Wed, 9 Apr 2025 13:47:55 +0200 Subject: [PATCH 2/3] Update cumulus/xcm/xcm-emulator/Cargo.toml --- cumulus/xcm/xcm-emulator/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/xcm/xcm-emulator/Cargo.toml b/cumulus/xcm/xcm-emulator/Cargo.toml index efa74985c134d..52e551d6a4699 100644 --- a/cumulus/xcm/xcm-emulator/Cargo.toml +++ b/cumulus/xcm/xcm-emulator/Cargo.toml @@ -58,4 +58,4 @@ sp-tracing.workspace = true xcm-executor.default-features = true xcm-executor.workspace = true xcm.default-features = true -xcm.workspace = true \ No newline at end of file +xcm.workspace = true From aec1609830b9ba6f8f29fb049f3e53c2f82cee96 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Wed, 9 Apr 2025 13:49:08 +0200 Subject: [PATCH 3/3] prdoc --- prdoc/{pr_8083.prdoc => pr_8170.prdoc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename prdoc/{pr_8083.prdoc => pr_8170.prdoc} (100%) diff --git a/prdoc/pr_8083.prdoc b/prdoc/pr_8170.prdoc similarity index 100% rename from prdoc/pr_8083.prdoc rename to prdoc/pr_8170.prdoc