From fdfe7c6fcb60be6a0620df39b23bc0d5bf985cd4 Mon Sep 17 00:00:00 2001 From: Duncan Dean Date: Thu, 2 May 2024 10:05:43 +0200 Subject: [PATCH] Handle re-establishment next_funding_txid --- lightning/src/ln/channel.rs | 27 ++++++++++++++++++++++----- lightning/src/ln/channelmanager.rs | 5 ++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 287b79fc63..3d1124ac4f 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -1495,6 +1495,10 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { /// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we /// store it here and only release it to the `ChannelManager` once it asks for it. blocked_monitor_updates: Vec, + // If we've sent `commtiment_signed` for an interactive transaction construction, + // but have not received `tx_signatures` we MUST set `next_funding_txid` to the + // txid of that interactive transaction, else we MUST NOT set it. + next_funding_txid: Option, } pub(super) trait InteractivelyFunded where SP::Target: SignerProvider { @@ -2040,6 +2044,8 @@ impl ChannelContext where SP::Target: SignerProvider { blocked_monitor_updates: Vec::new(), is_manual_broadcast: false, + + next_funding_txid: None, }; Ok(channel_context) @@ -2271,6 +2277,7 @@ impl ChannelContext where SP::Target: SignerProvider { blocked_monitor_updates: Vec::new(), local_initiated_shutdown: None, is_manual_broadcast: false, + next_funding_txid: None, }) } @@ -4452,6 +4459,14 @@ impl Channel where self.context.channel_state.clear_waiting_for_batch(); } + pub fn set_next_funding_txid(&mut self, txid: &Txid) { + self.context.next_funding_txid = Some(*txid); + } + + pub fn clear_next_funding_txid(&mut self) { + self.context.next_funding_txid = None; + } + /// Unsets the existing funding information. /// /// This must only be used if the channel has not yet completed funding and has not been used. @@ -7605,10 +7620,7 @@ impl Channel where next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1, your_last_per_commitment_secret: remote_last_secret, my_current_per_commitment_point: dummy_pubkey, - // TODO(dual_funding): If we've sent `commtiment_signed` for an interactive transaction - // construction but have not received `tx_signatures` we MUST set `next_funding_txid` to the - // txid of that interactive transaction, else we MUST NOT set it. - next_funding_txid: None, + next_funding_txid: self.context.next_funding_txid, } } @@ -9549,7 +9561,8 @@ impl Writeable for Channel where SP::Target: SignerProvider { (47, next_holder_commitment_point, option), (49, self.context.local_initiated_shutdown, option), // Added in 0.0.122 (51, is_manual_broadcast, option), // Added in 0.0.124 - (53, funding_tx_broadcast_safe_event_emitted, option) // Added in 0.0.124 + (53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124 + (55, self.context.next_funding_txid, option) // Added in 0.0.124 }); Ok(()) @@ -10158,6 +10171,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch blocked_monitor_updates: blocked_monitor_updates.unwrap(), is_manual_broadcast: is_manual_broadcast.unwrap_or(false), + // If we've sent `commtiment_signed` for an interactive transaction construction, + // but have not received `tx_signatures` we MUST set `next_funding_txid` to the + // txid of that interactive transaction, else we MUST NOT set it. + next_funding_txid: None, }, dual_funding_channel_context: None, interactive_tx_constructor: None, diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 3a14036b14..29fcb8f459 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -7950,6 +7950,7 @@ where peer_state.pending_msg_events.push(msg_send_event); } if let Some(signing_session) = signing_session_opt { + let funding_txid = signing_session.unsigned_tx.txid(); let (channel_id, channel_phase) = chan_phase_entry.remove_entry(); let res = match channel_phase { ChannelPhase::UnfundedOutboundV2(chan) => { @@ -7971,7 +7972,7 @@ where .into()))), }; match res { - Ok((channel, commitment_signed, funding_ready_for_sig_event_opt)) => { + Ok((mut channel, commitment_signed, funding_ready_for_sig_event_opt)) => { if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt { let mut pending_events = self.pending_events.lock().unwrap(); pending_events.push_back((funding_ready_for_sig_event, None)); @@ -7987,6 +7988,7 @@ where update_fee: None, }, }); + channel.set_next_funding_txid(&funding_txid); peer_state.channel_by_id.insert(channel_id.clone(), ChannelPhase::Funded(channel)); }, Err((channel_phase, err)) => { @@ -8021,6 +8023,7 @@ where match channel_phase { ChannelPhase::Funded(chan) => { let (tx_signatures_opt, funding_tx_opt) = try_chan_phase_entry!(self, chan.tx_signatures(&msg), chan_phase_entry); + chan.clear_next_funding_txid(); if let Some(tx_signatures) = tx_signatures_opt { peer_state.pending_msg_events.push(events::MessageSendEvent::SendTxSignatures { node_id: *counterparty_node_id,