Skip to content

Commit

Permalink
[ROSETTA1-219] ledger: switch to ciborium for CBOR
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-kashitsyn committed Feb 28, 2022
1 parent 7d26c4b commit a5379d1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
25 changes: 25 additions & 0 deletions rs/Cargo.lock

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

3 changes: 2 additions & 1 deletion rs/rosetta-api/ledger_canister/Cargo.toml
Expand Up @@ -6,11 +6,12 @@ description = "Like an accountant, but on the blockchain!"
edition = "2018"

[dependencies]
candid = "0.7.10"
ciborium = { git = "https://github.com/enarx/ciborium", rev = "e719537c99b564c3674a56defe53713c702c6f46" }
dfn_core = {path = "../../rust_canisters/dfn_core"}
dfn_candid = {path = "../../rust_canisters/dfn_candid"}
dfn_http = {path = "../../rust_canisters/dfn_http"}
dfn_protobuf = {path = "../../rust_canisters/dfn_protobuf"}
candid = "0.7.10"
lazy_static = "1.4.0"
serde = "1.0"
ic-crypto-sha = {path = "../../crypto/sha/"}
Expand Down
9 changes: 3 additions & 6 deletions rs/rosetta-api/ledger_canister/src/archive_node.rs
Expand Up @@ -229,15 +229,14 @@ fn post_upgrade() {
over_init(|_: BytesS| {
let bytes = stable::get();
let mut state = ARCHIVE_STATE.write().unwrap();
*state = serde_cbor::from_slice(&bytes).expect("Decoding stable memory failed");
*state = ciborium::de::from_reader(std::io::Cursor::new(&bytes))
.expect("Decoding stable memory failed");
state.last_upgrade_timestamp = dfn_core::api::time_nanos();
});
}

#[export_name = "canister_pre_upgrade"]
fn pre_upgrade() {
use std::io::Write;

dfn_core::setup::START.call_once(|| {
dfn_core::printer::hook();
});
Expand All @@ -246,9 +245,7 @@ fn pre_upgrade() {
.read()
// This should never happen, but it's better to be safe than sorry
.unwrap_or_else(|poisoned| poisoned.into_inner());
let mut writer = stable::StableWriter::new();
serde_cbor::to_writer(&mut writer, &*archive_state).unwrap();
writer.flush().unwrap();
ciborium::ser::into_writer(&*archive_state, stable::StableWriter::new()).unwrap();
}

fn encode_metrics(w: &mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
Expand Down
11 changes: 3 additions & 8 deletions rs/rosetta-api/ledger_canister/src/main.rs
Expand Up @@ -455,7 +455,7 @@ fn main() {
fn post_upgrade() {
over_init(|_: BytesS| {
let mut ledger = LEDGER.write().unwrap();
*ledger = serde_cbor::from_reader(&mut stable::StableReader::new())
*ledger = ciborium::de::from_reader(stable::StableReader::new())
.expect("Decoding stable memory failed");

ledger.maximum_number_of_accounts = 28_000_000;
Expand All @@ -471,8 +471,6 @@ fn post_upgrade() {

#[export_name = "canister_pre_upgrade"]
fn pre_upgrade() {
use std::io::Write;

setup::START.call_once(|| {
printer::hook();
});
Expand All @@ -481,11 +479,8 @@ fn pre_upgrade() {
.read()
// This should never happen, but it's better to be safe than sorry
.unwrap_or_else(|poisoned| poisoned.into_inner());
let mut writer = stable::StableWriter::new();
serde_cbor::to_writer(&mut writer, &*ledger).unwrap();
writer
.flush()
.expect("failed to flush stable memory writer");
ciborium::ser::into_writer(&*ledger, stable::StableWriter::new())
.expect("failed to write ledger state to stable memory");
}

/// Upon reaching a `trigger_threshold` we will archive `num_blocks`.
Expand Down

0 comments on commit a5379d1

Please sign in to comment.