Skip to content

Commit

Permalink
fix Renames and removes
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Mar 4, 2025
1 parent df4e030 commit b7c2d45
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 129 deletions.
102 changes: 6 additions & 96 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,23 +1729,6 @@ struct PendingSplice {
pub our_funding_contribution: i64,
}

#[cfg(splicing)]
impl PendingSplice {
#[inline]
fn add_checked(base: u64, delta: i64) -> u64 {
if delta >= 0 {
base.saturating_add(delta as u64)
} else {
base.saturating_sub(delta.abs() as u64)
}
}

/// Compute the post-splice channel value from the pre-splice values and the peer contributions
pub fn compute_post_value(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> u64 {
Self::add_checked(pre_channel_value, our_funding_contribution.saturating_add(their_funding_contribution))
}
}

/// Contains everything about the channel including state, and various flags.
pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
config: LegacyChannelConfig,
Expand Down Expand Up @@ -8447,7 +8430,6 @@ impl<SP: Deref> FundedChannel<SP> where
// (Cannot test for miminum required post-splice channel value)

// Check that inputs are sufficient to cover our contribution.
// Extra common weight is the weight for spending the old funding
let _fee = check_v2_funding_inputs_sufficient(our_funding_contribution_satoshis, &our_funding_inputs, true, true, funding_feerate_per_kw)
.map_err(|err| APIError::APIMisuseError { err: format!(
"Insufficient inputs for splicing; channel ID {}, err {}",
Expand All @@ -8464,7 +8446,7 @@ impl<SP: Deref> FundedChannel<SP> where

/// Get the splice message that can be sent during splice initiation.
#[cfg(splicing)]
pub fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
fn get_splice_init(&self, our_funding_contribution_satoshis: i64,
funding_feerate_per_kw: u32, locktime: u32,
) -> msgs::SpliceInit {
// TODO(splicing): The exisiting pubkey is reused, but a new one should be generated. See #3542.
Expand Down Expand Up @@ -8519,23 +8501,16 @@ impl<SP: Deref> FundedChannel<SP> where
// TODO(splicing): Store msg.funding_pubkey
// TODO(splicing): Apply start of splice (splice_start)

let splice_ack_msg = self.get_splice_ack(our_funding_contribution_satoshis);
// TODO(splicing): start interactive funding negotiation
Ok(splice_ack_msg)
}

/// Get the splice_ack message that can be sent in response to splice initiation.
#[cfg(splicing)]
pub fn get_splice_ack(&self, our_funding_contribution_satoshis: i64) -> msgs::SpliceAck {
// TODO(splicing): The exisiting pubkey is reused, but a new one should be generated. See #3542.
// Note that channel_keys_id is supposed NOT to change
let funding_pubkey = self.funding.get_holder_pubkeys().funding_pubkey;
msgs::SpliceAck {
let splice_ack_msg = msgs::SpliceAck {
channel_id: self.context.channel_id,
funding_contribution_satoshis: our_funding_contribution_satoshis,
funding_pubkey,
funding_pubkey: self.funding.get_holder_pubkeys().funding_pubkey,
require_confirmed_inputs: None,
}
};
// TODO(splicing): start interactive funding negotiation
Ok(splice_ack_msg)
}

/// Handle splice_ack
Expand Down Expand Up @@ -13029,69 +13004,4 @@ mod tests {
);
}
}

#[cfg(splicing)]
fn get_pre_and_post(pre_channel_value: u64, our_funding_contribution: i64, their_funding_contribution: i64) -> (u64, u64) {
use crate::ln::channel::PendingSplice;

let post_channel_value = PendingSplice::compute_post_value(pre_channel_value, our_funding_contribution, their_funding_contribution);
(pre_channel_value, post_channel_value)
}

#[cfg(splicing)]
#[test]
fn test_splice_compute_post_value() {
{
// increase, small amounts
let (pre_channel_value, post_channel_value) = get_pre_and_post(9_000, 6_000, 0);
assert_eq!(pre_channel_value, 9_000);
assert_eq!(post_channel_value, 15_000);
}
{
// increase, small amounts
let (pre_channel_value, post_channel_value) = get_pre_and_post(9_000, 4_000, 2_000);
assert_eq!(pre_channel_value, 9_000);
assert_eq!(post_channel_value, 15_000);
}
{
// increase, small amounts
let (pre_channel_value, post_channel_value) = get_pre_and_post(9_000, 0, 6_000);
assert_eq!(pre_channel_value, 9_000);
assert_eq!(post_channel_value, 15_000);
}
{
// decrease, small amounts
let (pre_channel_value, post_channel_value) = get_pre_and_post(15_000, -6_000, 0);
assert_eq!(pre_channel_value, 15_000);
assert_eq!(post_channel_value, 9_000);
}
{
// decrease, small amounts
let (pre_channel_value, post_channel_value) = get_pre_and_post(15_000, -4_000, -2_000);
assert_eq!(pre_channel_value, 15_000);
assert_eq!(post_channel_value, 9_000);
}
{
// increase and decrease
let (pre_channel_value, post_channel_value) = get_pre_and_post(15_000, 4_000, -2_000);
assert_eq!(pre_channel_value, 15_000);
assert_eq!(post_channel_value, 17_000);
}
let base2: u64 = 2;
let huge63i3 = (base2.pow(63) - 3) as i64;
assert_eq!(huge63i3, 9223372036854775805);
assert_eq!(-huge63i3, -9223372036854775805);
{
// increase, large amount
let (pre_channel_value, post_channel_value) = get_pre_and_post(9_000, huge63i3, 3);
assert_eq!(pre_channel_value, 9_000);
assert_eq!(post_channel_value, 9223372036854784807);
}
{
// increase, large amounts
let (pre_channel_value, post_channel_value) = get_pre_and_post(9_000, huge63i3, huge63i3);
assert_eq!(pre_channel_value, 9_000);
assert_eq!(post_channel_value, 9223372036854784807);
}
}
}
64 changes: 32 additions & 32 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4283,6 +4283,37 @@ where
}
}

/// Initiate a splice, to change the channel capacity of an existing funded channel.
/// After completion of splicing, the funding transaction will be replaced by a new one, spending the old funding transaction,
/// with optional extra inputs (splice-in) and/or extra outputs (splice-out or change).
/// TODO(splicing): Implementation is currently incomplete.
///
/// Note: Currently only splice-in is supported (increase in channel capacity), splice-out is not.
///
/// - `our_funding_contribution_satoshis`: the amount contributed by us to the channel. This will increase our channel balance.
/// - `our_funding_inputs`: the funding inputs provided by us. If our contribution is positive, our funding inputs must cover at least that amount.
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
/// - `locktime`: Optional locktime for the new funding transaction. If None, set to the current block height.
#[cfg(splicing)]
pub fn splice_channel(
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
funding_feerate_per_kw: u32, locktime: Option<u32>,
) -> Result<(), APIError> {
let mut res = Ok(());
PersistenceNotifierGuard::optionally_notify(self, || {
let result = self.internal_splice_channel(
channel_id, counterparty_node_id, our_funding_contribution_satoshis, &our_funding_inputs, funding_feerate_per_kw, locktime
);
res = result;
match res {
Ok(_) => NotifyOption::SkipPersistHandleEvents,
Err(_) => NotifyOption::SkipPersistNoEvents,
}
});
res
}

/// See [`splice_channel`]
#[cfg(splicing)]
fn internal_splice_channel(
Expand All @@ -4304,7 +4335,7 @@ where
// Look for the channel
match peer_state.channel_by_id.entry(*channel_id) {
hash_map::Entry::Occupied(mut chan_phase_entry) => {
let locktime = locktime.unwrap_or(self.current_best_block().height);
let locktime = locktime.unwrap_or_else(|| self.current_best_block().height);
if let Some(chan) = chan_phase_entry.get_mut().as_funded_mut() {
let msg = chan.splice_channel(our_funding_contribution_satoshis, our_funding_inputs, funding_feerate_per_kw, locktime)?;
peer_state.pending_msg_events.push(events::MessageSendEvent::SendSpliceInit {
Expand Down Expand Up @@ -4332,37 +4363,6 @@ where
}
}

/// Initiate a splice, to change the channel capacity of an existing funded channel.
/// After completion of splicing, the funding transaction will be replaced by a new one, spending the old funding transaction,
/// with optional extra inputs (splice-in) and/or extra outputs (splice-out or change).
/// TODO(splicing): Implementation is currently incomplete.
///
/// Note: Currently only splice-in is supported (increase in channel capacity), splice-out is not.
///
/// - our_funding_contribution_satoshis: the amount contributed by us to the channel. This will increase our channel balance.
/// - our_funding_inputs: the funding inputs provided by us. If our contribution is positive, our funding inputs must cover at least that amount.
/// Includes the witness weight for this input (e.g. P2WPKH_WITNESS_WEIGHT=109 for typical P2WPKH inputs).
/// - locktime: Optional locktime for the new funding transaction. If None, set to the current block height.
#[cfg(splicing)]
pub fn splice_channel(
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>,
funding_feerate_per_kw: u32, locktime: Option<u32>,
) -> Result<(), APIError> {
let mut res = Ok(());
PersistenceNotifierGuard::optionally_notify(self, || {
let result = self.internal_splice_channel(
channel_id, counterparty_node_id, our_funding_contribution_satoshis, &our_funding_inputs, funding_feerate_per_kw, locktime
);
res = result;
match res {
Ok(_) => NotifyOption::SkipPersistHandleEvents,
Err(_) => NotifyOption::SkipPersistNoEvents,
}
});
res
}

fn can_forward_htlc_to_outgoing_channel(
&self, chan: &mut FundedChannel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
) -> Result<(), (&'static str, u16)> {
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod async_payments_tests;
pub mod functional_tests;
#[cfg(all(test, splicing))]
#[allow(unused_mut)]
mod functional_tests_splice;
mod splicing_tests;
#[cfg(test)]
#[allow(unused_mut)]
mod max_payment_path_len_tests;
Expand Down
File renamed without changes.

0 comments on commit b7c2d45

Please sign in to comment.