Skip to content

Commit

Permalink
test(ICP_Ledger): FI-1595: Check migration metrics in ICP golden stat…
Browse files Browse the repository at this point in the history
…e test (#3146)

Check the metrics to verify that the migration to stable structures was
performed as expected in the ICP ledger golden state test.
  • Loading branch information
mbjorkqvist authored Dec 13, 2024
1 parent 395ebdf commit 7c6309c
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions rs/ledger_suite/icp/tests/golden_nns_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use ic_ledger_core::Tokens;
use ic_ledger_suite_state_machine_tests::in_memory_ledger::{
BlockConsumer, BurnsWithoutSpender, InMemoryLedger,
};
use ic_ledger_suite_state_machine_tests::{generate_transactions, TransactionGenerationParameters};
use ic_ledger_suite_state_machine_tests::metrics::{parse_metric, retrieve_metrics};
use ic_ledger_suite_state_machine_tests::{
generate_transactions, wait_ledger_ready, TransactionGenerationParameters,
};
use ic_ledger_test_utils::state_machine_helpers::index::{
get_all_blocks, wait_until_sync_is_completed,
};
Expand All @@ -25,6 +28,11 @@ use icp_ledger::{
};
use std::time::Instant;

/// The number of instructions that can be executed in a single canister upgrade.
/// The limit (<https://internetcomputer.org/docs/current/developer-docs/smart-contracts/maintain/resource-limits#resource-constraints-and-limits>)
/// is actually 300B, but in the ledger implementation we use a value slightly lower than the old
/// limit 200B.
const CANISTER_UPGRADE_INSTRUCTION_LIMIT: u64 = 190_000_000_000;
const INDEX_CANISTER_ID: CanisterId =
CanisterId::from_u64(LEDGER_INDEX_CANISTER_INDEX_IN_NNS_SUBNET);
const LEDGER_CANISTER_ID: CanisterId = CanisterId::from_u64(LEDGER_CANISTER_INDEX_IN_NNS_SUBNET);
Expand Down Expand Up @@ -239,9 +247,9 @@ fn should_create_state_machine_with_golden_nns_state() {
setup.perform_upgrade_downgrade_testing(false);

// Upgrade all the canisters to the latest version
setup.upgrade_to_master();
setup.upgrade_to_master(ExpectMigration::Yes);
// Upgrade again to test the pre-upgrade
setup.upgrade_to_master();
setup.upgrade_to_master(ExpectMigration::No);

// Perform upgrade and downgrade testing
setup.perform_upgrade_downgrade_testing(true);
Expand All @@ -267,6 +275,12 @@ struct Setup {
previous_ledger_state: Option<LedgerState>,
}

#[derive(Eq, PartialEq)]
enum ExpectMigration {
Yes,
No,
}

impl Setup {
pub fn new() -> Self {
let state_machine = new_state_machine_with_golden_nns_state_or_panic();
Expand All @@ -291,11 +305,15 @@ impl Setup {
}
}

pub fn upgrade_to_master(&self) {
pub fn upgrade_to_master(&self, expect_migration: ExpectMigration) {
println!("Upgrading to master version");
self.upgrade_index(&self.master_wasms.index);
self.upgrade_ledger(&self.master_wasms.ledger)
.expect("should successfully upgrade ledger to new local version");
if expect_migration == ExpectMigration::Yes {
wait_ledger_ready(&self.state_machine, LEDGER_CANISTER_ID, 100);
}
self.check_ledger_metrics(expect_migration);
self.upgrade_archive_canisters(&self.master_wasms.archive);
}

Expand All @@ -317,6 +335,7 @@ impl Setup {
);
}
}
self.check_ledger_metrics(ExpectMigration::No);
self.upgrade_archive_canisters(&self.mainnet_wasms.archive);
}

Expand All @@ -334,6 +353,37 @@ impl Setup {
));
}

fn check_ledger_metrics(&self, expect_migration: ExpectMigration) {
let metrics = retrieve_metrics(&self.state_machine, LEDGER_CANISTER_ID);
println!("Ledger metrics:");
for metric in metrics {
println!(" {}", metric);
}
if expect_migration == ExpectMigration::Yes {
let migration_steps = parse_metric(
&self.state_machine,
LEDGER_CANISTER_ID,
"ledger_stable_upgrade_migration_steps",
);
assert!(
migration_steps > 0u64,
"Migration steps ({}) should be greater than 0",
migration_steps
);
}
let upgrade_instructions = parse_metric(
&self.state_machine,
LEDGER_CANISTER_ID,
"ledger_total_upgrade_instructions_consumed",
);
assert!(
upgrade_instructions < CANISTER_UPGRADE_INSTRUCTION_LIMIT,
"Upgrade instructions ({}) should be less than the instruction limit ({})",
upgrade_instructions,
CANISTER_UPGRADE_INSTRUCTION_LIMIT
);
}

fn list_archives(&self) -> Archives {
Decode!(
&self
Expand Down

0 comments on commit 7c6309c

Please sign in to comment.