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