Skip to content

Commit

Permalink
feat: add creator dao stats to platform orchestrator (#462)
Browse files Browse the repository at this point in the history
* added creator dao stats to platform orchestrator

* collect existing creator dao stats and added test

* fix refactor issue
  • Loading branch information
ravi-sawlani-yral authored Nov 14, 2024
1 parent 091d6c3 commit eac8b32
Show file tree
Hide file tree
Showing 23 changed files with 350 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/canister/individual_user_template/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ service : (IndividualUserTemplateInitArgs) -> {
request_airdrop : (principal, opt blob, nat, principal) -> (Result_21);
return_cycles_to_user_index_canister : (opt nat) -> ();
save_snapshot_json : () -> (nat32);
send_creator_dao_stats_to_subnet_orchestrator : () -> ();
set_controller_as_subnet_orchestrator : (principal) -> ();
settle_neurons_fund_participation : (
SettleNeuronsFundParticipationRequest,
Expand Down
8 changes: 8 additions & 0 deletions src/canister/individual_user_template/src/api/cdao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use crate::{
CANISTER_DATA,
};

pub mod send_creator_dao_stats_to_subnet_orchestrator;
pub mod upgrade_creator_dao_governance_canisters;

#[update]
Expand Down Expand Up @@ -304,5 +305,12 @@ async fn deploy_cdao_sns(
cdata.token_roots.insert(root.0, ());
});

let send_creator_dao_stats_res =
subnet_orchestrator.send_creator_dao_stats(vec![root.into()].into_iter().collect());

if let Err(e) = send_creator_dao_stats_res {
ic_cdk::println!("Error sending creator stats to subnet orchestrator {}", e)
}

Ok(deployed_cans)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::collections::HashSet;

use candid::Principal;
use ic_cdk_macros::update;
use shared_utils::common::utils::permissions::is_caller_controller;

use crate::{util::subnet_orchestrator::SubnetOrchestrator, CANISTER_DATA};

#[update(guard = "is_caller_controller")]
fn send_creator_dao_stats_to_subnet_orchestrator() {
let root_canisters: HashSet<Principal> = CANISTER_DATA.with_borrow(|canister_data| {
canister_data
.cdao_canisters
.iter()
.map(|deployed_canisters| deployed_canisters.root)
.collect()
});

let subnet_orchestrator = SubnetOrchestrator::new();

if let Ok(subnet_orchestrator) = subnet_orchestrator {
match subnet_orchestrator.send_creator_dao_stats(root_canisters) {
Ok(()) => {}
Err(e) => {
ic_cdk::println!("Error sending creator dao stats to subnet orchestrator {e}")
}
}
} else {
ic_cdk::println!("Subnet Orchestrator canister id not found");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashSet;

use candid::Principal;
use ic_cdk::notify;
use shared_utils::common::types::known_principal::KnownPrincipalType;

use crate::CANISTER_DATA;
Expand Down Expand Up @@ -28,4 +31,18 @@ impl SubnetOrchestrator {

result
}

pub fn send_creator_dao_stats(&self, root_canisters: HashSet<Principal>) -> Result<(), String> {
notify(
self.canister_id,
"receive_creator_dao_stats_from_individual_canister",
(root_canisters,),
)
.map_err(|e| {
format!(
"error sending canister stats to subnet orchestrator {:?}",
e
)
})
}
}
17 changes: 17 additions & 0 deletions src/canister/platform_orchestrator/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ type CanisterUpgradeStatus = record {
count : nat64;
upgrade_arg : UpgradeCanisterArg;
};
type CreatorDaoTokenStats = record {
total_number_of_creator_dao_tokens : nat64;
creator_dao_token_sns_canisters : vec record {
principal;
IndividualUserCreatorDaoEntry;
};
};
type HttpRequest = record {
url : text;
method : text;
Expand All @@ -14,6 +21,10 @@ type HttpResponse = record {
headers : vec record { text; text };
status_code : nat16;
};
type IndividualUserCreatorDaoEntry = record {
deployed_canisters : vec principal;
individual_profile_id : principal;
};
type KnownPrincipalType = variant {
CanisterIdUserIndex;
CanisterIdPlatformOrchestrator;
Expand Down Expand Up @@ -71,11 +82,13 @@ type WasmType = variant {
};
service : (PlatformOrchestratorInitArgs) -> {
add_principal_as_global_admin : (principal) -> ();
collect_creator_dao_stats_in_the_network : () -> ();
deposit_cycles_to_canister : (principal, nat) -> (Result);
deregister_subnet_orchestrator : (principal, bool) -> ();
get_all_available_subnet_orchestrators : () -> (vec principal) query;
get_all_global_admins : () -> (vec principal) query;
get_all_subnet_orchestrators : () -> (vec principal) query;
get_creator_dao_stats : () -> (CreatorDaoTokenStats) query;
get_global_known_principal : (KnownPrincipalType) -> (principal) query;
get_subnet_known_principal : (principal, KnownPrincipalType) -> (
principal,
Expand All @@ -97,6 +110,10 @@ service : (PlatformOrchestratorInitArgs) -> {
populate_known_principal_for_all_subnet : () -> ();
provision_empty_canisters_in_a_subnet : (principal, nat64) -> (Result_1);
provision_subnet_orchestrator_canister : (principal) -> (Result_2);
receive_creator_dao_stats_from_subnet_orchestrator : (
principal,
vec principal,
) -> (Result_1);
recharge_subnet_orchestrator : () -> (Result_1);
register_new_subnet_orchestrator : (principal, bool) -> (Result_1);
reinstall_yral_post_cache_canister : () -> ();
Expand Down
1 change: 1 addition & 0 deletions src/canister/platform_orchestrator/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod canister_management;
pub mod cycle_management;
pub mod generic_proposal;
pub mod monitoring;
pub mod stats;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ic_cdk::notify;
use ic_cdk_macros::update;

use crate::{guard::is_caller::is_caller_global_admin_or_controller, CANISTER_DATA};

#[update(guard = "is_caller_global_admin_or_controller")]
pub fn collect_creator_dao_stats_in_the_network() {
CANISTER_DATA.with_borrow(|canister_data| {
canister_data
.all_subnet_orchestrator_canisters_list
.iter()
.for_each(|subnet_orchestrator_canister_id| {
match notify(
*subnet_orchestrator_canister_id,
"collect_creator_dao_stats_in_the_network",
(),
) {
Err(e) => {
ic_cdk::println!(
"Failed to collect data from {:?}. Error {:?}",
subnet_orchestrator_canister_id,
e
)
}
_ => {}
}
});
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use ic_cdk_macros::query;
use shared_utils::types::creator_dao_stats::CreatorDaoTokenStats;

use crate::{guard::is_caller::is_caller_global_admin_or_controller, CANISTER_DATA};

#[query(guard = "is_caller_global_admin_or_controller")]
pub fn get_creator_dao_stats() -> CreatorDaoTokenStats {
CANISTER_DATA.with_borrow(|canister_data| canister_data.creator_dao_stats.clone())
}
3 changes: 3 additions & 0 deletions src/canister/platform_orchestrator/src/api/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod collect_creator_dao_stats_in_the_network;
pub mod get_creator_dao_stats;
pub mod receive_creator_dao_stats_from_subnet_orchestrator;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use ic_cdk_macros::update;
use std::collections::HashSet;

use candid::Principal;
use ic_cdk::caller;

use crate::{utils::registered_subnet_orchestrator::RegisteredSubnetOrchestrator, CANISTER_DATA};

#[update]
pub fn receive_creator_dao_stats_from_subnet_orchestrator(
individual_user_profile_id: Principal,
root_canister_ids: HashSet<Principal>,
) -> Result<(), String> {
let _registered_subnet_orchestrator = RegisteredSubnetOrchestrator::new(caller())?;
CANISTER_DATA.with_borrow_mut(|canister_data| {
canister_data.add_creator_dao_stats_recieved_from_subnet_orchestrator(
individual_user_profile_id,
root_canister_ids,
);
});

Ok(())
}
28 changes: 24 additions & 4 deletions src/canister/platform_orchestrator/src/data_model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ use ciborium::de;
use ic_stable_structures::{storable::Bound, StableBTreeMap, StableLog, Storable};
use std::{
borrow::Cow,
collections::HashSet,
collections::{HashMap, HashSet},
time::{SystemTime, UNIX_EPOCH},
};

use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use shared_utils::{
canister_specific::platform_orchestrator::types::{
args::UpgradeCanisterArg, well_known_principal::PlatformOrchestratorKnownPrincipal,
SubnetUpgradeReport,
canister_specific::{
individual_user_template::types::{cdao::DeployedCdaoCanisters, session::SessionType},
platform_orchestrator::types::{
args::UpgradeCanisterArg, well_known_principal::PlatformOrchestratorKnownPrincipal,
SubnetUpgradeReport,
},
},
common::types::wasm::{CanisterWasm, WasmType},
types::creator_dao_stats::CreatorDaoTokenStats,
};

use self::memory::{
Expand Down Expand Up @@ -47,6 +51,8 @@ pub struct CanisterData {
pub subnets_upgrade_report: SubnetUpgradeReport,
#[serde(default)]
pub state_guard: StateGuard,
#[serde(default)]
pub creator_dao_stats: CreatorDaoTokenStats,
}

fn _default_wasms() -> StableBTreeMap<WasmType, CanisterWasm, Memory> {
Expand Down Expand Up @@ -75,6 +81,7 @@ impl Default for CanisterData {
platform_global_admins: Default::default(),
subnets_upgrade_report: SubnetUpgradeReport::default(),
state_guard: StateGuard::default(),
creator_dao_stats: CreatorDaoTokenStats::default(),
}
}
}
Expand Down Expand Up @@ -131,3 +138,16 @@ impl Default for CanisterUpgradeStatus {
}
}
}

impl CanisterData {
pub fn add_creator_dao_stats_recieved_from_subnet_orchestrator(
&mut self,
individual_user_profile_id: Principal,
root_canister_ids: HashSet<Principal>,
) {
root_canister_ids.iter().for_each(|root_canister_id| {
self.creator_dao_stats
.insert_new_entry(individual_user_profile_id, *root_canister_id);
});
}
}
3 changes: 3 additions & 0 deletions src/canister/platform_orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use candid::Principal;
use std::cell::RefCell;

use std::collections::HashSet;

use crate::api::generic_proposal::{
PlatformOrchestratorGenericArgumentType, PlatformOrchestratorGenericResultType,
};
Expand All @@ -16,6 +18,7 @@ use shared_utils::{
common::types::http::{HttpRequest, HttpResponse},
common::types::known_principal::KnownPrincipalType,
common::types::wasm::WasmType,
types::creator_dao_stats::CreatorDaoTokenStats,
};

mod api;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use candid::{Nat, Principal};
use ic_cdk::{
api::management_canister::main::{
Expand Down
4 changes: 4 additions & 0 deletions src/canister/user_index/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type UserIndexInitArgs = record {
service : (UserIndexInitArgs) -> {
allot_empty_canister : () -> (Result);
are_signups_enabled : () -> (bool) query;
collect_creator_dao_stats_in_the_network : () -> ();
create_pool_of_individual_user_available_canisters : (text, blob) -> (
Result_1,
);
Expand Down Expand Up @@ -163,6 +164,9 @@ service : (UserIndexInitArgs) -> {
blob,
) -> (Result_3);
provision_empty_canisters : (nat64) -> ();
receive_creator_dao_stats_from_individual_canister : (vec principal) -> (
Result_3,
);
recharge_individual_user_canister : () -> (Result_3);
reclaim_cycles_from_individual_canisters : () -> ();
request_cycles : (nat) -> (Result_3);
Expand Down
1 change: 1 addition & 0 deletions src/canister/user_index/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod canister_management;
pub mod cycle_management;
pub mod http;
pub mod monitoring;
pub mod stats;
pub mod upgrade_individual_user_template;
pub mod user_record;
pub mod user_signup;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ic_cdk::notify;
use ic_cdk_macros::update;
use shared_utils::common::utils::permissions::is_caller_controller;

use crate::CANISTER_DATA;

#[update(guard = "is_caller_controller")]
pub fn collect_creator_dao_stats_in_the_network() {
CANISTER_DATA.with_borrow(|canister_data| {
canister_data
.user_principal_id_to_canister_id_map
.iter()
.for_each(|(_, user_canister)| {
match notify(
*user_canister,
"send_creator_dao_stats_to_subnet_orchestrator",
(),
) {
Err(e) => {
ic_cdk::println!(
"Failed to collect data from {:?}. Error {:?}",
user_canister,
e
)
}
_ => {}
}
});
})
}
2 changes: 2 additions & 0 deletions src/canister/user_index/src/api/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod collect_creator_dao_stats_in_the_network;
pub mod receive_creator_dao_stats_from_individual_canister;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::collections::HashSet;

use ic_cdk::{caller, notify};
use ic_cdk_macros::update;

use candid::Principal;
use shared_utils::common::types::known_principal::KnownPrincipalType;

use crate::{util::types::individual_user_canister::IndividualUserCanister, CANISTER_DATA};

#[update]
pub fn receive_creator_dao_stats_from_individual_canister(
root_canister_ids: HashSet<Principal>,
) -> Result<(), String> {
let individual_user = IndividualUserCanister::new(caller())?;

let platform_orchestrator_canister_id = CANISTER_DATA.with_borrow(|canister_data| {
canister_data
.configuration
.known_principal_ids
.get(&KnownPrincipalType::CanisterIdPlatformOrchestrator)
.copied()
});

let platform_orchestrator_canister_id =
platform_orchestrator_canister_id.ok_or("Platform Orchestrator Canister Id not found")?;

notify(
platform_orchestrator_canister_id,
"receive_creator_dao_stats_from_subnet_orchestrator",
(individual_user.profile_id, root_canister_ids),
)
.map_err(|e| format!("failed to notify platform orchestrator {:?}", e))
}
1 change: 1 addition & 0 deletions src/canister/user_index/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::collections::HashSet;

use candid::Principal;
use data_model::CanisterData;
Expand Down
Loading

0 comments on commit eac8b32

Please sign in to comment.