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, the [`market::Market`] trait used by both `pallet-broker` and
21//! market implementations, and the [`revenue::OnDemandRevenue`] trait connecting `pallet-broker`
22//! to a local on-demand Coretime market.
23
24#![cfg_attr(not(feature = "std"), no_std)]
25
26extern crate alloc;
27
28mod core_mask;
29pub mod market;
30pub mod revenue;
31
32pub use core_mask::*;
33
34use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
35use scale_info::TypeInfo;
36
37/// Index of a Polkadot Core.
38pub type CoreIndex = u16;
39
40/// A Coretime timeslice index. Each timeslice represents a fixed block interval.
41pub type Timeslice = u32;
42
43/// A Task Id. In general this is called a ParachainId.
44pub type TaskId = u32;
45
46/// Fraction expressed as a numerator with an implicit denominator of 57,600.
47pub type PartsOf57600 = u16;
48
49/// Self-describing identity for a Region of Bulk Coretime.
50#[derive(
51 Encode,
52 Decode,
53 DecodeWithMemTracking,
54 Copy,
55 Clone,
56 PartialEq,
57 Eq,
58 Debug,
59 TypeInfo,
60 MaxEncodedLen,
61)]
62pub struct RegionId {
63 /// The timeslice at which this Region begins.
64 pub begin: Timeslice,
65 /// The index of the Polkadot Core on which this Region will be scheduled.
66 pub core: CoreIndex,
67 /// The regularity parts in which this Region will be scheduled.
68 pub mask: CoreMask,
69}
70impl From<u128> for RegionId {
71 fn from(x: u128) -> Self {
72 Self { begin: (x >> 96) as u32, core: (x >> 80) as u16, mask: x.into() }
73 }
74}
75impl From<RegionId> for u128 {
76 fn from(x: RegionId) -> Self {
77 ((x.begin as u128) << 96) | ((x.core as u128) << 80) | u128::from(x.mask)
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn region_id_converts_u128() {
87 let r =
88 RegionId { begin: 0x12345678u32, core: 0xabcdu16, mask: 0xdeadbeefcafef00d0123.into() };
89 let u = 0x12345678_abcd_deadbeefcafef00d0123u128;
90 assert_eq!(RegionId::from(u), r);
91 assert_eq!(u128::from(r), u);
92 }
93}
94
95/// The identity of a possibly renewable Core workload.
96#[derive(
97 Encode,
98 Decode,
99 DecodeWithMemTracking,
100 Copy,
101 Clone,
102 PartialEq,
103 Eq,
104 Debug,
105 TypeInfo,
106 MaxEncodedLen,
107)]
108pub struct PotentialRenewalId {
109 /// The core whose workload at the sale ending with `when` may be renewed to begin at `when`.
110 pub core: CoreIndex,
111 /// The point in time that the renewable workload on `core` ends and a fresh renewal may begin.
112 pub when: Timeslice,
113}