Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client::get_slot_index #1158

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(self.get_protocol_parameters().await?.slot_index(current_time))
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Loading