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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
Copy Markdown
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
Copy Markdown
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