diff --git a/node/src/chain/acceptor.rs b/node/src/chain/acceptor.rs index 6220010ffa..0f9244a658 100644 --- a/node/src/chain/acceptor.rs +++ b/node/src/chain/acceptor.rs @@ -24,10 +24,10 @@ use dusk_consensus::operations::Voter; use execution_core::stake::{Withdraw, STAKE_CONTRACT}; use metrics::{counter, gauge, histogram}; use node_data::message::payload::Vote; -use node_data::{Serializable, StepName}; +use node_data::{get_current_timestamp, Serializable, StepName}; use std::collections::BTreeMap; use std::sync::{Arc, LazyLock}; -use std::time::{self, Duration, UNIX_EPOCH}; +use std::time::Duration; use tokio::sync::mpsc::Sender; use tokio::sync::RwLock; use tracing::{debug, info, warn}; @@ -845,10 +845,7 @@ impl Acceptor { // Delete any rocksdb record related to this block t.delete_block(&b)?; - let now = time::SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|n| n.as_secs()) - .expect("valid timestamp"); + let now = get_current_timestamp(); // Attempt to resubmit transactions back to mempool. // An error here is not considered critical. diff --git a/node/src/chain/header_validation.rs b/node/src/chain/header_validation.rs index 2dc72c29ec..693bd55be4 100644 --- a/node/src/chain/header_validation.rs +++ b/node/src/chain/header_validation.rs @@ -18,10 +18,9 @@ use execution_core::stake::EPOCH; use node_data::ledger::{Fault, InvalidFault, Seed, Signature}; use node_data::message::payload::{RatificationResult, Vote}; use node_data::message::ConsensusHeader; -use node_data::{ledger, StepName}; +use node_data::{get_current_timestamp, ledger, StepName}; use std::collections::BTreeMap; use std::sync::Arc; -use std::time::{SystemTime, UNIX_EPOCH}; use thiserror::Error; use tokio::sync::RwLock; use tracing::info; @@ -114,10 +113,7 @@ impl<'a, DB: database::DB> Validator<'a, DB> { return Err(anyhow!("block time is less than minimum block time")); } - let local_time = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|n| n.as_secs()) - .expect("valid unix epoch"); + let local_time = get_current_timestamp(); if candidate_block.timestamp > local_time + MARGIN_TIMESTAMP { return Err(anyhow!( diff --git a/node/src/mempool.rs b/node/src/mempool.rs index f38869927c..5289192674 100644 --- a/node/src/mempool.rs +++ b/node/src/mempool.rs @@ -14,10 +14,10 @@ use conf::{ DEFAULT_DOWNLOAD_REDUNDANCY, DEFAULT_EXPIRY_TIME, DEFAULT_IDLE_INTERVAL, }; use node_data::events::{Event, TransactionEvent}; +use node_data::get_current_timestamp; use node_data::ledger::Transaction; use node_data::message::{payload, AsyncQueue, Payload, Topics}; use std::sync::Arc; -use std::time::{self, UNIX_EPOCH}; use thiserror::Error; use tokio::sync::mpsc::Sender; use tokio::sync::RwLock; @@ -92,7 +92,7 @@ impl self.conf.idle_interval.unwrap_or(DEFAULT_IDLE_INTERVAL); let mempool_expiry = - self.conf.mempool_expiry.unwrap_or(DEFAULT_EXPIRY_TIME); + self.conf.mempool_expiry.unwrap_or(DEFAULT_EXPIRY_TIME).as_secs(); // Mempool service loop let mut on_idle_event = tokio::time::interval(idle_interval); @@ -102,15 +102,13 @@ impl _ = on_idle_event.tick() => { info!(event = "mempool_idle", interval = ?idle_interval); - let expiration_time = time::SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("valid timestamp") + let expiration_time = get_current_timestamp() .checked_sub(mempool_expiry) .expect("valid duration"); // Remove expired transactions from the mempool db.read().await.update(|db| { - let expired_txs = db.get_expired_txs(expiration_time.as_secs())?; + let expired_txs = db.get_expired_txs(expiration_time)?; for tx_id in expired_txs { info!(event = "expired_tx", hash = hex::encode(tx_id)); if db.delete_tx(tx_id)? { @@ -216,11 +214,9 @@ impl MempoolSrv { events.push(TransactionEvent::Included(tx)); // Persist transaction in mempool storage - let now = time::SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("valid timestamp"); + let now = get_current_timestamp(); - db.add_tx(tx, now.as_secs()) + db.add_tx(tx, now) })?; tracing::info!( diff --git a/node/src/network.rs b/node/src/network.rs index d4d91758e2..79e643388c 100644 --- a/node/src/network.rs +++ b/node/src/network.rs @@ -12,11 +12,10 @@ use async_trait::async_trait; use kadcast::config::Config; use kadcast::{MessageInfo, Peer}; use metrics::counter; +use node_data::get_current_timestamp_safe; use node_data::message::payload::{GetResource, Inv}; -use node_data::message::AsyncQueue; -use node_data::message::Metadata; +use node_data::message::{AsyncQueue, Metadata}; use std::sync::atomic::{AtomicU64, Ordering}; -use std::time::{SystemTime, UNIX_EPOCH}; use tokio::sync::RwLock; use tracing::{error, info, trace}; @@ -206,16 +205,8 @@ impl crate::Network for Kadcast { ttl_as_sec: Option, hops_limit: u16, ) -> anyhow::Result<()> { - let ttl_as_sec = ttl_as_sec.map_or_else( - || u64::MAX, - |v| { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap_or_default() - .as_secs() - + v - }, - ); + let ttl_as_sec = ttl_as_sec + .map_or_else(|| u64::MAX, |v| get_current_timestamp_safe() + v); self.send_to_alive_peers( &Message::new_get_resource(GetResource::new(