Skip to content

Commit 7c77daf

Browse files
authored
Merge pull request #3498 from jkczyz/2024-12-combine-inbound-v2-phases
Combine `InboundV2Channel` and `OutboundV2Channel`
2 parents 2c0066e + d6637d7 commit 7c77daf

File tree

3 files changed

+116
-201
lines changed

3 files changed

+116
-201
lines changed

lightning/src/ln/channel.rs

+49-120
Original file line numberDiff line numberDiff line change
@@ -1127,9 +1127,7 @@ 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>),
1131-
#[allow(dead_code)] // TODO(dual_funding): Remove once accepting V2 channels is enabled.
1132-
UnfundedInboundV2(InboundV2Channel<SP>),
1130+
UnfundedV2(PendingV2Channel<SP>),
11331131
Funded(Channel<SP>),
11341132
}
11351133

@@ -1142,8 +1140,7 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11421140
ChannelPhase::Funded(chan) => &chan.context,
11431141
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
11441142
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
1145-
ChannelPhase::UnfundedOutboundV2(chan) => &chan.context,
1146-
ChannelPhase::UnfundedInboundV2(chan) => &chan.context,
1143+
ChannelPhase::UnfundedV2(chan) => &chan.context,
11471144
}
11481145
}
11491146

@@ -1152,8 +1149,7 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11521149
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
11531150
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
11541151
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
1155-
ChannelPhase::UnfundedOutboundV2(ref mut chan) => &mut chan.context,
1156-
ChannelPhase::UnfundedInboundV2(ref mut chan) => &mut chan.context,
1152+
ChannelPhase::UnfundedV2(ref mut chan) => &mut chan.context,
11571153
}
11581154
}
11591155
}
@@ -1684,63 +1680,53 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for Channel<SP> where SP::Ta
16841680
}
16851681
}
16861682

1687-
pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider {
1688-
fn context(&self) -> &ChannelContext<SP>;
1689-
1690-
fn context_mut(&mut self) -> &mut ChannelContext<SP>;
1691-
1692-
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor>;
1693-
1694-
fn dual_funding_context(&self) -> &DualFundingChannelContext;
1695-
1696-
fn unfunded_context(&self) -> &UnfundedChannelContext;
1697-
1698-
fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
1699-
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
1683+
impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1684+
pub fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
1685+
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
17001686
Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_input(msg).map_err(
1701-
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
1687+
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
17021688
None => Err(msgs::TxAbort {
1703-
channel_id: self.context().channel_id(),
1689+
channel_id: self.context.channel_id(),
17041690
data: b"No interactive transaction negotiation in progress".to_vec()
17051691
}),
17061692
})
17071693
}
17081694

1709-
fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
1710-
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
1695+
pub fn tx_add_output(&mut self, msg: &msgs::TxAddOutput)-> InteractiveTxMessageSendResult {
1696+
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
17111697
Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_output(msg).map_err(
1712-
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
1698+
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
17131699
None => Err(msgs::TxAbort {
1714-
channel_id: self.context().channel_id(),
1700+
channel_id: self.context.channel_id(),
17151701
data: b"No interactive transaction negotiation in progress".to_vec()
17161702
}),
17171703
})
17181704
}
17191705

1720-
fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
1721-
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
1706+
pub fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput)-> InteractiveTxMessageSendResult {
1707+
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
17221708
Some(ref mut tx_constructor) => tx_constructor.handle_tx_remove_input(msg).map_err(
1723-
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
1709+
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
17241710
None => Err(msgs::TxAbort {
1725-
channel_id: self.context().channel_id(),
1711+
channel_id: self.context.channel_id(),
17261712
data: b"No interactive transaction negotiation in progress".to_vec()
17271713
}),
17281714
})
17291715
}
17301716

1731-
fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
1732-
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
1717+
pub fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput)-> InteractiveTxMessageSendResult {
1718+
InteractiveTxMessageSendResult(match &mut self.interactive_tx_constructor {
17331719
Some(ref mut tx_constructor) => tx_constructor.handle_tx_remove_output(msg).map_err(
1734-
|reason| reason.into_tx_abort_msg(self.context().channel_id())),
1720+
|reason| reason.into_tx_abort_msg(self.context.channel_id())),
17351721
None => Err(msgs::TxAbort {
1736-
channel_id: self.context().channel_id(),
1722+
channel_id: self.context.channel_id(),
17371723
data: b"No interactive transaction negotiation in progress".to_vec()
17381724
}),
17391725
})
17401726
}
17411727

1742-
fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
1743-
let tx_constructor = match self.interactive_tx_constructor_mut() {
1728+
pub fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
1729+
let tx_constructor = match &mut self.interactive_tx_constructor {
17441730
Some(ref mut tx_constructor) => tx_constructor,
17451731
None => {
17461732
let tx_abort = msgs::TxAbort {
@@ -1759,26 +1745,25 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17591745
};
17601746

17611747
if let HandleTxCompleteValue::SendTxComplete(_, ref signing_session) = tx_complete {
1762-
self.context_mut().next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
1748+
self.context.next_funding_txid = Some(signing_session.unsigned_tx.compute_txid());
17631749
};
17641750

17651751
HandleTxCompleteResult(Ok(tx_complete))
17661752
}
17671753

1768-
fn funding_tx_constructed<L: Deref>(
1754+
pub fn funding_tx_constructed<L: Deref>(
17691755
&mut self, signing_session: &mut InteractiveTxSigningSession, logger: &L
17701756
) -> Result<(msgs::CommitmentSigned, Option<Event>), ChannelError>
17711757
where
17721758
L::Target: Logger
17731759
{
1774-
let our_funding_satoshis = self.dual_funding_context().our_funding_satoshis;
1775-
let transaction_number = self.unfunded_context().transaction_number();
1776-
let context = self.context_mut();
1760+
let our_funding_satoshis = self.dual_funding_context.our_funding_satoshis;
1761+
let transaction_number = self.unfunded_context.transaction_number();
17771762

17781763
let mut output_index = None;
1779-
let expected_spk = context.get_funding_redeemscript().to_p2wsh();
1764+
let expected_spk = self.context.get_funding_redeemscript().to_p2wsh();
17801765
for (idx, outp) in signing_session.unsigned_tx.outputs().enumerate() {
1781-
if outp.script_pubkey() == &expected_spk && outp.value() == context.get_value_satoshis() {
1766+
if outp.script_pubkey() == &expected_spk && outp.value() == self.context.get_value_satoshis() {
17821767
if output_index.is_some() {
17831768
return Err(ChannelError::Close(
17841769
(
@@ -1798,26 +1783,26 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17981783
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
17991784
)));
18001785
};
1801-
context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
1802-
context.holder_signer.as_mut().provide_channel_parameters(&context.channel_transaction_parameters);
1786+
self.context.channel_transaction_parameters.funding_outpoint = Some(outpoint);
1787+
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
18031788

1804-
context.assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
1805-
let commitment_signed = context.get_initial_commitment_signed(logger);
1789+
self.context.assert_no_commitment_advancement(transaction_number, "initial commitment_signed");
1790+
let commitment_signed = self.context.get_initial_commitment_signed(logger);
18061791
let commitment_signed = match commitment_signed {
18071792
Ok(commitment_signed) => {
1808-
context.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
1793+
self.context.funding_transaction = Some(signing_session.unsigned_tx.build_unsigned_tx());
18091794
commitment_signed
18101795
},
18111796
Err(err) => {
1812-
context.channel_transaction_parameters.funding_outpoint = None;
1797+
self.context.channel_transaction_parameters.funding_outpoint = None;
18131798
return Err(ChannelError::Close((err.to_string(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) })))
18141799
},
18151800
};
18161801

18171802
let funding_ready_for_sig_event = None;
18181803
if signing_session.local_inputs_count() == 0 {
18191804
debug_assert_eq!(our_funding_satoshis, 0);
1820-
if signing_session.provide_holder_witnesses(context.channel_id, Vec::new()).is_err() {
1805+
if signing_session.provide_holder_witnesses(self.context.channel_id, Vec::new()).is_err() {
18211806
debug_assert!(
18221807
false,
18231808
"Zero inputs were provided & zero witnesses were provided, but a count mismatch was somehow found",
@@ -1840,51 +1825,15 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
18401825
// </div>
18411826
}
18421827

1843-
context.channel_state = ChannelState::FundingNegotiated;
1828+
self.context.channel_state = ChannelState::FundingNegotiated;
18441829

18451830
// Clear the interactive transaction constructor
1846-
self.interactive_tx_constructor_mut().take();
1831+
self.interactive_tx_constructor.take();
18471832

18481833
Ok((commitment_signed, funding_ready_for_sig_event))
18491834
}
18501835
}
18511836

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 {
1871-
fn context(&self) -> &ChannelContext<SP> {
1872-
&self.context
1873-
}
1874-
fn context_mut(&mut self) -> &mut ChannelContext<SP> {
1875-
&mut self.context
1876-
}
1877-
fn dual_funding_context(&self) -> &DualFundingChannelContext {
1878-
&self.dual_funding_context
1879-
}
1880-
fn unfunded_context(&self) -> &UnfundedChannelContext {
1881-
&self.unfunded_context
1882-
}
1883-
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
1884-
&mut self.interactive_tx_constructor
1885-
}
1886-
}
1887-
18881837
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
18891838
fn new_for_inbound_channel<'a, ES: Deref, F: Deref, L: Deref>(
18901839
fee_estimator: &'a LowerBoundedFeeEstimator<F>,
@@ -8819,24 +8768,24 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
88198768
}
88208769
}
88218770

8822-
// A not-yet-funded outbound (from holder) channel using V2 channel establishment.
8823-
pub(super) struct OutboundV2Channel<SP: Deref> where SP::Target: SignerProvider {
8771+
// A not-yet-funded channel using V2 channel establishment.
8772+
pub(super) struct PendingV2Channel<SP: Deref> where SP::Target: SignerProvider {
88248773
pub context: ChannelContext<SP>,
88258774
pub unfunded_context: UnfundedChannelContext,
88268775
pub dual_funding_context: DualFundingChannelContext,
88278776
/// The current interactive transaction construction session under negotiation.
8828-
interactive_tx_constructor: Option<InteractiveTxConstructor>,
8777+
pub interactive_tx_constructor: Option<InteractiveTxConstructor>,
88298778
}
88308779

8831-
impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
8780+
impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
88328781
#[allow(dead_code)] // TODO(dual_funding): Remove once creating V2 channels is enabled.
8833-
pub fn new<ES: Deref, F: Deref, L: Deref>(
8782+
pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
88348783
fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
88358784
counterparty_node_id: PublicKey, their_features: &InitFeatures, funding_satoshis: u64,
88368785
funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>, user_id: u128, config: &UserConfig,
88378786
current_chain_height: u32, outbound_scid_alias: u64, funding_confirmation_target: ConfirmationTarget,
88388787
logger: L,
8839-
) -> Result<OutboundV2Channel<SP>, APIError>
8788+
) -> Result<Self, APIError>
88408789
where ES::Target: EntropySource,
88418790
F::Target: FeeEstimator,
88428791
L::Target: Logger,
@@ -8908,6 +8857,10 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
89088857
}
89098858

89108859
pub fn get_open_channel_v2(&self, chain_hash: ChainHash) -> msgs::OpenChannelV2 {
8860+
if !self.context.is_outbound() {
8861+
debug_assert!(false, "Tried to send open_channel2 for an inbound channel?");
8862+
}
8863+
89118864
if self.context.have_received_message() {
89128865
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");
89138866
}
@@ -8957,40 +8910,16 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
89578910
}
89588911
}
89598912

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

0 commit comments

Comments
 (0)