@@ -49,7 +49,7 @@ use crate::ln::chan_utils::{
49
49
ClosingTransaction, commit_tx_fee_sat,
50
50
};
51
51
use crate::ln::chan_utils;
52
- use crate::ln::onion_utils::{HTLCFailReason};
52
+ use crate::ln::onion_utils::{HTLCFailReason, ATTRIBUTION_DATA_LEN };
53
53
use crate::chain::BestBlock;
54
54
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator, fee_for_weight};
55
55
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
@@ -67,8 +67,11 @@ use crate::util::scid_utils::scid_from_parts;
67
67
68
68
use crate::io;
69
69
use crate::prelude::*;
70
+ use core::time::Duration;
70
71
use core::{cmp,mem,fmt};
71
72
use core::ops::Deref;
73
+ #[cfg(feature = "std")]
74
+ use std::time::SystemTime;
72
75
#[cfg(any(test, fuzzing, debug_assertions))]
73
76
use crate::sync::Mutex;
74
77
use crate::sign::type_resolver::ChannelSignerType;
@@ -322,6 +325,7 @@ struct OutboundHTLCOutput {
322
325
source: HTLCSource,
323
326
blinding_point: Option<PublicKey>,
324
327
skimmed_fee_msat: Option<u64>,
328
+ timestamp: Option<Duration>,
325
329
}
326
330
327
331
/// See AwaitingRemoteRevoke ChannelState for more info
@@ -4856,7 +4860,7 @@ trait FailHTLCContents {
4856
4860
impl FailHTLCContents for msgs::OnionErrorPacket {
4857
4861
type Message = msgs::UpdateFailHTLC;
4858
4862
fn to_message(self, htlc_id: u64, channel_id: ChannelId) -> Self::Message {
4859
- msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data }
4863
+ msgs::UpdateFailHTLC { htlc_id, channel_id, reason: self.data, attribution_data: self.attribution_data }
4860
4864
}
4861
4865
fn to_inbound_htlc_state(self) -> InboundHTLCState {
4862
4866
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(self))
@@ -6034,10 +6038,16 @@ impl<SP: Deref> FundedChannel<SP> where
6034
6038
false
6035
6039
} else { true }
6036
6040
});
6041
+ let now = duration_since_epoch();
6037
6042
pending_outbound_htlcs.retain(|htlc| {
6038
6043
if let &OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) = &htlc.state {
6039
6044
log_trace!(logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}", &htlc.payment_hash);
6040
- if let OutboundHTLCOutcome::Failure(reason) = outcome.clone() { // We really want take() here, but, again, non-mut ref :(
6045
+ if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() { // We really want take() here, but, again, non-mut ref :(
6046
+ if let (Some(timestamp), Some(now)) = (htlc.timestamp, now) {
6047
+ let hold_time = u32::try_from((now - timestamp).as_millis()).unwrap_or(u32::MAX);
6048
+ reason.set_hold_time(hold_time);
6049
+ }
6050
+
6041
6051
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
6042
6052
} else {
6043
6053
finalized_claimed_htlcs.push(htlc.source.clone());
@@ -6779,6 +6789,7 @@ impl<SP: Deref> FundedChannel<SP> where
6779
6789
channel_id: self.context.channel_id(),
6780
6790
htlc_id: htlc.htlc_id,
6781
6791
reason: err_packet.data.clone(),
6792
+ attribution_data: err_packet.attribution_data,
6782
6793
});
6783
6794
},
6784
6795
&InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
@@ -8471,6 +8482,7 @@ impl<SP: Deref> FundedChannel<SP> where
8471
8482
return Ok(None);
8472
8483
}
8473
8484
8485
+ let timestamp = duration_since_epoch();
8474
8486
self.context.pending_outbound_htlcs.push(OutboundHTLCOutput {
8475
8487
htlc_id: self.context.next_holder_htlc_id,
8476
8488
amount_msat,
@@ -8480,6 +8492,7 @@ impl<SP: Deref> FundedChannel<SP> where
8480
8492
source,
8481
8493
blinding_point,
8482
8494
skimmed_fee_msat,
8495
+ timestamp,
8483
8496
});
8484
8497
8485
8498
let res = msgs::UpdateAddHTLC {
@@ -10037,6 +10050,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10037
10050
dropped_inbound_htlcs += 1;
10038
10051
}
10039
10052
}
10053
+ let mut removed_htlc_failure_attribution_data: Vec<&Option<[u8; ATTRIBUTION_DATA_LEN]>> = Vec::new();
10040
10054
(self.context.pending_inbound_htlcs.len() as u64 - dropped_inbound_htlcs).write(writer)?;
10041
10055
for htlc in self.context.pending_inbound_htlcs.iter() {
10042
10056
if let &InboundHTLCState::RemoteAnnounced(_) = &htlc.state {
@@ -10062,9 +10076,10 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10062
10076
&InboundHTLCState::LocalRemoved(ref removal_reason) => {
10063
10077
4u8.write(writer)?;
10064
10078
match removal_reason {
10065
- InboundHTLCRemovalReason::FailRelay(msgs::OnionErrorPacket { data }) => {
10079
+ InboundHTLCRemovalReason::FailRelay(msgs::OnionErrorPacket { data, attribution_data }) => {
10066
10080
0u8.write(writer)?;
10067
10081
data.write(writer)?;
10082
+ removed_htlc_failure_attribution_data.push(&attribution_data);
10068
10083
},
10069
10084
InboundHTLCRemovalReason::FailMalformed((hash, code)) => {
10070
10085
1u8.write(writer)?;
@@ -10126,10 +10141,11 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10126
10141
10127
10142
let mut holding_cell_skimmed_fees: Vec<Option<u64>> = Vec::new();
10128
10143
let mut holding_cell_blinding_points: Vec<Option<PublicKey>> = Vec::new();
10144
+ let mut holding_cell_failure_attribution_data: Vec<(u32, [u8; ATTRIBUTION_DATA_LEN])> = Vec::new();
10129
10145
// Vec of (htlc_id, failure_code, sha256_of_onion)
10130
10146
let mut malformed_htlcs: Vec<(u64, u16, [u8; 32])> = Vec::new();
10131
10147
(self.context.holding_cell_htlc_updates.len() as u64).write(writer)?;
10132
- for update in self.context.holding_cell_htlc_updates.iter() {
10148
+ for (i, update) in self.context.holding_cell_htlc_updates.iter().enumerate () {
10133
10149
match update {
10134
10150
&HTLCUpdateAwaitingACK::AddHTLC {
10135
10151
ref amount_msat, ref cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet,
@@ -10154,6 +10170,13 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10154
10170
2u8.write(writer)?;
10155
10171
htlc_id.write(writer)?;
10156
10172
err_packet.data.write(writer)?;
10173
+
10174
+ // Store the attribution data for later writing. Include the holding cell htlc update index because
10175
+ // FailMalformedHTLC is stored with the same type 2 and we wouldn't be able to distinguish the two
10176
+ // when reading back in.
10177
+ if let Some(attribution_data ) = err_packet.attribution_data {
10178
+ holding_cell_failure_attribution_data.push((i as u32, attribution_data));
10179
+ }
10157
10180
}
10158
10181
&HTLCUpdateAwaitingACK::FailMalformedHTLC {
10159
10182
htlc_id, failure_code, sha256_of_onion
@@ -10337,6 +10360,8 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10337
10360
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
10338
10361
(51, is_manual_broadcast, option), // Added in 0.0.124
10339
10362
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
10363
+ (55, removed_htlc_failure_attribution_data, optional_vec), // Added in 0.2
10364
+ (57, holding_cell_failure_attribution_data, optional_vec), // Added in 0.2
10340
10365
});
10341
10366
10342
10367
Ok(())
@@ -10414,6 +10439,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10414
10439
let reason = match <u8 as Readable>::read(reader)? {
10415
10440
0 => InboundHTLCRemovalReason::FailRelay(msgs::OnionErrorPacket {
10416
10441
data: Readable::read(reader)?,
10442
+ attribution_data: None,
10417
10443
}),
10418
10444
1 => InboundHTLCRemovalReason::FailMalformed(Readable::read(reader)?),
10419
10445
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
@@ -10454,6 +10480,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10454
10480
},
10455
10481
skimmed_fee_msat: None,
10456
10482
blinding_point: None,
10483
+ timestamp: None,
10457
10484
});
10458
10485
}
10459
10486
@@ -10478,6 +10505,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10478
10505
htlc_id: Readable::read(reader)?,
10479
10506
err_packet: OnionErrorPacket {
10480
10507
data: Readable::read(reader)?,
10508
+ attribution_data: None,
10481
10509
},
10482
10510
},
10483
10511
_ => return Err(DecodeError::InvalidValue),
@@ -10621,6 +10649,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10621
10649
let mut pending_outbound_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
10622
10650
let mut holding_cell_blinding_points_opt: Option<Vec<Option<PublicKey>>> = None;
10623
10651
10652
+ let mut removed_htlc_failure_attribution_data: Option<Vec<Option<[u8; ATTRIBUTION_DATA_LEN]>>> = None;
10653
+ let mut holding_cell_failure_attribution_data: Option<Vec<(u32, [u8; ATTRIBUTION_DATA_LEN])>> = None;
10654
+
10624
10655
let mut malformed_htlcs: Option<Vec<(u64, u16, [u8; 32])>> = None;
10625
10656
let mut monitor_pending_update_adds: Option<Vec<msgs::UpdateAddHTLC>> = None;
10626
10657
@@ -10663,6 +10694,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10663
10694
(49, local_initiated_shutdown, option),
10664
10695
(51, is_manual_broadcast, option),
10665
10696
(53, funding_tx_broadcast_safe_event_emitted, option),
10697
+ (55, removed_htlc_failure_attribution_data, optional_vec),
10698
+ (57, holding_cell_failure_attribution_data, optional_vec),
10666
10699
});
10667
10700
10668
10701
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -10744,6 +10777,38 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10744
10777
if iter.next().is_some() { return Err(DecodeError::InvalidValue) }
10745
10778
}
10746
10779
10780
+ if let Some(attribution_datas) = removed_htlc_failure_attribution_data {
10781
+ let mut removed_htlc_relay_failures =
10782
+ pending_inbound_htlcs.iter_mut().filter_map(|status|
10783
+ if let InboundHTLCState::LocalRemoved(ref mut reason) = &mut status.state {
10784
+ if let InboundHTLCRemovalReason::FailRelay(ref mut packet) = reason {
10785
+ Some(&mut packet.attribution_data)
10786
+ } else {
10787
+ None
10788
+ }
10789
+ } else {
10790
+ None
10791
+ }
10792
+ );
10793
+
10794
+ for attribution_data in attribution_datas {
10795
+ *removed_htlc_relay_failures.next().ok_or(DecodeError::InvalidValue)? = attribution_data;
10796
+ }
10797
+ if removed_htlc_relay_failures.next().is_some() { return Err(DecodeError::InvalidValue); }
10798
+ }
10799
+
10800
+ if let Some(attribution_datas) = holding_cell_failure_attribution_data {
10801
+ for (i, attribution_data) in attribution_datas {
10802
+ let update = holding_cell_htlc_updates.get_mut(i as usize).ok_or(DecodeError::InvalidValue)?;
10803
+
10804
+ if let HTLCUpdateAwaitingACK::FailHTLC { htlc_id: _, ref mut err_packet } = update {
10805
+ err_packet.attribution_data = Some(attribution_data);
10806
+ } else {
10807
+ return Err(DecodeError::InvalidValue);
10808
+ }
10809
+ }
10810
+ }
10811
+
10747
10812
if let Some(malformed_htlcs) = malformed_htlcs {
10748
10813
for (malformed_htlc_id, failure_code, sha256_of_onion) in malformed_htlcs {
10749
10814
let htlc_idx = holding_cell_htlc_updates.iter().position(|htlc| {
@@ -10932,6 +10997,18 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10932
10997
}
10933
10998
}
10934
10999
11000
+ fn duration_since_epoch() -> Option<Duration> {
11001
+ #[cfg(not(feature = "std"))]
11002
+ let now = None;
11003
+
11004
+ #[cfg(feature = "std")]
11005
+ let now = Some(std::time::SystemTime::now()
11006
+ .duration_since(std::time::SystemTime::UNIX_EPOCH)
11007
+ .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH"));
11008
+
11009
+ now
11010
+ }
11011
+
10935
11012
#[cfg(test)]
10936
11013
mod tests {
10937
11014
use std::cmp;
@@ -10941,7 +11018,7 @@ mod tests {
10941
11018
use bitcoin::transaction::{Transaction, TxOut, Version};
10942
11019
use bitcoin::opcodes;
10943
11020
use bitcoin::network::Network;
10944
- use crate::ln::onion_utils::INVALID_ONION_BLINDING;
11021
+ use crate::ln::onion_utils::{ATTRIBUTION_DATA_LEN, INVALID_ONION_BLINDING} ;
10945
11022
use crate::types::payment::{PaymentHash, PaymentPreimage};
10946
11023
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
10947
11024
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
@@ -11162,6 +11239,7 @@ mod tests {
11162
11239
},
11163
11240
skimmed_fee_msat: None,
11164
11241
blinding_point: None,
11242
+ timestamp: None,
11165
11243
});
11166
11244
11167
11245
// Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
@@ -11546,6 +11624,7 @@ mod tests {
11546
11624
source: dummy_htlc_source.clone(),
11547
11625
skimmed_fee_msat: None,
11548
11626
blinding_point: None,
11627
+ timestamp: None,
11549
11628
};
11550
11629
let mut pending_outbound_htlcs = vec![dummy_outbound_output.clone(); 10];
11551
11630
for (idx, htlc) in pending_outbound_htlcs.iter_mut().enumerate() {
@@ -11577,7 +11656,7 @@ mod tests {
11577
11656
htlc_id: 0,
11578
11657
};
11579
11658
let dummy_holding_cell_failed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailHTLC {
11580
- htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42] }
11659
+ htlc_id, err_packet: msgs::OnionErrorPacket { data: vec![42], attribution_data: Some([1; ATTRIBUTION_DATA_LEN]) }
11581
11660
};
11582
11661
let dummy_holding_cell_malformed_htlc = |htlc_id| HTLCUpdateAwaitingACK::FailMalformedHTLC {
11583
11662
htlc_id, failure_code: INVALID_ONION_BLINDING, sha256_of_onion: [0; 32],
0 commit comments