paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.network/
1.63k stars 573 forks source link

[xcm-emulator] Add `on_initialize` and `on_finalize` hooks #1384

Open NachoPal opened 10 months ago

NachoPal commented 10 months ago

More info here: https://github.com/paritytech/cumulus/issues/2734

0xmovses commented 8 months ago

Picking this up

NachoPal commented 8 months ago

@0xmovses Ok, I had something in mind already.

The idea is to expand decl_test_relay_chains and decl_test_parachains adding on_initialize and on_finalize attributes. And probably to rename on_init to on_genesis as it could now be confused with on_initialize. Something like this:

decl_test_parachains! {
    pub struct AssetHub {
        genesis = genesis(),
        on_genesis = {
            asset_hub_kusama_runtime::AuraExt::on_initialize(1);
        },
        runtime = asset_hub_kusama_runtime,
        core = {
            XcmpMessageHandler: asset_hub_kusama_runtime::XcmpQueue,
            DmpMessageHandler: asset_hub_kusama_runtime::DmpQueue,
            LocationToAccountId: asset_hub_kusama_runtime::xcm_config::LocationToAccountId,
            ParachainInfo: asset_hub_kusama_runtime::ParachainInfo,
        },
        pallets = {
            PolkadotXcm: asset_hub_kusama_runtime::PolkadotXcm,
            Assets: asset_hub_kusama_runtime::Assets,
            ForeignAssets: asset_hub_kusama_runtime::ForeignAssets,
            PoolAssets: asset_hub_kusama_runtime::PoolAssets,
            AssetConversion: asset_hub_kusama_runtime::AssetConversion,
            Balances: asset_hub_kusama_runtime::Balances,
        },
        on_initialize = // Accepts a function that is expecting `BlockNumber` as argument
        on_finalize = // Accepts a function that is expecting `BlockNumber` as argument,
    },
}

Maybe just init instead of on_genesis. I am not fully convinced about the naming.

NachoPal commented 8 months ago

Actually on_initialize and on_finalize should be a function that is expecting a BlockNumber only an argument.

Inside the function, individual calls to each selected pallets on_initialize/on_finalize calls would be added.

For Parachains BlockNumber is coming from:

The block numbers can be queried in xcm-emulator. It would be something like:

ntn-x2 commented 1 month ago

Hey, I would like to pick up on this issue for a reason:

For Parachains BlockNumber is coming from: parachains_common::BlockNumber;

as done in https://github.com/paritytech/polkadot-sdk/blob/00440779d42b754292783612fc0f7e99d7cde2d2/cumulus/xcm/xcm-emulator/src/lib.rs#L660

This represents a problem for parachains that use a different type for block numbers, e.g. a u64. We cannot update to Polkadot SDK 1.1 because of this issue that leads to compilation errors for XCM Emulator. BlockNumber should be taken from the implementation of the frame_system pallet rather. Should I open a different issue for it?