diff --git a/crates/subcoin-network/src/transaction_manager.rs b/crates/subcoin-network/src/transaction_manager.rs index 30814b1f..2be59b50 100644 --- a/crates/subcoin-network/src/transaction_manager.rs +++ b/crates/subcoin-network/src/transaction_manager.rs @@ -1,7 +1,5 @@ use crate::PeerId; use bitcoin::consensus::deserialize; -use bitcoin::p2p::message::NetworkMessage; -use bitcoin::p2p::message_blockdata::Inventory; use bitcoin::{Transaction, Txid}; use indexmap::map::Entry; use indexmap::IndexMap; @@ -40,14 +38,17 @@ impl TransactionManager { } } + /// 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, - peers: impl Iterator, + connected_peers: impl Iterator, ) -> Vec<(PeerId, Vec)> { // Remove timeout transactions. - // Broadcast transactions to peers. - peers + connected_peers .filter_map(|address| { let mut to_advertise = vec![]; @@ -67,6 +68,12 @@ impl TransactionManager { .collect() } + pub fn get_transaction(&self, txid: &Txid) -> Option { + self.transactions + .get(txid) + .map(|tx_info| tx_info.transaction.clone()) + } + pub fn add_transaction(&mut self, raw_tx: &[u8]) { if let Ok(transaction) = deserialize::(raw_tx) { let txid = transaction.compute_txid(); diff --git a/crates/subcoin-network/src/worker.rs b/crates/subcoin-network/src/worker.rs index 7177173d..74535bc6 100644 --- a/crates/subcoin-network/src/worker.rs +++ b/crates/subcoin-network/src/worker.rs @@ -391,7 +391,16 @@ where return Ok(SyncAction::Disconnect(from, Error::TooManyInventoryItems)); } - // TODO: handle getdata tx + // 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 Err(err) = self.send(from, NetworkMessage::Tx(transaction)) { + tracing::error!(?err, "Failed to send transaction {txid}"); + } + } + } + }); Ok(self.chain_sync.on_inv(inv, from)) }