Skip to content

Commit

Permalink
bugfix/feature: added NymApiClient method to get all skimmed nodes (#…
Browse files Browse the repository at this point in the history
…5062)

* bugfix/feature: added NymApiClient method to get all skimmed nodes

* wasm

* helper: utility method for getting ed25519 identity directly from node description
  • Loading branch information
jstuczyn authored Oct 30, 2024
1 parent 76da4ab commit 753a21f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl GeoAwareTopologyProvider {
async fn get_topology(&self) -> Option<NymTopology> {
let mixnodes = match self
.validator_client
.get_basic_active_mixing_assigned_nodes(Some(self.client_version.clone()))
.get_all_basic_active_mixing_assigned_nodes(Some(self.client_version.clone()))
.await
{
Err(err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl NymApiTopologyProvider {
async fn get_current_compatible_topology(&mut self) -> Option<NymTopology> {
let mixnodes = match self
.validator_client
.get_basic_active_mixing_assigned_nodes(Some(self.client_version.clone()))
.get_all_basic_active_mixing_assigned_nodes(Some(self.client_version.clone()))
.await
{
Err(err) => {
Expand Down
4 changes: 3 additions & 1 deletion common/client-core/src/init/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ pub async fn current_mixnodes<R: Rng>(

log::trace!("Fetching list of mixnodes from: {nym_api}");

let mixnodes = client.get_basic_active_mixing_assigned_nodes(None).await?;
let mixnodes = client
.get_all_basic_active_mixing_assigned_nodes(None)
.await?;
let valid_mixnodes = mixnodes
.iter()
.filter_map(|mixnode| mixnode.try_into().ok())
Expand Down
30 changes: 28 additions & 2 deletions common/client-libs/validator-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl NymApiClient {
self.nym_api.change_base_url(new_endpoint);
}

#[deprecated(note = "use get_basic_active_mixing_assigned_nodes instead")]
#[deprecated(note = "use get_all_basic_active_mixing_assigned_nodes instead")]
pub async fn get_basic_mixnodes(
&self,
semver_compatibility: Option<String>,
Expand Down Expand Up @@ -387,7 +387,7 @@ impl NymApiClient {

/// retrieve basic information for nodes that got assigned 'mixing' node in this epoch
/// this includes legacy mixnodes and nym-nodes
pub async fn get_basic_active_mixing_assigned_nodes(
pub async fn get_all_basic_active_mixing_assigned_nodes(
&self,
semver_compatibility: Option<String>,
) -> Result<Vec<SkimmedNode>, ValidatorClientError> {
Expand Down Expand Up @@ -417,6 +417,32 @@ impl NymApiClient {
Ok(nodes)
}

/// retrieve basic information for all bonded nodes on the network
pub async fn get_all_basic_nodes(
&self,
semver_compatibility: Option<String>,
) -> Result<Vec<SkimmedNode>, 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 nodes = Vec::new();

loop {
let mut res = self
.nym_api
.get_basic_nodes(semver_compatibility.clone(), false, Some(page), None)
.await?;

nodes.append(&mut res.nodes.data);
if nodes.len() < res.nodes.pagination.total {
page += 1
} else {
break;
}
}

Ok(nodes)
}

pub async fn get_cached_active_mixnodes(
&self,
) -> Result<Vec<MixNodeDetails>, ValidatorClientError> {
Expand Down
32 changes: 32 additions & 0 deletions common/client-libs/validator-client/src/nym_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,38 @@ pub trait NymApiClientExt: ApiClient {
.await
}

async fn get_basic_nodes(
&self,
semver_compatibility: Option<String>,
no_legacy: bool,
page: Option<u32>,
per_page: Option<u32>,
) -> Result<PaginatedCachedNodesResponse<SkimmedNode>, NymAPIError> {
let mut params = Vec::new();

if let Some(arg) = &semver_compatibility {
params.push(("semver_compatibility", arg.clone()))
}

if no_legacy {
params.push(("no_legacy", "true".to_string()))
}

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, "unstable", "nym-nodes", "skimmed"],
&params,
)
.await
}

async fn get_active_mixnodes(&self) -> Result<Vec<MixNodeDetails>, NymAPIError> {
self.get_json(
&[routes::API_VERSION, routes::MIXNODES, routes::ACTIVE],
Expand Down
2 changes: 1 addition & 1 deletion common/wasm/client-core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub async fn current_network_topology_async(

let api_client = NymApiClient::new(url);
let mixnodes = api_client
.get_basic_active_mixing_assigned_nodes(None)
.get_all_basic_active_mixing_assigned_nodes(None)
.await?;
let gateways = api_client.get_all_basic_entry_assigned_nodes(None).await?;

Expand Down
4 changes: 4 additions & 0 deletions nym-api/nym-api-requests/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,10 @@ impl NymNodeDescription {
}
}

pub fn ed25519_identity_key(&self) -> ed25519::PublicKey {
self.description.host_information.keys.ed25519
}

pub fn to_skimmed_node(&self, role: NodeRole, performance: Performance) -> SkimmedNode {
let keys = &self.description.host_information.keys;
let entry = if self.description.declared_role.entry {
Expand Down
2 changes: 1 addition & 1 deletion sdk/rust/nym-sdk/examples/custom_topology_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl MyTopologyProvider {
async fn get_topology(&self) -> NymTopology {
let mixnodes = self
.validator_client
.get_basic_active_mixing_assigned_nodes(None)
.get_all_basic_active_mixing_assigned_nodes(None)
.await
.unwrap();

Expand Down

0 comments on commit 753a21f

Please sign in to comment.