Skip to content

Commit 0884eac

Browse files
feat: add read_state_subnet_canister_ranges (#661)
* Add 'read_state_subnet_canister_ranges' which can query the canister id ranges for a given subnet. * Updated changelog. * Fixed lint.
1 parent 8ebaf4d commit 0884eac

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## Unreleased
1010

11+
* Added `read_state_subnet_canister_ranges` which can query the canister id ranges for a given subnet.
12+
1113
## [0.44.0] - 2025-08-25
1214

1315
* BREAKING: Bump `ic-management-canister-types` to v0.4.0.

ic-agent/src/agent/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ mod agent_test;
4545
use crate::{
4646
agent::response_authentication::{
4747
extract_der, lookup_canister_info, lookup_canister_metadata, lookup_request_status,
48-
lookup_subnet, lookup_subnet_metrics, lookup_time, lookup_value,
48+
lookup_subnet, lookup_subnet_canister_ranges, lookup_subnet_metrics, lookup_time,
49+
lookup_value,
4950
},
5051
agent_error::TransportError,
5152
export::Principal,
@@ -1093,6 +1094,20 @@ impl Agent {
10931094
lookup_subnet_metrics(cert, subnet_id)
10941095
}
10951096

1097+
/// Request a list of metrics about the subnet.
1098+
pub async fn read_state_subnet_canister_ranges(
1099+
&self,
1100+
subnet_id: Principal,
1101+
) -> Result<Vec<(Principal, Principal)>, AgentError> {
1102+
let paths = vec![vec![
1103+
"subnet".into(),
1104+
Label::from_bytes(subnet_id.as_slice()),
1105+
"canister_ranges".into(),
1106+
]];
1107+
let cert = self.read_subnet_state_raw(paths, subnet_id).await?;
1108+
lookup_subnet_canister_ranges(cert, subnet_id)
1109+
}
1110+
10961111
/// Fetches the status of a particular request by its ID.
10971112
pub async fn request_status_raw(
10981113
&self,

ic-agent/src/agent/response_authentication.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ pub(crate) fn lookup_subnet_metrics<Storage: AsRef<[u8]>>(
7777
Ok(serde_cbor::from_slice(metrics)?)
7878
}
7979

80+
pub(crate) fn lookup_subnet_canister_ranges<Storage: AsRef<[u8]>>(
81+
certificate: Certificate<Storage>,
82+
subnet_id: Principal,
83+
) -> Result<Vec<(Principal, Principal)>, AgentError> {
84+
let path_ranges = [b"subnet", subnet_id.as_slice(), b"canister_ranges"];
85+
let ranges = lookup_value(&certificate.tree, path_ranges)?;
86+
Ok(serde_cbor::from_slice(ranges)?)
87+
}
88+
8089
pub(crate) fn lookup_request_status<Storage: AsRef<[u8]>>(
8190
certificate: &Certificate<Storage>,
8291
request_id: &RequestId,

ref-tests/tests/ic-ref.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,24 @@ mod management_canister {
938938
Ok(())
939939
})
940940
}
941+
942+
#[ignore]
943+
#[test]
944+
fn subnet_canister_ranges() {
945+
with_agent(|agent| async move {
946+
// fetch root subnet canister ranges
947+
let ranges = agent
948+
.read_state_subnet_canister_ranges(Principal::self_authenticating(
949+
agent.read_root_key(),
950+
))
951+
.await?;
952+
assert!(
953+
!ranges.is_empty(),
954+
"expected at least one canister range on the root subnet"
955+
);
956+
Ok(())
957+
})
958+
}
941959
}
942960

943961
mod simple_calls {

0 commit comments

Comments
 (0)