-
Notifications
You must be signed in to change notification settings - Fork 418
Add splice-out support #3979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jkczyz
wants to merge
11
commits into
lightningdevkit:main
Choose a base branch
from
jkczyz:2025-07-splice-out
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+694
−382
Open
Add splice-out support #3979
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9bd2144
Check splice contributions against SignedAmount::MAX_MONEY
jkczyz c9ddcbf
Remove their_funding_contribution_satoshis from FundingNegotiationCon…
jkczyz 70929ae
Remove TransactionU16LenLimited
jkczyz 3327382
Include witness weights in FundingNegotiationContext
jkczyz 75b7e80
Account for shared input EMPTY_SCRIPT_SIG_WEIGHT
jkczyz 7c936e2
Replace funding input tuple with struct
jkczyz 3e04691
Use a SpliceContribution enum for passing splice-in params
jkczyz 20b6809
Add splice-out support
jkczyz 604762a
f - ensure splice contribution is non-zero
jkczyz 74e68b0
Support accepting splice-out
jkczyz ef24de4
f - DRY
jkczyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// This file is Copyright its original authors, visible in version control | ||
// history. | ||
// | ||
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE | ||
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. | ||
// You may not use this file except in accordance with one or both of these | ||
// licenses. | ||
|
||
//! Types pertaining to funding channels. | ||
|
||
#[cfg(splicing)] | ||
use bitcoin::{Amount, ScriptBuf, SignedAmount, TxOut}; | ||
use bitcoin::{Script, Sequence, Transaction, Weight}; | ||
|
||
use crate::events::bump_transaction::{Utxo, EMPTY_SCRIPT_SIG_WEIGHT}; | ||
use crate::sign::{P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT}; | ||
|
||
/// The components of a splice's funding transaction that are contributed by one party. | ||
#[cfg(splicing)] | ||
pub enum SpliceContribution { | ||
/// When funds are added to a channel. | ||
SpliceIn { | ||
/// The amount to contribute to the splice. | ||
value: Amount, | ||
|
||
/// The inputs included in the splice's funding transaction to meet the contributed amount. | ||
/// Any excess amount will be sent to a change output. | ||
inputs: Vec<FundingTxInput>, | ||
|
||
/// An optional change output script. This will be used if needed or, when not set, | ||
/// generated using [`SignerProvider::get_destination_script`]. | ||
change_script: Option<ScriptBuf>, | ||
}, | ||
/// When funds are removed from a channel. | ||
SpliceOut { | ||
/// The outputs to include in the splice's funding transaction. The total value of all | ||
/// outputs will be the amount that is removed. | ||
outputs: Vec<TxOut>, | ||
}, | ||
} | ||
|
||
#[cfg(splicing)] | ||
impl SpliceContribution { | ||
pub(super) fn value(&self) -> SignedAmount { | ||
match self { | ||
SpliceContribution::SpliceIn { value, .. } => { | ||
value.to_signed().unwrap_or(SignedAmount::MAX) | ||
}, | ||
SpliceContribution::SpliceOut { outputs } => { | ||
let value_removed = outputs | ||
.iter() | ||
.map(|txout| txout.value) | ||
.sum::<Amount>() | ||
.to_signed() | ||
.unwrap_or(SignedAmount::MAX); | ||
-value_removed | ||
}, | ||
} | ||
} | ||
|
||
pub(super) fn inputs(&self) -> &[FundingTxInput] { | ||
match self { | ||
SpliceContribution::SpliceIn { inputs, .. } => &inputs[..], | ||
SpliceContribution::SpliceOut { .. } => &[], | ||
} | ||
} | ||
|
||
pub(super) fn outputs(&self) -> &[TxOut] { | ||
match self { | ||
SpliceContribution::SpliceIn { .. } => &[], | ||
SpliceContribution::SpliceOut { outputs } => &outputs[..], | ||
} | ||
} | ||
|
||
pub(super) fn into_tx_parts(self) -> (Vec<FundingTxInput>, Vec<TxOut>, Option<ScriptBuf>) { | ||
match self { | ||
SpliceContribution::SpliceIn { inputs, change_script, .. } => { | ||
(inputs, vec![], change_script) | ||
}, | ||
SpliceContribution::SpliceOut { outputs } => (vec![], outputs, None), | ||
} | ||
} | ||
} | ||
|
||
/// An input to contribute to a channel's funding transaction either when using the v2 channel | ||
/// establishment protocol or when splicing. | ||
#[derive(Clone)] | ||
pub struct FundingTxInput { | ||
/// The unspent [`TxOut`] that the input spends. | ||
/// | ||
/// [`TxOut`]: bitcoin::TxOut | ||
pub(super) utxo: Utxo, | ||
|
||
/// The sequence number to use in the [`TxIn`]. | ||
/// | ||
/// [`TxIn`]: bitcoin::TxIn | ||
pub(super) sequence: Sequence, | ||
|
||
/// The transaction containing the unspent [`TxOut`] referenced by [`utxo`]. | ||
/// | ||
/// [`TxOut`]: bitcoin::TxOut | ||
/// [`utxo`]: Self::utxo | ||
pub(super) prevtx: Transaction, | ||
} | ||
|
||
impl FundingTxInput { | ||
fn new<F: FnOnce(&bitcoin::Script) -> bool>( | ||
prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight, | ||
script_filter: F, | ||
) -> Result<Self, ()> { | ||
Ok(FundingTxInput { | ||
utxo: Utxo { | ||
outpoint: bitcoin::OutPoint { txid: prevtx.compute_txid(), vout }, | ||
output: prevtx | ||
.output | ||
.get(vout as usize) | ||
.filter(|output| script_filter(&output.script_pubkey)) | ||
.ok_or(())? | ||
.clone(), | ||
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu(), | ||
}, | ||
sequence, | ||
prevtx, | ||
}) | ||
} | ||
|
||
/// Creates an input spending a P2WPKH output from the given `prevtx` at index `vout` using the | ||
/// provided `sequence` number. | ||
/// | ||
/// Returns `Err` if no such output exists in `prevtx` at index `vout`. | ||
pub fn new_p2wpkh(prevtx: Transaction, vout: u32, sequence: Sequence) -> Result<Self, ()> { | ||
let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT); | ||
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2wpkh) | ||
} | ||
|
||
/// Creates an input spending a P2WSH output from the given `prevtx` at index `vout` using the | ||
/// provided `sequence` number. | ||
/// | ||
/// Requires passing the weight of witness needed to satisfy the output's script. | ||
/// | ||
/// Returns `Err` if no such output exists in `prevtx` at index `vout`. | ||
pub fn new_p2wsh( | ||
prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight, | ||
) -> Result<Self, ()> { | ||
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2wsh) | ||
} | ||
|
||
/// Creates an input spending a P2TR output from the given `prevtx` at index `vout` using the | ||
/// provided `sequence` number. | ||
/// | ||
/// This is meant for inputs spending a taproot output using the key path. See | ||
/// [`new_p2tr_script_spend`] for when spending using a script path. | ||
/// | ||
/// Returns `Err` if no such output exists in `prevtx` at index `vout`. | ||
/// | ||
/// [`new_p2tr_script_spend`]: Self::new_p2tr_script_spend | ||
pub fn new_p2tr_key_spend( | ||
prevtx: Transaction, vout: u32, sequence: Sequence, | ||
) -> Result<Self, ()> { | ||
let witness_weight = Weight::from_wu(P2TR_KEY_PATH_WITNESS_WEIGHT); | ||
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2tr) | ||
} | ||
|
||
/// Creates an input spending a P2TR output from the given `prevtx` at index `vout` using the | ||
/// provided `sequence` number. | ||
/// | ||
/// Requires passing the weight of witness needed to satisfy a script path of the taproot | ||
/// output. See [`new_p2tr_key_spend`] for when spending using the key path. | ||
/// | ||
/// Returns `Err` if no such output exists in `prevtx` at index `vout`. | ||
/// | ||
/// [`new_p2tr_key_spend`]: Self::new_p2tr_key_spend | ||
pub fn new_p2tr_script_spend( | ||
prevtx: Transaction, vout: u32, sequence: Sequence, witness_weight: Weight, | ||
) -> Result<Self, ()> { | ||
FundingTxInput::new(prevtx, vout, sequence, witness_weight, Script::is_p2tr) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
sequence
could be made optional with a default value ofENABLE_RBF_NO_LOCKTIME
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about always setting it to
ENABLE_RBF_NO_LOCKTIME
and providing a mutator to override it? Would be nice to avoid passing anOption
here if possible.