[xcm-emulator] Redo Parachain init (#1356)

* bring back proper init

* refactor block cycle

* ".git/.scripts/commands/fmt/fmt.sh"

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

Co-authored-by: Squirrel <gilescope@gmail.com>

---------

Co-authored-by: command-bot <>
Co-authored-by: Giles Cope <gilescope@gmail.com>
This commit is contained in:
Ignacio Palacios
2023-09-04 12:30:25 +02:00
committed by GitHub
parent a30092ab42
commit 2c35bc3d27
7 changed files with 142 additions and 138 deletions
@@ -174,8 +174,6 @@ fn send_xcm_from_para_to_system_para_paying_fee_with_assets_works() {
PenpalKusamaA::assert_xcm_pallet_sent(); PenpalKusamaA::assert_xcm_pallet_sent();
}); });
PenpalKusamaA::execute_with(|| {});
AssetHubKusama::execute_with(|| { AssetHubKusama::execute_with(|| {
type RuntimeEvent = <AssetHubKusama as Chain>::RuntimeEvent; type RuntimeEvent = <AssetHubKusama as Chain>::RuntimeEvent;
@@ -177,8 +177,6 @@ fn send_xcm_from_para_to_system_para_paying_fee_with_assets_works() {
PenpalPolkadotA::assert_xcm_pallet_sent(); PenpalPolkadotA::assert_xcm_pallet_sent();
}); });
PenpalPolkadotA::execute_with(|| {});
AssetHubPolkadot::execute_with(|| { AssetHubPolkadot::execute_with(|| {
type RuntimeEvent = <AssetHubPolkadot as Chain>::RuntimeEvent; type RuntimeEvent = <AssetHubPolkadot as Chain>::RuntimeEvent;
@@ -187,7 +187,6 @@ fn limited_teleport_native_assets_from_relay_to_system_para_works() {
/// Limited Teleport of native asset from System Parachain to Relay Chain /// Limited Teleport of native asset from System Parachain to Relay Chain
/// should work when there is enough balance in Relay Chain's `CheckAccount` /// should work when there is enough balance in Relay Chain's `CheckAccount`
#[test] #[test]
#[cfg(feature = "FIXME-IGNORED")] // <https://github.com/paritytech/cumulus/issues/3027>
fn limited_teleport_native_assets_back_from_system_para_to_relay_works() { fn limited_teleport_native_assets_back_from_system_para_to_relay_works() {
// Dependency - Relay Chain's `CheckAccount` should have enough balance // Dependency - Relay Chain's `CheckAccount` should have enough balance
limited_teleport_native_assets_from_relay_to_system_para_works(); limited_teleport_native_assets_from_relay_to_system_para_works();
@@ -226,7 +225,6 @@ fn limited_teleport_native_assets_back_from_system_para_to_relay_works() {
/// Limited Teleport of native asset from System Parachain to Relay Chain /// Limited Teleport of native asset from System Parachain to Relay Chain
/// should't work when there is not enough balance in Relay Chain's `CheckAccount` /// should't work when there is not enough balance in Relay Chain's `CheckAccount`
#[test] #[test]
#[cfg(feature = "FIXME-IGNORED")] // <https://github.com/paritytech/cumulus/issues/3027>
fn limited_teleport_native_assets_from_system_para_to_relay_fails() { fn limited_teleport_native_assets_from_system_para_to_relay_fails() {
// Init values for Relay Chain // Init values for Relay Chain
let amount_to_send: Balance = ASSET_HUB_POLKADOT_ED * 1000; let amount_to_send: Balance = ASSET_HUB_POLKADOT_ED * 1000;
@@ -116,8 +116,6 @@ fn send_xcm_from_para_to_system_para_paying_fee_with_assets_works() {
PenpalWestendA::assert_xcm_pallet_sent(); PenpalWestendA::assert_xcm_pallet_sent();
}); });
PenpalWestendA::execute_with(|| {});
AssetHubWestend::execute_with(|| { AssetHubWestend::execute_with(|| {
type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent; type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent;
@@ -14,85 +14,86 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>. // along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use crate::*;
#[test] #[test]
#[ignore]
fn example() { fn example() {
// // Init tests variables // Init tests variables
// // XcmPallet send arguments // XcmPallet send arguments
// let sudo_origin = <Rococo as Chain>::RuntimeOrigin::root(); let sudo_origin = <Rococo as Chain>::RuntimeOrigin::root();
// let destination = Rococo::child_location_of(BridgeHubRococo::para_id()).into(); let destination = Rococo::child_location_of(BridgeHubRococo::para_id()).into();
// let weight_limit = WeightLimit::Unlimited; let weight_limit = WeightLimit::Unlimited;
// let check_origin = None; let check_origin = None;
// let remote_xcm = Xcm(vec![ClearOrigin]); let remote_xcm = Xcm(vec![ClearOrigin]);
// let xcm = VersionedXcm::from(Xcm(vec![ let xcm = VersionedXcm::from(Xcm(vec![
// UnpaidExecution { weight_limit, check_origin }, UnpaidExecution { weight_limit, check_origin },
// ExportMessage { ExportMessage {
// network: WococoId, network: WococoId,
// destination: X1(Parachain(AssetHubWococo::para_id().into())), destination: X1(Parachain(AssetHubWococo::para_id().into())),
// xcm: remote_xcm, xcm: remote_xcm,
// }, },
// ])); ]));
// //Rococo Global Consensus //Rococo Global Consensus
// // Send XCM message from Relay Chain to Bridge Hub source Parachain // Send XCM message from Relay Chain to Bridge Hub source Parachain
// Rococo::execute_with(|| { Rococo::execute_with(|| {
// assert_ok!(<Rococo as RococoPallet>::XcmPallet::send( assert_ok!(<Rococo as RococoPallet>::XcmPallet::send(
// sudo_origin, sudo_origin,
// bx!(destination), bx!(destination),
// bx!(xcm), bx!(xcm),
// )); ));
// type RuntimeEvent = <Rococo as Chain>::RuntimeEvent; type RuntimeEvent = <Rococo as Chain>::RuntimeEvent;
// assert_expected_events!( assert_expected_events!(
// Rococo, Rococo,
// vec![ vec![
// RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }) => {}, RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }) => {},
// ] ]
// ); );
// }); });
// // Receive XCM message in Bridge Hub source Parachain // Receive XCM message in Bridge Hub source Parachain
// BridgeHubRococo::execute_with(|| { BridgeHubRococo::execute_with(|| {
// type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent; type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
// assert_expected_events!( assert_expected_events!(
// BridgeHubRococo, BridgeHubRococo,
// vec![ vec![
// RuntimeEvent::DmpQueue(cumulus_pallet_dmp_queue::Event::ExecutedDownward { RuntimeEvent::DmpQueue(cumulus_pallet_dmp_queue::Event::ExecutedDownward {
// outcome: Outcome::Complete(_), outcome: Outcome::Complete(_),
// .. ..
// }) => {}, }) => {},
// RuntimeEvent::BridgeWococoMessages(pallet_bridge_messages::Event::MessageAccepted { RuntimeEvent::BridgeWococoMessages(pallet_bridge_messages::Event::MessageAccepted {
// lane_id: LaneId([0, 0, 0, 1]), lane_id: LaneId([0, 0, 0, 1]),
// nonce: 1, nonce: 1,
// }) => {}, }) => {},
// ] ]
// ); );
// }); });
// // Wococo GLobal Consensus // Wococo GLobal Consensus
// // Receive XCM message in Bridge Hub target Parachain // Receive XCM message in Bridge Hub target Parachain
// BridgeHubWococo::execute_with(|| { BridgeHubWococo::execute_with(|| {
// type RuntimeEvent = <BridgeHubWococo as Chain>::RuntimeEvent; type RuntimeEvent = <BridgeHubWococo as Chain>::RuntimeEvent;
// assert_expected_events!( assert_expected_events!(
// BridgeHubWococo, BridgeHubWococo,
// vec![ vec![
// RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {}, RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
// ] ]
// ); );
// }); });
// // Receive embeded XCM message within `ExportMessage` in Parachain destination // Receive embeded XCM message within `ExportMessage` in Parachain destination
// AssetHubWococo::execute_with(|| { AssetHubWococo::execute_with(|| {
// type RuntimeEvent = <AssetHubWococo as Chain>::RuntimeEvent; type RuntimeEvent = <AssetHubWococo as Chain>::RuntimeEvent;
// assert_expected_events!( assert_expected_events!(
// AssetHubWococo, AssetHubWococo,
// vec![ vec![
// RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::Fail { .. }) => {}, RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::Fail { .. }) => {},
// ] ]
// ); );
// }); });
} }
@@ -58,11 +58,8 @@ fn pay_salary() {
); );
}); });
Collectives::execute_with(|| {});
AssetHubPolkadot::execute_with(|| { AssetHubPolkadot::execute_with(|| {
type RuntimeEvent = <AssetHubPolkadot as Chain>::RuntimeEvent; type RuntimeEvent = <AssetHubPolkadot as Chain>::RuntimeEvent;
assert_expected_events!( assert_expected_events!(
AssetHubPolkadot, AssetHubPolkadot,
vec![ vec![
+71 -57
View File
@@ -248,6 +248,14 @@ pub trait Parachain: Chain {
type ParachainInfo: Get<ParaId>; type ParachainInfo: Get<ParaId>;
type ParachainSystem; type ParachainSystem;
fn init();
fn new_block();
fn finalize_block();
fn set_last_head();
fn para_id() -> ParaId { fn para_id() -> ParaId {
Self::ext_wrapper(|| Self::ParachainInfo::get()) Self::ext_wrapper(|| Self::ParachainInfo::get())
} }
@@ -263,8 +271,6 @@ pub trait Parachain: Chain {
fn sovereign_account_id_of(location: MultiLocation) -> AccountId { fn sovereign_account_id_of(location: MultiLocation) -> AccountId {
Self::LocationToAccountId::convert_location(&location).unwrap() Self::LocationToAccountId::convert_location(&location).unwrap()
} }
fn init();
} }
pub trait Bridge { pub trait Bridge {
@@ -603,28 +609,74 @@ macro_rules! decl_test_parachains {
type ParachainSystem = $crate::ParachainSystemPallet<<Self as $crate::Chain>::Runtime>; type ParachainSystem = $crate::ParachainSystemPallet<<Self as $crate::Chain>::Runtime>;
type ParachainInfo = $parachain_info; type ParachainInfo = $parachain_info;
// We run an empty block during initialisation to open HRMP channels
// and have them ready for the next block
fn init() { fn init() {
use $crate::{Chain, HeadData, Network, NetworkComponent, Hooks, Encode, Parachain, TestExt}; use $crate::{Chain, HeadData, Network, NetworkComponent, Hooks, Encode, Parachain, TestExt};
// Set the last block head for later use in the next block
Self::set_last_head();
// Initialize a new block
Self::new_block();
// Finalize the new block
Self::finalize_block();
}
let para_id = Self::para_id(); fn new_block() {
use $crate::{Chain, HeadData, Network, NetworkComponent, Hooks, Encode, Parachain, TestExt};
let para_id = Self::para_id().into();
Self::ext_wrapper(|| {
// Increase Relay Chain block number
let mut relay_block_number = <$name as NetworkComponent>::Network::relay_block_number();
relay_block_number += 1;
<$name as NetworkComponent>::Network::set_relay_block_number(relay_block_number);
// Initialize a new Parachain block
let mut block_number = <Self as Chain>::System::block_number();
block_number += 1;
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()
);
<Self as Chain>::System::initialize(&block_number, &parent_head_data.hash(), &Default::default());
<<Self as Parachain>::ParachainSystem as Hooks<$crate::BlockNumber>>::on_initialize(block_number);
let _ = <Self as Parachain>::ParachainSystem::set_validation_data(
<Self as Chain>::RuntimeOrigin::none(),
<$name as NetworkComponent>::Network::hrmp_channel_parachain_inherent_data(para_id, relay_block_number, parent_head_data),
);
});
}
fn finalize_block() {
use $crate::{Chain, Encode, Hooks, Network, NetworkComponent, Parachain, TestExt};
Self::ext_wrapper(|| { Self::ext_wrapper(|| {
let block_number = <Self as Chain>::System::block_number(); let block_number = <Self as Chain>::System::block_number();
let mut relay_block_number = <Self as NetworkComponent>::Network::relay_block_number(); <Self as Parachain>::ParachainSystem::on_finalize(block_number);
});
// Get parent head data Self::set_last_head();
let header = <Self as Chain>::System::finalize(); }
let parent_head_data = HeadData(header.encode());
fn set_last_head() {
use $crate::{Chain, Encode, HeadData, Network, NetworkComponent, Parachain, TestExt};
let para_id = Self::para_id().into();
Self::ext_wrapper(|| {
// Store parent head data for use later.
let created_header = <Self as Chain>::System::finalize();
$crate::LAST_HEAD.with(|b| b.borrow_mut() $crate::LAST_HEAD.with(|b| b.borrow_mut()
.get_mut(<Self as NetworkComponent>::Network::name()) .get_mut(<Self as NetworkComponent>::Network::name())
.expect("network not initialized?") .expect("network not initialized?")
.insert(para_id.into(), parent_head_data.clone()) .insert(para_id, HeadData(created_header.encode()))
); );
let next_block_number = block_number + 1;
<Self as Chain>::System::initialize(&next_block_number, &header.hash(), &Default::default());
<<Self as Parachain>::ParachainSystem as Hooks<$crate::BlockNumber>>::on_initialize(next_block_number);
}); });
} }
} }
@@ -746,43 +798,20 @@ macro_rules! __impl_test_ext_for_parachain {
// Make sure the Network is initialized // Make sure the Network is initialized
<$name as NetworkComponent>::Network::init(); <$name as NetworkComponent>::Network::init();
let para_id = <$name>::para_id().into(); // Initialize a new block
Self::new_block();
// 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;
<$name as NetworkComponent>::Network::set_relay_block_number(relay_block_number);
let _ = <Self as Parachain>::ParachainSystem::set_validation_data(
<Self as Chain>::RuntimeOrigin::none(),
<$name as NetworkComponent>::Network::hrmp_channel_parachain_inherent_data(para_id, relay_block_number, parent_head_data),
);
})
});
// Execute // Execute
let r = $local_ext.with(|v| v.borrow_mut().execute_with(execute)); let r = $local_ext.with(|v| v.borrow_mut().execute_with(execute));
// provide inbound DMP/HRMP messages through a side-channel. // Finalize the block
// normally this would come through the `set_validation_data`, Self::finalize_block();
// but we go around that.
<$name as NetworkComponent>::Network::process_messages();
// Finalize block and send messages if needed let para_id = <$name>::para_id().into();
// Send messages if needed
$local_ext.with(|v| { $local_ext.with(|v| {
v.borrow_mut().execute_with(|| { v.borrow_mut().execute_with(|| {
let block_number = <Self as Chain>::System::block_number();
let mock_header = $crate::HeaderT::new( let mock_header = $crate::HeaderT::new(
0, 0,
Default::default(), Default::default(),
@@ -791,16 +820,6 @@ macro_rules! __impl_test_ext_for_parachain {
Default::default(), Default::default(),
); );
// Finalize to get xcmp messages.
<Self as Parachain>::ParachainSystem::on_finalize(block_number);
// Store parent head data for use later.
let created_header = <Self as Chain>::System::finalize();
$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()))
);
let collation_info = <Self as Parachain>::ParachainSystem::collect_collation_info(&mock_header); let collation_info = <Self as Parachain>::ParachainSystem::collect_collation_info(&mock_header);
// send upward messages // send upward messages
@@ -834,11 +853,6 @@ macro_rules! __impl_test_ext_for_parachain {
// clean events // clean events
<Self as $crate::Chain>::System::reset_events(); <Self as $crate::Chain>::System::reset_events();
// reinitialize before next call.
let next_block_number = block_number + 1;
<Self as $crate::Chain>::System::initialize(&next_block_number, &created_header.hash(), &Default::default());
<<Self as $crate::Parachain>::ParachainSystem as Hooks<$crate::BlockNumber>>::on_initialize(next_block_number);
}) })
}); });