Skip to content

Commit b20a839

Browse files
committed
Add prune_persisted_channel to Persist trait
1 parent 68d5e88 commit b20a839

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
194194
///
195195
/// [`Writeable::write`]: crate::util::ser::Writeable::write
196196
fn update_persisted_channel(&self, channel_funding_outpoint: OutPoint, update: Option<&ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>, update_id: MonitorUpdateId) -> ChannelMonitorUpdateStatus;
197+
198+
/// Prune a channel's data.
199+
///
200+
/// This is called when a channel is stale, ie we dont have balance to claim and its
201+
/// closed.
202+
fn prune_persisted_channel(&self, channel_funding_outpoint: OutPoint) -> bool;
197203
}
198204

199205
struct MonitorHolder<ChannelSigner: WriteableEcdsaChannelSigner> {

lightning/src/util/persist.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ pub const CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
5858
/// The primary namespace under which [`ChannelMonitorUpdate`]s will be persisted.
5959
pub const CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE: &str = "monitor_updates";
6060

61+
/// The primary namespace under which [`ChannelMonitor`]s will be persisted.
62+
pub const PRUNED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE: &str = "pruned_monitors";
63+
/// The secondary namespace under which [`ChannelMonitor`]s will be persisted.
64+
pub const PRUNED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
65+
6166
/// The primary namespace under which the [`NetworkGraph`] will be persisted.
6267
pub const NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE: &str = "";
6368
/// The secondary namespace under which the [`NetworkGraph`] will be persisted.
@@ -241,6 +246,18 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
241246
Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError
242247
}
243248
}
249+
250+
fn prune_persisted_channel(&self, funding_txo: OutPoint) -> bool {
251+
let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index);
252+
match self.remove(
253+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
254+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
255+
&key, false)
256+
{
257+
Ok(()) => true,
258+
Err(_) => false
259+
}
260+
}
244261
}
245262

246263
impl<ChannelSigner: WriteableEcdsaChannelSigner> Persist<ChannelSigner> for dyn KVStore + Send + Sync {
@@ -272,6 +289,18 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> Persist<ChannelSigner> for dyn
272289
Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError
273290
}
274291
}
292+
293+
fn prune_persisted_channel(&self, funding_txo: OutPoint) -> bool {
294+
let key = format!("{}_{}", funding_txo.txid.to_string(), funding_txo.index);
295+
match self.remove(
296+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
297+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
298+
&key, false)
299+
{
300+
Ok(()) => true,
301+
Err(_) => false
302+
}
303+
}
275304
}
276305

277306
/// Read previously persisted [`ChannelMonitor`]s from the store.
@@ -778,6 +807,29 @@ where
778807
self.persist_new_channel(funding_txo, monitor, monitor_update_call_id)
779808
}
780809
}
810+
811+
fn prune_persisted_channel(&self, funding_txo: OutPoint) -> bool {
812+
let monitor_name = MonitorName::from(funding_txo);
813+
match self.kv_store.remove(
814+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
815+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
816+
monitor_name.as_str(),
817+
false,
818+
) {
819+
Ok(()) => true,
820+
Err(e) => {
821+
log_error!(
822+
self.logger,
823+
"Failed to remove ChannelMonitor {}/{}/{} reason: {}",
824+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
825+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
826+
monitor_name.as_str(),
827+
e
828+
);
829+
false
830+
}
831+
}
832+
}
781833
}
782834

783835
impl<K: Deref, L: Deref, ES: Deref, SP: Deref> MonitorUpdatingPersister<K, L, ES, SP>

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ impl<Signer: sign::ecdsa::WriteableEcdsaChannelSigner> chainmonitor::Persist<Sig
501501
}
502502
res
503503
}
504+
505+
fn prune_persisted_channel(&self, _funding_txo: OutPoint) -> bool {
506+
false
507+
}
504508
}
505509

506510
pub struct TestPersister {
@@ -549,6 +553,10 @@ impl<Signer: sign::ecdsa::WriteableEcdsaChannelSigner> chainmonitor::Persist<Sig
549553
}
550554
ret
551555
}
556+
557+
fn prune_persisted_channel(&self, _funding_txo: OutPoint) -> bool {
558+
false
559+
}
552560
}
553561

554562
pub struct TestStore {

0 commit comments

Comments
 (0)