Skip to content

Commit 61471cf

Browse files
chaininterface: add watched tx retrieval methods
These methods will be used by the new BlockNotifier struct to retrieve information about watched transactions.
1 parent 7cfd8bf commit 61471cf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/chain/chaininterface.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ pub trait ChainWatchInterface: Sync + Send {
5555
/// bytes are the block height, the next 3 the transaction index within the block, and the
5656
/// final two the output within the transaction.
5757
fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
58+
59+
/// Gets the list of transactions and transaction indices that the ChainWatchInterface is
60+
/// watching for.
61+
fn get_watched_txs(&self, block: &Block) -> (Vec<Transaction>, Vec<u32>);
62+
63+
/// Provides a way for users of the ChainWatchInterface to determine whether its watched
64+
/// data has been modified since the last time it was read. This is useful for triggering
65+
/// block rescans.
66+
fn reentered(&self) -> usize;
5867
}
5968

6069
/// An interface to send a transaction to the Bitcoin network.
@@ -312,6 +321,25 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
312321
}
313322
Err(ChainError::NotSupported)
314323
}
324+
325+
fn get_watched_txs(&self, block: &Block) -> (Vec<Transaction>, Vec<u32>) {
326+
let mut matched = Vec::new();
327+
let mut matched_index = Vec::new();
328+
{
329+
let watched = self.watched.lock().unwrap();
330+
for (index, transaction) in block.txdata.iter().enumerate() {
331+
if self.does_match_tx_unguarded(transaction, &watched) {
332+
matched.push(transaction.clone());
333+
matched_index.push(index as u32);
334+
}
335+
}
336+
}
337+
(matched, matched_index)
338+
}
339+
340+
fn reentered(&self) -> usize {
341+
self.reentered.load(Ordering::Relaxed)
342+
}
315343
}
316344

317345
impl ChainWatchInterfaceUtil {

0 commit comments

Comments
 (0)