Skip to content

Commit

Permalink
feat: optimize cycle cost and refactor (#464)
Browse files Browse the repository at this point in the history
* optimize cycle cost and refactor

* use notify to recharge individual user canister

* refactor

* remove request cycles while adding post and use notify to inform bet participants

* added recharge canister method on every update call

* update cycle computation max amount

* update pocket ic

* remove metrics and fix test

* refactor and add cycles before tabulate hotornotbets

* fix test

* fix test
  • Loading branch information
ravi-sawlani-yral authored Nov 21, 2024
1 parent 54067ab commit 13fa185
Show file tree
Hide file tree
Showing 55 changed files with 583 additions and 360 deletions.
129 changes: 105 additions & 24 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ members = [
[workspace.dependencies]
candid = "0.10.2"
ciborium = "0.2.1"
pocket-ic = "3.0.0"
pocket-ic = "6.0.0"
ic-cdk = "0.15.1"
ic-cdk-timers = "0.7.0"
ic-cdk-macros = "0.16.0"
Expand Down
Binary file modified pocket-ic
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::{data_model::CanisterData, CANISTER_DATA};
use ic_cdk_macros::init;
use shared_utils::{
canister_specific::individual_user_template::types::arg::IndividualUserTemplateInitArgs,
common::timer::send_metrics::enqueue_timer_for_calling_metrics_rest_api,
};
use shared_utils::canister_specific::individual_user_template::types::arg::IndividualUserTemplateInitArgs;

#[init]
fn init(init_args: IndividualUserTemplateInitArgs) {
CANISTER_DATA.with(|canister_data_ref_cell| {
let mut data = canister_data_ref_cell.borrow_mut();
init_impl(init_args, &mut data);
});

send_canister_metrics();
}

fn init_impl(init_args: IndividualUserTemplateInitArgs, data: &mut CanisterData) {
Expand All @@ -33,26 +28,12 @@ fn init_impl(init_args: IndividualUserTemplateInitArgs, data: &mut CanisterData)
data.version_details.version = init_args.version;
}

pub fn send_canister_metrics() {
let url_to_send_canister_metrics_to = CANISTER_DATA.with(|canister_data_ref_cell| {
canister_data_ref_cell
.borrow()
.configuration
.url_to_send_canister_metrics_to
.clone()
});

if let Some(url_to_send_canister_metrics_to) = url_to_send_canister_metrics_to {
enqueue_timer_for_calling_metrics_rest_api(url_to_send_canister_metrics_to);
}
}

#[cfg(test)]
mod test {
use shared_utils::common::types::known_principal::{KnownPrincipalMap, KnownPrincipalType};
use test_utils::setup::test_constants::{
get_global_super_admin_principal_id,
get_mock_canister_id_user_index, get_mock_user_alice_principal_id,
get_global_super_admin_principal_id, get_mock_canister_id_user_index,
get_mock_user_alice_principal_id,
};

use super::*;
Expand All @@ -78,7 +59,7 @@ mod test {
url_to_send_canister_metrics_to: Some(
"http://metrics-url.com/receive-metrics".to_string(),
),
version: String::from("v1.0.0")
version: String::from("v1.0.0"),
};
let mut data = CanisterData::default();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::util::migration::{IndividualUser, Migration};
use crate::util::{
cycles::notify_to_recharge_canister,
migration::{IndividualUser, Migration},
subnet_orchestrator,
};
use candid::Principal;
use ic_cdk::caller;
use ic_cdk_macros::update;
Expand All @@ -11,6 +15,8 @@ pub async fn transfer_tokens_and_posts(
to_account: Principal,
to_account_canister_id: Principal,
) -> Result<(), MigrationErrors> {
notify_to_recharge_canister();

let caller = caller();
let user = IndividualUser::from_canister_data().await?;
let to_individual_user = IndividualUser::new(to_account_canister_id, to_account, None).await?;
Expand All @@ -24,6 +30,8 @@ pub async fn receive_data_from_hotornot(
amount: u64,
posts: Vec<Post>,
) -> Result<(), MigrationErrors> {
notify_to_recharge_canister();

let user = IndividualUser::from_canister_data().await?;

let from_individual_user = IndividualUser::new(caller(), from_account, None).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use ic_cdk::caller;
use ic_cdk_macros::update;
use shared_utils::common::utils::system_time::get_current_system_time_from_ic;

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

#[update]
fn update_last_access_time() -> Result<String, String> {
Expand All @@ -28,5 +31,5 @@ pub fn update_last_canister_functionality_access_time() {
CANISTER_DATA.with_borrow_mut(|canister_data| {
canister_data.last_canister_functionality_access_time =
Some(get_current_system_time_from_ic());
})
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use candid::Principal;
use ic_cdk::api::{self, is_controller};
use ic_cdk_macros::update;

use crate::CANISTER_DATA;
use crate::{util::cycles::notify_to_recharge_canister, CANISTER_DATA};

use super::update_last_access_time::update_last_canister_functionality_access_time;

#[update]
pub async fn update_profile_owner(user_id: Option<Principal>) -> Result<(), String> {
notify_to_recharge_canister();
if !is_controller(&api::caller()) {
return Err("Unauthorised".into());
}

update_last_canister_functionality_access_time();

CANISTER_DATA.with_borrow_mut(|canister_data| {
Expand Down
Loading

0 comments on commit 13fa185

Please sign in to comment.