Skip to content

Commit

Permalink
Add prune_stale_channel_monitors to ChainMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Mar 24, 2024
1 parent 6d11111 commit b8b4199
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,19 @@ where C::Target: chain::Filter,
}
}
}

pub fn prune_stale_channel_monitors(&self) {
let mut monitors = self.monitors.write().unwrap();
let mut to_remove = Vec::new();
for (funding_outpoint, monitor_holder) in monitors.iter() {
if monitor_holder.monitor.is_stale() {
to_remove.push(*funding_outpoint);
}
}
for funding_outpoint in to_remove {
monitors.remove(&funding_outpoint);
}
}
}

impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
Expand Down Expand Up @@ -1108,4 +1121,17 @@ mod tests {
core::mem::drop(nodes);
}).is_err());
}

#[test]
fn prune_stale_channel_monitor() {
// Test that we can prune a ChannelMonitor that has no active channel.
let chanmon_cfgs = create_chanmon_cfgs(2);
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None]);
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
let push_msat = 0;
let channel_id = exchange_open_accept_chan(&nodes[0], &nodes[1], 10000, push_msat);
nodes[0].node.close_channel(&channel_id, &nodes[1].node.get_our_node_id()).unwrap();
// try to prune stale channel monitor
}
}
10 changes: 10 additions & 0 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
spendable_outputs
}

pub(crate) fn is_stale(&self) -> bool {
let _inner = self.inner.lock().unwrap();
// Access channel data to check if:
// 1) No push_msat is set
// 2) Inbound channel
// 3) No commitment tx updates occured
// 4) Channel is closed
false
}

#[cfg(test)]
pub fn get_counterparty_payment_script(&self) -> ScriptBuf {
self.inner.lock().unwrap().counterparty_payment_script.clone()
Expand Down

0 comments on commit b8b4199

Please sign in to comment.