From d5c60357ca3373c676c069580055913146cdd128 Mon Sep 17 00:00:00 2001 From: rupansh Date: Fri, 29 Nov 2024 22:31:12 +0530 Subject: [PATCH] refactor: switch from ciborium to minicbor-serde TODO: check compatibility between the two --- Cargo.lock | 38 +++++++++++++++++-- Cargo.toml | 2 +- .../individual_user_template/Cargo.toml | 2 +- .../api/canister_lifecycle/post_upgrade.rs | 3 +- .../src/api/canister_lifecycle/pre_upgrade.rs | 6 +-- .../src/data_model/kv_storage.rs | 10 ++--- src/canister/platform_orchestrator/Cargo.toml | 2 +- .../api/canister_lifecycle/post_upgrade.rs | 3 +- .../src/api/canister_lifecycle/pre_upgrade.rs | 6 +-- .../src/data_model/mod.rs | 6 +-- src/canister/user_index/Cargo.toml | 2 +- .../api/canister_lifecycle/post_upgrade.rs | 4 +- .../src/api/canister_lifecycle/pre_upgrade.rs | 6 +-- .../create_pool_of_available_canisters.rs | 4 +- src/lib/shared_utils/Cargo.toml | 2 +- .../src/common/participant_crypto/mod.rs | 7 +--- src/lib/shared_utils/src/common/types/wasm.rs | 11 ++---- 17 files changed, 62 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7864e69d..53c04d50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3749,7 +3749,6 @@ name = "individual_user_template" version = "0.1.0" dependencies = [ "candid", - "ciborium", "futures", "hex", "ic-base-types", @@ -3767,6 +3766,7 @@ dependencies = [ "ic-sns-wasm", "ic-stable-structures", "icrc-ledger-types 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "minicbor-serde", "serde", "serde_bytes", "serde_json", @@ -4075,6 +4075,36 @@ dependencies = [ "unicase", ] +[[package]] +name = "minicbor" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0452a60c1863c1f50b5f77cd295e8d2786849f35883f0b9e18e7e6e1b5691b0" +dependencies = [ + "minicbor-derive", +] + +[[package]] +name = "minicbor-derive" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd2209fff77f705b00c737016a48e73733d7fbccb8b007194db148f03561fb70" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.89", +] + +[[package]] +name = "minicbor-serde" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "becf18ac384ecf6f53b2db3b1549eebff664c67ecf259ae99be5912193291686" +dependencies = [ + "minicbor", + "serde", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -4491,10 +4521,10 @@ name = "platform_orchestrator" version = "0.1.0" dependencies = [ "candid", - "ciborium", "ic-cdk 0.15.1", "ic-cdk-macros 0.16.0", "ic-stable-structures", + "minicbor-serde", "serde", "shared_utils", "test_utils", @@ -5570,7 +5600,6 @@ version = "0.1.0" dependencies = [ "blake3", "candid", - "ciborium", "ed25519-compact", "futures", "generic-array 1.1.1", @@ -5581,6 +5610,7 @@ dependencies = [ "ic-stable-structures", "icrc-ledger-types 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "memory-db", + "minicbor-serde", "parity-scale-codec", "pprof", "rmp-serde", @@ -6287,12 +6317,12 @@ name = "user_index" version = "0.1.0" dependencies = [ "candid", - "ciborium", "futures", "ic-cdk 0.15.1", "ic-cdk-macros 0.16.0", "ic-cdk-timers", "ic-stable-structures", + "minicbor-serde", "serde", "serde_json", "shared_utils", diff --git a/Cargo.toml b/Cargo.toml index ee2f9747..98dab6a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ members = [ [workspace.dependencies] candid = "0.10.2" -ciborium = "0.2.1" +minicbor-serde = { version = "0.3.2", features = ["std"] } pocket-ic = "6.0.0" ic-cdk = "0.15.1" ic-cdk-timers = "0.7.0" diff --git a/src/canister/individual_user_template/Cargo.toml b/src/canister/individual_user_template/Cargo.toml index a7ae276f..daa9ad3f 100644 --- a/src/canister/individual_user_template/Cargo.toml +++ b/src/canister/individual_user_template/Cargo.toml @@ -10,7 +10,7 @@ crate-type = ["cdylib"] [dependencies] candid = { workspace = true } -ciborium = { workspace = true } +minicbor-serde = { workspace = true } ic-stable-structures = { workspace = true } ic-cdk = { workspace = true } ic-cdk-timers = { workspace = true } diff --git a/src/canister/individual_user_template/src/api/canister_lifecycle/post_upgrade.rs b/src/canister/individual_user_template/src/api/canister_lifecycle/post_upgrade.rs index 7197f3bf..282e952b 100644 --- a/src/canister/individual_user_template/src/api/canister_lifecycle/post_upgrade.rs +++ b/src/canister/individual_user_template/src/api/canister_lifecycle/post_upgrade.rs @@ -1,4 +1,3 @@ -use ciborium::de; use ic_cdk::api::call::ArgDecoderConfig; use ic_cdk_macros::post_upgrade; use ic_stable_structures::Memory; @@ -31,7 +30,7 @@ fn restore_data_from_stable_memory() { heap_data.read(4, &mut canister_data_bytes); let canister_data = - de::from_reader(&*canister_data_bytes).expect("Failed to deserialize heap data"); + minicbor_serde::from_slice(&canister_data_bytes).expect("Failed to deserialize heap data"); CANISTER_DATA.with(|canister_data_ref_cell| { *canister_data_ref_cell.borrow_mut() = canister_data; }); diff --git a/src/canister/individual_user_template/src/api/canister_lifecycle/pre_upgrade.rs b/src/canister/individual_user_template/src/api/canister_lifecycle/pre_upgrade.rs index 85226274..ba7d0f6c 100644 --- a/src/canister/individual_user_template/src/api/canister_lifecycle/pre_upgrade.rs +++ b/src/canister/individual_user_template/src/api/canister_lifecycle/pre_upgrade.rs @@ -1,4 +1,3 @@ -use ciborium::ser; use ic_cdk::api::stable; use ic_cdk_macros::pre_upgrade; use ic_stable_structures::writer::Writer; @@ -9,9 +8,8 @@ use crate::CANISTER_DATA; #[pre_upgrade] fn pre_upgrade() { - let mut state_bytes = vec![]; - CANISTER_DATA.with(|canister_data_ref_cell| { - ser::into_writer(&*canister_data_ref_cell.borrow(), &mut state_bytes) + let state_bytes = CANISTER_DATA.with(|canister_data_ref_cell| { + minicbor_serde::to_vec(&*canister_data_ref_cell.borrow()) }) .expect("failed to encode state"); diff --git a/src/canister/individual_user_template/src/data_model/kv_storage.rs b/src/canister/individual_user_template/src/data_model/kv_storage.rs index 12546545..5c49649f 100644 --- a/src/canister/individual_user_template/src/data_model/kv_storage.rs +++ b/src/canister/individual_user_template/src/data_model/kv_storage.rs @@ -37,13 +37,12 @@ impl From for NamespaceForFrontend { impl Storable for Namespace { fn to_bytes(&self) -> std::borrow::Cow<[u8]> { - let mut bytes = vec![]; - ciborium::ser::into_writer(self, &mut bytes).unwrap(); + let bytes = minicbor_serde::to_vec(self).unwrap(); Cow::Owned(bytes) } fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { - let namespace: Self = ciborium::de::from_reader(bytes.as_ref()).unwrap(); + let namespace: Self = minicbor_serde::from_slice(bytes.as_ref()).unwrap(); namespace } @@ -188,13 +187,12 @@ pub struct NameSpaceKey { impl Storable for NameSpaceKey { fn to_bytes(&self) -> std::borrow::Cow<[u8]> { - let mut bytes = vec![]; - ciborium::ser::into_writer(self, &mut bytes).unwrap(); + let bytes = minicbor_serde::to_vec(self).unwrap(); Cow::Owned(bytes) } fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { - let namespace: Self = ciborium::de::from_reader(bytes.as_ref()).unwrap(); + let namespace: Self = minicbor_serde::from_slice(bytes.as_ref()).unwrap(); namespace } diff --git a/src/canister/platform_orchestrator/Cargo.toml b/src/canister/platform_orchestrator/Cargo.toml index eccfa50d..97f33c44 100644 --- a/src/canister/platform_orchestrator/Cargo.toml +++ b/src/canister/platform_orchestrator/Cargo.toml @@ -15,7 +15,7 @@ ic-cdk = { workspace = true } serde = { workspace = true } ic-cdk-macros = { workspace = true } ic-stable-structures = { workspace = true } -ciborium = { workspace = true } +minicbor-serde = { workspace = true } shared_utils = { workspace = true } [dev-dependencies] diff --git a/src/canister/platform_orchestrator/src/api/canister_lifecycle/post_upgrade.rs b/src/canister/platform_orchestrator/src/api/canister_lifecycle/post_upgrade.rs index bff21a44..c2a5f0fc 100644 --- a/src/canister/platform_orchestrator/src/api/canister_lifecycle/post_upgrade.rs +++ b/src/canister/platform_orchestrator/src/api/canister_lifecycle/post_upgrade.rs @@ -1,4 +1,3 @@ -use ciborium::de; use ic_cdk::api::call::ArgDecoderConfig; use ic_cdk_macros::post_upgrade; use ic_stable_structures::Memory; @@ -25,7 +24,7 @@ fn restore_data_from_stable_memory() { let mut canister_data_bytes = vec![0; heap_data_len]; heap_data.read(4, &mut canister_data_bytes); let canister_data = - de::from_reader(&*canister_data_bytes).expect("Failed to deserialize heap data"); + minicbor_serde::from_slice(&canister_data_bytes).expect("Failed to deserialize heap data"); CANISTER_DATA.with_borrow_mut(|cd| { *cd = canister_data; }) diff --git a/src/canister/platform_orchestrator/src/api/canister_lifecycle/pre_upgrade.rs b/src/canister/platform_orchestrator/src/api/canister_lifecycle/pre_upgrade.rs index 7a20bd3a..7d12952e 100644 --- a/src/canister/platform_orchestrator/src/api/canister_lifecycle/pre_upgrade.rs +++ b/src/canister/platform_orchestrator/src/api/canister_lifecycle/pre_upgrade.rs @@ -1,15 +1,13 @@ use ic_cdk_macros::pre_upgrade; use ic_stable_structures::writer::Writer; -use ciborium::ser; use crate::{data_model::memory, CANISTER_DATA}; #[pre_upgrade] fn pre_upgrade() { - let mut state_bytes = vec![]; - CANISTER_DATA.with_borrow(|canister_data| - ser::into_writer(&*canister_data, &mut state_bytes) + let state_bytes = CANISTER_DATA.with_borrow(|canister_data| + minicbor_serde::to_vec(canister_data) ) .expect("failed to encode state"); let len = state_bytes.len() as u32; diff --git a/src/canister/platform_orchestrator/src/data_model/mod.rs b/src/canister/platform_orchestrator/src/data_model/mod.rs index abdf9329..f033388e 100644 --- a/src/canister/platform_orchestrator/src/data_model/mod.rs +++ b/src/canister/platform_orchestrator/src/data_model/mod.rs @@ -1,4 +1,3 @@ -use ciborium::de; use ic_stable_structures::{storable::Bound, StableBTreeMap, StableLog, Storable}; use std::{ borrow::Cow, @@ -161,13 +160,12 @@ pub struct CanisterUpgradeStatus { impl Storable for CanisterUpgradeStatus { fn to_bytes(&self) -> std::borrow::Cow<[u8]> { - let mut bytes = vec![]; - ciborium::ser::into_writer(self, &mut bytes).unwrap(); + let bytes = minicbor_serde::to_vec(self).unwrap(); Cow::Owned(bytes) } fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { - let canister_upgrade_log: CanisterUpgradeStatus = de::from_reader(bytes.as_ref()).unwrap(); + let canister_upgrade_log: CanisterUpgradeStatus = minicbor_serde::from_slice(bytes.as_ref()).unwrap(); canister_upgrade_log } diff --git a/src/canister/user_index/Cargo.toml b/src/canister/user_index/Cargo.toml index d1bd5d66..52efaf5c 100644 --- a/src/canister/user_index/Cargo.toml +++ b/src/canister/user_index/Cargo.toml @@ -17,7 +17,7 @@ shared_utils = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } ic-stable-structures = { workspace = true } -ciborium = { workspace = true } +minicbor-serde = { workspace = true } futures = { workspace = true } [dev-dependencies] diff --git a/src/canister/user_index/src/api/canister_lifecycle/post_upgrade.rs b/src/canister/user_index/src/api/canister_lifecycle/post_upgrade.rs index 9d6ee35b..ebe44103 100644 --- a/src/canister/user_index/src/api/canister_lifecycle/post_upgrade.rs +++ b/src/canister/user_index/src/api/canister_lifecycle/post_upgrade.rs @@ -1,5 +1,3 @@ -use ciborium::de; - use ic_cdk::api::call::ArgDecoderConfig; use ic_cdk_macros::post_upgrade; use ic_stable_structures::Memory; @@ -44,7 +42,7 @@ fn restore_data_from_stable_memory() { let mut canister_data_bytes = vec![0; heap_data_len]; heap_data.read(4, &mut canister_data_bytes); let canister_data = - de::from_reader(&*canister_data_bytes).expect("Failed to deserialize heap data"); + minicbor_serde::from_slice(&canister_data_bytes).expect("Failed to deserialize heap data"); CANISTER_DATA.with_borrow_mut(|cd| { *cd = canister_data; }) diff --git a/src/canister/user_index/src/api/canister_lifecycle/pre_upgrade.rs b/src/canister/user_index/src/api/canister_lifecycle/pre_upgrade.rs index c7e22557..1e1e4f93 100644 --- a/src/canister/user_index/src/api/canister_lifecycle/pre_upgrade.rs +++ b/src/canister/user_index/src/api/canister_lifecycle/pre_upgrade.rs @@ -1,14 +1,12 @@ use ic_cdk_macros::pre_upgrade; use ic_stable_structures::writer::Writer; -use ciborium::ser; use crate::{data_model::memory, CANISTER_DATA}; #[pre_upgrade] fn pre_upgrade() { - let mut state_bytes = vec![]; - CANISTER_DATA.with_borrow(|canister_data| - ser::into_writer(&*canister_data, &mut state_bytes) + let state_bytes = CANISTER_DATA.with_borrow(|canister_data| + minicbor_serde::to_vec(canister_data) ) .expect("failed to encode state"); let len = state_bytes.len() as u32; diff --git a/src/canister/user_index/src/api/canister_management/create_pool_of_available_canisters.rs b/src/canister/user_index/src/api/canister_management/create_pool_of_available_canisters.rs index dfd2bd60..b7051a08 100644 --- a/src/canister/user_index/src/api/canister_management/create_pool_of_available_canisters.rs +++ b/src/canister/user_index/src/api/canister_management/create_pool_of_available_canisters.rs @@ -47,7 +47,7 @@ pub async fn impl_create_pool_of_individual_user_available_canisters(version: St let version = version.clone(); let individual_user_wasm = individual_user_wasm.clone(); async move { - recharge_canister_for_installing_wasm(canister_id).await.map_err(|e| (canister_id, e))?; + recharge_canister_for_installing_wasm(canister_id).await.map_err(|e| (canister_id, format!("recharge error {e}")))?; install_canister_wasm( canister_id, None, @@ -65,7 +65,7 @@ pub async fn impl_create_pool_of_individual_user_available_canisters(version: St }) } Err((canister_id, e)) => { - ic_cdk::println!("Failed to install wasm on canister: {:?}, error: {:?}", canister_id, e); + ic_cdk::println!("Failed to install wasm on canister: {}, error: {:?}", canister_id, e); CANISTER_DATA.with_borrow_mut(|cdata| { cdata.insert_backup_canister(canister_id); }) diff --git a/src/lib/shared_utils/Cargo.toml b/src/lib/shared_utils/Cargo.toml index 37a56d88..bd5cc060 100644 --- a/src/lib/shared_utils/Cargo.toml +++ b/src/lib/shared_utils/Cargo.toml @@ -13,7 +13,7 @@ ic-stable-structures = { workspace = true } rmp-serde = { workspace = true } futures = { workspace = true } serde = { workspace = true } -ciborium = { workspace = true } +minicbor-serde = { workspace = true } serde_json_any_key = "2.0.0" serde_bytes = "0.11.14" icrc-ledger-types = { workspace = true } diff --git a/src/lib/shared_utils/src/common/participant_crypto/mod.rs b/src/lib/shared_utils/src/common/participant_crypto/mod.rs index 865a7427..cff355a3 100644 --- a/src/lib/shared_utils/src/common/participant_crypto/mod.rs +++ b/src/lib/shared_utils/src/common/participant_crypto/mod.rs @@ -87,11 +87,8 @@ impl ProofOfAuthorityMsg { } pub fn serialize_cbor(&self) -> Vec { - let mut bytes = vec![]; - ciborium::into_writer(self, &mut bytes) - .expect("PoaMessage should serialize succesfully"); - - bytes + minicbor_serde::to_vec(self) + .expect("PoaMessage should serialize succesfully") } } diff --git a/src/lib/shared_utils/src/common/types/wasm.rs b/src/lib/shared_utils/src/common/types/wasm.rs index 3b525fd5..b52f0312 100644 --- a/src/lib/shared_utils/src/common/types/wasm.rs +++ b/src/lib/shared_utils/src/common/types/wasm.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; use candid::CandidType; -use ciborium::de; use serde::{Deserialize, Serialize}; use ic_stable_structures::{storable::Bound, Storable}; @@ -15,13 +14,12 @@ pub enum WasmType { impl Storable for WasmType { fn to_bytes(&self) -> std::borrow::Cow<[u8]> { - let mut bytes = vec![]; - ciborium::ser::into_writer(self, &mut bytes).unwrap(); + let bytes = minicbor_serde::to_vec(self).unwrap(); Cow::Owned(bytes) } fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { - let wasm_type: WasmType = de::from_reader(bytes.as_ref()).unwrap(); + let wasm_type: WasmType = minicbor_serde::from_slice(bytes.as_ref()).unwrap(); wasm_type } @@ -37,13 +35,12 @@ pub struct CanisterWasm { impl Storable for CanisterWasm { fn to_bytes(&self) -> std::borrow::Cow<[u8]> { - let mut bytes = vec![]; - ciborium::ser::into_writer(self, &mut bytes).unwrap(); + let bytes = minicbor_serde::to_vec(self).unwrap(); Cow::Owned(bytes) } fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { - let canister_wasm: CanisterWasm = de::from_reader(bytes.as_ref()).unwrap(); + let canister_wasm: CanisterWasm = minicbor_serde::from_slice(bytes.as_ref()).unwrap(); canister_wasm }