Skip to content

Commit

Permalink
Prepare runtimes and dev services for future async backing support (#376
Browse files Browse the repository at this point in the history
)

* add async backing runtime changes

* update moonkit pin

* include para_head and relay_slot in dev mode

* add ParaSlotProvider

* modify mocks

* point to xcm-emulator branch in polkadot-sdk fork

* adapt flashbox

* adapt template services

* fmt

* fmt again

* zepter

* clippy

* toml fmt

* update polkadot pin

* remove second insert of para_head in template services

* inject own para head as additional key

* typescript api changes

* change the slot in the container-chains to use the one from the para

* add back checkinherents

* document

* FMT

* point to proper branches

---------

Co-authored-by: girazoki <[email protected]>
  • Loading branch information
Agusrodri and girazoki authored Jan 3, 2024
1 parent 88e0fe7 commit 76c38b2
Show file tree
Hide file tree
Showing 27 changed files with 2,237 additions and 2,534 deletions.
4,282 changes: 1,836 additions & 2,446 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dc-orchestrator-chain-interface = { git = "https://github.com/moondance-labs/dan
# Moonkit (wasm)
nimbus-consensus = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.3.0" }
nimbus-primitives = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-async-backing = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-author-inherent = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-evm-precompile-balances-erc20 = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-evm-precompile-batch = { git = "https://github.com/moondance-labs/moonkit", branch = "tanssi-polkadot-v1.3.0", default-features = false }
Expand Down Expand Up @@ -182,7 +183,7 @@ polkadot-service = { git = "https://github.com/moondance-labs/polkadot-sdk", bra

# Cumulus (wasm)
cumulus-pallet-dmp-queue = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
cumulus-pallet-parachain-system = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
cumulus-pallet-parachain-system = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false, features = [ "parameterized-consensus-hook" ] }
cumulus-pallet-session-benchmarking = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
cumulus-pallet-xcm = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
cumulus-pallet-xcmp-queue = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions container-chains/templates/frontier/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ sp-consensus = { workspace = true }
sp-debug-derive = { workspace = true }

sp-consensus-aura = { workspace = true }
sp-consensus-slots = { workspace = true }
sp-core = { workspace = true, features = [ "std" ] }
sp-inherents = { workspace = true, features = [ "std" ] }
sp-io = { workspace = true, features = [ "std" ] }
Expand All @@ -78,6 +79,7 @@ try-runtime-cli = { workspace = true, optional = true }

# Polkadot
polkadot-cli = { workspace = true }
polkadot-parachain-primitives = { workspace = true }
polkadot-primitives = { workspace = true }
polkadot-service = { workspace = true }

Expand Down Expand Up @@ -116,6 +118,7 @@ runtime-benchmarks = [
"nimbus-primitives/runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks",
"polkadot-cli/runtime-benchmarks",
"polkadot-parachain-primitives/runtime-benchmarks",
"polkadot-primitives/runtime-benchmarks",
"polkadot-service/runtime-benchmarks",
"sc-service/runtime-benchmarks",
Expand Down
38 changes: 35 additions & 3 deletions container-chains/templates/frontier/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use {
cumulus_client_cli::CollatorOptions,
cumulus_client_consensus_common::ParachainBlockImport as TParachainBlockImport,
cumulus_client_service::prepare_node_config,
cumulus_primitives_core::ParaId,
cumulus_primitives_core::{relay_chain::well_known_keys as RelayWellKnownKeys, ParaId},
cumulus_primitives_parachain_inherent::{
MockValidationDataInherentDataProvider, MockXcmConfig,
},
Expand All @@ -32,11 +32,13 @@ use {
fc_rpc_core::types::{FeeHistoryCache, FilterPool},
nimbus_primitives::NimbusId,
node_common::service::{ManualSealConfiguration, NodeBuilder, NodeBuilderConfig, Sealing},
parity_scale_codec::Encode,
polkadot_parachain_primitives::primitives::HeadData,
sc_consensus::BasicQueue,
sc_executor::NativeElseWasmExecutor,
sc_service::{Configuration, TFullBackend, TFullClient, TaskManager},
sp_blockchain::HeaderBackend,
sp_consensus_aura::SlotDuration,
sp_consensus_slots::{Slot, SlotDuration},
sp_core::{Pair, H256},
std::{
collections::BTreeMap,
Expand Down Expand Up @@ -384,8 +386,35 @@ pub async fn start_dev_node(
.expect("Header lookup should succeed")
.expect("Header passed in as parent should be present in backend.");

let hash = client
.hash(current_para_block.saturating_sub(1))
.expect("Hash of the desired block must be present")
.expect("Hash of the desired block should exist");

let para_header = client
.expect_header(hash)
.expect("Expected parachain header should exist")
.encode();

let para_head_data: Vec<u8> = HeadData(para_header).encode();
let client_for_xcm = client.clone();
let authorities_for_cidp = authorities.clone();
let para_head_key = RelayWellKnownKeys::para_head(para_id);
let relay_slot_key = RelayWellKnownKeys::CURRENT_SLOT.to_vec();
let slot_duration = container_chain_template_frontier_runtime::SLOT_DURATION;

let mut timestamp: u64 = 0u64;
TIMESTAMP.with(|x| {
timestamp = x.clone().take();
});

timestamp += slot_duration;

let relay_slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
timestamp.into(),
SlotDuration::from_millis(slot_duration),
);
let relay_slot = u64::from(*relay_slot).saturating_mul(2);

let downward_xcm_receiver = downward_xcm_receiver.clone();
let hrmp_xcm_receiver = hrmp_xcm_receiver.clone();
Expand All @@ -401,6 +430,9 @@ pub async fn start_dev_node(
authorities: authorities_for_cidp
};

let mut additional_keys = mocked_authorities_noting.get_key_values();
additional_keys.append(&mut vec![(para_head_key, para_head_data), (relay_slot_key, Slot::from(relay_slot).encode())]);

let time = MockTimestampInherentDataProvider;
let mocked_parachain = MockValidationDataInherentDataProvider {
current_para_block,
Expand All @@ -417,7 +449,7 @@ pub async fn start_dev_node(
),
raw_downward_messages: downward_xcm_receiver.drain().collect(),
raw_horizontal_messages: hrmp_xcm_receiver.drain().collect(),
additional_key_values: Some(mocked_authorities_noting.get_key_values()),
additional_key_values: Some(additional_keys),
};

Ok((time, mocked_parachain, mocked_authorities_noting))
Expand Down
4 changes: 4 additions & 0 deletions container-chains/templates/frontier/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tp-consensus = { workspace = true }

# Moonkit
nimbus-primitives = { workspace = true }
pallet-async-backing = { workspace = true }
pallet-author-inherent = { workspace = true }
pallet-evm-precompile-balances-erc20 = { workspace = true }
pallet-evm-precompile-batch = { workspace = true }
Expand Down Expand Up @@ -52,6 +53,7 @@ pallet-utility = { workspace = true }
sp-api = { workspace = true }
sp-block-builder = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-consensus-slots = { workspace = true }
sp-core = { workspace = true }
sp-debug-derive = { workspace = true }
sp-inherents = { workspace = true }
Expand Down Expand Up @@ -126,6 +128,7 @@ std = [
"log/std",
"nimbus-primitives/std",
"num_enum/std",
"pallet-async-backing/std",
"pallet-author-inherent/std",
"pallet-balances/std",
"pallet-base-fee/std",
Expand Down Expand Up @@ -165,6 +168,7 @@ std = [
"sp-api/std",
"sp-block-builder/std",
"sp-consensus-aura/std",
"sp-consensus-slots/std",
"sp-core/std",
"sp-debug-derive/std",
"sp-inherents/std",
Expand Down
38 changes: 34 additions & 4 deletions container-chains/templates/frontier/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ use {
pallet_prelude::DispatchResult,
parameter_types,
traits::{
ConstU128, ConstU32, ConstU64, ConstU8, Contains, Currency as CurrencyT, FindAuthor,
Imbalance, InsideBoth, InstanceFilter, OffchainWorker, OnFinalize, OnIdle,
ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, Contains, Currency as CurrencyT,
FindAuthor, Imbalance, InsideBoth, InstanceFilter, OffchainWorker, OnFinalize, OnIdle,
OnInitialize, OnRuntimeUpgrade, OnUnbalanced,
},
weights::{
Expand All @@ -62,7 +62,7 @@ use {
limits::{BlockLength, BlockWeights},
EnsureRoot,
},
nimbus_primitives::NimbusId,
nimbus_primitives::{NimbusId, SlotBeacon},
pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction},
pallet_evm::{
Account as EVMAccount, EVMCurrencyAdapter, EnsureAddressNever, EnsureAddressRoot,
Expand All @@ -74,6 +74,7 @@ use {
scale_info::TypeInfo,
smallvec::smallvec,
sp_api::impl_runtime_apis,
sp_consensus_slots::{Slot, SlotDuration},
sp_core::{Get, MaxEncodedLen, OpaqueMetadata, H160, H256, U256},
sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
Expand Down Expand Up @@ -504,6 +505,16 @@ parameter_types! {
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
}

pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;
pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 2;
pub const BLOCK_PROCESSING_VELOCITY: u32 = 1;

type ConsensusHook = pallet_async_backing::consensus_hook::FixedVelocityConsensusHook<
Runtime,
BLOCK_PROCESSING_VELOCITY,
UNINCLUDED_SEGMENT_CAPACITY,
>;

impl cumulus_pallet_parachain_system::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnSystemEvent = ();
Expand All @@ -514,6 +525,21 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases;
type ConsensusHook = ConsensusHook;
}

pub struct ParaSlotProvider;
impl Get<(Slot, SlotDuration)> for ParaSlotProvider {
fn get() -> (Slot, SlotDuration) {
let slot = <Runtime as pallet_author_inherent::Config>::SlotBeacon::slot() as u64;
(Slot::from(slot), SlotDuration::from_millis(SLOT_DURATION))
}
}

impl pallet_async_backing::Config for Runtime {
type AllowMultipleBlocksPerSlot = ConstBool<false>;
type GetAndVerifySlot =
pallet_async_backing::ParaSlot<RELAY_CHAIN_SLOT_DURATION_MILLIS, ParaSlotProvider>;
}

impl parachain_info::Config for Runtime {}
Expand Down Expand Up @@ -932,6 +958,7 @@ construct_runtime!(
PolkadotXcm: pallet_xcm::{Pallet, Call, Storage, Event<T>, Origin, Config<T>} = 73,

RootTesting: pallet_root_testing = 100,
AsyncBacking: pallet_async_backing::{Pallet, Storage} = 110,
}
);

Expand Down Expand Up @@ -1282,6 +1309,9 @@ impl_runtime_apis! {

struct CheckInherents;

// TODO: this should be removed but currently if we remove it the relay does not check anything
// related to other inherents that are not parachain-system
#[allow(deprecated)]
impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {
fn check_inherents(
block: &Block,
Expand All @@ -1305,6 +1335,6 @@ impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {

cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = pallet_author_inherent::BlockExecutor::<Runtime, Executive>
CheckInherents = CheckInherents,
BlockExecutor = pallet_author_inherent::BlockExecutor::<Runtime, Executive>,
}
3 changes: 3 additions & 0 deletions container-chains/templates/simple/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sp-block-builder = { workspace = true }
sp-blockchain = { workspace = true }
sp-consensus = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-consensus-slots = { workspace = true }
sp-core = { workspace = true, features = [ "std" ] }
sp-inherents = { workspace = true, features = [ "std" ] }
sp-io = { workspace = true, features = [ "std" ] }
Expand All @@ -71,6 +72,7 @@ try-runtime-cli = { workspace = true, optional = true }

# Polkadot
polkadot-cli = { workspace = true }
polkadot-parachain-primitives = { workspace = true }
polkadot-primitives = { workspace = true }
polkadot-service = { workspace = true }

Expand All @@ -94,6 +96,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"nimbus-primitives/runtime-benchmarks",
"polkadot-cli/runtime-benchmarks",
"polkadot-parachain-primitives/runtime-benchmarks",
"polkadot-primitives/runtime-benchmarks",
"polkadot-service/runtime-benchmarks",
"sc-service/runtime-benchmarks",
Expand Down
38 changes: 35 additions & 3 deletions container-chains/templates/simple/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ use {
cumulus_client_cli::CollatorOptions,
cumulus_client_consensus_common::ParachainBlockImport as TParachainBlockImport,
cumulus_client_service::prepare_node_config,
cumulus_primitives_core::ParaId,
cumulus_primitives_core::{relay_chain::well_known_keys as RelayWellKnownKeys, ParaId},
cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider,
cumulus_primitives_parachain_inherent::MockXcmConfig,
nimbus_primitives::NimbusId,
node_common::service::ManualSealConfiguration,
node_common::service::Sealing,
node_common::service::{NodeBuilder, NodeBuilderConfig},
parity_scale_codec::Encode,
polkadot_parachain_primitives::primitives::HeadData,
sc_consensus::BasicQueue,
sc_executor::NativeElseWasmExecutor,
sc_service::{Configuration, TFullBackend, TFullClient, TaskManager},
sp_blockchain::HeaderBackend,
sp_consensus_aura::SlotDuration,
sp_consensus_slots::{Slot, SlotDuration},
sp_core::Pair,
sp_core::H256,
std::{sync::Arc, time::Duration},
Expand Down Expand Up @@ -243,8 +245,35 @@ pub async fn start_dev_node(
.expect("Header lookup should succeed")
.expect("Header passed in as parent should be present in backend.");

let hash = client
.hash(current_para_block.saturating_sub(1))
.expect("Hash of the desired block must be present")
.expect("Hash of the desired block should exist");

let para_header = client
.expect_header(hash)
.expect("Expected parachain header should exist")
.encode();

let para_head_data: Vec<u8> = HeadData(para_header).encode();
let client_for_xcm = client.clone();
let authorities_for_cidp = authorities.clone();
let para_head_key = RelayWellKnownKeys::para_head(para_id);
let relay_slot_key = RelayWellKnownKeys::CURRENT_SLOT.to_vec();
let slot_duration = container_chain_template_simple_runtime::SLOT_DURATION;

let mut timestamp: u64 = 0u64;
TIMESTAMP.with(|x| {
timestamp = x.clone().take();
});

timestamp += slot_duration;

let relay_slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
timestamp.into(),
SlotDuration::from_millis(slot_duration),
);
let relay_slot = u64::from(*relay_slot).saturating_mul(2);

let downward_xcm_receiver = downward_xcm_receiver.clone();
let hrmp_xcm_receiver = hrmp_xcm_receiver.clone();
Expand All @@ -260,6 +289,9 @@ pub async fn start_dev_node(
authorities: authorities_for_cidp
};

let mut additional_keys = mocked_authorities_noting.get_key_values();
additional_keys.append(&mut vec![(para_head_key, para_head_data), (relay_slot_key, Slot::from(relay_slot).encode())]);

let time = MockTimestampInherentDataProvider;
let mocked_parachain = MockValidationDataInherentDataProvider {
current_para_block,
Expand All @@ -276,7 +308,7 @@ pub async fn start_dev_node(
),
raw_downward_messages: downward_xcm_receiver.drain().collect(),
raw_horizontal_messages: hrmp_xcm_receiver.drain().collect(),
additional_key_values: Some(mocked_authorities_noting.get_key_values()),
additional_key_values: Some(additional_keys),
};

Ok((time, mocked_parachain, mocked_authorities_noting))
Expand Down
4 changes: 4 additions & 0 deletions container-chains/templates/simple/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tp-consensus = { workspace = true }

# Moonkit
nimbus-primitives = { workspace = true }
pallet-async-backing = { workspace = true }
pallet-author-inherent = { workspace = true }
pallet-maintenance-mode = { workspace = true, features = [ "xcm-support" ] }
pallet-migrations = { workspace = true }
Expand All @@ -46,6 +47,7 @@ pallet-utility = { workspace = true }
sp-api = { workspace = true }
sp-block-builder = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-consensus-slots = { workspace = true }
sp-core = { workspace = true }
sp-debug-derive = { workspace = true }
sp-inherents = { workspace = true }
Expand Down Expand Up @@ -104,6 +106,7 @@ std = [
"frame-try-runtime/std",
"log/std",
"nimbus-primitives/std",
"pallet-async-backing/std",
"pallet-author-inherent/std",
"pallet-balances/std",
"pallet-cc-authorities-noting/std",
Expand All @@ -129,6 +132,7 @@ std = [
"sp-api/std",
"sp-block-builder/std",
"sp-consensus-aura/std",
"sp-consensus-slots/std",
"sp-core/std",
"sp-debug-derive/std",
"sp-inherents/std",
Expand Down
Loading

0 comments on commit 76c38b2

Please sign in to comment.