diff --git a/nym-api/src/ecash/tests/mod.rs b/nym-api/src/ecash/tests/mod.rs index 0fe5282943..b222aa8b6e 100644 --- a/nym-api/src/ecash/tests/mod.rs +++ b/nym-api/src/ecash/tests/mod.rs @@ -1275,7 +1275,7 @@ impl TestFixture { AppState { nyxd_client, chain_status_cache: ChainStatusCache::new(Duration::from_secs(42)), - address_info_cache: AddressInfoCache::new(), + address_info_cache: AddressInfoCache::new(Duration::from_secs(42), 1000), forced_refresh: ForcedRefresh::new(true), nym_contract_cache: NymContractCache::new(), node_status_cache: NodeStatusCache::new(), diff --git a/nym-api/src/support/cli/run.rs b/nym-api/src/support/cli/run.rs index 334a800657..e1c889007c 100644 --- a/nym-api/src/support/cli/run.rs +++ b/nym-api/src/support/cli/run.rs @@ -102,6 +102,15 @@ pub(crate) struct Args { #[clap(long)] pub(crate) bind_address: Option, + /// account/address cache TTL: should be lower than epoch length (1 hour) + /// because, at worst, data will be stale for + seconds + #[clap(long, env = "ADDRESS_CACHE_REFRESH_INTERVAL_S")] + pub(crate) address_cache_ttl_seconds: Option, + + /// number of addresses that are cached on account/address endpoint + #[clap(long, env = "ADDRESS_CACHE_CAPACITY")] + pub(crate) address_cache_capacity: Option, + #[clap(hide = true, long, default_value_t = false)] pub(crate) allow_illegal_ips: bool, } @@ -194,7 +203,10 @@ async fn start_nym_api_tasks_axum(config: &Config) -> anyhow::Result Self { + Self { + time_to_live: DEFAULT_ADDRESS_CACHE_TTL, + capacity: DEFAULT_ADDRESS_CACHE_CAPACITY, + } + } +} + // this got separated into 2 structs so that we could have a sane `default` implementation for the latter #[derive(Debug, Deserialize, PartialEq, Eq, Serialize)] pub struct NetworkMonitor { diff --git a/nym-api/src/support/config/override.rs b/nym-api/src/support/config/override.rs index a2f6d9c76c..4f8b96db31 100644 --- a/nym-api/src/support/config/override.rs +++ b/nym-api/src/support/config/override.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::support::cli::{init, run}; -use std::net::SocketAddr; +use std::{net::SocketAddr, time::Duration}; // Configuration that can be overridden. pub(crate) struct OverrideConfig { @@ -32,6 +32,9 @@ pub(crate) struct OverrideConfig { /// default: `127.0.0.1:8080` in `debug` builds and `0.0.0.0:8080` in `release` pub(crate) bind_address: Option, + pub(crate) address_cache_ttl: Option, + pub(crate) address_cache_capacity: Option, + pub(crate) allow_illegal_ips: bool, } @@ -46,6 +49,9 @@ impl From for OverrideConfig { announce_address: args.announce_address, monitor_credentials_mode: Some(args.monitor_credentials_mode), bind_address: args.bind_address, + // irrelevant for --init command because we set the value in --run + address_cache_ttl: None, + address_cache_capacity: None, allow_illegal_ips: args.allow_illegal_ips, } } @@ -62,6 +68,8 @@ impl From for OverrideConfig { announce_address: args.announce_address, monitor_credentials_mode: args.monitor_credentials_mode, bind_address: args.bind_address, + address_cache_ttl: args.address_cache_ttl_seconds.map(Duration::from_secs), + address_cache_capacity: args.address_cache_capacity, allow_illegal_ips: args.allow_illegal_ips, } } diff --git a/nym-api/src/unstable_routes/account/cache.rs b/nym-api/src/unstable_routes/account/cache.rs index a60a13abb0..34f0ddbee1 100644 --- a/nym-api/src/unstable_routes/account/cache.rs +++ b/nym-api/src/unstable_routes/account/cache.rs @@ -17,17 +17,11 @@ pub(crate) struct AddressInfoCache { } impl AddressInfoCache { - pub(crate) fn new() -> Self { - // epoch duration = 1 hour - // cache TTL is slightly lower than that to avoid too stale data in case - // cache was refreshed JUST BEFORE epoch transition - let cache_ttl = Duration::from_secs(60 * 30); - let max_capacity = 1000; - + pub(crate) fn new(cache_ttl: Duration, capacity: u64) -> Self { AddressInfoCache { inner: Cache::builder() .time_to_live(cache_ttl) - .max_capacity(max_capacity) + .max_capacity(capacity) .build(), } }