Skip to content

Commit

Permalink
Reintroduce cfg(dual_funding) for handling of open_channel2 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dunxen committed Dec 13, 2024
1 parent 6e85a0d commit 5e93522
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 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)",
]
1 change: 1 addition & 0 deletions lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ mod tests {
) {
}
fn handle_channel_update(&self, _their_node_id: PublicKey, _msg: &ChannelUpdate) {}
#[cfg(dual_funding)]
fn handle_open_channel_v2(&self, _their_node_id: PublicKey, _msg: &OpenChannelV2) {}
fn handle_accept_channel_v2(&self, _their_node_id: PublicKey, _msg: &AcceptChannelV2) {}
fn handle_stfu(&self, _their_node_id: PublicKey, _msg: &Stfu) {}
Expand Down
9 changes: 8 additions & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,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 @@ -4080,7 +4081,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 @@ -4150,6 +4151,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 @@ -4199,6 +4201,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 @@ -8276,6 +8279,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 @@ -8901,6 +8905,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 @@ -9005,6 +9010,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 @@ -9027,6 +9033,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.context.holder_commitment_point.transaction_number(), &self.context.secp_ctx)
Expand Down
18 changes: 14 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(dual_funding)]
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 @@ -1376,11 +1378,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 @@ -7686,8 +7690,8 @@ where

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 @@ -7728,10 +7732,11 @@ where
(*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 @@ -7882,6 +7887,7 @@ where
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 @@ -7959,6 +7965,7 @@ where
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 @@ -7968,6 +7975,7 @@ where
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 @@ -8000,6 +8008,7 @@ where
};
(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 @@ -11269,6 +11278,7 @@ where
});
}

#[cfg(dual_funding)]
fn handle_open_channel_v2(&self, counterparty_node_id: PublicKey, msg: &msgs::OpenChannelV2) {
// 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
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
1 change: 1 addition & 0 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,7 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
/// Handle an incoming `open_channel` message from the given peer.
fn handle_open_channel(&self, their_node_id: PublicKey, msg: &OpenChannel);
/// Handle an incoming `open_channel2` message from the given peer.
#[cfg(dual_funding)]
fn handle_open_channel_v2(&self, their_node_id: PublicKey, msg: &OpenChannelV2);
/// Handle an incoming `accept_channel` message from the given peer.
fn handle_accept_channel(&self, their_node_id: PublicKey, msg: &AcceptChannel);
Expand Down
6 changes: 4 additions & 2 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl ChannelMessageHandler for ErroringMessageHandler {
None
}

#[cfg(dual_funding)]
fn handle_open_channel_v2(&self, their_node_id: PublicKey, msg: &msgs::OpenChannelV2) {
ErroringMessageHandler::push_error(self, their_node_id, msg.common_fields.temporary_channel_id);
}
Expand Down Expand Up @@ -1820,8 +1821,9 @@ 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) => {
#[cfg(dual_funding)]
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
1 change: 1 addition & 0 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
Some(vec![self.chain_hash])
}

#[cfg(dual_funding)]
fn handle_open_channel_v2(&self, _their_node_id: PublicKey, msg: &msgs::OpenChannelV2) {
self.received_msg(wire::Message::OpenChannelV2(msg.clone()));
}
Expand Down

0 comments on commit 5e93522

Please sign in to comment.