@@ -50,7 +50,7 @@ use crate::ln::chan_utils::{
50
50
#[cfg(splicing)]
51
51
use crate::ln::chan_utils::FUNDING_TRANSACTION_WITNESS_WEIGHT;
52
52
use crate::ln::chan_utils;
53
- use crate::ln::onion_utils::{HTLCFailReason};
53
+ use crate::ln::onion_utils::{HTLCFailReason, ATTRIBUTION_DATA_LEN };
54
54
use crate::chain::BestBlock;
55
55
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator, fee_for_weight};
56
56
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
@@ -68,8 +68,11 @@ use crate::util::scid_utils::scid_from_parts;
68
68
69
69
use crate::io;
70
70
use crate::prelude::*;
71
+ use core::time::Duration;
71
72
use core::{cmp,mem,fmt};
72
73
use core::ops::Deref;
74
+ #[cfg(feature = "std")]
75
+ use std::time::SystemTime;
73
76
#[cfg(any(test, fuzzing, debug_assertions))]
74
77
use crate::sync::Mutex;
75
78
use crate::sign::type_resolver::ChannelSignerType;
@@ -323,6 +326,7 @@ struct OutboundHTLCOutput {
323
326
source: HTLCSource,
324
327
blinding_point: Option<PublicKey>,
325
328
skimmed_fee_msat: Option<u64>,
329
+ timestamp: Option<Duration>,
326
330
}
327
331
328
332
/// See AwaitingRemoteRevoke ChannelState for more info
@@ -4933,7 +4937,7 @@ trait FailHTLCContents {
4933
4937
impl FailHTLCContents for msgs::OnionErrorPacket {
4934
4938
type Message = msgs::UpdateFailHTLC;
4935
4939
fn to_message(self, htlc_id: u64, channel_id: ChannelId) -> Self::Message {
4936
- msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data }
4940
+ msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data, attribution_data: self.attribution_data }
4937
4941
}
4938
4942
fn to_inbound_htlc_state(self) -> InboundHTLCState {
4939
4943
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(self))
@@ -6100,10 +6104,16 @@ impl<SP: Deref> FundedChannel<SP> where
6100
6104
false
6101
6105
} else { true }
6102
6106
});
6107
+ let now = duration_since_epoch();
6103
6108
pending_outbound_htlcs.retain(|htlc| {
6104
6109
if let &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) = &htlc.state {
6105
6110
log_trace!(logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}", &htlc.payment_hash);
6106
- if let OutboundHTLCOutcome::Failure(reason) = outcome.clone() { // We really want take() here, but, again, non-mut ref :(
6111
+ if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() { // We really want take() here, but, again, non-mut ref :(
6112
+ if let (Some(timestamp), Some(now)) = (htlc.timestamp, now) {
6113
+ let hold_time = u32::try_from((now - timestamp).as_millis()).unwrap_or(u32::MAX);
6114
+ reason.set_hold_time(hold_time);
6115
+ }
6116
+
6107
6117
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
6108
6118
} else {
6109
6119
finalized_claimed_htlcs.push(htlc.source.clone());
@@ -6845,6 +6855,7 @@ impl<SP: Deref> FundedChannel<SP> where
6845
6855
channel_id: self.context.channel_id(),
6846
6856
htlc_id: htlc.htlc_id,
6847
6857
reason: err_packet.data.clone(),
6858
+ attribution_data: err_packet.attribution_data,
6848
6859
});
6849
6860
},
6850
6861
&InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
@@ -8671,6 +8682,7 @@ impl<SP: Deref> FundedChannel<SP> where
8671
8682
return Ok(None);
8672
8683
}
8673
8684
8685
+ let timestamp = duration_since_epoch();
8674
8686
self.context.pending_outbound_htlcs.push(OutboundHTLCOutput {
8675
8687
htlc_id: self.context.next_holder_htlc_id,
8676
8688
amount_msat,
@@ -8680,6 +8692,7 @@ impl<SP: Deref> FundedChannel<SP> where
8680
8692
source,
8681
8693
blinding_point,
8682
8694
skimmed_fee_msat,
8695
+ timestamp,
8683
8696
});
8684
8697
8685
8698
let res = msgs::UpdateAddHTLC {
@@ -10247,6 +10260,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10247
10260
dropped_inbound_htlcs += 1;
10248
10261
}
10249
10262
}
10263
+ let mut removed_htlc_failure_attribution_data: Vec<&Option<[u8; ATTRIBUTION_DATA_LEN]>> = Vec::new();
10250
10264
(self.context.pending_inbound_htlcs.len() as u64 - dropped_inbound_htlcs).write(writer)?;
10251
10265
for htlc in self.context.pending_inbound_htlcs.iter() {
10252
10266
if let &InboundHTLCState::RemoteAnnounced(_) = &htlc.state {
@@ -10272,9 +10286,10 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10272
10286
&InboundHTLCState::LocalRemoved(ref removal_reason) => {
10273
10287
4u8.write(writer)?;
10274
10288
match removal_reason {
10275
- InboundHTLCRemovalReason::FailRelay(msgs::OnionErrorPacket { data }) => {
10289
+ InboundHTLCRemovalReason::FailRelay(msgs::OnionErrorPacket { data, attribution_data }) => {
10276
10290
0u8.write(writer)?;
10277
10291
data.write(writer)?;
10292
+ removed_htlc_failure_attribution_data.push(&attribution_data);
10278
10293
},
10279
10294
InboundHTLCRemovalReason::FailMalformed((hash, code)) => {
10280
10295
1u8.write(writer)?;
@@ -10336,10 +10351,11 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10336
10351
10337
10352
let mut holding_cell_skimmed_fees: Vec<Option<u64>> = Vec::new();
10338
10353
let mut holding_cell_blinding_points: Vec<Option<PublicKey>> = Vec::new();
10354
+ let mut holding_cell_failure_attribution_data: Vec<(u32, [u8; ATTRIBUTION_DATA_LEN])> = Vec::new();
10339
10355
// Vec of (htlc_id, failure_code, sha256_of_onion)
10340
10356
let mut malformed_htlcs: Vec<(u64, u16, [u8; 32])> = Vec::new();
10341
10357
(self.context.holding_cell_htlc_updates.len() as u64).write(writer)?;
10342
- for update in self.context.holding_cell_htlc_updates.iter() {
10358
+ for (i, update) in self.context.holding_cell_htlc_updates.iter().enumerate () {
10343
10359
match update {
10344
10360
&HTLCUpdateAwaitingACK::AddHTLC {
10345
10361
ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
@@ -10364,6 +10380,13 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10364
10380
2u8.write(writer)?;
10365
10381
htlc_id.write(writer)?;
10366
10382
err_packet.data.write(writer)?;
10383
+
10384
+ // Store the attribution data for later writing. Include the holding cell htlc update index because
10385
+ // FailMalformedHTLC is stored with the same type 2 and we wouldn't be able to distinguish the two
10386
+ // when reading back in.
10387
+ if let Some(attribution_data ) = err_packet.attribution_data {
10388
+ holding_cell_failure_attribution_data.push((i as u32, attribution_data));
10389
+ }
10367
10390
}
10368
10391
&HTLCUpdateAwaitingACK::FailMalformedHTLC {
10369
10392
htlc_id, failure_code, sha256_of_onion
@@ -10547,6 +10570,8 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10547
10570
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
10548
10571
(51, is_manual_broadcast, option), // Added in 0.0.124
10549
10572
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
10573
+ (55, removed_htlc_failure_attribution_data, optional_vec), // Added in 0.2
10574
+ (57, holding_cell_failure_attribution_data, optional_vec), // Added in 0.2
10550
10575
});
10551
10576
10552
10577
Ok(())
@@ -10624,6 +10649,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10624
10649
let reason = match <u8 as Readable>::read(reader)? {
10625
10650
0 => InboundHTLCRemovalReason::FailRelay(msgs::OnionErrorPacket {
10626
10651
data: Readable::read(reader)?,
10652
+ attribution_data: None,
10627
10653
}),
10628
10654
1 => InboundHTLCRemovalReason::FailMalformed(Readable::read(reader)?),
10629
10655
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
@@ -10664,6 +10690,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10664
10690
},
10665
10691
skimmed_fee_msat: None,
10666
10692
blinding_point: None,
10693
+ timestamp: None,
10667
10694
});
10668
10695
}
10669
10696
@@ -10688,6 +10715,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10688
10715
htlc_id: Readable::read(reader)?,
10689
10716
err_packet: OnionErrorPacket {
10690
10717
data: Readable::read(reader)?,
10718
+ attribution_data: None,
10691
10719
},
10692
10720
},
10693
10721
_ => return Err(DecodeError::InvalidValue),
@@ -10831,6 +10859,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10831
10859
let mut pending_outbound_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
10832
10860
let mut holding_cell_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
10833
10861
10862
+ let mut removed_htlc_failure_attribution_data: Option<Vec<Option<[u8; ATTRIBUTION_DATA_LEN]>>> = None;
10863
+ let mut holding_cell_failure_attribution_data: Option<Vec<(u32, [u8; ATTRIBUTION_DATA_LEN])>> = None;
10864
+
10834
10865
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
10835
10866
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
10836
10867
@@ -10873,6 +10904,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10873
10904
(49, local_initiated_shutdown, option),
10874
10905
(51, is_manual_broadcast, option),
10875
10906
(53, funding_tx_broadcast_safe_event_emitted, option),
10907
+ (55, removed_htlc_failure_attribution_data, optional_vec),
10908
+ (57, holding_cell_failure_attribution_data, optional_vec),
10876
10909
});
10877
10910
10878
10911
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -10954,6 +10987,38 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10954
10987
if iter.next().is_some() { return Err(DecodeError::InvalidValue) }
10955
10988
}
10956
10989
10990
+ if let Some(attribution_datas) = removed_htlc_failure_attribution_data {
10991
+ let mut removed_htlc_relay_failures =
10992
+ pending_inbound_htlcs.iter_mut().filter_map(|status|
10993
+ if let InboundHTLCState::LocalRemoved(ref mut reason) = &mut status.state {
10994
+ if let InboundHTLCRemovalReason::FailRelay(ref mut packet) = reason {
10995
+ Some(&mut packet.attribution_data)
10996
+ } else {
10997
+ None
10998
+ }
10999
+ } else {
11000
+ None
11001
+ }
11002
+ );
11003
+
11004
+ for attribution_data in attribution_datas {
11005
+ *removed_htlc_relay_failures.next().ok_or(DecodeError::InvalidValue)? = attribution_data;
11006
+ }
11007
+ if removed_htlc_relay_failures.next().is_some() { return Err(DecodeError::InvalidValue); }
11008
+ }
11009
+
11010
+ if let Some(attribution_datas) = holding_cell_failure_attribution_data {
11011
+ for (i, attribution_data) in attribution_datas {
11012
+ let update = holding_cell_htlc_updates.get_mut(i as usize).ok_or(DecodeError::InvalidValue)?;
11013
+
11014
+ if let HTLCUpdateAwaitingACK::FailHTLC { htlc_id: _, ref mut err_packet } = update {
11015
+ err_packet.attribution_data = Some(attribution_data);
11016
+ } else {
11017
+ return Err(DecodeError::InvalidValue);
11018
+ }
11019
+ }
11020
+ }
11021
+
10957
11022
if let Some(malformed_htlcs) = malformed_htlcs {
10958
11023
for (malformed_htlc_id, failure_code, sha256_of_onion) in malformed_htlcs {
10959
11024
let htlc_idx = holding_cell_htlc_updates.iter().position(|htlc| {
@@ -11144,6 +11209,18 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11144
11209
}
11145
11210
}
11146
11211
11212
+ fn duration_since_epoch() -> Option<Duration> {
11213
+ #[cfg(not(feature = "std"))]
11214
+ let now = None;
11215
+
11216
+ #[cfg(feature = "std")]
11217
+ let now = Some(std::time::SystemTime::now()
11218
+ .duration_since(std::time::SystemTime::UNIX_EPOCH)
11219
+ .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH"));
11220
+
11221
+ now
11222
+ }
11223
+
11147
11224
#[cfg(test)]
11148
11225
mod tests {
11149
11226
use std::cmp;
@@ -11157,7 +11234,7 @@ mod tests {
11157
11234
use bitcoin::network::Network;
11158
11235
#[cfg(splicing)]
11159
11236
use bitcoin::Weight;
11160
- use crate::ln::onion_utils::INVALID_ONION_BLINDING;
11237
+ use crate::ln::onion_utils::{ATTRIBUTION_DATA_LEN, INVALID_ONION_BLINDING} ;
11161
11238
use crate::types::payment::{PaymentHash, PaymentPreimage};
11162
11239
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
11163
11240
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
@@ -11378,6 +11455,7 @@ mod tests {
11378
11455
},
11379
11456
skimmed_fee_msat: None,
11380
11457
blinding_point: None,
11458
+ timestamp: None,
11381
11459
});
11382
11460
11383
11461
// Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
@@ -11762,6 +11840,7 @@ mod tests {
11762
11840
source: dummy_htlc_source.clone(),
11763
11841
skimmed_fee_msat: None,
11764
11842
blinding_point: None,
11843
+ timestamp: None,
11765
11844
};
11766
11845
let mut pending_outbound_htlcs = vec![dummy_outbound_output.clone(); 10];
11767
11846
for (idx, htlc) in pending_outbound_htlcs.iter_mut().enumerate() {
@@ -11793,7 +11872,7 @@ mod tests {
11793
11872
htlc_id: 0,
11794
11873
};
11795
11874
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
11796
- htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] }
11875
+ htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: Some([1; ATTRIBUTION_DATA_LEN]) }
11797
11876
};
11798
11877
let dummy_holding_cell_malformed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
11799
11878
htlc_id, failure_code: INVALID_ONION_BLINDING, sha256_of_onion: [0; 32],
0 commit comments