Skip to content

Commit 071337a

Browse files
committed
Add OutputSweeper utility persisting and sweeping spendable outputs
We add a `OutputSweeper` utility that allows to track the state of spendable output descriptors as emitted by `Event::SpendableOutputs`. To this end, the `OutputSweeper` persists the necessary information in our `KVStore` and regularly tries to sweep the spendable outputs, removing them after reaching threshold confirmations, i.e., `ANTI_REORG_DELAY`.
1 parent ec95651 commit 071337a

File tree

5 files changed

+857
-1
lines changed

5 files changed

+857
-1
lines changed

lightning/src/chain/chaininterface.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,16 @@ pub enum ConfirmationTarget {
124124
///
125125
/// [`ChannelManager::close_channel_with_feerate_and_script`]: crate::ln::channelmanager::ChannelManager::close_channel_with_feerate_and_script
126126
ChannelCloseMinimum,
127-
/// XXX
127+
/// The feerate [`OutputSweeper`] will use on transactions spending
128+
/// [`SpendableOutputDescriptor`]s after a channel closure.
129+
///
130+
/// Generally spending these outputs is safe as long as they eventually confirm, so a value
131+
/// (slightly above) the mempool minimum should suffice. However, as this value will influence
132+
/// how long funds will be unavailable after channel closure, [`FeeEstimator`] implementors
133+
/// might want to choose a higher feerate to regain control over funds faster.
134+
///
135+
/// [`OutputSweeper`]: crate::util::sweep::OutputSweeper
136+
/// [`SpendableOutputDescriptor`]: crate::sign::SpendableOutputDescriptor
128137
OutputSpendingFee,
129138
}
130139

lightning/src/chain/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Monitor
2020
use crate::ln::ChannelId;
2121
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
2222
use crate::chain::transaction::{OutPoint, TransactionData};
23+
use crate::impl_writeable_tlv_based;
2324

2425
#[allow(unused_imports)]
2526
use crate::prelude::*;
@@ -56,6 +57,11 @@ impl BestBlock {
5657
}
5758
}
5859

60+
impl_writeable_tlv_based!(BestBlock, {
61+
(0, block_hash, required),
62+
(2, height, required),
63+
});
64+
5965

6066
/// The `Listen` trait is used to notify when blocks have been connected or disconnected from the
6167
/// chain.

lightning/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod invoice;
2222
pub mod persist;
2323
pub mod scid_utils;
2424
pub mod string;
25+
pub mod sweep;
2526
pub mod wakers;
2627
#[cfg(fuzzing)]
2728
pub mod base32;

lightning/src/util/persist.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ pub const SCORER_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
7070
/// The key under which the [`WriteableScore`] will be persisted.
7171
pub const SCORER_PERSISTENCE_KEY: &str = "scorer";
7272

73+
/// The primary namespace under which [`OutputSweeper`] state will be persisted.
74+
///
75+
/// [`OutputSweeper`]: crate::util::sweep::OutputSweeper
76+
pub const OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE: &str = "";
77+
/// The secondary namespace under which [`OutputSweeper`] state will be persisted.
78+
///
79+
/// [`OutputSweeper`]: crate::util::sweep::OutputSweeper
80+
pub const OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";
81+
/// The secondary namespace under which [`OutputSweeper`] state will be persisted.
82+
/// The key under which [`OutputSweeper`] state will be persisted.
83+
///
84+
/// [`OutputSweeper`]: crate::util::sweep::OutputSweeper
85+
pub const OUTPUT_SWEEPER_PERSISTENCE_KEY: &str = "output_sweeper";
86+
7387
/// A sentinel value to be prepended to monitors persisted by the [`MonitorUpdatingPersister`].
7488
///
7589
/// This serves to prevent someone from accidentally loading such monitors (which may need

0 commit comments

Comments
 (0)