Skip to content

Commit

Permalink
Send&handle&encode infrastructure for splicing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Sep 4, 2023
1 parent 191c254 commit 2ddc70f
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ mod tests {
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &ChannelUpdate) {}
fn handle_open_channel_v2(&self, _their_node_id: &PublicKey, _msg: &OpenChannelV2) {}
fn handle_accept_channel_v2(&self, _their_node_id: &PublicKey, _msg: &AcceptChannelV2) {}
fn handle_splice(&self, _their_node_id: &PublicKey, _msg: &Splice) {}
fn handle_splice_ack(&self, _their_node_id: &PublicKey, _msg: &SpliceAck) {}
fn handle_splice_locked(&self, _their_node_id: &PublicKey, _msg: &SpliceLocked) {}
fn handle_tx_add_input(&self, _their_node_id: &PublicKey, _msg: &TxAddInput) {}
fn handle_tx_add_output(&self, _their_node_id: &PublicKey, _msg: &TxAddOutput) {}
fn handle_tx_remove_input(&self, _their_node_id: &PublicKey, _msg: &TxRemoveInput) {}
Expand Down
21 changes: 21 additions & 0 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,27 @@ pub enum MessageSendEvent {
/// The message which should be sent.
msg: msgs::FundingSigned,
},
/// Used to indicate that a splice message should be sent to the peer with the given node id.
SendSplice {
/// The node_id of the node which should receive this message
node_id: PublicKey,
/// The message which should be sent.
msg: msgs::Splice,
},
/// Used to indicate that a splice_ack message should be sent to the peer with the given node id.
SendSpliceAck {
/// The node_id of the node which should receive this message
node_id: PublicKey,
/// The message which should be sent.
msg: msgs::SpliceAck,
},
/// Used to indicate that a splice_locked message should be sent to the peer with the given node id.
SendSpliceLocked {
/// The node_id of the node which should receive this message
node_id: PublicKey,
/// The message which should be sent.
msg: msgs::SpliceLocked,
},
/// Used to indicate that a tx_add_input message should be sent to the peer with the given node_id.
SendTxAddInput {
/// The node_id of the node which should receive this message
Expand Down
22 changes: 22 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7326,6 +7326,24 @@ where
let _ = handle_error!(self, self.internal_channel_ready(counterparty_node_id, msg), *counterparty_node_id);
}

fn handle_splice(&self, counterparty_node_id: &PublicKey, msg: &msgs::Splice) {
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
"Splicing not supported".to_owned(),
msg.channel_id.clone())), *counterparty_node_id);
}

fn handle_splice_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceAck) {
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
"Splicing not supported (splice_ack)".to_owned(),
msg.channel_id.clone())), *counterparty_node_id);
}

fn handle_splice_locked(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
"Splicing not supported (splice_locked)".to_owned(),
msg.channel_id.clone())), *counterparty_node_id);
}

fn handle_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) {
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
let _ = handle_error!(self, self.internal_shutdown(counterparty_node_id, msg), *counterparty_node_id);
Expand Down Expand Up @@ -7438,6 +7456,10 @@ where
// Common Channel Establishment
&events::MessageSendEvent::SendChannelReady { .. } => false,
&events::MessageSendEvent::SendAnnouncementSignatures { .. } => false,
// Splicing
&events::MessageSendEvent::SendSplice { .. } => false,
&events::MessageSendEvent::SendSpliceAck { .. } => false,
&events::MessageSendEvent::SendSpliceLocked { .. } => false,
// Interactive Transaction Construction
&events::MessageSendEvent::SendTxAddInput { .. } => false,
&events::MessageSendEvent::SendTxAddOutput { .. } => false,
Expand Down
9 changes: 9 additions & 0 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,15 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &mut
MessageSendEvent::SendOpenChannelV2 { node_id, .. } => {
node_id == msg_node_id
},
MessageSendEvent::SendSplice { node_id, .. } => {
node_id == msg_node_id
},
MessageSendEvent::SendSpliceAck { node_id, .. } => {
node_id == msg_node_id
},
MessageSendEvent::SendSpliceLocked { node_id, .. } => {
node_id == msg_node_id
},
MessageSendEvent::SendTxAddInput { node_id, .. } => {
node_id == msg_node_id
},
Expand Down
38 changes: 38 additions & 0 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ impl ChannelMessageHandler for ErroringMessageHandler {
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
}
fn handle_splice(&self, their_node_id: &PublicKey, msg: &msgs::Splice) {
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
}
fn handle_splice_ack(&self, their_node_id: &PublicKey, msg: &msgs::SpliceAck) {
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
}
fn handle_splice_locked(&self, their_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
}
fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
}
Expand Down Expand Up @@ -1637,6 +1646,17 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
self.message_handler.chan_handler.handle_channel_ready(&their_node_id, &msg);
},

// Splicing messages:
wire::Message::Splice(msg) => {
self.message_handler.chan_handler.handle_splice(&their_node_id, &msg);
}
wire::Message::SpliceAck(msg) => {
self.message_handler.chan_handler.handle_splice_ack(&their_node_id, &msg);
}
wire::Message::SpliceLocked(msg) => {
self.message_handler.chan_handler.handle_splice_locked(&their_node_id, &msg);
}

// Interactive transaction construction messages:
wire::Message::TxAddInput(msg) => {
self.message_handler.chan_handler.handle_tx_add_input(&their_node_id, &msg);
Expand Down Expand Up @@ -1956,6 +1976,24 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
&msg.channel_id);
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
},
MessageSendEvent::SendSplice { ref node_id, ref msg} => {
log_debug!(self.logger, "Handling SendSplice event in peer_handler for node {} for channel {}",
log_pubkey!(node_id),
&msg.channel_id);
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
}
MessageSendEvent::SendSpliceAck { ref node_id, ref msg} => {
log_debug!(self.logger, "Handling SendSpliceAck event in peer_handler for node {} for channel {}",
log_pubkey!(node_id),
&msg.channel_id);
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
}
MessageSendEvent::SendSpliceLocked { ref node_id, ref msg} => {
log_debug!(self.logger, "Handling SendSpliceLocked event in peer_handler for node {} for channel {}",
log_pubkey!(node_id),
&msg.channel_id);
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
}
MessageSendEvent::SendTxAddInput { ref node_id, ref msg } => {
log_debug!(self.logger, "Handling SendTxAddInput event in peer_handler for node {} for channel {}",
log_pubkey!(node_id),
Expand Down
31 changes: 31 additions & 0 deletions lightning/src/ln/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type + TestEq {
AcceptChannelV2(msgs::AcceptChannelV2),
FundingCreated(msgs::FundingCreated),
FundingSigned(msgs::FundingSigned),
Splice(msgs::Splice),
SpliceAck(msgs::SpliceAck),
SpliceLocked(msgs::SpliceLocked),
TxAddInput(msgs::TxAddInput),
TxAddOutput(msgs::TxAddOutput),
TxRemoveInput(msgs::TxRemoveInput),
Expand Down Expand Up @@ -110,6 +113,9 @@ impl<T> Writeable for Message<T> where T: core::fmt::Debug + Type + TestEq {
&Message::AcceptChannelV2(ref msg) => msg.write(writer),
&Message::FundingCreated(ref msg) => msg.write(writer),
&Message::FundingSigned(ref msg) => msg.write(writer),
&Message::Splice(ref msg) => msg.write(writer),
&Message::SpliceAck(ref msg) => msg.write(writer),
&Message::SpliceLocked(ref msg) => msg.write(writer),
&Message::TxAddInput(ref msg) => msg.write(writer),
&Message::TxAddOutput(ref msg) => msg.write(writer),
&Message::TxRemoveInput(ref msg) => msg.write(writer),
Expand Down Expand Up @@ -161,6 +167,9 @@ impl<T> Type for Message<T> where T: core::fmt::Debug + Type + TestEq {
&Message::AcceptChannelV2(ref msg) => msg.type_id(),
&Message::FundingCreated(ref msg) => msg.type_id(),
&Message::FundingSigned(ref msg) => msg.type_id(),
&Message::Splice(ref msg) => msg.type_id(),
&Message::SpliceAck(ref msg) => msg.type_id(),
&Message::SpliceLocked(ref msg) => msg.type_id(),
&Message::TxAddInput(ref msg) => msg.type_id(),
&Message::TxAddOutput(ref msg) => msg.type_id(),
&Message::TxRemoveInput(ref msg) => msg.type_id(),
Expand Down Expand Up @@ -258,6 +267,15 @@ fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u1
msgs::FundingSigned::TYPE => {
Ok(Message::FundingSigned(Readable::read(buffer)?))
},
msgs::Splice::TYPE => {
Ok(Message::Splice(Readable::read(buffer)?))
},
msgs::SpliceAck::TYPE => {
Ok(Message::SpliceAck(Readable::read(buffer)?))
},
msgs::SpliceLocked::TYPE => {
Ok(Message::SpliceLocked(Readable::read(buffer)?))
},
msgs::TxAddInput::TYPE => {
Ok(Message::TxAddInput(Readable::read(buffer)?))
},
Expand Down Expand Up @@ -464,6 +482,19 @@ impl Encode for msgs::AcceptChannelV2 {
const TYPE: u16 = 65;
}

impl Encode for msgs::Splice {
// TODO(splicing) Double check with spec; spec contains 74, which is probably wrong as it is used by tx_Abort; CLN uses 75
const TYPE: u16 = 75;
}

impl Encode for msgs::SpliceAck {
const TYPE: u16 = 76;
}

impl Encode for msgs::SpliceLocked {
const TYPE: u16 = 77;
}

impl Encode for msgs::TxAddInput {
const TYPE: u16 = 66;
}
Expand Down
9 changes: 9 additions & 0 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
fn handle_closing_signed(&self, _their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
self.received_msg(wire::Message::ClosingSigned(msg.clone()));
}
fn handle_splice(&self, _their_node_id: &PublicKey, msg: &msgs::Splice) {
self.received_msg(wire::Message::Splice(msg.clone()));
}
fn handle_splice_ack(&self, _their_node_id: &PublicKey, msg: &msgs::SpliceAck) {
self.received_msg(wire::Message::SpliceAck(msg.clone()));
}
fn handle_splice_locked(&self, _their_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
self.received_msg(wire::Message::SpliceLocked(msg.clone()));
}
fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
self.received_msg(wire::Message::UpdateAddHTLC(msg.clone()));
}
Expand Down

0 comments on commit 2ddc70f

Please sign in to comment.