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

Enable Coretime on Polkadot Relay #401

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Core-Fellowship: new `promote_fast` call ([runtimes#381](https://github.com/polkadot-fellows/runtimes/pull/381), [SDK v1.14 #4877](https://github.com/paritytech/polkadot-sdk/pull/4877)).
- Pallet ranked collective: max member count per rank ([runtimes#381](https://github.com/polkadot-fellows/runtimes/pull/381), [SDK v1.14 #4807](https://github.com/paritytech/polkadot-sdk/pull/4807)).
- All runtimes: XcmPaymentApi and DryRunApi ([polkadot-fellows/runtimes#380](https://github.com/polkadot-fellows/runtimes/pull/380))
- Enable Agile Coretime on Polkadot ([polkadot-fellows/runtimes#322](https://github.com/polkadot-fellows/runtimes/pull/322))
tdimitrov marked this conversation as resolved.
Show resolved Hide resolved

#### From [#322](https://github.com/polkadot-fellows/runtimes/pull/322):

Expand Down
13 changes: 13 additions & 0 deletions relay/polkadot/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,22 @@ pub mod system_parachain {
pub const BRIDGE_HUB_ID: u32 = 1002;
/// People parachain ID.
pub const PEOPLE_ID: u32 = 1004;
/// Brokerage parachain ID.
tdimitrov marked this conversation as resolved.
Show resolved Hide resolved
pub const BROKER_ID: u32 = 1005;

// System parachains from Polkadot point of view.
pub type SystemParachains = IsChildSystemParachain<Id>;

/// Coretime constants
pub mod coretime {
/// Coretime timeslice period in blocks
/// WARNING: This constant is used across chains, so additional care should be taken
/// when changing it.
#[cfg(feature = "fast-runtime")]
pub const TIMESLICE_PERIOD: u32 = 20;
#[cfg(not(feature = "fast-runtime"))]
pub const TIMESLICE_PERIOD: u32 = 80;
}
}

/// Polkadot Treasury pallet instance.
Expand Down
156 changes: 139 additions & 17 deletions relay/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ use polkadot_runtime_common::{
impls::{
DealWithFees, LocatableAssetConverter, VersionedLocatableAsset, VersionedLocationConverter,
},
paras_registrar, prod_or_fast, slots, BlockHashCount, BlockLength, CurrencyToVote,
SlowAdjustingFeeUpdate,
paras_registrar, prod_or_fast, slots,
traits::OnSwap,
BlockHashCount, BlockLength, CurrencyToVote, SlowAdjustingFeeUpdate,
};

use runtime_parachains::{
assigner_parachains as parachains_assigner_parachains,
configuration as parachains_configuration,
assigner_coretime as parachains_assigner_coretime,
assigner_on_demand as parachains_assigner_on_demand, configuration as parachains_configuration,
configuration::ActiveConfigHrmpChannelSizeAndCapacityRatio,
disputes as parachains_disputes,
coretime, disputes as parachains_disputes,
disputes::slashing as parachains_slashing,
dmp as parachains_dmp, hrmp as parachains_hrmp, inclusion as parachains_inclusion,
inclusion::{AggregateMessageOrigin, UmpQueueId},
Expand Down Expand Up @@ -62,10 +63,10 @@ use frame_support::{
parameter_types,
traits::{
fungible::HoldConsideration, ConstU32, Contains, EitherOf, EitherOfDiverse, EverythingBut,
Get, InstanceFilter, KeyOwnerProofSystem, LinearStoragePrice, PrivilegeCmp, ProcessMessage,
ProcessMessageError, WithdrawReasons,
Get, InstanceFilter, KeyOwnerProofSystem, LinearStoragePrice, OnRuntimeUpgrade,
PrivilegeCmp, ProcessMessage, ProcessMessageError, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter, WeightToFee as _},
weights::{constants, ConstantMultiplier, WeightMeter, WeightToFee as _},
PalletId,
};
use frame_system::EnsureRoot;
Expand All @@ -87,8 +88,9 @@ use sp_runtime::{
curve::PiecewiseLinear,
generic, impl_opaque_keys,
traits::{
AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto, Extrinsic as ExtrinsicT,
IdentityLookup, Keccak256, OpaqueKeys, SaturatedConversion, Verify,
AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto,
Extrinsic as ExtrinsicT, IdentityLookup, Keccak256, OpaqueKeys, SaturatedConversion,
Verify,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedU128, KeyTypeId, Perbill, Percent, Permill, RuntimeDebug,
Expand Down Expand Up @@ -118,7 +120,9 @@ pub use pallet_timestamp::Call as TimestampCall;
pub use sp_runtime::BuildStorage;

/// Constant values used within the runtime.
use polkadot_runtime_constants::{currency::*, fee::*, time::*, TREASURY_PALLET_ID};
use polkadot_runtime_constants::{
currency::*, fee::*, system_parachain, time::*, TREASURY_PALLET_ID,
};

// Weights used in the runtime.
mod weights;
Expand Down Expand Up @@ -1293,7 +1297,7 @@ impl parachains_paras::Config for Runtime {
type QueueFootprinter = ParaInclusion;
type NextSessionRotation = Babe;
type OnNewHead = Registrar;
type AssignCoretime = ();
type AssignCoretime = CoretimeAssignmentProvider;
}

parameter_types! {
Expand Down Expand Up @@ -1374,16 +1378,63 @@ impl parachains_paras_inherent::Config for Runtime {
}

impl parachains_scheduler::Config for Runtime {
type AssignmentProvider = ParaAssignmentProvider;
// If you change this, make sure the `Assignment` type of the new provider is binary compatible,
// otherwise provide a migration.
type AssignmentProvider = CoretimeAssignmentProvider;
}

parameter_types! {
pub const BrokerId: u32 = system_parachain::BROKER_ID;
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
pub MaxXcmTransactWeight: Weight = Weight::from_parts(constants::WEIGHT_REF_TIME_PER_MILLIS, 20 * constants::WEIGHT_PROOF_SIZE_PER_KB);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as here: do we need such a huge buffer? Or maybe the other way round, do we care that we have such a big buffer? If not then I'd increase the Coretime Chain side too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took this from Kusama. If the value is not appropriate we should change it there too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I probably took the values from master instead of from the release branch. Fixed.

}

pub struct BrokerPot;
impl Get<InteriorLocation> for BrokerPot {
fn get() -> InteriorLocation {
Junction::AccountId32 { network: None, id: BrokerPalletId::get().into_account_truncating() }
.into()
}
}

impl coretime::Config for Runtime {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type BrokerId = BrokerId;
type WeightInfo = weights::runtime_parachains_coretime::WeightInfo<Runtime>;
type SendXcm = crate::xcm_config::XcmRouter;
type MaxXcmTransactWeight = MaxXcmTransactWeight;
type BrokerPotLocation = BrokerPot;
type AssetTransactor = crate::xcm_config::LocalAssetTransactor;
type AccountToLocation = xcm_builder::AliasesIntoAccountId32<
xcm_config::ThisNetwork,
<Runtime as frame_system::Config>::AccountId,
>;
}

impl parachains_assigner_parachains::Config for Runtime {}
parameter_types! {
pub const OnDemandTrafficDefaultValue: FixedU128 = FixedU128::from_u32(1);
pub const MaxHistoricalRevenue: BlockNumber = 2 * system_parachain::coretime::TIMESLICE_PERIOD;
pub const OnDemandPalletId: PalletId = PalletId(*b"py/ondmd");
}

impl parachains_assigner_on_demand::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type TrafficDefaultValue = OnDemandTrafficDefaultValue;
type WeightInfo = weights::runtime_parachains_assigner_on_demand::WeightInfo<Runtime>;
type MaxHistoricalRevenue = MaxHistoricalRevenue;
type PalletId = OnDemandPalletId;
}

impl parachains_assigner_coretime::Config for Runtime {}

impl parachains_initializer::Config for Runtime {
type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
type ForceOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::runtime_parachains_initializer::WeightInfo<Runtime>;
type CoretimeOnNewSession = ();
type CoretimeOnNewSession = Coretime;
}

impl parachains_disputes::Config for Runtime {
Expand Down Expand Up @@ -1421,7 +1472,7 @@ impl paras_registrar::Config for Runtime {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type OnSwap = (Crowdloan, Slots);
type OnSwap = (Crowdloan, Slots, SwapLeases);
type ParaDeposit = ParaDeposit;
type DataDepositPerByte = ParaDataByteDeposit;
type WeightInfo = weights::polkadot_runtime_common_paras_registrar::WeightInfo<Runtime>;
Expand Down Expand Up @@ -1571,6 +1622,13 @@ impl pallet_asset_rate::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = polkadot_runtime_common::impls::benchmarks::AssetRateArguments;
}
// Notify `coretime` Pallet when a lease swap occurs
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
pub struct SwapLeases;
impl OnSwap for SwapLeases {
fn on_swap(one: ParaId, other: ParaId) {
coretime::Pallet::<Runtime>::on_legacy_lease_swap(one, other);
}
}

construct_runtime! {
pub enum Runtime
Expand Down Expand Up @@ -1654,13 +1712,15 @@ construct_runtime! {
ParaSessionInfo: parachains_session_info = 61,
ParasDisputes: parachains_disputes = 62,
ParasSlashing: parachains_slashing = 63,
ParaAssignmentProvider: parachains_assigner_parachains = 64,
OnDemand: parachains_assigner_on_demand = 64,
CoretimeAssignmentProvider: parachains_assigner_coretime = 65,

// Parachain Onboarding Pallets. Start indices at 70 to leave room.
Registrar: paras_registrar = 70,
Slots: slots = 71,
Auctions: auctions = 72,
Crowdloan: crowdloan = 73,
Coretime: coretime = 74,

// State trie migration pallet, only temporary.
StateTrieMigration: pallet_state_trie_migration = 98,
Expand Down Expand Up @@ -1726,13 +1786,73 @@ pub type Migrations = (migrations::Unreleased, migrations::Permanent);
/// The runtime migrations per release.
#[allow(deprecated, missing_docs)]
pub mod migrations {
use polkadot_runtime_common::traits::Leaser;

use super::*;

pub struct GetLegacyLeaseImpl;
impl coretime::migration::GetLegacyLease<BlockNumber> for GetLegacyLeaseImpl {
fn get_parachain_lease_in_blocks(para: ParaId) -> Option<BlockNumber> {
let now = frame_system::Pallet::<Runtime>::block_number();
let lease = slots::Leases::<Runtime>::get(para);
if lease.is_empty() {
return None
tdimitrov marked this conversation as resolved.
Show resolved Hide resolved
}
let (index, _) =
<slots::Pallet<Runtime> as Leaser<BlockNumber>>::lease_period_index(now)?;
Some(index.saturating_add(lease.len() as u32).saturating_mul(LeasePeriod::get()))
}
}

/// Cancel all ongoing auctions.
///
/// Any leases that come into existence after coretime was launched will not be served. Yet,
/// any ongoing auctions must be cancelled.
///
/// Safety:
///
/// - After coretime is launched, there are no auctions anymore. So if this forgotten to
/// be removed after the runtime upgrade, running this again on the next one is harmless.
/// - I am assuming scheduler `TaskName`s are unique, so removal of the scheduled entry
/// multiple times should also be fine.
pub struct CancelAuctions;
impl OnRuntimeUpgrade for CancelAuctions {
fn on_runtime_upgrade() -> Weight {
if let Err(err) = Auctions::cancel_auction(frame_system::RawOrigin::Root.into()) {
log::debug!(target: "runtime", "Cancelling auctions failed: {:?}", err);
}
// Cancel scheduled auction as well:
if let Err(err) = Scheduler::cancel_named(
pallet_custom_origins::Origin::AuctionAdmin.into(),
[
0x87, 0xa8, 0x71, 0xb4, 0xd6, 0x21, 0xf0, 0xb9, 0x73, 0x47, 0x5a, 0xaf, 0xcc,
0x32, 0x61, 0x0b, 0xd7, 0x68, 0x8f, 0x15, 0x02, 0x33, 0x8a, 0xcd, 0x00, 0xee,
0x48, 0x8a, 0xc3, 0x62, 0x0f, 0x4c,
],
) {
log::debug!(target: "runtime", "Cancelling scheduled auctions failed: {:?}", err);
}
use pallet_scheduler::WeightInfo as _;
use polkadot_runtime_common::auctions::WeightInfo as _;
weights::polkadot_runtime_common_auctions::WeightInfo::<Runtime>::cancel_auction()
.saturating_add(weights::pallet_scheduler::WeightInfo::<Runtime>::cancel_named(
<Runtime as pallet_scheduler::Config>::MaxScheduledPerBlock::get(),
))
}
}

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
parachains_configuration::migration::v12::MigrateToV12<Runtime>,
parachains_inclusion::migration::MigrateToV1<Runtime>,
pallet_staking::migrations::v15::MigrateV14ToV15<Runtime>,
// Migrate from legacy lease to coretime. Needs to run after configuration v11
coretime::migration::MigrateToCoretime<
Runtime,
crate::xcm_config::XcmRouter,
GetLegacyLeaseImpl,
>,
CancelAuctions,
);

/// Migrations/checks that do not need to be versioned and can run on every update.
Expand Down Expand Up @@ -1773,6 +1893,8 @@ mod benches {
[runtime_parachains::initializer, Initializer]
[runtime_parachains::paras, Paras]
[runtime_parachains::paras_inherent, ParaInherent]
[runtime_parachains::assigner_on_demand, OnDemand]
[runtime_parachains::coretime, Coretime]
// Substrate
[pallet_bags_list, VoterList]
[pallet_balances, Balances]
Expand Down
2 changes: 2 additions & 0 deletions relay/polkadot/src/weights/mod.rs

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

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

Loading
Loading