Skip to content

Commit

Permalink
test: integration tests for deposited cycles
Browse files Browse the repository at this point in the history
jedna committed Sep 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 441f916 commit 7d2aca3
Showing 4 changed files with 113 additions and 4 deletions.
5 changes: 4 additions & 1 deletion examples/advanced_funding/advanced_funding.did
Original file line number Diff line number Diff line change
@@ -2,4 +2,7 @@ type FundingConfig = record {
funded_canister_ids : vec principal;
};

service : (FundingConfig) -> {}
service : (FundingConfig) -> {
// A method to retrieve the total of deposited cycles per canister.
get_deposited_cycles : () -> (vec record { canister_id: principal; deposited_cycles: nat128 }) query;
}
23 changes: 22 additions & 1 deletion examples/advanced_funding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use std::{cell::RefCell, rc::Rc, sync::Arc};

use candid::{self, CandidType, Deserialize, Principal};
use canfund::{api::{cmc::IcCyclesMintingCanister, ledger::IcLedgerCanister}, manager::{options::{EstimatedRuntime, FundManagerOptions, FundStrategy, ObtainCyclesOptions}, RegisterOpts}, operations::{fetch::FetchCyclesBalanceFromCanisterStatus, obtain::MintCycles}, FundManager};
use ic_cdk::id;
use ic_cdk::{id, query};
use ic_cdk_macros::{init, post_upgrade};
use ic_ledger_types::{Subaccount, DEFAULT_SUBACCOUNT, MAINNET_CYCLES_MINTING_CANISTER_ID, MAINNET_LEDGER_CANISTER_ID};

@@ -94,3 +94,24 @@ pub fn get_obtain_cycles_config() -> Option<ObtainCyclesOptions> {
top_up_self: true,
})
}

#[derive(CandidType, Deserialize)]
pub struct GetDepositedCyclesRetItem {
pub deposited_cycles: u128,
pub canister_id: Principal,
}

#[query(name = "get_deposited_cycles")]
fn get_deposited_cycles() -> Vec<GetDepositedCyclesRetItem> {
FUND_MANAGER.with(|fund_manager| {
let fund_manager = fund_manager.borrow();

fund_manager.get_canisters().iter().map(|(canister_id, record)| {
let deposited_cycles = record.get_deposited_cycles().as_ref().map(|c| c.amount).unwrap_or(0);
GetDepositedCyclesRetItem {
deposited_cycles,
canister_id: *canister_id,
}
}).collect()
})
}
67 changes: 66 additions & 1 deletion tests/integration/src/cycles_monitor_tests.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use std::time::Duration;

use ic_ledger_types::{AccountIdentifier, DEFAULT_SUBACCOUNT};

use crate::interfaces::{get_icp_account_balance, send_icp_to_account, ICP};
use crate::interfaces::{get_icp_account_balance, send_icp_to_account, query_deposited_cycles, ICP};
use crate::setup::{install_advanced_funding_canister, install_funded_canister, install_simple_funding_canister, setup_new_env};
use crate::utils::{advance_time_to_burn_cycles, controller_test_id};
use crate::TestEnv;
@@ -42,6 +42,71 @@ fn successfuly_monitors_funded_canister_and_tops_up() {
assert!(env.cycle_balance(funded_canister_id) > 350_000_000_000);
}

#[test]
fn successfuly_stores_funding_data() {
let TestEnv {
env, controller, ..
} = setup_new_env();

let top_up_should_happen_when_cycles_below = 125_000_000_000;

let funded_canister_id = install_funded_canister(&env, controller, top_up_should_happen_when_cycles_below + 5_000_000_000);
let funding_canister_id = install_advanced_funding_canister(&env, controller, 100_000_000_000_000, vec![funded_canister_id]);

env.set_controllers(funded_canister_id, Some(controller), vec![controller, funding_canister_id]).unwrap();

let funded_canister_cycles_balance = env.cycle_balance(funded_canister_id);
if funded_canister_cycles_balance <= top_up_should_happen_when_cycles_below {
panic!("Funded canister's cycles balance is too low to run the test");
}

// wait for the fund manager to complete and release the lock
env.tick();
env.tick();

advance_time_to_burn_cycles(
&env,
controller_test_id(),
funded_canister_id,
top_up_should_happen_when_cycles_below - 5_000_000_000,
);

env.tick();
env.tick();

let deposited_cycles = query_deposited_cycles(&env, funding_canister_id);

// check if the deposited cycles are stored correctly for the funding canister and the funded canister
for record in &deposited_cycles {
if record.canister_id == funded_canister_id {
assert_eq!(record.deposited_cycles, 250_000_000_000);
} else {
assert_eq!(record.deposited_cycles, 0);
}
}

advance_time_to_burn_cycles(
&env,
controller_test_id(),
funded_canister_id,
top_up_should_happen_when_cycles_below - 5_000_000_000,
);

env.tick();
env.tick();

let deposited_cycles = query_deposited_cycles(&env, funding_canister_id);

// check if the deposited cycles are stored correctly for the funding canister and the funded canister
for record in &deposited_cycles {
if record.canister_id == funded_canister_id {
assert_eq!(record.deposited_cycles, 500_000_000_000);
} else {
assert_eq!(record.deposited_cycles, 0);
}
}
}

#[test]
fn can_mint_cycles_to_top_up_self() {
let TestEnv {
22 changes: 21 additions & 1 deletion tests/integration/src/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use candid::{CandidType, Principal};
use ic_ledger_types::{AccountBalanceArgs, AccountIdentifier, Memo, Subaccount, Tokens, TransferArgs, TransferError, MAINNET_LEDGER_CANISTER_ID};
use pocket_ic::{update_candid_as, PocketIc};
use pocket_ic::{query_candid_as, update_candid_as, PocketIc};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};

#[derive(CandidType)]
@@ -64,3 +65,22 @@ pub fn send_icp_to_account(
.unwrap();
res.0
}

#[derive(CandidType, Deserialize, Debug)]
pub struct GetDepositedCyclesRetItem {
pub deposited_cycles: u128,
pub canister_id: Principal,
}

pub fn query_deposited_cycles(env: &PocketIc, canister_id: Principal) -> Vec<GetDepositedCyclesRetItem> {
let res: (Vec<GetDepositedCyclesRetItem>,) = query_candid_as(
env,
canister_id,
Principal::anonymous(),
"get_deposited_cycles",
(),
)
.unwrap();

res.0
}

0 comments on commit 7d2aca3

Please sign in to comment.