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 27, 2024
1 parent b20a839 commit 3282321
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,24 @@ where C::Target: chain::Filter,
}
}
}

pub fn prune_stale_channel_monitors(&self, to_prune: Vec<OutPoint>) {
let mut monitors = self.monitors.write().unwrap();
for funding_txo in to_prune {
let channel_monitor = monitors.get(&funding_txo);
if let Some(channel_monitor) = channel_monitor {
if channel_monitor.monitor.is_stale() {
log_info!(self.logger, "Pruning stale ChannelMonitor for
channel {}", log_funding_info!(channel_monitor.monitor));
//TODO: save the channel monitor to disk in an archived namespace before removing it

self.persister.prune_persisted_channel(funding_txo);
monitors.remove(&funding_txo);
}
}

}
}
}

impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
Expand Down Expand Up @@ -1114,4 +1132,27 @@ 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, None]);
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
create_announced_chan_between_nodes(&nodes, 0, 1);
// Get a route for later and rebalance the channel somewhat
send_payment(&nodes[0], &[&nodes[1]], 10_000_000);
// First route a payment that we will claim on chain and give the recipient the preimage.
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
nodes[1].node.claim_funds(payment_preimage);
expect_payment_claimed!(nodes[1], payment_hash, 1_000_000);
nodes[1].node.get_and_clear_pending_msg_events();
let binding = node_cfgs[1].chain_monitor.added_monitors.lock().unwrap();
let monitors_b = binding.first().unwrap();
let outpoint = monitors_b.0.clone();
dbg!(&outpoint);
// nodes[1].chain_monitor().unwrap().chain_monitor.prune_stale_channel_monitors(vec![outpoint]); // lock order problem
assert!(false);
}
}
4 changes: 4 additions & 0 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
spendable_outputs
}

pub(crate) fn is_stale(&self) -> bool {
self.get_claimable_balances().is_empty()
}

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

0 comments on commit 3282321

Please sign in to comment.