Skip to content

Commit b9158c5

Browse files
Add manual-funding broadcast tracking to ChannelMonitor
Adds `is_manual_broadcast` and `funding_seen_onchain` flags to track whether the channel uses manual funding broadcasts and whether we've seen the funding tx confirm. This enables deferring holder commitment broadcasts until after the funding tx is actually broadcast. For example, in LSPS2 with client_trusts_lsp=true, the LSP may defer broadcasting the funding tx until the client claims an HTLC, so we need to avoid broadcasting commitments that reference outputs that don't exist yet.
1 parent 6287ed3 commit b9158c5

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,19 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
12021202
funding: FundingScope,
12031203
pending_funding: Vec<FundingScope>,
12041204

1205+
/// True if this channel was configured for manual funding broadcasts. Monitors written by
1206+
/// versions prior to LDK 0.2 load with `false` until a new update persists it.
1207+
is_manual_broadcast: bool,
1208+
/// True once we've observed either funding transaction on-chain. Older monitors prior to LDK 0.2
1209+
/// assume this is `true` when absent during upgrade so holder broadcasts aren't gated unexpectedly.
1210+
/// In manual-broadcast channels we also use this to trigger deferred holder
1211+
/// broadcasts once the funding transaction finally appears on-chain.
1212+
///
1213+
/// Note: This tracks whether the funding transaction was ever broadcast, not whether it is
1214+
/// currently confirmed. It is never reset, even if the funding transaction is unconfirmed due
1215+
/// to a reorg.
1216+
funding_seen_onchain: bool,
1217+
12051218
latest_update_id: u64,
12061219
commitment_transaction_number_obscure_factor: u64,
12071220

@@ -1740,6 +1753,8 @@ pub(crate) fn write_chanmon_internal<Signer: EcdsaChannelSigner, W: Writer>(
17401753
(32, channel_monitor.pending_funding, optional_vec),
17411754
(33, channel_monitor.htlcs_resolved_to_user, required),
17421755
(34, channel_monitor.alternative_funding_confirmed, option),
1756+
(35, channel_monitor.is_manual_broadcast, required),
1757+
(37, channel_monitor.funding_seen_onchain, required),
17431758
});
17441759

17451760
Ok(())
@@ -1868,6 +1883,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
18681883
commitment_transaction_number_obscure_factor: u64,
18691884
initial_holder_commitment_tx: HolderCommitmentTransaction, best_block: BestBlock,
18701885
counterparty_node_id: PublicKey, channel_id: ChannelId,
1886+
is_manual_broadcast: bool,
18711887
) -> ChannelMonitor<Signer> {
18721888

18731889
assert!(commitment_transaction_number_obscure_factor <= (1 << 48));
@@ -1914,6 +1930,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19141930
},
19151931
pending_funding: vec![],
19161932

1933+
is_manual_broadcast,
1934+
funding_seen_onchain: false,
1935+
19171936
latest_update_id: 0,
19181937
commitment_transaction_number_obscure_factor,
19191938

@@ -6562,6 +6581,8 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
65626581
let mut channel_parameters = None;
65636582
let mut pending_funding = None;
65646583
let mut alternative_funding_confirmed = None;
6584+
let mut is_manual_broadcast = RequiredWrapper(None);
6585+
let mut funding_seen_onchain = RequiredWrapper(None);
65656586
read_tlv_fields!(reader, {
65666587
(1, funding_spend_confirmed, option),
65676588
(3, htlcs_resolved_on_chain, optional_vec),
@@ -6582,6 +6603,8 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
65826603
(32, pending_funding, optional_vec),
65836604
(33, htlcs_resolved_to_user, option),
65846605
(34, alternative_funding_confirmed, option),
6606+
(35, is_manual_broadcast, (default_value, false)),
6607+
(37, funding_seen_onchain, (default_value, true)),
65856608
});
65866609
// Note that `payment_preimages_with_info` was added (and is always written) in LDK 0.1, so
65876610
// we can use it to determine if this monitor was last written by LDK 0.1 or later.
@@ -6695,6 +6718,10 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
66956718
prev_holder_commitment_tx,
66966719
},
66976720
pending_funding: pending_funding.unwrap_or(vec![]),
6721+
is_manual_broadcast: is_manual_broadcast.0.unwrap(),
6722+
// Older monitors prior to LDK 0.2 assume this is `true` when absent
6723+
// during upgrade so holder broadcasts aren't gated unexpectedly.
6724+
funding_seen_onchain: funding_seen_onchain.0.unwrap(),
66986725

66996726
latest_update_id,
67006727
commitment_transaction_number_obscure_factor,

lightning/src/ln/channel.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,6 +3196,7 @@ where
31963196
funding.get_holder_selected_contest_delay(), &context.destination_script,
31973197
&funding.channel_transaction_parameters, funding.is_outbound(), obscure_factor,
31983198
holder_commitment_tx, best_block, context.counterparty_node_id, context.channel_id(),
3199+
context.is_manual_broadcast,
31993200
);
32003201
channel_monitor.provide_initial_counterparty_commitment_tx(
32013202
counterparty_initial_commitment_tx.clone(),

0 commit comments

Comments
 (0)