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 Jun 4, 2024
1 parent 72eaed3 commit 4b8e447
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
26 changes: 22 additions & 4 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,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>,
}

#[cfg(any(dual_funding, splicing))]
Expand Down Expand Up @@ -2126,6 +2130,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
local_initiated_shutdown: None,

blocked_monitor_updates: Vec::new(),
next_funding_txid: None,
};

Ok(channel_context)
Expand Down Expand Up @@ -2346,6 +2351,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {

blocked_monitor_updates: Vec::new(),
local_initiated_shutdown: None,
next_funding_txid: None,
})
}

Expand Down Expand Up @@ -4519,6 +4525,16 @@ impl<SP: Deref> Channel<SP> where
self.context.channel_state.clear_waiting_for_batch();
}

#[cfg(any(dual_funding, splicing))]
pub fn set_next_funding_txid(&mut self, txid: &Txid) {
self.context.next_funding_txid = Some(*txid);
}

#[cfg(any(dual_funding, splicing))]
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 @@ -7505,10 +7521,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 @@ -9435,6 +9448,7 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
(43, malformed_htlcs, optional_vec), // Added in 0.0.119
// 45 and 47 are reserved for async signing
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
(51, self.context.next_funding_txid, option), // Added in 0.0.124
});

Ok(())
Expand Down Expand Up @@ -10010,6 +10024,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
local_initiated_shutdown,

blocked_monitor_updates: blocked_monitor_updates.unwrap(),
// 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,
},
#[cfg(any(dual_funding, splicing))]
dual_funding_channel_context: 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 @@ -8033,6 +8033,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) => {
Expand All @@ -8054,7 +8055,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));
Expand All @@ -8070,6 +8071,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)) => {
Expand Down Expand Up @@ -8105,6 +8107,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 4b8e447

Please sign in to comment.