From ff145be03169f120df825caff4e3aa7f73dba8da Mon Sep 17 00:00:00 2001 From: Maciej Modelski Date: Mon, 20 Jan 2025 13:12:32 +0000 Subject: [PATCH 1/3] use default value of 0 in case LEDGER_VERSION is missing in serialized ledger state --- WORKSPACE.bazel | 4 ++ mainnet-canister-revisions.json | 8 ++++ rs/ledger_suite/icrc1/ledger/BUILD.bazel | 4 ++ rs/ledger_suite/icrc1/ledger/src/lib.rs | 6 +-- rs/ledger_suite/icrc1/ledger/tests/tests.rs | 51 +++++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 10c29ef4de2..5dfba8f4bac 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -28,11 +28,13 @@ canisters( "ck_btc_ledger": "ic-icrc1-ledger.wasm.gz", "ck_btc_ledger_v1": "ic-icrc1-ledger.wasm.gz", "ck_btc_ledger_v2": "ic-icrc1-ledger.wasm.gz", + "ck_btc_ledger_v2_noledgerversion": "ic-icrc1-ledger.wasm.gz", "ck_btc_index": "ic-icrc1-index-ng.wasm.gz", "ck_eth_archive": "ic-icrc1-archive-u256.wasm.gz", "ck_eth_ledger": "ic-icrc1-ledger-u256.wasm.gz", "ck_eth_ledger_v1": "ic-icrc1-ledger-u256.wasm.gz", "ck_eth_ledger_v2": "ic-icrc1-ledger-u256.wasm.gz", + "ck_eth_ledger_v2_noledgerversion": "ic-icrc1-ledger-u256.wasm.gz", "ck_eth_index": "ic-icrc1-index-ng-u256.wasm.gz", "sns_root": "sns-root-canister.wasm.gz", "sns_governance": "sns-governance-canister.wasm.gz", @@ -58,11 +60,13 @@ canisters( "ck_btc_ledger": "mainnet_ckbtc_ic-icrc1-ledger", "ck_btc_ledger_v1": "mainnet_ckbtc_ic-icrc1-ledger-v1", "ck_btc_ledger_v2": "mainnet_ckbtc_ic-icrc1-ledger-v2", + "ck_btc_ledger_v2_noledgerversion": "mainnet_ckbtc_ic-icrc1-ledger-v2-noledgerversion", "ck_btc_index": "mainnet_ckbtc-index-ng", "ck_eth_archive": "mainnet_cketh_ic-icrc1-archive-u256", "ck_eth_ledger": "mainnet_cketh_ic-icrc1-ledger-u256", "ck_eth_ledger_v1": "mainnet_cketh_ic-icrc1-ledger-u256-v1", "ck_eth_ledger_v2": "mainnet_cketh_ic-icrc1-ledger-u256-v2", + "ck_eth_ledger_v2_noledgerversion": "mainnet_cketh_ic-icrc1-ledger-u256-v2-noledgerversion", "ck_eth_index": "mainnet_cketh-index-ng", "sns_root": "mainnet_sns-root-canister", "sns_governance": "mainnet_sns-governance-canister", diff --git a/mainnet-canister-revisions.json b/mainnet-canister-revisions.json index f9d79c4935f..c10c3e4c6ab 100644 --- a/mainnet-canister-revisions.json +++ b/mainnet-canister-revisions.json @@ -23,6 +23,10 @@ "rev": "e54d3fa34ded227c885d04e64505fa4b5d564743", "sha256": "3d808fa63a3d8ebd4510c0400aa078e99a31afaa0515f0b68778f929ce4b2a46" }, + "ck_btc_ledger_v2_noledgerversion": { + "rev": "aba60ffbc46acfc8990bf4d5685c1360bd7026b9", + "sha256": "67cfcbabb79e683b6fc855450d9972c9efaa7a1cd28c6387965616fbead191ea" + }, "ck_eth_archive": { "rev": "2190613d3b5bcd9b74c382b22d151580b8ac271a", "sha256": "2d25f7831894100d48aa9043c65e87c293487523f0958c15760027d004fbbda9" @@ -43,6 +47,10 @@ "rev": "e54d3fa34ded227c885d04e64505fa4b5d564743", "sha256": "98a7b7391608dc4a554d6964bad24157b6aaf890a05bbaad3fcc92033d9c7b02" }, + "ck_eth_ledger_v2_noledgerversion": { + "rev": "aba60ffbc46acfc8990bf4d5685c1360bd7026b9", + "sha256": "73d0c5f057aaf33004218ce588780e1b454c717c702b1cf47532f32c23515f1e" + }, "cycles-minting": { "rev": "b5192581ccd35b67fe5a1f795ead9cbcd25956d6", "sha256": "11c8dedd11741f05990498c90f925e9e37ad60647a65ef47caa59cdba234be6f" diff --git a/rs/ledger_suite/icrc1/ledger/BUILD.bazel b/rs/ledger_suite/icrc1/ledger/BUILD.bazel index afaec0a3b60..c39ebaff775 100644 --- a/rs/ledger_suite/icrc1/ledger/BUILD.bazel +++ b/rs/ledger_suite/icrc1/ledger/BUILD.bazel @@ -280,9 +280,11 @@ rust_test( "//rs/universal_canister/impl:universal_canister.wasm.gz", "@mainnet_ckbtc_ic-icrc1-ledger-v1//file", "@mainnet_ckbtc_ic-icrc1-ledger-v2//file", + "@mainnet_ckbtc_ic-icrc1-ledger-v2-noledgerversion//file", "@mainnet_ckbtc_ic-icrc1-ledger//file", "@mainnet_cketh_ic-icrc1-ledger-u256-v1//file", "@mainnet_cketh_ic-icrc1-ledger-u256-v2//file", + "@mainnet_cketh_ic-icrc1-ledger-u256-v2-noledgerversion//file", "@mainnet_cketh_ic-icrc1-ledger-u256//file", "@mainnet_ic-icrc1-ledger//file", ], @@ -291,9 +293,11 @@ rust_test( "CKBTC_IC_ICRC1_LEDGER_DEPLOYED_VERSION_WASM_PATH": "$(rootpath @mainnet_ckbtc_ic-icrc1-ledger//file)", "CKBTC_IC_ICRC1_LEDGER_V1_VERSION_WASM_PATH": "$(rootpath @mainnet_ckbtc_ic-icrc1-ledger-v1//file)", "CKBTC_IC_ICRC1_LEDGER_V2_VERSION_WASM_PATH": "$(rootpath @mainnet_ckbtc_ic-icrc1-ledger-v2//file)", + "CKBTC_IC_ICRC1_LEDGER_V2_NOLEDGERLEVRION_VERSION_WASM_PATH": "$(rootpath @mainnet_ckbtc_ic-icrc1-ledger-v2-noledgerversion//file)", "CKETH_IC_ICRC1_LEDGER_DEPLOYED_VERSION_WASM_PATH": "$(rootpath @mainnet_cketh_ic-icrc1-ledger-u256//file)", "CKETH_IC_ICRC1_LEDGER_V1_VERSION_WASM_PATH": "$(rootpath @mainnet_cketh_ic-icrc1-ledger-u256-v1//file)", "CKETH_IC_ICRC1_LEDGER_V2_VERSION_WASM_PATH": "$(rootpath @mainnet_cketh_ic-icrc1-ledger-u256-v2//file)", + "CKETH_IC_ICRC1_LEDGER_V2_NOLEDGERLEVRION_VERSION_WASM_PATH": "$(rootpath @mainnet_cketh_ic-icrc1-ledger-u256-v2-noledgerversion//file)", "IC_ICRC1_ARCHIVE_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/archive:archive_canister" + name_suffix + ".wasm.gz)", "IC_ICRC1_LEDGER_DEPLOYED_VERSION_WASM_PATH": "$(rootpath @mainnet_ic-icrc1-ledger//file)", "IC_ICRC1_LEDGER_ICRC3_COMPATIBLE_DATA_CERTIFICATE_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/ledger:ledger_canister_icrc3_compatible_data_certificate)", diff --git a/rs/ledger_suite/icrc1/ledger/src/lib.rs b/rs/ledger_suite/icrc1/ledger/src/lib.rs index ee7d2fc00bf..03694b9b853 100644 --- a/rs/ledger_suite/icrc1/ledger/src/lib.rs +++ b/rs/ledger_suite/icrc1/ledger/src/lib.rs @@ -582,14 +582,10 @@ pub struct Ledger { #[serde(default)] accounts_overflow_trim_quantity: usize, - #[serde(default = "default_ledger_version")] + #[serde(default)] pub ledger_version: u64, } -fn default_ledger_version() -> u64 { - LEDGER_VERSION -} - #[derive(Clone, Eq, PartialEq, Debug, CandidType, Deserialize, Serialize)] pub struct FeatureFlags { pub icrc2: bool, diff --git a/rs/ledger_suite/icrc1/ledger/tests/tests.rs b/rs/ledger_suite/icrc1/ledger/tests/tests.rs index e3b0fee8020..ed6c69af51d 100644 --- a/rs/ledger_suite/icrc1/ledger/tests/tests.rs +++ b/rs/ledger_suite/icrc1/ledger/tests/tests.rs @@ -85,6 +85,14 @@ fn ledger_mainnet_v2_wasm() -> Vec { mainnet_wasm } +fn ledger_mainnet_v2_noledgerversion_wasm() -> Vec { + #[cfg(not(feature = "u256-tokens"))] + let mainnet_wasm = ledger_mainnet_v2_noledgerversion_u64_wasm(); + #[cfg(feature = "u256-tokens")] + let mainnet_wasm = ledger_mainnet_v2_noledgerversion_u256_wasm(); + mainnet_wasm +} + fn ledger_mainnet_v1_wasm() -> Vec { #[cfg(not(feature = "u256-tokens"))] let mainnet_wasm = ledger_mainnet_v1_u64_wasm(); @@ -103,6 +111,14 @@ fn ledger_mainnet_v2_u64_wasm() -> Vec { std::fs::read(std::env::var("CKBTC_IC_ICRC1_LEDGER_V2_VERSION_WASM_PATH").unwrap()).unwrap() } +#[cfg(not(feature = "u256-tokens"))] +fn ledger_mainnet_v2_noledgerversion_u64_wasm() -> Vec { + std::fs::read( + std::env::var("CKBTC_IC_ICRC1_LEDGER_V2_NOLEDGERLEVRION_VERSION_WASM_PATH").unwrap(), + ) + .unwrap() +} + #[cfg(not(feature = "u256-tokens"))] fn ledger_mainnet_v1_u64_wasm() -> Vec { std::fs::read(std::env::var("CKBTC_IC_ICRC1_LEDGER_V1_VERSION_WASM_PATH").unwrap()).unwrap() @@ -118,6 +134,14 @@ fn ledger_mainnet_v2_u256_wasm() -> Vec { std::fs::read(std::env::var("CKETH_IC_ICRC1_LEDGER_V2_VERSION_WASM_PATH").unwrap()).unwrap() } +#[cfg(feature = "u256-tokens")] +fn ledger_mainnet_v2_noledgerversion_u256_wasm() -> Vec { + std::fs::read( + std::env::var("CKETH_IC_ICRC1_LEDGER_V2_NOLEDGERLEVRION_VERSION_WASM_PATH").unwrap(), + ) + .unwrap() +} + #[cfg(feature = "u256-tokens")] fn ledger_mainnet_v1_u256_wasm() -> Vec { std::fs::read(std::env::var("CKETH_IC_ICRC1_LEDGER_V1_VERSION_WASM_PATH").unwrap()).unwrap() @@ -512,6 +536,15 @@ fn icrc1_test_multi_step_migration_from_v2() { ); } +#[test] +fn icrc1_test_multi_step_migration_from_v2_noledgerversion() { + ic_ledger_suite_state_machine_tests::icrc1_test_multi_step_migration( + ledger_mainnet_v2_noledgerversion_wasm(), + ledger_wasm_lowupgradeinstructionlimits(), + encode_init_args, + ); +} + #[test] fn icrc1_test_downgrade_from_incompatible_version() { ic_ledger_suite_state_machine_tests::test_downgrade_from_incompatible_version( @@ -561,6 +594,15 @@ fn icrc1_test_incomplete_migration_from_v2() { ); } +#[test] +fn icrc1_test_incomplete_migration_from_v2_noledgerversion() { + ic_ledger_suite_state_machine_tests::test_incomplete_migration( + ledger_mainnet_v2_noledgerversion_wasm(), + ledger_wasm_lowupgradeinstructionlimits(), + encode_init_args, + ); +} + #[test] fn icrc1_test_incomplete_migration_to_current_from_mainnet() { ic_ledger_suite_state_machine_tests::test_incomplete_migration_to_current( @@ -579,6 +621,15 @@ fn icrc1_test_incomplete_migration_to_current_from_v2() { ); } +#[test] +fn icrc1_test_incomplete_migration_to_current_from_v2_noledgerversion() { + ic_ledger_suite_state_machine_tests::test_incomplete_migration_to_current( + ledger_mainnet_v2_noledgerversion_wasm(), + ledger_wasm_lowupgradeinstructionlimits(), + encode_init_args, + ); +} + #[test] fn icrc1_test_migration_resumes_from_frozen_from_mainnet() { ic_ledger_suite_state_machine_tests::test_migration_resumes_from_frozen( From e6bebbf663b81ec7444af527221facd9963f966e Mon Sep 17 00:00:00 2001 From: Maciej Modelski Date: Mon, 20 Jan 2025 13:45:08 +0000 Subject: [PATCH 2/3] buildifier fix --- rs/ledger_suite/icrc1/ledger/BUILD.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rs/ledger_suite/icrc1/ledger/BUILD.bazel b/rs/ledger_suite/icrc1/ledger/BUILD.bazel index c39ebaff775..b526dcdad90 100644 --- a/rs/ledger_suite/icrc1/ledger/BUILD.bazel +++ b/rs/ledger_suite/icrc1/ledger/BUILD.bazel @@ -279,12 +279,12 @@ rust_test( "//rs/ledger_suite/icrc1/ledger:ledger_canister_icrc3_compatible_data_certificate", "//rs/universal_canister/impl:universal_canister.wasm.gz", "@mainnet_ckbtc_ic-icrc1-ledger-v1//file", - "@mainnet_ckbtc_ic-icrc1-ledger-v2//file", "@mainnet_ckbtc_ic-icrc1-ledger-v2-noledgerversion//file", + "@mainnet_ckbtc_ic-icrc1-ledger-v2//file", "@mainnet_ckbtc_ic-icrc1-ledger//file", "@mainnet_cketh_ic-icrc1-ledger-u256-v1//file", - "@mainnet_cketh_ic-icrc1-ledger-u256-v2//file", "@mainnet_cketh_ic-icrc1-ledger-u256-v2-noledgerversion//file", + "@mainnet_cketh_ic-icrc1-ledger-u256-v2//file", "@mainnet_cketh_ic-icrc1-ledger-u256//file", "@mainnet_ic-icrc1-ledger//file", ], From 074f7b90cd2bf6601a5e67f921ac6413fd68b650 Mon Sep 17 00:00:00 2001 From: Maciej Modelski Date: Mon, 20 Jan 2025 14:59:32 +0000 Subject: [PATCH 3/3] clean up heap approvals and balances --- rs/ledger_suite/common/ledger_core/src/approvals.rs | 8 ++++++++ rs/ledger_suite/icp/ledger/src/lib.rs | 4 ++++ rs/ledger_suite/icrc1/ledger/src/lib.rs | 9 +++++++++ rs/ledger_suite/icrc1/ledger/src/main.rs | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/rs/ledger_suite/common/ledger_core/src/approvals.rs b/rs/ledger_suite/common/ledger_core/src/approvals.rs index ebb3f5b03be..c7d531e65cc 100644 --- a/rs/ledger_suite/common/ledger_core/src/approvals.rs +++ b/rs/ledger_suite/common/ledger_core/src/approvals.rs @@ -66,6 +66,8 @@ pub trait AllowancesData { fn len_expirations(&self) -> usize; fn clear_arrivals(&mut self); + + fn clear_all(&mut self); } #[derive(Debug, Deserialize, Serialize)] @@ -159,6 +161,12 @@ where fn clear_arrivals(&mut self) { self.arrival_queue.clear(); } + + fn clear_all(&mut self) { + self.allowances.clear(); + self.expiration_queue.clear(); + self.arrival_queue.clear(); + } } #[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)] diff --git a/rs/ledger_suite/icp/ledger/src/lib.rs b/rs/ledger_suite/icp/ledger/src/lib.rs index d401537dd74..a842135b014 100644 --- a/rs/ledger_suite/icp/ledger/src/lib.rs +++ b/rs/ledger_suite/icp/ledger/src/lib.rs @@ -732,4 +732,8 @@ impl AllowancesData for StableAllowancesData { fn clear_arrivals(&mut self) { panic!("The method `clear_arrivals` should not be called for StableAllowancesData") } + + fn clear_all(&mut self) { + panic!("The method `clear_all` should not be called for StableAllowancesData") + } } diff --git a/rs/ledger_suite/icrc1/ledger/src/lib.rs b/rs/ledger_suite/icrc1/ledger/src/lib.rs index 03694b9b853..e47d995410f 100644 --- a/rs/ledger_suite/icrc1/ledger/src/lib.rs +++ b/rs/ledger_suite/icrc1/ledger/src/lib.rs @@ -727,6 +727,11 @@ impl Ledger { pub fn copy_token_pool(&mut self) { self.stable_balances.token_pool = self.balances.token_pool; } + + pub fn clear_heap_approvals_and_balances(&mut self) { + self.approvals.allowances_data.clear_all(); + self.balances.store.clear(); + } } impl LedgerContext for Ledger { @@ -1257,6 +1262,10 @@ impl AllowancesData for StableAllowancesData { fn clear_arrivals(&mut self) { panic!("The method `clear_arrivals` should not be called for StableAllowancesData") } + + fn clear_all(&mut self) { + panic!("The method `clear_all` should not be called for StableAllowancesData") + } } #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] diff --git a/rs/ledger_suite/icrc1/ledger/src/main.rs b/rs/ledger_suite/icrc1/ledger/src/main.rs index 154819b4249..3d893685abc 100644 --- a/rs/ledger_suite/icrc1/ledger/src/main.rs +++ b/rs/ledger_suite/icrc1/ledger/src/main.rs @@ -259,6 +259,10 @@ fn post_upgrade(args: Option) { migrate_next_part( MAX_INSTRUCTIONS_PER_UPGRADE.saturating_sub(pre_upgrade_instructions_consumed), ); + } else { + Access::with_ledger_mut(|ledger| { + ledger.clear_heap_approvals_and_balances(); + }); } let end = ic_cdk::api::instruction_counter();