Skip to content

Commit 56812cd

Browse files
committed
Handle receiving channel_reestablish with next_funding_txid
This follows the the specification closely in branching without being too verbose, so that it should be easy to follow the logic. See: https://github.com/lightning/bolts/blob/aa5207a/02-peer-protocol.md?plain=1#L2520-L2531
1 parent 44c1d1c commit 56812cd

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

lightning/src/ln/channel.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,8 @@ pub(super) struct ReestablishResponses {
925925
pub order: RAACommitmentOrder,
926926
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
927927
pub shutdown_msg: Option<msgs::Shutdown>,
928+
pub tx_signatures: Option<msgs::TxSignatures>,
929+
pub tx_abort: Option<msgs::TxAbort>,
928930
}
929931

930932
/// The result of a shutdown that should be handled.
@@ -6391,6 +6393,8 @@ impl<SP: Deref> Channel<SP> where
63916393
raa: None, commitment_update: None,
63926394
order: RAACommitmentOrder::CommitmentFirst,
63936395
shutdown_msg, announcement_sigs,
6396+
tx_signatures: None,
6397+
tx_abort: None,
63946398
});
63956399
}
63966400

@@ -6400,6 +6404,8 @@ impl<SP: Deref> Channel<SP> where
64006404
raa: None, commitment_update: None,
64016405
order: RAACommitmentOrder::CommitmentFirst,
64026406
shutdown_msg, announcement_sigs,
6407+
tx_signatures: None,
6408+
tx_abort: None,
64036409
});
64046410
}
64056411

@@ -6445,11 +6451,53 @@ impl<SP: Deref> Channel<SP> where
64456451
log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
64466452
}
64476453

6454+
// if next_funding_txid is set:
6455+
let (commitment_update, tx_signatures, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
6456+
if let Some(session) = &self.interactive_tx_signing_session {
6457+
// if next_funding_txid matches the latest interactive funding transaction:
6458+
if session.unsigned_tx.compute_txid() == next_funding_txid {
6459+
// if it has not received tx_signatures for that funding transaction:
6460+
if !session.counterparty_sent_tx_signatures {
6461+
// MUST retransmit its commitment_signed for that funding transaction.
6462+
let commitment_signed = self.context.get_initial_commitment_signed(logger)?;
6463+
let commitment_update = Some(msgs::CommitmentUpdate {
6464+
commitment_signed,
6465+
update_add_htlcs: vec![],
6466+
update_fulfill_htlcs: vec![],
6467+
update_fail_htlcs: vec![],
6468+
update_fail_malformed_htlcs: vec![],
6469+
update_fee: None,
6470+
});
6471+
// if it has already received commitment_signed and it should sign first, as specified in the tx_signatures requirements:
6472+
if session.received_commitment_signed && session.holder_sends_tx_signatures_first {
6473+
// MUST send its tx_signatures for that funding transaction.
6474+
(commitment_update, session.holder_tx_signatures.clone(), None)
6475+
} else {
6476+
(commitment_update, None, None)
6477+
}
6478+
} else {
6479+
// if it has already received tx_signatures for that funding transaction:
6480+
// MUST send its tx_signatures for that funding transaction.
6481+
(None, session.holder_tx_signatures.clone(), None)
6482+
}
6483+
} else {
6484+
// MUST send tx_abort to let the sending node know that they can forget this funding transaction.
6485+
(None, None, Some(msgs::TxAbort { channel_id: self.context.channel_id(), data: vec![] }))
6486+
}
6487+
} else {
6488+
// Counterparty set `next_funding_txid` at incorrect state.
6489+
// TODO(dual_funding): Should probably error here (or send tx_abort) but not in spec.
6490+
(None, None, None)
6491+
}
6492+
} else { (None, None, None) };
6493+
64486494
Ok(ReestablishResponses {
64496495
channel_ready, shutdown_msg, announcement_sigs,
64506496
raa: required_revoke,
6451-
commitment_update: None,
6497+
commitment_update,
64526498
order: self.context.resend_order.clone(),
6499+
tx_signatures,
6500+
tx_abort,
64536501
})
64546502
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
64556503
if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
@@ -6464,6 +6512,8 @@ impl<SP: Deref> Channel<SP> where
64646512
channel_ready, shutdown_msg, announcement_sigs,
64656513
commitment_update: None, raa: None,
64666514
order: self.context.resend_order.clone(),
6515+
tx_signatures: None,
6516+
tx_abort: None,
64676517
})
64686518
} else {
64696519
let commitment_update = if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
@@ -6486,6 +6536,8 @@ impl<SP: Deref> Channel<SP> where
64866536
channel_ready, shutdown_msg, announcement_sigs,
64876537
raa, commitment_update,
64886538
order: self.context.resend_order.clone(),
6539+
tx_signatures: None,
6540+
tx_abort: None,
64896541
})
64906542
}
64916543
} else if msg.next_local_commitment_number < next_counterparty_commitment_number {

lightning/src/ln/channelmanager.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -3168,7 +3168,7 @@ macro_rules! handle_monitor_update_completion {
31683168
&mut $peer_state.pending_msg_events, $chan, updates.raa,
31693169
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
31703170
updates.funding_broadcastable, updates.channel_ready,
3171-
updates.announcement_sigs, updates.tx_signatures);
3171+
updates.announcement_sigs, updates.tx_signatures, None);
31723172
if let Some(upd) = channel_update {
31733173
$peer_state.pending_msg_events.push(upd);
31743174
}
@@ -7398,10 +7398,10 @@ where
73987398
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
73997399
funding_broadcastable: Option<Transaction>,
74007400
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
7401-
tx_signatures: Option<msgs::TxSignatures>
7401+
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
74027402
) -> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
74037403
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
7404-
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures",
7404+
log_trace!(logger, "Handling channel resumption for channel {} with {} RAA, {} commitment update, {} pending forwards, {} pending update_add_htlcs, {}broadcasting funding, {} channel ready, {} announcement, {} tx_signatures, {} tx_abort",
74057405
&channel.context.channel_id(),
74067406
if raa.is_some() { "an" } else { "no" },
74077407
if commitment_update.is_some() { "a" } else { "no" },
@@ -7410,6 +7410,7 @@ where
74107410
if channel_ready.is_some() { "sending" } else { "without" },
74117411
if announcement_sigs.is_some() { "sending" } else { "without" },
74127412
if tx_signatures.is_some() { "sending" } else { "without" },
7413+
if tx_abort.is_some() { "sending" } else { "without" },
74137414
);
74147415

74157416
let counterparty_node_id = channel.context.get_counterparty_node_id();
@@ -7443,6 +7444,12 @@ where
74437444
msg,
74447445
});
74457446
}
7447+
if let Some(msg) = tx_abort {
7448+
pending_msg_events.push(events::MessageSendEvent::SendTxAbort {
7449+
node_id: counterparty_node_id,
7450+
msg,
7451+
});
7452+
}
74467453

74477454
macro_rules! handle_cs { () => {
74487455
if let Some(update) = commitment_update {
@@ -9241,7 +9248,8 @@ where
92419248
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
92429249
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
92439250
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
9244-
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs, None);
9251+
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
9252+
responses.tx_signatures, responses.tx_abort);
92459253
debug_assert!(htlc_forwards.is_none());
92469254
debug_assert!(decode_update_add_htlcs.is_none());
92479255
if let Some(upd) = channel_update {

lightning/src/ln/interactivetxs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ impl ConstructedTransaction {
291291
pub(crate) struct InteractiveTxSigningSession {
292292
pub unsigned_tx: ConstructedTransaction,
293293
pub counterparty_sent_tx_signatures: bool,
294-
holder_sends_tx_signatures_first: bool,
295-
received_commitment_signed: bool,
296-
holder_tx_signatures: Option<TxSignatures>,
294+
pub holder_sends_tx_signatures_first: bool,
295+
pub received_commitment_signed: bool,
296+
pub holder_tx_signatures: Option<TxSignatures>,
297297
}
298298

299299
impl InteractiveTxSigningSession {

0 commit comments

Comments
 (0)