Skip to content

Commit 1a1ca9c

Browse files
committed
f further simplify the return value of get_update_fulfill_htlc
1 parent 098d01c commit 1a1ca9c

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

lightning/src/ln/channel.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ impl<Signer: Sign> Channel<Signer> {
12321232
make_funding_redeemscript(&self.get_holder_pubkeys().funding_pubkey, self.counterparty_funding_pubkey())
12331233
}
12341234

1235-
fn get_update_fulfill_htlc<L: Deref>(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L) -> (Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitorUpdate>) where L::Target: Logger {
1235+
fn get_update_fulfill_htlc<L: Deref>(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L) -> Option<(Option<msgs::UpdateFulfillHTLC>, ChannelMonitorUpdate)> where L::Target: Logger {
12361236
// Either ChannelFunded got set (which means it won't be unset) or there is no way any
12371237
// caller thought we could have something claimed (cause we wouldn't have accepted in an
12381238
// incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -1260,7 +1260,7 @@ impl<Signer: Sign> Channel<Signer> {
12601260
log_warn!(logger, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id()));
12611261
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
12621262
}
1263-
return (None, None);
1263+
return None;
12641264
},
12651265
_ => {
12661266
debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
@@ -1276,7 +1276,7 @@ impl<Signer: Sign> Channel<Signer> {
12761276
// If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
12771277
// this is simply a duplicate claim, not previously failed and we lost funds.
12781278
debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
1279-
return (None, None);
1279+
return None;
12801280
}
12811281

12821282
// Now update local state:
@@ -1300,7 +1300,7 @@ impl<Signer: Sign> Channel<Signer> {
13001300
self.latest_monitor_update_id -= 1;
13011301
#[cfg(any(test, feature = "fuzztarget"))]
13021302
debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
1303-
return (None, None);
1303+
return None;
13041304
}
13051305
},
13061306
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
@@ -1309,7 +1309,7 @@ impl<Signer: Sign> Channel<Signer> {
13091309
// TODO: We may actually be able to switch to a fulfill here, though its
13101310
// rare enough it may not be worth the complexity burden.
13111311
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
1312-
return (None, Some(monitor_update));
1312+
return Some((None, monitor_update));
13131313
}
13141314
},
13151315
_ => {}
@@ -1321,7 +1321,7 @@ impl<Signer: Sign> Channel<Signer> {
13211321
});
13221322
#[cfg(any(test, feature = "fuzztarget"))]
13231323
self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
1324-
return (None, Some(monitor_update));
1324+
return Some((None, monitor_update));
13251325
}
13261326
#[cfg(any(test, feature = "fuzztarget"))]
13271327
self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
@@ -1331,36 +1331,31 @@ impl<Signer: Sign> Channel<Signer> {
13311331
if let InboundHTLCState::Committed = htlc.state {
13321332
} else {
13331333
debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
1334-
return (None, Some(monitor_update));
1334+
return Some((None, monitor_update));
13351335
}
13361336
log_trace!(logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id));
13371337
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
13381338
}
13391339

1340-
(Some(msgs::UpdateFulfillHTLC {
1340+
Some((Some(msgs::UpdateFulfillHTLC {
13411341
channel_id: self.channel_id(),
13421342
htlc_id: htlc_id_arg,
13431343
payment_preimage: payment_preimage_arg,
1344-
}), Some(monitor_update))
1344+
}), monitor_update))
13451345
}
13461346

1347-
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitorUpdate>), ChannelError> where L::Target: Logger {
1347+
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> Result<Option<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, ChannelMonitorUpdate)>, ChannelError> where L::Target: Logger {
13481348
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
1349-
(Some(update_fulfill_htlc), Some(mut monitor_update)) => {
1349+
Some((Some(update_fulfill_htlc), mut monitor_update)) => {
13501350
let (commitment, mut additional_update) = self.send_commitment_no_status_check(logger)?;
13511351
// send_commitment_no_status_check may bump latest_monitor_id but we want them to be
13521352
// strictly increasing by one, so decrement it here.
13531353
self.latest_monitor_update_id = monitor_update.update_id;
13541354
monitor_update.updates.append(&mut additional_update.updates);
1355-
Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
1356-
},
1357-
(Some(_), None) => {
1358-
// If we generated a claim message, we absolutely should have generated a
1359-
// ChannelMonitorUpdate, otherwise we are going to probably lose funds.
1360-
unreachable!();
1355+
Ok(Some((Some((update_fulfill_htlc, commitment)), monitor_update)))
13611356
},
1362-
(None, Some(monitor_update)) => Ok((None, Some(monitor_update))),
1363-
(None, None) => Ok((None, None))
1357+
Some((None, monitor_update)) => Ok(Some((None, monitor_update))),
1358+
None => Ok(None)
13641359
}
13651360
}
13661361

@@ -2468,11 +2463,9 @@ impl<Signer: Sign> Channel<Signer> {
24682463
// not fail - any in between attempts to claim the HTLC will have resulted
24692464
// in it hitting the holding cell again and we cannot change the state of a
24702465
// holding cell HTLC from fulfill to anything else.
2471-
let (update_fulfill_msg_option, additional_monitor_update_opt) = self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger);
2466+
let (update_fulfill_msg_option, mut additional_monitor_update) = self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger).unwrap();
24722467
update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap());
2473-
if let Some(mut additional_monitor_update) = additional_monitor_update_opt {
2474-
monitor_update.updates.append(&mut additional_monitor_update.updates);
2475-
}
2468+
monitor_update.updates.append(&mut additional_monitor_update.updates);
24762469
},
24772470
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {
24782471
match self.get_update_fail_htlc(htlc_id, err_packet.clone(), logger) {

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,30 +2681,30 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
26812681
if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(chan_id) {
26822682
let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
26832683
match chan.get_mut().get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger) {
2684-
Ok((msgs, monitor_option)) => {
2685-
if let Some(monitor_update) = monitor_option {
2684+
Ok(msgs_monitor_option) => {
2685+
if let Some((msgs, monitor_update)) = msgs_monitor_option {
26862686
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
26872687
if was_frozen_for_monitor {
26882688
assert!(msgs.is_none());
26892689
} else {
26902690
return Err(Some((chan.get().get_counterparty_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err())));
26912691
}
26922692
}
2693-
}
2694-
if let Some((msg, commitment_signed)) = msgs {
2695-
log_debug!(self.logger, "Claiming funds for HTLC with preimage {} resulted in a commitment_signed for channel {}",
2696-
log_bytes!(payment_preimage.0), log_bytes!(chan.get().channel_id()));
2697-
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2698-
node_id: chan.get().get_counterparty_node_id(),
2699-
updates: msgs::CommitmentUpdate {
2700-
update_add_htlcs: Vec::new(),
2701-
update_fulfill_htlcs: vec![msg],
2702-
update_fail_htlcs: Vec::new(),
2703-
update_fail_malformed_htlcs: Vec::new(),
2704-
update_fee: None,
2705-
commitment_signed,
2706-
}
2707-
});
2693+
if let Some((msg, commitment_signed)) = msgs {
2694+
log_debug!(self.logger, "Claiming funds for HTLC with preimage {} resulted in a commitment_signed for channel {}",
2695+
log_bytes!(payment_preimage.0), log_bytes!(chan.get().channel_id()));
2696+
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
2697+
node_id: chan.get().get_counterparty_node_id(),
2698+
updates: msgs::CommitmentUpdate {
2699+
update_add_htlcs: Vec::new(),
2700+
update_fulfill_htlcs: vec![msg],
2701+
update_fail_htlcs: Vec::new(),
2702+
update_fail_malformed_htlcs: Vec::new(),
2703+
update_fee: None,
2704+
commitment_signed,
2705+
}
2706+
});
2707+
}
27082708
}
27092709
return Ok(())
27102710
},

0 commit comments

Comments
 (0)