Skip to content

Commit

Permalink
Handle re-establishment next_funding_txid
Browse files Browse the repository at this point in the history
  • Loading branch information
dunxen committed Sep 30, 2024
1 parent c32f78c commit 32b7b0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
29 changes: 24 additions & 5 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,10 @@ pub(super) struct ChannelContext<SP: Deref> 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<PendingChannelMonitorUpdate>,
// 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<Txid>,
}

pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider {
Expand Down Expand Up @@ -1992,6 +1996,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
blocked_monitor_updates: Vec::new(),

is_manual_broadcast: false,

next_funding_txid: None,
};

Ok(channel_context)
Expand Down Expand Up @@ -2223,6 +2229,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
blocked_monitor_updates: Vec::new(),
local_initiated_shutdown: None,
is_manual_broadcast: false,
next_funding_txid: None,
})
}

Expand Down Expand Up @@ -4426,6 +4433,14 @@ impl<SP: Deref> Channel<SP> 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.
Expand Down Expand Up @@ -7569,10 +7584,7 @@ impl<SP: Deref> Channel<SP> 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,
}
}

Expand Down Expand Up @@ -9475,7 +9487,8 @@ impl<SP: Deref> Writeable for Channel<SP> 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.125
});

Ok(())
Expand Down Expand Up @@ -9765,6 +9778,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
let mut channel_pending_event_emitted = None;
let mut channel_ready_event_emitted = None;
let mut funding_tx_broadcast_safe_event_emitted = None;
let mut next_funding_txid = None;

let mut user_id_high_opt: Option<u64> = None;
let mut channel_keys_id: Option<[u8; 32]> = None;
Expand Down Expand Up @@ -9825,6 +9839,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
(49, local_initiated_shutdown, option),
(51, is_manual_broadcast, option),
(53, funding_tx_broadcast_safe_event_emitted, option),
(55, next_funding_txid, option) // Added in 0.0.125
});

let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
Expand Down Expand Up @@ -10084,6 +10099,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,
},
dual_funding_channel_context: None,
interactive_tx_constructor: None,
Expand Down
5 changes: 4 additions & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8146,6 +8146,7 @@ where
peer_state.pending_msg_events.push(msg_send_event);
};
if let Some(mut signing_session) = signing_session_opt {
let funding_txid = signing_session.unsigned_tx.txid();
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut() {
ChannelPhase::UnfundedOutboundV2(chan) => {
chan.funding_tx_constructed(&mut signing_session, &self.logger)
Expand All @@ -8158,7 +8159,7 @@ where
.into())),
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
let channel = match channel_phase {
let mut channel = match channel_phase {
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
_ => {
Expand All @@ -8168,6 +8169,7 @@ where
.into()))
},
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
channel.set_next_funding_txid(&funding_txid);
peer_state.channel_by_id.insert(channel_id, ChannelPhase::Funded(channel));
if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
let mut pending_events = self.pending_events.lock().unwrap();
Expand Down Expand Up @@ -8211,6 +8213,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,
Expand Down

0 comments on commit 32b7b0b

Please sign in to comment.