|
| 1 | +use candid::Principal; |
| 2 | +use ic_base_types::PrincipalId; |
| 3 | +use ic_crypto_sha2::Sha256; |
| 4 | +use ic_nervous_system_common_test_utils::wasm_helpers::SMALLEST_VALID_WASM_BYTES; |
| 5 | +use ic_nervous_system_integration_tests::pocket_ic_helpers::{NnsInstaller, nns}; |
| 6 | +use ic_nns_constants::ROOT_CANISTER_ID; |
| 7 | +use ic_nns_governance_api::{ |
| 8 | + CreateCanisterAndInstallCodeRequest, MakeProposalRequest, ProposalActionRequest, |
| 9 | + SuccessfulProposalExecutionValue, WasmModule, |
| 10 | +}; |
| 11 | +use pocket_ic::PocketIcBuilder; |
| 12 | + |
| 13 | +/// Verifies that a CreateCanisterAndInstallCode proposal can be submitted, |
| 14 | +/// adopted, and executed, and that the new canister appears on the target |
| 15 | +/// subnet with the expected WASM module installed and the expected controller. |
| 16 | +#[tokio::test] |
| 17 | +async fn test_create_canister_and_install_code() { |
| 18 | + // Step 1: Prepare the world. |
| 19 | + |
| 20 | + // Step 1.1: Set up PocketIC with NNS + SNS (required by NnsInstaller) + |
| 21 | + // system subnet. |
| 22 | + let pocket_ic = PocketIcBuilder::new() |
| 23 | + .with_nns_subnet() |
| 24 | + // Even though this test has nothing to do with SNS, we do this, because |
| 25 | + // NnsInstaller panics without an SNS subnet. |
| 26 | + .with_sns_subnet() |
| 27 | + .with_system_subnet() |
| 28 | + .build_async() |
| 29 | + .await; |
| 30 | + |
| 31 | + // Step 1.2: Install NNS canisters with the test governance canister |
| 32 | + // (which has the CreateCanisterAndInstallCode feature flag enabled). |
| 33 | + { |
| 34 | + let mut nns_installer = NnsInstaller::default(); |
| 35 | + nns_installer.with_current_nns_canister_versions(); |
| 36 | + nns_installer.with_test_governance_canister(); |
| 37 | + nns_installer.install(&pocket_ic).await; |
| 38 | + } |
| 39 | + |
| 40 | + // Step 2: Call the code under test. |
| 41 | + |
| 42 | + // Step 2.1: Get the system subnet ID to use as host_subnet_id. |
| 43 | + let topology = pocket_ic.topology().await; |
| 44 | + let system_subnets = topology.get_system_subnets(); |
| 45 | + let host_subnet_id = PrincipalId::from(*system_subnets.first().unwrap()); |
| 46 | + |
| 47 | + // Step 2.2: Prepare the WASM and compute its expected hash. |
| 48 | + let wasm_module_bytes = SMALLEST_VALID_WASM_BYTES; |
| 49 | + let expected_module_hash = Sha256::hash(wasm_module_bytes).to_vec(); |
| 50 | + |
| 51 | + // Step 2.3: Execute proposal. |
| 52 | + let proposal_info = nns::governance::propose_and_wait( |
| 53 | + &pocket_ic, |
| 54 | + MakeProposalRequest { |
| 55 | + title: Some("Create canister and install code".to_string()), |
| 56 | + summary: "Integration test".to_string(), |
| 57 | + url: "".to_string(), |
| 58 | + action: Some(ProposalActionRequest::CreateCanisterAndInstallCode( |
| 59 | + CreateCanisterAndInstallCodeRequest { |
| 60 | + host_subnet_id: Some(host_subnet_id), |
| 61 | + canister_settings: None, |
| 62 | + wasm_module: Some(WasmModule::Inlined(wasm_module_bytes.to_vec())), |
| 63 | + install_arg: None, |
| 64 | + }, |
| 65 | + )), |
| 66 | + }, |
| 67 | + ) |
| 68 | + .await |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + // Step 3: Verify results. |
| 72 | + |
| 73 | + // Step 3.1: Inspect timestamp fields. |
| 74 | + assert_eq!( |
| 75 | + proposal_info.failure_reason, None, |
| 76 | + "Proposal failed: {:?}", |
| 77 | + proposal_info.failure_reason |
| 78 | + ); |
| 79 | + assert!( |
| 80 | + proposal_info.executed_timestamp_seconds > 0, |
| 81 | + "Proposal was not executed" |
| 82 | + ); |
| 83 | + |
| 84 | + // Step 3.2: Get the canister ID from proposal's success_value. |
| 85 | + let canister_id: PrincipalId = match proposal_info.success_value { |
| 86 | + Some(SuccessfulProposalExecutionValue::CreateCanisterAndInstallCode(ok)) => { |
| 87 | + ok.canister_id.unwrap() |
| 88 | + } |
| 89 | + wrong => panic!( |
| 90 | + "Expected CreateCanisterAndInstallCode success_value, got: {:?}", |
| 91 | + wrong |
| 92 | + ), |
| 93 | + }; |
| 94 | + |
| 95 | + // Step 3.3: Verify the canister lives on the expected subnet. |
| 96 | + let canister_subnet = pocket_ic |
| 97 | + .get_subnet(Principal::from(canister_id)) |
| 98 | + .await |
| 99 | + .unwrap(); |
| 100 | + assert_eq!( |
| 101 | + PrincipalId::from(canister_subnet), |
| 102 | + host_subnet_id, |
| 103 | + "Canister is on the wrong subnet" |
| 104 | + ); |
| 105 | + |
| 106 | + // Step 3.4: Verify the canister has specified code. |
| 107 | + let root_principal = Principal::from(PrincipalId::from(ROOT_CANISTER_ID)); |
| 108 | + let status = pocket_ic |
| 109 | + .canister_status(Principal::from(canister_id), Some(root_principal)) |
| 110 | + .await |
| 111 | + .unwrap(); |
| 112 | + |
| 113 | + assert_eq!( |
| 114 | + status.module_hash, |
| 115 | + Some(expected_module_hash), |
| 116 | + "WASM module hash mismatch" |
| 117 | + ); |
| 118 | + |
| 119 | + // Step 3.5: Verify that root controls the created canister. |
| 120 | + assert!( |
| 121 | + status.settings.controllers.contains(&root_principal), |
| 122 | + "Root should be a controller. Controllers: {:?}", |
| 123 | + status.settings.controllers, |
| 124 | + ); |
| 125 | +} |
0 commit comments