Skip to content

Commit fcc1a8c

Browse files
committed
Hold time reporting
Adds hold time reporting for the final and intermediate nodes.
1 parent 9a4a3e2 commit fcc1a8c

File tree

3 files changed

+109
-21
lines changed

3 files changed

+109
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6215,7 +6215,7 @@ where
62156215
assert!(!self.context.channel_state.can_generate_new_commitment());
62166216
let mon_update_id = self.context.latest_monitor_update_id; // Forget the ChannelMonitor update
62176217
let fulfill_resp =
6218-
self.get_update_fulfill_htlc(htlc_id_arg, payment_preimage_arg, None, logger);
6218+
self.get_update_fulfill_htlc(htlc_id_arg, payment_preimage_arg, None, None, logger);
62196219
self.context.latest_monitor_update_id = mon_update_id;
62206220
if let UpdateFulfillFetch::NewClaim { update_blocked, .. } = fulfill_resp {
62216221
assert!(update_blocked); // The HTLC must have ended up in the holding cell.
@@ -6224,7 +6224,8 @@ where
62246224

62256225
fn get_update_fulfill_htlc<L: Deref>(
62266226
&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage,
6227-
payment_info: Option<PaymentClaimDetails>, logger: &L,
6227+
payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
6228+
logger: &L,
62286229
) -> UpdateFulfillFetch
62296230
where
62306231
L::Target: Logger,
@@ -6340,7 +6341,7 @@ where
63406341
self.context.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
63416342
payment_preimage: payment_preimage_arg,
63426343
htlc_id: htlc_id_arg,
6343-
attribution_data: None,
6344+
attribution_data,
63446345
});
63456346
return UpdateFulfillFetch::NewClaim {
63466347
monitor_update,
@@ -6371,7 +6372,7 @@ where
63716372
);
63726373
htlc.state = InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(
63736374
payment_preimage_arg.clone(),
6374-
None,
6375+
attribution_data,
63756376
));
63766377
}
63776378

@@ -6380,13 +6381,20 @@ where
63806381

63816382
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(
63826383
&mut self, htlc_id: u64, payment_preimage: PaymentPreimage,
6383-
payment_info: Option<PaymentClaimDetails>, logger: &L,
6384+
payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
6385+
logger: &L,
63846386
) -> UpdateFulfillCommitFetch
63856387
where
63866388
L::Target: Logger,
63876389
{
63886390
let release_cs_monitor = self.context.blocked_monitor_updates.is_empty();
6389-
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, payment_info, logger) {
6391+
match self.get_update_fulfill_htlc(
6392+
htlc_id,
6393+
payment_preimage,
6394+
payment_info,
6395+
attribution_data,
6396+
logger,
6397+
) {
63906398
UpdateFulfillFetch::NewClaim {
63916399
mut monitor_update,
63926400
htlc_value_msat,
@@ -6720,7 +6728,7 @@ where
67206728

67216729
pub fn update_fulfill_htlc(
67226730
&mut self, msg: &msgs::UpdateFulfillHTLC,
6723-
) -> Result<(HTLCSource, u64, Option<u64>), ChannelError> {
6731+
) -> Result<(HTLCSource, u64, Option<u64>, Option<Duration>), ChannelError> {
67246732
if self.context.channel_state.is_remote_stfu_sent()
67256733
|| self.context.channel_state.is_quiescent()
67266734
{
@@ -6743,7 +6751,9 @@ where
67436751
msg.htlc_id,
67446752
OutboundHTLCOutcome::Success(msg.payment_preimage),
67456753
)
6746-
.map(|htlc| (htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat))
6754+
.map(|htlc| {
6755+
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
6756+
})
67476757
}
67486758

67496759
#[rustfmt::skip]
@@ -7279,7 +7289,11 @@ where
72797289
}
72807290
None
72817291
},
7282-
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
7292+
&HTLCUpdateAwaitingACK::ClaimHTLC {
7293+
ref payment_preimage,
7294+
htlc_id,
7295+
ref attribution_data,
7296+
} => {
72837297
// If an HTLC claim was previously added to the holding cell (via
72847298
// `get_update_fulfill_htlc`, then generating the claim message itself must
72857299
// not fail - any in between attempts to claim the HTLC will have resulted
@@ -7292,8 +7306,13 @@ where
72927306
// We do not bother to track and include `payment_info` here, however.
72937307
let mut additional_monitor_update =
72947308
if let UpdateFulfillFetch::NewClaim { monitor_update, .. } = self
7295-
.get_update_fulfill_htlc(htlc_id, *payment_preimage, None, logger)
7296-
{
7309+
.get_update_fulfill_htlc(
7310+
htlc_id,
7311+
*payment_preimage,
7312+
None,
7313+
attribution_data.clone(),
7314+
logger,
7315+
) {
72977316
monitor_update
72987317
} else {
72997318
unreachable!()
@@ -13582,7 +13601,7 @@ where
1358213601
}
1358313602
}
1358413603

13585-
fn duration_since_epoch() -> Option<Duration> {
13604+
pub(crate) fn duration_since_epoch() -> Option<Duration> {
1358613605
#[cfg(not(feature = "std"))]
1358713606
let now = None;
1358813607

@@ -13596,7 +13615,9 @@ fn duration_since_epoch() -> Option<Duration> {
1359613615
now
1359713616
}
1359813617

13599-
fn get_hold_time(send_timestamp: Option<Duration>, now: Option<Duration>) -> Option<u32> {
13618+
pub(crate) fn get_hold_time(
13619+
send_timestamp: Option<Duration>, now: Option<Duration>,
13620+
) -> Option<u32> {
1360013621
send_timestamp.and_then(|t| {
1360113622
now.map(|now| {
1360213623
let elapsed = now.saturating_sub(t).as_millis() / HOLD_TIME_UNIT_MILLIS;

lightning/src/ln/channelmanager.rs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ use crate::events::{
5858
use crate::events::{FundingInfo, PaidBolt12Invoice};
5959
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
6060
// construct one themselves.
61-
use crate::ln::channel::PendingV2Channel;
6261
use crate::ln::channel::{
63-
self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, InboundV1Channel,
64-
OutboundV1Channel, ReconnectionMsg, ShutdownResult, UpdateFulfillCommitFetch,
62+
self, get_hold_time, Channel, ChannelError, ChannelUpdateStatus, FundedChannel,
63+
InboundV1Channel, OutboundV1Channel, ReconnectionMsg, ShutdownResult, UpdateFulfillCommitFetch,
6564
WithChannelContext,
6665
};
66+
use crate::ln::channel::{duration_since_epoch, PendingV2Channel};
6767
use crate::ln::channel_state::ChannelDetails;
6868
use crate::ln::inbound_payment;
6969
use crate::ln::msgs;
@@ -77,6 +77,7 @@ use crate::ln::onion_payment::{
7777
NextPacketDetails,
7878
};
7979
use crate::ln::onion_utils::{self};
80+
use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
8081
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
8182
use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
8283
#[cfg(test)]
@@ -7671,10 +7672,20 @@ where
76717672
pending_claim: PendingMPPClaimPointer(Arc::clone(pending_claim)),
76727673
}
76737674
});
7675+
7676+
// Create new attribution data as the final hop. Always report a zero hold time, because reporting a
7677+
// non-zero value will not make a difference in the penalty that may be applied by the sender.
7678+
let attribution_data = process_fulfill_attribution_data(
7679+
None,
7680+
&htlc.prev_hop.incoming_packet_shared_secret,
7681+
0,
7682+
);
7683+
76747684
self.claim_funds_from_hop(
76757685
htlc.prev_hop,
76767686
payment_preimage,
76777687
payment_info.clone(),
7688+
Some(attribution_data),
76787689
|_, definitely_duplicate| {
76797690
debug_assert!(
76807691
!definitely_duplicate,
@@ -7719,7 +7730,8 @@ where
77197730
) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
77207731
>(
77217732
&self, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage,
7722-
payment_info: Option<PaymentClaimDetails>, completion_action: ComplFunc,
7733+
payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
7734+
completion_action: ComplFunc,
77237735
) {
77247736
let counterparty_node_id = prev_hop.counterparty_node_id.or_else(|| {
77257737
let short_to_chan_info = self.short_to_chan_info.read().unwrap();
@@ -7732,7 +7744,13 @@ where
77327744
channel_id: prev_hop.channel_id,
77337745
htlc_id: prev_hop.htlc_id,
77347746
};
7735-
self.claim_mpp_part(htlc_source, payment_preimage, payment_info, completion_action)
7747+
self.claim_mpp_part(
7748+
htlc_source,
7749+
payment_preimage,
7750+
payment_info,
7751+
attribution_data,
7752+
completion_action,
7753+
)
77367754
}
77377755

77387756
fn claim_mpp_part<
@@ -7742,7 +7760,8 @@ where
77427760
) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
77437761
>(
77447762
&self, prev_hop: HTLCClaimSource, payment_preimage: PaymentPreimage,
7745-
payment_info: Option<PaymentClaimDetails>, completion_action: ComplFunc,
7763+
payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
7764+
completion_action: ComplFunc,
77467765
) {
77477766
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
77487767

@@ -7783,6 +7802,7 @@ where
77837802
prev_hop.htlc_id,
77847803
payment_preimage,
77857804
payment_info,
7805+
attribution_data,
77867806
&&logger,
77877807
);
77887808

@@ -7991,7 +8011,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79918011
forwarded_htlc_value_msat: Option<u64>, skimmed_fee_msat: Option<u64>, from_onchain: bool,
79928012
startup_replay: bool, next_channel_counterparty_node_id: PublicKey,
79938013
next_channel_outpoint: OutPoint, next_channel_id: ChannelId,
7994-
next_user_channel_id: Option<u128>,
8014+
next_user_channel_id: Option<u128>, attribution_data: Option<&AttributionData>,
8015+
send_timestamp: Option<Duration>,
79958016
) {
79968017
match source {
79978018
HTLCSource::OutboundRoute {
@@ -8023,10 +8044,25 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80238044
let prev_node_id = hop_data.counterparty_node_id;
80248045
let completed_blocker =
80258046
RAAMonitorUpdateBlockingAction::from_prev_hop_data(&hop_data);
8047+
8048+
// Obtain hold time, if available.
8049+
let now = duration_since_epoch();
8050+
let hold_time = get_hold_time(send_timestamp, now).unwrap_or(0);
8051+
8052+
// If attribution data was received from downstream, we shift it and get it ready for adding our hold
8053+
// time. Note that fulfilled HTLCs take a fast path to the incoming side. We don't need to wait for RAA
8054+
// to record the hold time like we do for failed HTLCs.
8055+
let attribution_data = process_fulfill_attribution_data(
8056+
attribution_data,
8057+
&hop_data.incoming_packet_shared_secret,
8058+
hold_time,
8059+
);
8060+
80268061
self.claim_funds_from_hop(
80278062
hop_data,
80288063
payment_preimage,
80298064
None,
8065+
Some(attribution_data),
80308066
|htlc_claim_value_msat, definitely_duplicate| {
80318067
let chan_to_release = Some(EventUnblockedChannel {
80328068
counterparty_node_id: next_channel_counterparty_node_id,
@@ -9584,7 +9620,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
95849620
) -> Result<(), MsgHandleErrInternal> {
95859621
let funding_txo;
95869622
let next_user_channel_id;
9587-
let (htlc_source, forwarded_htlc_value, skimmed_fee_msat) = {
9623+
let (htlc_source, forwarded_htlc_value, skimmed_fee_msat, send_timestamp) = {
95889624
let per_peer_state = self.per_peer_state.read().unwrap();
95899625
let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
95909626
debug_assert!(false);
@@ -9639,6 +9675,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96399675
funding_txo,
96409676
msg.channel_id,
96419677
Some(next_user_channel_id),
9678+
msg.attribution_data.as_ref(),
9679+
send_timestamp,
96429680
);
96439681

96449682
Ok(())
@@ -10462,6 +10500,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1046210500
"Claiming HTLC with preimage {} from our monitor",
1046310501
preimage
1046410502
);
10503+
// Claim the funds from the previous hop, if there is one. Because this is in response to a
10504+
// chain event, no attribution data is available.
1046510505
self.claim_funds_internal(
1046610506
htlc_update.source,
1046710507
preimage,
@@ -10473,6 +10513,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1047310513
funding_outpoint,
1047410514
channel_id,
1047510515
None,
10516+
None,
10517+
None,
1047610518
);
1047710519
} else {
1047810520
log_trace!(
@@ -16292,10 +16334,14 @@ where
1629216334
// Note that we don't need to pass the `payment_info` here - its
1629316335
// already (clearly) durably on disk in the `ChannelMonitor` so there's
1629416336
// no need to worry about getting it into others.
16337+
//
16338+
// We don't encode any attribution data, because the required onion shared secret isn't
16339+
// available here.
1629516340
channel_manager.claim_mpp_part(
1629616341
part.into(),
1629716342
payment_preimage,
1629816343
None,
16344+
None,
1629916345
|_, _| {
1630016346
(
1630116347
Some(MonitorUpdateCompletionAction::PaymentClaimed {
@@ -16444,6 +16490,7 @@ where
1644416490
// We use `downstream_closed` in place of `from_onchain` here just as a guess - we
1644516491
// don't remember in the `ChannelMonitor` where we got a preimage from, but if the
1644616492
// channel is closed we just assume that it probably came from an on-chain claim.
16493+
// The same holds for attribution data. We don't have any, so we pass an empty one.
1644716494
channel_manager.claim_funds_internal(
1644816495
source,
1644916496
preimage,
@@ -16455,6 +16502,8 @@ where
1645516502
downstream_funding,
1645616503
downstream_channel_id,
1645716504
None,
16505+
None,
16506+
None,
1645816507
);
1645916508
}
1646016509

lightning/src/ln/onion_utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,24 @@ fn process_failure_packet(
28142814
update_attribution_data(onion_error, shared_secret, hold_time);
28152815
}
28162816

2817+
pub(crate) fn process_fulfill_attribution_data(
2818+
attribution_data: Option<&AttributionData>, shared_secret: &[u8], hold_time: u32,
2819+
) -> AttributionData {
2820+
let mut attribution_data =
2821+
attribution_data.map_or(AttributionData::new(), |attribution_data| {
2822+
let mut attribution_data = attribution_data.clone();
2823+
2824+
attribution_data.shift_right();
2825+
2826+
attribution_data
2827+
});
2828+
2829+
attribution_data.update(&[], &shared_secret, hold_time);
2830+
attribution_data.crypt(&shared_secret);
2831+
2832+
attribution_data
2833+
}
2834+
28172835
#[cfg(test)]
28182836
mod tests {
28192837
use core::iter;

0 commit comments

Comments
 (0)