We are trying to create canisters on a specific subnet using cycle minting canister. I am testing this scenario using pocket-ic.
Getting Error on creation of canisters using CMC canister:
Error: value: Refunded { refund_amount: 99900000000, create_error: \"Creating canister in subnet fbysm-3acaa-aaaaa-aaaap-yai failed with code 3: Unable to route management canister request create_canister: AlreadyResolved(fbysm-3acaa-aaaaa-aaaap-yai)\" }'
pub async fn provision_subnet_orchestrator_canister(subnet: Principal, user_index_wasm: Vec<u8>) -> Principal {
let create_canister_arg = CreateCanisterCmcArgument {
subnet_selection: Some(SubnetType::Subnet(Subnet {
subnet
})),
canister_settings: Some(CanisterSettings {
controllers: Some(vec![ api::id()]),
compute_allocation: None,
memory_allocation: None,
freezing_threshold: None,
}),
subnet_type: None
};
// Creates Canister using CMC Canister
let (res, ): (Result<Principal, CmcCreateCanisterError>, ) = call::call_with_payment(
Principal::from_str(NNS_CYCLE_MINTING_CANISTER).unwrap(),
"create_canister",
(create_canister_arg,),
INDIVIDUAL_USER_CANISTER_RECHARGE_AMOUNT as u64
)
.await
.unwrap();
let subnet_orchestrator_canister_id = res.unwrap();
// ... Rest of the Code
}
Test
let pocket_ic = PocketIcBuilder::new()
.with_nns_subnet()
.with_application_subnet()
.with_system_subnet()
.build();
let super_admin = get_global_super_admin_principal_id();
let application_subnets = pocket_ic.topology().get_app_subnets();
let platform_canister_id = pocket_ic.create_canister_with_settings(Some(super_admin), Some(CanisterSettings {controllers: Some(vec![super_admin]), compute_allocation: None, memory_allocation: None, freezing_threshold: None}));
pocket_ic.add_cycles(platform_canister_id, CANISTER_INITIAL_CYCLES_FOR_SPAWNING_CANISTERS);
let platform_orchestrator_wasm = include_bytes!("../../../../../target/wasm32-unknown-unknown/release/platform_orchestrator.wasm.gz");
let subnet_orchestrator_canister_wasm = include_bytes!("../../../../../target/wasm32-unknown-unknown/release/user_index.wasm.gz");
let platform_orchestrator_init_args = PlatformOrchestratorInitArgs {
version: "v1.0.0".into()
};
pocket_ic.install_canister(platform_canister_id, platform_orchestrator_wasm.into(), candid::encode_one(platform_orchestrator_init_args).unwrap(), Some(super_admin));
pocket_ic.add_cycles(platform_canister_id, CANISTER_INITIAL_CYCLES_FOR_SPAWNING_CANISTERS);
//Ledger Canister
let minting_account = AccountIdentifier::new(&super_admin, &DEFAULT_SUBACCOUNT);
let ledger_canister_wasm = include_bytes!("../../ledger-canister.wasm");
let ledger_canister_id = pocket_ic.create_canister_with_id(Some(super_admin), None, Principal::from_text(NNS_LEDGER_CANISTER_ID).unwrap()).unwrap();
let icp_ledger_init_args = NnsLedgerCanisterInitPayload {
minting_account: minting_account.to_string(),
initial_values: HashMap::new(),
send_whitelist: HashSet::new(),
transfer_fee: Some(Tokens::from_e8s(10_000)),
};
pocket_ic.install_canister(ledger_canister_id, ledger_canister_wasm.into(), candid::encode_one(icp_ledger_init_args).unwrap(), Some(super_admin));
//Cycle Minting Canister
let cycle_minting_canister_wasm = include_bytes!("../../cycles-minting-canister.wasm");
let cycle_minting_canister_id = pocket_ic.create_canister_with_id(Some(super_admin), None, Principal::from_text(NNS_CYCLE_MINTING_CANISTER).unwrap()).unwrap();
pocket_ic.add_cycles(cycle_minting_canister_id, CANISTER_INITIAL_CYCLES_FOR_SPAWNING_CANISTERS);
let cycles_minting_canister_init_args = CyclesMintingCanisterInitPayload {
ledger_canister_id: ledger_canister_id,
governance_canister_id: CanisterId::anonymous(),
minting_account_id: Some(minting_account.to_string()),
last_purged_notification: Some(0),
};
pocket_ic.install_canister(
cycle_minting_canister_id,
cycle_minting_canister_wasm.into(),
candid::encode_one(cycles_minting_canister_init_args).unwrap(),
Some(super_admin)
);
let authorized_subnetwork_list_args = AuthorizedSubnetWorks {
who: Some(platform_canister_id),
subnets: application_subnets.clone()
};
pocket_ic.update_call(
cycle_minting_canister_id,
CanisterId::anonymous(),
"set_authorized_subnetwork_list",
candid::encode_one(authorized_subnetwork_list_args).unwrap()
).unwrap();
for i in 0..50 {
pocket_ic.tick();
}
let subnet_orchestrator_canister_id: Principal = pocket_ic.update_call(
platform_canister_id,
super_admin,
"provision_subnet_orchestrator_canister",
candid::encode_args((application_subnets[0], subnet_orchestrator_canister_wasm)).unwrap()
)
.map(|res| {
let canister_id: Principal = match res {
WasmResult::Reply(payload) => candid::decode_one(&payload).unwrap(),
_ => panic!("Canister call failed")
};
canister_id
})
.unwrap();