Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk committed Oct 25, 2024
1 parent 41ea758 commit 9f1ab39
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 76 deletions.
115 changes: 98 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ futures-util = "0.3.21"
hex = "0.4.3"
k256 = "0.13.4"
leb128 = "0.2.5"
pocket-ic = "5.0.0"
pocket-ic = { git = "https://github.com/dfinity/ic", rev = "3c76b9142f67da01393d9280c705f88b6e522a93" }
p256 = "0.13.2"
rand = "0.8.5"
reqwest = { version = "0.12", default-features = false }
Expand Down
20 changes: 5 additions & 15 deletions ic-utils/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,24 +395,14 @@ mod tests {
const POCKET_IC: &str = "POCKET_IC";

async fn get_effective_canister_id() -> Principal {
let default_effective_canister_id =
Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap();
if let Ok(pocket_ic_url) = std::env::var(POCKET_IC) {
let client = reqwest::Client::new();
let topology: pocket_ic::common::rest::Topology = client
.get(format!("{}{}", pocket_ic_url, "/_/topology"))
.send()
pocket_ic::nonblocking::get_default_effective_canister_id(pocket_ic_url)
.await
.unwrap()
.json()
.await
.unwrap();
let app_subnet = topology.get_app_subnets()[0];
Principal::from_slice(
&topology.0.get(&app_subnet).unwrap().canister_ranges[0]
.start
.canister_id,
)
.unwrap_or(default_effective_canister_id)
} else {
Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap()
default_effective_canister_id
}
}

Expand Down
32 changes: 4 additions & 28 deletions icx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,34 +352,10 @@ async fn main() -> Result<()> {
.build()
.context("Failed to build the Agent")?;

let get_default_effective_canister_id = || async {
let client = reqwest::Client::new();
let topology: pocket_ic::common::rest::Topology = client
.get(format!(
"{}{}",
opts.replica.trim_end_matches('/'),
"/_/topology"
))
.send()
.await?
.json()
.await?;
let subnet = topology.get_app_subnets().into_iter().next().unwrap_or_else(||
topology
.get_verified_app_subnets()
.into_iter()
.next()
.unwrap_or_else(|| topology.get_system_subnets().into_iter().next().unwrap_or_else(|| panic!("PocketIC topology contains no application, verified application, and system subnet."))),
);
Ok::<_, reqwest::Error>(Principal::from_slice(
&topology.0.get(&subnet).unwrap().canister_ranges[0]
.start
.canister_id,
))
};
let default_effective_canister_id = get_default_effective_canister_id()
.await
.unwrap_or(Principal::management_canister());
let default_effective_canister_id =
pocket_ic::nonblocking::get_default_effective_canister_id(opts.replica.clone())
.await
.unwrap_or(Principal::management_canister());

// You can handle information about subcommands by requesting their matches by name
// (as below), requesting just the name used, or both at the same time
Expand Down
20 changes: 5 additions & 15 deletions ref-tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@ const HSM_PIN: &str = "HSM_PIN";
const POCKET_IC: &str = "POCKET_IC";

pub async fn get_effective_canister_id() -> Principal {
let default_effective_canister_id =
Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap();
if let Ok(pocket_ic_url) = std::env::var(POCKET_IC) {
let client = reqwest::Client::new();
let topology: pocket_ic::common::rest::Topology = client
.get(format!("{}{}", pocket_ic_url, "/_/topology"))
.send()
pocket_ic::nonblocking::get_default_effective_canister_id(pocket_ic_url)
.await
.unwrap()
.json()
.await
.unwrap();
let app_subnet = topology.get_app_subnets()[0];
Principal::from_slice(
&topology.0.get(&app_subnet).unwrap().canister_ranges[0]
.start
.canister_id,
)
.unwrap_or(default_effective_canister_id)
} else {
Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap()
default_effective_canister_id
}
}

Expand Down

0 comments on commit 9f1ab39

Please sign in to comment.