diff --git a/canisters/src/storage/src/env.rs b/canisters/src/storage/src/env.rs index e90c460..430ddaa 100644 --- a/canisters/src/storage/src/env.rs +++ b/canisters/src/storage/src/env.rs @@ -6,6 +6,3 @@ pub static ADMIN: [&str; 2] = [ ]; pub static MANAGER: [&str; 1] = ["kulzz-yaaaa-aaaal-aai3a-cai"]; - -// TODO: TODO: to be deleted -pub static MIGRATE_MOTOKO_STATE: bool = true; diff --git a/canisters/src/storage/src/impls_mo.rs b/canisters/src/storage/src/impls_mo.rs deleted file mode 100644 index 5886988..0000000 --- a/canisters/src/storage/src/impls_mo.rs +++ /dev/null @@ -1,54 +0,0 @@ -#![allow(non_snake_case)] - -// -// Conversion for migration from Motoko to Rust. -// TODO: to be deleted -// - -use crate::constants::ASSET_ENCODING_KEY_RAW; -use crate::types::store::{Asset, AssetEncoding, AssetKey}; -use crate::types_mo::mo::store::MoAsset; -use ic_cdk::api::time; -use sha2::{Digest, Sha256}; -use std::collections::HashMap; - -impl From<&MoAsset> for Asset { - fn from( - MoAsset { - key: mo_key, - headers: mo_headers, - encoding: mo_encoding, - }: &MoAsset, - ) -> Self { - let mut encodings = HashMap::new(); - - // Calculate sha256 - Duplicate implementation to preserve easily the original Motoko "modified" and "total_length" information - let mut hasher = Sha256::new(); - for chunk in mo_encoding.contentChunks.iter() { - hasher.update(chunk); - } - let sha256 = hasher.finalize().into(); - - encodings.insert( - ASSET_ENCODING_KEY_RAW.to_string(), - AssetEncoding { - modified: u64::try_from(mo_encoding.modified.clone().0).unwrap_or(time()), - content_chunks: mo_encoding.contentChunks.clone(), - total_length: mo_encoding.totalLength, - sha256, - }, - ); - - Asset { - key: AssetKey { - name: mo_key.name.clone(), - folder: mo_key.folder.clone(), - full_path: mo_key.fullPath.clone(), - token: mo_key.token.clone(), - sha256: mo_key.sha256.clone(), - }, - headers: mo_headers.clone(), - encodings, - } - } -} diff --git a/canisters/src/storage/src/lib.rs b/canisters/src/storage/src/lib.rs index 765bddc..8f9a8c3 100644 --- a/canisters/src/storage/src/lib.rs +++ b/canisters/src/storage/src/lib.rs @@ -3,23 +3,20 @@ mod constants; mod env; mod http; mod impls; -mod impls_mo; mod store; mod types; -mod types_mo; mod utils; -use candid::{decode_args, Principal}; +use candid::Principal; use ic_cdk::api::management_canister::main::{deposit_cycles, CanisterIdRecord}; use ic_cdk::api::{caller, canister_balance128, trap}; use ic_cdk::export::candid::{candid_method, export_service}; -use ic_cdk::{api, storage}; +use ic_cdk::storage; use ic_cdk_macros::{init, post_upgrade, pre_upgrade, query, update}; use std::cell::RefCell; use std::collections::HashMap; use crate::cert::update_certified_data; -use crate::env::MIGRATE_MOTOKO_STATE; use crate::http::{build_headers, create_token, streaming_strategy}; use crate::store::{ commit_batch, create_batch, create_chunk, delete_asset, get_asset, get_asset_for_url, get_keys, @@ -29,9 +26,8 @@ use crate::types::http::{ HttpRequest, HttpResponse, StreamingCallbackHttpResponse, StreamingCallbackToken, }; use crate::types::interface::{CommitBatch, Del, InitUpload, UploadChunk}; -use crate::types::state::{Assets, RuntimeState, StableState, State}; +use crate::types::state::{RuntimeState, StableState, State}; use crate::types::store::{Asset, AssetKey, Chunk}; -use crate::types_mo::mo::state::MoState; use crate::utils::{is_manager, principal_not_equal}; thread_local! { @@ -82,35 +78,6 @@ fn post_upgrade() { } fn stable_state_to_upgrade() -> StableState { - if MIGRATE_MOTOKO_STATE { - // TODO: delete after migration from Motoko to Rust - // By senior.joinu - not all heroes wear capes - let mut stable_length_buf = [0u8; std::mem::size_of::()]; - api::stable::stable_read(0, &mut stable_length_buf); - let stable_length = u32::from_le_bytes(stable_length_buf); // maybe use from_be_bytes, I don't remember what endianess is candid - - let mut buf = vec![0u8; stable_length as usize]; - api::stable::stable_read(std::mem::size_of::() as u32, &mut buf); - - let (mo_state,): (MoState,) = decode_args(&buf).unwrap(); - - let user: Option = mo_state.user.clone(); - - fn migrate_assets(MoState { entries, user: _ }: MoState) -> Assets { - match entries { - None => HashMap::new(), - Some(e) => e - .iter() - .map(|(key, mo_asset)| (key.clone(), Asset::from(mo_asset))) - .collect(), - } - } - - let assets: Assets = migrate_assets(mo_state); - - return StableState { user, assets }; - } - let (stable,): (StableState,) = storage::stable_restore().unwrap(); stable } diff --git a/canisters/src/storage/src/types_mo.rs b/canisters/src/storage/src/types_mo.rs deleted file mode 100644 index 4f20048..0000000 --- a/canisters/src/storage/src/types_mo.rs +++ /dev/null @@ -1,66 +0,0 @@ -#![allow(non_snake_case)] - -// -// Types for migration from Motoko to Rust. -// TODO: to be deleted -// - -pub mod mo { - pub mod state { - use crate::types_mo::mo::store::MoAsset; - use candid::{CandidType, Deserialize, Principal}; - - #[derive(CandidType, Deserialize)] - pub struct MoState { - pub user: Option, - pub entries: Option>, - } - } - - pub mod store { - use crate::types::http::HeaderField; - use candid::{CandidType, Int}; - use serde::Deserialize; - use std::clone::Clone; - - #[derive(CandidType, Deserialize, Clone)] - pub struct MoChunk { - pub batchId: u128, - pub content: Vec, - } - - #[derive(CandidType, Deserialize, Clone)] - pub struct MoAssetEncoding { - pub modified: Int, - pub contentChunks: Vec>, - pub totalLength: u128, - } - - #[derive(CandidType, Deserialize, Clone)] - pub struct MoAssetKey { - // myimage.jpg - pub name: String, - // images - pub folder: String, - // /images/myimage.jpg - pub fullPath: String, - // ?token=1223-3345-5564-3333 - pub token: Option, - // The sha256 representation of the content - pub sha256: Option>, - } - - #[derive(CandidType, Deserialize, Clone)] - pub struct MoAsset { - pub key: MoAssetKey, - pub headers: Vec, - pub encoding: MoAssetEncoding, - } - - #[derive(CandidType, Deserialize, Clone)] - pub struct MoBatch { - pub key: MoAssetKey, - pub expiresAt: u64, - } - } -}