Skip to content

Commit 0daad7d

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 f97be8d commit 0daad7d

File tree

4 files changed

+142
-45
lines changed

4 files changed

+142
-45
lines changed

lightning/src/ln/channel.rs

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

56205620
// Holder designates channel data owned for the benefit of the user client.
@@ -10906,7 +10906,7 @@ where
1090610906
pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
1090710907
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1090810908
counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
10909-
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, user_id: u128, config: &UserConfig,
10909+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>, user_id: u128, config: &UserConfig,
1091010910
current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
1091110911
logger: L,
1091210912
) -> Result<Self, APIError>
@@ -11048,23 +11048,19 @@ where
1104811048

1104911049
/// Creates a new dual-funded channel from a remote side's request for one.
1105011050
/// Assumes chain_hash has already been checked and corresponds with what we expect!
11051-
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
1105211051
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
1105311052
#[rustfmt::skip]
1105411053
pub fn new_inbound<ES: Deref, F: Deref, L: Deref>(
1105511054
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1105611055
holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
11057-
their_features: &InitFeatures, msg: &msgs::OpenChannelV2,
11058-
user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
11056+
their_features: &InitFeatures, msg: &msgs::OpenChannelV2, user_id: u128, config: &UserConfig,
11057+
current_chain_height: u32, logger: &L, our_funding_satoshis: u64,
11058+
our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>,
1105911059
) -> Result<Self, ChannelError>
1106011060
where ES::Target: EntropySource,
1106111061
F::Target: FeeEstimator,
1106211062
L::Target: Logger,
1106311063
{
11064-
// TODO(dual_funding): Take these as input once supported
11065-
let our_funding_satoshis = 0u64;
11066-
let our_funding_inputs = Vec::new();
11067-
1106811064
let channel_value_satoshis = our_funding_satoshis.saturating_add(msg.common_fields.funding_satoshis);
1106911065
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
1107011066
channel_value_satoshis, msg.common_fields.dust_limit_satoshis);
@@ -11114,7 +11110,7 @@ where
1111411110
context.channel_id = channel_id;
1111511111

1111611112
let dual_funding_context = DualFundingChannelContext {
11117-
our_funding_satoshis: our_funding_satoshis,
11113+
our_funding_satoshis,
1111811114
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
1111911115
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
1112011116
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,

lightning/src/ln/channelmanager.rs

Lines changed: 62 additions & 7 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;
@@ -8273,6 +8271,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82738271
false,
82748272
user_channel_id,
82758273
config_overrides,
8274+
0,
8275+
vec![],
82768276
)
82778277
}
82788278

@@ -8304,14 +8304,69 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83048304
true,
83058305
user_channel_id,
83068306
config_overrides,
8307+
0,
8308+
vec![],
83078309
)
83088310
}
83098311

8312+
/// Accepts a request to open a dual-funded channel with a contribution provided by us after an
8313+
/// [`Event::OpenChannelRequest`].
8314+
///
8315+
/// The [`Event::OpenChannelRequest::channel_negotiation_type`] field will indicate the open channel
8316+
/// request is for a dual-funded channel when the variant is `InboundChannelFunds::DualFunded`.
8317+
///
8318+
/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,
8319+
/// and the `counterparty_node_id` parameter is the id of the peer which has requested to open
8320+
/// the channel.
8321+
///
8322+
/// The `user_channel_id` parameter will be provided back in
8323+
/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
8324+
/// with which `accept_inbound_channel_*` call.
8325+
///
8326+
/// The `funding_inputs` parameter provides the `txin`s along with their previous transactions, and
8327+
/// a corresponding witness weight for each input that will be used to contribute towards our
8328+
/// portion of the channel value. Our contribution will be calculated as the total value of these
8329+
/// inputs minus the fees we need to cover for the interactive funding transaction. The witness
8330+
/// weights must correspond to the witnesses you will provide through [`ChannelManager::funding_transaction_signed`]
8331+
/// after receiving [`Event::FundingTransactionReadyForSigning`].
8332+
///
8333+
/// Note that this method will return an error and reject the channel if it requires support for
8334+
/// zero confirmations.
8335+
// TODO(dual_funding): Discussion on complications with 0conf dual-funded channels where "locking"
8336+
// of UTXOs used for funding would be required and other issues.
8337+
// See https://diyhpl.us/~bryan/irc/bitcoin/bitcoin-dev/linuxfoundation-pipermail/lightning-dev/2023-May/003922.txt
8338+
///
8339+
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
8340+
/// [`Event::OpenChannelRequest::channel_negotiation_type`]: events::Event::OpenChannelRequest::channel_negotiation_type
8341+
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
8342+
/// [`Event::FundingTransactionReadyForSigning`]: events::Event::FundingTransactionReadyForSigning
8343+
/// [`ChannelManager::funding_transaction_signed`]: ChannelManager::funding_transaction_signed
8344+
pub fn accept_inbound_channel_with_contribution(
8345+
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128,
8346+
config_overrides: Option<ChannelConfigOverrides>, our_funding_satoshis: u64,
8347+
funding_inputs: Vec<(TxIn, Transaction, Weight)>
8348+
) -> Result<(), APIError> {
8349+
let funding_inputs = Self::length_limit_holder_input_prev_txs(funding_inputs)?;
8350+
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id,
8351+
config_overrides, our_funding_satoshis, funding_inputs)
8352+
}
8353+
8354+
fn length_limit_holder_input_prev_txs(funding_inputs: Vec<(TxIn, Transaction, Weight)>) -> Result<Vec<(TxIn, TransactionU16LenLimited, Weight)>, APIError> {
8355+
funding_inputs.into_iter().map(|(txin, tx, witness_weight)| {
8356+
match TransactionU16LenLimited::new(tx) {
8357+
Ok(tx) => Ok((txin, tx, witness_weight)),
8358+
Err(err) => Err(err)
8359+
}
8360+
}).collect::<Result<Vec<(TxIn, TransactionU16LenLimited, Weight)>, ()>>()
8361+
.map_err(|_| APIError::APIMisuseError { err: "One or more transactions had a serialized length exceeding 65535 bytes".into() })
8362+
}
8363+
83108364
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
83118365
#[rustfmt::skip]
83128366
fn do_accept_inbound_channel(
83138367
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool,
8314-
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>
8368+
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>, our_funding_satoshis: u64,
8369+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>
83158370
) -> Result<(), APIError> {
83168371

83178372
let mut config = self.default_configuration.clone();
@@ -8370,7 +8425,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83708425
&self.channel_type_features(), &peer_state.latest_features,
83718426
&open_channel_msg,
83728427
user_channel_id, &config, best_block_height,
8373-
&self.logger,
8428+
&self.logger, our_funding_satoshis, funding_inputs,
83748429
).map_err(|_| MsgHandleErrInternal::from_chan_no_close(
83758430
ChannelError::Close(
83768431
(
@@ -8657,7 +8712,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
86578712
&self.fee_estimator, &self.entropy_source, &self.signer_provider,
86588713
self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(),
86598714
&peer_state.latest_features, msg, user_channel_id,
8660-
&self.default_configuration, best_block_height, &self.logger,
8715+
&self.default_configuration, best_block_height, &self.logger, 0, vec![],
86618716
).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
86628717
let message_send_event = MessageSendEvent::SendAcceptChannelV2 {
86638718
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)