Skip to content

Commit b7c308b

Browse files
committed
f - Filter txdata in ChannelMonitor
1 parent c8bbd68 commit b7c308b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,17 +1835,42 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18351835
Vec::new()
18361836
}
18371837

1838+
/// Checks if a given transaction matches the funding transaction or spends any watched outputs.
1839+
fn does_tx_match(&self, tx: &Transaction) -> bool {
1840+
let funding_txo = self.get_funding_txo();
1841+
if tx.txid() == funding_txo.0.txid {
1842+
for output in tx.output.iter() {
1843+
if output.script_pubkey == funding_txo.1 {
1844+
return true;
1845+
}
1846+
}
1847+
}
1848+
1849+
for input in tx.input.iter() {
1850+
for (txid, outputs) in self.get_outputs_to_watch().iter() {
1851+
for (idx, _script_pubkey) in outputs.iter().enumerate() {
1852+
if txid == &input.previous_output.txid && idx == input.previous_output.vout as usize {
1853+
return true;
1854+
}
1855+
}
1856+
}
1857+
}
1858+
1859+
false
1860+
}
1861+
18381862
/// Determines if any HTLCs have been resolved on chain in the connected block.
18391863
///
1840-
/// Returns any transaction outputs from `txn_matched` that spends of should be watched for.
1841-
/// After called these are also available via [`get_outputs_to_watch`].
1864+
/// Returns any new outputs to watch from `txdata`. After called, these are also included in
1865+
/// [`get_outputs_to_watch`].
18421866
///
18431867
/// [`get_outputs_to_watch`]: #method.get_outputs_to_watch
1844-
pub fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txn_matched: &[(usize, &Transaction)], height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec<TxOut>)>
1868+
pub fn block_connected<B: Deref, F: Deref, L: Deref>(&mut self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32, broadcaster: B, fee_estimator: F, logger: L)-> Vec<(Txid, Vec<TxOut>)>
18451869
where B::Target: BroadcasterInterface,
18461870
F::Target: FeeEstimator,
18471871
L::Target: Logger,
18481872
{
1873+
let txn_matched = &txdata.iter().filter(|&&(_, tx)| self.does_tx_match(tx)).map(|e| *e).collect::<Vec<_>>();
18491874
for &(_, tx) in txn_matched {
18501875
let mut output_val = 0;
18511876
for out in tx.output.iter() {

lightning/src/chain/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ pub trait Watch: Send + Sync {
6767
///
6868
/// Implementations are responsible for watching the chain for the funding transaction along
6969
/// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
70-
/// calling [`block_connected`] and [`block_disconnected`] on the monitor and including all such
71-
/// transactions that meet this criteria.
70+
/// calling [`block_connected`] and [`block_disconnected`] on the monitor.
7271
///
7372
/// [`get_outputs_to_watch`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
7473
/// [`block_connected`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.block_connected

0 commit comments

Comments
 (0)