Skip to content

Commit 5f9bb0d

Browse files
committed
f - Move recursion outside nested for-loops
1 parent 730c62c commit 5f9bb0d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,32 @@ where C::Target: chain::Filter,
9393
/// [`chain::Watch::release_pending_monitor_events`]: ../trait.Watch.html#tymethod.release_pending_monitor_events
9494
/// [`chain::Filter`]: ../trait.Filter.html
9595
pub fn block_connected(&self, header: &BlockHeader, txdata: &TransactionDataRef, height: u32) {
96+
let mut dependent_txdata = Vec::new();
9697
let monitors = self.monitors.read().unwrap();
9798
for monitor in monitors.values() {
9899
let mut txn_outputs = monitor.block_connected(header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
99100

101+
// Register any new outputs with the chain source for filtering, storing any dependent
102+
// transactions from within the block that previously had not been included in txdata.
100103
if let Some(ref chain_source) = self.chain_source {
101104
for (txid, outputs) in txn_outputs.drain(..) {
102105
for (idx, output) in outputs.iter() {
103-
// Register any new outputs with the chain source for filtering and recurse
104-
// if it indicates that there are dependent transactions within the block
105-
// that had not been previously included in txdata.
106106
let outpoint = OutPoint { txid, index: *idx as u16 };
107-
if let Some(txdata) = chain_source.register_output(&outpoint, &output.script_pubkey) {
108-
let txdata: Vec<_> = txdata.iter().map(|(index, tx)| (*index, tx)).collect();
109-
self.block_connected(header, &txdata, height);
107+
if let Some(mut new_txdata) = chain_source.register_output(&outpoint, &output.script_pubkey) {
108+
dependent_txdata.append(&mut new_txdata);
110109
}
111110
}
112111
}
113112
}
114113
}
114+
115+
// Recursively call for any dependent transactions that were identified by the chain source.
116+
if !dependent_txdata.is_empty() {
117+
dependent_txdata.sort_unstable_by_key(|(index, _tx)| *index);
118+
dependent_txdata.dedup_by_key(|(index, _tx)| *index);
119+
let txdata: Vec<_> = dependent_txdata.iter().map(|(index, tx)| (*index, tx)).collect();
120+
self.block_connected(header, &txdata, height);
121+
}
115122
}
116123

117124
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view

0 commit comments

Comments
 (0)