Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

advance parachain properly in XCM-emulator for tests #3023

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use frame_support::{
ProcessMessageError, ServiceQueues,
},
weights::{Weight, WeightMeter},
StorageHasher,
};
pub use frame_system::{AccountInfo, Config as SystemConfig, Pallet as SystemPallet};
pub use pallet_balances::AccountData;
Expand All @@ -53,8 +54,9 @@ pub use cumulus_pallet_dmp_queue;
pub use cumulus_pallet_parachain_system::{self, Pallet as ParachainSystemPallet};
pub use cumulus_pallet_xcmp_queue::{Config as XcmpQueueConfig, Pallet as XcmpQueuePallet};
pub use cumulus_primitives_core::{
self, relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId,
PersistedValidationData, XcmpMessageHandler,
self,
relay_chain::{BlockNumber as RelayBlockNumber, HeadData},
DmpMessageHandler, ParaId, PersistedValidationData, XcmpMessageHandler,
};
pub use cumulus_primitives_parachain_inherent::ParachainInherentData;
pub use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
Expand Down Expand Up @@ -97,6 +99,8 @@ thread_local! {
pub static PARA_IDS: RefCell<HashMap<String, Vec<u32>>> = RefCell::new(HashMap::new());
/// Flag indicating if global variables have been initialized for a certain Network
pub static INITIALIZED: RefCell<HashMap<String, bool>> = RefCell::new(HashMap::new());
/// Most recent `HeadData` of each parachain, encoded.
pub static LAST_HEAD: RefCell<HashMap<String, HashMap<u32, HeadData>>> = RefCell::new(HashMap::new());
}

pub trait CheckAssertion<Origin, Destination, Hops, Args>
Expand Down Expand Up @@ -276,7 +280,7 @@ pub trait Parachain: Chain {
Self::LocationToAccountId::convert_location(&location).unwrap()
}

fn prepare_for_xcmp();
fn init();
}

pub trait Bridge {
Expand Down Expand Up @@ -613,7 +617,7 @@ macro_rules! decl_test_parachains {
type ParachainSystem = $crate::ParachainSystemPallet<<Self as Chain>::Runtime>;
type ParachainInfo = $parachain_info;

fn prepare_for_xcmp() {
fn init() {
use $crate::{Network, NetworkComponent, Hooks};

let para_id = Self::para_id();
Expand All @@ -622,14 +626,16 @@ macro_rules! decl_test_parachains {
let block_number = <Self as Chain>::System::block_number();
let mut relay_block_number = <Self as NetworkComponent>::Network::relay_block_number();

let _ = <Self as Parachain>::ParachainSystem::set_validation_data(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling set_validation_data here is definitely illegal so had to be removed. Tests possibly depended on it, but it lead to an impossible sequence of calls.

Copy link
Contributor Author

@rphmeier rphmeier Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elaborating a bit more. init() is called within execute_block the first time it's run.

set_validation_data needs to be followed by a System::finalize call.
It might be more correct as

  1. initialize
  2. set_validation_data
  3. finalize
  4. initialize

but that is basically just running a full block, which shouldn't be necessary during the initialization step.

<Self as Chain>::RuntimeOrigin::none(),
<Self as NetworkComponent>::Network::hrmp_channel_parachain_inherent_data(
para_id.into(),
relay_block_number,
),
// Get parent head data
let header = <Self as Chain>::System::finalize();
let parent_head_data = $crate::HeadData(header.encode());

$crate::LAST_HEAD.with(|b| b.borrow_mut()
.get_mut(<Self as NetworkComponent>::Network::name())
.expect("network not initialized?")
.insert(para_id.into(), parent_head_data.clone())
);
// set `AnnouncedHrmpMessagesPerCandidate`

<Self as Parachain>::ParachainSystem::on_initialize(block_number);
});
}
Expand Down Expand Up @@ -750,6 +756,9 @@ macro_rules! __impl_test_ext_for_parachain {

fn execute_with<R>(execute: impl FnOnce() -> R) -> R {
use $crate::{Get, Hooks, NetworkComponent, Network, Bridge};
use sp_core::Encode;
use sp_runtime::traits::BlakeTwo256;
use polkadot_primitives::HashT;

// Make sure the Network is initialized
<$name as NetworkComponent>::Network::init();
Expand All @@ -759,6 +768,14 @@ macro_rules! __impl_test_ext_for_parachain {
// Initialize block
$local_ext.with(|v| {
v.borrow_mut().execute_with(|| {
let _parent_head_data = $crate::LAST_HEAD.with(|b| b.borrow_mut()
.get_mut(<Self as NetworkComponent>::Network::name())
.expect("network not initialized?")
.get(&para_id)
.expect("network not initialized?")
.clone()
);

// Increase block number
let mut relay_block_number = <$name as NetworkComponent>::Network::relay_block_number();
relay_block_number += 1;
Expand Down Expand Up @@ -816,16 +833,26 @@ macro_rules! __impl_test_ext_for_parachain {
<$name>::send_bridged_messages(msg);
}

// clean messages
<Self as Parachain>::ParachainSystem::on_initialize(block_number);

// log events
Self::events().iter().for_each(|event| {
$crate::log::debug!(target: concat!("events::", stringify!($name)), "{:?}", event);
});

// Store parent head data for use later.
let created_header = <Self as Chain>::System::finalize();
let parent_head_data = $crate::LAST_HEAD.with(|b| b.borrow_mut()
.get_mut(<Self as NetworkComponent>::Network::name())
.expect("network not initialized?")
.insert(para_id.into(), $crate::HeadData(created_header.encode()))
);

// clean events
<Self as Chain>::System::reset_events();

// reinitialize before next call.
let next_block_number = block_number + 1;
<Self as Chain>::System::initialize(&next_block_number, &created_header.hash(), &Default::default());
<Self as Parachain>::ParachainSystem::on_initialize(next_block_number);
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
})
});

Expand Down Expand Up @@ -878,13 +905,14 @@ macro_rules! decl_test_networks {
$crate::UPWARD_MESSAGES.with(|b| b.borrow_mut().remove(Self::name()));
$crate::HORIZONTAL_MESSAGES.with(|b| b.borrow_mut().remove(Self::name()));
$crate::BRIDGED_MESSAGES.with(|b| b.borrow_mut().remove(Self::name()));
$crate::LAST_HEAD.with(|b| b.borrow_mut().remove(Self::name()));

<$relay_chain>::reset_ext();
$( <$parachain>::reset_ext(); )*
}

fn init() {
// If Network has not been itialized yet, it gets initialized
// If Network has not been initialized yet, it gets initialized
if $crate::INITIALIZED.with(|b| b.borrow_mut().get(Self::name()).is_none()) {
$crate::INITIALIZED.with(|b| b.borrow_mut().insert(Self::name().to_string(), true));
$crate::DOWNWARD_MESSAGES.with(|b| b.borrow_mut().insert(Self::name().to_string(), $crate::VecDeque::new()));
Expand All @@ -893,8 +921,9 @@ macro_rules! decl_test_networks {
$crate::HORIZONTAL_MESSAGES.with(|b| b.borrow_mut().insert(Self::name().to_string(), $crate::VecDeque::new()));
$crate::BRIDGED_MESSAGES.with(|b| b.borrow_mut().insert(Self::name().to_string(), $crate::VecDeque::new()));
$crate::PARA_IDS.with(|b| b.borrow_mut().insert(Self::name().to_string(), Self::para_ids()));
$crate::LAST_HEAD.with(|b| b.borrow_mut().insert(Self::name().to_string(), $crate::HashMap::new()));

$( <$parachain>::prepare_for_xcmp(); )*
$( <$parachain>::init(); )*
}
}

Expand Down Expand Up @@ -1027,7 +1056,7 @@ macro_rules! decl_test_networks {
para_id: u32,
relay_parent_number: u32,
) -> $crate::ParachainInherentData {
use $crate::cumulus_primitives_core::{relay_chain::HrmpChannelId, AbridgedHrmpChannel};
use $crate::cumulus_primitives_core::{relay_chain::{HeadData, HrmpChannelId}, AbridgedHrmpChannel};

let mut sproof = $crate::RelayStateSproofBuilder::default();
sproof.para_id = para_id.into();
Expand Down
Loading