better time management

This commit is contained in:
Ivan Folgueira Bande 2024-12-22 15:54:55 +01:00
parent 40e908feca
commit 3cbb259b80
No known key found for this signature in database
GPG Key ID: 3C117481F89E24A7
5 changed files with 33 additions and 40 deletions

View File

@ -132,12 +132,6 @@ impl TicTacToeApp<Running> {
&serialized_game_state,
content_topic,
0,
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis()
.try_into()
.unwrap(),
Vec::new(),
false,
);

View File

@ -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<Initialized> {
impl App<Running> {
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<Running> {
buff,
TOY_CHAT_CONTENT_TOPIC.clone(),
1,
Utc::now().timestamp_nanos() as usize,
meta,
false,
);

View File

@ -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<u8>,
#[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(),

View File

@ -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
}

View File

@ -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<Running> {
timeout: Option<Duration>,
) -> Result<MessageHash> {
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<Running> {
content_topics: Vec<WakuContentTopic>,
peer_addr: &str,
include_data: bool, // is true, resp contains payload, etc. Only msg_hashes otherwise
time_start: Option<usize>, // unix time nanoseconds
time_end: Option<usize>, // unix time nanoseconds
) -> Result<Vec<StoreWakuMessageResponse>> {
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<MessageHash> = None;
let mut messages: Vec<StoreWakuMessageResponse> = Vec::new();
@ -211,12 +192,12 @@ impl WakuNodeHandle<Running> {
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
)