Skip to content

Commit 999f70e

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 765c757 commit 999f70e

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ pub(super) struct ReestablishResponses {
927927
pub order: RAACommitmentOrder,
928928
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
929929
pub shutdown_msg: Option<msgs::Shutdown>,
930+
pub tx_signatures: Option<msgs::TxSignatures>,
931+
pub tx_abort: Option<msgs::TxAbort>,
930932
}
931933

932934
/// The result of a shutdown that should be handled.
@@ -6594,6 +6596,8 @@ impl<SP: Deref> FundedChannel<SP> where
65946596
raa: None, commitment_update: None,
65956597
order: RAACommitmentOrder::CommitmentFirst,
65966598
shutdown_msg, announcement_sigs,
6599+
tx_signatures: None,
6600+
tx_abort: None,
65976601
});
65986602
}
65996603

@@ -6603,6 +6607,8 @@ impl<SP: Deref> FundedChannel<SP> where
66036607
raa: None, commitment_update: None,
66046608
order: RAACommitmentOrder::CommitmentFirst,
66056609
shutdown_msg, announcement_sigs,
6610+
tx_signatures: None,
6611+
tx_abort: None,
66066612
});
66076613
}
66086614

@@ -6648,11 +6654,53 @@ impl<SP: Deref> FundedChannel<SP> where
66486654
log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
66496655
}
66506656

6657+
// if next_funding_txid is set:
6658+
let (commitment_update, tx_signatures, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
6659+
if let Some(session) = &self.interactive_tx_signing_session {
6660+
// if next_funding_txid matches the latest interactive funding transaction:
6661+
if session.unsigned_tx.compute_txid() == next_funding_txid {
6662+
// if it has not received tx_signatures for that funding transaction:
6663+
if !session.counterparty_sent_tx_signatures {
6664+
// MUST retransmit its commitment_signed for that funding transaction.
6665+
let commitment_signed = self.context.get_initial_commitment_signed(logger)?;
6666+
let commitment_update = Some(msgs::CommitmentUpdate {
6667+
commitment_signed,
6668+
update_add_htlcs: vec![],
6669+
update_fulfill_htlcs: vec![],
6670+
update_fail_htlcs: vec![],
6671+
update_fail_malformed_htlcs: vec![],
6672+
update_fee: None,
6673+
});
6674+
// if it has already received commitment_signed and it should sign first, as specified in the tx_signatures requirements:
6675+
if session.received_commitment_signed && session.holder_sends_tx_signatures_first {
6676+
// MUST send its tx_signatures for that funding transaction.
6677+
(commitment_update, session.holder_tx_signatures.clone(), None)
6678+
} else {
6679+
(commitment_update, None, None)
6680+
}
6681+
} else {
6682+
// if it has already received tx_signatures for that funding transaction:
6683+
// MUST send its tx_signatures for that funding transaction.
6684+
(None, session.holder_tx_signatures.clone(), None)
6685+
}
6686+
} else {
6687+
// MUST send tx_abort to let the sending node know that they can forget this funding transaction.
6688+
(None, None, Some(msgs::TxAbort { channel_id: self.context.channel_id(), data: vec![] }))
6689+
}
6690+
} else {
6691+
// Counterparty set `next_funding_txid` at incorrect state.
6692+
// TODO(dual_funding): Should probably error here (or send tx_abort) but not in spec.
6693+
(None, None, None)
6694+
}
6695+
} else { (None, None, None) };
6696+
66516697
Ok(ReestablishResponses {
66526698
channel_ready, shutdown_msg, announcement_sigs,
66536699
raa: required_revoke,
6654-
commitment_update: None,
6700+
commitment_update,
66556701
order: self.context.resend_order.clone(),
6702+
tx_signatures,
6703+
tx_abort,
66566704
})
66576705
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
66586706
if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
@@ -6667,6 +6715,8 @@ impl<SP: Deref> FundedChannel<SP> where
66676715
channel_ready, shutdown_msg, announcement_sigs,
66686716
commitment_update: None, raa: None,
66696717
order: self.context.resend_order.clone(),
6718+
tx_signatures: None,
6719+
tx_abort: None,
66706720
})
66716721
} else {
66726722
let commitment_update = if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
@@ -6689,6 +6739,8 @@ impl<SP: Deref> FundedChannel<SP> where
66896739
channel_ready, shutdown_msg, announcement_sigs,
66906740
raa, commitment_update,
66916741
order: self.context.resend_order.clone(),
6742+
tx_signatures: None,
6743+
tx_abort: None,
66926744
})
66936745
}
66946746
} else if msg.next_local_commitment_number < next_counterparty_commitment_number {

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,7 +3181,7 @@ macro_rules! handle_monitor_update_completion {
31813181
&mut $peer_state.pending_msg_events, $chan, updates.raa,
31823182
updates.commitment_update, updates.order, updates.accepted_htlcs, updates.pending_update_adds,
31833183
updates.funding_broadcastable, updates.channel_ready,
3184-
updates.announcement_sigs, updates.tx_signatures);
3184+
updates.announcement_sigs, updates.tx_signatures, None);
31853185
if let Some(upd) = channel_update {
31863186
$peer_state.pending_msg_events.push(upd);
31873187
}
@@ -7450,10 +7450,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
74507450
pending_forwards: Vec<(PendingHTLCInfo, u64)>, pending_update_adds: Vec<msgs::UpdateAddHTLC>,
74517451
funding_broadcastable: Option<Transaction>,
74527452
channel_ready: Option<msgs::ChannelReady>, announcement_sigs: Option<msgs::AnnouncementSignatures>,
7453-
tx_signatures: Option<msgs::TxSignatures>
7453+
tx_signatures: Option<msgs::TxSignatures>, tx_abort: Option<msgs::TxAbort>,
74547454
) -> (Option<(u64, Option<PublicKey>, OutPoint, ChannelId, u128, Vec<(PendingHTLCInfo, u64)>)>, Option<(u64, Vec<msgs::UpdateAddHTLC>)>) {
74557455
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
7456-
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",
7456+
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",
74577457
&channel.context.channel_id(),
74587458
if raa.is_some() { "an" } else { "no" },
74597459
if commitment_update.is_some() { "a" } else { "no" },
@@ -7462,6 +7462,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
74627462
if channel_ready.is_some() { "sending" } else { "without" },
74637463
if announcement_sigs.is_some() { "sending" } else { "without" },
74647464
if tx_signatures.is_some() { "sending" } else { "without" },
7465+
if tx_abort.is_some() { "sending" } else { "without" },
74657466
);
74667467

74677468
let counterparty_node_id = channel.context.get_counterparty_node_id();
@@ -7495,6 +7496,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
74957496
msg,
74967497
});
74977498
}
7499+
if let Some(msg) = tx_abort {
7500+
pending_msg_events.push(events::MessageSendEvent::SendTxAbort {
7501+
node_id: counterparty_node_id,
7502+
msg,
7503+
});
7504+
}
74987505

74997506
macro_rules! handle_cs { () => {
75007507
if let Some(update) = commitment_update {
@@ -9288,7 +9295,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
92889295
let need_lnd_workaround = chan.context.workaround_lnd_bug_4006.take();
92899296
let (htlc_forwards, decode_update_add_htlcs) = self.handle_channel_resumption(
92909297
&mut peer_state.pending_msg_events, chan, responses.raa, responses.commitment_update, responses.order,
9291-
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs, None);
9298+
Vec::new(), Vec::new(), None, responses.channel_ready, responses.announcement_sigs,
9299+
responses.tx_signatures, responses.tx_abort);
92929300
debug_assert!(htlc_forwards.is_none());
92939301
debug_assert!(decode_update_add_htlcs.is_none());
92949302
if let Some(upd) = channel_update {

lightning/src/ln/interactivetxs.rs

Lines changed: 3 additions & 3 deletions
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)