referrerpolicy=no-referrer-when-downgrade

fp_coretime/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Primitives for Polkadot Coretime.
19//!
20//! Contains fundamental types and the [`market::Market`] trait used by both `pallet-broker` and
21//! market implementations.
22
23#![cfg_attr(not(feature = "std"), no_std)]
24
25extern crate alloc;
26
27mod core_mask;
28pub mod market;
29
30pub use core_mask::*;
31
32use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
33use scale_info::TypeInfo;
34
35/// Index of a Polkadot Core.
36pub type CoreIndex = u16;
37
38/// A Coretime timeslice index. Each timeslice represents a fixed block interval.
39pub type Timeslice = u32;
40
41/// A Task Id. In general this is called a ParachainId.
42pub type TaskId = u32;
43
44/// Fraction expressed as a numerator with an implicit denominator of 57,600.
45pub type PartsOf57600 = u16;
46
47/// Self-describing identity for a Region of Bulk Coretime.
48#[derive(
49	Encode,
50	Decode,
51	DecodeWithMemTracking,
52	Copy,
53	Clone,
54	PartialEq,
55	Eq,
56	Debug,
57	TypeInfo,
58	MaxEncodedLen,
59)]
60pub struct RegionId {
61	/// The timeslice at which this Region begins.
62	pub begin: Timeslice,
63	/// The index of the Polkadot Core on which this Region will be scheduled.
64	pub core: CoreIndex,
65	/// The regularity parts in which this Region will be scheduled.
66	pub mask: CoreMask,
67}
68impl From<u128> for RegionId {
69	fn from(x: u128) -> Self {
70		Self { begin: (x >> 96) as u32, core: (x >> 80) as u16, mask: x.into() }
71	}
72}
73impl From<RegionId> for u128 {
74	fn from(x: RegionId) -> Self {
75		((x.begin as u128) << 96) | ((x.core as u128) << 80) | u128::from(x.mask)
76	}
77}
78
79#[cfg(test)]
80mod tests {
81	use super::*;
82
83	#[test]
84	fn region_id_converts_u128() {
85		let r =
86			RegionId { begin: 0x12345678u32, core: 0xabcdu16, mask: 0xdeadbeefcafef00d0123.into() };
87		let u = 0x12345678_abcd_deadbeefcafef00d0123u128;
88		assert_eq!(RegionId::from(u), r);
89		assert_eq!(u128::from(r), u);
90	}
91}
92
93/// The identity of a possibly renewable Core workload.
94#[derive(
95	Encode,
96	Decode,
97	DecodeWithMemTracking,
98	Copy,
99	Clone,
100	PartialEq,
101	Eq,
102	Debug,
103	TypeInfo,
104	MaxEncodedLen,
105)]
106pub struct PotentialRenewalId {
107	/// The core whose workload at the sale ending with `when` may be renewed to begin at `when`.
108	pub core: CoreIndex,
109	/// The point in time that the renewable workload on `core` ends and a fresh renewal may begin.
110	pub when: Timeslice,
111}