Skip to content

Commit

Permalink
Add Client::get_slot_index (#1158)
Browse files Browse the repository at this point in the history
* initial attempt

* oop

* reuse network info params
  • Loading branch information
Alexandcoats authored Sep 7, 2023
1 parent 1add7b7 commit 9aa0dd6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
15 changes: 4 additions & 11 deletions sdk/src/client/api/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,13 @@ impl Client {
Ok(selected_inputs)
}

/// Returns the local time checked with the tangle timestamp. If the difference is larger than 5
/// minutes an error is returned to prevent locking outputs by accident for a wrong time.
pub async fn get_time_checked(&self) -> Result<u32> {
let current_time = unix_timestamp_now().as_secs() as u32;
// 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 network_info = self.get_network_info().await?;

if let Some(tangle_time) = network_info.tangle_time {
let tangle_time = tangle_time as u32;
// 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) {
return Err(Error::TimeNotSynced {
Expand All @@ -133,11 +131,6 @@ impl Client {
}
}

Ok(current_time)
}

// TODO
pub async fn get_slot_index(&self) -> Result<SlotIndex> {
todo!()
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: u32 = 300;
pub(crate) const FIVE_MINUTES_IN_SECONDS: u64 = 300;
/// 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
4 changes: 2 additions & 2 deletions sdk/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ pub enum Error {
#[error("local time {current_time} doesn't match the tangle time: {tangle_time}")]
TimeNotSynced {
/// The local time.
current_time: u32,
current_time: u64,
/// The tangle time.
tangle_time: u32,
tangle_time: u64,
},
/// The semantic validation of a transaction failed.
#[error("the semantic validation of a transaction failed with conflict reason: {} - {0:?}", *.0 as u8)]
Expand Down

0 comments on commit 9aa0dd6

Please sign in to comment.