@@ -1693,15 +1693,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
16931693 /// [`msgs::RevokeAndACK`] message from the counterparty.
16941694 sent_message_awaiting_response: Option<usize>,
16951695
1696- #[cfg(any(test, fuzzing))]
1697- // When we receive an HTLC fulfill on an outbound path, we may immediately fulfill the
1698- // corresponding HTLC on the inbound path. If, then, the outbound path channel is
1699- // disconnected and reconnected (before we've exchange commitment_signed and revoke_and_ack
1700- // messages), they may re-broadcast their update_fulfill_htlc, causing a duplicate claim. This
1701- // is fine, but as a sanity check in our failure to generate the second claim, we check here
1702- // that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
1703- historical_inbound_htlc_fulfills: HashSet<u64>,
1704-
17051696 /// This channel's type, as negotiated during channel open
17061697 channel_type: ChannelTypeFeatures,
17071698
@@ -2403,9 +2394,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24032394 funding_tx_broadcast_safe_event_emitted: false,
24042395 channel_ready_event_emitted: false,
24052396
2406- #[cfg(any(test, fuzzing))]
2407- historical_inbound_htlc_fulfills: new_hash_set(),
2408-
24092397 channel_type,
24102398 channel_keys_id,
24112399
@@ -2636,9 +2624,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
26362624 funding_tx_broadcast_safe_event_emitted: false,
26372625 channel_ready_event_emitted: false,
26382626
2639- #[cfg(any(test, fuzzing))]
2640- historical_inbound_htlc_fulfills: new_hash_set(),
2641-
26422627 channel_type,
26432628 channel_keys_id,
26442629
@@ -4665,10 +4650,6 @@ impl<SP: Deref> FundedChannel<SP> where
46654650 }
46664651 }
46674652 if pending_idx == core::usize::MAX {
4668- #[cfg(any(test, fuzzing))]
4669- // If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
4670- // this is simply a duplicate claim, not previously failed and we lost funds.
4671- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
46724653 return UpdateFulfillFetch::DuplicateClaim {};
46734654 }
46744655
@@ -4698,8 +4679,6 @@ impl<SP: Deref> FundedChannel<SP> where
46984679 if htlc_id_arg == htlc_id {
46994680 // Make sure we don't leave latest_monitor_update_id incremented here:
47004681 self.context.latest_monitor_update_id -= 1;
4701- #[cfg(any(test, fuzzing))]
4702- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
47034682 return UpdateFulfillFetch::DuplicateClaim {};
47044683 }
47054684 },
@@ -4721,12 +4700,8 @@ impl<SP: Deref> FundedChannel<SP> where
47214700 self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
47224701 payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
47234702 });
4724- #[cfg(any(test, fuzzing))]
4725- self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
47264703 return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
47274704 }
4728- #[cfg(any(test, fuzzing))]
4729- self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
47304705
47314706 {
47324707 let htlc = &mut self.context.pending_inbound_htlcs[pending_idx];
@@ -4791,14 +4766,8 @@ impl<SP: Deref> FundedChannel<SP> where
47914766 }
47924767 }
47934768
4794- /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
4795- /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
4796- /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
4797- /// before we fail backwards.
4798- ///
4799- /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
4800- /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
4801- /// [`ChannelError::Ignore`].
4769+ /// Returns `Err` (always with [`ChannelError::Ignore`]) if the HTLC could not be failed (e.g.
4770+ /// if it was already resolved). Otherwise returns `Ok`.
48024771 pub fn queue_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L)
48034772 -> Result<(), ChannelError> where L::Target: Logger {
48044773 self.fail_htlc(htlc_id_arg, err_packet, true, logger)
@@ -4816,14 +4785,8 @@ impl<SP: Deref> FundedChannel<SP> where
48164785 .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
48174786 }
48184787
4819- /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
4820- /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
4821- /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
4822- /// before we fail backwards.
4823- ///
4824- /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
4825- /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
4826- /// [`ChannelError::Ignore`].
4788+ /// Returns `Err` (always with [`ChannelError::Ignore`]) if the HTLC could not be failed (e.g.
4789+ /// if it was already resolved). Otherwise returns `Ok`.
48274790 fn fail_htlc<L: Deref, E: FailHTLCContents + Clone>(
48284791 &mut self, htlc_id_arg: u64, err_contents: E, mut force_holding_cell: bool,
48294792 logger: &L
@@ -4841,12 +4804,8 @@ impl<SP: Deref> FundedChannel<SP> where
48414804 if htlc.htlc_id == htlc_id_arg {
48424805 match htlc.state {
48434806 InboundHTLCState::Committed => {},
4844- InboundHTLCState::LocalRemoved(ref reason) => {
4845- if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
4846- } else {
4847- debug_assert!(false, "Tried to fail an HTLC that was already failed");
4848- }
4849- return Ok(None);
4807+ InboundHTLCState::LocalRemoved(_) => {
4808+ return Err(ChannelError::Ignore(format!("HTLC {} was already resolved", htlc.htlc_id)));
48504809 },
48514810 _ => {
48524811 debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
@@ -4857,11 +4816,7 @@ impl<SP: Deref> FundedChannel<SP> where
48574816 }
48584817 }
48594818 if pending_idx == core::usize::MAX {
4860- #[cfg(any(test, fuzzing))]
4861- // If we failed to find an HTLC to fail, make sure it was previously fulfilled and this
4862- // is simply a duplicate fail, not previously failed and we failed-back too early.
4863- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
4864- return Ok(None);
4819+ return Err(ChannelError::Ignore(format!("Unable to find a pending HTLC which matched the given HTLC ID ({})", htlc_id_arg)));
48654820 }
48664821
48674822 if !self.context.channel_state.can_generate_new_commitment() {
@@ -4875,17 +4830,14 @@ impl<SP: Deref> FundedChannel<SP> where
48754830 match pending_update {
48764831 &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
48774832 if htlc_id_arg == htlc_id {
4878- #[cfg(any(test, fuzzing))]
4879- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
4880- return Ok(None);
4833+ return Err(ChannelError::Ignore(format!("HTLC {} was already claimed!", htlc_id)));
48814834 }
48824835 },
48834836 &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } |
48844837 &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, .. } =>
48854838 {
48864839 if htlc_id_arg == htlc_id {
4887- debug_assert!(false, "Tried to fail an HTLC that was already failed");
4888- return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
4840+ return Err(ChannelError::Ignore(format!("HTLC {} was already pending failure", htlc_id)));
48894841 }
48904842 },
48914843 _ => {}
@@ -9718,13 +9670,6 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
97189670
97199671 self.context.channel_update_status.write(writer)?;
97209672
9721- #[cfg(any(test, fuzzing))]
9722- (self.context.historical_inbound_htlc_fulfills.len() as u64).write(writer)?;
9723- #[cfg(any(test, fuzzing))]
9724- for htlc in self.context.historical_inbound_htlc_fulfills.iter() {
9725- htlc.write(writer)?;
9726- }
9727-
97289673 // If the channel type is something other than only-static-remote-key, then we need to have
97299674 // older clients fail to deserialize this channel at all. If the type is
97309675 // only-static-remote-key, we simply consider it "default" and don't write the channel type
@@ -10058,16 +10003,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1005810003
1005910004 let channel_update_status = Readable::read(reader)?;
1006010005
10061- #[cfg(any(test, fuzzing))]
10062- let mut historical_inbound_htlc_fulfills = new_hash_set();
10063- #[cfg(any(test, fuzzing))]
10064- {
10065- let htlc_fulfills_len: u64 = Readable::read(reader)?;
10066- for _ in 0..htlc_fulfills_len {
10067- assert!(historical_inbound_htlc_fulfills.insert(Readable::read(reader)?));
10068- }
10069- }
10070-
1007110006 let pending_update_fee = if let Some(feerate) = pending_update_fee_value {
1007210007 Some((feerate, if channel_parameters.is_outbound_from_holder {
1007310008 FeeUpdateState::Outbound
@@ -10408,9 +10343,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1040810343 channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
1040910344 channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
1041010345
10411- #[cfg(any(test, fuzzing))]
10412- historical_inbound_htlc_fulfills,
10413-
1041410346 channel_type: channel_type.unwrap(),
1041510347 channel_keys_id,
1041610348
0 commit comments