caching self reported host info

This commit is contained in:
Jędrzej Stuczyński
2023-10-06 09:55:27 +01:00
parent 79ef88dd49
commit 8f2cb95ffa
5 changed files with 55 additions and 14 deletions
+3
View File
@@ -10,6 +10,7 @@ use nym_mixnet_contract_common::{
GatewayBond, IdentityKey, Interval, MixId, MixNode, Percent, RewardedSetNodeStatus,
};
use nym_node_requests::api::v1::gateway::models::WebSockets;
use nym_node_requests::api::v1::node::models::HostInformation;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
@@ -357,6 +358,8 @@ pub struct CirculatingSupplyResponse {
#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct NymNodeDescription {
pub host_information: HostInformation,
// for now we only care about their ws/wss situation, nothing more
pub mixnet_websockets: WebSockets,
}
+29 -8
View File
@@ -11,7 +11,6 @@ use nym_api_requests::models::NymNodeDescription;
use nym_contracts_common::IdentityKey;
use nym_mixnet_contract_common::Gateway;
use nym_node_requests::api::client::{NymNodeApiClientError, NymNodeApiClientExt};
use nym_node_requests::api::v1::gateway::models::WebSockets;
use std::collections::HashMap;
use thiserror::Error;
@@ -43,6 +42,10 @@ pub enum NodeDescribeCacheError {
#[source]
source: NymNodeApiClientError,
},
// TODO: perhaps include more details here like whether key/signature/payload was malformed
#[error("could not verify signed host information for gateway '{gateway}'")]
MissignedHostInformation { gateway: IdentityKey },
}
pub struct NodeDescriptionProvider {
@@ -68,9 +71,9 @@ impl NodeDescriptionProvider {
}
}
async fn get_gateway_websocket_support(
async fn get_gateway_description(
gateway: Gateway,
) -> Result<(IdentityKey, WebSockets), NodeDescribeCacheError> {
) -> Result<(IdentityKey, NymNodeDescription), NodeDescribeCacheError> {
let client = match nym_node_requests::api::Client::new_url(&gateway.host, None) {
Ok(client) => client,
Err(err) => {
@@ -82,6 +85,21 @@ async fn get_gateway_websocket_support(
}
};
let host_info =
client
.get_host_information()
.await
.map_err(|err| NodeDescribeCacheError::ApiFailure {
gateway: gateway.identity_key.clone(),
source: err,
})?;
if !host_info.verify_host_information() {
return Err(NodeDescribeCacheError::MissignedHostInformation {
gateway: gateway.identity_key,
});
}
let websockets =
client
.get_mixnet_websockets()
@@ -91,7 +109,12 @@ async fn get_gateway_websocket_support(
source: err,
})?;
Ok((gateway.identity_key, websockets))
let description = NymNodeDescription {
host_information: host_info.data,
mixnet_websockets: websockets,
};
Ok((gateway.identity_key, description))
}
#[async_trait]
@@ -120,14 +143,12 @@ impl CacheItemProvider for NodeDescriptionProvider {
// .clone()
.into_iter()
.map(|bond| bond.gateway)
.map(get_gateway_websocket_support),
.map(get_gateway_description),
)
.buffer_unordered(self.batch_size)
.filter_map(|res| async move {
match res {
Ok((identity, mixnet_websockets)) => {
Some((identity, NymNodeDescription { mixnet_websockets }))
}
Ok((identity, description)) => Some((identity, description)),
Err(err) => {
// TODO: reduce it to trace/debug before PR
warn!("{err}");
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
use crate::api::v1::gateway::models::WebSockets;
use crate::api::v1::node::models::SignedHostInformation;
use crate::routes;
use async_trait::async_trait;
use http_api_client::{ApiClient, HttpClientError};
@@ -13,6 +14,11 @@ pub type NymNodeApiClientError = HttpClientError;
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait NymNodeApiClientExt: ApiClient {
async fn get_host_information(&self) -> Result<SignedHostInformation, NymNodeApiClientError> {
self.get_json_from(routes::api::v1::host_info_absolute())
.await
}
// TODO: implement calls for other endpoints; for now I only care about the wss
async fn get_mixnet_websockets(&self) -> Result<WebSockets, NymNodeApiClientError> {
self.get_json_from(
+17 -5
View File
@@ -1,14 +1,12 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::api::v1::node::models::HostInformation;
use crate::error::Error;
use nym_crypto::asymmetric::identity;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
#[cfg(feature = "openapi")]
use crate::api::v1::node::models::HostInformation;
#[cfg(feature = "client")]
pub mod client;
pub mod v1;
@@ -16,7 +14,11 @@ pub mod v1;
#[cfg(feature = "client")]
pub use client::Client;
#[derive(Debug, Clone, Serialize)]
// create the type alias manually if openapi is not enabled
#[cfg(not(feature = "openapi"))]
pub type SignedHostInformation = SignedData<HostInformation>;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "openapi", aliases(SignedHostInformation = SignedData<HostInformation>))]
pub struct SignedData<T> {
@@ -50,6 +52,16 @@ impl<T> SignedData<T> {
}
}
impl SignedHostInformation {
pub fn verify_host_information(&self) -> bool {
let Ok(pub_key) = identity::PublicKey::from_base58_string(&self.keys.ed25519) else {
return false;
};
self.verify(&pub_key)
}
}
impl<T> Deref for SignedData<T> {
type Target = T;
@@ -4,7 +4,6 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[cfg(feature = "openapi")]
pub use crate::api::SignedHostInformation;
pub use nym_bin_common::build_information::BinaryBuildInformationOwned;