Skip to content
Merged
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
533 changes: 353 additions & 180 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
31 changes: 10 additions & 21 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 @@ -61,13 +62,11 @@ use bitcoin::pow::CompactTarget;
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::transaction::{Transaction, TxIn, TxOut};
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).unwrap())
.collect()
}

pub fn sign_funding_transaction<'a, 'b, 'c>(
Expand Down
208 changes: 208 additions & 0 deletions lightning/src/ln/funding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"to meet the contributed amount plus the fees" ?

/// 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.
Comment on lines +37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We will remove the total value of the outputs plus the transaction fees" ?

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, 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: Sequence::ENABLE_RBF_NO_LOCKTIME,
prevtx,
})
}

/// Creates an input spending a P2WPKH output from the given `prevtx` at index `vout`.
///
/// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden
/// by [`set_sequence`].
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
///
/// [`TxIn::sequence`]: bitcoin::TxIn::sequence
/// [`set_sequence`]: Self::set_sequence
pub fn new_p2wpkh(prevtx: Transaction, vout: u32) -> Result<Self, ()> {
let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT);
FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2wpkh)
}

/// Creates an input spending a P2WSH output from the given `prevtx` at index `vout`.
///
/// Requires passing the weight of witness needed to satisfy the output's script.
///
/// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden
/// by [`set_sequence`].
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
///
/// [`TxIn::sequence`]: bitcoin::TxIn::sequence
/// [`set_sequence`]: Self::set_sequence
pub fn new_p2wsh(prevtx: Transaction, vout: u32, witness_weight: Weight) -> Result<Self, ()> {
FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2wsh)
}

/// Creates an input spending a P2TR output from the given `prevtx` at index `vout`.
///
/// 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.
///
/// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden
/// by [`set_sequence`].
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
///
/// [`new_p2tr_script_spend`]: Self::new_p2tr_script_spend
///
/// [`TxIn::sequence`]: bitcoin::TxIn::sequence
/// [`set_sequence`]: Self::set_sequence
pub fn new_p2tr_key_spend(prevtx: Transaction, vout: u32) -> Result<Self, ()> {
let witness_weight = Weight::from_wu(P2TR_KEY_PATH_WITNESS_WEIGHT);
FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2tr)
}

/// Creates an input spending a P2TR output from the given `prevtx` at index `vout`.
///
/// 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.
///
/// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden
/// by [`set_sequence`].
///
/// Returns `Err` if no such output exists in `prevtx` at index `vout`.
///
/// [`new_p2tr_key_spend`]: Self::new_p2tr_key_spend
///
/// [`TxIn::sequence`]: bitcoin::TxIn::sequence
/// [`set_sequence`]: Self::set_sequence
pub fn new_p2tr_script_spend(
prevtx: Transaction, vout: u32, witness_weight: Weight,
) -> Result<Self, ()> {
FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2tr)
}

/// The sequence number to use in the [`TxIn`].
///
/// [`TxIn`]: bitcoin::TxIn
pub fn sequence(&self) -> Sequence {
self.sequence
}

/// Sets the sequence number to use in the [`TxIn`].
///
/// [`TxIn`]: bitcoin::TxIn
pub fn set_sequence(&mut self, sequence: Sequence) {
self.sequence = sequence;
}
}
Loading
Loading