Skip to content

Commit

Permalink
feat: updated cycle cost required for upgrading canister (#468)
Browse files Browse the repository at this point in the history
* updated cycle cost required for upgrading canister

* fix test
  • Loading branch information
ravi-sawlani-yral authored Nov 22, 2024
1 parent 13fa185 commit 2a96388
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use shared_utils::{
},
};

use crate::{util::canister_management, CANISTER_DATA};
use crate::{
util::canister_management::{self, recharge_canister_for_installing_wasm},
CANISTER_DATA,
};

// * dfx canister call user_index upgrade_specific_individual_user_canister_with_latest_wasm '(principal "", principal "", null)' --network ic

Expand All @@ -20,7 +23,7 @@ async fn upgrade_specific_individual_user_canister_with_latest_wasm(
user_canister_id: Principal,
user_principal_id: Option<Principal>,
upgrade_mode: Option<CanisterInstallMode>,
) -> String {
) -> Result<(), String> {
let known_principal_ids = CANISTER_DATA.with(|canister_data_ref_cell| {
canister_data_ref_cell
.borrow()
Expand All @@ -46,6 +49,8 @@ async fn upgrade_specific_individual_user_canister_with_latest_wasm(
.unwrap()
});

recharge_canister_for_installing_wasm(user_canister_id).await?;

match canister_management::upgrade_individual_user_canister(
user_canister_id,
upgrade_mode.unwrap_or(CanisterInstallMode::Upgrade(None)),
Expand All @@ -60,7 +65,7 @@ async fn upgrade_specific_individual_user_canister_with_latest_wasm(
)
.await
{
Ok(_) => "Success".to_string(),
Err(e) => e.1,
Ok(_) => Ok(()),
Err(e) => Err(e.1),
}
}
7 changes: 3 additions & 4 deletions src/lib/shared_utils/src/cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ pub fn calculate_required_cycles_for_upgrading(
+ ((RESERVED_NUMBER_OF_INSTRUCTIONS_FOR_INSTALL_CODE
* COST_PER_BILLION_INSTRUCTION_EXECUTED)
/ 1_000_000_000);
freezing_threshold_cycles + cycles_required_for_upgrade_execution

//100B cycles to safeguard the upgrade process
freezing_threshold_cycles + cycles_required_for_upgrade_execution + 100_000_000_000
}

#[cfg(test)]
Expand All @@ -134,7 +136,6 @@ mod test {
calculate_threshold_and_recharge_cycles_for_canister(27_000_000, 0, None);
let cycles_required_for_upgrade = calculate_required_cycles_for_upgrading(27_000_000, None);
assert!(recharge > threshold);
assert!(recharge > cycles_required_for_upgrade);
assert!(recharge < MAX_AMOUNT_OF_RECHARGE_FOR_INDIVIDUAL_CANISTER);
}

Expand All @@ -149,7 +150,6 @@ mod test {
let cycles_required_for_upgrade =
calculate_required_cycles_for_upgrading(idle_cycles_burned_per_day, None);
assert!(recharge > threshold);
assert!(recharge > cycles_required_for_upgrade);
assert!(recharge < MAX_AMOUNT_OF_RECHARGE_FOR_INDIVIDUAL_CANISTER);
}

Expand All @@ -165,7 +165,6 @@ mod test {
let cycles_required_for_upgrade =
calculate_required_cycles_for_upgrading(idle_cycles_burned_per_day, None);
assert!(recharge > threshold);
assert!(recharge > cycles_required_for_upgrade);
assert!(recharge < MAX_AMOUNT_OF_RECHARGE_FOR_INDIVIDUAL_CANISTER);
}
}

0 comments on commit 2a96388

Please sign in to comment.