Skip to content

Commit 735456e

Browse files
committed
Simplify call graph of get_update_fulfill_htlc since it can't Err.
1 parent 40b7615 commit 735456e

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

lightning/src/ln/channel.rs

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

1235-
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1236-
/// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1237-
/// return Ok(_) if debug assertions are turned on or preconditions are met.
1238-
///
1239-
/// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1240-
/// but then have a reorg which settles on an HTLC-failure on chain.
1241-
fn get_update_fulfill_htlc<L: Deref>(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage, logger: &L) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitorUpdate>), ChannelError> 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<msgs::UpdateFulfillHTLC>, Option<ChannelMonitorUpdate>) where L::Target: Logger {
12421236
// Either ChannelFunded got set (which means it won't be unset) or there is no way any
12431237
// caller thought we could have something claimed (cause we wouldn't have accepted in an
12441238
// incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -1266,7 +1260,7 @@ impl<Signer: Sign> Channel<Signer> {
12661260
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()));
12671261
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
12681262
}
1269-
return Ok((None, None));
1263+
return (None, None);
12701264
},
12711265
_ => {
12721266
debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
@@ -1282,7 +1276,7 @@ impl<Signer: Sign> Channel<Signer> {
12821276
// If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
12831277
// this is simply a duplicate claim, not previously failed and we lost funds.
12841278
debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
1285-
return Ok((None, None));
1279+
return (None, None);
12861280
}
12871281

12881282
// Now update local state:
@@ -1306,7 +1300,7 @@ impl<Signer: Sign> Channel<Signer> {
13061300
self.latest_monitor_update_id -= 1;
13071301
#[cfg(any(test, feature = "fuzztarget"))]
13081302
debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
1309-
return Ok((None, None));
1303+
return (None, None);
13101304
}
13111305
},
13121306
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
@@ -1315,7 +1309,7 @@ impl<Signer: Sign> Channel<Signer> {
13151309
// TODO: We may actually be able to switch to a fulfill here, though its
13161310
// rare enough it may not be worth the complexity burden.
13171311
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
1318-
return Ok((None, Some(monitor_update)));
1312+
return (None, Some(monitor_update));
13191313
}
13201314
},
13211315
_ => {}
@@ -1327,7 +1321,7 @@ impl<Signer: Sign> Channel<Signer> {
13271321
});
13281322
#[cfg(any(test, feature = "fuzztarget"))]
13291323
self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
1330-
return Ok((None, Some(monitor_update)));
1324+
return (None, Some(monitor_update));
13311325
}
13321326
#[cfg(any(test, feature = "fuzztarget"))]
13331327
self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
@@ -1337,21 +1331,21 @@ impl<Signer: Sign> Channel<Signer> {
13371331
if let InboundHTLCState::Committed = htlc.state {
13381332
} else {
13391333
debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
1340-
return Ok((None, Some(monitor_update)));
1334+
return (None, Some(monitor_update));
13411335
}
13421336
log_trace!(logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id));
13431337
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
13441338
}
13451339

1346-
Ok((Some(msgs::UpdateFulfillHTLC {
1340+
(Some(msgs::UpdateFulfillHTLC {
13471341
channel_id: self.channel_id(),
13481342
htlc_id: htlc_id_arg,
13491343
payment_preimage: payment_preimage_arg,
1350-
}), Some(monitor_update)))
1344+
}), Some(monitor_update))
13511345
}
13521346

13531347
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 {
1354-
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger)? {
1348+
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
13551349
(Some(update_fulfill_htlc), Some(mut monitor_update)) => {
13561350
let (commitment, mut additional_update) = self.send_commitment_no_status_check(logger)?;
13571351
// send_commitment_no_status_check may bump latest_monitor_id but we want them to be
@@ -1360,21 +1354,22 @@ impl<Signer: Sign> Channel<Signer> {
13601354
monitor_update.updates.append(&mut additional_update.updates);
13611355
Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
13621356
},
1363-
(Some(update_fulfill_htlc), None) => {
1364-
let (commitment, monitor_update) = self.send_commitment_no_status_check(logger)?;
1365-
Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
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!();
13661361
},
13671362
(None, Some(monitor_update)) => Ok((None, Some(monitor_update))),
13681363
(None, None) => Ok((None, None))
13691364
}
13701365
}
13711366

1372-
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1373-
/// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1374-
/// return Ok(_) if debug assertions are turned on or preconditions are met.
1375-
///
1376-
/// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1377-
/// but then have a reorg which settles on an HTLC-failure on chain.
1367+
/// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
1368+
/// and HTLC more than once or fulfill once and then attempt to fail after reconnect. We
1369+
/// cannot, however, fail more than once as we wait for an upstream failure to be irrevocably
1370+
/// committed before we fail backwards.
1371+
/// If we do fail twice, we debug_assert!(false) and return a ChannelError::Ignore. Thus, will
1372+
/// always return Ok(_) if debug assertions are turned on or preconditions are met.
13781373
pub fn get_update_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L) -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> where L::Target: Logger {
13791374
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
13801375
panic!("Was asked to fail an HTLC when channel was not in an operational state");
@@ -2468,19 +2463,10 @@ impl<Signer: Sign> Channel<Signer> {
24682463
}
24692464
},
24702465
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
2471-
match self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger) {
2472-
Ok((update_fulfill_msg_option, additional_monitor_update_opt)) => {
2473-
update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap());
2474-
if let Some(mut additional_monitor_update) = additional_monitor_update_opt {
2475-
monitor_update.updates.append(&mut additional_monitor_update.updates);
2476-
}
2477-
},
2478-
Err(e) => {
2479-
if let ChannelError::Ignore(_) = e {}
2480-
else {
2481-
panic!("Got a non-IgnoreError action trying to fulfill holding cell HTLC");
2482-
}
2483-
}
2466+
let (update_fulfill_msg_option, additional_monitor_update_opt) = self.get_update_fulfill_htlc(htlc_id, *payment_preimage, logger);
2467+
update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap());
2468+
if let Some(mut additional_monitor_update) = additional_monitor_update_opt {
2469+
monitor_update.updates.append(&mut additional_monitor_update.updates);
24842470
}
24852471
},
24862472
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, ref err_packet } => {

0 commit comments

Comments
 (0)