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

Add broker pallet to coretime-westend #3272

Merged
merged 21 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b754d5d
Add broker pallet to coretime-westend
seadanda Feb 8, 2024
d5538e9
Fix typo in broker pallet
seadanda Feb 8, 2024
367044a
Also fix broker typo in coretime-westend...
seadanda Feb 8, 2024
f77c395
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Feb 8, 2024
14ca1fe
".git/.scripts/commands/bench-all/bench-all.sh" --runtime=coretime-we…
Feb 8, 2024
b2d1869
Add weight function that bench bot removed
seadanda Feb 9, 2024
02f306a
Add prdoc
seadanda Feb 9, 2024
a519c32
Remove extra bootnodes and session keys
seadanda Feb 9, 2024
0f0c4cf
Merge branch 'master' into donal-coretime-westend
seadanda Feb 9, 2024
115071f
Fix universal_origin weights properly for coretime
seadanda Feb 9, 2024
7afb516
Add coretime-westend to polkadot-parachain and fix chainspec script
seadanda Feb 12, 2024
f750148
Remove pallet-sudo from coretime-westend
seadanda Feb 12, 2024
3046d93
Generate chainspec as at f750148
seadanda Feb 12, 2024
8f83682
Change coretime log levels to debug
seadanda Feb 14, 2024
8a95ed6
Merge branch 'master' into donal-coretime-westend
seadanda Feb 14, 2024
70a0f4e
Rename CurrencyTransactor
seadanda Feb 14, 2024
dc70c4f
Use chainspecs via symlink like other parachains
seadanda Feb 14, 2024
a5a791b
Resolve AccountID TODO
seadanda Feb 15, 2024
deeef97
Refine hardcoded weights to reflect benchmarks
seadanda Feb 15, 2024
7cf5eac
Change coretime pallet index for westend runtime
seadanda Feb 16, 2024
eb00b8c
Update westend chainspec
seadanda Feb 16, 2024
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions cumulus/parachains/chain-specs/coretime-westend.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct CoretimeAllocator;
impl CoretimeInterface for CoretimeAllocator {
type AccountId = AccountId;
type Balance = Balance;
type RealyChainBlockNumberProvider = RelaychainDataProvider<Runtime>;
type RelayChainBlockNumberProvider = RelaychainDataProvider<Runtime>;

fn request_core_count(count: CoreIndex) {
use crate::coretime::CoretimeProviderCalls::RequestCoreCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<Call> XcmWeightInfo<Call> for CoretimeRococoXcmWeight<Call> {
XcmGeneric::<Runtime>::clear_transact_status()
}
fn universal_origin(_: &Junction) -> Weight {
XcmGeneric::<Runtime>::universal_origin()
Weight::MAX
}
fn export_message(_: &NetworkId, _: &Junctions, _: &Xcm<()>) -> Weight {
Weight::MAX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,6 @@ impl<T: frame_system::Config> WeightInfo<T> {
// Minimum execution time: 2_082_000 picoseconds.
Weight::from_parts(2_144_000, 0)
}
// Storage: `ParachainInfo::ParachainId` (r:1 w:0)
// Proof: `ParachainInfo::ParachainId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
pub fn universal_origin() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `1489`
// Minimum execution time: 4_275_000 picoseconds.
Weight::from_parts(4_381_000, 1489)
.saturating_add(T::DbWeight::get().reads(1))
}
pub fn set_fees_mode() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
Expand Down
10 changes: 6 additions & 4 deletions cumulus/parachains/runtimes/coretime/coretime-westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pallet-aura = { path = "../../../../../substrate/frame/aura", default-features =
pallet-authorship = { path = "../../../../../substrate/frame/authorship", default-features = false }
pallet-balances = { path = "../../../../../substrate/frame/balances", default-features = false }
pallet-message-queue = { path = "../../../../../substrate/frame/message-queue", default-features = false }
pallet-broker = { path = "../../../../../substrate/frame/broker", default-features = false }
pallet-multisig = { path = "../../../../../substrate/frame/multisig", default-features = false }
pallet-session = { path = "../../../../../substrate/frame/session", default-features = false }
pallet-sudo = { path = "../../../../../substrate/frame/sudo", default-features = false }
pallet-timestamp = { path = "../../../../../substrate/frame/timestamp", default-features = false }
pallet-transaction-payment = { path = "../../../../../substrate/frame/transaction-payment", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { path = "../../../../../substrate/frame/transaction-payment/rpc/runtime-api", default-features = false }
Expand Down Expand Up @@ -100,11 +100,11 @@ std = [
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-broker/std",
"pallet-collator-selection/std",
"pallet-message-queue/std",
"pallet-multisig/std",
"pallet-session/std",
"pallet-sudo/std",
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
Expand Down Expand Up @@ -150,10 +150,10 @@ runtime-benchmarks = [
"frame-system-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-broker/runtime-benchmarks",
"pallet-collator-selection/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-multisig/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-xcm-benchmarks/runtime-benchmarks",
Expand All @@ -178,11 +178,11 @@ try-runtime = [
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-broker/try-runtime",
"pallet-collator-selection/try-runtime",
"pallet-message-queue/try-runtime",
"pallet-multisig/try-runtime",
"pallet-session/try-runtime",
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-utility/try-runtime",
Expand All @@ -193,3 +193,5 @@ try-runtime = [
]

experimental = ["pallet-aura/experimental"]

fast-runtime = []
10 changes: 9 additions & 1 deletion cumulus/parachains/runtimes/coretime/coretime-westend/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ fn main() {
.with_current_project()
.export_heap_base()
.import_memory()
.build()
.build();

substrate_wasm_builder::WasmBuilder::new()
.with_current_project()
.set_file_name("fast_runtime_binary.rs")
.enable_feature("fast-runtime")
.import_memory()
.export_heap_base()
.build();
}

#[cfg(not(feature = "std"))]
Expand Down
238 changes: 238 additions & 0 deletions cumulus/parachains/runtimes/coretime/coretime-westend/src/coretime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus 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.

// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::*;
use codec::{Decode, Encode};
use cumulus_pallet_parachain_system::RelaychainDataProvider;
use cumulus_primitives_core::relay_chain;
use frame_support::{
parameter_types,
traits::{
fungible::{Balanced, Credit},
OnUnbalanced,
},
};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
use parachains_common::{AccountId, Balance, BlockNumber};
use xcm::latest::prelude::*;

// TODO: check AccountId import
seadanda marked this conversation as resolved.
Show resolved Hide resolved
pub struct CreditToCollatorPot;
impl OnUnbalanced<Credit<AccountId, Balances>> for CreditToCollatorPot {
fn on_nonzero_unbalanced(credit: Credit<polkadot_core_primitives::AccountId, Balances>) {
let staking_pot = CollatorSelection::account_id();
let _ = <Balances as Balanced<_>>::resolve(&staking_pot, credit);
}
}

/// A type containing the encoding of the coretime pallet in the Relay chain runtime. Used to
/// construct any remote calls. The codec index must correspond to the index of `Coretime` in the
/// `construct_runtime` of the Relay chain.
#[derive(Encode, Decode)]
enum RelayRuntimePallets {
#[codec(index = 74)]
seadanda marked this conversation as resolved.
Show resolved Hide resolved
Coretime(CoretimeProviderCalls),
}

/// Call encoding for the calls needed from the relay coretime pallet.
#[derive(Encode, Decode)]
enum CoretimeProviderCalls {
#[codec(index = 1)]
RequestCoreCount(CoreIndex),
#[codec(index = 2)]
RequestRevenueInfoAt(relay_chain::BlockNumber),
#[codec(index = 3)]
CreditAccount(AccountId, Balance),
#[codec(index = 4)]
AssignCore(
CoreIndex,
relay_chain::BlockNumber,
Vec<(CoreAssignment, PartsOf57600)>,
Option<relay_chain::BlockNumber>,
),
}

parameter_types! {
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
}

parameter_types! {
pub storage CoreCount: Option<CoreIndex> = None;
pub storage CoretimeRevenue: Option<(BlockNumber, Balance)> = None;
}

/// Type that implements the `CoretimeInterface` for the allocation of Coretime. Meant to operate
/// from the parachain context. That is, the parachain provides a market (broker) for the sale of
/// coretime, but assumes a `CoretimeProvider` (i.e. a Relay Chain) to actually provide cores.
pub struct CoretimeAllocator;
impl CoretimeInterface for CoretimeAllocator {
type AccountId = AccountId;
type Balance = Balance;
type RelayChainBlockNumberProvider = RelaychainDataProvider<Runtime>;

fn request_core_count(count: CoreIndex) {
use crate::coretime::CoretimeProviderCalls::RequestCoreCount;
let request_core_count_call = RelayRuntimePallets::Coretime(RequestCoreCount(count));

let message = Xcm(vec![
Instruction::UnpaidExecution {
weight_limit: WeightLimit::Unlimited,
check_origin: None,
},
Instruction::Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(1000000000, 200000),
call: request_core_count_call.encode().into(),
},
]);

match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) {
Ok(_) => log::debug!(
target: "runtime::coretime",
"Request to update schedulable cores sent successfully."
),
Err(e) => log::error!(
target: "runtime::coretime",
"Failed to send request to update schedulable cores: {:?}",
e
),
}
}

fn request_revenue_info_at(when: RCBlockNumberOf<Self>) {
use crate::coretime::CoretimeProviderCalls::RequestRevenueInfoAt;
let request_revenue_info_at_call =
RelayRuntimePallets::Coretime(RequestRevenueInfoAt(when));

let message = Xcm(vec![
Instruction::UnpaidExecution {
weight_limit: WeightLimit::Unlimited,
check_origin: None,
},
Instruction::Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(1000000000, 200000),
call: request_revenue_info_at_call.encode().into(),
},
]);

match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) {
Ok(_) => log::debug!(
target: "runtime::coretime",
"Request for revenue information sent successfully."
),
Err(e) => log::error!(
target: "runtime::coretime",
"Request for revenue information failed to send: {:?}",
e
),
}
}

fn credit_account(who: Self::AccountId, amount: Self::Balance) {
use crate::coretime::CoretimeProviderCalls::CreditAccount;
let credit_account_call = RelayRuntimePallets::Coretime(CreditAccount(who, amount));

let message = Xcm(vec![
Instruction::UnpaidExecution {
weight_limit: WeightLimit::Unlimited,
check_origin: None,
},
Instruction::Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(1000000000, 200000),
call: credit_account_call.encode().into(),
},
]);

match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) {
Ok(_) => log::debug!(
target: "runtime::coretime",
"Instruction to credit account sent successfully."
),
Err(e) => log::error!(
target: "runtime::coretime",
"Instruction to credit account failed to send: {:?}",
e
),
}
}

fn assign_core(
core: CoreIndex,
begin: RCBlockNumberOf<Self>,
assignment: Vec<(CoreAssignment, PartsOf57600)>,
end_hint: Option<RCBlockNumberOf<Self>>,
) {
use crate::coretime::CoretimeProviderCalls::AssignCore;
let assign_core_call =
RelayRuntimePallets::Coretime(AssignCore(core, begin, assignment, end_hint));

let message = Xcm(vec![
Instruction::UnpaidExecution {
weight_limit: WeightLimit::Unlimited,
check_origin: None,
},
Instruction::Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(1_000_000_000, 200000),
call: assign_core_call.encode().into(),
},
]);

match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) {
Ok(_) => log::debug!(
target: "runtime::coretime",
"Core assignment sent successfully."
),
Err(e) => log::error!(
target: "runtime::coretime",
"Core assignment failed to send: {:?}",
e
),
}
}

fn check_notify_revenue_info() -> Option<(RCBlockNumberOf<Self>, Self::Balance)> {
let revenue = CoretimeRevenue::get();
CoretimeRevenue::set(&None);
revenue
}

#[cfg(feature = "runtime-benchmarks")]
fn ensure_notify_revenue_info(when: RCBlockNumberOf<Self>, revenue: Self::Balance) {
CoretimeRevenue::set(&Some((when, revenue)));
}
}

impl pallet_broker::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type OnRevenue = CreditToCollatorPot;
#[cfg(feature = "fast-runtime")]
type TimeslicePeriod = ConstU32<10>;
#[cfg(not(feature = "fast-runtime"))]
type TimeslicePeriod = ConstU32<80>;
// We don't actually need any leases at launch but set to 10 in case we want to sudo some in.
type MaxLeasedCores = ConstU32<10>;
type MaxReservedCores = ConstU32<10>;
type Coretime = CoretimeAllocator;
type ConvertBalance = sp_runtime::traits::Identity;
type WeightInfo = weights::pallet_broker::WeightInfo<Runtime>;
type PalletId = BrokerPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type PriceAdapter = pallet_broker::Linear;
}
Loading
Loading