Skip to content

Commit f267a30

Browse files
committed
Only yield DelayedPaymentOutput descriptors once their delay expires
Otherwise, we could give users a descriptor ahead of time that will result in an invalid transaction spend/broadcast.
1 parent 6cf0351 commit f267a30

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
16721672

16731673
/// Returns the descriptors for relevant outputs (i.e., those that we can spend) within the
16741674
/// transaction if they exist and the transaction has at least [`ANTI_REORG_DELAY`]
1675+
/// confirmations. For [`SpendableOutputDescriptor::DelayedPaymentOutput`] descriptors to be
1676+
/// returned, the transaction must have at least `max(ANTI_REORG_DELAY, to_self_delay)`
16751677
/// confirmations.
16761678
///
16771679
/// Descriptors returned by this method are primarily exposed via [`Event::SpendableOutputs`]
@@ -1689,11 +1691,16 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
16891691
pub fn get_spendable_outputs(&self, tx: &Transaction, confirmation_height: u32) -> Vec<SpendableOutputDescriptor> {
16901692
let inner = self.inner.lock().unwrap();
16911693
let current_height = inner.best_block.height;
1692-
if current_height.saturating_sub(ANTI_REORG_DELAY) + 1 >= confirmation_height {
1693-
inner.get_spendable_outputs(tx)
1694-
} else {
1695-
Vec::new()
1696-
}
1694+
let mut spendable_outputs = inner.get_spendable_outputs(tx);
1695+
spendable_outputs.retain(|descriptor| {
1696+
let mut conf_threshold = current_height.saturating_sub(ANTI_REORG_DELAY) + 1;
1697+
if let SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) = descriptor {
1698+
conf_threshold = cmp::min(conf_threshold,
1699+
current_height.saturating_sub(descriptor.to_self_delay as u32) + 1);
1700+
}
1701+
conf_threshold >= confirmation_height
1702+
});
1703+
spendable_outputs
16971704
}
16981705
}
16991706

lightning/src/ln/monitor_tests.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ fn test_balances_on_local_commitment_htlcs() {
637637
// First confirm the commitment transaction on nodes[0], which should leave us with three
638638
// claimable balances.
639639
let node_a_commitment_claimable = nodes[0].best_block_info().1 + BREAKDOWN_TIMEOUT as u32;
640-
mine_transaction(&nodes[0], &as_txn[0]);
640+
let commitment_tx_conf_height_a = block_from_scid(&mine_transaction(&nodes[0], &as_txn[0]));
641641
check_added_monitors!(nodes[0], 1);
642642
check_closed_broadcast!(nodes[0], true);
643643
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 1000000);
@@ -729,13 +729,20 @@ fn test_balances_on_local_commitment_htlcs() {
729729

730730
// Connect blocks until the commitment transaction's CSV expires, providing us the relevant
731731
// `SpendableOutputs` event and removing the claimable balance entry.
732-
connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1);
732+
connect_blocks(&nodes[0], node_a_commitment_claimable - nodes[0].best_block_info().1 - 1);
733+
assert!(get_monitor!(nodes[0], chan_id)
734+
.get_spendable_outputs(&as_txn[0], commitment_tx_conf_height_a).is_empty());
735+
connect_blocks(&nodes[0], 1);
733736
assert_eq!(vec![Balance::ClaimableAwaitingConfirmations {
734737
amount_satoshis: 10_000,
735738
confirmation_height: node_a_htlc_claimable,
736739
}],
737740
nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
738-
test_spendable_output(&nodes[0], &as_txn[0]);
741+
let to_self_spendable_output = test_spendable_output(&nodes[0], &as_txn[0]);
742+
assert_eq!(
743+
get_monitor!(nodes[0], chan_id).get_spendable_outputs(&as_txn[0], commitment_tx_conf_height_a),
744+
to_self_spendable_output
745+
);
739746

740747
// Connect blocks until the HTLC-Timeout's CSV expires, providing us the relevant
741748
// `SpendableOutputs` event and removing the claimable balance entry.

0 commit comments

Comments
 (0)