diff --git a/examples/tic-tac-toe-gui/src/main.rs b/examples/tic-tac-toe-gui/src/main.rs index 5fdd951..adeadcd 100644 --- a/examples/tic-tac-toe-gui/src/main.rs +++ b/examples/tic-tac-toe-gui/src/main.rs @@ -132,12 +132,6 @@ impl TicTacToeApp { &serialized_game_state, content_topic, 0, - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_millis() - .try_into() - .unwrap(), Vec::new(), false, ); diff --git a/examples/toy-chat/src/main.rs b/examples/toy-chat/src/main.rs index 3c26650..4547765 100644 --- a/examples/toy-chat/src/main.rs +++ b/examples/toy-chat/src/main.rs @@ -2,13 +2,13 @@ mod protocol; use crate::protocol::{Chat2Message, TOY_CHAT_CONTENT_TOPIC}; use tokio::task; -use chrono::Utc; use crossterm::{ event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; use prost::Message; +use chrono::Utc; use std::io::Write; use std::sync::{Arc, RwLock}; use std::{error::Error, io}; @@ -135,8 +135,19 @@ impl App { impl App { async fn retrieve_history(&mut self) { + let one_day_in_secs = 60 * 60 * 24; + let time_start = (Duration::from_secs(Utc::now().timestamp() as u64) + - Duration::from_secs(one_day_in_secs)) + .as_nanos() as usize; + let include_data = true; - let messages = self.waku.store_query(None, vec![TOY_CHAT_CONTENT_TOPIC.clone()], STORE_NODE, include_data).await.unwrap(); + + let messages = self.waku.store_query(None, + vec![TOY_CHAT_CONTENT_TOPIC.clone()], + STORE_NODE, + include_data, + Some(time_start), + None).await.unwrap(); let messages:Vec<_> = messages .iter() .map(|store_resp_msg| { @@ -180,7 +191,6 @@ impl App { buff, TOY_CHAT_CONTENT_TOPIC.clone(), 1, - Utc::now().timestamp_nanos() as usize, meta, false, ); diff --git a/waku-bindings/src/general/mod.rs b/waku-bindings/src/general/mod.rs index 32b9008..0ea374c 100644 --- a/waku-bindings/src/general/mod.rs +++ b/waku-bindings/src/general/mod.rs @@ -4,9 +4,11 @@ pub mod contenttopic; pub mod libwaku_response; pub mod messagehash; pub mod pubsubtopic; +pub mod time; pub mod waku_decode; // crates +use crate::general::time::get_now_in_nanosecs; use contenttopic::WakuContentTopic; use serde::{Deserialize, Serialize}; use serde_aux::prelude::*; @@ -32,7 +34,7 @@ pub struct WakuMessage { pub version: WakuMessageVersion, /// Unix timestamp in nanoseconds #[serde(deserialize_with = "deserialize_number_from_string")] - pub timestamp: usize, + pub timestamp: u64, #[serde(with = "base64_serde", default = "Vec::new")] pub meta: Vec, #[serde(default)] @@ -67,7 +69,6 @@ impl WakuMessage { payload: PAYLOAD, content_topic: WakuContentTopic, version: WakuMessageVersion, - timestamp: usize, meta: META, ephemeral: bool, ) -> Self { @@ -78,7 +79,7 @@ impl WakuMessage { payload, content_topic, version, - timestamp, + timestamp: get_now_in_nanosecs(), meta, ephemeral, _extras: Default::default(), diff --git a/waku-bindings/src/general/time.rs b/waku-bindings/src/general/time.rs new file mode 100644 index 0000000..2024b84 --- /dev/null +++ b/waku-bindings/src/general/time.rs @@ -0,0 +1,7 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +pub fn get_now_in_nanosecs() -> u64 { + let now = SystemTime::now(); + let since_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards"); + since_epoch.as_secs() * 1_000_000_000 + since_epoch.subsec_nanos() as u64 +} diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index 605998f..1ca219c 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -12,7 +12,6 @@ mod store; // std pub use aes_gcm::Key; -use chrono::Utc; pub use multiaddr::Multiaddr; pub use secp256k1::{PublicKey, SecretKey}; use std::marker::PhantomData; @@ -31,8 +30,6 @@ pub use config::WakuNodeConfig; pub use events::{WakuEvent, WakuMessageEvent}; pub use relay::waku_create_content_topic; -use std::time::SystemTime; - // Define state marker types pub struct Initialized; pub struct Running; @@ -125,20 +122,7 @@ impl WakuNodeHandle { timeout: Option, ) -> Result { let content_topic = WakuContentTopic::new("waku", "2", content_topic_name, Encoding::Proto); - let message = WakuMessage::new( - msg_txt, - content_topic, - 0, - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_millis() - .try_into() - .unwrap(), - Vec::new(), - false, - ); - + let message = WakuMessage::new(msg_txt, content_topic, 0, Vec::new(), false); relay::waku_relay_publish_message(&self.ctx, &message, pubsub_topic, timeout).await } @@ -193,12 +177,9 @@ impl WakuNodeHandle { content_topics: Vec, peer_addr: &str, include_data: bool, // is true, resp contains payload, etc. Only msg_hashes otherwise + time_start: Option, // unix time nanoseconds + time_end: Option, // unix time nanoseconds ) -> Result> { - let one_day_in_secs = 60 * 60 * 24; - let time_start = (Duration::from_secs(Utc::now().timestamp() as u64) - - Duration::from_secs(one_day_in_secs)) - .as_nanos() as usize; - let mut cursor: Option = None; let mut messages: Vec = Vec::new(); @@ -211,12 +192,12 @@ impl WakuNodeHandle { include_data, pubsub_topic.clone(), content_topics.clone(), - Some(time_start), // time_start - None, // end_time - None, // message_hashes - cursor, // pagination_cursor - true, // pagination_forward - Some(25), // pagination_limit, + time_start, + time_end, + None, // message_hashes + cursor, // pagination_cursor + true, // pagination_forward + Some(25), // pagination_limit, peer_addr, None, // timeout_millis )