Skip to content

Commit 91ff6c1

Browse files
committed
Combine InboundV2Channel and OutboundV2Channel
Pending v2 channels will need to be broken up into separate phases for constructing and signing the funding transaction. To avoid increasing the number of phases, combine the InboundV2Channel and OutboundV2Channel types so that the can be used in one phase. Whether the channel is inbound or outbound can be inferred from the ChannelContext.
1 parent 42cc4e7 commit 91ff6c1

File tree

3 files changed

+29
-63
lines changed

3 files changed

+29
-63
lines changed

lightning/src/ln/channel.rs

+14-52
Original file line numberDiff line numberDiff line change
@@ -1127,9 +1127,9 @@ pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
11271127
UnfundedOutboundV1(OutboundV1Channel<SP>),
11281128
UnfundedInboundV1(InboundV1Channel<SP>),
11291129
#[allow(dead_code)] // TODO(dual_funding): Remove once creating V2 channels is enabled.
1130-
UnfundedOutboundV2(OutboundV2Channel<SP>),
1130+
UnfundedOutboundV2(PendingV2Channel<SP>),
11311131
#[allow(dead_code)] // TODO(dual_funding): Remove once accepting V2 channels is enabled.
1132-
UnfundedInboundV2(InboundV2Channel<SP>),
1132+
UnfundedInboundV2(PendingV2Channel<SP>),
11331133
Funded(Channel<SP>),
11341134
}
11351135

@@ -1849,25 +1849,7 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
18491849
}
18501850
}
18511851

1852-
impl<SP: Deref> InteractivelyFunded<SP> for OutboundV2Channel<SP> where SP::Target: SignerProvider {
1853-
fn context(&self) -> &ChannelContext<SP> {
1854-
&self.context
1855-
}
1856-
fn context_mut(&mut self) -> &mut ChannelContext<SP> {
1857-
&mut self.context
1858-
}
1859-
fn dual_funding_context(&self) -> &DualFundingChannelContext {
1860-
&self.dual_funding_context
1861-
}
1862-
fn unfunded_context(&self) -> &UnfundedChannelContext {
1863-
&self.unfunded_context
1864-
}
1865-
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
1866-
&mut self.interactive_tx_constructor
1867-
}
1868-
}
1869-
1870-
impl<SP: Deref> InteractivelyFunded<SP> for InboundV2Channel<SP> where SP::Target: SignerProvider {
1852+
impl<SP: Deref> InteractivelyFunded<SP> for PendingV2Channel<SP> where SP::Target: SignerProvider {
18711853
fn context(&self) -> &ChannelContext<SP> {
18721854
&self.context
18731855
}
@@ -8822,24 +8804,24 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
88228804
}
88238805
}
88248806

8825-
// A not-yet-funded outbound (from holder) channel using V2 channel establishment.
8826-
pub(super) struct OutboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
8807+
// A not-yet-funded channel using V2 channel establishment.
8808+
pub(super) struct PendingV2Channel<SP: Deref> where SP::Target: SignerProvider {
88278809
pub context: ChannelContext<SP>,
88288810
pub unfunded_context: UnfundedChannelContext,
88298811
pub dual_funding_context: DualFundingChannelContext,
88308812
/// The current interactive transaction construction session under negotiation.
88318813
interactive_tx_constructor: Option<InteractiveTxConstructor>,
88328814
}
88338815

8834-
impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
8816+
impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
88358817
#[allow(dead_code)] // TODO(dual_funding): Remove once creating V2 channels is enabled.
8836-
pub fn new<ES: Deref, F: Deref, L: Deref>(
8818+
pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
88378819
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
88388820
counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
88398821
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, user_id: u128, config: &UserConfig,
88408822
current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
88418823
logger: L,
8842-
) -> Result<OutboundV2Channel<SP>, APIError>
8824+
) -> Result<Self, APIError>
88438825
where ES::Target: EntropySource,
88448826
F::Target: FeeEstimator,
88458827
L::Target: Logger,
@@ -8911,6 +8893,10 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
89118893
}
89128894

89138895
pub fn get_open_channel_v2(&self, chain_hash: ChainHash) -> msgs::OpenChannelV2 {
8896+
if !self.context.is_outbound() {
8897+
debug_assert!(false, "Tried to send open_channel2 for an inbound channel?");
8898+
}
8899+
89148900
if self.context.have_received_message() {
89158901
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");
89168902
}
@@ -8960,40 +8946,16 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
89608946
}
89618947
}
89628948

8963-
pub fn into_channel(self, signing_session: InteractiveTxSigningSession) -> Result<Channel<SP>, ChannelError>{
8964-
let holder_commitment_point = self.unfunded_context.holder_commitment_point.ok_or(ChannelError::close(
8965-
format!("Expected to have holder commitment points available upon finishing interactive tx construction for channel {}",
8966-
self.context.channel_id())))?;
8967-
let channel = Channel {
8968-
context: self.context,
8969-
interactive_tx_signing_session: Some(signing_session),
8970-
holder_commitment_point,
8971-
};
8972-
8973-
Ok(channel)
8974-
}
8975-
}
8976-
8977-
// A not-yet-funded inbound (from counterparty) channel using V2 channel establishment.
8978-
pub(super) struct InboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
8979-
pub context: ChannelContext<SP>,
8980-
pub unfunded_context: UnfundedChannelContext,
8981-
pub dual_funding_context: DualFundingChannelContext,
8982-
/// The current interactive transaction construction session under negotiation.
8983-
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8984-
}
8985-
8986-
impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
89878949
/// Creates a new dual-funded channel from a remote side's request for one.
89888950
/// Assumes chain_hash has already been checked and corresponds with what we expect!
89898951
#[allow(dead_code)] // TODO(dual_funding): Remove once V2 channels is enabled.
8990-
pub fn new<ES: Deref, F: Deref, L: Deref>(
8952+
pub fn new_inbound<ES: Deref, F: Deref, L: Deref>(
89918953
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
89928954
holder_node_id: PublicKey, counterparty_node_id: PublicKey, our_supported_features: &ChannelTypeFeatures,
89938955
their_features: &InitFeatures, msg: &msgs::OpenChannelV2,
89948956
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, total_witness_weight: Weight,
89958957
user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
8996-
) -> Result<InboundV2Channel<SP>, ChannelError>
8958+
) -> Result<Self, ChannelError>
89978959
where ES::Target: EntropySource,
89988960
F::Target: FeeEstimator,
89998961
L::Target: Logger,

lightning/src/ln/channelmanager.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use crate::ln::types::ChannelId;
5050
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
5151
use crate::ln::channel::{self, Channel, ChannelPhase, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext, InteractivelyFunded as _};
5252
#[cfg(any(dual_funding, splicing))]
53-
use crate::ln::channel::InboundV2Channel;
53+
use crate::ln::channel::PendingV2Channel;
5454
use crate::ln::channel_state::ChannelDetails;
5555
use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
5656
#[cfg(any(feature = "_test_utils", test))]
@@ -7702,10 +7702,13 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
77027702
},
77037703
#[cfg(dual_funding)]
77047704
OpenChannelMessage::V2(open_channel_msg) => {
7705-
InboundV2Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
7706-
self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(), &peer_state.latest_features,
7707-
&open_channel_msg, _funding_inputs, _total_witness_weight, user_channel_id,
7708-
&self.default_configuration, best_block_height, &self.logger
7705+
PendingV2Channel::new_inbound(
7706+
&self.fee_estimator, &self.entropy_source, &self.signer_provider,
7707+
self.get_our_node_id(), *counterparty_node_id,
7708+
&self.channel_type_features(), &peer_state.latest_features,
7709+
&open_channel_msg, _funding_inputs, _total_witness_weight,
7710+
user_channel_id, &self.default_configuration, best_block_height,
7711+
&self.logger,
77097712
).map_err(|_| MsgHandleErrInternal::from_chan_no_close(
77107713
ChannelError::Close(
77117714
(
@@ -7983,10 +7986,11 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79837986
},
79847987
#[cfg(dual_funding)]
79857988
OpenChannelMessageRef::V2(msg) => {
7986-
let channel = InboundV2Channel::new(&self.fee_estimator, &self.entropy_source,
7987-
&self.signer_provider, self.get_our_node_id(), *counterparty_node_id,
7988-
&self.channel_type_features(), &peer_state.latest_features, msg, vec![], Weight::from_wu(0),
7989-
user_channel_id, &self.default_configuration, best_block_height, &self.logger
7989+
let channel = PendingV2Channel::new_inbound(
7990+
&self.fee_estimator, &self.entropy_source, &self.signer_provider,
7991+
self.get_our_node_id(), *counterparty_node_id, &self.channel_type_features(),
7992+
&peer_state.latest_features, msg, vec![], Weight::from_wu(0), user_channel_id,
7993+
&self.default_configuration, best_block_height, &self.logger,
79907994
).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, msg.common_fields.temporary_channel_id))?;
79917995
let message_send_event = events::MessageSendEvent::SendAcceptChannelV2 {
79927996
node_id: *counterparty_node_id,

lightning/src/ln/dual_funding_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {
1818
CounterpartyChannelTransactionParameters,
1919
},
2020
crate::ln::channel::{
21-
calculate_our_funding_satoshis, OutboundV2Channel, MIN_CHAN_DUST_LIMIT_SATOSHIS,
21+
calculate_our_funding_satoshis, PendingV2Channel, MIN_CHAN_DUST_LIMIT_SATOSHIS,
2222
},
2323
crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint},
2424
crate::ln::functional_test_utils::*,
@@ -71,7 +71,7 @@ fn do_test_v2_channel_establishment(
7171
MIN_CHAN_DUST_LIMIT_SATOSHIS,
7272
)
7373
.unwrap();
74-
let mut channel = OutboundV2Channel::new(
74+
let mut channel = PendingV2Channel::new_outbound(
7575
&LowerBoundedFeeEstimator(node_cfgs[0].fee_estimator),
7676
&nodes[0].node.entropy_source,
7777
&nodes[0].node.signer_provider,

0 commit comments

Comments
 (0)