referrerpolicy=no-referrer-when-downgrade

pallet_broker/
coretime_interface.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#![deny(missing_docs)]
19
20use alloc::vec::Vec;
21use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
22use core::fmt::Debug;
23use frame_support::Parameter;
24use scale_info::TypeInfo;
25use sp_arithmetic::traits::AtLeast32BitUnsigned;
26use sp_runtime::traits::BlockNumberProvider;
27
28use crate::Timeslice;
29
30/// Index of a Polkadot Core.
31pub type CoreIndex = u16;
32
33/// A Task Id. In general this is called a ParachainId.
34pub type TaskId = u32;
35
36/// Fraction expressed as a nominator with an assumed denominator of 57,600.
37pub type PartsOf57600 = u16;
38
39/// An element to which a core can be assigned.
40#[derive(
41	Encode,
42	Decode,
43	DecodeWithMemTracking,
44	Clone,
45	Eq,
46	PartialEq,
47	Ord,
48	PartialOrd,
49	Debug,
50	TypeInfo,
51	MaxEncodedLen,
52)]
53pub enum CoreAssignment {
54	/// Core need not be used for anything.
55	Idle,
56	/// Core should be used for the Instantaneous Coretime Pool.
57	Pool,
58	/// Core should be used to process the given task.
59	Task(TaskId),
60}
61
62/// Relay chain block number of `T` that implements [`CoretimeInterface`].
63pub type RCBlockNumberOf<T> = <RCBlockNumberProviderOf<T> as BlockNumberProvider>::BlockNumber;
64
65/// Relay chain block number provider of `T` that implements [`CoretimeInterface`].
66pub type RCBlockNumberProviderOf<T> = <T as CoretimeInterface>::RelayChainBlockNumberProvider;
67
68/// Type able to accept Coretime scheduling instructions and provide certain usage information.
69/// Generally implemented by the Relay-chain or some means of communicating with it.
70///
71/// The trait representation of RFC#5 `<https://github.com/polkadot-fellows/RFCs/pull/5>`.
72pub trait CoretimeInterface {
73	/// A (Relay-chain-side) account ID.
74	type AccountId: Parameter;
75
76	/// A (Relay-chain-side) balance.
77	type Balance: AtLeast32BitUnsigned + Encode + Decode + MaxEncodedLen + TypeInfo + Debug;
78
79	/// A provider for the relay chain block number.
80	type RelayChainBlockNumberProvider: BlockNumberProvider;
81
82	/// Requests the Relay-chain to alter the number of schedulable cores to `count`. Under normal
83	/// operation, the Relay-chain SHOULD send a `notify_core_count(count)` message back.
84	fn request_core_count(count: CoreIndex);
85
86	/// Requests that the Relay-chain send a `notify_revenue` message back at or soon after
87	/// Relay-chain block number `when` whose `until` parameter is equal to `when`.
88	///
89	/// `when` may never be greater than the result of `Self::latest()`.
90	/// The period in to the past which `when` is allowed to be may be limited; if so the limit
91	/// should be understood on a channel outside of this proposal. In the case that the request
92	/// cannot be serviced because `when` is too old a block then a `notify_revenue` message must
93	/// still be returned, but its `revenue` field may be `None`.
94	fn request_revenue_info_at(when: RCBlockNumberOf<Self>);
95
96	/// Instructs the Relay-chain to add the `amount` of DOT to the Instantaneous Coretime Market
97	/// Credit account of `who`.
98	///
99	/// It is expected that Instantaneous Coretime Market Credit on the Relay-chain is NOT
100	/// transferable and only redeemable when used to assign cores in the Instantaneous Coretime
101	/// Pool.
102	fn credit_account(who: Self::AccountId, amount: Self::Balance);
103
104	/// Instructs the Relay-chain to ensure that the core indexed as `core` is utilised for a number
105	/// of assignments in specific ratios given by `assignment` starting as soon after `begin` as
106	/// possible. Core assignments take the form of a `CoreAssignment` value which can either task
107	/// the core to a `ParaId` value or indicate that the core should be used in the Instantaneous
108	/// Pool. Each assignment comes with a ratio value, represented as the numerator of the fraction
109	/// with a denominator of 57,600.
110	///
111	/// If `end_hint` is `Some` and the inner is greater than the current block number, then the
112	/// Relay-chain should optimize in the expectation of receiving a new `assign_core(core, ...)`
113	/// message at or prior to the block number of the inner value. Specific functionality should
114	/// remain unchanged regardless of the `end_hint` value.
115	fn assign_core(
116		core: CoreIndex,
117		begin: RCBlockNumberOf<Self>,
118		assignment: Vec<(CoreAssignment, PartsOf57600)>,
119		end_hint: Option<RCBlockNumberOf<Self>>,
120	);
121
122	/// A hook supposed to be called right after a new timeslice has begun. Likely to be used for
123	/// batching different matters happened during the timeslice that may benefit from batched
124	/// processing.
125	fn on_new_timeslice(_timeslice: Timeslice) {}
126}
127
128impl CoretimeInterface for () {
129	type AccountId = ();
130	type Balance = u64;
131	type RelayChainBlockNumberProvider = ();
132
133	fn request_core_count(_count: CoreIndex) {}
134	fn request_revenue_info_at(_when: RCBlockNumberOf<Self>) {}
135	fn credit_account(_who: Self::AccountId, _amount: Self::Balance) {}
136	fn assign_core(
137		_core: CoreIndex,
138		_begin: RCBlockNumberOf<Self>,
139		_assignment: Vec<(CoreAssignment, PartsOf57600)>,
140		_end_hint: Option<RCBlockNumberOf<Self>>,
141	) {
142	}
143}