Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xcm-emulator] Add on_initialize and on_finalize hooks #2007

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8b774b4
update declarative macros
0xmovses Oct 24, 2023
4da588c
use expr matcher
0xmovses Oct 24, 2023
4eb21a6
address feedback
0xmovses Oct 25, 2023
0931828
undo fmt
0xmovses Oct 25, 2023
2bb193a
Update cumulus/xcm/xcm-emulator/src/lib.rs
0xmovses Oct 25, 2023
bcf6c79
add OnHooks trait
0xmovses Oct 25, 2023
0eac6a2
remove old api
0xmovses Oct 25, 2023
a940d22
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Oct 25, 2023
4d5caf6
pass in hooks arg
0xmovses Oct 25, 2023
7414bfb
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Oct 30, 2023
a0770f5
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Oct 31, 2023
3109ad8
impl onHooks for ()
0xmovses Nov 2, 2023
50ad200
clippy
0xmovses Nov 2, 2023
eb73324
fix import
0xmovses Nov 2, 2023
4d0f49f
assosiate Hooks for Chain
0xmovses Nov 2, 2023
e5ef26b
Update cumulus/xcm/xcm-emulator/src/lib.rs
0xmovses Nov 2, 2023
9bbd7c2
Update cumulus/xcm/xcm-emulator/src/lib.rs
0xmovses Nov 2, 2023
321a555
Update cumulus/xcm/xcm-emulator/src/lib.rs
0xmovses Nov 2, 2023
f7b5dd4
Update cumulus/xcm/xcm-emulator/src/lib.rs
0xmovses Nov 2, 2023
b269284
fix macros
0xmovses Nov 2, 2023
e352d78
call hooks initialize and finalize
0xmovses Nov 2, 2023
514884a
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Nov 2, 2023
8c03551
clippy
0xmovses Nov 2, 2023
8e618de
Revert "clippy"
0xmovses Nov 2, 2023
ac6ffcd
update method name, avoid namespace collision
0xmovses Nov 2, 2023
d88ed38
fix failing test, add back ParachainSystem Hook
0xmovses Nov 3, 2023
74cd64f
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Nov 3, 2023
e0d8172
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Nov 3, 2023
c4dc31f
write as OnHooks
0xmovses Nov 6, 2023
13d4c40
Merge branch 'master' into 0xmovses-emulator-add-hooks
0xmovses Nov 7, 2023
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
35 changes: 23 additions & 12 deletions cumulus/parachains/integration-tests/emulated/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ pub use pallet_message_queue;

// Cumulus
pub use cumulus_pallet_xcmp_queue;
pub use xcm_emulator::Chain;
use xcm_emulator::{
decl_test_bridges, decl_test_networks, decl_test_parachains, decl_test_relay_chains,
decl_test_sender_receiver_accounts_parameter_types, DefaultParaMessageProcessor,
DefaultRelayMessageProcessor, Parachain, TestExt,
};
pub use xcm_emulator::{Chain, OnHooks};

// Polkadot
pub use pallet_xcm;
Expand All @@ -60,7 +60,8 @@ decl_test_relay_chains! {
Balances: westend_runtime::Balances,
Treasury: westend_runtime::Treasury,
AssetRate: westend_runtime::AssetRate,
}
},
hooks = (),
},
#[api_version(8)]
pub struct Rococo {
Expand All @@ -76,7 +77,8 @@ decl_test_relay_chains! {
Sudo: rococo_runtime::Sudo,
Balances: rococo_runtime::Balances,
Hrmp: rococo_runtime::Hrmp,
}
},
hooks = (),
},
#[api_version(8)]
pub struct Wococo {
Expand All @@ -91,7 +93,8 @@ decl_test_relay_chains! {
XcmPallet: rococo_runtime::XcmPallet,
Sudo: rococo_runtime::Sudo,
Balances: rococo_runtime::Balances,
}
},
hooks = (),
}
}

Expand All @@ -116,7 +119,8 @@ decl_test_parachains! {
ForeignAssets: asset_hub_westend_runtime::ForeignAssets,
PoolAssets: asset_hub_westend_runtime::PoolAssets,
AssetConversion: asset_hub_westend_runtime::AssetConversion,
}
},
hooks = (),
},
pub struct PenpalWestendA {
genesis = penpal::genesis(penpal::PARA_ID_A),
Expand All @@ -134,7 +138,8 @@ decl_test_parachains! {
PolkadotXcm: penpal_runtime::PolkadotXcm,
Assets: penpal_runtime::Assets,
Balances: penpal_runtime::Balances,
}
},
hooks = (),
},
// Rococo Parachains
pub struct BridgeHubRococo {
Expand All @@ -152,7 +157,8 @@ decl_test_parachains! {
pallets = {
PolkadotXcm: bridge_hub_rococo_runtime::PolkadotXcm,
Balances: bridge_hub_rococo_runtime::Balances,
}
},
hooks = (),
},
// AssetHubRococo
pub struct AssetHubRococo {
Expand All @@ -174,7 +180,8 @@ decl_test_parachains! {
PoolAssets: asset_hub_rococo_runtime::PoolAssets,
AssetConversion: asset_hub_rococo_runtime::AssetConversion,
Balances: asset_hub_rococo_runtime::Balances,
}
},
hooks = (),
},
pub struct PenpalRococoA {
genesis = penpal::genesis(penpal::PARA_ID_A),
Expand All @@ -191,7 +198,8 @@ decl_test_parachains! {
pallets = {
PolkadotXcm: penpal_runtime::PolkadotXcm,
Assets: penpal_runtime::Assets,
}
},
hooks = (),
},
pub struct PenpalRococoB {
genesis = penpal::genesis(penpal::PARA_ID_B),
Expand All @@ -208,7 +216,8 @@ decl_test_parachains! {
pallets = {
PolkadotXcm: penpal_runtime::PolkadotXcm,
Assets: penpal_runtime::Assets,
}
},
hooks = (),
},
// Wococo Parachains
pub struct BridgeHubWococo {
Expand All @@ -226,7 +235,8 @@ decl_test_parachains! {
},
pallets = {
PolkadotXcm: bridge_hub_rococo_runtime::PolkadotXcm,
}
},
hooks = (),
},
pub struct AssetHubWococo {
genesis = asset_hub_wococo::genesis(),
Expand All @@ -248,7 +258,8 @@ decl_test_parachains! {
PoolAssets: asset_hub_rococo_runtime::PoolAssets,
AssetConversion: asset_hub_rococo_runtime::AssetConversion,
Balances: asset_hub_rococo_runtime::Balances,
}
},
hooks = (),
}
}

Expand Down
25 changes: 21 additions & 4 deletions cumulus/xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ pub use xcm::v3::prelude::{
XcmHash, X1,
};
pub use xcm_executor::traits::ConvertLocation;

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

thread_local! {
Expand Down Expand Up @@ -214,12 +213,25 @@ pub trait NetworkComponent {
}
}

pub trait OnHooks {
fn on_initialize_hook(n: BlockNumber) -> Weight;
fn on_finalize_hook(n: BlockNumber);
0xmovses marked this conversation as resolved.
Show resolved Hide resolved
}

impl OnHooks for () {
fn on_initialize_hook(_n: BlockNumber) -> Weight {
Weight::default()
}
fn on_finalize_hook(_n: BlockNumber) {}
}

pub trait Chain: TestExt + NetworkComponent {
type Runtime: SystemConfig;
type RuntimeCall;
type RuntimeOrigin;
type RuntimeEvent;
type System;
type Hooks: OnHooks;

fn account_id_of(seed: &str) -> AccountId {
helpers::get_account_id_from_seed::<sr25519::Public>(seed)
Expand Down Expand Up @@ -353,7 +365,8 @@ macro_rules! decl_test_relay_chains {
},
pallets = {
$($pallet_name:ident: $pallet_path:path,)*
}
},
hooks = $on_hooks:ty,
}
),
+
Expand All @@ -369,6 +382,7 @@ macro_rules! decl_test_relay_chains {
type RuntimeOrigin = $runtime::RuntimeOrigin;
type RuntimeEvent = $runtime::RuntimeEvent;
type System = $crate::SystemPallet::<Self::Runtime>;
type Hooks = $on_hooks;

fn account_data_of(account: $crate::AccountIdOf<Self::Runtime>) -> $crate::AccountData<$crate::Balance> {
<Self as $crate::TestExt>::ext_wrapper(|| $crate::SystemPallet::<Self::Runtime>::account(account).data.into())
Expand Down Expand Up @@ -578,7 +592,8 @@ macro_rules! decl_test_parachains {
},
pallets = {
$($pallet_name:ident: $pallet_path:path,)*
}
},
hooks = $on_hooks:ty,
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we want to have a similar to pallets above way to set the hooks?
In most cases we do not need a custom with OnHooks trait, but we just want to list for which pallets it should run on_initialize and on_finalize.
What you think? @0xmovses @NachoPal

Copy link
Contributor

@NachoPal NachoPal Nov 9, 2023

Choose a reason for hiding this comment

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

I am not sure that something like:

  hooks = {
	  $($pallet_name:ident: $pallet_path:path,)*
  }

could be possible to be passed as argument of __impl_test_ext_for_parachain/relay()

Having the OnHooks trait also gives more flexibility to the dev in case he wants to execute some custom code during on_initialize/on_finalize()

}
),
+
Expand All @@ -594,6 +609,7 @@ macro_rules! decl_test_parachains {
type RuntimeOrigin = $runtime::RuntimeOrigin;
type RuntimeEvent = $runtime::RuntimeEvent;
type System = $crate::SystemPallet::<Self::Runtime>;
type Hooks = $on_hooks;

fn account_data_of(account: $crate::AccountIdOf<Self::Runtime>) -> $crate::AccountData<$crate::Balance> {
<Self as $crate::TestExt>::ext_wrapper(|| $crate::SystemPallet::<Self::Runtime>::account(account).data.into())
Expand Down Expand Up @@ -649,6 +665,7 @@ macro_rules! decl_test_parachains {
);
<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);
<$name as Chain>::Hooks::on_initialize_hook(block_number);

let _ = <Self as Parachain>::ParachainSystem::set_validation_data(
<Self as Chain>::RuntimeOrigin::none(),
Expand All @@ -663,12 +680,12 @@ macro_rules! decl_test_parachains {
Self::ext_wrapper(|| {
let block_number = <Self as Chain>::System::block_number();
<Self as Parachain>::ParachainSystem::on_finalize(block_number);
<$name as Chain>::Hooks::on_finalize_hook(block_number);
});

Self::set_last_head();
}


fn set_last_head() {
use $crate::{Chain, Encode, HeadData, Network, NetworkComponent, Parachain, TestExt};

Expand Down
Loading