Skip to content

Commit

Permalink
feat: check for max number of controllers (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 11, 2023
1 parent a75549a commit 2cce7e6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/mission_control/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mission_control"
version = "0.0.1"
version = "0.0.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
10 changes: 10 additions & 0 deletions src/mission_control/src/controllers/mission_control.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use crate::controllers::store::{add_controllers, get_controllers, remove_controllers};
use crate::store::get_user;
use ic_cdk::id;
use shared::constants::MAX_NUMBER_OF_MISSION_CONTROL_CONTROLLERS;
use shared::ic::update_canister_controllers;
use shared::types::interface::{Controllers, UserId};

pub async fn add_mission_control_controllers(controllers: &[UserId]) -> Result<(), String> {
let current_controllers = get_controllers();

if current_controllers.len() >= MAX_NUMBER_OF_MISSION_CONTROL_CONTROLLERS {
return Err(format!(
"Maximum number of controllers ({}) is already reached.",
MAX_NUMBER_OF_MISSION_CONTROL_CONTROLLERS
));
}

add_controllers(controllers);

let updated_controllers = get_controllers();
Expand Down
10 changes: 10 additions & 0 deletions src/satellite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use ic_cdk::export::candid::{candid_method, export_service};
use ic_cdk::storage::{stable_restore, stable_save};
use ic_cdk_macros::{init, post_upgrade, pre_upgrade, query, update};
use rules::constants::DEFAULT_DB_COLLECTIONS;
use shared::constants::MAX_NUMBER_OF_SATELLITE_CONTROLLERS;
use shared::types::interface::{Controllers, ControllersArgs, SatelliteArgs};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, HashSet};
Expand Down Expand Up @@ -219,6 +220,15 @@ fn set_rule(rules_type: RulesType, collection: CollectionKey, rule: SetRule) {
#[candid_method(update)]
#[update(guard = "caller_is_controller")]
fn add_controllers(ControllersArgs { controllers }: ControllersArgs) -> Controllers {
let current_controllers = get_controllers();

if current_controllers.len() >= MAX_NUMBER_OF_SATELLITE_CONTROLLERS {
trap(&format!(
"Maximum number of controllers ({}) is already reached.",
MAX_NUMBER_OF_SATELLITE_CONTROLLERS
));
}

add_controllers_store(&controllers);
get_controllers()
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shared"
version = "0.0.1"
version = "0.0.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 6 additions & 0 deletions src/shared/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ pub const MEMO_CANISTER_CREATE: Memo = Memo(0x41455243); // == 'CREA'
pub const MEMO_CANISTER_TOP_UP: Memo = Memo(0x50555054); // == 'TPUP'

pub const MEMO_SATELLITE_CREATE_REFUND: Memo = Memo(0x44464552544153); // == 'SATREFD'

// 10 controllers max on the IC - the canister itself and user of the console because these two are not added to the mission control state controllers
pub const MAX_NUMBER_OF_MISSION_CONTROL_CONTROLLERS: usize = 8;

// 10 controllers max on the IC. User and mission control principals are copied in satellite state controllers
pub const MAX_NUMBER_OF_SATELLITE_CONTROLLERS: usize = 10;

0 comments on commit 2cce7e6

Please sign in to comment.