Skip to content

Commit

Permalink
Implement ability to expand onto additional subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Dec 17, 2024
1 parent bcada18 commit bcbd3ed
Show file tree
Hide file tree
Showing 77 changed files with 1,058 additions and 163 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ members = [
"backend/canisters/notifications/client",
"backend/canisters/notifications/impl",
"backend/canisters/notifications_index/api",
"backend/canisters/notifications_index/c2c_client",
"backend/canisters/notifications_index/client",
"backend/canisters/notifications_index/impl",
"backend/canisters/online_users/api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use types::{BuildVersion, CanisterId, Cycles, Milliseconds};
pub struct Args {
pub governance_principals: Vec<Principal>,
pub canisters: Vec<CanisterId>,
pub registry_canister_id: CanisterId,
pub max_top_up_amount: Cycles,
pub min_interval: Milliseconds,
pub min_cycles_balance: Cycles,
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/cycles_dispenser/c2c_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use cycles_dispenser_canister::*;
// Queries

// Updates
generate_candid_c2c_call!(add_canister);
generate_candid_c2c_call!(c2c_request_cycles);
1 change: 1 addition & 0 deletions backend/canisters/cycles_dispenser/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ canister_client = { path = "../../../libraries/canister_client" }
canister_logger = { path = "../../../libraries/canister_logger" }
canister_state_macros = { path = "../../../libraries/canister_state_macros" }
canister_tracing_macros = { path = "../../../libraries/canister_tracing_macros" }
constants = { path = "../../../libraries/constants" }
cycles_dispenser_canister = { path = "../api" }
cycles_minting_canister = { path = "../../../external_canisters/cmc/api" }
cycles_minting_canister_c2c_client = { path = "../../../external_canisters/cmc/c2c_client" }
Expand Down
6 changes: 3 additions & 3 deletions backend/canisters/cycles_dispenser/impl/src/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub fn caller_is_governance_principal() -> Result<(), String> {
}
}

pub fn caller_is_authorized_to_add_canister() -> Result<(), String> {
if read_state(|state| state.is_caller_authorized_to_add_canister()) {
pub fn caller_is_registry_canister() -> Result<(), String> {
if read_state(|state| state.is_caller_registry_canister()) {
Ok(())
} else {
Err("Caller is not authorized to add a canister".to_string())
Err("Caller is not the Registry canister".to_string())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{mutate_state, State};
use constants::NANOS_PER_MILLISECOND;
use ic_ledger_types::{AccountIdentifier, BlockIndex, Memo, Subaccount, Timestamp, Tokens, TransferArgs};
use std::time::Duration;
use tracing::{error, info};
Expand Down Expand Up @@ -77,7 +78,7 @@ async fn burn_icp(burn_details: BurnIcpDetails) {
from_subaccount: None,
to: AccountIdentifier::new(&burn_details.cmc, &Subaccount::from(burn_details.this_canister_id)),
created_at_time: Some(Timestamp {
timestamp_nanos: burn_details.now * 1_000_000,
timestamp_nanos: burn_details.now * NANOS_PER_MILLISECOND,
}),
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async fn run_async(canister_id: CanisterId) {
mutate_state(|state| {
let now = state.env.now();
for canister_id in canisters.iter().filter_map(|s| s.canister_id) {
state.data.canisters_directly_controlled_by_sns_root.insert(canister_id);
state.data.canisters.add(canister_id, now);
}
});
Expand Down
32 changes: 20 additions & 12 deletions backend/canisters/cycles_dispenser/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ic_ledger_types::{BlockIndex, Tokens};
use ledger_utils::default_ledger_account;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::collections::{BTreeMap, HashSet};
use types::{BuildVersion, CanisterId, Cycles, Milliseconds, TimestampMillis, Timestamped};
use utils::env::Environment;

Expand Down Expand Up @@ -37,10 +37,8 @@ impl State {
self.data.governance_principals.contains(&self.env.caller())
}

pub fn is_caller_authorized_to_add_canister(&self) -> bool {
let caller = self.env.caller();
self.data.governance_principals.contains(&caller)
|| self.data.canisters_directly_controlled_by_sns_root.contains(&caller)
pub fn is_caller_registry_canister(&self) -> bool {
self.env.caller() == self.data.registry_canister_id
}

pub fn metrics(&self) -> Metrics {
Expand All @@ -60,8 +58,11 @@ impl State {
min_cycles_balance: self.data.min_cycles_balance,
icp_burn_amount: self.data.icp_burn_amount,
stable_memory_sizes: memory::memory_sizes(),
ledger_canister: self.data.ledger_canister,
cycles_minting_canister: self.data.cycles_minting_canister,
canister_ids: CanisterIds {
registry: self.data.registry_canister_id,
ledger: self.data.ledger_canister,
cmc: self.data.cycles_minting_canister,
},
}
}
}
Expand All @@ -70,8 +71,8 @@ impl State {
struct Data {
pub governance_principals: HashSet<Principal>,
pub canisters: Canisters,
#[serde(default)]
pub canisters_directly_controlled_by_sns_root: BTreeSet<CanisterId>,
#[serde(default = "CanisterId::anonymous")]
pub registry_canister_id: CanisterId,
pub sns_root_canister: Option<CanisterId>,
pub max_top_up_amount: Cycles,
pub min_interval: Milliseconds,
Expand All @@ -89,6 +90,7 @@ impl Data {
pub fn new(
governance_principals: Vec<Principal>,
canisters: Vec<CanisterId>,
registry_canister_id: CanisterId,
max_top_up_amount: Cycles,
min_interval: Milliseconds,
min_cycles_balance: Cycles,
Expand All @@ -101,7 +103,7 @@ impl Data {
Data {
governance_principals: governance_principals.into_iter().collect(),
canisters: Canisters::new(canisters, now),
canisters_directly_controlled_by_sns_root: BTreeSet::default(),
registry_canister_id,
sns_root_canister: None,
max_top_up_amount,
min_interval,
Expand Down Expand Up @@ -133,6 +135,12 @@ pub struct Metrics {
pub min_cycles_balance: Cycles,
pub icp_burn_amount: Tokens,
pub stable_memory_sizes: BTreeMap<u8, u64>,
pub ledger_canister: CanisterId,
pub cycles_minting_canister: CanisterId,
pub canister_ids: CanisterIds,
}

#[derive(CandidType, Serialize, Debug)]
pub struct CanisterIds {
registry: CanisterId,
ledger: CanisterId,
cmc: CanisterId,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn init(args: Args) {
let data = Data::new(
args.governance_principals,
args.canisters,
args.registry_canister_id,
args.max_top_up_amount,
args.min_interval,
args.min_cycles_balance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ use cycles_dispenser_canister::post_upgrade::Args;
use ic_cdk::post_upgrade;
use stable_memory::get_reader;
use tracing::info;
use types::CanisterId;

#[post_upgrade]
#[trace]
fn post_upgrade(args: Args) {
let memory = get_upgrades_memory();
let reader = get_reader(&memory);

let (data, errors, logs, traces): (Data, Vec<LogEntry>, Vec<LogEntry>, Vec<LogEntry>) =
let (mut data, errors, logs, traces): (Data, Vec<LogEntry>, Vec<LogEntry>, Vec<LogEntry>) =
msgpack::deserialize(reader).unwrap();

if data.test_mode {
data.registry_canister_id = CanisterId::from_text("cglwi-oaaaa-aaaar-aqw4q-cai").unwrap();
} else {
data.registry_canister_id = CanisterId::from_text("cpi5u-yiaaa-aaaar-aqw5a-cai").unwrap();
}

canister_logger::init_with_logs(data.test_mode, errors, logs, traces);

let env = init_env(data.rng_seed);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::guards::caller_is_authorized_to_add_canister;
use crate::guards::caller_is_registry_canister;
use crate::{mutate_state, State};
use canister_api_macros::proposal;
use canister_tracing_macros::trace;
use cycles_dispenser_canister::add_canister::{Response::*, *};

#[proposal(guard = "caller_is_authorized_to_add_canister")]
#[proposal(guard = "caller_is_registry_canister")]
#[trace]
fn add_canister(args: Args) -> Response {
mutate_state(|state| add_canister_impl(args, state))
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/event_relay/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Args {
pub push_events_whitelist: Vec<Principal>,
pub event_store_canister_id: CanisterId,
pub cycles_dispenser_canister_id: CanisterId,
pub registry_canister_id: CanisterId,
pub chat_ledger_canister_id: CanisterId,
pub chat_governance_canister_id: CanisterId,
pub wasm_version: BuildVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub principals: Vec<Principal>,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success,
}
1 change: 1 addition & 0 deletions backend/canisters/event_relay/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod authorize_principals;
pub mod push_events;
13 changes: 13 additions & 0 deletions backend/canisters/event_relay/c2c_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "event_relay_canister_c2c_client"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
candid = { workspace = true }
canister_client = { path = "../../../libraries/canister_client" }
event_relay_canister = { path = "../api" }
ic-cdk = { workspace = true }
types = { path = "../../../libraries/types" }
7 changes: 7 additions & 0 deletions backend/canisters/event_relay/c2c_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use canister_client::generate_candid_c2c_call;
use event_relay_canister::*;

// Queries

// Updates
generate_candid_c2c_call!(authorize_principals);
8 changes: 8 additions & 0 deletions backend/canisters/event_relay/impl/src/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ pub fn caller_can_push_events() -> Result<(), String> {
Err("Caller is not whitelisted to push events".to_string())
}
}

pub fn caller_is_registry_canister() -> Result<(), String> {
if read_state(|state| state.is_caller_registry_canister()) {
Ok(())
} else {
Err("Caller is not the Registry canister".to_string())
}
}
10 changes: 10 additions & 0 deletions backend/canisters/event_relay/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl RuntimeState {
self.data.push_events_whitelist.contains(&caller)
}

pub fn is_caller_registry_canister(&self) -> bool {
self.env.caller() == self.data.registry_canister_id
}

pub fn metrics(&self) -> Metrics {
let event_store_client_info = self.data.event_store_client.info();
let event_store_canister_id = event_store_client_info.event_store_canister_id;
Expand All @@ -58,6 +62,7 @@ impl RuntimeState {
canister_ids: CanisterIds {
event_sink: event_store_canister_id,
cycles_dispenser: self.data.cycles_dispenser_canister_id,
registry: self.data.registry_canister_id,
chat_ledger: self.data.chat_ledger_canister_id,
chat_governance: self.data.chat_governance_canister_id,
},
Expand All @@ -71,6 +76,8 @@ struct Data {
pub event_store_client: EventStoreClient<CdkRuntime>,
pub event_deduper: EventDeduper,
pub cycles_dispenser_canister_id: CanisterId,
#[serde(default = "CanisterId::anonymous")]
pub registry_canister_id: CanisterId,
pub chat_ledger_canister_id: CanisterId,
pub chat_governance_canister_id: CanisterId,
pub chat_treasury_subaccount: [u8; 32],
Expand All @@ -84,6 +91,7 @@ impl Data {
push_events_whitelist: HashSet<Principal>,
event_store_canister_id: CanisterId,
cycles_dispenser_canister_id: CanisterId,
registry_canister_id: CanisterId,
chat_ledger_canister_id: CanisterId,
chat_governance_canister_id: CanisterId,
test_mode: bool,
Expand All @@ -95,6 +103,7 @@ impl Data {
.build(),
event_deduper: EventDeduper::default(),
cycles_dispenser_canister_id,
registry_canister_id,
chat_ledger_canister_id,
chat_governance_canister_id,
chat_treasury_subaccount: compute_distribution_subaccount_bytes(chat_governance_canister_id, 0),
Expand Down Expand Up @@ -131,6 +140,7 @@ pub struct Metrics {
pub struct CanisterIds {
pub event_sink: CanisterId,
pub cycles_dispenser: CanisterId,
pub registry: CanisterId,
pub chat_ledger: CanisterId,
pub chat_governance: CanisterId,
}
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/event_relay/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn init(args: Args) {
args.push_events_whitelist.into_iter().collect(),
args.event_store_canister_id,
args.cycles_dispenser_canister_id,
args.registry_canister_id,
args.chat_ledger_canister_id,
args.chat_governance_canister_id,
args.test_mode,
Expand Down
Loading

0 comments on commit bcbd3ed

Please sign in to comment.