Skip to content

Commit 78283a0

Browse files
committed
Simplify call graph of get_update_fulfill_htlc since it can't Err.
1 parent 99c701d commit 78283a0

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
@@ -1231,13 +1231,7 @@ impl<Signer: Sign> Channel<Signer> {
12311231
make_funding_redeemscript(&self.get_holder_pubkeys().funding_pubkey, self.counterparty_funding_pubkey())
12321232
}
12331233

1234-
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1235-
/// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1236-
/// return Ok(_) if debug assertions are turned on or preconditions are met.
1237-
///
1238-
/// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1239-
/// but then have a reorg which settles on an HTLC-failure on chain.
1240-
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 {
1234+
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 {
12411235
// Either ChannelFunded got set (which means it won't be unset) or there is no way any
12421236
// caller thought we could have something claimed (cause we wouldn't have accepted in an
12431237
// incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -1265,7 +1259,7 @@ impl<Signer: Sign> Channel<Signer> {
12651259
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()));
12661260
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
12671261
}
1268-
return Ok((None, None));
1262+
return (None, None);
12691263
},
12701264
_ => {
12711265
debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
@@ -1281,7 +1275,7 @@ impl<Signer: Sign> Channel<Signer> {
12811275
// If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
12821276
// this is simply a duplicate claim, not previously failed and we lost funds.
12831277
debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
1284-
return Ok((None, None));
1278+
return (None, None);
12851279
}
12861280

12871281
// Now update local state:
@@ -1305,7 +1299,7 @@ impl<Signer: Sign> Channel<Signer> {
13051299
self.latest_monitor_update_id -= 1;
13061300
#[cfg(any(test, feature = "fuzztarget"))]
13071301
debug_assert!(self.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
1308-
return Ok((None, None));
1302+
return (None, None);
13091303
}
13101304
},
13111305
&HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
@@ -1314,7 +1308,7 @@ impl<Signer: Sign> Channel<Signer> {
13141308
// TODO: We may actually be able to switch to a fulfill here, though its
13151309
// rare enough it may not be worth the complexity burden.
13161310
debug_assert!(false, "Tried to fulfill an HTLC that was already failed");
1317-
return Ok((None, Some(monitor_update)));
1311+
return (None, Some(monitor_update));
13181312
}
13191313
},
13201314
_ => {}
@@ -1326,7 +1320,7 @@ impl<Signer: Sign> Channel<Signer> {
13261320
});
13271321
#[cfg(any(test, feature = "fuzztarget"))]
13281322
self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
1329-
return Ok((None, Some(monitor_update)));
1323+
return (None, Some(monitor_update));
13301324
}
13311325
#[cfg(any(test, feature = "fuzztarget"))]
13321326
self.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
@@ -1336,21 +1330,21 @@ impl<Signer: Sign> Channel<Signer> {
13361330
if let InboundHTLCState::Committed = htlc.state {
13371331
} else {
13381332
debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
1339-
return Ok((None, Some(monitor_update)));
1333+
return (None, Some(monitor_update));
13401334
}
13411335
log_trace!(logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill in channel {}!", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id));
13421336
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(payment_preimage_arg.clone()));
13431337
}
13441338

1345-
Ok((Some(msgs::UpdateFulfillHTLC {
1339+
(Some(msgs::UpdateFulfillHTLC {
13461340
channel_id: self.channel_id(),
13471341
htlc_id: htlc_id_arg,
13481342
payment_preimage: payment_preimage_arg,
1349-
}), Some(monitor_update)))
1343+
}), Some(monitor_update))
13501344
}
13511345

13521346
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 {
1353-
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger)? {
1347+
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
13541348
(Some(update_fulfill_htlc), Some(mut monitor_update)) => {
13551349
let (commitment, mut additional_update) = self.send_commitment_no_status_check(logger)?;
13561350
// send_commitment_no_status_check may bump latest_monitor_id but we want them to be
@@ -1359,21 +1353,22 @@ impl<Signer: Sign> Channel<Signer> {
13591353
monitor_update.updates.append(&mut additional_update.updates);
13601354
Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
13611355
},
1362-
(Some(update_fulfill_htlc), None) => {
1363-
let (commitment, monitor_update) = self.send_commitment_no_status_check(logger)?;
1364-
Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
1356+
(Some(_), None) => {
1357+
// If we generated a claim message, we absolutely should have generated a
1358+
// ChannelMonitorUpdate, otherwise we are going to probably lose funds.
1359+
unreachable!();
13651360
},
13661361
(None, Some(monitor_update)) => Ok((None, Some(monitor_update))),
13671362
(None, None) => Ok((None, None))
13681363
}
13691364
}
13701365

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

0 commit comments

Comments
 (0)