Skip to content

Commit f23e017

Browse files
committed
Allow passing funding inputs when manually accepting a dual-funded channel
We introduce a `ChannelManager::accept_inbound_channel_with_contribution` method allowing contributing to the overall channel capacity of an inbound dual-funded channel by contributing inputs.
1 parent fff6eaf commit f23e017

File tree

4 files changed

+140
-47
lines changed

4 files changed

+140
-47
lines changed

lightning/src/ln/channel.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5175,7 +5175,7 @@ pub(super) struct DualFundingChannelContext {
51755175
///
51765176
/// Note that this field may be emptied once the interactive negotiation has been started.
51775177
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
5178-
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
5178+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>,
51795179
}
51805180

51815181
// Holder designates channel data owned for the benefit of the user client.
@@ -10400,7 +10400,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1040010400
pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
1040110401
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1040210402
counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
10403-
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, user_id: u128, config: &UserConfig,
10403+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>, user_id: u128, config: &UserConfig,
1040410404
current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
1040510405
logger: L,
1040610406
) -> Result<Self, APIError>
@@ -10538,22 +10538,18 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1053810538

1053910539
/// Creates a new dual-funded channel from a remote side's request for one.
1054010540
/// Assumes chain_hash has already been checked and corresponds with what we expect!
10541-
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
1054210541
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
1054310542
pub fn new_inbound<ES: Deref, F: Deref, L: Deref>(
1054410543
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1054510544
holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
10546-
their_features: &InitFeatures, msg: &msgs::OpenChannelV2,
10547-
user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
10545+
their_features: &InitFeatures, msg: &msgs::OpenChannelV2, user_id: u128, config: &UserConfig,
10546+
current_chain_height: u32, logger: &L, our_funding_satoshis: u64,
10547+
our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>,
1054810548
) -> Result<Self, ChannelError>
1054910549
where ES::Target: EntropySource,
1055010550
F::Target: FeeEstimator,
1055110551
L::Target: Logger,
1055210552
{
10553-
// TODO(dual_funding): Take these as input once supported
10554-
let our_funding_satoshis = 0u64;
10555-
let our_funding_inputs = Vec::new();
10556-
1055710553
let channel_value_satoshis = our_funding_satoshis.saturating_add(msg.common_fields.funding_satoshis);
1055810554
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
1055910555
channel_value_satoshis, msg.common_fields.dust_limit_satoshis);
@@ -10603,7 +10599,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1060310599
context.channel_id = channel_id;
1060410600

1060510601
let dual_funding_context = DualFundingChannelContext {
10606-
our_funding_satoshis: our_funding_satoshis,
10602+
our_funding_satoshis,
1060710603
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
1060810604
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
1060910605
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,

lightning/src/ln/channelmanager.rs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ use bitcoin::hashes::{Hash, HashEngine, HmacEngine};
3030

3131
use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::secp256k1::{PublicKey, SecretKey};
33-
use bitcoin::{secp256k1, Sequence};
34-
#[cfg(splicing)]
35-
use bitcoin::{TxIn, Weight};
33+
use bitcoin::{secp256k1, Sequence, TxIn, Weight};
3634

3735
use crate::blinded_path::message::MessageForwardNode;
3836
use crate::blinded_path::message::{AsyncPaymentsContext, OffersContext};
@@ -122,7 +120,7 @@ use crate::util::errors::APIError;
122120
use crate::util::logger::{Level, Logger, WithContext};
123121
use crate::util::scid_utils::fake_scid;
124122
use crate::util::ser::{
125-
BigSize, FixedLengthReader, LengthReadable, MaybeReadable, Readable, ReadableArgs, VecWriter,
123+
BigSize, FixedLengthReader, LengthReadable, MaybeReadable, Readable, ReadableArgs, TransactionU16LenLimited, VecWriter,
126124
Writeable, Writer,
127125
};
128126
use crate::util::string::UntrustedString;
@@ -8078,7 +8076,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80788076
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
80798077
#[rustfmt::skip]
80808078
pub fn accept_inbound_channel(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>) -> Result<(), APIError> {
8081-
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id, config_overrides)
8079+
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id, config_overrides, 0, vec![])
80828080
}
80838081

80848082
/// Accepts a request to open a channel after a [`events::Event::OpenChannelRequest`], treating
@@ -8101,14 +8099,67 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81018099
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
81028100
#[rustfmt::skip]
81038101
pub fn accept_inbound_channel_from_trusted_peer_0conf(&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>) -> Result<(), APIError> {
8104-
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, true, user_channel_id, config_overrides)
8102+
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, true, user_channel_id, config_overrides, 0, vec![])
8103+
}
8104+
8105+
/// Accepts a request to open a dual-funded channel with a contribution provided by us after an
8106+
/// [`Event::OpenChannelRequest`].
8107+
///
8108+
/// The [`Event::OpenChannelRequest::channel_negotiation_type`] field will indicate the open channel
8109+
/// request is for a dual-funded channel when the variant is `InboundChannelFunds::DualFunded`.
8110+
///
8111+
/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,
8112+
/// and the `counterparty_node_id` parameter is the id of the peer which has requested to open
8113+
/// the channel.
8114+
///
8115+
/// The `user_channel_id` parameter will be provided back in
8116+
/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
8117+
/// with which `accept_inbound_channel_*` call.
8118+
///
8119+
/// The `funding_inputs` parameter provides the `txin`s along with their previous transactions, and
8120+
/// a corresponding witness weight for each input that will be used to contribute towards our
8121+
/// portion of the channel value. Our contribution will be calculated as the total value of these
8122+
/// inputs minus the fees we need to cover for the interactive funding transaction. The witness
8123+
/// weights must correspond to the witnesses you will provide through [`ChannelManager::funding_transaction_signed`]
8124+
/// after receiving [`Event::FundingTransactionReadyForSigning`].
8125+
///
8126+
/// Note that this method will return an error and reject the channel if it requires support for
8127+
/// zero confirmations.
8128+
// TODO(dual_funding): Discussion on complications with 0conf dual-funded channels where "locking"
8129+
// of UTXOs used for funding would be required and other issues.
8130+
// See https://diyhpl.us/~bryan/irc/bitcoin/bitcoin-dev/linuxfoundation-pipermail/lightning-dev/2023-May/003922.txt
8131+
///
8132+
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
8133+
/// [`Event::OpenChannelRequest::channel_negotiation_type`]: events::Event::OpenChannelRequest::channel_negotiation_type
8134+
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
8135+
/// [`Event::FundingTransactionReadyForSigning`]: events::Event::FundingTransactionReadyForSigning
8136+
/// [`ChannelManager::funding_transaction_signed`]: ChannelManager::funding_transaction_signed
8137+
pub fn accept_inbound_channel_with_contribution(
8138+
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128,
8139+
config_overrides: Option<ChannelConfigOverrides>, our_funding_satoshis: u64,
8140+
funding_inputs: Vec<(TxIn, Transaction, Weight)>
8141+
) -> Result<(), APIError> {
8142+
let funding_inputs = Self::length_limit_holder_input_prev_txs(funding_inputs)?;
8143+
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id,
8144+
config_overrides, our_funding_satoshis, funding_inputs)
8145+
}
8146+
8147+
fn length_limit_holder_input_prev_txs(funding_inputs: Vec<(TxIn, Transaction, Weight)>) -> Result<Vec<(TxIn, TransactionU16LenLimited, Weight)>, APIError> {
8148+
funding_inputs.into_iter().map(|(txin, tx, witness_weight)| {
8149+
match TransactionU16LenLimited::new(tx) {
8150+
Ok(tx) => Ok((txin, tx, witness_weight)),
8151+
Err(err) => Err(err)
8152+
}
8153+
}).collect::<Result<Vec<(TxIn, TransactionU16LenLimited, Weight)>, ()>>()
8154+
.map_err(|_| APIError::APIMisuseError { err: "One or more transactions had a serialized length exceeding 65535 bytes".into() })
81058155
}
81068156

81078157
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
81088158
#[rustfmt::skip]
81098159
fn do_accept_inbound_channel(
81108160
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool,
8111-
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>
8161+
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>, our_funding_satoshis: u64,
8162+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>
81128163
) -> Result<(), APIError> {
81138164

81148165
let mut config = self.default_configuration.clone();
@@ -8167,7 +8218,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81678218
&self.channel_type_features(), &peer_state.latest_features,
81688219
&open_channel_msg,
81698220
user_channel_id, &config, best_block_height,
8170-
&self.logger,
8221+
&self.logger, our_funding_satoshis, funding_inputs,
81718222
).map_err(|_| MsgHandleErrInternal::from_chan_no_close(
81728223
ChannelError::Close(
81738224
(
@@ -8451,7 +8502,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84518502
&self.fee_estimator, &self.entropy_source, &self.signer_provider,
84528503
self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(),
84538504
&peer_state.latest_features, msg, user_channel_id,
8454-
&self.default_configuration, best_block_height, &self.logger,
8505+
&self.default_configuration, best_block_height, &self.logger, 0, vec![],
84558506
).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
84568507
let message_send_event = MessageSendEvent::SendAcceptChannelV2 {
84578508
node_id: *counterparty_node_id,

lightning/src/ln/dual_funding_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
5151
&[session.initiator_input_value_satoshis],
5252
)
5353
.into_iter()
54-
.map(|(txin, tx, _)| (txin, TransactionU16LenLimited::new(tx).unwrap()))
54+
.map(|(txin, tx, weight)| (txin, TransactionU16LenLimited::new(tx).unwrap(), weight))
5555
.collect();
5656

5757
// Alice creates a dual-funded channel as initiator.

0 commit comments

Comments
 (0)