-
Notifications
You must be signed in to change notification settings - Fork 417
Contribute funding inputs on accepting dual-funded channel #3735
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
dunxen
wants to merge
2
commits into
lightningdevkit:main
Choose a base branch
from
dunxen:2025-04-contributeinputs
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.
+437
−105
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -14,7 +14,7 @@ use bitcoin::constants::ChainHash; | |
use bitcoin::script::{Builder, Script, ScriptBuf, WScriptHash}; | ||
use bitcoin::sighash::EcdsaSighashType; | ||
use bitcoin::transaction::{Transaction, TxOut}; | ||
use bitcoin::Witness; | ||
use bitcoin::{Weight, Witness}; | ||
|
||
use bitcoin::hash_types::{BlockHash, Txid}; | ||
use bitcoin::hashes::sha256::Hash as Sha256; | ||
|
@@ -59,14 +59,13 @@ use crate::ln::channelmanager::{ | |
use crate::ln::funding::FundingTxInput; | ||
#[cfg(splicing)] | ||
use crate::ln::funding::SpliceContribution; | ||
#[cfg(splicing)] | ||
use crate::ln::interactivetxs::{ | ||
calculate_change_output_value, AbortReason, InteractiveTxMessageSend, | ||
}; | ||
use crate::ln::interactivetxs::{ | ||
get_output_weight, InteractiveTxConstructor, InteractiveTxConstructorArgs, | ||
InteractiveTxSigningSession, SharedOwnedInput, SharedOwnedOutput, TX_COMMON_FIELDS_WEIGHT, | ||
calculate_change_output_value, get_output_weight, InteractiveTxConstructor, | ||
InteractiveTxConstructorArgs, InteractiveTxSigningSession, SharedOwnedInput, SharedOwnedOutput, | ||
TX_COMMON_FIELDS_WEIGHT, | ||
}; | ||
#[cfg(splicing)] | ||
use crate::ln::interactivetxs::{AbortReason, InteractiveTxMessageSend}; | ||
use crate::ln::msgs; | ||
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError, OnionErrorPacket}; | ||
use crate::ln::onion_utils::{ | ||
|
@@ -6353,7 +6352,11 @@ impl FundingNegotiationContext { | |
.our_funding_inputs | ||
.into_iter() | ||
.map(|FundingTxInput { utxo, sequence, prevtx }| { | ||
(TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() }, prevtx) | ||
( | ||
TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() }, | ||
prevtx, | ||
Weight::from_wu(utxo.satisfaction_weight), | ||
) | ||
}) | ||
.collect(); | ||
|
||
|
@@ -10116,7 +10119,7 @@ where | |
#[rustfmt::skip] | ||
pub fn is_awaiting_initial_mon_persist(&self) -> bool { | ||
if !self.is_awaiting_monitor_update() { return false; } | ||
if matches!( | ||
if self.context.channel_state.is_interactive_signing() || matches!( | ||
self.context.channel_state, ChannelState::AwaitingChannelReady(flags) | ||
if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty() | ||
) { | ||
|
@@ -13052,22 +13055,22 @@ where | |
|
||
/// Creates a new dual-funded channel from a remote side's request for one. | ||
/// Assumes chain_hash has already been checked and corresponds with what we expect! | ||
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs | ||
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled. | ||
#[rustfmt::skip] | ||
pub fn new_inbound<ES: Deref, F: Deref, L: Deref>( | ||
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP, | ||
holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures, | ||
their_features: &InitFeatures, msg: &msgs::OpenChannelV2, | ||
user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L, | ||
their_features: &InitFeatures, msg: &msgs::OpenChannelV2, user_id: u128, config: &UserConfig, | ||
current_chain_height: u32, logger: &L, our_funding_contribution_sats: u64, | ||
our_funding_inputs: Vec<FundingTxInput>, | ||
) -> Result<Self, ChannelError> | ||
where ES::Target: EntropySource, | ||
F::Target: FeeEstimator, | ||
L::Target: Logger, | ||
{ | ||
// TODO(dual_funding): Take these as input once supported | ||
let (our_funding_contribution, our_funding_contribution_sats) = (SignedAmount::ZERO, 0u64); | ||
let our_funding_inputs = Vec::new(); | ||
// This u64 -> i64 cast is safe as `our_funding_contribution_sats` is bounded above by the total | ||
// bitcoin supply, which is far less than i64::MAX. | ||
let our_funding_contribution = SignedAmount::from_sat(our_funding_contribution_sats as i64); | ||
|
||
let channel_value_satoshis = | ||
our_funding_contribution_sats.saturating_add(msg.common_fields.funding_satoshis); | ||
|
@@ -13127,10 +13130,34 @@ where | |
let inputs_to_contribute = our_funding_inputs | ||
.into_iter() | ||
.map(|FundingTxInput { utxo, sequence, prevtx }| { | ||
(TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() }, prevtx) | ||
(TxIn { previous_output: utxo.outpoint, sequence, ..Default::default() }, prevtx, Weight::from_wu(utxo.satisfaction_weight)) | ||
}) | ||
.collect(); | ||
|
||
// Optionally add change output | ||
let change_script = signer_provider.get_destination_script(context.channel_keys_id) | ||
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. Maybe let the user optionally provide a change address along with their inputs? |
||
.map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?; | ||
let change_value_opt = if our_funding_contribution > SignedAmount::ZERO { | ||
calculate_change_output_value( | ||
&funding_negotiation_context, false, &shared_funding_output.script_pubkey, context.holder_dust_limit_satoshis).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))? } else { | ||
None | ||
}; | ||
let mut our_funding_outputs = vec![]; | ||
if let Some(change_value) = change_value_opt { | ||
let mut change_output = TxOut { | ||
value: Amount::from_sat(change_value), | ||
script_pubkey: change_script, | ||
}; | ||
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu(); | ||
let change_output_fee = fee_for_weight(funding_negotiation_context.funding_feerate_sat_per_1000_weight, change_output_weight); | ||
let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee); | ||
// Check dust limit again | ||
if change_value_decreased_with_fee > context.holder_dust_limit_satoshis { | ||
change_output.value = Amount::from_sat(change_value_decreased_with_fee); | ||
our_funding_outputs.push(change_output); | ||
} | ||
} | ||
|
||
let interactive_tx_constructor = Some(InteractiveTxConstructor::new( | ||
InteractiveTxConstructorArgs { | ||
entropy_source, | ||
|
@@ -13143,7 +13170,7 @@ where | |
inputs_to_contribute, | ||
shared_funding_input: None, | ||
shared_funding_output: SharedOwnedOutput::new(shared_funding_output, our_funding_contribution_sats), | ||
outputs_to_contribute: funding_negotiation_context.our_funding_outputs.clone(), | ||
outputs_to_contribute: our_funding_outputs, | ||
} | ||
).map_err(|err| { | ||
let reason = ClosureReason::ProcessingError { err: err.to_string() }; | ||
|
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
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.
Wouldn't this already be covered by
begin_interactive_funding_tx_construction
?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.
Oh indeed.
begin_interactive_funding_tx_construction
was modified to include that in a previous pre-splicing work. Thanks.