polkadot_runtime_parachains/scheduler/common.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17//! Common traits and types used by the scheduler and assignment providers.
18
19use scale_info::TypeInfo;
20use sp_runtime::{
21 codec::{Decode, Encode},
22 RuntimeDebug,
23};
24
25use polkadot_primitives::{CoreIndex, Id as ParaId};
26
27/// Assignment (ParaId -> CoreIndex).
28#[derive(Encode, Decode, TypeInfo, RuntimeDebug, Clone, PartialEq)]
29pub enum Assignment {
30 /// A pool assignment.
31 Pool {
32 /// The assigned para id.
33 para_id: ParaId,
34 /// The core index the para got assigned to.
35 core_index: CoreIndex,
36 },
37 /// A bulk assignment.
38 Bulk(ParaId),
39}
40
41impl Assignment {
42 /// Returns the [`ParaId`] this assignment is associated to.
43 pub fn para_id(&self) -> ParaId {
44 match self {
45 Self::Pool { para_id, .. } => *para_id,
46 Self::Bulk(para_id) => *para_id,
47 }
48 }
49}
50
51pub trait AssignmentProvider<BlockNumber> {
52 /// Pops an [`Assignment`] from the provider for a specified [`CoreIndex`].
53 ///
54 /// This is where assignments come into existence.
55 fn pop_assignment_for_core(core_idx: CoreIndex) -> Option<Assignment>;
56
57 /// A previously popped `Assignment` has been fully processed.
58 ///
59 /// Report back to the assignment provider that an assignment is done and no longer present in
60 /// the scheduler.
61 ///
62 /// This is one way of the life of an assignment coming to an end.
63 fn report_processed(assignment: Assignment);
64
65 /// Push back a previously popped assignment.
66 ///
67 /// If the assignment could not be processed within the current session, it can be pushed back
68 /// to the assignment provider in order to be popped again later.
69 ///
70 /// This is the second way the life of an assignment can come to an end.
71 fn push_back_assignment(assignment: Assignment);
72
73 /// Push some assignment for mocking/benchmarks purposes.
74 ///
75 /// Useful for benchmarks and testing. The returned assignment is "valid" and can if need be
76 /// passed into `report_processed` for example.
77 #[cfg(any(feature = "runtime-benchmarks", test))]
78 fn get_mock_assignment(core_idx: CoreIndex, para_id: ParaId) -> Assignment;
79
80 /// Report that an assignment was duplicated by the scheduler.
81 fn assignment_duplicated(assignment: &Assignment);
82}