Skip to content

Add splice-out support #3979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
450 changes: 295 additions & 155 deletions lightning/src/ln/channel.rs

Large diffs are not rendered by default.

28 changes: 10 additions & 18 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ use bitcoin::hashes::{Hash, HashEngine, HmacEngine};

use bitcoin::secp256k1::Secp256k1;
use bitcoin::secp256k1::{PublicKey, SecretKey};
use bitcoin::{secp256k1, Sequence};
#[cfg(splicing)]
use bitcoin::{ScriptBuf, TxIn, Weight};
use bitcoin::{secp256k1, Sequence, SignedAmount};

use crate::blinded_path::message::MessageForwardNode;
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
Expand Down Expand Up @@ -65,6 +63,8 @@ use crate::ln::channel::{
UpdateFulfillCommitFetch, WithChannelContext,
};
use crate::ln::channel_state::ChannelDetails;
#[cfg(splicing)]
use crate::ln::funding::SpliceContribution;
use crate::ln::inbound_payment;
use crate::ln::interactivetxs::{HandleTxCompleteResult, InteractiveTxMessageSendResult};
use crate::ln::msgs;
Expand Down Expand Up @@ -4458,14 +4458,13 @@ where
#[cfg(splicing)]
#[rustfmt::skip]
pub fn splice_channel(
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey, our_funding_contribution_satoshis: i64,
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
funding_feerate_per_kw: u32, locktime: Option<u32>,
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
contribution: SpliceContribution, 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, change_script, funding_feerate_per_kw, locktime
channel_id, counterparty_node_id, contribution, funding_feerate_per_kw, locktime
);
res = result;
match res {
Expand All @@ -4480,9 +4479,7 @@ where
#[cfg(splicing)]
fn internal_splice_channel(
&self, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
our_funding_contribution_satoshis: i64,
our_funding_inputs: Vec<(TxIn, Transaction, Weight)>, change_script: Option<ScriptBuf>,
funding_feerate_per_kw: u32, locktime: Option<u32>,
contribution: SpliceContribution, funding_feerate_per_kw: u32, locktime: Option<u32>,
) -> Result<(), APIError> {
let per_peer_state = self.per_peer_state.read().unwrap();

Expand All @@ -4503,13 +4500,8 @@ where
hash_map::Entry::Occupied(mut chan_phase_entry) => {
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,
change_script,
funding_feerate_per_kw,
locktime,
)?;
let msg =
chan.splice_channel(contribution, funding_feerate_per_kw, locktime)?;
peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceInit {
node_id: *counterparty_node_id,
msg,
Expand Down Expand Up @@ -9401,7 +9393,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/

// Inbound V2 channels with contributed inputs are not considered unfunded.
if let Some(unfunded_chan) = chan.as_unfunded_v2() {
if unfunded_chan.funding_negotiation_context.our_funding_contribution_satoshis > 0 {
if unfunded_chan.funding_negotiation_context.our_funding_contribution > SignedAmount::ZERO {
continue;
}
}
Expand Down
14 changes: 6 additions & 8 deletions lightning/src/ln/dual_funding_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use {
crate::ln::channel::PendingV2Channel,
crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint},
crate::ln::functional_test_utils::*,
crate::ln::funding::FundingTxInput,
crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, MessageSendEvent},
crate::ln::msgs::{CommitmentSigned, TxAddInput, TxAddOutput, TxComplete, TxSignatures},
crate::ln::types::ChannelId,
crate::prelude::*,
crate::util::ser::TransactionU16LenLimited,
crate::util::test_utils,
bitcoin::Witness,
};
Expand All @@ -49,10 +49,7 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
let initiator_funding_inputs: Vec<_> = create_dual_funding_utxos_with_prev_txs(
&nodes[0],
&[session.initiator_input_value_satoshis],
)
.into_iter()
.map(|(txin, tx, _)| (txin, TransactionU16LenLimited::new(tx).unwrap()))
.collect();
);

// Alice creates a dual-funded channel as initiator.
let funding_satoshis = session.funding_input_sats;
Expand Down Expand Up @@ -86,15 +83,16 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
&RevocationBasepoint::from(open_channel_v2_msg.common_fields.revocation_basepoint),
);

let FundingTxInput { sequence, prevtx, .. } = &initiator_funding_inputs[0];
let tx_add_input_msg = TxAddInput {
channel_id,
serial_id: 2, // Even serial_id from initiator.
prevtx: Some(initiator_funding_inputs[0].1.clone()),
prevtx: Some(prevtx.clone()),
prevtx_out: 0,
sequence: initiator_funding_inputs[0].0.sequence.0,
sequence: sequence.0,
shared_input_txid: None,
};
let input_value = tx_add_input_msg.prevtx.as_ref().unwrap().as_transaction().output
let input_value = tx_add_input_msg.prevtx.as_ref().unwrap().output
[tx_add_input_msg.prevtx_out as usize]
.value;
assert_eq!(input_value.to_sat(), session.initiator_input_value_satoshis);
Expand Down
29 changes: 9 additions & 20 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::ln::channelmanager::{
AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, PaymentId,
RAACommitmentOrder, RecipientOnionFields, MIN_CLTV_EXPIRY_DELTA,
};
use crate::ln::funding::FundingTxInput;
use crate::ln::msgs;
use crate::ln::msgs::{
BaseMessageHandler, ChannelMessageHandler, MessageSendEvent, RoutingMessageHandler,
Expand Down Expand Up @@ -62,12 +63,10 @@ use bitcoin::script::ScriptBuf;
use bitcoin::secp256k1::{PublicKey, SecretKey};
use bitcoin::transaction::{self, Version as TxVersion};
use bitcoin::transaction::{Sequence, Transaction, TxIn, TxOut};
use bitcoin::witness::Witness;
use bitcoin::{WPubkeyHash, Weight};
use bitcoin::WPubkeyHash;

use crate::io;
use crate::prelude::*;
use crate::sign::P2WPKH_WITNESS_WEIGHT;
use crate::sync::{Arc, LockTestExt, Mutex, RwLock};
use alloc::rc::Rc;
use core::cell::RefCell;
Expand Down Expand Up @@ -1440,7 +1439,7 @@ fn internal_create_funding_transaction<'a, 'b, 'c>(
/// Return the inputs (with prev tx), and the total witness weight for these inputs
pub fn create_dual_funding_utxos_with_prev_txs(
node: &Node<'_, '_, '_>, utxo_values_in_satoshis: &[u64],
) -> Vec<(TxIn, Transaction, Weight)> {
) -> Vec<FundingTxInput> {
// Ensure we have unique transactions per node by using the locktime.
let tx = Transaction {
version: TxVersion::TWO,
Expand All @@ -1460,22 +1459,12 @@ pub fn create_dual_funding_utxos_with_prev_txs(
.collect(),
};

let mut inputs = vec![];
for i in 0..utxo_values_in_satoshis.len() {
inputs.push((
TxIn {
previous_output: OutPoint { txid: tx.compute_txid(), index: i as u16 }
.into_bitcoin_outpoint(),
script_sig: ScriptBuf::new(),
sequence: Sequence::ZERO,
witness: Witness::new(),
},
tx.clone(),
Weight::from_wu(P2WPKH_WITNESS_WEIGHT),
));
}

inputs
tx.output
.iter()
.enumerate()
.map(|(index, _)| index as u32)
.map(|vout| FundingTxInput::new_p2wpkh(tx.clone(), vout, Sequence::ZERO).unwrap())
.collect()
}

pub fn sign_funding_transaction<'a, 'b, 'c>(
Expand Down
179 changes: 179 additions & 0 deletions lightning/src/ln/funding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Types pertaining to funding channels.

#[cfg(splicing)]
use bitcoin::{Amount, ScriptBuf, SignedAmount, TxOut};
use bitcoin::{Script, Sequence, Transaction, Weight};

use crate::events::bump_transaction::{Utxo, EMPTY_SCRIPT_SIG_WEIGHT};
use crate::sign::{P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT};

/// The components of a splice's funding transaction that are contributed by one party.
#[cfg(splicing)]
pub enum SpliceContribution {
/// When funds are added to a channel.
SpliceIn {
/// The amount to contribute to the splice.
value: Amount,

/// The inputs included in the splice's funding transaction to meet the contributed amount.
/// Any excess amount will be sent to a change output.
inputs: Vec<FundingTxInput>,

/// An optional change output script. This will be used if needed or, when not set,
/// generated using [`SignerProvider::get_destination_script`].
change_script: Option<ScriptBuf>,
},
/// When funds are removed from a channel.
SpliceOut {
/// The outputs to include in the splice's funding transaction. The total value of all
/// outputs will be the amount that is removed.
outputs: Vec<TxOut>,
},
}

#[cfg(splicing)]
impl SpliceContribution {
pub(super) fn value(&self) -> SignedAmount {
match self {
SpliceContribution::SpliceIn { value, .. } => {
value.to_signed().unwrap_or(SignedAmount::MAX)
},
SpliceContribution::SpliceOut { outputs } => {
let value_removed = outputs
.iter()
.map(|txout| txout.value)
.sum::<Amount>()
.to_signed()
.unwrap_or(SignedAmount::MAX);
-value_removed
},
}
}

pub(super) fn inputs(&self) -> &[FundingTxInput] {
match self {
SpliceContribution::SpliceIn { inputs, .. } => &inputs[..],
SpliceContribution::SpliceOut { .. } => &[],
}
}

pub(super) fn outputs(&self) -> &[TxOut] {
match self {
SpliceContribution::SpliceIn { .. } => &[],
SpliceContribution::SpliceOut { outputs } => &outputs[..],
}
}

pub(super) fn into_tx_parts(self) -> (Vec<FundingTxInput>, Vec<TxOut>, Option<ScriptBuf>) {
match self {
SpliceContribution::SpliceIn { inputs, change_script, .. } => {
(inputs, vec![], change_script)
},
SpliceContribution::SpliceOut { outputs } => (vec![], outputs, None),
}
}
}

/// An input to contribute to a channel's funding transaction either when using the v2 channel
/// establishment protocol or when splicing.
#[derive(Clone)]
pub struct FundingTxInput {
/// The unspent [`TxOut`] that the input spends.
///
/// [`TxOut`]: bitcoin::TxOut
pub(super) utxo: Utxo,

/// The sequence number to use in the [`TxIn`].
///
/// [`TxIn`]: bitcoin::TxIn
pub(super) sequence: Sequence,

/// The transaction containing the unspent [`TxOut`] referenced by [`utxo`].
///
/// [`TxOut`]: bitcoin::TxOut
/// [`utxo`]: Self::utxo
pub(super) prevtx: Transaction,
}

impl FundingTxInput {
fn new<F: FnOnce(&bitcoin::Script) -> bool>(
prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight,
script_filter: F,
) -> Result<Self, ()> {
Ok(FundingTxInput {
utxo: Utxo {
outpoint: bitcoin::OutPoint { txid: prevtx.compute_txid(), vout },
output: prevtx
.output
.get(vout as usize)
.filter(|output| script_filter(&output.script_pubkey))
.ok_or(())?
.clone(),
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu(),
},
sequence,
prevtx,
})
}

/// Creates an input spending a P2WPKH output from the given `prevtx` at index `vout` using the
/// provided `sequence` number.
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
pub fn new_p2wpkh(prevtx: Transaction, vout: u32, sequence: Sequence) -> Result<Self, ()> {
let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT);
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2wpkh)
}

/// Creates an input spending a P2WSH output from the given `prevtx` at index `vout` using the
/// provided `sequence` number.
///
/// Requires passing the weight of witness needed to satisfy the output's script.
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
pub fn new_p2wsh(
prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight,
) -> Result<Self, ()> {
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2wsh)
}

/// Creates an input spending a P2TR output from the given `prevtx` at index `vout` using the
/// provided `sequence` number.
///
/// This is meant for inputs spending a taproot output using the key path. See
/// [`new_p2tr_script_spend`] for when spending using a script path.
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
///
/// [`new_p2tr_script_spend`]: Self::new_p2tr_script_spend
pub fn new_p2tr_key_spend(
prevtx: Transaction, vout: u32, sequence: Sequence,
) -> Result<Self, ()> {
let witness_weight = Weight::from_wu(P2TR_KEY_PATH_WITNESS_WEIGHT);
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2tr)
}

/// Creates an input spending a P2TR output from the given `prevtx` at index `vout` using the
/// provided `sequence` number.
///
/// Requires passing the weight of witness needed to satisfy a script path of the taproot
/// output. See [`new_p2tr_key_spend`] for when spending using the key path.
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
///
/// [`new_p2tr_key_spend`]: Self::new_p2tr_key_spend
pub fn new_p2tr_script_spend(
prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight,
) -> Result<Self, ()> {
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2tr)
}
}
Loading
Loading