|
| 1 | +use ic_base_types::CanisterId; |
| 2 | +use ic_management_canister_types_private::{CanisterSnapshotResponse, ListCanisterSnapshotArgs}; |
| 3 | +use ic_nns_constants::{LEDGER_CANISTER_ID, ROOT_CANISTER_ID}; |
| 4 | +use ic_nns_governance::pb::v1::ProposalStatus; |
| 5 | +use ic_nns_governance_api::{ |
| 6 | + MakeProposalRequest, ProposalActionRequest, TakeCanisterSnapshot, |
| 7 | + manage_neuron_response::Command, |
| 8 | +}; |
| 9 | +use ic_nns_test_utils::{ |
| 10 | + common::NnsInitPayloadsBuilder, |
| 11 | + neuron_helpers::get_neuron_1, |
| 12 | + state_test_helpers::{ |
| 13 | + nns_governance_get_proposal_info_as_anonymous, nns_governance_make_proposal, |
| 14 | + nns_wait_for_proposal_execution, setup_nns_canisters, state_machine_builder_for_nns_tests, |
| 15 | + update_with_sender, |
| 16 | + }, |
| 17 | +}; |
| 18 | +use std::time::{Duration, SystemTime}; |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn test_canister_snapshot() { |
| 22 | + // Step 1: Prepare the world: Set up the NNS canisters with a super powerful neuron. |
| 23 | + let state_machine = state_machine_builder_for_nns_tests().build(); |
| 24 | + let nns_init_payloads = NnsInitPayloadsBuilder::new().with_test_neurons().build(); |
| 25 | + setup_nns_canisters(&state_machine, nns_init_payloads); |
| 26 | + |
| 27 | + // Basic facts. |
| 28 | + let neuron = get_neuron_1(); |
| 29 | + let target_canister_id = LEDGER_CANISTER_ID; |
| 30 | + |
| 31 | + // Scenario A: The most basic thing: take a snapshot. |
| 32 | + |
| 33 | + // Step 2(A): Run the code under test: Take a snapshot via proposal. |
| 34 | + |
| 35 | + // Step 2(A).1: Assemble MakeProposalRequest. |
| 36 | + let take_snapshot = TakeCanisterSnapshot { |
| 37 | + canister_id: Some(target_canister_id.get()), |
| 38 | + replace_snapshot: None, |
| 39 | + }; |
| 40 | + let action = ProposalActionRequest::TakeCanisterSnapshot(take_snapshot); |
| 41 | + let make_proposal_request = MakeProposalRequest { |
| 42 | + title: Some("Take a Snapshot of the Ledger Canister".to_string()), |
| 43 | + summary: "Do what the title says.".to_string(), |
| 44 | + url: "https://forum.dfinity.org/discuss-take-canister-snapshot".to_string(), |
| 45 | + action: Some(action), |
| 46 | + }; |
| 47 | + |
| 48 | + // Step 2A.2: Submit the proposal. |
| 49 | + let make_proposal_response = nns_governance_make_proposal( |
| 50 | + &state_machine, |
| 51 | + neuron.principal_id, |
| 52 | + neuron.neuron_id, |
| 53 | + &make_proposal_request, |
| 54 | + ); |
| 55 | + let first_proposal_id = match make_proposal_response.command.as_ref().unwrap() { |
| 56 | + Command::MakeProposal(response) => response.proposal_id.unwrap(), |
| 57 | + _ => panic!("{make_proposal_response:#?}"), |
| 58 | + }; |
| 59 | + |
| 60 | + // Step 2A.3: Wait for execution. |
| 61 | + nns_wait_for_proposal_execution(&state_machine, first_proposal_id.id); |
| 62 | + |
| 63 | + // Step 3A. Verify results. |
| 64 | + |
| 65 | + // Step 3A.1: Proposal marked success. |
| 66 | + let first_proposal_info = |
| 67 | + nns_governance_get_proposal_info_as_anonymous(&state_machine, first_proposal_id.id); |
| 68 | + assert_eq!( |
| 69 | + ProposalStatus::try_from(first_proposal_info.status), |
| 70 | + Ok(ProposalStatus::Executed), |
| 71 | + "{first_proposal_info:#?}", |
| 72 | + ); |
| 73 | + |
| 74 | + // Step 3A.2: Verify that a snapshot was created, by calling |
| 75 | + // list_canister_snapshots. |
| 76 | + let list_canister_snapshots_response: Vec<CanisterSnapshotResponse> = update_with_sender( |
| 77 | + &state_machine, |
| 78 | + CanisterId::ic_00(), |
| 79 | + "list_canister_snapshots", |
| 80 | + ListCanisterSnapshotArgs::new(target_canister_id), |
| 81 | + // Must call as the Root canister, because only controllers are allowed |
| 82 | + // to list snapshots (and Root is a controller of Ledger, the target |
| 83 | + // canister, the canister being snapshotted). |
| 84 | + ROOT_CANISTER_ID.get(), |
| 85 | + ) |
| 86 | + .expect("Failed to list snapshots"); |
| 87 | + |
| 88 | + assert_eq!( |
| 89 | + list_canister_snapshots_response.len(), |
| 90 | + 1, |
| 91 | + "{list_canister_snapshots_response:#?}" |
| 92 | + ); |
| 93 | + |
| 94 | + let first_snapshot = &list_canister_snapshots_response[0]; |
| 95 | + #[track_caller] |
| 96 | + fn assert_snapshot_seems_reasonable(snapshot: &CanisterSnapshotResponse) { |
| 97 | + let CanisterSnapshotResponse { |
| 98 | + id, |
| 99 | + total_size, |
| 100 | + taken_at_timestamp, |
| 101 | + } = snapshot.clone(); |
| 102 | + |
| 103 | + // Snapshot belongs to the Ledger canister. |
| 104 | + assert_eq!( |
| 105 | + id.get_canister_id(), |
| 106 | + LEDGER_CANISTER_ID, |
| 107 | + "Snapshot target is not Ledger: {snapshot:#?}" |
| 108 | + ); |
| 109 | + |
| 110 | + // Size is "reasonable". |
| 111 | + assert!( |
| 112 | + total_size > 10_000_000, |
| 113 | + "Snapshot is too small: {snapshot:#?}" |
| 114 | + ); |
| 115 | + assert!( |
| 116 | + total_size < 100_000_000, |
| 117 | + "Snapshot is too large: {snapshot:#?}" |
| 118 | + ); |
| 119 | + |
| 120 | + // Is recent. |
| 121 | + let taken_at = SystemTime::UNIX_EPOCH |
| 122 | + .checked_add(Duration::from_nanos(taken_at_timestamp)) |
| 123 | + .unwrap(); |
| 124 | + let age = SystemTime::now().duration_since(taken_at).unwrap(); |
| 125 | + assert!( |
| 126 | + age < Duration::from_secs(5 * 60), |
| 127 | + "Snapshot is more than 5 min. old: {snapshot:#?}" |
| 128 | + ); |
| 129 | + } |
| 130 | + assert_snapshot_seems_reasonable(first_snapshot); |
| 131 | + |
| 132 | + // Scenario B: Replace an existing snapshot. |
| 133 | + |
| 134 | + // Step 2(B): Run code under test. |
| 135 | + |
| 136 | + // Make a proposal, like in scenario A, but this time, set the |
| 137 | + // replace_snapshot field to the ID of the first snapshot (from scenario A). |
| 138 | + let take_snapshot_replace = TakeCanisterSnapshot { |
| 139 | + canister_id: Some(target_canister_id.get()), |
| 140 | + replace_snapshot: Some(first_snapshot.snapshot_id().to_vec()), |
| 141 | + }; |
| 142 | + let action = ProposalActionRequest::TakeCanisterSnapshot(take_snapshot_replace); |
| 143 | + let make_proposal_request = MakeProposalRequest { |
| 144 | + title: Some("Take ANOTHER Snapshot and clobber the first".to_string()), |
| 145 | + summary: "Delete old, make new.".to_string(), |
| 146 | + url: "https://forum.dfinity.org/clobber-snapshot".to_string(), |
| 147 | + action: Some(action), |
| 148 | + }; |
| 149 | + let propose_replace_response = nns_governance_make_proposal( |
| 150 | + &state_machine, |
| 151 | + neuron.principal_id, |
| 152 | + neuron.neuron_id, |
| 153 | + &make_proposal_request, |
| 154 | + ); |
| 155 | + let replace_proposal_id = match propose_replace_response.command.unwrap() { |
| 156 | + Command::MakeProposal(response) => response.proposal_id.unwrap(), |
| 157 | + _ => panic!("Propose replace didn't return MakeProposal"), |
| 158 | + }; |
| 159 | + |
| 160 | + assert_ne!(replace_proposal_id, first_proposal_id); |
| 161 | + nns_wait_for_proposal_execution(&state_machine, replace_proposal_id.id); |
| 162 | + |
| 163 | + let replace_proposal_info = |
| 164 | + nns_governance_get_proposal_info_as_anonymous(&state_machine, replace_proposal_id.id); |
| 165 | + assert_eq!( |
| 166 | + ProposalStatus::try_from(replace_proposal_info.status), |
| 167 | + Ok(ProposalStatus::Executed), |
| 168 | + "{replace_proposal_info:#?}", |
| 169 | + ); |
| 170 | + |
| 171 | + // Step 3(B): Verify results. |
| 172 | + |
| 173 | + // Fetch (new) snapshots. |
| 174 | + let list_canister_snapshots_response: Vec<CanisterSnapshotResponse> = update_with_sender( |
| 175 | + &state_machine, |
| 176 | + CanisterId::ic_00(), |
| 177 | + "list_canister_snapshots", |
| 178 | + ListCanisterSnapshotArgs::new(target_canister_id), |
| 179 | + ROOT_CANISTER_ID.get(), |
| 180 | + ) |
| 181 | + .expect("Failed to list snapshots after replace"); |
| 182 | + |
| 183 | + // There should only be 1 snapshot, because even though we have a new |
| 184 | + // (second) snapshot, the existing (first) one is supposed to be replaced by |
| 185 | + // the new one. |
| 186 | + assert_eq!( |
| 187 | + list_canister_snapshots_response.len(), |
| 188 | + 1, |
| 189 | + "{list_canister_snapshots_response:#?}" |
| 190 | + ); |
| 191 | + |
| 192 | + // More interestingly, the one snapshot should NOT be the first one. The |
| 193 | + // first one should be CLOBBERED, blown away, replaced by the new one. |
| 194 | + let second_snapshot = &list_canister_snapshots_response[0]; |
| 195 | + assert_ne!( |
| 196 | + second_snapshot.snapshot_id(), |
| 197 | + first_snapshot.snapshot_id(), |
| 198 | + "Snapshot ID should have changed" |
| 199 | + ); |
| 200 | + |
| 201 | + // Generic checks of the second snapshot. |
| 202 | + assert_snapshot_seems_reasonable(second_snapshot); |
| 203 | +} |
0 commit comments