referrerpolicy=no-referrer-when-downgrade

pallet_broker/
types.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
18use crate::{
19	Config, CoreAssignment, CoreIndex, CoreMask, CoretimeInterface, RCBlockNumberOf, TaskId,
20	Timeslice, CORE_MASK_BITS,
21};
22use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
23use frame_support::traits::fungible::Inspect;
24use frame_system::Config as SConfig;
25use scale_info::TypeInfo;
26use sp_arithmetic::Perbill;
27use sp_core::ConstU32;
28use sp_runtime::BoundedVec;
29
30pub type BalanceOf<T> = <<T as Config>::Currency as Inspect<<T as SConfig>::AccountId>>::Balance;
31pub type RelayBalanceOf<T> = <<T as Config>::Coretime as CoretimeInterface>::Balance;
32pub type RelayBlockNumberOf<T> = RCBlockNumberOf<<T as Config>::Coretime>;
33pub type RelayAccountIdOf<T> = <<T as Config>::Coretime as CoretimeInterface>::AccountId;
34
35/// Counter for the total number of set bits over every core's `CoreMask`. `u32` so we don't
36/// ever get an overflow. This is 1/80th of a Polkadot Core per timeslice. Assuming timeslices are
37/// 80 blocks, then this indicates usage of a single core one time over a timeslice.
38pub type CoreMaskBitCount = u32;
39/// The same as `CoreMaskBitCount` but signed.
40pub type SignedCoreMaskBitCount = i32;
41/// A sequential index for identifying a sale period.
42pub type SaleIndex = u32;
43
44/// Whether a core assignment is revokable or not.
45#[derive(
46	Encode,
47	Decode,
48	DecodeWithMemTracking,
49	Copy,
50	Clone,
51	PartialEq,
52	Eq,
53	Debug,
54	TypeInfo,
55	MaxEncodedLen,
56)]
57pub enum Finality {
58	/// The region remains with the same owner allowing the assignment to be altered.
59	Provisional,
60	/// The region is removed; the assignment may be eligible for renewal.
61	Final,
62}
63
64/// The rest of the information describing a Region.
65#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
66pub struct RegionRecord<AccountId, Balance> {
67	/// The end of the Region.
68	pub end: Timeslice,
69	/// The owner of the Region.
70	pub owner: Option<AccountId>,
71	/// The amount paid to Polkadot for this Region, or `None` if renewal is not allowed.
72	pub paid: Option<Balance>,
73}
74pub type RegionRecordOf<T> = RegionRecord<<T as SConfig>::AccountId, BalanceOf<T>>;
75
76/// An distinct item which can be scheduled on a Polkadot Core.
77#[derive(
78	Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
79)]
80pub struct ScheduleItem {
81	/// The regularity parts in which this Item will be scheduled on the Core.
82	pub mask: CoreMask,
83	/// The job that the Core should be doing.
84	pub assignment: CoreAssignment,
85}
86pub type Schedule = BoundedVec<ScheduleItem, ConstU32<{ CORE_MASK_BITS as u32 }>>;
87
88/// The record body of a Region which was contributed to the Instantaneous Coretime Pool. This helps
89/// with making pro rata payments to contributors.
90#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
91pub struct ContributionRecord<AccountId> {
92	/// The end of the Region contributed.
93	pub length: Timeslice,
94	/// The identity of the contributor.
95	pub payee: AccountId,
96}
97pub type ContributionRecordOf<T> = ContributionRecord<<T as SConfig>::AccountId>;
98
99/// A per-timeslice bookkeeping record for tracking Instantaneous Coretime Pool activity and
100/// making proper payments to contributors.
101#[derive(Encode, Decode, Clone, Default, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
102pub struct InstaPoolHistoryRecord<Balance> {
103	/// The total amount of Coretime (measured in Core Mask Bits minus any contributions which have
104	/// already been paid out.
105	pub private_contributions: CoreMaskBitCount,
106	/// The total amount of Coretime (measured in Core Mask Bits contributed by the Polkadot System
107	/// in this timeslice.
108	pub system_contributions: CoreMaskBitCount,
109	/// The payout remaining for the `private_contributions`, or `None` if the revenue is not yet
110	/// known.
111	pub maybe_payout: Option<Balance>,
112}
113pub type InstaPoolHistoryRecordOf<T> = InstaPoolHistoryRecord<BalanceOf<T>>;
114
115/// How much of a core has been assigned or, if completely assigned, the workload itself.
116#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
117pub enum CompletionStatus {
118	/// The core is not fully assigned; the inner is the parts which have.
119	Partial(CoreMask),
120	/// The core is fully assigned; the inner is the workload which has been assigned.
121	Complete(Schedule),
122}
123impl CompletionStatus {
124	/// Return reference to the complete workload, or `None` if incomplete.
125	pub fn complete(&self) -> Option<&Schedule> {
126		match self {
127			Self::Complete(s) => Some(s),
128			Self::Partial(_) => None,
129		}
130	}
131	/// Return the complete workload, or `None` if incomplete.
132	pub fn drain_complete(self) -> Option<Schedule> {
133		match self {
134			Self::Complete(s) => Some(s),
135			Self::Partial(_) => None,
136		}
137	}
138}
139
140/// A record of a potential renewal.
141///
142/// The renewal will only actually be allowed if `CompletionStatus` is `Complete` at the time of
143/// renewal.
144#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
145pub struct PotentialRenewalRecord<Balance> {
146	/// The price for which the next renewal can be made.
147	pub price: Balance,
148	/// The workload which will be scheduled on the Core in the case a renewal is made, or if
149	/// incomplete, then the parts of the core which have been scheduled.
150	pub completion: CompletionStatus,
151}
152pub type PotentialRenewalRecordOf<T> = PotentialRenewalRecord<BalanceOf<T>>;
153
154/// General status of the system.
155#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
156pub struct StatusRecord {
157	/// The total number of cores which can be assigned (one plus the maximum index which can
158	/// be used in `Coretime::assign`).
159	pub core_count: CoreIndex,
160	/// The current size of the Instantaneous Coretime Pool, measured in
161	/// Core Mask Bits.
162	pub private_pool_size: CoreMaskBitCount,
163	/// The current amount of the Instantaneous Coretime Pool which is provided by the Polkadot
164	/// System, rather than provided as a result of privately operated Coretime.
165	pub system_pool_size: CoreMaskBitCount,
166	/// The last (Relay-chain) timeslice which we committed to the Relay-chain.
167	pub last_committed_timeslice: Timeslice,
168	/// The timeslice of the last time we ticked.
169	pub last_timeslice: Timeslice,
170}
171
172/// A record of flux in the InstaPool.
173#[derive(Encode, Decode, Clone, Copy, Default, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
174pub struct PoolIoRecord {
175	/// The total change of the portion of the pool supplied by purchased Bulk Coretime, measured
176	/// in Core Mask Bits.
177	pub private: SignedCoreMaskBitCount,
178	/// The total change of the portion of the pool supplied by the Polkadot System, measured in
179	/// Core Mask Bits.
180	pub system: SignedCoreMaskBitCount,
181}
182
183/// The status of a Bulk Coretime Sale.
184#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
185pub struct SaleInfoRecord<Balance, RelayBlockNumber> {
186	/// The relay block number at which the sale will/did start.
187	pub sale_start: RelayBlockNumber,
188	/// The length in blocks of the Leadin Period (where the price is decreasing).
189	pub leadin_length: RelayBlockNumber,
190	/// The price of Bulk Coretime after the Leadin Period.
191	pub end_price: Balance,
192	/// The first timeslice of the Regions which are being sold in this sale.
193	pub region_begin: Timeslice,
194	/// The timeslice on which the Regions which are being sold in the sale terminate. (i.e. One
195	/// after the last timeslice which the Regions control.)
196	pub region_end: Timeslice,
197	/// The number of cores we want to sell, ideally. Selling this amount would result in no
198	/// change to the price for the next sale.
199	pub ideal_cores_sold: CoreIndex,
200	/// Number of cores which are/have been offered for sale.
201	pub cores_offered: CoreIndex,
202	/// The index of the first core which is for sale. Core of Regions which are sold have
203	/// incrementing indices from this.
204	pub first_core: CoreIndex,
205	/// The price at which cores have been sold out.
206	///
207	/// Will only be `None` if no core was offered for sale.
208	pub sellout_price: Option<Balance>,
209	/// Number of cores which have been sold; never more than cores_offered.
210	pub cores_sold: CoreIndex,
211	/// Identifier for the current sale.
212	pub sale_index: SaleIndex,
213}
214pub type SaleInfoRecordOf<T> = SaleInfoRecord<BalanceOf<T>, RelayBlockNumberOf<T>>;
215
216/// Record for Polkadot Core reservations (generally tasked with the maintenance of System
217/// Chains).
218pub type ReservationsRecord<Max> = BoundedVec<Schedule, Max>;
219pub type ReservationsRecordOf<T> = ReservationsRecord<<T as Config>::MaxReservedCores>;
220
221/// Information on a single legacy lease.
222#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
223pub struct LeaseRecordItem {
224	/// The timeslice until the lease is valid.
225	pub until: Timeslice,
226	/// The task which the lease is for.
227	pub task: TaskId,
228}
229
230/// Record for Polkadot Core legacy leases.
231pub type LeasesRecord<Max> = BoundedVec<LeaseRecordItem, Max>;
232pub type LeasesRecordOf<T> = LeasesRecord<<T as Config>::MaxLeasedCores>;
233
234/// Record for On demand core sales.
235///
236/// The blocknumber is the relay chain block height `until` which the original request
237/// for revenue was made.
238#[derive(
239	Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
240)]
241pub struct OnDemandRevenueRecord<RelayBlockNumber, RelayBalance> {
242	/// The height of the Relay-chain at the time the revenue request was made.
243	pub until: RelayBlockNumber,
244	/// The accumulated balance of on demand sales made on the relay chain.
245	pub amount: RelayBalance,
246}
247
248pub type OnDemandRevenueRecordOf<T> =
249	OnDemandRevenueRecord<RelayBlockNumberOf<T>, RelayBalanceOf<T>>;
250
251/// Configuration of this pallet.
252#[derive(
253	Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen,
254)]
255pub struct ConfigRecord<RelayBlockNumber> {
256	/// The number of Relay-chain blocks in advance which scheduling should be fixed and the
257	/// `Coretime::assign` API used to inform the Relay-chain.
258	pub advance_notice: RelayBlockNumber,
259	/// The length in blocks of the Interlude Period for forthcoming sales.
260	pub interlude_length: RelayBlockNumber,
261	/// The length in blocks of the Leadin Period for forthcoming sales.
262	pub leadin_length: RelayBlockNumber,
263	/// The length in timeslices of Regions which are up for sale in forthcoming sales.
264	pub region_length: Timeslice,
265	/// The proportion of cores available for sale which should be sold.
266	///
267	/// If more cores are sold than this, then further sales will no longer be considered in
268	/// determining the sellout price. In other words the sellout price will be the last price
269	/// paid, without going over this limit.
270	pub ideal_bulk_proportion: Perbill,
271	/// An artificial limit to the number of cores which are allowed to be sold. If `Some` then
272	/// no more cores will be sold than this.
273	pub limit_cores_offered: Option<CoreIndex>,
274	/// The amount by which the renewal price increases each sale period.
275	pub renewal_bump: Perbill,
276	/// The duration by which rewards for contributions to the InstaPool must be collected.
277	pub contribution_timeout: Timeslice,
278}
279pub type ConfigRecordOf<T> = ConfigRecord<RelayBlockNumberOf<T>>;
280
281impl<RelayBlockNumber> ConfigRecord<RelayBlockNumber>
282where
283	RelayBlockNumber: sp_arithmetic::traits::Zero,
284{
285	/// Check the config for basic validity constraints.
286	pub(crate) fn validate(&self) -> Result<(), ()> {
287		if self.leadin_length.is_zero() {
288			return Err(());
289		}
290
291		Ok(())
292	}
293}
294
295/// A record containing information regarding auto-renewal for a specific core.
296#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo, MaxEncodedLen)]
297pub struct AutoRenewalRecord {
298	/// The core for which auto renewal is enabled.
299	pub core: CoreIndex,
300	/// The task assigned to the core. We keep track of it so we don't have to look it up when
301	/// performing auto-renewal.
302	pub task: TaskId,
303	/// Specifies when the upcoming renewal should be performed. This is used for lease holding
304	/// tasks to ensure that the renewal process does not begin until the lease expires.
305	pub next_renewal: Timeslice,
306}