Skip to content

Commit

Permalink
migrate individual template data stage 1 (#198)
Browse files Browse the repository at this point in the history
* add memory manager and update pre_upgrade hook

* remove send_metrices from post_upgrade; update recharge amout in user_index

* fix test
  • Loading branch information
ravibazz committed Dec 8, 2023
1 parent 54dc368 commit 951f0ab
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/canister/individual_user_template/Cargo.toml
Expand Up @@ -10,6 +10,8 @@ crate-type = ["cdylib"]

[dependencies]
candid = { workspace = true }
ciborium = { workspace = true }
ic-stable-structures = {workspace = true }
ic-cdk = { workspace = true }
ic-cdk-timers = { workspace = true }
serde = { workspace = true }
Expand Down
Expand Up @@ -22,7 +22,6 @@ fn post_upgrade() {
save_upgrade_args_to_memory();
refetch_well_known_principals();
reenqueue_timers_for_pending_bet_outcomes();
send_canister_metrics();
}

fn restore_data_from_stable_memory() {
Expand Down
@@ -1,17 +1,22 @@
use shared_utils::common::utils::stable_memory_serializer_deserializer;
use ciborium::ser;
use ic_stable_structures::writer::Writer;

use crate::data_model::memory;
use crate::CANISTER_DATA;

pub const BUFFER_SIZE_BYTES: usize = 2 * 1024 * 1024; // 2 MiB

#[ic_cdk::pre_upgrade]
fn pre_upgrade() {
let mut state_bytes = vec![];
CANISTER_DATA.with(|canister_data_ref_cell| {
let canister_data = canister_data_ref_cell.take();
stable_memory_serializer_deserializer::serialize_to_stable_memory(
canister_data,
BUFFER_SIZE_BYTES,
)
.expect("Failed to serialize canister data");
});
}
ser::into_writer(&*canister_data_ref_cell.borrow(), &mut state_bytes)
})
.expect("failed to encode state");

let len = state_bytes.len() as u32;
let mut memory = memory::get_upgrades_memory();
let mut writer = Writer::new(&mut memory, 0);
writer.write(&len.to_le_bytes()).unwrap();
writer.write(&state_bytes).unwrap();
}
22 changes: 22 additions & 0 deletions src/canister/individual_user_template/src/data_model/memory.rs
@@ -0,0 +1,22 @@
use ic_stable_structures::{DefaultMemoryImpl, memory_manager::{MemoryId, VirtualMemory, MemoryManager}};
use std::cell::RefCell;

// A memory for upgrades, where data from the heap can be serialized/deserialized.
const UPGRADES: MemoryId = MemoryId::new(0);

// A memory for the StableBTreeMap we're using. A new memory should be created for
// every additional stable structure.


pub type Memory = VirtualMemory<DefaultMemoryImpl>;

thread_local! {
// The memory manager is used for simulating multiple memories. Given a `MemoryId` it can
// return a memory that can be used by stable structures.
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
}

pub fn get_upgrades_memory() -> Memory {
MEMORY_MANAGER.with(|m| m.borrow_mut().get(UPGRADES))
}
Expand Up @@ -17,6 +17,7 @@ use shared_utils::{
use self::version_details::VersionDetails;

pub mod version_details;
pub mod memory;

#[derive(Default, Deserialize, Serialize)]
pub struct CanisterData {
Expand Down
Expand Up @@ -62,7 +62,7 @@ fn when_canister_cycle_balance_is_below_the_configured_or_freezing_threshold_the
alice_canister_id,
*user_index_canister_id,
"return_cycles_to_user_index_canister",
candid::encode_one(Some(1_000_000_000_000_u128)).unwrap(),
candid::encode_one(Some(600_000_000_000_u128)).unwrap(),
)
.unwrap();

Expand Down Expand Up @@ -125,5 +125,5 @@ fn when_canister_cycle_balance_is_below_the_configured_or_freezing_threshold_the
alice_cycle_balance_after_user_index_upgrade
);

assert!(alice_cycle_balance_after_user_index_upgrade >= 1_000_000_000_000);
assert!(alice_cycle_balance_after_user_index_upgrade >= 600_000_000_000);
}
2 changes: 1 addition & 1 deletion src/lib/shared_utils/src/constant.rs
Expand Up @@ -2,7 +2,7 @@ use candid::Principal;

use crate::common::types::known_principal::{KnownPrincipalMap, KnownPrincipalType};

pub const INDIVIDUAL_USER_CANISTER_RECHARGE_AMOUNT: u128 = 1_000_000_000_000; // 1T Cycles
pub const INDIVIDUAL_USER_CANISTER_RECHARGE_AMOUNT: u128 = 600_000_000_000; // 0.6T Cycles
pub const CYCLES_THRESHOLD_TO_INITIATE_RECHARGE: u128 = 500_000_000_000; // 0.5T Cycles

pub const MAX_USERS_IN_FOLLOWER_FOLLOWING_LIST: u64 = 10000;
Expand Down

0 comments on commit 951f0ab

Please sign in to comment.