@@ -1453,15 +1453,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
14531453 /// [`msgs::RevokeAndACK`] message from the counterparty.
14541454 sent_message_awaiting_response: Option<usize>,
14551455
1456- #[cfg(any(test, fuzzing))]
1457- // When we receive an HTLC fulfill on an outbound path, we may immediately fulfill the
1458- // corresponding HTLC on the inbound path. If, then, the outbound path channel is
1459- // disconnected and reconnected (before we've exchange commitment_signed and revoke_and_ack
1460- // messages), they may re-broadcast their update_fulfill_htlc, causing a duplicate claim. This
1461- // is fine, but as a sanity check in our failure to generate the second claim, we check here
1462- // that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
1463- historical_inbound_htlc_fulfills: HashSet<u64>,
1464-
14651456 /// This channel's type, as negotiated during channel open
14661457 channel_type: ChannelTypeFeatures,
14671458
@@ -2210,9 +2201,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
22102201 funding_tx_broadcast_safe_event_emitted: false,
22112202 channel_ready_event_emitted: false,
22122203
2213- #[cfg(any(test, fuzzing))]
2214- historical_inbound_htlc_fulfills: new_hash_set(),
2215-
22162204 channel_type,
22172205 channel_keys_id,
22182206
@@ -2443,9 +2431,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24432431 funding_tx_broadcast_safe_event_emitted: false,
24442432 channel_ready_event_emitted: false,
24452433
2446- #[cfg(any(test, fuzzing))]
2447- historical_inbound_htlc_fulfills: new_hash_set(),
2448-
24492434 channel_type,
24502435 channel_keys_id,
24512436
@@ -4472,10 +4457,6 @@ impl<SP: Deref> Channel<SP> where
44724457 }
44734458 }
44744459 if pending_idx == core::usize::MAX {
4475- #[cfg(any(test, fuzzing))]
4476- // If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
4477- // this is simply a duplicate claim, not previously failed and we lost funds.
4478- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
44794460 return UpdateFulfillFetch::DuplicateClaim {};
44804461 }
44814462
@@ -4505,8 +4486,6 @@ impl<SP: Deref> Channel<SP> where
45054486 if htlc_id_arg == htlc_id {
45064487 // Make sure we don't leave latest_monitor_update_id incremented here:
45074488 self.context.latest_monitor_update_id -= 1;
4508- #[cfg(any(test, fuzzing))]
4509- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
45104489 return UpdateFulfillFetch::DuplicateClaim {};
45114490 }
45124491 },
@@ -4528,12 +4507,8 @@ impl<SP: Deref> Channel<SP> where
45284507 self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
45294508 payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
45304509 });
4531- #[cfg(any(test, fuzzing))]
4532- self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
45334510 return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
45344511 }
4535- #[cfg(any(test, fuzzing))]
4536- self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
45374512
45384513 {
45394514 let htlc = &mut self.context.pending_inbound_htlcs[pending_idx];
@@ -4598,14 +4573,8 @@ impl<SP: Deref> Channel<SP> where
45984573 }
45994574 }
46004575
4601- /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
4602- /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
4603- /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
4604- /// before we fail backwards.
4605- ///
4606- /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
4607- /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
4608- /// [`ChannelError::Ignore`].
4576+ /// Returns `Err` (always with [`ChannelError::Ignore`]) if the HTLC could not be failed (e.g.
4577+ /// if it was already resolved). Otherwise returns `Ok`.
46094578 pub fn queue_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L)
46104579 -> Result<(), ChannelError> where L::Target: Logger {
46114580 self.fail_htlc(htlc_id_arg, err_packet, true, logger)
@@ -4623,14 +4592,8 @@ impl<SP: Deref> Channel<SP> where
46234592 .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
46244593 }
46254594
4626- /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
4627- /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
4628- /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
4629- /// before we fail backwards.
4630- ///
4631- /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
4632- /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
4633- /// [`ChannelError::Ignore`].
4595+ /// Returns `Err` (always with [`ChannelError::Ignore`]) if the HTLC could not be failed (e.g.
4596+ /// if it was already resolved). Otherwise returns `Ok`.
46344597 fn fail_htlc<L: Deref, E: FailHTLCContents + Clone>(
46354598 &mut self, htlc_id_arg: u64, err_contents: E, mut force_holding_cell: bool,
46364599 logger: &L
@@ -4648,12 +4611,8 @@ impl<SP: Deref> Channel<SP> where
46484611 if htlc.htlc_id == htlc_id_arg {
46494612 match htlc.state {
46504613 InboundHTLCState::Committed => {},
4651- InboundHTLCState::LocalRemoved(ref reason) => {
4652- if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
4653- } else {
4654- debug_assert!(false, "Tried to fail an HTLC that was already failed");
4655- }
4656- return Ok(None);
4614+ InboundHTLCState::LocalRemoved(_) => {
4615+ return Err(ChannelError::Ignore(format!("HTLC {} was already resolved", htlc.htlc_id)));
46574616 },
46584617 _ => {
46594618 debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
@@ -4664,11 +4623,7 @@ impl<SP: Deref> Channel<SP> where
46644623 }
46654624 }
46664625 if pending_idx == core::usize::MAX {
4667- #[cfg(any(test, fuzzing))]
4668- // If we failed to find an HTLC to fail, make sure it was previously fulfilled and this
4669- // is simply a duplicate fail, not previously failed and we failed-back too early.
4670- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
4671- return Ok(None);
4626+ return Err(ChannelError::Ignore(format!("Unable to find a pending HTLC which matched the given HTLC ID ({})", htlc_id_arg)));
46724627 }
46734628
46744629 if !self.context.channel_state.can_generate_new_commitment() {
@@ -4682,17 +4637,14 @@ impl<SP: Deref> Channel<SP> where
46824637 match pending_update {
46834638 &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
46844639 if htlc_id_arg == htlc_id {
4685- #[cfg(any(test, fuzzing))]
4686- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
4687- return Ok(None);
4640+ return Err(ChannelError::Ignore(format!("HTLC {} was already claimed!", htlc_id)));
46884641 }
46894642 },
46904643 &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } |
46914644 &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, .. } =>
46924645 {
46934646 if htlc_id_arg == htlc_id {
4694- debug_assert!(false, "Tried to fail an HTLC that was already failed");
4695- return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
4647+ return Err(ChannelError::Ignore(format!("HTLC {} was already pending failure", htlc_id)));
46964648 }
46974649 },
46984650 _ => {}
@@ -9543,13 +9495,6 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
95439495
95449496 self.context.channel_update_status.write(writer)?;
95459497
9546- #[cfg(any(test, fuzzing))]
9547- (self.context.historical_inbound_htlc_fulfills.len() as u64).write(writer)?;
9548- #[cfg(any(test, fuzzing))]
9549- for htlc in self.context.historical_inbound_htlc_fulfills.iter() {
9550- htlc.write(writer)?;
9551- }
9552-
95539498 // If the channel type is something other than only-static-remote-key, then we need to have
95549499 // older clients fail to deserialize this channel at all. If the type is
95559500 // only-static-remote-key, we simply consider it "default" and don't write the channel type
@@ -9883,16 +9828,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
98839828
98849829 let channel_update_status = Readable::read(reader)?;
98859830
9886- #[cfg(any(test, fuzzing))]
9887- let mut historical_inbound_htlc_fulfills = new_hash_set();
9888- #[cfg(any(test, fuzzing))]
9889- {
9890- let htlc_fulfills_len: u64 = Readable::read(reader)?;
9891- for _ in 0..htlc_fulfills_len {
9892- assert!(historical_inbound_htlc_fulfills.insert(Readable::read(reader)?));
9893- }
9894- }
9895-
98969831 let pending_update_fee = if let Some(feerate) = pending_update_fee_value {
98979832 Some((feerate, if channel_parameters.is_outbound_from_holder {
98989833 FeeUpdateState::Outbound
@@ -10233,9 +10168,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1023310168 channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
1023410169 channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
1023510170
10236- #[cfg(any(test, fuzzing))]
10237- historical_inbound_htlc_fulfills,
10238-
1023910171 channel_type: channel_type.unwrap(),
1024010172 channel_keys_id,
1024110173
0 commit comments