Skip to content

Commit

Permalink
Merge pull request #3485 from dunxen/2024-12-cfgflagdualfunding
Browse files Browse the repository at this point in the history
Reintroduce cfg(dual_funding) for handling of open_channel2 messages
  • Loading branch information
TheBlueMatt authored Dec 16, 2024
2 parents c55f548 + 76608f7 commit f53a09d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ check-cfg = [
"cfg(require_route_graph_test)",
"cfg(splicing)",
"cfg(async_payments)",
"cfg(dual_funding)",
]
9 changes: 8 additions & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
UnfundedInboundV1(InboundV1Channel<SP>),
#[allow(dead_code)] // TODO(dual_funding): Remove once creating V2 channels is enabled.
UnfundedOutboundV2(OutboundV2Channel<SP>),
#[allow(dead_code)] // TODO(dual_funding): Remove once accepting V2 channels is enabled.
UnfundedInboundV2(InboundV2Channel<SP>),
Funded(Channel<SP>),
}
Expand Down Expand Up @@ -4089,7 +4090,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
})
}

#[cfg(test)]
#[cfg(all(test, dual_funding))]
pub fn get_initial_counterparty_commitment_signature_for_test<L: Deref>(
&mut self, logger: &L, channel_transaction_parameters: ChannelTransactionParameters,
counterparty_cur_commitment_point_override: PublicKey,
Expand Down Expand Up @@ -4159,6 +4160,7 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
}

#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
pub(super) fn calculate_our_funding_satoshis(
is_initiator: bool, funding_inputs: &[(TxIn, TransactionU16LenLimited)],
total_witness_weight: Weight, funding_feerate_sat_per_1000_weight: u32,
Expand Down Expand Up @@ -4208,6 +4210,7 @@ pub(super) struct DualFundingChannelContext {
/// to the current block height to align incentives against fee-sniping.
pub funding_tx_locktime: LockTime,
/// The feerate set by the initiator to be used for the funding transaction.
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
pub funding_feerate_sat_per_1000_weight: u32,
/// The funding inputs we will be contributing to the channel.
///
Expand Down Expand Up @@ -8292,6 +8295,7 @@ pub(super) struct OutboundV1Channel<SP: Deref> where SP::Target: SignerProvider
}

impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
#[allow(dead_code)] // TODO(dual_funding): Remove once opending V2 channels is enabled.
pub fn new<ES: Deref, F: Deref, L: Deref>(
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP, counterparty_node_id: PublicKey, their_features: &InitFeatures,
channel_value_satoshis: u64, push_msat: u64, user_id: u128, config: &UserConfig, current_chain_height: u32,
Expand Down Expand Up @@ -9016,6 +9020,7 @@ pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
/// 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!
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
pub fn new<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,
Expand Down Expand Up @@ -9124,6 +9129,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
/// should be sent back to the counterparty node.
///
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
pub fn accept_inbound_dual_funded_channel(&self) -> msgs::AcceptChannelV2 {
if self.context.is_outbound() {
debug_assert!(false, "Tried to send accept_channel for an outbound channel?");
Expand All @@ -9146,6 +9152,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
/// use [`InboundV1Channel::accept_inbound_channel`] instead.
///
/// [`msgs::AcceptChannelV2`]: crate::ln::msgs::AcceptChannelV2
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
fn generate_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
let first_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(
self.unfunded_context.transaction_number(), &self.context.secp_ctx)
Expand Down
23 changes: 19 additions & 4 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ use crate::events::{self, Event, EventHandler, EventsProvider, InboundChannelFun
use crate::ln::inbound_payment;
use crate::ln::types::ChannelId;
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
use crate::ln::channel::{self, Channel, ChannelPhase, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext, InboundV2Channel, InteractivelyFunded as _};
use crate::ln::channel::{self, Channel, ChannelPhase, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext, InteractivelyFunded as _};
#[cfg(any(dual_funding, splicing))]
use crate::ln::channel::InboundV2Channel;
use crate::ln::channel_state::ChannelDetails;
use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
#[cfg(any(feature = "_test_utils", test))]
Expand Down Expand Up @@ -1391,11 +1393,13 @@ impl <SP: Deref> PeerState<SP> where SP::Target: SignerProvider {
#[derive(Clone)]
pub(super) enum OpenChannelMessage {
V1(msgs::OpenChannel),
#[cfg(dual_funding)]
V2(msgs::OpenChannelV2),
}

pub(super) enum OpenChannelMessageRef<'a> {
V1(&'a msgs::OpenChannel),
#[cfg(dual_funding)]
V2(&'a msgs::OpenChannelV2),
}

Expand Down Expand Up @@ -7651,8 +7655,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/

fn do_accept_inbound_channel(
&self, temporary_channel_id: &ChannelId, counterparty_node_id: &PublicKey, accept_0conf: bool,
user_channel_id: u128, funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
total_witness_weight: Weight,
user_channel_id: u128, _funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
_total_witness_weight: Weight,
) -> Result<(), APIError> {
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), Some(*temporary_channel_id), None);
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
Expand Down Expand Up @@ -7696,10 +7700,11 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
(*temporary_channel_id, ChannelPhase::UnfundedInboundV1(channel), message_send_event)
})
},
#[cfg(dual_funding)]
OpenChannelMessage::V2(open_channel_msg) => {
InboundV2Channel::new(&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,
&open_channel_msg, funding_inputs, total_witness_weight, user_channel_id,
&open_channel_msg, _funding_inputs, _total_witness_weight, user_channel_id,
&self.default_configuration, best_block_height, &self.logger
).map_err(|_| MsgHandleErrInternal::from_chan_no_close(
ChannelError::Close(
Expand Down Expand Up @@ -7852,6 +7857,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
fn internal_open_channel(&self, counterparty_node_id: &PublicKey, msg: OpenChannelMessageRef<'_>) -> Result<(), MsgHandleErrInternal> {
let common_fields = match msg {
OpenChannelMessageRef::V1(msg) => &msg.common_fields,
#[cfg(dual_funding)]
OpenChannelMessageRef::V2(msg) => &msg.common_fields,
};

Expand Down Expand Up @@ -7929,6 +7935,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
funding_satoshis: common_fields.funding_satoshis,
channel_negotiation_type: match msg {
OpenChannelMessageRef::V1(msg) => InboundChannelFunds::PushMsat(msg.push_msat),
#[cfg(dual_funding)]
OpenChannelMessageRef::V2(_) => InboundChannelFunds::DualFunded,
},
channel_type,
Expand All @@ -7938,6 +7945,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
peer_state.inbound_channel_request_by_id.insert(channel_id, InboundChannelRequest {
open_channel_msg: match msg {
OpenChannelMessageRef::V1(msg) => OpenChannelMessage::V1(msg.clone()),
#[cfg(dual_funding)]
OpenChannelMessageRef::V2(msg) => OpenChannelMessage::V2(msg.clone()),
},
ticks_remaining: UNACCEPTED_INBOUND_CHANNEL_AGE_LIMIT_TICKS,
Expand Down Expand Up @@ -7973,6 +7981,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
});
(ChannelPhase::UnfundedInboundV1(channel), message_send_event)
},
#[cfg(dual_funding)]
OpenChannelMessageRef::V2(msg) => {
let channel = InboundV2Channel::new(&self.fee_estimator, &self.entropy_source,
&self.signer_provider, self.get_our_node_id(), *counterparty_node_id,
Expand Down Expand Up @@ -11271,6 +11280,7 @@ where
// Note that we never need to persist the updated ChannelManager for an inbound
// open_channel message - pre-funded channels are never written so there should be no
// change to the contents.
#[cfg(dual_funding)]
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
let res = self.internal_open_channel(&counterparty_node_id, OpenChannelMessageRef::V2(msg));
let persist = match &res {
Expand All @@ -11283,6 +11293,10 @@ where
let _ = handle_error!(self, res, counterparty_node_id);
persist
});
#[cfg(not(dual_funding))]
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
"Dual-funded channels not supported".to_owned(),
msg.common_fields.temporary_channel_id.clone())), counterparty_node_id);
}

fn handle_accept_channel(&self, counterparty_node_id: PublicKey, msg: &msgs::AcceptChannel) {
Expand Down Expand Up @@ -12334,6 +12348,7 @@ pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx {
features.set_anchors_zero_fee_htlc_tx_optional();
}
#[cfg(dual_funding)]
features.set_dual_fund_optional();
features
}
Expand Down
43 changes: 24 additions & 19 deletions lightning/src/ln/dual_funding_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,36 @@

//! Tests that test the creation of dual-funded channels in ChannelManager.
use bitcoin::Weight;

use crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator};
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider};
use crate::ln::chan_utils::{
make_funding_redeemscript, ChannelPublicKeys, ChannelTransactionParameters,
CounterpartyChannelTransactionParameters,
};
use crate::ln::channel::{
calculate_our_funding_satoshis, OutboundV2Channel, MIN_CHAN_DUST_LIMIT_SATOSHIS,
#[cfg(dual_funding)]
use {
crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator},
crate::events::{Event, MessageSendEvent, MessageSendEventsProvider},
crate::ln::chan_utils::{
make_funding_redeemscript, ChannelPublicKeys, ChannelTransactionParameters,
CounterpartyChannelTransactionParameters,
},
crate::ln::channel::{
calculate_our_funding_satoshis, OutboundV2Channel, MIN_CHAN_DUST_LIMIT_SATOSHIS,
},
crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint},
crate::ln::functional_test_utils::*,
crate::ln::msgs::ChannelMessageHandler,
crate::ln::msgs::{CommitmentSigned, TxAddInput, TxAddOutput, TxComplete},
crate::ln::types::ChannelId,
crate::prelude::*,
crate::sign::{ChannelSigner as _, P2WPKH_WITNESS_WEIGHT},
crate::util::ser::TransactionU16LenLimited,
crate::util::test_utils,
bitcoin::Weight,
};
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint};
use crate::ln::functional_test_utils::*;
use crate::ln::msgs::ChannelMessageHandler;
use crate::ln::msgs::{CommitmentSigned, TxAddInput, TxAddOutput, TxComplete};
use crate::ln::types::ChannelId;
use crate::prelude::*;
use crate::sign::{ChannelSigner as _, P2WPKH_WITNESS_WEIGHT};
use crate::util::ser::TransactionU16LenLimited;
use crate::util::test_utils;

#[cfg(dual_funding)]
// Dual-funding: V2 Channel Establishment Tests
struct V2ChannelEstablishmentTestSession {
initiator_input_value_satoshis: u64,
}

#[cfg(dual_funding)]
// TODO(dual_funding): Use real node and API for creating V2 channels as initiator when available,
// instead of manually constructing messages.
fn do_test_v2_channel_establishment(
Expand Down Expand Up @@ -252,6 +256,7 @@ fn do_test_v2_channel_establishment(
}

#[test]
#[cfg(dual_funding)]
fn test_v2_channel_establishment() {
// Only initiator contributes, no persist pending
do_test_v2_channel_establishment(
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ impl ChannelMessageHandler for ErroringMessageHandler {
features.set_basic_mpp_optional();
features.set_wumbo_optional();
features.set_shutdown_any_segwit_optional();
#[cfg(dual_funding)]
features.set_dual_fund_optional();
features.set_channel_type_optional();
features.set_scid_privacy_optional();
Expand Down Expand Up @@ -1820,8 +1821,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
wire::Message::OpenChannel(msg) => {
self.message_handler.chan_handler.handle_open_channel(their_node_id, &msg);
},
wire::Message::OpenChannelV2(msg) => {
self.message_handler.chan_handler.handle_open_channel_v2(their_node_id, &msg);
wire::Message::OpenChannelV2(_msg) => {
self.message_handler.chan_handler.handle_open_channel_v2(their_node_id, &_msg);
},
wire::Message::AcceptChannel(msg) => {
self.message_handler.chan_handler.handle_accept_channel(their_node_id, &msg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# API Updates
# API Updates (0.2)
* Accepting dual-funded (V2 establishment) channels (without contibuting) is now supported (#3137).
Some particulars to be aware of for this feature:
* Creating dual-funded channels is not yet supported.
Expand Down
7 changes: 7 additions & 0 deletions pending_changelog/3137-channel-negotiation-type.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# API Updates
* `Event::OpenChannelRequest::push_msat` has been replaced by the field `channel_negotiation_type` to
differentiate between an inbound request for a dual-funded (V2) or non-dual-funded (V1) channel to be
opened, with value being either of the enum variants `InboundChannelFunds::DualFunded` and
`InboundChannelFunds::PushMsat(u64)` corresponding to V2 and V1 channel open requests respectively.
This is in preparation for supporting accepting dual-funded channels, which will be available in a later release.

0 comments on commit f53a09d

Please sign in to comment.