Skip to content

Commit d6dd33a

Browse files
committed
Handle re-establishment next_funding_txid
1 parent 91a37c2 commit d6dd33a

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,21 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14941494
/// If we can't release a [`ChannelMonitorUpdate`] until some external action completes, we
14951495
/// store it here and only release it to the `ChannelManager` once it asks for it.
14961496
blocked_monitor_updates: Vec<PendingChannelMonitorUpdate>,
1497+
// The `next_funding_txid` field allows peers to finalize the signing steps of an interactive
1498+
// transaction construction, or safely abort that transaction if it was not signed by one of the
1499+
// peers, who has thus already removed it from its state.
1500+
//
1501+
// If we've sent `commtiment_signed` for an interactive an interactively constructed transaction
1502+
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
1503+
// to the txid of that interactive transaction, else we MUST NOT set it.
1504+
//
1505+
// See the spec for further details on this:
1506+
// * `channel_reestablish`-sending node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2466-L2470
1507+
// * `channel_reestablish`-receiving node: https://github.com/lightning/bolts/blob/247e83d/02-peer-protocol.md?plain=1#L2520-L2531
1508+
//
1509+
// TODO(dual_funding): Persist this when we actually contribute funding inputs. For now we always
1510+
// send an empty witnesses array in `tx_signatures` as a V2 channel acceptor
1511+
next_funding_txid: Option<Txid>,
14971512
}
14981513

14991514
/// A channel struct implementing this trait can receive an initial counterparty commitment
@@ -2159,6 +2174,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21592174
blocked_monitor_updates: Vec::new(),
21602175

21612176
is_manual_broadcast: false,
2177+
2178+
next_funding_txid: None,
21622179
};
21632180

21642181
Ok(channel_context)
@@ -2390,6 +2407,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23902407
blocked_monitor_updates: Vec::new(),
23912408
local_initiated_shutdown: None,
23922409
is_manual_broadcast: false,
2410+
next_funding_txid: None,
23932411
})
23942412
}
23952413

@@ -4601,6 +4619,14 @@ impl<SP: Deref> Channel<SP> where
46014619
self.context.channel_state.clear_waiting_for_batch();
46024620
}
46034621

4622+
pub fn set_next_funding_txid(&mut self, txid: &Txid) {
4623+
self.context.next_funding_txid = Some(*txid);
4624+
}
4625+
4626+
pub fn clear_next_funding_txid(&mut self) {
4627+
self.context.next_funding_txid = None;
4628+
}
4629+
46044630
/// Unsets the existing funding information.
46054631
///
46064632
/// This must only be used if the channel has not yet completed funding and has not been used.
@@ -7680,10 +7706,7 @@ impl<SP: Deref> Channel<SP> where
76807706
next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.context.cur_counterparty_commitment_transaction_number - 1,
76817707
your_last_per_commitment_secret: remote_last_secret,
76827708
my_current_per_commitment_point: dummy_pubkey,
7683-
// TODO(dual_funding): If we've sent `commtiment_signed` for an interactive transaction
7684-
// construction but have not received `tx_signatures` we MUST set `next_funding_txid` to the
7685-
// txid of that interactive transaction, else we MUST NOT set it.
7686-
next_funding_txid: None,
7709+
next_funding_txid: self.context.next_funding_txid,
76877710
}
76887711
}
76897712

@@ -9439,7 +9462,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
94399462
(47, next_holder_commitment_point, option),
94409463
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
94419464
(51, is_manual_broadcast, option), // Added in 0.0.124
9442-
(53, funding_tx_broadcast_safe_event_emitted, option) // Added in 0.0.124
9465+
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
9466+
(55, self.context.next_funding_txid, option) // Added in 0.0.125
94439467
});
94449468

94459469
Ok(())
@@ -9729,6 +9753,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
97299753
let mut channel_pending_event_emitted = None;
97309754
let mut channel_ready_event_emitted = None;
97319755
let mut funding_tx_broadcast_safe_event_emitted = None;
9756+
let mut next_funding_txid = None;
97329757

97339758
let mut user_id_high_opt: Option<u64> = None;
97349759
let mut channel_keys_id: Option<[u8; 32]> = None;
@@ -9789,6 +9814,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
97899814
(49, local_initiated_shutdown, option),
97909815
(51, is_manual_broadcast, option),
97919816
(53, funding_tx_broadcast_safe_event_emitted, option),
9817+
(55, next_funding_txid, option) // Added in 0.0.125
97929818
});
97939819

97949820
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -10048,6 +10074,10 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1004810074

1004910075
blocked_monitor_updates: blocked_monitor_updates.unwrap(),
1005010076
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
10077+
// If we've sent `commtiment_signed` for an interactive transaction construction,
10078+
// but have not received `tx_signatures` we MUST set `next_funding_txid` to the
10079+
// txid of that interactive transaction, else we MUST NOT set it.
10080+
next_funding_txid,
1005110081
},
1005210082
interactive_tx_signing_session: None,
1005310083
})

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8279,6 +8279,7 @@ where
82798279
peer_state.pending_msg_events.push(msg_send_event);
82808280
};
82818281
if let Some(mut signing_session) = signing_session_opt {
8282+
let funding_txid = signing_session.unsigned_tx.txid();
82828283
let (commitment_signed, funding_ready_for_sig_event_opt) = match chan_phase_entry.get_mut() {
82838284
ChannelPhase::UnfundedOutboundV2(chan) => {
82848285
chan.funding_tx_constructed(&mut signing_session, &self.logger)
@@ -8291,7 +8292,7 @@ where
82918292
.into())),
82928293
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
82938294
let (channel_id, channel_phase) = chan_phase_entry.remove_entry();
8294-
let channel = match channel_phase {
8295+
let mut channel = match channel_phase {
82958296
ChannelPhase::UnfundedOutboundV2(chan) => chan.into_channel(signing_session),
82968297
ChannelPhase::UnfundedInboundV2(chan) => chan.into_channel(signing_session),
82978298
_ => {
@@ -8301,6 +8302,7 @@ where
83018302
.into()))
83028303
},
83038304
}.map_err(|err| MsgHandleErrInternal::send_err_msg_no_close(format!("{}", err), msg.channel_id))?;
8305+
channel.set_next_funding_txid(&funding_txid);
83048306
peer_state.channel_by_id.insert(channel_id, ChannelPhase::Funded(channel));
83058307
if let Some(funding_ready_for_sig_event) = funding_ready_for_sig_event_opt {
83068308
let mut pending_events = self.pending_events.lock().unwrap();
@@ -8344,6 +8346,7 @@ where
83448346
match channel_phase {
83458347
ChannelPhase::Funded(chan) => {
83468348
let (tx_signatures_opt, funding_tx_opt) = try_chan_phase_entry!(self, chan.tx_signatures(msg), chan_phase_entry);
8349+
chan.clear_next_funding_txid();
83478350
if let Some(tx_signatures) = tx_signatures_opt {
83488351
peer_state.pending_msg_events.push(events::MessageSendEvent::SendTxSignatures {
83498352
node_id: *counterparty_node_id,

0 commit comments

Comments
 (0)