Skip to content

Commit 5c88594

Browse files
committed
Add ClosingComplete and ClosingSig message types + serialization
1 parent b732964 commit 5c88594

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ mod tests {
716716
fn handle_channel_ready(&self, _their_node_id: PublicKey, _msg: &ChannelReady) {}
717717
fn handle_shutdown(&self, _their_node_id: PublicKey, _msg: &Shutdown) {}
718718
fn handle_closing_signed(&self, _their_node_id: PublicKey, _msg: &ClosingSigned) {}
719+
#[cfg(simple_close)]
720+
fn handle_closing_complete(&self, _their_node_id: PublicKey, _msg: ClosingComplete) {}
721+
#[cfg(simple_close)]
722+
fn handle_closing_sig(&self, _their_node_id: PublicKey, _msg: ClosingSig) {}
719723
fn handle_update_add_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateAddHTLC) {}
720724
fn handle_update_fulfill_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateFulfillHTLC) {}
721725
fn handle_update_fail_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateFailHTLC) {}

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9352,6 +9352,20 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
93529352
Ok(())
93539353
}
93549354

9355+
#[cfg(simple_close)]
9356+
fn internal_closing_complete(
9357+
&self, _counterparty_node_id: PublicKey, _msg: msgs::ClosingComplete,
9358+
) -> Result<(), MsgHandleErrInternal> {
9359+
unimplemented!("Handling ClosingComplete is not implemented");
9360+
}
9361+
9362+
#[cfg(simple_close)]
9363+
fn internal_closing_sig(
9364+
&self, _counterparty_node_id: PublicKey, _msg: msgs::ClosingSig,
9365+
) -> Result<(), MsgHandleErrInternal> {
9366+
unimplemented!("Handling ClosingSig is not implemented");
9367+
}
9368+
93559369
#[rustfmt::skip]
93569370
fn internal_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), MsgHandleErrInternal> {
93579371
//TODO: BOLT 4 points out a specific attack where a peer may re-send an onion packet and
@@ -12650,6 +12664,26 @@ where
1265012664
let _ = handle_error!(self, res, counterparty_node_id);
1265112665
}
1265212666

12667+
#[cfg(simple_close)]
12668+
fn handle_closing_complete(&self, counterparty_node_id: PublicKey, msg: msgs::ClosingComplete) {
12669+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
12670+
let _ = handle_error!(
12671+
self,
12672+
self.internal_closing_complete(counterparty_node_id, msg),
12673+
counterparty_node_id
12674+
);
12675+
}
12676+
12677+
#[cfg(simple_close)]
12678+
fn handle_closing_sig(&self, counterparty_node_id: PublicKey, msg: msgs::ClosingSig) {
12679+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
12680+
let _ = handle_error!(
12681+
self,
12682+
self.internal_closing_sig(counterparty_node_id, msg),
12683+
counterparty_node_id
12684+
);
12685+
}
12686+
1265312687
fn handle_update_add_htlc(&self, counterparty_node_id: PublicKey, msg: &msgs::UpdateAddHTLC) {
1265412688
// Note that we never need to persist the updated ChannelManager for an inbound
1265512689
// update_add_htlc message - the message itself doesn't change our channel state only the

lightning/src/ln/msgs.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,48 @@ pub struct ClosingSigned {
684684
pub fee_range: Option<ClosingSignedFeeRange>,
685685
}
686686

687+
/// A [`closing_complete`] message to be sent to or received from a peer.
688+
///
689+
/// [`closing_complete`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#closing-negotiation-closing_complete-and-closing_sig
690+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
691+
pub struct ClosingComplete {
692+
/// The channel ID.
693+
pub channel_id: ChannelId,
694+
/// The destination of the closer's funds on closing.
695+
pub closer_scriptpubkey: ScriptBuf,
696+
/// The destination of the closee's funds on closing.
697+
pub closee_scriptpubkey: ScriptBuf,
698+
/// The proposed total fee for the closing transaction.
699+
pub fee_satoshis: u64,
700+
/// The locktime of the closing transaction.
701+
pub locktime: u32,
702+
/// A signature on the closing transaction omitting the `closee` output.
703+
pub closer_output_only: Option<Signature>,
704+
/// A signature on the closing transaction omitting the `closer` output.
705+
pub closee_output_only: Option<Signature>,
706+
/// A signature on the closing transaction covering both `closer` and `closee` outputs.
707+
pub closer_and_closee_outputs: Option<Signature>,
708+
}
709+
710+
/// A [`closing_sig`] message to be sent to or received from a peer.
711+
///
712+
/// [`closing_sig`]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#closing-negotiation-closing_complete-and-closing_sig
713+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
714+
pub struct ClosingSig {
715+
/// The channel ID.
716+
pub channel_id: ChannelId,
717+
/// The destination of the closer's funds on closing.
718+
pub closer_scriptpubkey: ScriptBuf,
719+
/// The destination of the closee's funds on closing.
720+
pub closee_scriptpubkey: ScriptBuf,
721+
/// A signature on the closing transaction omitting the `closee` output.
722+
pub closer_output_only: Option<Signature>,
723+
/// A signature on the closing transaction omitting the `closer` output.
724+
pub closee_output_only: Option<Signature>,
725+
/// A signature on the closing transaction covering both `closer` and `closee` outputs.
726+
pub closer_and_closee_outputs: Option<Signature>,
727+
}
728+
687729
/// A [`start_batch`] message to be sent to group together multiple channel messages as a single
688730
/// logical message.
689731
///
@@ -1914,6 +1956,12 @@ pub trait ChannelMessageHandler: BaseMessageHandler {
19141956
fn handle_shutdown(&self, their_node_id: PublicKey, msg: &Shutdown);
19151957
/// Handle an incoming `closing_signed` message from the given peer.
19161958
fn handle_closing_signed(&self, their_node_id: PublicKey, msg: &ClosingSigned);
1959+
/// Handle an incoming `closing_complete` message from the given peer.
1960+
#[cfg(simple_close)]
1961+
fn handle_closing_complete(&self, their_node_id: PublicKey, msg: ClosingComplete);
1962+
/// Handle an incoming `closing_sig` message from the given peer.
1963+
#[cfg(simple_close)]
1964+
fn handle_closing_sig(&self, their_node_id: PublicKey, msg: ClosingSig);
19171965

19181966
// Quiescence
19191967
/// Handle an incoming `stfu` message from the given peer.
@@ -2744,6 +2792,24 @@ impl_writeable_msg!(ClosingSigned,
27442792
{ (1, fee_range, option) }
27452793
);
27462794

2795+
impl_writeable_msg!(ClosingComplete,
2796+
{ channel_id, closer_scriptpubkey, closee_scriptpubkey, fee_satoshis, locktime },
2797+
{
2798+
(1, closer_output_only, option),
2799+
(2, closee_output_only, option),
2800+
(3, closer_and_closee_outputs, option)
2801+
}
2802+
);
2803+
2804+
impl_writeable_msg!(ClosingSig,
2805+
{ channel_id, closer_scriptpubkey, closee_scriptpubkey },
2806+
{
2807+
(1, closer_output_only, option),
2808+
(2, closee_output_only, option),
2809+
(3, closer_and_closee_outputs, option)
2810+
}
2811+
);
2812+
27472813
impl_writeable!(ClosingSignedFeeRange, {
27482814
min_fee_satoshis,
27492815
max_fee_satoshis

lightning/src/ln/peer_handler.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ impl ChannelMessageHandler for ErroringMessageHandler {
304304
fn handle_closing_signed(&self, their_node_id: PublicKey, msg: &msgs::ClosingSigned) {
305305
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
306306
}
307+
#[cfg(simple_close)]
308+
fn handle_closing_complete(&self, their_node_id: PublicKey, msg: msgs::ClosingComplete) {
309+
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
310+
}
311+
#[cfg(simple_close)]
312+
fn handle_closing_sig(&self, their_node_id: PublicKey, msg: msgs::ClosingSig) {
313+
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
314+
}
307315
fn handle_stfu(&self, their_node_id: PublicKey, msg: &msgs::Stfu) {
308316
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
309317
}
@@ -449,7 +457,7 @@ pub struct MessageHandler<CM: Deref, RM: Deref, OM: Deref, CustomM: Deref, SM: D
449457

450458
/// A message handler which only allows sending messages. This should generally be a
451459
/// [`ChainMonitor`].
452-
///
460+
///
453461
/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
454462
pub send_only_message_handler: SM,
455463
}
@@ -2097,6 +2105,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20972105
wire::Message::ClosingSigned(msg) => {
20982106
self.message_handler.chan_handler.handle_closing_signed(their_node_id, &msg);
20992107
},
2108+
#[cfg(simple_close)]
2109+
wire::Message::ClosingComplete(msg) => {
2110+
self.message_handler.chan_handler.handle_closing_complete(their_node_id, msg);
2111+
},
2112+
#[cfg(simple_close)]
2113+
wire::Message::ClosingSig(msg) => {
2114+
self.message_handler.chan_handler.handle_closing_sig(their_node_id, msg);
2115+
},
21002116

21012117
// Commitment messages:
21022118
wire::Message::UpdateAddHTLC(msg) => {

lightning/src/ln/wire.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ pub(crate) enum Message<T: core::fmt::Debug + Type + TestEq> {
8181
ChannelReady(msgs::ChannelReady),
8282
Shutdown(msgs::Shutdown),
8383
ClosingSigned(msgs::ClosingSigned),
84+
#[cfg(simple_close)]
85+
ClosingComplete(msgs::ClosingComplete),
86+
#[cfg(simple_close)]
87+
ClosingSig(msgs::ClosingSig),
8488
OnionMessage(msgs::OnionMessage),
8589
StartBatch(msgs::StartBatch),
8690
UpdateAddHTLC(msgs::UpdateAddHTLC),
@@ -142,6 +146,10 @@ impl<T: core::fmt::Debug + Type + TestEq> Writeable for Message<T> {
142146
&Message::ChannelReady(ref msg) => msg.write(writer),
143147
&Message::Shutdown(ref msg) => msg.write(writer),
144148
&Message::ClosingSigned(ref msg) => msg.write(writer),
149+
#[cfg(simple_close)]
150+
&Message::ClosingComplete(ref msg) => msg.write(writer),
151+
#[cfg(simple_close)]
152+
&Message::ClosingSig(ref msg) => msg.write(writer),
145153
&Message::OnionMessage(ref msg) => msg.write(writer),
146154
&Message::StartBatch(ref msg) => msg.write(writer),
147155
&Message::UpdateAddHTLC(ref msg) => msg.write(writer),
@@ -203,6 +211,10 @@ impl<T: core::fmt::Debug + Type + TestEq> Type for Message<T> {
203211
&Message::ChannelReady(ref msg) => msg.type_id(),
204212
&Message::Shutdown(ref msg) => msg.type_id(),
205213
&Message::ClosingSigned(ref msg) => msg.type_id(),
214+
#[cfg(simple_close)]
215+
&Message::ClosingComplete(ref msg) => msg.type_id(),
216+
#[cfg(simple_close)]
217+
&Message::ClosingSig(ref msg) => msg.type_id(),
206218
&Message::OnionMessage(ref msg) => msg.type_id(),
207219
&Message::StartBatch(ref msg) => msg.type_id(),
208220
&Message::UpdateAddHTLC(ref msg) => msg.type_id(),
@@ -350,6 +362,14 @@ where
350362
msgs::ClosingSigned::TYPE => {
351363
Ok(Message::ClosingSigned(LengthReadable::read_from_fixed_length_buffer(buffer)?))
352364
},
365+
#[cfg(simple_close)]
366+
msgs::ClosingComplete::TYPE => {
367+
Ok(Message::ClosingComplete(LengthReadable::read_from_fixed_length_buffer(buffer)?))
368+
},
369+
#[cfg(simple_close)]
370+
msgs::ClosingSig::TYPE => {
371+
Ok(Message::ClosingSig(LengthReadable::read_from_fixed_length_buffer(buffer)?))
372+
},
353373
msgs::OnionMessage::TYPE => {
354374
Ok(Message::OnionMessage(LengthReadable::read_from_fixed_length_buffer(buffer)?))
355375
},
@@ -535,6 +555,14 @@ impl Encode for msgs::ClosingSigned {
535555
const TYPE: u16 = 39;
536556
}
537557

558+
impl Encode for msgs::ClosingComplete {
559+
const TYPE: u16 = 40;
560+
}
561+
562+
impl Encode for msgs::ClosingSig {
563+
const TYPE: u16 = 41;
564+
}
565+
538566
impl Encode for msgs::OpenChannelV2 {
539567
const TYPE: u16 = 64;
540568
}

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,14 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
10241024
fn handle_closing_signed(&self, _their_node_id: PublicKey, msg: &msgs::ClosingSigned) {
10251025
self.received_msg(wire::Message::ClosingSigned(msg.clone()));
10261026
}
1027+
#[cfg(simple_close)]
1028+
fn handle_closing_complete(&self, _their_node_id: PublicKey, msg: msgs::ClosingComplete) {
1029+
self.received_msg(wire::Message::ClosingComplete(msg.clone()));
1030+
}
1031+
#[cfg(simple_close)]
1032+
fn handle_closing_sig(&self, _their_node_id: PublicKey, msg: msgs::ClosingSig) {
1033+
self.received_msg(wire::Message::ClosingSig(msg.clone()));
1034+
}
10271035
fn handle_stfu(&self, _their_node_id: PublicKey, msg: &msgs::Stfu) {
10281036
self.received_msg(wire::Message::Stfu(msg.clone()));
10291037
}

0 commit comments

Comments
 (0)