Skip to content

Commit

Permalink
Merge branch '2.0' into native-token-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Nov 20, 2023
2 parents ad96d8e + 8644dec commit 1581b6f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions sdk/src/client/api/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -115,20 +115,23 @@ impl Client {

// Returns the slot index corresponding to the current timestamp.
pub async fn get_slot_index(&self) -> Result<SlotIndex> {
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(&current_time) {
if !(tangle_time - FIVE_MINUTES_IN_NANOSECONDS..tangle_time + FIVE_MINUTES_IN_NANOSECONDS)
.contains(&current_time)
{
return Err(Error::TimeNotSynced {
current_time,
tangle_time,
});
}
}

// TODO double check with TIP if this should be seconds or nanoseconds
Ok(network_info.protocol_parameters.slot_index(current_time))
}
}
2 changes: 1 addition & 1 deletion sdk/src/client/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1581b6f

Please sign in to comment.