Skip to content

Commit 2c9397c

Browse files
committed
Create CommitmentSignedResult enum for Channel::commitment_signed();
The return type of commitment_signed() started groing into a really long and unwieldy tuple. We introduce a dedicated enum return type for it here.
1 parent e72c2c0 commit 2c9397c

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,15 @@ where
14561456
Funded(FundedChannel<SP>),
14571457
}
14581458

1459+
pub(super) enum CommitmentSignedResult<SP: Deref>
1460+
where
1461+
SP::Target: SignerProvider,
1462+
{
1463+
ChannelMonitor(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>),
1464+
ChannelMonitorUpdate(ChannelMonitorUpdate),
1465+
None,
1466+
}
1467+
14591468
impl<SP: Deref> Channel<SP>
14601469
where
14611470
SP::Target: SignerProvider,
@@ -1781,7 +1790,7 @@ where
17811790
#[rustfmt::skip]
17821791
pub fn commitment_signed<L: Deref>(
17831792
&mut self, msg: &msgs::CommitmentSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
1784-
) -> Result<(Option<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>, Option<Transaction>), ChannelError>
1793+
) -> Result<CommitmentSignedResult<SP>, ChannelError>
17851794
where
17861795
L::Target: Logger
17871796
{
@@ -1808,7 +1817,9 @@ where
18081817
pending_splice: None,
18091818
};
18101819
let res = funded_channel.commitment_signed_initial_v2(msg, best_block, signer_provider, logger)
1811-
.map(|(monitor, funding_tx_opt)| (Some(monitor), None, funding_tx_opt))
1820+
.map(|(monitor, _)| {
1821+
CommitmentSignedResult::ChannelMonitor(monitor)
1822+
})
18121823
// TODO: Change to `inspect_err` when MSRV is high enough.
18131824
.map_err(|err| {
18141825
// We always expect a `ChannelError` close.
@@ -1835,15 +1846,33 @@ where
18351846
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
18361847
funded_channel
18371848
.splice_initial_commitment_signed(msg, logger)
1838-
.map(|monitor_update_opt| (None, monitor_update_opt, None))
1849+
.map(|monitor_update_opt|
1850+
if let Some(monitor_update) = monitor_update_opt {
1851+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update)
1852+
} else {
1853+
CommitmentSignedResult::None
1854+
}
1855+
)
18391856
} else {
18401857
funded_channel.commitment_signed(msg, logger)
1841-
.map(|monitor_update_opt| (None, monitor_update_opt, None))
1858+
.map(|monitor_update_opt|
1859+
if let Some(monitor_update) = monitor_update_opt {
1860+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update)
1861+
} else {
1862+
CommitmentSignedResult::None
1863+
}
1864+
)
18421865
};
18431866

18441867
#[cfg(not(splicing))]
18451868
let res = funded_channel.commitment_signed(msg, logger)
1846-
.map(|monitor_update_opt| (None, monitor_update_opt, None));
1869+
.map(|monitor_update_opt|
1870+
if let Some(monitor_update) = monitor_update_opt {
1871+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update)
1872+
} else {
1873+
CommitmentSignedResult::None
1874+
}
1875+
);
18471876

18481877
self.phase = ChannelPhase::Funded(funded_channel);
18491878
res

lightning/src/ln/channelmanager.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ use crate::ln::chan_utils::selected_commitment_sat_per_1000_weight;
6060
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
6161
// construct one themselves.
6262
use crate::ln::channel::{
63-
self, hold_time_since, Channel, ChannelError, ChannelUpdateStatus, FundedChannel,
64-
InboundV1Channel, OutboundV1Channel, PendingV2Channel, ReconnectionMsg, ShutdownResult,
65-
UpdateFulfillCommitFetch, WithChannelContext,
63+
self, hold_time_since, Channel, ChannelError, ChannelUpdateStatus, CommitmentSignedResult,
64+
FundedChannel, InboundV1Channel, OutboundV1Channel, PendingV2Channel, ReconnectionMsg,
65+
ShutdownResult, UpdateFulfillCommitFetch, WithChannelContext,
6666
};
6767
use crate::ln::channel_state::ChannelDetails;
6868
use crate::ln::inbound_payment;
@@ -135,6 +135,7 @@ use crate::util::wakers::{Future, Notifier};
135135

136136
#[cfg(all(test, async_payments))]
137137
use crate::blinded_path::payment::BlindedPaymentPath;
138+
138139
#[cfg(async_payments)]
139140
use {
140141
crate::blinded_path::message::BlindedMessagePath,
@@ -10346,27 +10347,32 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1034610347
let chan = chan_entry.get_mut();
1034710348
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
1034810349
let funding_txo = chan.funding().get_funding_txo();
10349-
let (monitor_opt, monitor_update_opt, _) = try_channel_entry!(
10350+
let res = try_channel_entry!(
1035010351
self, peer_state, chan.commitment_signed(msg, best_block, &self.signer_provider, &&logger),
1035110352
chan_entry);
1035210353

1035310354
if let Some(chan) = chan.as_funded_mut() {
10354-
if let Some(monitor) = monitor_opt {
10355-
let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
10356-
if let Ok(persist_state) = monitor_res {
10357-
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
10358-
per_peer_state, chan, INITIAL_MONITOR);
10359-
} else {
10360-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
10361-
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
10362-
let msg = "Channel ID was a duplicate";
10363-
let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
10364-
let err = ChannelError::Close((msg.to_owned(), reason));
10365-
try_channel_entry!(self, peer_state, Err(err), chan_entry)
10366-
}
10367-
} else if let Some(monitor_update) = monitor_update_opt {
10368-
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
10369-
peer_state, per_peer_state, chan);
10355+
let monitor = match res {
10356+
CommitmentSignedResult::ChannelMonitor(monitor) => monitor,
10357+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update) => {
10358+
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
10359+
peer_state, per_peer_state, chan);
10360+
return Ok(());
10361+
},
10362+
CommitmentSignedResult::None => return Ok(()),
10363+
};
10364+
10365+
let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
10366+
if let Ok(persist_state) = monitor_res {
10367+
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
10368+
per_peer_state, chan, INITIAL_MONITOR);
10369+
} else {
10370+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
10371+
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
10372+
let msg = "Channel ID was a duplicate";
10373+
let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
10374+
let err = ChannelError::Close((msg.to_owned(), reason));
10375+
try_channel_entry!(self, peer_state, Err(err), chan_entry)
1037010376
}
1037110377
}
1037210378
Ok(())

0 commit comments

Comments
 (0)