Skip to content

Commit 2940545

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 06e3fe8 commit 2940545

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
@@ -5393,7 +5393,7 @@ pub(super) struct DualFundingChannelContext {
53935393
///
53945394
/// Note that this field may be emptied once the interactive negotiation has been started.
53955395
#[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
5396-
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
5396+
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>,
53975397
}
53985398

53995399
// Holder designates channel data owned for the benefit of the user client.
@@ -10462,7 +10462,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1046210462
pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
1046310463
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1046410464
counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
10465-
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, user_id: u128, config: &UserConfig,
10465+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>, user_id: u128, config: &UserConfig,
1046610466
current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
1046710467
logger: L,
1046810468
) -> Result<Self, APIError>
@@ -10600,22 +10600,18 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1060010600

1060110601
/// Creates a new dual-funded channel from a remote side's request for one.
1060210602
/// Assumes chain_hash has already been checked and corresponds with what we expect!
10603-
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
1060410603
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
1060510604
pub fn new_inbound<ES: Deref, F: Deref, L: Deref>(
1060610605
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
1060710606
holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
10608-
their_features: &InitFeatures, msg: &msgs::OpenChannelV2,
10609-
user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
10607+
their_features: &InitFeatures, msg: &msgs::OpenChannelV2, user_id: u128, config: &UserConfig,
10608+
current_chain_height: u32, logger: &L, our_funding_satoshis: u64,
10609+
our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>,
1061010610
) -> Result<Self, ChannelError>
1061110611
where ES::Target: EntropySource,
1061210612
F::Target: FeeEstimator,
1061310613
L::Target: Logger,
1061410614
{
10615-
// TODO(dual_funding): Take these as input once supported
10616-
let our_funding_satoshis = 0u64;
10617-
let our_funding_inputs = Vec::new();
10618-
1061910615
let channel_value_satoshis = our_funding_satoshis.saturating_add(msg.common_fields.funding_satoshis);
1062010616
let counterparty_selected_channel_reserve_satoshis = get_v2_channel_reserve_satoshis(
1062110617
channel_value_satoshis, msg.common_fields.dust_limit_satoshis);
@@ -10665,7 +10661,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1066510661
context.channel_id = channel_id;
1066610662

1066710663
let dual_funding_context = DualFundingChannelContext {
10668-
our_funding_satoshis: our_funding_satoshis,
10664+
our_funding_satoshis,
1066910665
their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
1067010666
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
1067110667
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;
@@ -8180,6 +8178,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81808178
false,
81818179
user_channel_id,
81828180
config_overrides,
8181+
0,
8182+
vec![],
81838183
)
81848184
}
81858185

@@ -8211,14 +8211,69 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82118211
true,
82128212
user_channel_id,
82138213
config_overrides,
8214+
0,
8215+
vec![],
82148216
)
82158217
}
82168218

8219+
/// Accepts a request to open a dual-funded channel with a contribution provided by us after an
8220+
/// [`Event::OpenChannelRequest`].
8221+
///
8222+
/// The [`Event::OpenChannelRequest::channel_negotiation_type`] field will indicate the open channel
8223+
/// request is for a dual-funded channel when the variant is `InboundChannelFunds::DualFunded`.
8224+
///
8225+
/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted,
8226+
/// and the `counterparty_node_id` parameter is the id of the peer which has requested to open
8227+
/// the channel.
8228+
///
8229+
/// The `user_channel_id` parameter will be provided back in
8230+
/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
8231+
/// with which `accept_inbound_channel_*` call.
8232+
///
8233+
/// The `funding_inputs` parameter provides the `txin`s along with their previous transactions, and
8234+
/// a corresponding witness weight for each input that will be used to contribute towards our
8235+
/// portion of the channel value. Our contribution will be calculated as the total value of these
8236+
/// inputs minus the fees we need to cover for the interactive funding transaction. The witness
8237+
/// weights must correspond to the witnesses you will provide through [`ChannelManager::funding_transaction_signed`]
8238+
/// after receiving [`Event::FundingTransactionReadyForSigning`].
8239+
///
8240+
/// Note that this method will return an error and reject the channel if it requires support for
8241+
/// zero confirmations.
8242+
// TODO(dual_funding): Discussion on complications with 0conf dual-funded channels where "locking"
8243+
// of UTXOs used for funding would be required and other issues.
8244+
// See https://diyhpl.us/~bryan/irc/bitcoin/bitcoin-dev/linuxfoundation-pipermail/lightning-dev/2023-May/003922.txt
8245+
///
8246+
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
8247+
/// [`Event::OpenChannelRequest::channel_negotiation_type`]: events::Event::OpenChannelRequest::channel_negotiation_type
8248+
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
8249+
/// [`Event::FundingTransactionReadyForSigning`]: events::Event::FundingTransactionReadyForSigning
8250+
/// [`ChannelManager::funding_transaction_signed`]: ChannelManager::funding_transaction_signed
8251+
pub fn accept_inbound_channel_with_contribution(
8252+
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, user_channel_id: u128,
8253+
config_overrides: Option<ChannelConfigOverrides>, our_funding_satoshis: u64,
8254+
funding_inputs: Vec<(TxIn, Transaction, Weight)>
8255+
) -> Result<(), APIError> {
8256+
let funding_inputs = Self::length_limit_holder_input_prev_txs(funding_inputs)?;
8257+
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id,
8258+
config_overrides, our_funding_satoshis, funding_inputs)
8259+
}
8260+
8261+
fn length_limit_holder_input_prev_txs(funding_inputs: Vec<(TxIn, Transaction, Weight)>) -> Result<Vec<(TxIn, TransactionU16LenLimited, Weight)>, APIError> {
8262+
funding_inputs.into_iter().map(|(txin, tx, witness_weight)| {
8263+
match TransactionU16LenLimited::new(tx) {
8264+
Ok(tx) => Ok((txin, tx, witness_weight)),
8265+
Err(err) => Err(err)
8266+
}
8267+
}).collect::<Result<Vec<(TxIn, TransactionU16LenLimited, Weight)>, ()>>()
8268+
.map_err(|_| APIError::APIMisuseError { err: "One or more transactions had a serialized length exceeding 65535 bytes".into() })
8269+
}
8270+
82178271
/// TODO(dual_funding): Allow contributions, pass intended amount and inputs
82188272
#[rustfmt::skip]
82198273
fn do_accept_inbound_channel(
82208274
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool,
8221-
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>
8275+
user_channel_id: u128, config_overrides: Option<ChannelConfigOverrides>, our_funding_satoshis: u64,
8276+
funding_inputs: Vec<(TxIn, TransactionU16LenLimited, Weight)>
82228277
) -> Result<(), APIError> {
82238278

82248279
let mut config = self.default_configuration.clone();
@@ -8277,7 +8332,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82778332
&self.channel_type_features(), &peer_state.latest_features,
82788333
&open_channel_msg,
82798334
user_channel_id, &config, best_block_height,
8280-
&self.logger,
8335+
&self.logger, our_funding_satoshis, funding_inputs,
82818336
).map_err(|_| MsgHandleErrInternal::from_chan_no_close(
82828337
ChannelError::Close(
82838338
(
@@ -8561,7 +8616,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
85618616
&self.fee_estimator, &self.entropy_source, &self.signer_provider,
85628617
self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(),
85638618
&peer_state.latest_features, msg, user_channel_id,
8564-
&self.default_configuration, best_block_height, &self.logger,
8619+
&self.default_configuration, best_block_height, &self.logger, 0, vec![],
85658620
).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
85668621
let message_send_event = MessageSendEvent::SendAcceptChannelV2 {
85678622
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)