Skip to content

Commit

Permalink
fix(PocketIC): provisional canister creation with management effectiv…
Browse files Browse the repository at this point in the history
…e canister ID (#1571)

Canisters created via `provisional_create_canister_with_cycles` with the
management canister ID as the effective canister ID are created on an
arbitrary subnet. In particular, this enables creating canisters in a
PocketIC instance using `ic-repl`.
  • Loading branch information
mraszyk authored Sep 23, 2024
1 parent 89d768f commit 117ef9f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions rs/pocket_ic_server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ TEST_DEPENDENCIES = [
"@crate_index//:hex",
"@crate_index//:ic-agent",
"@crate_index//:ic-btc-interface",
"@crate_index//:ic-cdk",
"@crate_index//:ic-utils",
"@crate_index//:rcgen",
"@crate_index//:reqwest",
Expand Down
2 changes: 2 additions & 0 deletions rs/pocket_ic_server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Renamed `dfx_test_key1` tECDSA and tSchnorr keys to `dfx_test_key`.
- Canisters created via `provisional_create_canister_with_cycles` with the management canister ID as the effective canister ID
are created on an arbitrary subnet.



Expand Down
6 changes: 5 additions & 1 deletion rs/pocket_ic_server/src/pocket_ic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,11 @@ fn route(
EffectivePrincipal::CanisterId(canister_id) => match pic.try_route_canister(canister_id) {
Some(subnet) => Ok(subnet),
None => {
if is_provisional_create_canister {
// Canisters created via `provisional_create_canister_with_cycles` with the management canister ID as the effective canister ID
// are created on an arbitrary subnet.
if is_provisional_create_canister && canister_id == CanisterId::ic_00() {
Ok(pic.random_subnet())
} else if is_provisional_create_canister {
// We retrieve the PocketIC instace time (consistent across all subnets) from one subnet.
let time = pic.subnets.read().unwrap().values().next().unwrap().time();
// We create a new subnet with the IC mainnet configuration containing the effective canister ID.
Expand Down
26 changes: 25 additions & 1 deletion rs/pocket_ic_server/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod common;
use crate::common::raw_canister_id_range_into;
use candid::{Encode, Principal};
use ic_agent::agent::{http_transport::ReqwestTransport, CallResponse};
use ic_cdk::api::management_canister::main::CanisterIdRecord;
use ic_cdk::api::management_canister::provisional::ProvisionalCreateCanisterWithCyclesArgument;
use ic_interfaces_registry::{
RegistryDataProvider, RegistryVersionedRecord, ZERO_REGISTRY_VERSION,
};
Expand All @@ -16,7 +18,7 @@ use pocket_ic::common::rest::{
CreateHttpGatewayResponse, HttpGatewayBackend, HttpGatewayConfig, HttpGatewayDetails,
HttpsConfig, InstanceConfig, SubnetConfigSet,
};
use pocket_ic::{PocketIc, PocketIcBuilder, WasmResult};
use pocket_ic::{update_candid, PocketIc, PocketIcBuilder, WasmResult};
use rcgen::{CertificateParams, KeyPair};
use registry_canister::init::RegistryCanisterInitPayload;
use reqwest::blocking::Client;
Expand Down Expand Up @@ -1198,3 +1200,25 @@ fn registry_canister() {
None,
);
}

#[test]
fn provisional_create_canister_with_cycles() {
let pic = PocketIcBuilder::new()
.with_nns_subnet()
.with_application_subnet()
.build();

let arg = ProvisionalCreateCanisterWithCyclesArgument::default();
let res: (CanisterIdRecord,) = update_candid(
&pic,
Principal::management_canister(),
"provisional_create_canister_with_cycles",
(arg,),
)
.unwrap();
let canister_id = res.0.canister_id;

let topology = pic.topology();
let app_subnet = topology.get_app_subnets()[0];
assert_eq!(pic.get_subnet(canister_id).unwrap(), app_subnet);
}

0 comments on commit 117ef9f

Please sign in to comment.