Skip to content

Commit

Permalink
Introduce the EventRelay canister (#5334)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Feb 8, 2024
1 parent d2cd484 commit f384469
Show file tree
Hide file tree
Showing 40 changed files with 461 additions and 2 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ members = [
"backend/canisters/escrow/api",
"backend/canisters/escrow/c2c_client",
"backend/canisters/escrow/impl",
"backend/canisters/event_relay/api",
"backend/canisters/event_relay/impl",
"backend/canisters/group/api",
"backend/canisters/group/c2c_client",
"backend/canisters/group/client",
Expand Down
1 change: 1 addition & 0 deletions backend/canister_installer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ canister_agent_utils = { path = "../libraries/canister_agent_utils" }
clap = { workspace = true, features = ["derive"] }
cycles_dispenser_canister = { path = "../canisters/cycles_dispenser/api" }
escrow_canister = { path = "../canisters/escrow/api" }
event_relay_canister = { path = "../canisters/event_relay/api" }
futures = { workspace = true }
group_index_canister = { path = "../canisters/group_index/api" }
group_index_canister_client = { path = "../canisters/group_index/client" }
Expand Down
17 changes: 16 additions & 1 deletion backend/canister_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async fn install_service_canisters_impl(
set_controllers(management_canister, &canister_ids.neuron_controller, controllers.clone()),
set_controllers(management_canister, &canister_ids.escrow, controllers.clone()),
set_controllers(management_canister, &canister_ids.translations, controllers.clone()),
set_controllers(management_canister, &canister_ids.event_relay, controllers.clone()),
set_controllers(
management_canister,
&canister_ids.local_user_index,
Expand Down Expand Up @@ -220,6 +221,14 @@ async fn install_service_canisters_impl(
test_mode,
};

let event_relay_canister_wasm = get_canister_wasm(CanisterName::EventRelay, version);
let event_relay_init_args = event_relay_canister::init::Args {
push_events_whitelist: vec![canister_ids.user_index, canister_ids.online_users],
cycles_dispenser_canister_id: canister_ids.cycles_dispenser,
wasm_version: version,
test_mode,
};

futures::future::join5(
install_wasm(
management_canister,
Expand Down Expand Up @@ -288,7 +297,7 @@ async fn install_service_canisters_impl(
)
.await;

futures::future::join3(
futures::future::join4(
install_wasm(
management_canister,
&canister_ids.neuron_controller,
Expand All @@ -307,6 +316,12 @@ async fn install_service_canisters_impl(
&translations_canister_wasm.module,
translations_init_args,
),
install_wasm(
management_canister,
&canister_ids.event_relay,
&event_relay_canister_wasm.module,
event_relay_init_args,
),
)
.await;

Expand Down
4 changes: 4 additions & 0 deletions backend/canister_installer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async fn main() {
neuron_controller: opts.neuron_controller,
escrow: opts.escrow,
translations: opts.translations,
event_relay: opts.event_relay,
nns_root: opts.nns_root,
nns_governance: opts.nns_governance,
nns_internet_identity: opts.nns_internet_identity,
Expand Down Expand Up @@ -97,6 +98,9 @@ struct Opts {
#[arg(long)]
translations: CanisterId,

#[arg(long)]
event_relay: CanisterId,

#[arg(long)]
nns_root: CanisterId,

Expand Down
1 change: 1 addition & 0 deletions backend/canister_upgrader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ canister_agent_utils = { path = "../libraries/canister_agent_utils" }
clap = { workspace = true, features = ["derive"] }
cycles_dispenser_canister = { path = "../canisters/cycles_dispenser/api" }
escrow_canister = { path = "../canisters/escrow/api" }
event_relay_canister = { path = "../canisters/event_relay/api" }
group_index_canister = { path = "../canisters/group_index/api" }
group_index_canister_client = { path = "../canisters/group_index/client" }
ic-agent = { workspace = true }
Expand Down
19 changes: 19 additions & 0 deletions backend/canister_upgrader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ pub async fn upgrade_escrow_canister(
println!("Escrow canister upgraded");
}

pub async fn upgrade_event_relay_canister(
identity: Box<dyn Identity>,
url: String,
event_relay_canister_id: CanisterId,
version: BuildVersion,
) {
upgrade_top_level_canister(
identity,
url,
event_relay_canister_id,
version,
event_relay_canister::post_upgrade::Args { wasm_version: version },
CanisterName::EventRelay,
)
.await;

println!("Event relay canister upgraded");
}

pub async fn upgrade_local_group_index_canister(
identity: Box<dyn Identity>,
url: String,
Expand Down
4 changes: 4 additions & 0 deletions backend/canister_upgrader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async fn main() {
upgrade_cycles_dispenser_canister(identity, opts.url, opts.cycles_dispenser, opts.version).await
}
CanisterName::Escrow => upgrade_escrow_canister(identity, opts.url, opts.escrow, opts.version).await,
CanisterName::EventRelay => upgrade_event_relay_canister(identity, opts.url, opts.event_relay, opts.version).await,
CanisterName::Group => upgrade_group_canister(identity, opts.url, opts.group_index, opts.version).await,
CanisterName::LocalGroupIndex => {
upgrade_local_group_index_canister(identity, opts.url, opts.group_index, opts.version).await
Expand Down Expand Up @@ -98,6 +99,9 @@ struct Opts {
#[arg(long)]
escrow: CanisterId,

#[arg(long)]
event_relay: CanisterId,

#[arg(long)]
canister_to_upgrade: CanisterName,

Expand Down
12 changes: 12 additions & 0 deletions backend/canisters/event_relay/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "event_relay_canister"
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 }
candid_gen = { path = "../../../libraries/candid_gen" }
serde = { workspace = true }
types = { path = "../../../libraries/types" }
2 changes: 2 additions & 0 deletions backend/canisters/event_relay/api/can.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service : {
};
9 changes: 9 additions & 0 deletions backend/canisters/event_relay/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(unused_imports)]

mod lifecycle;
mod queries;
mod updates;

pub use lifecycle::*;
pub use queries::*;
pub use updates::*;
11 changes: 11 additions & 0 deletions backend/canisters/event_relay/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use types::{BuildVersion, CanisterId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub push_events_whitelist: Vec<Principal>,
pub cycles_dispenser_canister_id: CanisterId,
pub wasm_version: BuildVersion,
pub test_mode: bool,
}
2 changes: 2 additions & 0 deletions backend/canisters/event_relay/api/src/lifecycle/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod init;
pub mod post_upgrade;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::BuildVersion;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub wasm_version: BuildVersion,
}
4 changes: 4 additions & 0 deletions backend/canisters/event_relay/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
candid::export_service!();
std::print!("{}", __export_service());
}
1 change: 1 addition & 0 deletions backend/canisters/event_relay/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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
@@ -0,0 +1 @@

29 changes: 29 additions & 0 deletions backend/canisters/event_relay/impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "event_relay_canister_impl"
version = "0.1.0"
edition = "2021"

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

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
candid = { workspace = true }
canister_logger = { path = "../../../libraries/canister_logger" }
canister_state_macros = { path = "../../../libraries/canister_state_macros" }
canister_tracing_macros = { path = "../../../libraries/canister_tracing_macros" }
event_relay_canister = { path = "../api" }
http_request = { path = "../../../libraries/http_request" }
ic-cdk = { workspace = true }
ic-cdk-macros = { workspace = true }
ic-cdk-timers = { workspace = true }
ic-stable-structures = { workspace = true }
rand = { workspace = true }
serde = { workspace = true }
serializer = { path = "../../../libraries/serializer" }
stable_memory = { path = "../../../libraries/stable_memory" }
tracing = { workspace = true }
types = { path = "../../../libraries/types" }
utils = { path = "../../../libraries/utils" }
79 changes: 79 additions & 0 deletions backend/canisters/event_relay/impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use candid::Principal;
use canister_state_macros::canister_state;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::HashSet;
use types::{BuildVersion, CanisterId, Cycles, TimestampMillis, Timestamped};
use utils::env::Environment;

mod lifecycle;
mod memory;
mod model;
mod queries;
mod updates;

thread_local! {
static WASM_VERSION: RefCell<Timestamped<BuildVersion>> = RefCell::default();
}

canister_state!(RuntimeState);

struct RuntimeState {
pub env: Box<dyn Environment>,
pub data: Data,
}

impl RuntimeState {
pub fn new(env: Box<dyn Environment>, data: Data) -> RuntimeState {
RuntimeState { env, data }
}

pub fn metrics(&self) -> Metrics {
Metrics {
memory_used: utils::memory::used(),
now: self.env.now(),
cycles_balance: self.env.cycles_balance(),
wasm_version: WASM_VERSION.with_borrow(|v| **v),
git_commit_id: utils::git::git_commit_id().to_string(),
push_events_whitelist: self.data.push_events_whitelist.iter().copied().collect(),
canister_ids: CanisterIds {
cycles_dispenser: self.data.cycles_dispenser_canister_id,
},
}
}
}

#[derive(Serialize, Deserialize)]
struct Data {
pub push_events_whitelist: HashSet<Principal>,
pub cycles_dispenser_canister_id: CanisterId,
pub rng_seed: [u8; 32],
pub test_mode: bool,
}

impl Data {
pub fn new(push_events_whitelist: HashSet<Principal>, cycles_dispenser_canister_id: CanisterId, test_mode: bool) -> Data {
Data {
push_events_whitelist,
cycles_dispenser_canister_id,
rng_seed: [0; 32],
test_mode,
}
}
}

#[derive(Serialize, Debug)]
pub struct Metrics {
pub now: TimestampMillis,
pub memory_used: u64,
pub cycles_balance: Cycles,
pub wasm_version: BuildVersion,
pub git_commit_id: String,
pub push_events_whitelist: Vec<Principal>,
pub canister_ids: CanisterIds,
}

#[derive(Serialize, Debug)]
pub struct CanisterIds {
pub cycles_dispenser: CanisterId,
}
25 changes: 25 additions & 0 deletions backend/canisters/event_relay/impl/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::lifecycle::{init_env, init_state};
use crate::Data;
use canister_tracing_macros::trace;
use event_relay_canister::init::Args;
use ic_cdk_macros::init;
use tracing::info;
use utils::cycles::init_cycles_dispenser_client;

#[init]
#[trace]
fn init(args: Args) {
canister_logger::init(args.test_mode);
init_cycles_dispenser_client(args.cycles_dispenser_canister_id, args.test_mode);

let env = init_env([0; 32]);
let data = Data::new(
args.push_events_whitelist.into_iter().collect(),
args.cycles_dispenser_canister_id,
args.test_mode,
);

init_state(env, data, args.wasm_version);

info!(version = %args.wasm_version, "Initialization complete");
}
Loading

0 comments on commit f384469

Please sign in to comment.