From bbf0d06583bfbecda08dda3b61c779ae7479ad73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 20 Aug 2024 12:54:42 +0100 Subject: [PATCH] updated constants depending on all 30 days expiration --- gateway/src/config/mod.rs | 29 ++++++++++++++++--- .../ecash/credential_sender.rs | 2 +- nym-api/src/ecash/state/helpers.rs | 2 +- nym-node/src/config/entry_gateway.rs | 29 ++++++++++++++++--- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/gateway/src/config/mod.rs b/gateway/src/config/mod.rs index 0750b3bf0d..5a5c57e3f9 100644 --- a/gateway/src/config/mod.rs +++ b/gateway/src/config/mod.rs @@ -10,7 +10,7 @@ use nym_config::{ must_get_home, read_config_from_toml_file, save_formatted_config_to_file, NymConfigTemplate, DEFAULT_CONFIG_DIR, DEFAULT_CONFIG_FILENAME, DEFAULT_DATA_DIR, NYM_DIR, }; -use nym_network_defaults::{mainnet, DEFAULT_NYM_NODE_HTTP_PORT}; +use nym_network_defaults::{mainnet, DEFAULT_NYM_NODE_HTTP_PORT, TICKETBOOK_VALIDITY_DAYS}; use serde::{Deserialize, Serialize}; use std::io; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; @@ -501,7 +501,7 @@ pub struct ZkNymTicketHandlerDebug { pub minimum_redemption_tickets: usize, /// Specifies the maximum time between two subsequent tickets redemptions. - /// That's required as nym-apis will purge all ticket information for tickets older than 30 days. + /// That's required as nym-apis will purge all ticket information for tickets older than maximum validity. #[serde(with = "humantime_serde")] pub maximum_time_between_redemption: Duration, } @@ -511,7 +511,28 @@ impl ZkNymTicketHandlerDebug { pub const DEFAULT_PENDING_POLLER: Duration = Duration::from_secs(300); pub const DEFAULT_MINIMUM_API_QUORUM: f32 = 0.8; pub const DEFAULT_MINIMUM_REDEMPTION_TICKETS: usize = 100; - pub const DEFAULT_MAXIMUM_TIME_BETWEEN_REDEMPTION: Duration = Duration::from_secs(86400 * 25); + + // use min(4/5 of max validity, validity - 1), but making sure it's no greater than 1 day + // ASSUMPTION: our validity period is AT LEAST 2 days + // + // this could have been a constant, but it's more readable as a function + pub const fn default_maximum_time_between_redemption() -> Duration { + let desired_secs = TICKETBOOK_VALIDITY_DAYS * (86400 * 4) / 5; + let desired_secs_alt = (TICKETBOOK_VALIDITY_DAYS - 1) * 86400; + + // can't use `min` in const context + let target_secs = if desired_secs < desired_secs_alt { + desired_secs + } else { + desired_secs_alt + }; + + assert!( + target_secs < 86400, + "the maximum time between redemption can't be lower than 1 day!" + ); + Duration::from_secs(target_secs as u64) + } } impl Default for ZkNymTicketHandlerDebug { @@ -521,7 +542,7 @@ impl Default for ZkNymTicketHandlerDebug { pending_poller: Self::DEFAULT_PENDING_POLLER, minimum_api_quorum: Self::DEFAULT_MINIMUM_API_QUORUM, minimum_redemption_tickets: Self::DEFAULT_MINIMUM_REDEMPTION_TICKETS, - maximum_time_between_redemption: Self::DEFAULT_MAXIMUM_TIME_BETWEEN_REDEMPTION, + maximum_time_between_redemption: Self::default_maximum_time_between_redemption(), } } } diff --git a/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs b/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs index 16670fe132..dcc1bb62bf 100644 --- a/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs +++ b/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs @@ -122,7 +122,7 @@ pub(crate) struct CredentialHandlerConfig { pub(crate) minimum_redemption_tickets: usize, /// Specifies the maximum time between two subsequent tickets redemptions. - /// That's required as nym-apis will purge all ticket information for tickets older than 30 days. + /// That's required as nym-apis will purge all ticket information for tickets older than maximum validity. pub(crate) maximum_time_between_redemption: Duration, } diff --git a/nym-api/src/ecash/state/helpers.rs b/nym-api/src/ecash/state/helpers.rs index ffcab88644..2fcfd18a31 100644 --- a/nym-api/src/ecash/state/helpers.rs +++ b/nym-api/src/ecash/state/helpers.rs @@ -56,7 +56,7 @@ pub(crate) async fn prepare_partial_bloomfilter_builder( .try_load_partial_bloomfilter_bitmap(date, params_id) .await? else { - log::warn!("missing double spending bloomfilter bitmap for {date} (if this API hasn't been running for at least 30 days since 'ecash'-based zk-nyms were introduced this is expected)"); + log::warn!("missing double spending bloomfilter bitmap for {date} (if this API hasn't been running for at least {days} day(s) since 'ecash'-based zk-nyms were introduced this is expected)"); continue; }; if !filter_builder.add_bytes(&bitmap) { diff --git a/nym-node/src/config/entry_gateway.rs b/nym-node/src/config/entry_gateway.rs index dd3670f42f..8a0144db71 100644 --- a/nym-node/src/config/entry_gateway.rs +++ b/nym-node/src/config/entry_gateway.rs @@ -5,7 +5,7 @@ use crate::config::helpers::ephemeral_gateway_config; use crate::config::persistence::EntryGatewayPaths; use crate::config::Config; use crate::error::EntryGatewayError; -use nym_config::defaults::DEFAULT_CLIENT_LISTENING_PORT; +use nym_config::defaults::{DEFAULT_CLIENT_LISTENING_PORT, TICKETBOOK_VALIDITY_DAYS}; use nym_config::helpers::inaddr_any; use nym_config::serde_helpers::de_maybe_port; use nym_gateway::node::LocalAuthenticatorOpts; @@ -90,7 +90,7 @@ pub struct ZkNymTicketHandlerDebug { pub minimum_redemption_tickets: usize, /// Specifies the maximum time between two subsequent tickets redemptions. - /// That's required as nym-apis will purge all ticket information for tickets older than 30 days. + /// That's required as nym-apis will purge all ticket information for tickets older than maximum validity. #[serde(with = "humantime_serde")] pub maximum_time_between_redemption: Duration, } @@ -100,7 +100,28 @@ impl ZkNymTicketHandlerDebug { pub const DEFAULT_PENDING_POLLER: Duration = Duration::from_secs(300); pub const DEFAULT_MINIMUM_API_QUORUM: f32 = 0.8; pub const DEFAULT_MINIMUM_REDEMPTION_TICKETS: usize = 100; - pub const DEFAULT_MAXIMUM_TIME_BETWEEN_REDEMPTION: Duration = Duration::from_secs(86400 * 25); + + // use min(4/5 of max validity, validity - 1), but making sure it's no greater than 1 day + // ASSUMPTION: our validity period is AT LEAST 2 days + // + // this could have been a constant, but it's more readable as a function + pub const fn default_maximum_time_between_redemption() -> Duration { + let desired_secs = TICKETBOOK_VALIDITY_DAYS * (86400 * 4) / 5; + let desired_secs_alt = (TICKETBOOK_VALIDITY_DAYS - 1) * 86400; + + // can't use `min` in const context + let target_secs = if desired_secs < desired_secs_alt { + desired_secs + } else { + desired_secs_alt + }; + + assert!( + target_secs < 86400, + "the maximum time between redemption can't be lower than 1 day!" + ); + Duration::from_secs(target_secs as u64) + } } impl Default for ZkNymTicketHandlerDebug { @@ -110,7 +131,7 @@ impl Default for ZkNymTicketHandlerDebug { pending_poller: Self::DEFAULT_PENDING_POLLER, minimum_api_quorum: Self::DEFAULT_MINIMUM_API_QUORUM, minimum_redemption_tickets: Self::DEFAULT_MINIMUM_REDEMPTION_TICKETS, - maximum_time_between_redemption: Self::DEFAULT_MAXIMUM_TIME_BETWEEN_REDEMPTION, + maximum_time_between_redemption: Self::default_maximum_time_between_redemption(), } } }