Skip to content

Commit 3282321

Browse files
committed
Add prune_stale_channel_monitors to ChainMonitor
1 parent b20a839 commit 3282321

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,24 @@ where C::Target: chain::Filter,
662662
}
663663
}
664664
}
665+
666+
pub fn prune_stale_channel_monitors(&self, to_prune: Vec<OutPoint>) {
667+
let mut monitors = self.monitors.write().unwrap();
668+
for funding_txo in to_prune {
669+
let channel_monitor = monitors.get(&funding_txo);
670+
if let Some(channel_monitor) = channel_monitor {
671+
if channel_monitor.monitor.is_stale() {
672+
log_info!(self.logger, "Pruning stale ChannelMonitor for
673+
channel {}", log_funding_info!(channel_monitor.monitor));
674+
//TODO: save the channel monitor to disk in an archived namespace before removing it
675+
676+
self.persister.prune_persisted_channel(funding_txo);
677+
monitors.remove(&funding_txo);
678+
}
679+
}
680+
681+
}
682+
}
665683
}
666684

667685
impl<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
@@ -1114,4 +1132,27 @@ mod tests {
11141132
core::mem::drop(nodes);
11151133
}).is_err());
11161134
}
1135+
1136+
#[test]
1137+
fn prune_stale_channel_monitor() {
1138+
// Test that we can prune a ChannelMonitor that has no active channel.
1139+
let chanmon_cfgs = create_chanmon_cfgs(2);
1140+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1141+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1142+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1143+
create_announced_chan_between_nodes(&nodes, 0, 1);
1144+
// Get a route for later and rebalance the channel somewhat
1145+
send_payment(&nodes[0], &[&nodes[1]], 10_000_000);
1146+
// First route a payment that we will claim on chain and give the recipient the preimage.
1147+
let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1148+
nodes[1].node.claim_funds(payment_preimage);
1149+
expect_payment_claimed!(nodes[1], payment_hash, 1_000_000);
1150+
nodes[1].node.get_and_clear_pending_msg_events();
1151+
let binding = node_cfgs[1].chain_monitor.added_monitors.lock().unwrap();
1152+
let monitors_b = binding.first().unwrap();
1153+
let outpoint = monitors_b.0.clone();
1154+
dbg!(&outpoint);
1155+
// nodes[1].chain_monitor().unwrap().chain_monitor.prune_stale_channel_monitors(vec![outpoint]); // lock order problem
1156+
assert!(false);
1157+
}
11171158
}

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
18551855
spendable_outputs
18561856
}
18571857

1858+
pub(crate) fn is_stale(&self) -> bool {
1859+
self.get_claimable_balances().is_empty()
1860+
}
1861+
18581862
#[cfg(test)]
18591863
pub fn get_counterparty_payment_script(&self) -> ScriptBuf {
18601864
self.inner.lock().unwrap().counterparty_payment_script.clone()

0 commit comments

Comments
 (0)