diff --git a/nym-api/src/main.rs b/nym-api/src/main.rs index 1d16067f75..1320efa590 100644 --- a/nym-api/src/main.rs +++ b/nym-api/src/main.rs @@ -102,6 +102,7 @@ async fn start_nym_api_tasks( // let cache = refresher.get_shared_cache(); node_describe_cache::new_refresher_with_initial_value( &config.topology_cacher, + nym_contract_cache_state.clone(), described_nodes_state.to_owned(), ) .named("node-self-described-data-refresher") diff --git a/nym-api/src/node_describe_cache/mod.rs b/nym-api/src/node_describe_cache/mod.rs index cc2b547402..6b3a4f57ea 100644 --- a/nym-api/src/node_describe_cache/mod.rs +++ b/nym-api/src/node_describe_cache/mod.rs @@ -1,6 +1,7 @@ // Copyright 2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::nym_contract_cache::cache::NymContractCache; use crate::support::caching::cache::{SharedCache, UninitialisedCache}; use crate::support::caching::refresher::{CacheItemProvider, CacheRefresher}; use crate::support::config; @@ -8,11 +9,10 @@ use crate::support::config::DEFAULT_NODE_DESCRIBE_BATCH_SIZE; use futures_util::{stream, StreamExt}; use nym_api_requests::models::NymNodeDescription; use nym_contracts_common::IdentityKey; -use nym_mixnet_contract_common::{Gateway, GatewayBond}; +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 std::ops::Deref; use thiserror::Error; // type alias for ease of use @@ -47,15 +47,16 @@ pub enum NodeDescribeCacheError { pub struct NodeDescriptionProvider { // for now we only care about gateways, nothing more - network_gateways: SharedCache>, + // network_gateways: SharedCache>, + contract_cache: NymContractCache, batch_size: usize, } impl NodeDescriptionProvider { - pub(crate) fn new() -> NodeDescriptionProvider { + pub(crate) fn new(contract_cache: NymContractCache) -> NodeDescriptionProvider { NodeDescriptionProvider { - network_gateways: SharedCache::new(), + contract_cache, batch_size: DEFAULT_NODE_DESCRIBE_BATCH_SIZE, } } @@ -98,15 +99,25 @@ impl CacheItemProvider for NodeDescriptionProvider { type Item = HashMap; type Error = NodeDescribeCacheError; + async fn wait_until_ready(&self) { + self.contract_cache.wait_for_initial_values().await + } + async fn try_refresh(&self) -> Result { - let guard = self.network_gateways.get().await?; - let gateways = &*guard; + let gateways = self.contract_cache.gateways_all().await; + + // let guard = self.network_gateways.get().await?; + // let gateways = &*guard; + + if gateways.is_empty() { + return Ok(HashMap::new()); + } // TODO: somehow bypass the 'higher-ranked lifetime error' and remove that redundant clone let websockets = stream::iter( gateways - .deref() - .clone() + // .deref() + // .clone() .into_iter() .map(|bond| bond.gateway) .map(get_gateway_websocket_support), @@ -135,10 +146,14 @@ impl CacheItemProvider for NodeDescriptionProvider { #[allow(dead_code)] pub(crate) fn new_refresher( config: &config::TopologyCacher, + contract_cache: NymContractCache, + // hehe. we can't do that yet + // network_gateways: SharedCache>, ) -> CacheRefresher { CacheRefresher::new( Box::new( - NodeDescriptionProvider::new().with_batch_size(config.debug.node_describe_batch_size), + NodeDescriptionProvider::new(contract_cache) + .with_batch_size(config.debug.node_describe_batch_size), ), config.debug.node_describe_caching_interval, ) @@ -146,11 +161,15 @@ pub(crate) fn new_refresher( pub(crate) fn new_refresher_with_initial_value( config: &config::TopologyCacher, + contract_cache: NymContractCache, + // hehe. we can't do that yet + // network_gateways: SharedCache>, initial: SharedCache, ) -> CacheRefresher { CacheRefresher::new_with_initial_value( Box::new( - NodeDescriptionProvider::new().with_batch_size(config.debug.node_describe_batch_size), + NodeDescriptionProvider::new(contract_cache) + .with_batch_size(config.debug.node_describe_batch_size), ), config.debug.node_describe_caching_interval, initial, diff --git a/nym-api/src/support/caching/refresher.rs b/nym-api/src/support/caching/refresher.rs index bb8a25527a..9487f5130c 100644 --- a/nym-api/src/support/caching/refresher.rs +++ b/nym-api/src/support/caching/refresher.rs @@ -21,6 +21,8 @@ pub trait CacheItemProvider { type Item; type Error: std::error::Error; + async fn wait_until_ready(&self) {} + async fn try_refresh(&self) -> Result; } @@ -110,6 +112,8 @@ where } pub async fn run(&self, mut task_client: TaskClient) { + self.provider.wait_until_ready().await; + let mut refresh_interval = interval(self.refreshing_interval); while !task_client.is_shutdown() { tokio::select! {