From 862abc537a4ebbefea298b06a331b169961b9207 Mon Sep 17 00:00:00 2001 From: dasman Date: Tue, 1 Oct 2024 21:02:50 +0300 Subject: [PATCH] event_graph: go full millis (this is a breaking change) --- Cargo.lock | 1 - bin/darkirc/src/irc/client.rs | 26 +------------------------- bin/tau/taud/Cargo.toml | 1 - bin/tau/taud/src/main.rs | 26 +------------------------- src/event_graph/event.rs | 23 +++++++---------------- src/event_graph/mod.rs | 12 ++++++------ src/event_graph/util.rs | 22 +++++++++++----------- 7 files changed, 26 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25386198d..555317222 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7029,7 +7029,6 @@ dependencies = [ "log", "rand 0.8.5", "ring 0.17.8", - "semver 1.0.23", "serde", "signal-hook", "signal-hook-async-std", diff --git a/bin/darkirc/src/irc/client.rs b/bin/darkirc/src/irc/client.rs index 6f2c8f1f1..d54621427 100644 --- a/bin/darkirc/src/irc/client.rs +++ b/bin/darkirc/src/irc/client.rs @@ -47,8 +47,6 @@ use super::{ const PENALTY_LIMIT: usize = 5; -const PROTOCOL_VERSION_SECS_TIMESTAMPS: semver::Version = semver::Version::new(0, 5, 0); - /// Reply types, we can either send server replies, or client replies. pub enum ReplyType { /// Server reply, we have to use numerics @@ -192,29 +190,7 @@ impl Client { } // Otherwise, broadcast it - let connected_peers = self.server.darkirc.p2p.hosts().peers(); - let mut peers_millis = vec![]; - let mut peers_seconds = vec![]; - for peer in connected_peers { - let peer_version = peer.version.lock().await.clone(); - if let Some(ref peer_version) = peer_version { - if peer_version.version > PROTOCOL_VERSION_SECS_TIMESTAMPS{ - peers_millis.push(peer) - } else { - peers_seconds.push(peer) - } - } - } - - if !peers_millis.is_empty() { - self.server.darkirc.p2p.broadcast_to(&EventPut(event.clone()), &peers_millis).await; - } - if !peers_seconds.is_empty() { - let mut event = event; - event.timestamp /= 1000; - self.server.darkirc.p2p.broadcast_to(&EventPut(event), &peers_seconds).await; - } - // self.server.darkirc.p2p.broadcast(&EventPut(event)).await; + self.server.darkirc.p2p.broadcast(&EventPut(event)).await; } } } diff --git a/bin/tau/taud/Cargo.toml b/bin/tau/taud/Cargo.toml index f4102ea9f..194bd7002 100644 --- a/bin/tau/taud/Cargo.toml +++ b/bin/tau/taud/Cargo.toml @@ -37,7 +37,6 @@ ring = "0.17.8" # Encoding and parsing bs58 = "0.5.1" toml = "0.8.19" -semver = "1.0.23" # Misc async-trait = "0.1.83" diff --git a/bin/tau/taud/src/main.rs b/bin/tau/taud/src/main.rs index b84f6815a..c05d4349d 100644 --- a/bin/tau/taud/src/main.rs +++ b/bin/tau/taud/src/main.rs @@ -76,8 +76,6 @@ use crate::{ settings::{Args, CONFIG_FILE, CONFIG_FILE_CONTENTS}, }; -const PROTOCOL_VERSION_SECS_TIMESTAMPS: semver::Version = semver::Version::new(0, 5, 0); - struct Workspace { read_key: ChaChaBox, write_key: Option, @@ -320,29 +318,7 @@ async fn start_sync_loop( error!(target: "taud", "Failed inserting new event to DAG: {}", e); } else { // Otherwise, broadcast it - let connected_peers = p2p.hosts().peers(); - let mut peers_millis = vec![]; - let mut peers_seconds = vec![]; - for peer in connected_peers { - let peer_version = peer.version.lock().await.clone(); - if let Some(ref peer_version) = peer_version { - if peer_version.version > PROTOCOL_VERSION_SECS_TIMESTAMPS { - peers_millis.push(peer) - } else { - peers_seconds.push(peer) - } - } - } - - if !peers_millis.is_empty() { - p2p.broadcast_to(&EventPut(event.clone()), &peers_millis).await; - } - if !peers_seconds.is_empty() { - let mut event = event; - event.timestamp /= 1000; - p2p.broadcast_to(&EventPut(event), &peers_seconds).await; - } - // p2p.broadcast(&EventPut(event)).await; + p2p.broadcast(&EventPut(event)).await; } } } diff --git a/src/event_graph/event.rs b/src/event_graph/event.rs index fc890a3b6..a679ad6f1 100644 --- a/src/event_graph/event.rs +++ b/src/event_graph/event.rs @@ -66,9 +66,7 @@ impl Event { /// Hash the [`Event`] to retrieve its ID pub fn id(&self) -> blake3::Hash { let mut hasher = blake3::Hasher::new(); - let timestamp = - if self.timestamp > 1e10 as u64 { self.timestamp / 1000 } else { self.timestamp }; - timestamp.encode(&mut hasher).unwrap(); + self.timestamp.encode(&mut hasher).unwrap(); self.content.encode(&mut hasher).unwrap(); self.parents.encode(&mut hasher).unwrap(); self.layer.encode(&mut hasher).unwrap(); @@ -103,20 +101,16 @@ impl Event { return Ok(false) } - let timestamp = - if self.timestamp > 1e10 as u64 { self.timestamp / 1000 } else { self.timestamp }; - // Check if the event timestamp is after genesis timestamp - if timestamp < genesis_timestamp - (EVENT_TIME_DRIFT / 1000) { + if self.timestamp < genesis_timestamp - EVENT_TIME_DRIFT { return Ok(false) } // If a rotation has been set, check if the event timestamp // is after the next genesis timestamp if days_rotation > 0 { - let next_genesis_timestamp = - next_rotation_timestamp(INITIAL_GENESIS / 1000, days_rotation); - if timestamp > next_genesis_timestamp + (EVENT_TIME_DRIFT / 1000) { + let next_genesis_timestamp = next_rotation_timestamp(INITIAL_GENESIS, days_rotation); + if self.timestamp > next_genesis_timestamp + EVENT_TIME_DRIFT { return Ok(false) } } @@ -182,13 +176,10 @@ impl Event { return false } - let timestamp = - if self.timestamp > 1e10 as u64 { self.timestamp / 1000 } else { self.timestamp }; - // Check if the event is too old or too new - let now = UNIX_EPOCH.elapsed().unwrap().as_secs(); - let too_old = timestamp < (now - (EVENT_TIME_DRIFT / 1000)); - let too_new = timestamp > (now + (EVENT_TIME_DRIFT / 1000)); + let now = UNIX_EPOCH.elapsed().unwrap().as_millis() as u64; + let too_old = self.timestamp < now - EVENT_TIME_DRIFT; + let too_new = self.timestamp > now + EVENT_TIME_DRIFT; if too_old || too_new { return false } diff --git a/src/event_graph/mod.rs b/src/event_graph/mod.rs index 50e88dee7..7761e490d 100644 --- a/src/event_graph/mod.rs +++ b/src/event_graph/mod.rs @@ -34,14 +34,14 @@ use smol::{ use tinyjson::JsonValue::{self}; use crate::{ - event_graph::util::{replayer_log, seconds_until_next_rotation}, + event_graph::util::replayer_log, net::P2pPtr, rpc::{ jsonrpc::{JsonResponse, JsonResult}, util::json_map, }, system::{ - sleep, timeout::timeout, Publisher, PublisherPtr, StoppableTask, StoppableTaskPtr, + msleep, timeout::timeout, Publisher, PublisherPtr, StoppableTask, StoppableTaskPtr, Subscription, }, Error, Result, @@ -57,7 +57,7 @@ use proto::{EventRep, EventReq, TipRep, TipReq}; /// Utility functions pub mod util; -use util::{generate_genesis, next_rotation_timestamp}; +use util::{generate_genesis, millis_until_next_rotation, next_rotation_timestamp}; // Debugging event graph pub mod deg; @@ -512,7 +512,7 @@ impl EventGraph { loop { // Find the next rotation timestamp: - let next_rotation = next_rotation_timestamp(INITIAL_GENESIS / 1000, days_rotation); + let next_rotation = next_rotation_timestamp(INITIAL_GENESIS, days_rotation); // Prepare the new genesis event let current_genesis = Event { @@ -523,10 +523,10 @@ impl EventGraph { }; // Sleep until it's time to rotate. - let s = seconds_until_next_rotation(next_rotation); + let s = millis_until_next_rotation(next_rotation); debug!(target: "event_graph::dag_prune_task()", "Sleeping {}s until next DAG prune", s); - sleep(s).await; + msleep(s).await; debug!(target: "event_graph::dag_prune_task()", "Rotation period reached"); // Trigger DAG prune diff --git a/src/event_graph/util.rs b/src/event_graph/util.rs index 7377faadb..26bec4c3e 100644 --- a/src/event_graph/util.rs +++ b/src/event_graph/util.rs @@ -52,20 +52,20 @@ pub(super) fn midnight_timestamp(days: i64) -> u64 { let cur_midnight = (now / DAY) * DAY; // Adjust for days_from_now - ((cur_midnight + (DAY * days)) / 1000) as u64 + (cur_midnight + (DAY * days)) as u64 } /// Calculate the number of days since a given midnight timestamp. pub(super) fn days_since(midnight_ts: u64) -> u64 { // Get current time - let now = UNIX_EPOCH.elapsed().unwrap().as_secs(); + let now = UNIX_EPOCH.elapsed().unwrap().as_millis() as u64; // Calculate the difference between the current timestamp // and the given midnight timestamp let elapsed_seconds = now - midnight_ts; // Convert the elapsed seconds into days - elapsed_seconds / (DAY / 1000) as u64 + elapsed_seconds / DAY as u64 } /// Calculate the timestamp of the next DAG rotation. @@ -99,11 +99,11 @@ pub fn next_rotation_timestamp(starting_timestamp: u64, rotation_period: u64) -> /// Calculate the time in milliseconds until the next_rotation, given /// as a timestamp. /// `next_rotation` here represents a timestamp in UNIX epoch format. -pub fn seconds_until_next_rotation(next_rotation: u64) -> u64 { +pub fn millis_until_next_rotation(next_rotation: u64) -> u64 { // Store `now` in a variable in order to avoid a TOCTOU error. // There may be a drift of one second between this panic check and // the return value if we get unlucky. - let now = UNIX_EPOCH.elapsed().unwrap().as_secs(); + let now = UNIX_EPOCH.elapsed().unwrap().as_millis() as u64; if next_rotation < now { panic!("Next rotation timestamp is in the past"); } @@ -117,7 +117,7 @@ pub fn generate_genesis(days_rotation: u64) -> Event { INITIAL_GENESIS } else { // First check how many days passed since initial genesis. - let days_passed = days_since(INITIAL_GENESIS / 1000); + let days_passed = days_since(INITIAL_GENESIS); // Calculate the number of days_rotation intervals since INITIAL_GENESIS let rotations_since_genesis = days_passed / days_rotation; @@ -126,7 +126,7 @@ pub fn generate_genesis(days_rotation: u64) -> Event { INITIAL_GENESIS + (rotations_since_genesis * days_rotation * DAY as u64) }; Event { - timestamp: timestamp / 1000, // to keep the genesis hash equal to old nodes. + timestamp, content: GENESIS_CONTENTS.to_vec(), parents: [NULL_ID; N_EVENT_PARENTS], layer: 0, @@ -225,7 +225,7 @@ mod tests { // we should get tomorrow's timestamp. // This is a special case. let midnight_today: u64 = midnight_timestamp(0); - let midnight_tomorrow = midnight_today + 86_400u64; // add a day + let midnight_tomorrow = midnight_today + 86_400_000u64; // add a day assert_eq!(midnight_tomorrow, next_rotation_timestamp(midnight_today, 1)); } @@ -242,12 +242,12 @@ mod tests { } #[test] - fn test_seconds_until_next_rotation_is_within_rotation_interval() { + fn test_millis_until_next_rotation_is_within_rotation_interval() { let days_rotation = 1u64; // The amount of time in seconds between rotations. - let rotation_interval = days_rotation * 86_400u64; + let rotation_interval = days_rotation * 86_400_000u64; let next_rotation_timestamp = next_rotation_timestamp(INITIAL_GENESIS, days_rotation); - let s = seconds_until_next_rotation(next_rotation_timestamp); + let s = millis_until_next_rotation(next_rotation_timestamp); assert!(s < rotation_interval); } }