Feature/force refresh node (#5101)

* introduced nym-api endpoint for force refreshing described node data

* client code + updated return types

* nym-node to update self-described data cache on startup + change request type

* send request to all available nym-apis

* fixed 'is_stale' check
This commit is contained in:
Jędrzej Stuczyński
2024-11-06 09:17:44 +00:00
committed by GitHub
parent fd8dc63c88
commit c001059af9
15 changed files with 327 additions and 33 deletions
+40 -1
View File
@@ -32,13 +32,18 @@ use nym_node_http_api::{NymNodeHTTPServer, NymNodeRouter};
use nym_sphinx_acknowledgements::AckKey;
use nym_sphinx_addressing::Recipient;
use nym_task::{TaskClient, TaskManager};
use nym_validator_client::client::NymApiClientExt;
use nym_validator_client::models::NodeRefreshBody;
use nym_validator_client::NymApiClient;
use nym_wireguard::{peer_controller::PeerControlRequest, WireguardGatewayData};
use rand::rngs::OsRng;
use rand::{CryptoRng, RngCore};
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tracing::{debug, error, info, trace};
use tokio::time::timeout;
use tracing::{debug, error, info, trace, warn};
use zeroize::Zeroizing;
use self::helpers::load_x25519_wireguard_keypair;
@@ -740,6 +745,38 @@ impl NymNode {
.await?)
}
async fn try_refresh_remote_nym_api_cache(&self) {
info!("attempting to request described cache request from nym-api...");
if self.config.mixnet.nym_api_urls.is_empty() {
warn!("no nym-api urls available");
return;
}
for nym_api in &self.config.mixnet.nym_api_urls {
info!("trying {nym_api}...");
let client = NymApiClient::new_with_user_agent(nym_api.clone(), bin_info_owned!());
// make new request every time in case previous one takes longer and invalidates the signature
let request = NodeRefreshBody::new(self.ed25519_identity_keys.private_key());
match timeout(
Duration::from_secs(10),
client.nym_api.force_refresh_describe_cache(&request),
)
.await
{
Ok(Ok(_)) => {
info!("managed to refresh own self-described data cache")
}
Ok(Err(request_failure)) => {
warn!("failed to resolve the refresh request: {request_failure}")
}
Err(_timeout) => {
warn!("timed out while attempting to resolve the request. the cache might be stale")
}
};
}
}
pub(crate) async fn run(self) -> Result<(), NymNodeError> {
let mut task_manager = TaskManager::default().named("NymNode");
let http_server = self
@@ -754,6 +791,8 @@ impl NymNode {
}
});
self.try_refresh_remote_nym_api_cache().await;
match self.config.mode {
NodeMode::Mixnode => {
self.start_mixnode(task_manager.subscribe_named("mixnode"))?;