Skip to content

Commit

Permalink
Merge branch 'ulan/run-753-1' into 'master'
Browse files Browse the repository at this point in the history
RUN-753: Add the reserved balance to canister status result

This MR add the reserved balance of a canister to the
`CanisterStatusResultV2` type of the IC management canister.

The name of the new field is `reserved_cycles` to be
consistent with the existing `cycles` field. 

See merge request dfinity-lab/public/ic!14601
  • Loading branch information
ulan committed Sep 12, 2023
2 parents 0133e0b + cfefba9 commit 31fd739
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions rs/execution_environment/src/canister_manager.rs
Expand Up @@ -1002,6 +1002,7 @@ impl CanisterManager {
subnet_size,
)
.get(),
canister.system_state.reserved_balance().get(),
))
}

Expand Down
43 changes: 40 additions & 3 deletions rs/execution_environment/src/canister_manager/tests.rs
Expand Up @@ -21,9 +21,9 @@ use ic_cycles_account_manager::{CyclesAccountManager, ResourceSaturation};
use ic_error_types::{ErrorCode, UserError};
use ic_ic00_types::{
CanisterChange, CanisterChangeDetails, CanisterChangeOrigin, CanisterIdRecord,
CanisterInstallMode, CanisterInstallModeV2, CanisterSettingsArgsBuilder, CanisterStatusType,
CreateCanisterArgs, EmptyBlob, InstallCodeArgsV2, Method, Payload, SkipPreUpgrade,
UpdateSettingsArgs,
CanisterInstallMode, CanisterInstallModeV2, CanisterSettingsArgsBuilder,
CanisterStatusResultV2, CanisterStatusType, CreateCanisterArgs, EmptyBlob, InstallCodeArgsV2,
Method, Payload, SkipPreUpgrade, UpdateSettingsArgs,
};
use ic_interfaces::execution_environment::{
ExecutionComplexity, ExecutionMode, HypervisorError, SubnetAvailableMemory,
Expand Down Expand Up @@ -6525,3 +6525,40 @@ fn update_settings_can_set_reserved_cycles_limit() {
Some(Cycles::new(1))
);
}

#[test]
fn canister_status_contains_reserved_cycles() {
const CYCLES: Cycles = Cycles::new(1_000_000_000_000_000);
const CAPACITY: u64 = 20_000_000_000;

let mut test = ExecutionTestBuilder::new()
.with_subnet_execution_memory(CAPACITY as i64)
.with_subnet_memory_reservation(0)
.with_subnet_memory_threshold(0)
.build();

let canister_id = test
.create_canister_with_allocation(CYCLES, None, Some(1_000_000))
.unwrap();
let result = test.canister_status(canister_id);
let reply = get_reply(result);
let status = Decode!(reply.as_slice(), CanisterStatusResultV2).unwrap();
assert_eq!(
status.reserved_cycles(),
test.cycles_account_manager()
.storage_reservation_cycles(
NumBytes::new(1_000_000),
&ResourceSaturation::new(0, 0, CAPACITY),
test.subnet_size()
)
.get()
);
assert_eq!(
status.reserved_cycles(),
test.canister_state(canister_id)
.system_state
.reserved_balance()
.get()
);
assert!(status.reserved_cycles() > 0);
}
2 changes: 2 additions & 0 deletions rs/replica_tests/tests/canister_lifecycle.rs
Expand Up @@ -711,6 +711,7 @@ fn can_get_canister_information() {
None,
2592000,
0u128,
0u128,
)
);

Expand Down Expand Up @@ -762,6 +763,7 @@ fn can_get_canister_information() {
None,
259200,
0u128,
0u128,
),
CanisterStatusResultV2::decode(&res).unwrap(),
2 * BALANCE_EPSILON,
Expand Down
9 changes: 9 additions & 0 deletions rs/types/ic00_types/src/lib.rs
Expand Up @@ -841,7 +841,9 @@ impl Payload<'_> for CanisterStatusResult {}
/// controller: principal;
/// memory_size: nat;
/// cycles: nat;
/// freezing_threshold: nat,
/// idle_cycles_burned_per_day: nat;
/// reserved_cycles: nat;
/// })`
#[derive(CandidType, Debug, Deserialize, Eq, PartialEq)]
pub struct CanisterStatusResultV2 {
Expand All @@ -855,6 +857,7 @@ pub struct CanisterStatusResultV2 {
balance: Vec<(Vec<u8>, candid::Nat)>,
freezing_threshold: candid::Nat,
idle_cycles_burned_per_day: candid::Nat,
reserved_cycles: candid::Nat,
}

impl CanisterStatusResultV2 {
Expand All @@ -870,6 +873,7 @@ impl CanisterStatusResultV2 {
memory_allocation: Option<u64>,
freezing_threshold: u64,
idle_cycles_burned_per_day: u128,
reserved_cycles: u128,
) -> Self {
Self {
status,
Expand All @@ -889,6 +893,7 @@ impl CanisterStatusResultV2 {
),
freezing_threshold: candid::Nat::from(freezing_threshold),
idle_cycles_burned_per_day: candid::Nat::from(idle_cycles_burned_per_day),
reserved_cycles: candid::Nat::from(reserved_cycles),
}
}

Expand Down Expand Up @@ -923,6 +928,10 @@ impl CanisterStatusResultV2 {
pub fn idle_cycles_burned_per_day(&self) -> u128 {
self.idle_cycles_burned_per_day.0.to_u128().unwrap()
}

pub fn reserved_cycles(&self) -> u128 {
self.reserved_cycles.0.to_u128().unwrap()
}
}

/// Indicates whether the canister is running, stopping, or stopped.
Expand Down

0 comments on commit 31fd739

Please sign in to comment.