Skip to content

Commit b8b4199

Browse files
committed
Add prune_stale_channel_monitors to ChainMonitor
1 parent 6d11111 commit b8b4199

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lightning/src/chain/chainmonitor.rs

+26
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,19 @@ where C::Target: chain::Filter,
656656
}
657657
}
658658
}
659+
660+
pub fn prune_stale_channel_monitors(&self) {
661+
let mut monitors = self.monitors.write().unwrap();
662+
let mut to_remove = Vec::new();
663+
for (funding_outpoint, monitor_holder) in monitors.iter() {
664+
if monitor_holder.monitor.is_stale() {
665+
to_remove.push(*funding_outpoint);
666+
}
667+
}
668+
for funding_outpoint in to_remove {
669+
monitors.remove(&funding_outpoint);
670+
}
671+
}
659672
}
660673

661674
impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
@@ -1108,4 +1121,17 @@ mod tests {
11081121
core::mem::drop(nodes);
11091122
}).is_err());
11101123
}
1124+
1125+
#[test]
1126+
fn prune_stale_channel_monitor() {
1127+
// Test that we can prune a ChannelMonitor that has no active channel.
1128+
let chanmon_cfgs = create_chanmon_cfgs(2);
1129+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1130+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None]);
1131+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1132+
let push_msat = 0;
1133+
let channel_id = exchange_open_accept_chan(&nodes[0], &nodes[1], 10000, push_msat);
1134+
nodes[0].node.close_channel(&channel_id, &nodes[1].node.get_our_node_id()).unwrap();
1135+
// try to prune stale channel monitor
1136+
}
11111137
}

lightning/src/chain/channelmonitor.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
18551855
spendable_outputs
18561856
}
18571857

1858+
pub(crate) fn is_stale(&self) -> bool {
1859+
let _inner = self.inner.lock().unwrap();
1860+
// Access channel data to check if:
1861+
// 1) No push_msat is set
1862+
// 2) Inbound channel
1863+
// 3) No commitment tx updates occured
1864+
// 4) Channel is closed
1865+
false
1866+
}
1867+
18581868
#[cfg(test)]
18591869
pub fn get_counterparty_payment_script(&self) -> ScriptBuf {
18601870
self.inner.lock().unwrap().counterparty_payment_script.clone()

0 commit comments

Comments
 (0)