Skip to content

Commit f7e05f6

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 1c6c08f commit f7e05f6

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,15 @@ where
14591459
Funded(FundedChannel<SP>),
14601460
}
14611461

1462+
pub(super) enum CommitmentSignedResult<SP: Deref>
1463+
where
1464+
SP::Target: SignerProvider,
1465+
{
1466+
ChannelMonitor(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>),
1467+
ChannelMonitorUpdate(ChannelMonitorUpdate),
1468+
None,
1469+
}
1470+
14621471
impl<SP: Deref> Channel<SP>
14631472
where
14641473
SP::Target: SignerProvider,
@@ -1784,7 +1793,7 @@ where
17841793
#[rustfmt::skip]
17851794
pub fn commitment_signed<L: Deref>(
17861795
&mut self, msg: &msgs::CommitmentSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
1787-
) -> Result<(Option<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>, Option<Transaction>), ChannelError>
1796+
) -> Result<CommitmentSignedResult<SP>, ChannelError>
17881797
where
17891798
L::Target: Logger
17901799
{
@@ -1811,7 +1820,9 @@ where
18111820
pending_splice: None,
18121821
};
18131822
let res = funded_channel.commitment_signed_initial_v2(msg, best_block, signer_provider, logger)
1814-
.map(|(monitor, funding_tx_opt)| (Some(monitor), None, funding_tx_opt))
1823+
.map(|(monitor, _)| {
1824+
CommitmentSignedResult::ChannelMonitor(monitor)
1825+
})
18151826
// TODO: Change to `inspect_err` when MSRV is high enough.
18161827
.map_err(|err| {
18171828
// We always expect a `ChannelError` close.
@@ -1838,15 +1849,33 @@ where
18381849
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
18391850
funded_channel
18401851
.splice_initial_commitment_signed(msg, logger)
1841-
.map(|monitor_update_opt| (None, monitor_update_opt, None))
1852+
.map(|monitor_update_opt|
1853+
if let Some(monitor_update) = monitor_update_opt {
1854+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update)
1855+
} else {
1856+
CommitmentSignedResult::None
1857+
}
1858+
)
18421859
} else {
18431860
funded_channel.commitment_signed(msg, logger)
1844-
.map(|monitor_update_opt| (None, monitor_update_opt, None))
1861+
.map(|monitor_update_opt|
1862+
if let Some(monitor_update) = monitor_update_opt {
1863+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update)
1864+
} else {
1865+
CommitmentSignedResult::None
1866+
}
1867+
)
18451868
};
18461869

18471870
#[cfg(not(splicing))]
18481871
let res = funded_channel.commitment_signed(msg, logger)
1849-
.map(|monitor_update_opt| (None, monitor_update_opt, None));
1872+
.map(|monitor_update_opt|
1873+
if let Some(monitor_update) = monitor_update_opt {
1874+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update)
1875+
} else {
1876+
CommitmentSignedResult::None
1877+
}
1878+
);
18501879

18511880
self.phase = ChannelPhase::Funded(funded_channel);
18521881
res

lightning/src/ln/channelmanager.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ use crate::util::ser::{
130130
};
131131
use crate::util::wakers::{Future, Notifier};
132132

133+
use super::channel::CommitmentSignedResult;
133134
#[cfg(all(test, async_payments))]
134135
use crate::blinded_path::payment::BlindedPaymentPath;
136+
135137
#[cfg(async_payments)]
136138
use {
137139
crate::blinded_path::message::BlindedMessagePath,
@@ -9630,27 +9632,32 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96309632
let chan = chan_entry.get_mut();
96319633
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
96329634
let funding_txo = chan.funding().get_funding_txo();
9633-
let (monitor_opt, monitor_update_opt, _) = try_channel_entry!(
9635+
let res = try_channel_entry!(
96349636
self, peer_state, chan.commitment_signed(msg, best_block, &self.signer_provider, &&logger),
96359637
chan_entry);
96369638

96379639
if let Some(chan) = chan.as_funded_mut() {
9638-
if let Some(monitor) = monitor_opt {
9639-
let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
9640-
if let Ok(persist_state) = monitor_res {
9641-
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
9642-
per_peer_state, chan, INITIAL_MONITOR);
9643-
} else {
9644-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9645-
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
9646-
let msg = "Channel ID was a duplicate";
9647-
let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
9648-
let err = ChannelError::Close((msg.to_owned(), reason));
9649-
try_channel_entry!(self, peer_state, Err(err), chan_entry)
9650-
}
9651-
} else if let Some(monitor_update) = monitor_update_opt {
9652-
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
9653-
peer_state, per_peer_state, chan);
9640+
let monitor = match res {
9641+
CommitmentSignedResult::ChannelMonitor(monitor) => monitor,
9642+
CommitmentSignedResult::ChannelMonitorUpdate(monitor_update) => {
9643+
handle_new_monitor_update!(self, funding_txo.unwrap(), monitor_update, peer_state_lock,
9644+
peer_state, per_peer_state, chan);
9645+
return Ok(());
9646+
},
9647+
CommitmentSignedResult::None => return Ok(()),
9648+
};
9649+
9650+
let monitor_res = self.chain_monitor.watch_channel(monitor.channel_id(), monitor);
9651+
if let Ok(persist_state) = monitor_res {
9652+
handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
9653+
per_peer_state, chan, INITIAL_MONITOR);
9654+
} else {
9655+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9656+
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
9657+
let msg = "Channel ID was a duplicate";
9658+
let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
9659+
let err = ChannelError::Close((msg.to_owned(), reason));
9660+
try_channel_entry!(self, peer_state, Err(err), chan_entry)
96549661
}
96559662
}
96569663
Ok(())

0 commit comments

Comments
 (0)