Skip to content

Commit

Permalink
Add splice wire message structs
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Sep 4, 2023
1 parent 073f078 commit 191c254
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,53 @@ pub struct ChannelReady {
pub short_channel_id_alias: Option<u64>,
}

/// A splice message to be sent by or received from the splice initiator.
/// TODO(splicing): Is using 'splice initiator' role OK?
/// TODO(splicing): Can the channel acceptor later be the splice initiator?
///
// TODO(splicing): Add spec link for `splice`; still in draft, using from https://github.com/lightning/bolts/pull/863
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Splice {
/// The channel ID where splicing is intended
pub channel_id: ChannelId,
/// The genesis hash of the blockchain where the channel is intended to be spliced
pub chain_hash: BlockHash,
/// The intended change in channel capacity: the amount to be added (positive value)
/// or removed (negative value) by the sender (splice initiator) by splicing into/from the channel.
pub relative_satoshis: u64,
/// The feerate for the new funding transaction, set by the splice initiator
pub funding_feerate_perkw: u32,
/// The locktime for the new funding transaction
pub locktime: u32,
/// The key of the sender (splice initiator) controlling the new funding transaction
pub funding_pubkey: PublicKey,
}

/// A splice_ack message to be received by or sent to the splice initiator.
///
// TODO(splicing): Add spec link for `splice_ack`; still in draft, using from https://github.com/lightning/bolts/pull/863
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SpliceAck {
/// The channel ID where splicing is intended
pub channel_id: ChannelId,
/// The genesis hash of the blockchain where the channel is intended to be spliced
pub chain_hash: BlockHash,
/// The intended change in channel capacity: the amount to be added (positive value)
/// or removed (negative value) by the sender (splice acceptor) by splicing into/from the channel.
pub relative_satoshis: u64,
/// The key of the sender (splice acceptor) controlling the new funding transaction
pub funding_pubkey: PublicKey,
}

/// A splice_locked message to be sent to or received from a peer.
///
// TODO(splicing): Add spec link for `splice_locked`; still in draft, using from https://github.com/lightning/bolts/pull/863
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SpliceLocked {
/// The channel ID
pub channel_id: ChannelId,
}

/// A tx_add_input message for adding an input during interactive transaction construction
///
// TODO(dual_funding): Add spec link for `tx_add_input`.
Expand Down Expand Up @@ -1229,12 +1276,20 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
/// Handle an incoming `channel_ready` message from the given peer.
fn handle_channel_ready(&self, their_node_id: &PublicKey, msg: &ChannelReady);

// Channl close:
// Channel close:
/// Handle an incoming `shutdown` message from the given peer.
fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &Shutdown);
/// Handle an incoming `closing_signed` message from the given peer.
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &ClosingSigned);

// Splicing
/// Handle an incoming `splice` message from the given peer.
fn handle_splice(&self, their_node_id: &PublicKey, msg: &Splice);
/// Handle an incoming `splice_ack` message from the given peer.
fn handle_splice_ack(&self, their_node_id: &PublicKey, msg: &SpliceAck);
/// Handle an incoming `splice_locked` message from the given peer.
fn handle_splice_locked(&self, their_node_id: &PublicKey, msg: &SpliceLocked);

// Interactive channel construction
/// Handle an incoming `tx_add_input message` from the given peer.
fn handle_tx_add_input(&self, their_node_id: &PublicKey, msg: &TxAddInput);
Expand Down Expand Up @@ -1613,6 +1668,26 @@ impl_writeable_msg!(AcceptChannelV2, {
(2, require_confirmed_inputs, option),
});

impl_writeable_msg!(Splice, {
channel_id,
chain_hash,
relative_satoshis,
funding_feerate_perkw,
locktime,
funding_pubkey,
}, {});

impl_writeable_msg!(SpliceAck, {
channel_id,
chain_hash,
relative_satoshis,
funding_pubkey,
}, {});

impl_writeable_msg!(SpliceLocked, {
channel_id,
}, {});

impl_writeable_msg!(TxAddInput, {
channel_id,
serial_id,
Expand Down Expand Up @@ -3115,6 +3190,45 @@ mod tests {
assert_eq!(encoded_value, target_value);
}

#[test]
fn encoding_splice() {
let secp_ctx = Secp256k1::new();
let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
let splice = msgs::Splice {
chain_hash: BlockHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
channel_id: ChannelId::from_bytes([2; 32]),
relative_satoshis: 123456,
funding_feerate_perkw: 2000,
locktime: 0,
funding_pubkey: pubkey_1,
};
let encoded_value = splice.encode();
assert_eq!(hex::encode(encoded_value), "0202020202020202020202020202020202020202020202020202020202020202000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f000000000001e240000007d000000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
}

#[test]
fn encoding_splice_ack() {
let secp_ctx = Secp256k1::new();
let (_, pubkey_1,) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
let splice = msgs::SpliceAck {
chain_hash: BlockHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap(),
channel_id: ChannelId::from_bytes([2; 32]),
relative_satoshis: 123456,
funding_pubkey: pubkey_1,
};
let encoded_value = splice.encode();
assert_eq!(hex::encode(encoded_value), "0202020202020202020202020202020202020202020202020202020202020202000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f000000000001e240031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f");
}

#[test]
fn encoding_splice_locked() {
let splice = msgs::SpliceLocked {
channel_id: ChannelId::from_bytes([2; 32]),
};
let encoded_value = splice.encode();
assert_eq!(hex::encode(encoded_value), "0202020202020202020202020202020202020202020202020202020202020202");
}

#[test]
fn encoding_tx_add_input() {
let tx_add_input = msgs::TxAddInput {
Expand Down

0 comments on commit 191c254

Please sign in to comment.