Skip to content

Commit d9b0bbf

Browse files
committed
feat: introduce on-disk cache persistance for major nym-api caches
This includes: - mixnet contract cache - described nodes cache - nodes annotations cache (performance) those changes include taking some code developed for the purposes of #6277
1 parent ae54e86 commit d9b0bbf

File tree

29 files changed

+437
-152
lines changed

29 files changed

+437
-152
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/ecash-signer-check/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ readme.workspace = true
1414
futures = { workspace = true }
1515
thiserror = { workspace = true }
1616
semver = { workspace = true }
17+
serde = { workspace = true }
1718
tokio = { workspace = true, features = ["time"] }
1819
tracing = { workspace = true }
1920
url = { workspace = true }

common/ecash-signer-check/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33

44
use crate::client_check::check_client;
55
use futures::stream::{FuturesUnordered, StreamExt};
6+
use nym_ecash_signer_check_types::status::{SignerResult, Status};
67
use nym_network_defaults::NymNetworkDetails;
78
use nym_validator_client::QueryHttpRpcNyxdClient;
8-
use nym_validator_client::nyxd::contract_traits::{DkgQueryClient, PagedDkgQueryClient};
9-
use std::collections::HashMap;
10-
use url::Url;
11-
12-
pub use error::SignerCheckError;
13-
use nym_ecash_signer_check_types::status::{SignerResult, Status};
149
use nym_validator_client::ecash::models::EcashSignerStatusResponse;
1510
use nym_validator_client::models::{
1611
ChainBlocksStatusResponse, ChainStatusResponse, SignerInformationResponse,
1712
};
1813
use nym_validator_client::nyxd::contract_traits::dkg_query_client::{
1914
ContractVKShare, DealerDetails, Epoch,
2015
};
16+
use nym_validator_client::nyxd::contract_traits::{DkgQueryClient, PagedDkgQueryClient};
17+
use serde::{Deserialize, Serialize};
18+
use std::collections::HashMap;
19+
use url::Url;
20+
21+
pub use error::SignerCheckError;
2122

2223
mod client_check;
2324
pub mod error;
@@ -31,6 +32,7 @@ pub type TypedSignerResult = SignerResult<
3132
pub type LocalChainStatus = Status<ChainStatusResponse, ChainBlocksStatusResponse>;
3233
pub type SigningStatus = Status<SignerInformationResponse, EcashSignerStatusResponse>;
3334

35+
#[derive(Serialize, Deserialize)]
3436
pub struct SignersTestResult {
3537
pub threshold: Option<u64>,
3638
pub results: Vec<TypedSignerResult>,

nym-api/src/ecash/tests/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ use crate::ecash::api_routes::handlers::ecash_routes;
55
use crate::ecash::error::{EcashError, Result};
66
use crate::ecash::keys::KeyPairWithEpoch;
77
use crate::ecash::state::EcashState;
8-
use crate::mixnet_contract_cache::cache::MixnetContractCache;
98
use crate::network::models::NetworkDetails;
109
use crate::node_describe_cache::cache::DescribedNodes;
1110
use crate::node_status_api::handlers::unstable;
12-
use crate::node_status_api::NodeStatusCache;
1311
use crate::status::ApiStatusState;
1412
use crate::support::caching::cache::SharedCache;
1513
use crate::support::config;
@@ -1284,8 +1282,8 @@ impl TestFixture {
12841282
ecash_signers_cache: Default::default(),
12851283
address_info_cache: AddressInfoCache::new(Duration::from_secs(42), 1000),
12861284
forced_refresh: ForcedRefresh::new(true),
1287-
mixnet_contract_cache: MixnetContractCache::new(),
1288-
node_status_cache: NodeStatusCache::new(),
1285+
mixnet_contract_cache: SharedCache::new().into(),
1286+
node_status_cache: SharedCache::new().into(),
12891287
storage,
12901288
described_nodes_cache: SharedCache::<DescribedNodes>::new(),
12911289
network_details: NetworkDetails::new(

nym-api/src/epoch_operations/helpers.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: GPL-3.0-only
33

44
use crate::epoch_operations::EpochAdvancer;
5-
use crate::support::caching::Cache;
5+
use crate::support::caching::cache::UninitialisedCache;
66
use cosmwasm_std::{Decimal, Fraction};
77
use nym_api_requests::models::NodeAnnotation;
88
use nym_mixnet_contract_common::helpers::IntoBaseDecimal;
@@ -210,10 +210,13 @@ fn determine_per_node_work(
210210

211211
impl EpochAdvancer {
212212
fn load_performance(
213-
status_cache: &Option<RwLockReadGuard<'_, Cache<HashMap<NodeId, NodeAnnotation>>>>,
213+
status_cache: &Result<
214+
RwLockReadGuard<'_, HashMap<NodeId, NodeAnnotation>>,
215+
UninitialisedCache,
216+
>,
214217
node_id: NodeId,
215218
) -> NodeWithPerformance {
216-
let Some(status_cache) = status_cache.as_ref() else {
219+
let Ok(status_cache) = status_cache.as_ref() else {
217220
return NodeWithPerformance::new_zero(node_id);
218221
};
219222

@@ -239,7 +242,7 @@ impl EpochAdvancer {
239242
let standby_node_work_factor = nodes_work.standby;
240243

241244
let status_cache = self.status_cache.node_annotations().await;
242-
if status_cache.is_none() {
245+
if status_cache.is_err() {
243246
error!("there are no node annotations available");
244247
};
245248

nym-api/src/epoch_operations/rewarded_set_assignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl EpochAdvancer {
214214
#[allow(clippy::unwrap_used)]
215215
let described_cache = self.described_cache.get().await.unwrap();
216216

217-
let Some(status_cache) = self.status_cache.node_annotations().await else {
217+
let Ok(status_cache) = self.status_cache.node_annotations().await else {
218218
warn!("there are no node annotations available");
219219
return Vec::new();
220220
};

nym-api/src/mixnet_contract_cache/cache/data.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use nym_mixnet_contract_common::{
77
NymNodeDetails, RewardingParams,
88
};
99
use nym_topology::CachedEpochRewardedSet;
10+
use serde::{Deserialize, Serialize};
1011

11-
#[derive(Clone)]
12+
#[derive(Clone, Serialize, Deserialize)]
1213
pub(crate) struct ConfigScoreData {
1314
pub(crate) config_score_params: ConfigScoreParams,
1415
pub(crate) nym_node_version_history: Vec<HistoricalNymNodeVersionEntry>,
@@ -27,6 +28,7 @@ impl From<ConfigScoreData> for ConfigScoreDataResponse {
2728
}
2829
}
2930

31+
#[derive(Serialize, Deserialize)]
3032
pub(crate) struct MixnetContractCacheData {
3133
pub(crate) rewarding_denom: String,
3234

nym-api/src/mixnet_contract_cache/cache/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use nym_mixnet_contract_common::{
1414
};
1515
use nym_topology::CachedEpochRewardedSet;
1616
use nym_validator_client::nyxd::Coin;
17+
use std::path::Path;
18+
use std::time::Duration;
1719
use time::OffsetDateTime;
1820
use tokio::sync::RwLockReadGuard;
1921

@@ -27,10 +29,16 @@ pub struct MixnetContractCache {
2729
pub(crate) inner: SharedCache<MixnetContractCacheData>,
2830
}
2931

32+
impl From<SharedCache<MixnetContractCacheData>> for MixnetContractCache {
33+
fn from(inner: SharedCache<MixnetContractCacheData>) -> Self {
34+
MixnetContractCache { inner }
35+
}
36+
}
37+
3038
impl MixnetContractCache {
31-
pub(crate) fn new() -> Self {
39+
pub(crate) fn new<P: AsRef<Path>>(store_path: P, max_cache_age: Duration) -> Self {
3240
MixnetContractCache {
33-
inner: SharedCache::new(),
41+
inner: SharedCache::new_with_persistent(store_path, max_cache_age, None),
3442
}
3543
}
3644

nym-api/src/mixnet_contract_cache/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::mixnet_contract_cache::cache::MixnetContractCache;
77
use crate::support::caching::refresher::CacheRefresher;
88
use crate::support::{config, nyxd};
99
use nym_validator_client::nyxd::error::NyxdError;
10+
use std::path::PathBuf;
1011

1112
pub(crate) mod cache;
1213
pub(crate) mod handlers;
@@ -15,11 +16,13 @@ pub(crate) fn build_refresher(
1516
config: &config::MixnetContractCache,
1617
nym_contract_cache_state: &MixnetContractCache,
1718
nyxd_client: nyxd::Client,
19+
on_disk_file: PathBuf,
1820
) -> CacheRefresher<MixnetContractCacheData, NyxdError> {
1921
CacheRefresher::new_with_initial_value(
2022
Box::new(MixnetContractDataProvider::new(nyxd_client)),
2123
config.debug.caching_interval,
2224
nym_contract_cache_state.inner(),
2325
)
2426
.named("mixnet-contract-cache-refresher")
27+
.with_persistent_cache(on_disk_file)
2528
}

nym-api/src/network_monitor/monitor/preparer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl PacketPreparer {
233233
// routes so that they wouldn't be reused
234234
pub(crate) async fn prepare_test_routes(&self, n: usize) -> Option<Vec<TestRoute>> {
235235
let descriptions = self.described_cache.get().await.ok()?;
236-
let statuses = self.node_status_cache.node_annotations().await?;
236+
let statuses = self.node_status_cache.node_annotations().await.ok()?;
237237

238238
let mixing_nym_nodes = descriptions.mixing_nym_nodes();
239239
// last I checked `gatewaying` wasn't a word : )

0 commit comments

Comments
 (0)