Skip to content

Commit 0876ffd

Browse files
committed
Add AttributionData to OutboundHTLCOutcome::Success
AttributionData is needed as part of the outbound HTLC outcome when revoke_and_ack has happened and the AttributionData is decoded to get the hold times for inclusion in the PaymentPathSuccessful event.
1 parent 34fd047 commit 0876ffd

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

lightning/src/ln/channel.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,11 @@ impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
348348
// the state yet.
349349
OutboundHTLCState::RemoteRemoved(_) =>
350350
OutboundHTLCStateDetails::Committed,
351-
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) =>
351+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) =>
352352
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
353353
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Failure(_)) =>
354354
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
355-
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) =>
355+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) =>
356356
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
357357
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Failure(_)) =>
358358
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
@@ -387,9 +387,9 @@ impl OutboundHTLCState {
387387
#[rustfmt::skip]
388388
fn preimage(&self) -> Option<PaymentPreimage> {
389389
match self {
390-
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage))
391-
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage))
392-
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage)) => {
390+
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage, _))
391+
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage, _))
392+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage, _)) => {
393393
Some(*preimage)
394394
},
395395
_ => None,
@@ -402,14 +402,14 @@ impl OutboundHTLCState {
402402
enum OutboundHTLCOutcome {
403403
/// We started always filling in the preimages here in 0.0.105, and the requirement
404404
/// that the preimages always be filled in was added in 0.2.
405-
Success(PaymentPreimage),
405+
Success(PaymentPreimage, #[allow(dead_code)] Option<AttributionData>),
406406
Failure(HTLCFailReason),
407407
}
408408

409409
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
410410
fn into(self) -> Option<&'a HTLCFailReason> {
411411
match self {
412-
OutboundHTLCOutcome::Success(_) => None,
412+
OutboundHTLCOutcome::Success(_, _) => None,
413413
OutboundHTLCOutcome::Failure(ref r) => Some(r),
414414
}
415415
}
@@ -4159,9 +4159,9 @@ where
41594159
// transaction).
41604160
let mut removed_outbound_total_msat = 0;
41614161
for ref htlc in self.pending_outbound_htlcs.iter() {
4162-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
4162+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
41634163
removed_outbound_total_msat += htlc.amount_msat;
4164-
} else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
4164+
} else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
41654165
removed_outbound_total_msat += htlc.amount_msat;
41664166
}
41674167
}
@@ -4424,9 +4424,9 @@ where
44244424

44254425
let mut removed_outbound_total_msat = 0;
44264426
for ref htlc in self.pending_outbound_htlcs.iter() {
4427-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) = htlc.state {
4427+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
44284428
removed_outbound_total_msat += htlc.amount_msat;
4429-
} else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
4429+
} else if let OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
44304430
removed_outbound_total_msat += htlc.amount_msat;
44314431
}
44324432
}
@@ -6694,7 +6694,7 @@ where
66946694
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, outcome: OutboundHTLCOutcome) -> Result<&OutboundHTLCOutput, ChannelError> {
66956695
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
66966696
if htlc.htlc_id == htlc_id {
6697-
if let OutboundHTLCOutcome::Success(ref payment_preimage) = outcome {
6697+
if let OutboundHTLCOutcome::Success(ref payment_preimage, ..) = outcome {
66986698
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
66996699
if payment_hash != htlc.payment_hash {
67006700
return Err(ChannelError::close(format!("Remote tried to fulfill HTLC ({}) with an incorrect preimage", htlc_id)));
@@ -6738,7 +6738,7 @@ where
67386738

67396739
self.mark_outbound_htlc_removed(
67406740
msg.htlc_id,
6741-
OutboundHTLCOutcome::Success(msg.payment_preimage),
6741+
OutboundHTLCOutcome::Success(msg.payment_preimage, None),
67426742
)
67436743
.map(|htlc| (htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat))
67446744
}
@@ -7078,9 +7078,9 @@ where
70787078
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
70797079
&htlc.payment_hash, &self.context.channel_id);
70807080
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
7081-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
7081+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
70827082
mem::swap(outcome, &mut reason);
7083-
if let OutboundHTLCOutcome::Success(preimage) = reason {
7083+
if let OutboundHTLCOutcome::Success(preimage, _) = reason {
70847084
// If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
70857085
// upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
70867086
// have a `Success(None)` reason. In this case we could forget some HTLC
@@ -7525,19 +7525,22 @@ where
75257525
&htlc.payment_hash
75267526
);
75277527
// We really want take() here, but, again, non-mut ref :(
7528-
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7529-
if let (Some(timestamp), Some(now)) = (htlc.send_timestamp, now) {
7530-
let elapsed_millis = now.saturating_sub(timestamp).as_millis();
7531-
let elapsed_units = elapsed_millis / HOLD_TIME_UNIT_MILLIS;
7532-
let hold_time = u32::try_from(elapsed_units).unwrap_or(u32::MAX);
7533-
reason.set_hold_time(hold_time);
7534-
}
7528+
match outcome.clone() {
7529+
OutboundHTLCOutcome::Failure(mut reason) => {
7530+
if let (Some(timestamp), Some(now)) = (htlc.send_timestamp, now) {
7531+
let elapsed_millis = now.saturating_sub(timestamp).as_millis();
7532+
let elapsed_units = elapsed_millis / HOLD_TIME_UNIT_MILLIS;
7533+
let hold_time = u32::try_from(elapsed_units).unwrap_or(u32::MAX);
7534+
reason.set_hold_time(hold_time);
7535+
}
75357536

7536-
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7537-
} else {
7538-
finalized_claimed_htlcs.push(htlc.source.clone());
7539-
// They fulfilled, so we sent them money
7540-
value_to_self_msat_diff -= htlc.amount_msat as i64;
7537+
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7538+
},
7539+
OutboundHTLCOutcome::Success(..) => {
7540+
finalized_claimed_htlcs.push(htlc.source.clone());
7541+
// They fulfilled, so we sent them money
7542+
value_to_self_msat_diff -= htlc.amount_msat as i64;
7543+
},
75417544
}
75427545
false
75437546
} else {
@@ -7621,7 +7624,7 @@ where
76217624
{
76227625
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
76237626
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
7624-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
7627+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
76257628
mem::swap(outcome, &mut reason);
76267629
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
76277630
require_commitment = true;
@@ -10751,7 +10754,7 @@ where
1075110754
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
1075210755
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
1075310756
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
10754-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
10757+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
1075510758
mem::swap(outcome, &mut reason);
1075610759
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
1075710760
}
@@ -12532,15 +12535,15 @@ where
1253212535
},
1253312536
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
1253412537
3u8.write(writer)?;
12535-
if let OutboundHTLCOutcome::Success(preimage) = outcome {
12538+
if let OutboundHTLCOutcome::Success(preimage, _) = outcome {
1253612539
preimages.push(Some(preimage));
1253712540
}
1253812541
let reason: Option<&HTLCFailReason> = outcome.into();
1253912542
reason.write(writer)?;
1254012543
},
1254112544
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
1254212545
4u8.write(writer)?;
12543-
if let OutboundHTLCOutcome::Success(preimage) = outcome {
12546+
if let OutboundHTLCOutcome::Success(preimage, _) = outcome {
1254412547
preimages.push(Some(preimage));
1254512548
}
1254612549
let reason: Option<&HTLCFailReason> = outcome.into();
@@ -12940,7 +12943,7 @@ where
1294012943
let outcome = match option {
1294112944
Some(r) => OutboundHTLCOutcome::Failure(r),
1294212945
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12943-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12946+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1294412947
};
1294512948
OutboundHTLCState::RemoteRemoved(outcome)
1294612949
},
@@ -12949,7 +12952,7 @@ where
1294912952
let outcome = match option {
1295012953
Some(r) => OutboundHTLCOutcome::Failure(r),
1295112954
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12952-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12955+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1295312956
};
1295412957
OutboundHTLCState::AwaitingRemoteRevokeToRemove(outcome)
1295512958
},
@@ -12958,7 +12961,7 @@ where
1295812961
let outcome = match option {
1295912962
Some(r) => OutboundHTLCOutcome::Failure(r),
1296012963
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12961-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12964+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1296212965
};
1296312966
OutboundHTLCState::AwaitingRemovedRemoteRevoke(outcome)
1296412967
},
@@ -13227,6 +13230,7 @@ where
1322713230
match &mut htlc.state {
1322813231
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(
1322913232
ref mut preimage,
13233+
..,
1323013234
)) => {
1323113235
// This variant was initialized like this further above
1323213236
debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
@@ -13235,6 +13239,7 @@ where
1323513239
},
1323613240
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(
1323713241
ref mut preimage,
13242+
..,
1323813243
)) => {
1323913244
// This variant was initialized like this further above
1324013245
debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));

0 commit comments

Comments
 (0)