From f5de7c78003e23ec28e6dfa23f3984e4f0ee6add Mon Sep 17 00:00:00 2001 From: mraszyk <31483726+mraszyk@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:35:59 +0200 Subject: [PATCH] chore(PocketIC): use ReadStateResponse and SubnetMetrics type from ic-transport-types (#1636) This PR makes the PocketIC library reuse the `ReadStateResponse` and `SubnetMetrics` type from `ic-transport-types` instead of redefining it from scratch (incl. custom deserializer). --- Cargo.Bazel.Fuzzing.json.lock | 7 +++- Cargo.Bazel.Fuzzing.toml.lock | 1 + Cargo.Bazel.json.lock | 7 +++- Cargo.Bazel.toml.lock | 1 + Cargo.lock | 1 + Cargo.toml | 1 + bazel/external_crates.bzl | 3 ++ packages/pocket-ic/BUILD.bazel | 1 + packages/pocket-ic/Cargo.toml | 1 + packages/pocket-ic/src/lib.rs | 52 +-------------------------- packages/pocket-ic/src/nonblocking.rs | 9 ++--- 11 files changed, 24 insertions(+), 60 deletions(-) diff --git a/Cargo.Bazel.Fuzzing.json.lock b/Cargo.Bazel.Fuzzing.json.lock index 98717b1a86b..8fc7625576b 100644 --- a/Cargo.Bazel.Fuzzing.json.lock +++ b/Cargo.Bazel.Fuzzing.json.lock @@ -1,5 +1,5 @@ { - "checksum": "7059ee8ebf23a06427b529a3b7d636561f31d7f874d553442922453110341f6e", + "checksum": "2debd702b24f7c7c56d40695d32c74e12a292344893ba7f1a2441756723febfc", "crates": { "abnf 0.12.0": { "name": "abnf", @@ -18200,6 +18200,10 @@ "id": "ic-test-state-machine-client 3.0.1", "target": "ic_test_state_machine_client" }, + { + "id": "ic-transport-types 0.37.1", + "target": "ic_transport_types" + }, { "id": "ic-utils 0.37.0", "target": "ic_utils" @@ -78949,6 +78953,7 @@ "ic-response-verification 2.6.0", "ic-stable-structures 0.6.5", "ic-test-state-machine-client 3.0.1", + "ic-transport-types 0.37.1", "ic-utils 0.37.0", "ic-verify-bls-signature 0.6.0", "ic-wasm 0.8.4", diff --git a/Cargo.Bazel.Fuzzing.toml.lock b/Cargo.Bazel.Fuzzing.toml.lock index 2c0307c478b..71f03a5e66f 100644 --- a/Cargo.Bazel.Fuzzing.toml.lock +++ b/Cargo.Bazel.Fuzzing.toml.lock @@ -3147,6 +3147,7 @@ dependencies = [ "ic-response-verification", "ic-stable-structures", "ic-test-state-machine-client", + "ic-transport-types", "ic-utils", "ic-verify-bls-signature 0.6.0", "ic-wasm", diff --git a/Cargo.Bazel.json.lock b/Cargo.Bazel.json.lock index 332bb4c7880..3b4f8639fe1 100644 --- a/Cargo.Bazel.json.lock +++ b/Cargo.Bazel.json.lock @@ -1,5 +1,5 @@ { - "checksum": "69f608e3c94460cdd4fa815962baf0dbeaf2d1f7cb05c8efffae1d0276473ae9", + "checksum": "ef752013465c4aa89c0749caba4ca2cd50f4fdbc0c61e65e3cdc9d6010222704", "crates": { "abnf 0.12.0": { "name": "abnf", @@ -18001,6 +18001,10 @@ "id": "ic-test-state-machine-client 3.0.1", "target": "ic_test_state_machine_client" }, + { + "id": "ic-transport-types 0.37.1", + "target": "ic_transport_types" + }, { "id": "ic-utils 0.37.0", "target": "ic_utils" @@ -79125,6 +79129,7 @@ "ic-response-verification 2.6.0", "ic-stable-structures 0.6.5", "ic-test-state-machine-client 3.0.1", + "ic-transport-types 0.37.1", "ic-utils 0.37.0", "ic-verify-bls-signature 0.6.0", "ic-wasm 0.8.4", diff --git a/Cargo.Bazel.toml.lock b/Cargo.Bazel.toml.lock index 389fe382355..1c78d169e2c 100644 --- a/Cargo.Bazel.toml.lock +++ b/Cargo.Bazel.toml.lock @@ -3136,6 +3136,7 @@ dependencies = [ "ic-response-verification", "ic-stable-structures", "ic-test-state-machine-client", + "ic-transport-types", "ic-utils", "ic-verify-bls-signature 0.6.0", "ic-wasm", diff --git a/Cargo.lock b/Cargo.lock index 2ed237946fc..b1b80c8f658 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16518,6 +16518,7 @@ dependencies = [ "ic-agent", "ic-base-types", "ic-cdk 0.13.5", + "ic-transport-types", "ic-universal-canister", "icp-ledger", "k256", diff --git a/Cargo.toml b/Cargo.toml index 684320fdab0..df4dcc4669a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -541,6 +541,7 @@ ic-certification = "2.6.0" ic-http-certification = "2.6.0" ic-response-verification = "2.6.0" ic-stable-structures = "0.6.5" +ic-transport-types = { version = "0.37.1" } ic-utils = { version = "0.37.0", features = ["raw"] } ic_bls12_381 = { version = "=0.10.0", default-features = false, features = [ "groups", diff --git a/bazel/external_crates.bzl b/bazel/external_crates.bzl index e32347e52d2..b4cadfc22fc 100644 --- a/bazel/external_crates.bzl +++ b/bazel/external_crates.bzl @@ -633,6 +633,9 @@ def external_crates_repository(name, cargo_lockfile, lockfile, sanitizers_enable "ic-test-state-machine-client": crate.spec( version = "^3.0.0", ), + "ic-transport-types": crate.spec( + version = "^0.37.1", + ), "ic-utils": crate.spec( version = "^0.37.0", features = ["raw"], diff --git a/packages/pocket-ic/BUILD.bazel b/packages/pocket-ic/BUILD.bazel index 5a39745cdcf..7d9dc552493 100644 --- a/packages/pocket-ic/BUILD.bazel +++ b/packages/pocket-ic/BUILD.bazel @@ -10,6 +10,7 @@ DEPENDENCIES = [ "@crate_index//:hex", "@crate_index//:ic-agent", "@crate_index//:ic-cdk", + "@crate_index//:ic-transport-types", "@crate_index//:reqwest", "@crate_index//:schemars", "@crate_index//:serde", diff --git a/packages/pocket-ic/Cargo.toml b/packages/pocket-ic/Cargo.toml index f4f12f87ac0..d556cfc1408 100644 --- a/packages/pocket-ic/Cargo.toml +++ b/packages/pocket-ic/Cargo.toml @@ -25,6 +25,7 @@ candid = "^0.10.2" hex = { workspace = true } ic-agent = { workspace = true } ic-cdk = "0.13.5" +ic-transport-types = { workspace = true } reqwest = { workspace = true } schemars = "0.8.16" serde = { workspace = true } diff --git a/packages/pocket-ic/src/lib.rs b/packages/pocket-ic/src/lib.rs index 96f9847f093..755bfedc8d2 100644 --- a/packages/pocket-ic/src/lib.rs +++ b/packages/pocket-ic/src/lib.rs @@ -46,6 +46,7 @@ use candid::{ }; pub use ic_cdk::api::management_canister::main::CanisterSettings; use ic_cdk::api::management_canister::main::{CanisterId, CanisterStatusResponse}; +use ic_transport_types::SubnetMetrics; use reqwest::Url; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -1255,16 +1256,6 @@ pub enum WasmResult { Reject(String), } -/// This struct describes the result of retrieving subnet metrics via a read state request. -#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize)] -pub struct SubnetMetrics { - pub num_canisters: u64, - pub canister_state_bytes: u64, - #[serde(with = "map_u128")] - pub consumed_cycles_total: u128, - pub update_transactions_total: u64, -} - /// Attempt to start a new PocketIC server if it's not already running. pub fn start_or_reuse_server() -> Url { let bin_path = match std::env::var_os("POCKET_IC_BIN") { @@ -1316,44 +1307,3 @@ To download the binary, please visit https://github.com/dfinity/pocketic." std::thread::sleep(Duration::from_millis(20)); } } - -mod map_u128 { - use serde::{ - de::{Error, IgnoredAny, MapAccess, Visitor}, - ser::SerializeMap, - Deserializer, Serializer, - }; - use std::fmt; - - pub fn serialize(val: &u128, s: S) -> Result { - let low = *val & u64::MAX as u128; - let high = *val >> 64; - let mut map = s.serialize_map(Some(2))?; - map.serialize_entry(&0, &low)?; - map.serialize_entry(&1, &(high != 0).then_some(high))?; - map.end() - } - - pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result { - d.deserialize_map(MapU128Visitor) - } - - struct MapU128Visitor; - - impl<'de> Visitor<'de> for MapU128Visitor { - type Value = u128; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("a map of low and high") - } - - fn visit_map>(self, mut map: A) -> Result { - let (_, low): (IgnoredAny, u64) = map - .next_entry()? - .ok_or_else(|| A::Error::missing_field("0"))?; - let opt: Option<(IgnoredAny, Option)> = map.next_entry()?; - let high = opt.and_then(|x| x.1).unwrap_or(0); - Ok((high as u128) << 64 | low as u128) - } - } -} diff --git a/packages/pocket-ic/src/nonblocking.rs b/packages/pocket-ic/src/nonblocking.rs index 0aafa68f206..e50a36b93d2 100644 --- a/packages/pocket-ic/src/nonblocking.rs +++ b/packages/pocket-ic/src/nonblocking.rs @@ -7,7 +7,7 @@ use crate::common::rest::{ RawSetStableMemory, RawStableMemory, RawSubmitIngressResult, RawSubnetId, RawTime, RawVerifyCanisterSigArg, RawWasmResult, SubnetId, Topology, }; -use crate::{CallError, PocketIcBuilder, SubnetMetrics, UserError, WasmResult}; +use crate::{CallError, PocketIcBuilder, UserError, WasmResult}; use candid::{ decode_args, encode_args, utils::{ArgumentDecoder, ArgumentEncoder}, @@ -18,6 +18,7 @@ use ic_cdk::api::management_canister::main::{ ChunkHash, ClearChunkStoreArgument, InstallChunkedCodeArgument, InstallCodeArgument, SkipPreUpgrade, UpdateSettingsArgument, UploadChunkArgument, }; +use ic_transport_types::{ReadStateResponse, SubnetMetrics}; use reqwest::Url; use serde::{de::DeserializeOwned, Serialize}; use sha2::{Digest, Sha256}; @@ -985,12 +986,6 @@ impl PocketIc { /// Returns subnet metrics for a given subnet. #[instrument(ret, skip(self), fields(instance_id=self.instance_id, subnet_id = %subnet_id.to_string()))] pub async fn get_subnet_metrics(&self, subnet_id: Principal) -> Option { - #[derive(Debug, Clone, serde::Deserialize, serde::Serialize, PartialEq, Eq)] - struct ReadStateResponse { - #[serde(with = "serde_bytes")] - pub certificate: Vec, - } - let path = vec![ "subnet".into(), ic_agent::hash_tree::Label::from_bytes(subnet_id.as_slice()),