async fn install_stable_memory_reader(
    runtime: &'_ Runtime,
    mode: CanisterInstallMode,
    stop_before_installing: bool,
) {
    let root = set_up_root_canister(runtime, RootCanisterInitPayload {}).await;

    // Install the universal canister in place of the proposals canister
    let fake_proposal_canister = set_up_universal_canister(runtime).await;
    // Since it takes the id reserved for the proposal canister, it can impersonate
    // it
    assert_eq!(
        fake_proposal_canister.canister_id(),
        ic_nns_constants::GOVERNANCE_CANISTER_ID
    );

    // Create some NNS canister to be owned by the root
    let universal = set_up_universal_canister(runtime).await;
    universal
        .set_controller(root.canister_id().get())
        .await
        .unwrap();

    // Let's record something in stable memory
    universal
        .update_(
            "update",
            bytes,
            universal_canister_argument_builder()
                .stable_grow(1)
                .stable_write(0, MSG)
                .reply()
                .build(),
        )
        .await
        .unwrap();

    let proposal_payload = ChangeNnsCanisterProposalPayload::new(
        stop_before_installing,
        mode,
        universal.canister_id(),
    )
    .with_wasm(STABLE_MEMORY_READER_WASM.clone());

    // The upgrade should work
    assert!(
        forward_call_via_universal_canister(
            &fake_proposal_canister,
            &root,
            "change_nns_canister",
            Encode!(&proposal_payload).unwrap()
        )
        .await
    );

    // Now let's wait for the upgrade to complete
    loop {
        let status: CanisterStatusResult = root
            .update_(
                "canister_status",
                candid,
                (CanisterIdRecord::from(universal.canister_id()),),
            )
            .await
            .unwrap();
        if status.module_hash.unwrap() == *STABLE_MEMORY_READER_SHA256 && status.status == Running {
            break;
        }
    }

    match mode {
        Install => panic!("There should be a test for Install"),
        Upgrade =>
        // The stable memory should have ben preserved
        {
            assert_eq!(
                universal
                    .query_("read_10_bytes_from_stable", bytes, Vec::new())
                    .await
                    .unwrap(),
                &MSG[..10],
            )
        }
        Reinstall => {
            // The stable memory should have been wiped out: trying to read it should fail
            let res: Result<Vec<u8>, String> = universal
                .query_("read_10_bytes_from_stable", bytes, Vec::new())
                .await;
            assert_matches ! (res, Err(msg) if msg.to_lowercase().contains("out of bounds"));
        }
    }
}
/// Test template in which we try to reinstall or upgrade an NNS canister
/// to the canister defined by `STABLE_MEMORY_READER_WAT`.
///
/// The reinstall/upgrade should work, and we should be able to retrieve the
/// stable memory if it was an upgrade.
Test template in which we try to reinstall or upgrade an NNS canister
to the canister defined by `STABLE_MEMORY_READER_WAT`.

The reinstall/upgrade should work, and we should be able to retrieve the
stable memory if it was an upgrade.
Test template in which we try to reinstall or upgrade an NNS canister to the canister defined by ` STABLE_MEMORY_READER_WAT ` . The reinstall / upgrade should work and we should be able to retrieve the stable memory if it was an upgrade .
async fn install_stable_memory_reader(
    runtime: &'_ Runtime,
    mode: CanisterInstallMode,
    stop_before_installing: bool,
) {
    let root = set_up_root_canister(runtime, RootCanisterInitPayload {}).await;
    // Install the universal canister in place of the proposals canister
    let fake_proposal_canister = set_up_universal_canister(runtime).await;
    // Since it takes the id reserved for the proposal canister, it can impersonate
    // it
    assert_eq!(
        fake_proposal_canister.canister_id(),
        ic_nns_constants::GOVERNANCE_CANISTER_ID
    );
    // Create some NNS canister to be owned by the root
    let universal = set_up_universal_canister(runtime).await;
    universal
        .set_controller(root.canister_id().get())
        .await
        .unwrap();
    // Let's record something in stable memory
    universal
        .update_(
            "update",
            bytes,
            universal_canister_argument_builder()
                .stable_grow(1)
                .stable_write(0, MSG)
                .reply()
                .build(),
        )
        .await
        .unwrap();
    let proposal_payload = ChangeNnsCanisterProposalPayload::new(
        stop_before_installing,
        mode,
        universal.canister_id(),
    )
    .with_wasm(STABLE_MEMORY_READER_WASM.clone());
    // The upgrade should work
    assert!(
        forward_call_via_universal_canister(
            &fake_proposal_canister,
            &root,
            "change_nns_canister",
            Encode!(&proposal_payload).unwrap()
        )
        .await
    );
    // Now let's wait for the upgrade to complete
    loop {
        let status: CanisterStatusResult = root
            .update_(
                "canister_status",
                candid,
                (CanisterIdRecord::from(universal.canister_id()),),
            )
            .await
            .unwrap();
        if status.module_hash.unwrap() == *STABLE_MEMORY_READER_SHA256 && status.status == Running {
            break;
        }
    }
    match mode {
        Install => panic!("There should be a test for Install"),
        Upgrade =>
        // The stable memory should have ben preserved
        {
            assert_eq!(
                universal
                    .query_("read_10_bytes_from_stable", bytes, Vec::new())
                    .await
                    .unwrap(),
                &MSG[..10],
            )
        }
        Reinstall => {
            // The stable memory should have been wiped out: trying to read it should fail
            let res: Result<Vec<u8>, String> = universal
                .query_("read_10_bytes_from_stable", bytes, Vec::new())
                .await;
            assert_matches ! (res, Err(msg) if msg.to_lowercase().contains("out of bounds"));
        }
    }
}
async fn install_stable_memory_reader ( runtime : & ' _ Runtime , mode : CanisterInstallMode , stop_before_installing : bool , ) { let root = set_up_root_canister ( runtime , RootCanisterInitPayload { } ) . await ; // Install the universal canister in place of the proposals canister let fake_proposal_canister = set_up_universal_canister ( runtime ) . await ; // Since it takes the id reserved for the proposal canister, it can impersonate // it assert_eq ! ( fake_proposal_canister canister_id ( ) ic_nns_constants GOVERNANCE_CANISTER_ID ) ; // Create some NNS canister to be owned by the root let universal = set_up_universal_canister ( runtime ) . await ; universal . set_controller ( root . canister_id ( ) . get ( ) ) . await . unwrap ( ) ; // Let's record something in stable memory universal . update_ ( " " , bytes , universal_canister_argument_builder ( ) . stable_grow ( 1 ) . stable_write ( 0 , MSG ) . reply ( ) . build ( ) , ) . await . unwrap ( ) ; let proposal_payload = ChangeNnsCanisterProposalPayload :: new ( stop_before_installing , mode , universal . canister_id ( ) , ) . with_wasm ( STABLE_MEMORY_READER_WASM . clone ( ) ) ; // The upgrade should work assert ! ( forward_call_via_universal_canister ( fake_proposal_canister root " " Encode ( proposal_payload ) unwrap ( ) ) await ) ; // Now let's wait for the upgrade to complete loop { let status : CanisterStatusResult = root . update_ ( " " , candid , ( CanisterIdRecord :: from ( universal . canister_id ( ) ) , ) , ) . await . unwrap ( ) ; if status . module_hash . unwrap ( ) == * STABLE_MEMORY_READER_SHA256 && status . status == Running { break ; } } match mode { Install => panic ! ( " " ) , Upgrade => // The stable memory should have ben preserved { assert_eq ! ( universal query_ ( " " bytes Vec new ( ) ) await unwrap ( ) MSG [ 10 ] ) } Reinstall => { // The stable memory should have been wiped out: trying to read it should fail let res : Result < Vec < u8 > , String > = universal . query_ ( " " , bytes , Vec :: new ( ) ) . await ; assert_matches ! ( res Err ( msg ) if msg to_lowercase ( ) contains ( " " ) ) ; } } }
Test template in which we try to reinstall or upgrade an NNS canister
to the canister defined by `STABLE_MEMORY_READER_WAT`.
Test template in which we try to reinstall or upgrade an NNS canister to the canister defined by ` STABLE_MEMORY_READER_WAT ` .
runtime
&'_ Runtime
mode
CanisterInstallMode
stop_before_installing
bool
runtime
&'_ Runtime
mode
CanisterInstallMode
stop_before_installing
bool