Skip to content

Commit 30b9807

Browse files
Merge pull request #1834 from dunxen/2022-11-expose-pending-monitor-updates
Add public method to list pending monitor updates from `ChainMonitor`
2 parents f1428fd + 7f4ac0e commit 30b9807

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lightning/src/chain/chainmonitor.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,23 @@ where C::Target: chain::Filter,
395395
self.monitors.read().unwrap().keys().map(|outpoint| *outpoint).collect()
396396
}
397397

398+
#[cfg(not(c_bindings))]
399+
/// Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
400+
pub fn list_pending_monitor_updates(&self) -> HashMap<OutPoint, Vec<MonitorUpdateId>> {
401+
self.monitors.read().unwrap().iter().map(|(outpoint, holder)| {
402+
(*outpoint, holder.pending_monitor_updates.lock().unwrap().clone())
403+
}).collect()
404+
}
405+
406+
#[cfg(c_bindings)]
407+
/// Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
408+
pub fn list_pending_monitor_updates(&self) -> Vec<(OutPoint, Vec<MonitorUpdateId>)> {
409+
self.monitors.read().unwrap().iter().map(|(outpoint, holder)| {
410+
(*outpoint, holder.pending_monitor_updates.lock().unwrap().clone())
411+
}).collect()
412+
}
413+
414+
398415
#[cfg(test)]
399416
pub fn remove_monitor(&self, funding_txo: &OutPoint) -> ChannelMonitor<ChannelSigner> {
400417
self.monitors.write().unwrap().remove(funding_txo).unwrap().monitor
@@ -798,7 +815,22 @@ mod tests {
798815
// Note that updates is a HashMap so the ordering here is actually random. This shouldn't
799816
// fail either way but if it fails intermittently it's depending on the ordering of updates.
800817
let mut update_iter = updates.iter();
801-
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(*funding_txo, update_iter.next().unwrap().clone()).unwrap();
818+
let next_update = update_iter.next().unwrap().clone();
819+
// Should contain next_update when pending updates listed.
820+
#[cfg(not(c_bindings))]
821+
assert!(nodes[1].chain_monitor.chain_monitor.list_pending_monitor_updates().get(funding_txo)
822+
.unwrap().contains(&next_update));
823+
#[cfg(c_bindings)]
824+
assert!(nodes[1].chain_monitor.chain_monitor.list_pending_monitor_updates().iter()
825+
.find(|(txo, _)| txo == funding_txo).unwrap().1.contains(&next_update));
826+
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(*funding_txo, next_update.clone()).unwrap();
827+
// Should not contain the previously pending next_update when pending updates listed.
828+
#[cfg(not(c_bindings))]
829+
assert!(!nodes[1].chain_monitor.chain_monitor.list_pending_monitor_updates().get(funding_txo)
830+
.unwrap().contains(&next_update));
831+
#[cfg(c_bindings)]
832+
assert!(!nodes[1].chain_monitor.chain_monitor.list_pending_monitor_updates().iter()
833+
.find(|(txo, _)| txo == funding_txo).unwrap().1.contains(&next_update));
802834
assert!(nodes[1].chain_monitor.release_pending_monitor_events().is_empty());
803835
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
804836
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(*funding_txo, update_iter.next().unwrap().clone()).unwrap();

0 commit comments

Comments
 (0)