From 57dc406e7c64e6b4d2fa1736b8826ef540dcf532 Mon Sep 17 00:00:00 2001 From: Goshawk Date: Tue, 20 Aug 2024 15:47:34 +0300 Subject: [PATCH] node: Set tx timestamp in add_tx --- node/src/chain/acceptor.rs | 9 ++------- node/src/database.rs | 2 +- node/src/database/rocksdb.rs | 23 ++++++++++++++--------- node/src/mempool.rs | 10 +++------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/node/src/chain/acceptor.rs b/node/src/chain/acceptor.rs index 6220010ffa..1850985555 100644 --- a/node/src/chain/acceptor.rs +++ b/node/src/chain/acceptor.rs @@ -27,7 +27,7 @@ use node_data::message::payload::Vote; use node_data::{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,16 +845,11 @@ 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"); - // Attempt to resubmit transactions back to mempool. // An error here is not considered critical. // Txs timestamp is reset here for tx in b.txs().iter() { - if let Err(e) = Mempool::add_tx(t, tx, now) { + if let Err(e) = Mempool::add_tx(t, tx) { warn!("failed to resubmit transactions: {e}") }; } diff --git a/node/src/database.rs b/node/src/database.rs index 76a96f9df3..642fc5259a 100644 --- a/node/src/database.rs +++ b/node/src/database.rs @@ -120,7 +120,7 @@ pub trait Candidate { pub trait Mempool { /// Adds a transaction to the mempool with a timestamp. - fn add_tx(&self, tx: &ledger::Transaction, timestamp: u64) -> Result<()>; + fn add_tx(&self, tx: &ledger::Transaction) -> Result<()>; /// Gets a transaction from the mempool. fn get_tx(&self, tx_id: [u8; 32]) -> Result>; diff --git a/node/src/database/rocksdb.rs b/node/src/database/rocksdb.rs index 48c1a17749..029e96224e 100644 --- a/node/src/database/rocksdb.rs +++ b/node/src/database/rocksdb.rs @@ -12,6 +12,7 @@ use std::cell::RefCell; use node_data::ledger::{self, Fault, Header, Label, SpentTransaction}; use node_data::Serializable; +use std::time::{self, UNIX_EPOCH}; use crate::database::Mempool; @@ -679,7 +680,7 @@ impl<'db, DB: DBAccess> Persist for DBTransaction<'db, DB> { } impl<'db, DB: DBAccess> Mempool for DBTransaction<'db, DB> { - fn add_tx(&self, tx: &ledger::Transaction, timestamp: u64) -> Result<()> { + fn add_tx(&self, tx: &ledger::Transaction) -> Result<()> { // Map Hash to serialized transaction let mut tx_data = vec![]; tx.write(&mut tx_data)?; @@ -694,6 +695,11 @@ impl<'db, DB: DBAccess> Mempool for DBTransaction<'db, DB> { self.put_cf(self.nullifiers_cf, key, hash)?; } + let timestamp = time::SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|n| n.as_secs()) + .expect("valid timestamp"); + let timestamp = timestamp.to_be_bytes(); // Map Fee_Hash to Timestamp @@ -1207,7 +1213,7 @@ mod tests { Backend::create_or_open(path, DatabaseOptions::default()); let t: ledger::Transaction = Faker.fake(); - assert!(db.update(|txn| { txn.add_tx(&t, 0) }).is_ok()); + assert!(db.update(|txn| { txn.add_tx(&t) }).is_ok()); db.view(|vq| { assert!(Mempool::get_tx_exists(&vq, t.id()).unwrap()); @@ -1241,7 +1247,7 @@ mod tests { db.update(|txn| { for _i in 0..10u32 { let t: ledger::Transaction = Faker.fake(); - txn.add_tx(&t, 0)?; + txn.add_tx(&t)?; } Ok(()) }) @@ -1280,9 +1286,8 @@ mod tests { db.update(|db| { assert_eq!(db.txs_count(), 0); - txs.iter().for_each(|t| { - db.add_tx(&t, 0).expect("tx should be added") - }); + txs.iter() + .for_each(|t| db.add_tx(&t).expect("tx should be added")); Ok(()) }) .unwrap(); @@ -1319,7 +1324,7 @@ mod tests { db.update(|txn| { for i in 0..10u32 { let t = ledger::faker::gen_dummy_tx(i as u64); - txn.add_tx(&t, 0)?; + txn.add_tx(&t)?; } Ok(()) }) @@ -1348,13 +1353,13 @@ mod tests { let _ = db.update(|txn| { (1..101).for_each(|i| { let t = ledger::faker::gen_dummy_tx(i as u64); - txn.add_tx(&t, i).expect("tx should be added"); + txn.add_tx(&t).expect("tx should be added"); expiry_list.insert(t.id()); }); (1000..1100).for_each(|i| { let t = ledger::faker::gen_dummy_tx(i as u64); - txn.add_tx(&t, i).expect("tx should be added"); + txn.add_tx(&t).expect("tx should be added"); }); Ok(()) diff --git a/node/src/mempool.rs b/node/src/mempool.rs index 39815f2965..75404691c3 100644 --- a/node/src/mempool.rs +++ b/node/src/mempool.rs @@ -102,7 +102,7 @@ impl _ = on_idle_event.tick() => { info!(event = "mempool_idle", interval = ?idle_interval); - let dur = time::SystemTime::now() + let expiration_time = time::SystemTime::now() .duration_since(UNIX_EPOCH) .expect("valid timestamp") .checked_sub(mempool_expiry) @@ -110,7 +110,7 @@ impl // Remove expired transactions from the mempool db.read().await.update(|db| { - let expired_txs = db.get_expired_txs(dur.as_secs())?; + let expired_txs = db.get_expired_txs(expiration_time.as_secs())?; for tx_id in expired_txs { info!(event = "expired_tx", hash = hex::encode(tx_id)); if db.delete_tx(tx_id)? { @@ -216,11 +216,7 @@ impl MempoolSrv { events.push(TransactionEvent::Included(tx)); // Persist transaction in mempool storage - let now = time::SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("valid timestamp"); - - db.add_tx(tx, now.as_secs()) + db.add_tx(tx) })?; tracing::info!(