From b9421f376f4db4b9fb18dcea295448776a04cf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Vilhelm=20=C3=81sgeirsson?= Date: Fri, 24 Nov 2023 09:06:11 +0000 Subject: [PATCH] [WIP] Add common coretime parachain to Rococo (#2419) Co-authored-by: eskimor --- Cargo.lock | 1 + polkadot/runtime/common/Cargo.toml | 4 + polkadot/runtime/common/src/coretime/mod.rs | 150 ++++++++++++++++++ polkadot/runtime/common/src/lib.rs | 1 + .../parachains/src/paras_inherent/tests.rs | 5 + polkadot/runtime/rococo/src/lib.rs | 11 +- polkadot/runtime/rococo/src/xcm_config.rs | 4 + .../smoke/0004-coretime-smoke-test.toml | 31 ++++ 8 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 polkadot/runtime/common/src/coretime/mod.rs create mode 100644 polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml diff --git a/Cargo.lock b/Cargo.lock index 9a8a78e25f2e..3939b7bcf2bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13113,6 +13113,7 @@ dependencies = [ "pallet-authorship", "pallet-babe", "pallet-balances", + "pallet-broker", "pallet-election-provider-multi-phase", "pallet-fast-unstake", "pallet-identity", diff --git a/polkadot/runtime/common/Cargo.toml b/polkadot/runtime/common/Cargo.toml index 4391b6d81eb2..b7c8ee5e9930 100644 --- a/polkadot/runtime/common/Cargo.toml +++ b/polkadot/runtime/common/Cargo.toml @@ -29,6 +29,7 @@ sp-npos-elections = { path = "../../../substrate/primitives/npos-elections", def pallet-authorship = { path = "../../../substrate/frame/authorship", default-features = false } pallet-balances = { path = "../../../substrate/frame/balances", default-features = false } +pallet-broker = { path = "../../../substrate/frame/broker", default-features = false } pallet-fast-unstake = { path = "../../../substrate/frame/fast-unstake", default-features = false } pallet-identity = { path = "../../../substrate/frame/identity", default-features = false } pallet-session = { path = "../../../substrate/frame/session", default-features = false } @@ -84,6 +85,7 @@ std = [ "pallet-asset-rate?/std", "pallet-authorship/std", "pallet-balances/std", + "pallet-broker/std", "pallet-election-provider-multi-phase/std", "pallet-fast-unstake/std", "pallet-identity/std", @@ -124,6 +126,7 @@ runtime-benchmarks = [ "pallet-asset-rate/runtime-benchmarks", "pallet-babe/runtime-benchmarks", "pallet-balances/runtime-benchmarks", + "pallet-broker/runtime-benchmarks", "pallet-election-provider-multi-phase/runtime-benchmarks", "pallet-fast-unstake/runtime-benchmarks", "pallet-identity/runtime-benchmarks", @@ -148,6 +151,7 @@ try-runtime = [ "pallet-authorship/try-runtime", "pallet-babe?/try-runtime", "pallet-balances/try-runtime", + "pallet-broker/try-runtime", "pallet-election-provider-multi-phase/try-runtime", "pallet-fast-unstake/try-runtime", "pallet-identity/try-runtime", diff --git a/polkadot/runtime/common/src/coretime/mod.rs b/polkadot/runtime/common/src/coretime/mod.rs new file mode 100644 index 000000000000..adfb16670757 --- /dev/null +++ b/polkadot/runtime/common/src/coretime/mod.rs @@ -0,0 +1,150 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Extrinsics implementing the relay chain side of the Coretime interface. +//! +//! + +// #[cfg(test)] +// mod tests; + +use frame_support::{pallet_prelude::*, traits::Currency}; +use frame_system::pallet_prelude::*; +use pallet_broker::CoreAssignment; +use primitives::CoreIndex; +use runtime_parachains::assigner_bulk::{self, PartsOf57600}; + +use sp_std::prelude::*; + +pub use pallet::*; + +pub trait WeightInfo { + fn request_core_count() -> Weight; + fn request_revenue_info_at() -> Weight; + fn credit_account() -> Weight; + fn assign_core() -> Weight; +} + +/// A weight info that is only suitable for testing. +pub struct TestWeightInfo; + +impl WeightInfo for TestWeightInfo { + fn request_core_count() -> Weight { + Weight::MAX + } + fn request_revenue_info_at() -> Weight { + Weight::MAX + } + fn credit_account() -> Weight { + Weight::MAX + } + fn assign_core() -> Weight { + Weight::MAX + } +} + +/// Shorthand for the Balance type the runtime is using. +type BalanceOf = + <::Currency as Currency<::AccountId>>::Balance; + +#[frame_support::pallet(dev_mode)] +pub mod pallet { + + use super::*; + + #[pallet::pallet] + #[pallet::without_storage_info] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config + assigner_bulk::Config { + /// The runtime's definition of a Currency. + type Currency: Currency; + /// Something that provides the weight of this pallet. + //type WeightInfo: WeightInfo; + /// The external origin allowed to enact coretime extrinsics. Usually the broker system + /// parachain. + type ExternalBrokerOrigin: EnsureOrigin; + } + + #[pallet::error] + pub enum Error {} + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::call] + impl Pallet { + // TODO Impl me! + //#[pallet::weight(::WeightInfo::request_core_count())] + #[pallet::call_index(1)] + pub fn request_core_count(origin: OriginFor, _count: u16) -> DispatchResult { + // Ignore requests not coming from the External Broker parachain. + let _multi_location = ::ExternalBrokerOrigin::ensure_origin(origin)?; + Ok(()) + } + + // TODO Impl me! + //#[pallet::weight(::WeightInfo::request_revenue_info_at())] + #[pallet::call_index(2)] + pub fn request_revenue_info_at( + origin: OriginFor, + _when: BlockNumberFor, + ) -> DispatchResult { + // Ignore requests not coming from the External Broker parachain. + let _multi_location = ::ExternalBrokerOrigin::ensure_origin(origin)?; + Ok(()) + } + + // TODO Impl me! + //#[pallet::weight(::WeightInfo::credit_account())] + #[pallet::call_index(3)] + pub fn credit_account( + origin: OriginFor, + _who: T::AccountId, + _amount: BalanceOf, + ) -> DispatchResult { + // Ignore requests not coming from the External Broker parachain. + let _multi_location = ::ExternalBrokerOrigin::ensure_origin(origin)?; + Ok(()) + } + + /// Receive instructions from the `ExternalBrokerOrigin`, detailing how a specific core is + /// to be used. + /// + /// Parameters: + /// -`origin`: The `ExternalBrokerOrigin`, assumed to be the Broker system parachain. + /// -`core`: The core that should be scheduled. + /// -`begin`: The starting blockheight of the instruction. + /// -`assignment`: How the blockspace should be utilised. + /// -`end_hint`: An optional hint as to when this particular set of instructions will end. + // TODO: Weights! + //#[pallet::weight(::WeightInfo::assign_core())] + #[pallet::call_index(4)] + pub fn assign_core( + origin: OriginFor, + core: CoreIndex, + begin: BlockNumberFor, + assignment: Vec<(CoreAssignment, PartsOf57600)>, + end_hint: Option>, + ) -> DispatchResult { + // Ignore requests not coming from the External Broker parachain. + let _multi_location = ::ExternalBrokerOrigin::ensure_origin(origin)?; + + >::assign_core(core, begin, assignment, end_hint) + } + } +} diff --git a/polkadot/runtime/common/src/lib.rs b/polkadot/runtime/common/src/lib.rs index bd49d3cccc9c..3d0fb7684bfe 100644 --- a/polkadot/runtime/common/src/lib.rs +++ b/polkadot/runtime/common/src/lib.rs @@ -21,6 +21,7 @@ pub mod assigned_slots; pub mod auctions; pub mod claims; +pub mod coretime; pub mod crowdloan; pub mod elections; pub mod identity_migrator; diff --git a/polkadot/runtime/parachains/src/paras_inherent/tests.rs b/polkadot/runtime/parachains/src/paras_inherent/tests.rs index 9396a0359afd..e55271941a62 100644 --- a/polkadot/runtime/parachains/src/paras_inherent/tests.rs +++ b/polkadot/runtime/parachains/src/paras_inherent/tests.rs @@ -688,6 +688,11 @@ mod enter { Pallet::::on_chain_votes().unwrap().session, 2 ); + + assert_ok!(Pallet::::enter( + frame_system::RawOrigin::None.into(), + limit_inherent_data, + )); }); } diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index ad49a62cafdf..3ad34382d7d9 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -21,6 +21,7 @@ #![recursion_limit = "512"] use pallet_nis::WithMaximumOf; +use pallet_xcm::EnsureXcm; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use primitives::{ slashing, vstaging::NodeFeatures, AccountId, AccountIndex, Balance, BlockNumber, @@ -31,7 +32,7 @@ use primitives::{ ValidatorIndex, PARACHAIN_KEY_TYPE_ID, }; use runtime_common::{ - assigned_slots, auctions, claims, crowdloan, identity_migrator, impl_runtime_weights, + assigned_slots, auctions, claims, coretime, crowdloan, identity_migrator, impl_runtime_weights, impls::{ LocatableAssetConverter, ToAuthor, VersionedLocatableAsset, VersionedMultiLocationConverter, }, @@ -125,6 +126,7 @@ use governance::{ pallet_custom_origins, AuctionAdmin, Fellows, GeneralAdmin, LeaseAdmin, Treasurer, TreasurySpender, }; +use xcm_config::OnlyBroker; #[cfg(test)] mod tests; @@ -992,6 +994,12 @@ impl parachains_assigner_on_demand::Config for Runtime { type WeightInfo = weights::runtime_parachains_assigner_on_demand::WeightInfo; } +impl coretime::Config for Runtime { + type Currency = Balances; + //type WeightInfo = weights::runtime_coretime::WeightInfo; + type ExternalBrokerOrigin = EnsureXcm; +} + impl parachains_assigner_bulk::Config for Runtime {} impl parachains_assigner_parachains::Config for Runtime {} @@ -1393,6 +1401,7 @@ construct_runtime! { Slots: slots::{Pallet, Call, Storage, Event} = 71, Auctions: auctions::{Pallet, Call, Storage, Event} = 72, Crowdloan: crowdloan::{Pallet, Call, Storage, Event} = 73, + Coretime: coretime::{Pallet, Call} = 74, // Pallet for sending XCM. XcmPallet: pallet_xcm::{Pallet, Call, Storage, Event, Origin, Config} = 99, diff --git a/polkadot/runtime/rococo/src/xcm_config.rs b/polkadot/runtime/rococo/src/xcm_config.rs index f1731c58bc61..834a68b6de89 100644 --- a/polkadot/runtime/rococo/src/xcm_config.rs +++ b/polkadot/runtime/rococo/src/xcm_config.rs @@ -148,6 +148,10 @@ match_types! { pub type OnlyParachains: impl Contains = { MultiLocation { parents: 0, interior: X1(Parachain(_)) } }; + + pub type OnlyBroker: impl Contains = { + MultiLocation { parents: 0, interior: X1(Parachain(BROKER_ID)) } + }; } /// The barriers one of which must be passed for an XCM message to be executed. diff --git a/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml b/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml new file mode 100644 index 000000000000..82b417533d9b --- /dev/null +++ b/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml @@ -0,0 +1,31 @@ +[settings] +timeout = 1000 + +[relaychain] +default_image = "{{ZOMBIENET_INTEGRATION_TEST_IMAGE}}" +chain = "rococo-local" +command = "polkadot" + + [[relaychain.nodes]] + name = "alice" + args = [ "--alice", "-lruntime=debug,parachain=trace" ] + + [[relaychain.nodes]] + name = "bob" + args = [ "--bob", "-lruntime=debug,parachain=trace" ] + +[[parachains]] +id = 1004 +add_to_genesis = false +register_para = true + + [parachains.collator] + name = "coretime-collator" + image = "{{COL_IMAGE}}" + command = "polkadot-parachain" + args = [ "-lruntime=debug,parachain=trace" ] + +[types.Header] +number = "u64" +parent_hash = "Hash" +post_state = "Hash"