@@ -1790,15 +1790,6 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
17901790 /// [`msgs::RevokeAndACK`] message from the counterparty.
17911791 sent_message_awaiting_response: Option<usize>,
17921792
1793- #[cfg(any(test, fuzzing))]
1794- // When we receive an HTLC fulfill on an outbound path, we may immediately fulfill the
1795- // corresponding HTLC on the inbound path. If, then, the outbound path channel is
1796- // disconnected and reconnected (before we've exchange commitment_signed and revoke_and_ack
1797- // messages), they may re-broadcast their update_fulfill_htlc, causing a duplicate claim. This
1798- // is fine, but as a sanity check in our failure to generate the second claim, we check here
1799- // that the original was a claim, and that we aren't now trying to fulfill a failed HTLC.
1800- historical_inbound_htlc_fulfills: HashSet<u64>,
1801-
18021793 /// This channel's type, as negotiated during channel open
18031794 channel_type: ChannelTypeFeatures,
18041795
@@ -2518,9 +2509,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
25182509 funding_tx_broadcast_safe_event_emitted: false,
25192510 channel_ready_event_emitted: false,
25202511
2521- #[cfg(any(test, fuzzing))]
2522- historical_inbound_htlc_fulfills: new_hash_set(),
2523-
25242512 channel_type,
25252513 channel_keys_id,
25262514
@@ -2751,9 +2739,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
27512739 funding_tx_broadcast_safe_event_emitted: false,
27522740 channel_ready_event_emitted: false,
27532741
2754- #[cfg(any(test, fuzzing))]
2755- historical_inbound_htlc_fulfills: new_hash_set(),
2756-
27572742 channel_type,
27582743 channel_keys_id,
27592744
@@ -4784,10 +4769,6 @@ impl<SP: Deref> FundedChannel<SP> where
47844769 }
47854770 }
47864771 if pending_idx == core::usize::MAX {
4787- #[cfg(any(test, fuzzing))]
4788- // If we failed to find an HTLC to fulfill, make sure it was previously fulfilled and
4789- // this is simply a duplicate claim, not previously failed and we lost funds.
4790- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
47914772 return UpdateFulfillFetch::DuplicateClaim {};
47924773 }
47934774
@@ -4817,8 +4798,6 @@ impl<SP: Deref> FundedChannel<SP> where
48174798 if htlc_id_arg == htlc_id {
48184799 // Make sure we don't leave latest_monitor_update_id incremented here:
48194800 self.context.latest_monitor_update_id -= 1;
4820- #[cfg(any(test, fuzzing))]
4821- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
48224801 return UpdateFulfillFetch::DuplicateClaim {};
48234802 }
48244803 },
@@ -4840,12 +4819,8 @@ impl<SP: Deref> FundedChannel<SP> where
48404819 self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
48414820 payment_preimage: payment_preimage_arg, htlc_id: htlc_id_arg,
48424821 });
4843- #[cfg(any(test, fuzzing))]
4844- self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
48454822 return UpdateFulfillFetch::NewClaim { monitor_update, htlc_value_msat, msg: None };
48464823 }
4847- #[cfg(any(test, fuzzing))]
4848- self.context.historical_inbound_htlc_fulfills.insert(htlc_id_arg);
48494824
48504825 {
48514826 let htlc = &mut self.context.pending_inbound_htlcs[pending_idx];
@@ -4910,14 +4885,8 @@ impl<SP: Deref> FundedChannel<SP> where
49104885 }
49114886 }
49124887
4913- /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
4914- /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
4915- /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
4916- /// before we fail backwards.
4917- ///
4918- /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
4919- /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
4920- /// [`ChannelError::Ignore`].
4888+ /// Returns `Err` (always with [`ChannelError::Ignore`]) if the HTLC could not be failed (e.g.
4889+ /// if it was already resolved). Otherwise returns `Ok`.
49214890 pub fn queue_fail_htlc<L: Deref>(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket, logger: &L)
49224891 -> Result<(), ChannelError> where L::Target: Logger {
49234892 self.fail_htlc(htlc_id_arg, err_packet, true, logger)
@@ -4935,14 +4904,8 @@ impl<SP: Deref> FundedChannel<SP> where
49354904 .map(|msg_opt| assert!(msg_opt.is_none(), "We forced holding cell?"))
49364905 }
49374906
4938- /// We can only have one resolution per HTLC. In some cases around reconnect, we may fulfill
4939- /// an HTLC more than once or fulfill once and then attempt to fail after reconnect. We cannot,
4940- /// however, fail more than once as we wait for an upstream failure to be irrevocably committed
4941- /// before we fail backwards.
4942- ///
4943- /// If we do fail twice, we `debug_assert!(false)` and return `Ok(None)`. Thus, this will always
4944- /// return `Ok(_)` if preconditions are met. In any case, `Err`s will only be
4945- /// [`ChannelError::Ignore`].
4907+ /// Returns `Err` (always with [`ChannelError::Ignore`]) if the HTLC could not be failed (e.g.
4908+ /// if it was already resolved). Otherwise returns `Ok`.
49464909 fn fail_htlc<L: Deref, E: FailHTLCContents + Clone>(
49474910 &mut self, htlc_id_arg: u64, err_contents: E, mut force_holding_cell: bool,
49484911 logger: &L
@@ -4960,12 +4923,8 @@ impl<SP: Deref> FundedChannel<SP> where
49604923 if htlc.htlc_id == htlc_id_arg {
49614924 match htlc.state {
49624925 InboundHTLCState::Committed => {},
4963- InboundHTLCState::LocalRemoved(ref reason) => {
4964- if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
4965- } else {
4966- debug_assert!(false, "Tried to fail an HTLC that was already failed");
4967- }
4968- return Ok(None);
4926+ InboundHTLCState::LocalRemoved(_) => {
4927+ return Err(ChannelError::Ignore(format!("HTLC {} was already resolved", htlc.htlc_id)));
49694928 },
49704929 _ => {
49714930 debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
@@ -4976,11 +4935,7 @@ impl<SP: Deref> FundedChannel<SP> where
49764935 }
49774936 }
49784937 if pending_idx == core::usize::MAX {
4979- #[cfg(any(test, fuzzing))]
4980- // If we failed to find an HTLC to fail, make sure it was previously fulfilled and this
4981- // is simply a duplicate fail, not previously failed and we failed-back too early.
4982- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
4983- return Ok(None);
4938+ return Err(ChannelError::Ignore(format!("Unable to find a pending HTLC which matched the given HTLC ID ({})", htlc_id_arg)));
49844939 }
49854940
49864941 if !self.context.channel_state.can_generate_new_commitment() {
@@ -4994,17 +4949,14 @@ impl<SP: Deref> FundedChannel<SP> where
49944949 match pending_update {
49954950 &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
49964951 if htlc_id_arg == htlc_id {
4997- #[cfg(any(test, fuzzing))]
4998- debug_assert!(self.context.historical_inbound_htlc_fulfills.contains(&htlc_id_arg));
4999- return Ok(None);
4952+ return Err(ChannelError::Ignore(format!("HTLC {} was already claimed!", htlc_id)));
50004953 }
50014954 },
50024955 &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } |
50034956 &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, .. } =>
50044957 {
50054958 if htlc_id_arg == htlc_id {
5006- debug_assert!(false, "Tried to fail an HTLC that was already failed");
5007- return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID".to_owned()));
4959+ return Err(ChannelError::Ignore(format!("HTLC {} was already pending failure", htlc_id)));
50084960 }
50094961 },
50104962 _ => {}
@@ -9795,13 +9747,6 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
97959747
97969748 self.context.channel_update_status.write(writer)?;
97979749
9798- #[cfg(any(test, fuzzing))]
9799- (self.context.historical_inbound_htlc_fulfills.len() as u64).write(writer)?;
9800- #[cfg(any(test, fuzzing))]
9801- for htlc in self.context.historical_inbound_htlc_fulfills.iter() {
9802- htlc.write(writer)?;
9803- }
9804-
98059750 // If the channel type is something other than only-static-remote-key, then we need to have
98069751 // older clients fail to deserialize this channel at all. If the type is
98079752 // only-static-remote-key, we simply consider it "default" and don't write the channel type
@@ -10135,16 +10080,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1013510080
1013610081 let channel_update_status = Readable::read(reader)?;
1013710082
10138- #[cfg(any(test, fuzzing))]
10139- let mut historical_inbound_htlc_fulfills = new_hash_set();
10140- #[cfg(any(test, fuzzing))]
10141- {
10142- let htlc_fulfills_len: u64 = Readable::read(reader)?;
10143- for _ in 0..htlc_fulfills_len {
10144- assert!(historical_inbound_htlc_fulfills.insert(Readable::read(reader)?));
10145- }
10146- }
10147-
1014810083 let pending_update_fee = if let Some(feerate) = pending_update_fee_value {
1014910084 Some((feerate, if channel_parameters.is_outbound_from_holder {
1015010085 FeeUpdateState::Outbound
@@ -10485,9 +10420,6 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1048510420 channel_pending_event_emitted: channel_pending_event_emitted.unwrap_or(true),
1048610421 channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
1048710422
10488- #[cfg(any(test, fuzzing))]
10489- historical_inbound_htlc_fulfills,
10490-
1049110423 channel_type: channel_type.unwrap(),
1049210424 channel_keys_id,
1049310425
0 commit comments