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 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
429 changes: 293 additions & 136 deletions lightning/src/ln/channel.rs

Large diffs are not rendered by default.

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

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

use crate::blinded_path::message::MessageForwardNode;
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
Expand Down Expand Up @@ -200,6 +200,93 @@ pub use crate::ln::outbound_payment::{
};
use crate::ln::script::ShutdownScript;

/// 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 used to include in the splice's funding transaction used 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 {
/// An input for the funding transaction used to cover the channel contributions.
pub txin: TxIn,

/// The transaction containing the unspent [`TxOut`] referenced by [`txin`].
///
/// [`TxOut`]: bitcoin::TxOut
/// [`txin`]: Self::txin
pub prevtx: Transaction,

/// The weight of the witness that is needed to spend the [`TxOut`] referenced by [`txin`].
///
/// [`TxOut`]: bitcoin::TxOut
/// [`txin`]: Self::txin
pub witness_weight: Weight,
}

// We hold various information about HTLC relay in the HTLC objects in Channel itself:
//
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
Expand Down Expand Up @@ -4436,14 +4523,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 @@ -4458,9 +4544,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 @@ -4481,13 +4565,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 @@ -9198,7 +9277,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 @@ -18,12 +18,12 @@ use {
},
crate::ln::channel::PendingV2Channel,
crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint},
crate::ln::channelmanager::FundingTxInput,
crate::ln::functional_test_utils::*,
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 { txin, 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: txin.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
16 changes: 8 additions & 8 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::events::{
};
use crate::ln::chan_utils::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC};
use crate::ln::channelmanager::{
AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, PaymentId,
RAACommitmentOrder, RecipientOnionFields, MIN_CLTV_EXPIRY_DELTA,
AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, FundingTxInput,
PaymentId, RAACommitmentOrder, RecipientOnionFields, MIN_CLTV_EXPIRY_DELTA,
};
use crate::ln::msgs;
use crate::ln::msgs::{
Expand Down Expand Up @@ -1440,7 +1440,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 @@ -1462,17 +1462,17 @@ pub fn create_dual_funding_utxos_with_prev_txs(

let mut inputs = vec![];
for i in 0..utxo_values_in_satoshis.len() {
inputs.push((
TxIn {
inputs.push(FundingTxInput {
txin: 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),
));
prevtx: tx.clone(),
witness_weight: Weight::from_wu(P2WPKH_WITNESS_WEIGHT),
});
}

inputs
Expand Down
Loading
Loading