Skip to content

Commit 1a514f6

Browse files
Add digest processor xcm emulator (#7915)
Currently parachains are injecting through the xcm-emulator the default digests in each block, something that can prevent testing certain consensus aspects. We propose to add the type `DigestProvider`, which needs to implement the trait `Convert<blockNumber, Digest>`. The idea is that we can call the implementation of this trait before initializing every block, and thus, allowing us to inject custom digests provided by this trait. Obviously the default behavior persists if you set this type to `().` The utilization of the `Convert` trait was arbitrary, as it was the easiest to accomplish the solution. I am not against using a custom trait defined for this purpose if that is preferred. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 467d1f6 commit 1a514f6

File tree

3 files changed

+14
-5
lines changed
  • cumulus
    • parachains/integration-tests/emulated/chains/parachains/assets
    • xcm/xcm-emulator/src

3 files changed

+14
-5
lines changed

cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-rococo/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ decl_test_parachains! {
4242
LocationToAccountId: asset_hub_rococo_runtime::xcm_config::LocationToAccountId,
4343
ParachainInfo: asset_hub_rococo_runtime::ParachainInfo,
4444
MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin,
45+
DigestProvider: (),
4546
},
4647
pallets = {
4748
PolkadotXcm: asset_hub_rococo_runtime::PolkadotXcm,

cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-westend/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ decl_test_parachains! {
4242
LocationToAccountId: asset_hub_westend_runtime::xcm_config::LocationToAccountId,
4343
ParachainInfo: asset_hub_westend_runtime::ParachainInfo,
4444
MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin,
45+
DigestProvider: (),
4546
},
4647
pallets = {
4748
PolkadotXcm: asset_hub_westend_runtime::PolkadotXcm,

cumulus/xcm/xcm-emulator/src/lib.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub use cumulus_primitives_core::AggregateMessageOrigin as CumulusAggregateMessa
3737
pub use frame_support::{
3838
assert_ok,
3939
sp_runtime::{
40-
traits::{Dispatchable, Header as HeaderT},
41-
DispatchResult,
40+
traits::{Convert, Dispatchable, Header as HeaderT},
41+
Digest, DispatchResult,
4242
},
4343
traits::{
4444
EnqueueMessage, ExecuteOverweightError, Get, Hooks, OnInitialize, OriginTrait,
@@ -108,7 +108,6 @@ thread_local! {
108108
/// Most recent `HeadData` of each parachain, encoded.
109109
pub static LAST_HEAD: RefCell<HashMap<String, HashMap<u32, HeadData>>> = RefCell::new(HashMap::new());
110110
}
111-
112111
pub trait CheckAssertion<Origin, Destination, Hops, Args>
113112
where
114113
Origin: Chain + Clone,
@@ -264,6 +263,7 @@ pub trait Parachain: Chain {
264263
type ParachainInfo: Get<ParaId>;
265264
type ParachainSystem;
266265
type MessageProcessor: ProcessMessage + ServiceQueues;
266+
type DigestProvider: sp_runtime::traits::Convert<BlockNumberFor<Self::Runtime>, Digest>;
267267

268268
fn init();
269269

@@ -599,6 +599,7 @@ macro_rules! decl_test_parachains {
599599
LocationToAccountId: $location_to_account:path,
600600
ParachainInfo: $parachain_info:path,
601601
MessageOrigin: $message_origin:path,
602+
$( DigestProvider: $digest_provider:ty, )?
602603
},
603604
pallets = {
604605
$($pallet_name:ident: $pallet_path:path,)*
@@ -639,6 +640,7 @@ macro_rules! decl_test_parachains {
639640
type ParachainSystem = $crate::ParachainSystemPallet<<Self as $crate::Chain>::Runtime>;
640641
type ParachainInfo = $parachain_info;
641642
type MessageProcessor = $crate::DefaultParaMessageProcessor<$name<N>, $message_origin>;
643+
$crate::decl_test_parachains!(@inner_digest_provider $($digest_provider)?);
642644

643645
// We run an empty block during initialisation to open HRMP channels
644646
// and have them ready for the next block
@@ -657,7 +659,7 @@ macro_rules! decl_test_parachains {
657659
}
658660

659661
fn new_block() {
660-
use $crate::{Chain, HeadData, Network, Hooks, Encode, Parachain, TestExt};
662+
use $crate::{Chain, Convert, HeadData, Network, Hooks, Encode, Parachain, TestExt};
661663

662664
let para_id = Self::para_id().into();
663665

@@ -677,7 +679,10 @@ macro_rules! decl_test_parachains {
677679
.expect("network not initialized?")
678680
.clone()
679681
);
680-
<Self as Chain>::System::initialize(&block_number, &parent_head_data.hash(), &Default::default());
682+
683+
let digest = <Self as Parachain>::DigestProvider::convert(block_number);
684+
685+
<Self as Chain>::System::initialize(&block_number, &parent_head_data.hash(), &digest);
681686
<<Self as Parachain>::ParachainSystem as Hooks<$crate::BlockNumberFor<Self::Runtime>>>::on_initialize(block_number);
682687

683688
let _ = <Self as Parachain>::ParachainSystem::set_validation_data(
@@ -734,6 +739,8 @@ macro_rules! decl_test_parachains {
734739
$crate::__impl_check_assertion!($name, N);
735740
)+
736741
};
742+
( @inner_digest_provider $digest_provider:ty ) => { type DigestProvider = $digest_provider; };
743+
( @inner_digest_provider /* none */ ) => { type DigestProvider = (); };
737744
}
738745

739746
#[macro_export]

0 commit comments

Comments
 (0)