Skip to content

Commit

Permalink
refactor: switch from ciborium to minicbor-serde
Browse files Browse the repository at this point in the history
TODO: check compatibility between the two
  • Loading branch information
rupansh committed Nov 29, 2024
1 parent 97003fc commit d5c6035
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 52 deletions.
38 changes: 34 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/canister/individual_user_template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ciborium::de;
use ic_cdk::api::call::ArgDecoderConfig;
use ic_cdk_macros::post_upgrade;
use ic_stable_structures::Memory;
Expand Down Expand Up @@ -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;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ciborium::ser;
use ic_cdk::api::stable;
use ic_cdk_macros::pre_upgrade;
use ic_stable_structures::writer::Writer;
Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ impl From<Namespace> 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
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/canister/platform_orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ciborium::de;
use ic_cdk::api::call::ArgDecoderConfig;
use ic_cdk_macros::post_upgrade;
use ic_stable_structures::Memory;
Expand All @@ -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;
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/canister/platform_orchestrator/src/data_model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ciborium::de;
use ic_stable_structures::{storable::Bound, StableBTreeMap, StableLog, Storable};
use std::{
borrow::Cow,
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/canister/user_index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use ciborium::de;

use ic_cdk::api::call::ArgDecoderConfig;
use ic_cdk_macros::post_upgrade;
use ic_stable_structures::Memory;
Expand Down Expand Up @@ -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;
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shared_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 2 additions & 5 deletions src/lib/shared_utils/src/common/participant_crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ impl ProofOfAuthorityMsg {
}

pub fn serialize_cbor(&self) -> Vec<u8> {
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")
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/lib/shared_utils/src/common/types/wasm.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit d5c6035

Please sign in to comment.