@dsarlis Sorry for the confusion. Here are all the details
thread_local! {
pub static CANISTER_DATA: RefCell<CanisterData> = RefCell::default();
}
#[derive(Clone, Serialize, Deserialize, PartialEq, PartialOrd, Eq, Ord, CandidType)]
pub enum WasmType {
SubnetOrchestratorWasm,
IndividualUserWasm,
PostCacheWasm
}
impl BoundedStorable for WasmType {
const MAX_SIZE: u32 = 4;
const IS_FIXED_SIZE: bool = true;
}
impl Storable for WasmType {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
let mut bytes = vec![];
ciborium::ser::into_writer(self, &mut bytes).unwrap();
Cow::Owned(bytes)
}
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
let wasm_type: WasmType = de::from_reader(bytes.as_ref()).unwrap();
wasm_type
}
}
#[derive(Serialize, Deserialize)]
pub struct CanisterData {
pub all_subnet_orchestrator_canisters_list: HashSet<Principal>,
pub all_post_cache_orchestrator_list: HashSet<Principal>,
pub subet_orchestrator_with_capacity_left: HashSet<Principal>,
pub version_detail: VersionDetails,
#[serde(skip, default = "_default_wasms")]
pub wasms: StableBTreeMap<WasmType, Blob<2000000>, Memory>,
#[serde(skip, default = "_default_canister_upgrade_log")]
pub subnet_canister_upgrade_log: StableLog<CanisterUpgradeStatus, Memory, Memory>,
pub last_subnet_canister_upgrade_status: CanisterUpgradeStatus
}
fn _default_wasms() -> StableBTreeMap<WasmType, Blob<2000000>, Memory> {
StableBTreeMap::init(get_subnet_orchestrator_wasm_memory())
}
fn _default_canister_upgrade_log() -> StableLog<CanisterUpgradeStatus, Memory, Memory> {
StableLog::init(get_canister_upgrade_log_index_memory(), get_canister_upgrade_log_memory()).unwrap()
}
use ic_stable_structures::{DefaultMemoryImpl, memory_manager::{self, MemoryId, MemoryManager, VirtualMemory}};
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 BtreeMap to store wasms
const SUBNET_ORCHESTRATOR_WASM_MEMORY: MemoryId = MemoryId::new(1);
//A memory for canister upgrade log index
const CANISTER_UPGRADE_LOG_INDEX: MemoryId = MemoryId::new(2);
//A memory for canister upgrade log
const CANISTER_UPGRADE_LOG: MemoryId = MemoryId::new(3);
// 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_with_bucket_size(DefaultMemoryImpl::default(), 1));
}
pub fn get_upgrades_memory() -> Memory {
MEMORY_MANAGER.with(|m| m.borrow_mut().get(UPGRADES))
}
pub fn get_subnet_orchestrator_wasm_memory() -> Memory {
MEMORY_MANAGER.with_borrow_mut(|memory_manager| memory_manager.get(SUBNET_ORCHESTRATOR_WASM_MEMORY))
}
pub fn get_canister_upgrade_log_index_memory() -> Memory {
MEMORY_MANAGER.with_borrow_mut(|memory_manager| memory_manager.get(CANISTER_UPGRADE_LOG_INDEX))
}
pub fn get_canister_upgrade_log_memory() -> Memory {
MEMORY_MANAGER.with_borrow_mut(|memory_manager| memory_manager.get(CANISTER_UPGRADE_LOG))
}
pub fn init_memory_manager() {
MEMORY_MANAGER.with(|m| {
*m.borrow_mut() = MemoryManager::init_with_bucket_size(DefaultMemoryImpl::default(), 1);
})
}