ic_backup/model/control_authority/
mod.rs1use crate::model::attempt_journal::OperationBindingRecord;
4use thiserror::Error;
5
6pub const MAX_CONTROLLERS: usize = 10;
8
9#[derive(Clone, Debug, Eq, PartialEq)]
14pub struct ControllerSet {
15 principals: Vec<String>,
16}
17impl ControllerSet {
18 pub fn new(principals: Vec<String>) -> Result<Self, ControlObservationError> {
22 let principals = super::principal::canonical_set(
23 principals,
24 MAX_CONTROLLERS,
25 || ControlObservationError::TooManyControllers,
26 || ControlObservationError::InvalidPrincipal,
27 || ControlObservationError::DuplicateController,
28 )?;
29 Ok(Self { principals })
30 }
31 #[must_use]
33 pub fn principals(&self) -> &[String] {
34 &self.principals
35 }
36 #[must_use]
38 pub fn contains_caller(&self, binding: &OperationBindingRecord) -> bool {
39 self.principals
40 .binary_search_by(|principal| principal.as_str().cmp(binding.caller()))
41 .is_ok()
42 }
43}
44
45#[derive(Debug, Eq, Error, PartialEq)]
47pub enum ControlObservationError {
48 #[error("invalid control observation principal")]
50 InvalidPrincipal,
51 #[error("controller set exceeds {MAX_CONTROLLERS}")]
53 TooManyControllers,
54 #[error("duplicate controller principal")]
56 DuplicateController,
57}
58
59#[cfg(test)]
60mod tests;