From 8644dec99f860732e7e3de8db29d7ab7aef96c0e Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Mon, 20 Nov 2023 11:07:49 +0100 Subject: [PATCH] Fix get_slot_index with nanoseconds relative_accepted_tangle_time (#1640) --- sdk/src/client/api/high_level.rs | 9 ++++++--- sdk/src/client/constants.rs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sdk/src/client/api/high_level.rs b/sdk/src/client/api/high_level.rs index 2926091520..f606cfac23 100644 --- a/sdk/src/client/api/high_level.rs +++ b/sdk/src/client/api/high_level.rs @@ -8,7 +8,7 @@ use futures::{StreamExt, TryStreamExt}; use crate::{ client::{ api::input_selection::Error as InputSelectionError, - constants::FIVE_MINUTES_IN_SECONDS, + constants::FIVE_MINUTES_IN_NANOSECONDS, error::{Error, Result}, node_api::indexer::query_parameters::BasicOutputQueryParameters, unix_timestamp_now, Client, @@ -115,13 +115,15 @@ impl Client { // Returns the slot index corresponding to the current timestamp. pub async fn get_slot_index(&self) -> Result { - let current_time = unix_timestamp_now().as_secs(); + let current_time = unix_timestamp_now().as_nanos() as u64; let network_info = self.get_network_info().await?; if let Some(tangle_time) = network_info.tangle_time { // Check the local time is in the range of +-5 minutes of the node to prevent locking funds by accident - if !(tangle_time - FIVE_MINUTES_IN_SECONDS..tangle_time + FIVE_MINUTES_IN_SECONDS).contains(¤t_time) { + if !(tangle_time - FIVE_MINUTES_IN_NANOSECONDS..tangle_time + FIVE_MINUTES_IN_NANOSECONDS) + .contains(¤t_time) + { return Err(Error::TimeNotSynced { current_time, tangle_time, @@ -129,6 +131,7 @@ impl Client { } } + // TODO double check with TIP if this should be seconds or nanoseconds Ok(network_info.protocol_parameters.slot_index(current_time)) } } diff --git a/sdk/src/client/constants.rs b/sdk/src/client/constants.rs index 487e97cd15..b4d9b0252f 100644 --- a/sdk/src/client/constants.rs +++ b/sdk/src/client/constants.rs @@ -17,7 +17,7 @@ pub(crate) const DEFAULT_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", #[cfg(not(target_family = "wasm"))] pub(crate) const MAX_PARALLEL_API_REQUESTS: usize = 100; /// Max allowed difference between the local time and latest milestone time, 5 minutes in seconds -pub(crate) const FIVE_MINUTES_IN_SECONDS: u64 = 300; +pub(crate) const FIVE_MINUTES_IN_NANOSECONDS: u64 = 300_000_000_000; /// Delay for caching a node info response in WASM runtime #[cfg(target_family = "wasm")] pub(crate) const CACHE_NETWORK_INFO_TIMEOUT_IN_SECONDS: u32 = 60;