Skip to content

Commit

Permalink
node: use get_current_timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
fed-franz committed Aug 21, 2024
1 parent 282063c commit 0955d1e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
9 changes: 3 additions & 6 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -845,10 +845,7 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
// 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.
Expand Down
8 changes: 2 additions & 6 deletions node/src/chain/header_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
Expand Down
16 changes: 6 additions & 10 deletions node/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<N: Network, DB: database::DB, VM: vm::VMExecution>
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);
Expand All @@ -102,15 +102,13 @@ impl<N: Network, DB: database::DB, VM: vm::VMExecution>
_ = 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)? {
Expand Down Expand Up @@ -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!(
Expand Down
17 changes: 4 additions & 13 deletions node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -206,16 +205,8 @@ impl<const N: usize> crate::Network for Kadcast<N> {
ttl_as_sec: Option<u64>,
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(
Expand Down

0 comments on commit 0955d1e

Please sign in to comment.