Skip to content

Commit

Permalink
Remove timeout transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Aug 4, 2024
1 parent fe623cf commit 514235a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions crates/subcoin-network/src/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use bitcoin::{Transaction, Txid};
use indexmap::map::Entry;
use indexmap::IndexMap;
use std::collections::HashSet;
use std::time::Instant;
use std::time::{Duration, SystemTime};

const TRANSACTION_TIMEOUT_DURATION_SECS: u64 = 10 * 60;

#[derive(Debug)]
struct TransactionInfo {
Expand All @@ -15,8 +17,18 @@ struct TransactionInfo {
/// Note that having a peer in this set doesn't guarantee the the peer actually
/// received the transaction.
advertised: HashSet<PeerId>,
/// Time at which the transaction was added.
at: Instant,
/// How long the transaction should be stored.
ttl: SystemTime,
}

impl TransactionInfo {
fn new(transaction: Transaction) -> Self {
Self {
transaction,
advertised: HashSet::new(),
ttl: SystemTime::now() + Duration::from_secs(TRANSACTION_TIMEOUT_DURATION_SECS),
}
}
}

/// This struct manages the transactions received from the network.
Expand All @@ -30,23 +42,27 @@ impl TransactionManager {
/// Maximum number of transactions the manager holds.
const MAX_TRANSACTIONS: usize = 256;

const TRANSACTION_TIMEOUT_DURATION_SECS: u64 = 10 * 60;

pub fn new() -> Self {
Self {
transactions: IndexMap::new(),
}
}

/// Broadcast known transaction IDs to the connected peers.
///
/// If the timeout period has passed for a transaction ID, it is broadcasted again.
/// If the transaction has not been broadcasted, the transaction ID is broadcasted.
pub fn on_tick<'a>(
&mut self,
connected_peers: impl Iterator<Item = &'a PeerId>,
) -> Vec<(PeerId, Vec<Txid>)> {
// Remove timeout transactions.
let now = SystemTime::now();
self.transactions.retain(|txid, info| {
if info.ttl < now {
tracing::debug!("Removing timeout transaction {txid}");
false
} else {
true
}
});

connected_peers
.filter_map(|address| {
Expand Down Expand Up @@ -87,11 +103,7 @@ impl TransactionManager {
tracing::debug!("Tx {txid} already exists");
}
Entry::Vacant(entry) => {
entry.insert(TransactionInfo {
transaction,
advertised: HashSet::new(),
at: Instant::now(),
});
entry.insert(TransactionInfo::new(transaction));
tracing::debug!("Added new tx {txid}");
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/subcoin-network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ where
// Send transactions to the requesting node.
inv.iter().for_each(|inv| {
if let Inventory::Transaction(txid) = inv {
if let Some(transaction) = self.transaction_manager.get_transaction(&txid) {
if let Some(transaction) = self.transaction_manager.get_transaction(txid) {
if let Err(err) = self.send(from, NetworkMessage::Tx(transaction)) {
tracing::error!(?err, "Failed to send transaction {txid}");
}
Expand Down

0 comments on commit 514235a

Please sign in to comment.