-
Notifications
You must be signed in to change notification settings - Fork 416
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
Merged
Merged
Add splice-out support #3979
Changes from all commits
Commits
Show all changes
9 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 bdb8d5d
Replace funding input tuple with struct
jkczyz ae58a4f
Use a SpliceContribution enum for passing splice-in params
jkczyz 3245ebf
Add splice-out support
jkczyz ce203f2
Support accepting splice-out
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,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. | ||
/// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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.
"to meet the contributed amount plus the fees" ?