Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 43 additions & 34 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13946,7 +13946,7 @@ where
pub funding: FundingScope,
pub context: ChannelContext<SP>,
pub unfunded_context: UnfundedChannelContext,
pub funding_negotiation_context: FundingNegotiationContext,
pub funding_negotiation_context: Option<FundingNegotiationContext>,
/// The current interactive transaction construction session under negotiation.
pub interactive_tx_constructor: Option<InteractiveTxConstructor>,
}
Expand Down Expand Up @@ -14021,7 +14021,7 @@ where
funding,
context,
unfunded_context,
funding_negotiation_context,
funding_negotiation_context: Some(funding_negotiation_context),
interactive_tx_constructor: None,
};
Ok(chan)
Expand Down Expand Up @@ -14096,29 +14096,28 @@ where
},
funding_feerate_sat_per_1000_weight: self.context.feerate_per_kw,
second_per_commitment_point,
locktime: self.funding_negotiation_context.funding_tx_locktime.to_consensus_u32(),
locktime: self.funding_tx_locktime().to_consensus_u32(),
require_confirmed_inputs: None,
}
}

/// 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: Amount,
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();
debug_assert!(our_funding_contribution <= Amount::MAX_MONEY);
let our_funding_contribution_sats = our_funding_contribution.to_sat();

let channel_value_satoshis =
our_funding_contribution_sats.saturating_add(msg.common_fields.funding_satoshis);
Expand Down Expand Up @@ -14163,37 +14162,30 @@ where

let funding_negotiation_context = FundingNegotiationContext {
is_initiator: false,
our_funding_contribution,
our_funding_contribution: our_funding_contribution
.to_signed()
.expect("our_funding_contribution should not be greater than Amount::MAX_MONEY"),
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
shared_funding_input: None,
our_funding_inputs: our_funding_inputs.clone(),
our_funding_outputs: Vec::new(),
change_script: None,
};
let shared_funding_output = TxOut {
value: Amount::from_sat(funding.get_value_satoshis()),
script_pubkey: funding.get_funding_redeemscript().to_p2wsh(),
};

let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
InteractiveTxConstructorArgs {
let mut interactive_tx_constructor = funding_negotiation_context
.into_interactive_tx_constructor(
&context,
&funding,
signer_provider,
entropy_source,
holder_node_id,
counterparty_node_id,
channel_id: context.channel_id,
feerate_sat_per_kw: funding_negotiation_context.funding_feerate_sat_per_1000_weight,
funding_tx_locktime: funding_negotiation_context.funding_tx_locktime,
is_initiator: false,
inputs_to_contribute: our_funding_inputs,
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(),
}
).map_err(|err| {
let reason = ClosureReason::ProcessingError { err: err.reason.to_string() };
ChannelError::Close((err.reason.to_string(), reason))
})?);
)
.map_err(|err| {
let reason = ClosureReason::ProcessingError { err: err.reason.to_string() };
ChannelError::Close((err.reason.to_string(), reason))
})?;
debug_assert!(interactive_tx_constructor.take_initiator_first_message().is_none());

let unfunded_context = UnfundedChannelContext {
unfunded_channel_age_ticks: 0,
Expand All @@ -14202,8 +14194,8 @@ where
Ok(Self {
funding,
context,
funding_negotiation_context,
interactive_tx_constructor,
funding_negotiation_context: None,
interactive_tx_constructor: Some(interactive_tx_constructor),
Comment on lines +14216 to +14217
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.

Would be nice to reuse FundingNegotiation here, but it seems unclear whether the last AwaitingSignatures state can be adapted to the initial dual funding state

unfunded_context,
})
}
Expand Down Expand Up @@ -14267,8 +14259,7 @@ where
}),
channel_type: Some(self.funding.get_channel_type().clone()),
},
funding_satoshis: self.funding_negotiation_context.our_funding_contribution.to_sat()
as u64,
funding_satoshis: self.our_funding_contribution().to_sat(),
second_per_commitment_point,
require_confirmed_inputs: None,
}
Expand All @@ -14283,6 +14274,24 @@ where
pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
self.generate_accept_channel_v2_message()
}

pub fn our_funding_contribution(&self) -> Amount {
Amount::from_sat(self.funding.value_to_self_msat / 1000)
}

pub fn funding_tx_locktime(&self) -> LockTime {
self.funding_negotiation_context
.as_ref()
.map(|context| context.funding_tx_locktime)
.or_else(|| {
self.interactive_tx_constructor
.as_ref()
.map(|constructor| constructor.funding_tx_locktime())
})
.expect(
"either funding_negotiation_context or interactive_tx_constructor should be set",
)
}
}

// Unfunded channel utilities
Expand Down
72 changes: 64 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use bitcoin::hashes::{Hash, HashEngine, HmacEngine};

use bitcoin::secp256k1::Secp256k1;
use bitcoin::secp256k1::{PublicKey, SecretKey};
use bitcoin::{secp256k1, Sequence, SignedAmount};
use bitcoin::{secp256k1, Amount, Sequence};

use crate::blinded_path::message::{
AsyncPaymentsContext, BlindedMessagePath, MessageForwardNode, OffersContext,
Expand Down Expand Up @@ -65,7 +65,7 @@ use crate::ln::channel::{
WithChannelContext,
};
use crate::ln::channel_state::ChannelDetails;
use crate::ln::funding::SpliceContribution;
use crate::ln::funding::{FundingTxInput, SpliceContribution};
use crate::ln::inbound_payment;
use crate::ln::interactivetxs::InteractiveTxMessageSend;
use crate::ln::msgs;
Expand Down Expand Up @@ -9814,6 +9814,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
false,
user_channel_id,
config_overrides,
Amount::ZERO,
vec![],
)
}

Expand Down Expand Up @@ -9845,15 +9847,69 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
true,
user_channel_id,
config_overrides,
Amount::ZERO,
vec![],
)
}

/// Accepts a request to open a dual-funded channel with a contribution provided after an
/// [`Event::OpenChannelRequest`].
///
/// The [`Event::OpenChannelRequest::channel_negotiation_type`] field will indicate the open channel
/// request is for a dual-funded channel when the variant is [`InboundChannelFunds::DualFunded`].
///
/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,
/// and the `counterparty_node_id` parameter is the id of the peer that has requested to open
/// the channel.
///
/// The `user_channel_id` parameter will be provided back in
/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
/// with which `accept_inbound_channel_*` call.
///
/// The `funding_inputs` parameter provides which UTXOs to use for `our_funding_contribution`
/// along with the corresponding satisfaction weight. They must be able to cover any fees needed
/// to pay for the contributed weight to the funding transaction. This includes the witnesses
/// provided through calling [`ChannelManager::funding_transaction_signed`] after receiving
/// [`Event::FundingTransactionReadyForSigning`].
///
/// Note that this method will return an error and reject the channel if it requires support for
/// zero confirmations.
// TODO(dual_funding): Discussion on complications with 0conf dual-funded channels where "locking"
// of UTXOs used for funding would be required and other issues.
// See https://diyhpl.us/~bryan/irc/bitcoin/bitcoin-dev/linuxfoundation-pipermail/lightning-dev/2023-May/003922.txt
///
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
/// [`Event::OpenChannelRequest::channel_negotiation_type`]: events::Event::OpenChannelRequest::channel_negotiation_type
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
/// [`Event::FundingTransactionReadyForSigning`]: events::Event::FundingTransactionReadyForSigning
/// [`ChannelManager::funding_transaction_signed`]: ChannelManager::funding_transaction_signed
pub fn accept_inbound_channel_with_contribution(
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey,
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>,
our_funding_contribution: Amount, funding_inputs: Vec<FundingTxInput>,
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're also allowed to include outputs, behaving similar to the mixed mode splice, so maybe it's worth waiting until we figure that out in #4261 so we can adopt it here?

) -> Result<(), APIError> {
self.do_accept_inbound_channel(
temporary_channel_id,
counterparty_node_id,
false,
user_channel_id,
config_overrides,
our_funding_contribution,
funding_inputs,
)
}

/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
#[rustfmt::skip]
fn do_accept_inbound_channel(
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool,
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey,
accept_0conf: bool, user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>,
our_funding_contribution: Amount, funding_inputs: Vec<FundingTxInput>
) -> Result<(), APIError> {
if our_funding_contribution > Amount::MAX_MONEY {
return Err(APIError::APIMisuseError {
err: format!("the funding contribution must be smaller than the total bitcoin supply, it was {}", our_funding_contribution)
});
}

let mut config = self.config.read().unwrap().clone();

Expand Down Expand Up @@ -9911,7 +9967,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
&self.channel_type_features(), &peer_state.latest_features,
&open_channel_msg,
user_channel_id, &config, best_block_height,
&self.logger,
&self.logger, our_funding_contribution, funding_inputs,
).map_err(|e| {
let channel_id = open_channel_msg.common_fields.temporary_channel_id;
MsgHandleErrInternal::from_chan_no_close(e, channel_id)
Expand Down Expand Up @@ -10055,7 +10111,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 > SignedAmount::ZERO {
if unfunded_chan.our_funding_contribution() > Amount::ZERO {
continue;
}
}
Expand Down Expand Up @@ -10197,7 +10253,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
&self.fee_estimator, &self.entropy_source, &self.signer_provider,
self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(),
&peer_state.latest_features, msg, user_channel_id,
&self.config.read().unwrap(), best_block_height, &self.logger,
&self.config.read().unwrap(), best_block_height, &self.logger, Amount::ZERO, vec![],
).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
let message_send_event = MessageSendEvent::SendAcceptChannelV2 {
node_id: *counterparty_node_id,
Expand Down
11 changes: 11 additions & 0 deletions lightning/src/ln/interactivetxs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,7 @@ pub(super) struct InteractiveTxConstructor {
is_initiator: bool,
initiator_first_message: Option<InteractiveTxMessageSend>,
channel_id: ChannelId,
funding_tx_locktime: AbsoluteLockTime,
inputs_to_contribute: Vec<(SerialId, InputOwned)>,
outputs_to_contribute: Vec<(SerialId, OutputOwned)>,
next_input_index: Option<usize>,
Expand Down Expand Up @@ -2112,6 +2113,7 @@ impl InteractiveTxConstructor {
is_initiator,
initiator_first_message: None,
channel_id,
funding_tx_locktime,
inputs_to_contribute,
outputs_to_contribute,
next_input_index,
Expand All @@ -2131,6 +2133,10 @@ impl InteractiveTxConstructor {
Ok(constructor)
}

pub(super) fn funding_tx_locktime(&self) -> AbsoluteLockTime {
self.funding_tx_locktime
}

fn into_negotiation_error(self, reason: AbortReason) -> NegotiationError {
let (contributed_inputs, contributed_outputs) = self.into_contributed_inputs_and_outputs();
NegotiationError { reason, contributed_inputs, contributed_outputs }
Expand Down Expand Up @@ -2320,6 +2326,11 @@ impl InteractiveTxConstructor {
/// given inputs and outputs, and intended contribution. Takes into account the fees and the dust
/// limit.
///
/// Note that since the type of change output cannot be determined at this point, this calculation
/// does not account for the weight contributed by the change output itself. The fees for the
/// weight of this change output should be subtracted from the result of this function call to get
/// the final amount for the change output (if above dust).
///
/// Three outcomes are possible:
/// - Inputs are sufficient for intended contribution, fees, and a larger-than-dust change:
/// `Ok(Some(change_amount))`
Expand Down