From 38e66f6ddf16164ab319fa7447b19add7e66a3bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 23 Oct 2024 09:48:25 +0100 Subject: [PATCH] added 'get_all_described_nodes' to NymApiClient and adjusted return type on api itself (#5016) --- .../validator-client/src/client.rs | 25 +++++++++++++++++-- .../validator-client/src/nym_api/mod.rs | 24 ++++++++++++++++-- nym-api/src/nym_nodes/handlers/mod.rs | 19 ++++++-------- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/common/client-libs/validator-client/src/client.rs b/common/client-libs/validator-client/src/client.rs index b8375abdbd..6205e55998 100644 --- a/common/client-libs/validator-client/src/client.rs +++ b/common/client-libs/validator-client/src/client.rs @@ -19,7 +19,7 @@ use nym_api_requests::ecash::{ }; use nym_api_requests::models::{ GatewayCoreStatusResponse, MixnodeCoreStatusResponse, MixnodeStatusResponse, - RewardEstimationResponse, StakeSaturationResponse, + NymNodeDescription, RewardEstimationResponse, StakeSaturationResponse, }; use nym_api_requests::models::{LegacyDescribedGateway, MixNodeBondAnnotated}; use nym_api_requests::nym_nodes::SkimmedNode; @@ -320,7 +320,7 @@ impl NymApiClient { loop { let mut res = self .nym_api - .get_all_basic_entry_assigned_nodes( + .get_basic_entry_assigned_nodes( semver_compatibility.clone(), false, Some(page), @@ -397,6 +397,27 @@ impl NymApiClient { Ok(self.nym_api.get_gateways_described().await?) } + pub async fn get_all_described_nodes( + &self, + ) -> Result, ValidatorClientError> { + // TODO: deal with paging in macro or some helper function or something, because it's the same pattern everywhere + let mut page = 0; + let mut descriptions = Vec::new(); + + loop { + let mut res = self.nym_api.get_nodes_described(Some(page), None).await?; + + descriptions.append(&mut res.data); + if descriptions.len() < res.pagination.total { + page += 1 + } else { + break; + } + } + + Ok(descriptions) + } + pub async fn get_gateway_core_status_count( &self, identity: IdentityKeyRef<'_>, diff --git a/common/client-libs/validator-client/src/nym_api/mod.rs b/common/client-libs/validator-client/src/nym_api/mod.rs index 9660e470c7..8e13c84580 100644 --- a/common/client-libs/validator-client/src/nym_api/mod.rs +++ b/common/client-libs/validator-client/src/nym_api/mod.rs @@ -11,9 +11,10 @@ use nym_api_requests::ecash::models::{ }; use nym_api_requests::ecash::VerificationKeyResponse; use nym_api_requests::models::{ - AnnotationResponse, LegacyDescribedMixNode, NodePerformanceResponse, + AnnotationResponse, LegacyDescribedMixNode, NodePerformanceResponse, NymNodeDescription, }; use nym_api_requests::nym_nodes::PaginatedCachedNodesResponse; +use nym_api_requests::pagination::PaginatedResponse; pub use nym_api_requests::{ ecash::{ models::{ @@ -119,6 +120,25 @@ pub trait NymApiClientExt: ApiClient { .await } + async fn get_nodes_described( + &self, + page: Option, + per_page: Option, + ) -> Result, NymAPIError> { + let mut params = Vec::new(); + + if let Some(page) = page { + params.push(("page", page.to_string())) + } + + if let Some(per_page) = per_page { + params.push(("per_page", per_page.to_string())) + } + + self.get_json(&[routes::API_VERSION, "nym-nodes", "described"], ¶ms) + .await + } + async fn get_basic_mixnodes( &self, semver_compatibility: Option, @@ -167,7 +187,7 @@ pub trait NymApiClientExt: ApiClient { /// retrieve basic information for nodes are capable of operating as an entry gateway /// this includes legacy gateways and nym-nodes - async fn get_all_basic_entry_assigned_nodes( + async fn get_basic_entry_assigned_nodes( &self, semver_compatibility: Option, no_legacy: bool, diff --git a/nym-api/src/nym_nodes/handlers/mod.rs b/nym-api/src/nym_nodes/handlers/mod.rs index ad92a689c0..4ccb3c02de 100644 --- a/nym-api/src/nym_nodes/handlers/mod.rs +++ b/nym-api/src/nym_nodes/handlers/mod.rs @@ -9,7 +9,7 @@ use axum::routing::get; use axum::{Json, Router}; use nym_api_requests::models::{ AnnotationResponse, NodeDatePerformanceResponse, NodePerformanceResponse, NoiseDetails, - NymNodeData, PerformanceHistoryResponse, UptimeHistoryResponse, + NymNodeDescription, PerformanceHistoryResponse, UptimeHistoryResponse, }; use nym_api_requests::pagination::{PaginatedResponse, Pagination}; use nym_contracts_common::NaiveFloat; @@ -125,32 +125,27 @@ async fn get_bonded_nodes( path = "/described", context_path = "/v1/nym-nodes", responses( - (status = 200, body = PaginatedResponse) + (status = 200, body = PaginatedResponse) ), params(PaginationRequest) )] async fn get_described_nodes( State(state): State, Query(pagination): Query, -) -> AxumResult>> { +) -> AxumResult>> { // TODO: implement it let _ = pagination; let cache = state.described_nodes_cache.get().await?; - let descriptions = cache.all_nodes(); - - let data = descriptions - .map(|n| &n.description) - .cloned() - .collect::>(); + let descriptions = cache.all_nodes().cloned().collect::>(); Ok(Json(PaginatedResponse { pagination: Pagination { - total: data.len(), + total: descriptions.len(), page: 0, - size: data.len(), + size: descriptions.len(), }, - data, + data: descriptions, })) }