polkadot_node_primitives/approval/
criteria.rs1use crate::approval::{
20 v1::{DelayTranche, RelayVRFStory},
21 v2::{AssignmentCertV2, CoreBitfield},
22};
23use codec::{Decode, Encode};
24use polkadot_primitives::{
25 AssignmentId, CandidateHash, CoreIndex, GroupIndex, IndexedVec, SessionInfo, ValidatorIndex,
26};
27use sc_keystore::LocalKeystore;
28
29use std::collections::HashMap;
30
31#[derive(Debug, Clone, Encode, Decode, PartialEq)]
33pub struct OurAssignment {
34 cert: AssignmentCertV2,
35 tranche: DelayTranche,
36 validator_index: ValidatorIndex,
37 triggered: bool,
39}
40
41impl OurAssignment {
42 pub fn new(
44 cert: AssignmentCertV2,
45 tranche: DelayTranche,
46 validator_index: ValidatorIndex,
47 triggered: bool,
48 ) -> Self {
49 OurAssignment { cert, tranche, validator_index, triggered }
50 }
51 pub fn cert(&self) -> &AssignmentCertV2 {
53 &self.cert
54 }
55
56 pub fn into_cert(self) -> AssignmentCertV2 {
58 self.cert
59 }
60
61 pub fn tranche(&self) -> DelayTranche {
63 self.tranche
64 }
65
66 pub fn validator_index(&self) -> ValidatorIndex {
68 self.validator_index
69 }
70
71 pub fn triggered(&self) -> bool {
73 self.triggered
74 }
75
76 pub fn mark_triggered(&mut self) {
78 self.triggered = true;
79 }
80}
81
82#[derive(Clone, Debug)]
84pub struct Config {
85 pub assignment_keys: Vec<AssignmentId>,
87 pub validator_groups: IndexedVec<GroupIndex, Vec<ValidatorIndex>>,
89 pub n_cores: u32,
91 pub zeroth_delay_tranche_width: u32,
93 pub relay_vrf_modulo_samples: u32,
95 pub n_delay_tranches: u32,
97}
98
99impl<'a> From<&'a SessionInfo> for Config {
100 fn from(s: &'a SessionInfo) -> Self {
101 Config {
102 assignment_keys: s.assignment_keys.clone(),
103 validator_groups: s.validator_groups.clone(),
104 n_cores: s.n_cores,
105 zeroth_delay_tranche_width: s.zeroth_delay_tranche_width,
106 relay_vrf_modulo_samples: s.relay_vrf_modulo_samples,
107 n_delay_tranches: s.n_delay_tranches,
108 }
109 }
110}
111
112pub trait AssignmentCriteria {
117 fn compute_assignments(
119 &self,
120 keystore: &LocalKeystore,
121 relay_vrf_story: RelayVRFStory,
122 config: &Config,
123 leaving_cores: Vec<(CandidateHash, CoreIndex, GroupIndex)>,
124 enable_v2_assignments: bool,
125 ) -> HashMap<CoreIndex, OurAssignment>;
126
127 fn check_assignment_cert(
129 &self,
130 claimed_core_bitfield: CoreBitfield,
131 validator_index: ValidatorIndex,
132 config: &Config,
133 relay_vrf_story: RelayVRFStory,
134 assignment: &AssignmentCertV2,
135 backing_groups: Vec<GroupIndex>,
137 ) -> Result<DelayTranche, InvalidAssignment>;
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub struct InvalidAssignment(pub InvalidAssignmentReason);
143
144impl std::fmt::Display for InvalidAssignment {
145 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
146 write!(f, "Invalid Assignment: {:?}", self.0)
147 }
148}
149
150impl std::error::Error for InvalidAssignment {}
151
152#[derive(Debug, Clone, Copy, PartialEq, Eq)]
154pub enum InvalidAssignmentReason {
155 ValidatorIndexOutOfBounds,
157 SampleOutOfBounds,
159 CoreIndexOutOfBounds,
161 InvalidAssignmentKey,
163 IsInBackingGroup,
165 VRFModuloCoreIndexMismatch,
167 VRFModuloOutputMismatch,
169 VRFDelayCoreIndexMismatch,
171 VRFDelayOutputMismatch,
173 InvalidArguments,
175 NullAssignment,
177}