Skip to content

Commit 2170dca

Browse files
committed
Add Splicing wire message definitions
2 parents 281a0ae + e3d9c8b commit 2170dca

File tree

8 files changed

+247
-0
lines changed

8 files changed

+247
-0
lines changed

lightning-net-tokio/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,9 @@ mod tests {
605605
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &ChannelUpdate) {}
606606
fn handle_open_channel_v2(&self, _their_node_id: &PublicKey, _msg: &OpenChannelV2) {}
607607
fn handle_accept_channel_v2(&self, _their_node_id: &PublicKey, _msg: &AcceptChannelV2) {}
608+
fn handle_splice(&self, _their_node_id: &PublicKey, _msg: &Splice) {}
609+
fn handle_splice_ack(&self, _their_node_id: &PublicKey, _msg: &SpliceAck) {}
610+
fn handle_splice_locked(&self, _their_node_id: &PublicKey, _msg: &SpliceLocked) {}
608611
fn handle_tx_add_input(&self, _their_node_id: &PublicKey, _msg: &TxAddInput) {}
609612
fn handle_tx_add_output(&self, _their_node_id: &PublicKey, _msg: &TxAddOutput) {}
610613
fn handle_tx_remove_input(&self, _their_node_id: &PublicKey, _msg: &TxRemoveInput) {}

lightning/src/events/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,27 @@ pub enum MessageSendEvent {
16421642
/// The message which should be sent.
16431643
msg: msgs::FundingSigned,
16441644
},
1645+
/// Used to indicate that a splice message should be sent to the peer with the given node id.
1646+
SendSplice {
1647+
/// The node_id of the node which should receive this message
1648+
node_id: PublicKey,
1649+
/// The message which should be sent.
1650+
msg: msgs::Splice,
1651+
},
1652+
/// Used to indicate that a splice_ack message should be sent to the peer with the given node id.
1653+
SendSpliceAck {
1654+
/// The node_id of the node which should receive this message
1655+
node_id: PublicKey,
1656+
/// The message which should be sent.
1657+
msg: msgs::SpliceAck,
1658+
},
1659+
/// Used to indicate that a splice_locked message should be sent to the peer with the given node id.
1660+
SendSpliceLocked {
1661+
/// The node_id of the node which should receive this message
1662+
node_id: PublicKey,
1663+
/// The message which should be sent.
1664+
msg: msgs::SpliceLocked,
1665+
},
16451666
/// Used to indicate that a tx_add_input message should be sent to the peer with the given node_id.
16461667
SendTxAddInput {
16471668
/// The node_id of the node which should receive this message

lightning/src/ln/channelmanager.rs

+22
Original file line numberDiff line numberDiff line change
@@ -8521,6 +8521,24 @@ where
85218521
});
85228522
}
85238523

8524+
fn handle_splice(&self, counterparty_node_id: &PublicKey, msg: &msgs::Splice) {
8525+
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8526+
"Splicing not supported".to_owned(),
8527+
msg.channel_id.clone())), *counterparty_node_id);
8528+
}
8529+
8530+
fn handle_splice_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceAck) {
8531+
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8532+
"Splicing not supported (splice_ack)".to_owned(),
8533+
msg.channel_id.clone())), *counterparty_node_id);
8534+
}
8535+
8536+
fn handle_splice_locked(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
8537+
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
8538+
"Splicing not supported (splice_locked)".to_owned(),
8539+
msg.channel_id.clone())), *counterparty_node_id);
8540+
}
8541+
85248542
fn handle_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) {
85258543
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
85268544
let _ = handle_error!(self, self.internal_shutdown(counterparty_node_id, msg), *counterparty_node_id);
@@ -8689,6 +8707,10 @@ where
86898707
// Common Channel Establishment
86908708
&events::MessageSendEvent::SendChannelReady { .. } => false,
86918709
&events::MessageSendEvent::SendAnnouncementSignatures { .. } => false,
8710+
// Splicing
8711+
&events::MessageSendEvent::SendSplice { .. } => false,
8712+
&events::MessageSendEvent::SendSpliceAck { .. } => false,
8713+
&events::MessageSendEvent::SendSpliceLocked { .. } => false,
86928714
// Interactive Transaction Construction
86938715
&events::MessageSendEvent::SendTxAddInput { .. } => false,
86948716
&events::MessageSendEvent::SendTxAddOutput { .. } => false,

lightning/src/ln/functional_test_utils.rs

+9
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,15 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &mut
814814
MessageSendEvent::SendOpenChannelV2 { node_id, .. } => {
815815
node_id == msg_node_id
816816
},
817+
MessageSendEvent::SendSplice { node_id, .. } => {
818+
node_id == msg_node_id
819+
},
820+
MessageSendEvent::SendSpliceAck { node_id, .. } => {
821+
node_id == msg_node_id
822+
},
823+
MessageSendEvent::SendSpliceLocked { node_id, .. } => {
824+
node_id == msg_node_id
825+
},
817826
MessageSendEvent::SendTxAddInput { node_id, .. } => {
818827
node_id == msg_node_id
819828
},

lightning/src/ln/msgs.rs

+114
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,53 @@ pub struct ChannelReady {
441441
pub short_channel_id_alias: Option<u64>,
442442
}
443443

444+
/// A splice message to be sent by or received from the splice initiator.
445+
/// TODO(splicing): Is using 'splice initiator' role OK?
446+
/// TODO(splicing): Can the channel acceptor later be the splice initiator?
447+
///
448+
// TODO(splicing): Add spec link for `splice`; still in draft, using from https://github.com/lightning/bolts/pull/863
449+
#[derive(Clone, Debug, PartialEq, Eq)]
450+
pub struct Splice {
451+
/// The channel ID where splicing is intended
452+
pub channel_id: ChannelId,
453+
/// The genesis hash of the blockchain where the channel is intended to be spliced
454+
pub chain_hash: ChainHash,
455+
/// The intended change in channel capacity: the amount to be added (positive value)
456+
/// or removed (negative value) by the sender (splice initiator) by splicing into/from the channel.
457+
pub relative_satoshis: i64,
458+
/// The feerate for the new funding transaction, set by the splice initiator
459+
pub funding_feerate_perkw: u32,
460+
/// The locktime for the new funding transaction
461+
pub locktime: u32,
462+
/// The key of the sender (splice initiator) controlling the new funding transaction
463+
pub funding_pubkey: PublicKey,
464+
}
465+
466+
/// A splice_ack message to be received by or sent to the splice initiator.
467+
///
468+
// TODO(splicing): Add spec link for `splice_ack`; still in draft, using from https://github.com/lightning/bolts/pull/863
469+
#[derive(Clone, Debug, PartialEq, Eq)]
470+
pub struct SpliceAck {
471+
/// The channel ID where splicing is intended
472+
pub channel_id: ChannelId,
473+
/// The genesis hash of the blockchain where the channel is intended to be spliced
474+
pub chain_hash: ChainHash,
475+
/// The intended change in channel capacity: the amount to be added (positive value)
476+
/// or removed (negative value) by the sender (splice acceptor) by splicing into/from the channel.
477+
pub relative_satoshis: i64,
478+
/// The key of the sender (splice acceptor) controlling the new funding transaction
479+
pub funding_pubkey: PublicKey,
480+
}
481+
482+
/// A splice_locked message to be sent to or received from a peer.
483+
///
484+
// TODO(splicing): Add spec link for `splice_locked`; still in draft, using from https://github.com/lightning/bolts/pull/863
485+
#[derive(Clone, Debug, PartialEq, Eq)]
486+
pub struct SpliceLocked {
487+
/// The channel ID
488+
pub channel_id: ChannelId,
489+
}
490+
444491
/// A tx_add_input message for adding an input during interactive transaction construction
445492
///
446493
// TODO(dual_funding): Add spec link for `tx_add_input`.
@@ -1409,6 +1456,14 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
14091456
/// Handle an incoming `closing_signed` message from the given peer.
14101457
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned);
14111458

1459+
// Splicing
1460+
/// Handle an incoming `splice` message from the given peer.
1461+
fn handle_splice(&self, their_node_id: &PublicKey, msg: &Splice);
1462+
/// Handle an incoming `splice_ack` message from the given peer.
1463+
fn handle_splice_ack(&self, their_node_id: &PublicKey, msg: &SpliceAck);
1464+
/// Handle an incoming `splice_locked` message from the given peer.
1465+
fn handle_splice_locked(&self, their_node_id: &PublicKey, msg: &SpliceLocked);
1466+
14121467
// Interactive channel construction
14131468
/// Handle an incoming `tx_add_input message` from the given peer.
14141469
fn handle_tx_add_input(&self, their_node_id: &PublicKey, msg: &TxAddInput);
@@ -1813,6 +1868,26 @@ impl_writeable_msg!(AcceptChannelV2, {
18131868
(2, require_confirmed_inputs, option),
18141869
});
18151870

1871+
impl_writeable_msg!(Splice, {
1872+
channel_id,
1873+
chain_hash,
1874+
relative_satoshis,
1875+
funding_feerate_perkw,
1876+
locktime,
1877+
funding_pubkey,
1878+
}, {});
1879+
1880+
impl_writeable_msg!(SpliceAck, {
1881+
channel_id,
1882+
chain_hash,
1883+
relative_satoshis,
1884+
funding_pubkey,
1885+
}, {});
1886+
1887+
impl_writeable_msg!(SpliceLocked, {
1888+
channel_id,
1889+
}, {});
1890+
18161891
impl_writeable_msg!(TxAddInput, {
18171892
channel_id,
18181893
serial_id,
@@ -3365,6 +3440,45 @@ mod tests {
33653440
assert_eq!(encoded_value, target_value);
33663441
}
33673442

3443+
#[test]
3444+
fn encoding_splice() {
3445+
let secp_ctx = Secp256k1::new();
3446+
let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3447+
let splice = msgs::Splice {
3448+
chain_hash: ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
3449+
channel_id: ChannelId::from_bytes([2; 32]),
3450+
relative_satoshis: 123456,
3451+
funding_feerate_perkw: 2000,
3452+
locktime: 0,
3453+
funding_pubkey: pubkey_1,
3454+
};
3455+
let encoded_value = splice.encode();
3456+
assert_eq!(hex::encode(encoded_value), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240000007d000000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
3457+
}
3458+
3459+
#[test]
3460+
fn encoding_splice_ack() {
3461+
let secp_ctx = Secp256k1::new();
3462+
let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
3463+
let splice = msgs::SpliceAck {
3464+
chain_hash: ChainHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
3465+
channel_id: ChannelId::from_bytes([2; 32]),
3466+
relative_satoshis: 123456,
3467+
funding_pubkey: pubkey_1,
3468+
};
3469+
let encoded_value = splice.encode();
3470+
assert_eq!(hex::encode(encoded_value), "02020202020202020202020202020202020202020202020202020202020202026fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000001e240031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
3471+
}
3472+
3473+
#[test]
3474+
fn encoding_splice_locked() {
3475+
let splice = msgs::SpliceLocked {
3476+
channel_id: ChannelId::from_bytes([2; 32]),
3477+
};
3478+
let encoded_value = splice.encode();
3479+
assert_eq!(hex::encode(encoded_value), "0202020202020202020202020202020202020202020202020202020202020202");
3480+
}
3481+
33683482
#[test]
33693483
fn encoding_tx_add_input() {
33703484
let tx_add_input = msgs::TxAddInput {

lightning/src/ln/peer_handler.rs

+38
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ impl ChannelMessageHandler for ErroringMessageHandler {
231231
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
232232
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
233233
}
234+
fn handle_splice(&self, their_node_id: &PublicKey, msg: &msgs::Splice) {
235+
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
236+
}
237+
fn handle_splice_ack(&self, their_node_id: &PublicKey, msg: &msgs::SpliceAck) {
238+
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
239+
}
240+
fn handle_splice_locked(&self, their_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
241+
ErroringMessageHandler::push_error(&self, their_node_id, msg.channel_id);
242+
}
234243
fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
235244
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
236245
}
@@ -1643,6 +1652,17 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
16431652
self.message_handler.chan_handler.handle_channel_ready(&their_node_id, &msg);
16441653
},
16451654

1655+
// Splicing messages:
1656+
wire::Message::Splice(msg) => {
1657+
self.message_handler.chan_handler.handle_splice(&their_node_id, &msg);
1658+
}
1659+
wire::Message::SpliceAck(msg) => {
1660+
self.message_handler.chan_handler.handle_splice_ack(&their_node_id, &msg);
1661+
}
1662+
wire::Message::SpliceLocked(msg) => {
1663+
self.message_handler.chan_handler.handle_splice_locked(&their_node_id, &msg);
1664+
}
1665+
16461666
// Interactive transaction construction messages:
16471667
wire::Message::TxAddInput(msg) => {
16481668
self.message_handler.chan_handler.handle_tx_add_input(&their_node_id, &msg);
@@ -1960,6 +1980,24 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
19601980
&msg.channel_id);
19611981
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
19621982
},
1983+
MessageSendEvent::SendSplice { ref node_id, ref msg} => {
1984+
log_debug!(self.logger, "Handling SendSplice event in peer_handler for node {} for channel {}",
1985+
log_pubkey!(node_id),
1986+
&msg.channel_id);
1987+
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
1988+
}
1989+
MessageSendEvent::SendSpliceAck { ref node_id, ref msg} => {
1990+
log_debug!(self.logger, "Handling SendSpliceAck event in peer_handler for node {} for channel {}",
1991+
log_pubkey!(node_id),
1992+
&msg.channel_id);
1993+
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
1994+
}
1995+
MessageSendEvent::SendSpliceLocked { ref node_id, ref msg} => {
1996+
log_debug!(self.logger, "Handling SendSpliceLocked event in peer_handler for node {} for channel {}",
1997+
log_pubkey!(node_id),
1998+
&msg.channel_id);
1999+
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
2000+
}
19632001
MessageSendEvent::SendTxAddInput { ref node_id, ref msg } => {
19642002
log_debug!(self.logger, "Handling SendTxAddInput event in peer_handler for node {} for channel {}",
19652003
log_pubkey!(node_id),

lightning/src/ln/wire.rs

+31
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type + TestEq {
5959
AcceptChannelV2(msgs::AcceptChannelV2),
6060
FundingCreated(msgs::FundingCreated),
6161
FundingSigned(msgs::FundingSigned),
62+
Splice(msgs::Splice),
63+
SpliceAck(msgs::SpliceAck),
64+
SpliceLocked(msgs::SpliceLocked),
6265
TxAddInput(msgs::TxAddInput),
6366
TxAddOutput(msgs::TxAddOutput),
6467
TxRemoveInput(msgs::TxRemoveInput),
@@ -110,6 +113,9 @@ impl<T> Writeable for Message<T> where T: core::fmt::Debug + Type + TestEq {
110113
&Message::AcceptChannelV2(ref msg) => msg.write(writer),
111114
&Message::FundingCreated(ref msg) => msg.write(writer),
112115
&Message::FundingSigned(ref msg) => msg.write(writer),
116+
&Message::Splice(ref msg) => msg.write(writer),
117+
&Message::SpliceAck(ref msg) => msg.write(writer),
118+
&Message::SpliceLocked(ref msg) => msg.write(writer),
113119
&Message::TxAddInput(ref msg) => msg.write(writer),
114120
&Message::TxAddOutput(ref msg) => msg.write(writer),
115121
&Message::TxRemoveInput(ref msg) => msg.write(writer),
@@ -161,6 +167,9 @@ impl<T> Type for Message<T> where T: core::fmt::Debug + Type + TestEq {
161167
&Message::AcceptChannelV2(ref msg) => msg.type_id(),
162168
&Message::FundingCreated(ref msg) => msg.type_id(),
163169
&Message::FundingSigned(ref msg) => msg.type_id(),
170+
&Message::Splice(ref msg) => msg.type_id(),
171+
&Message::SpliceAck(ref msg) => msg.type_id(),
172+
&Message::SpliceLocked(ref msg) => msg.type_id(),
164173
&Message::TxAddInput(ref msg) => msg.type_id(),
165174
&Message::TxAddOutput(ref msg) => msg.type_id(),
166175
&Message::TxRemoveInput(ref msg) => msg.type_id(),
@@ -258,6 +267,15 @@ fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u1
258267
msgs::FundingSigned::TYPE => {
259268
Ok(Message::FundingSigned(Readable::read(buffer)?))
260269
},
270+
msgs::Splice::TYPE => {
271+
Ok(Message::Splice(Readable::read(buffer)?))
272+
},
273+
msgs::SpliceAck::TYPE => {
274+
Ok(Message::SpliceAck(Readable::read(buffer)?))
275+
},
276+
msgs::SpliceLocked::TYPE => {
277+
Ok(Message::SpliceLocked(Readable::read(buffer)?))
278+
},
261279
msgs::TxAddInput::TYPE => {
262280
Ok(Message::TxAddInput(Readable::read(buffer)?))
263281
},
@@ -464,6 +482,19 @@ impl Encode for msgs::AcceptChannelV2 {
464482
const TYPE: u16 = 65;
465483
}
466484

485+
impl Encode for msgs::Splice {
486+
// TODO(splicing) Double check with spec; spec contains 74, which is probably wrong as it is used by tx_Abort; CLN uses 75
487+
const TYPE: u16 = 75;
488+
}
489+
490+
impl Encode for msgs::SpliceAck {
491+
const TYPE: u16 = 76;
492+
}
493+
494+
impl Encode for msgs::SpliceLocked {
495+
const TYPE: u16 = 77;
496+
}
497+
467498
impl Encode for msgs::TxAddInput {
468499
const TYPE: u16 = 66;
469500
}

lightning/src/util/test_utils.rs

+9
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,15 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
651651
fn handle_closing_signed(&self, _their_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
652652
self.received_msg(wire::Message::ClosingSigned(msg.clone()));
653653
}
654+
fn handle_splice(&self, _their_node_id: &PublicKey, msg: &msgs::Splice) {
655+
self.received_msg(wire::Message::Splice(msg.clone()));
656+
}
657+
fn handle_splice_ack(&self, _their_node_id: &PublicKey, msg: &msgs::SpliceAck) {
658+
self.received_msg(wire::Message::SpliceAck(msg.clone()));
659+
}
660+
fn handle_splice_locked(&self, _their_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
661+
self.received_msg(wire::Message::SpliceLocked(msg.clone()));
662+
}
654663
fn handle_update_add_htlc(&self, _their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
655664
self.received_msg(wire::Message::UpdateAddHTLC(msg.clone()));
656665
}

0 commit comments

Comments
 (0)