Skip to content

Commit 9ec18aa

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 740b43e commit 9ec18aa

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
350350
// the state yet.
351351
OutboundHTLCState::RemoteRemoved(_) =>
352352
OutboundHTLCStateDetails::Committed,
353-
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) =>
353+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) =>
354354
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
355355
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Failure(_)) =>
356356
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
357-
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) =>
357+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) =>
358358
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
359359
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Failure(_)) =>
360360
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
@@ -389,9 +389,9 @@ impl OutboundHTLCState {
389389
#[rustfmt::skip]
390390
fn preimage(&self) -> Option<PaymentPreimage> {
391391
match self {
392-
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage))
393-
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage))
394-
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage)) => {
392+
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage, _))
393+
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage, _))
394+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage, _)) => {
395395
Some(*preimage)
396396
},
397397
_ => None,
@@ -404,14 +404,14 @@ impl OutboundHTLCState {
404404
enum OutboundHTLCOutcome {
405405
/// We started always filling in the preimages here in 0.0.105, and the requirement
406406
/// that the preimages always be filled in was added in 0.2.
407-
Success(PaymentPreimage),
407+
Success(PaymentPreimage, #[allow(dead_code)] Option<AttributionData>),
408408
Failure(HTLCFailReason),
409409
}
410410

411411
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
412412
fn into(self) -> Option<&'a HTLCFailReason> {
413413
match self {
414-
OutboundHTLCOutcome::Success(_) => None,
414+
OutboundHTLCOutcome::Success(_, _) => None,
415415
OutboundHTLCOutcome::Failure(ref r) => Some(r),
416416
}
417417
}
@@ -4142,7 +4142,7 @@ where
41424142
let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = {
41434143
let mut removed_outbound_total_msat = 0;
41444144
for htlc in self.pending_outbound_htlcs.iter() {
4145-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
4145+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
41464146
removed_outbound_total_msat += htlc.amount_msat;
41474147
}
41484148
}
@@ -4372,7 +4372,7 @@ where
43724372
if !funding.is_outbound() {
43734373
let mut removed_outbound_total_msat = 0;
43744374
for htlc in self.pending_outbound_htlcs.iter() {
4375-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
4375+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
43764376
removed_outbound_total_msat += htlc.amount_msat;
43774377
}
43784378
}
@@ -6635,7 +6635,7 @@ where
66356635
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, outcome: OutboundHTLCOutcome) -> Result<&OutboundHTLCOutput, ChannelError> {
66366636
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
66376637
if htlc.htlc_id == htlc_id {
6638-
if let OutboundHTLCOutcome::Success(ref payment_preimage) = outcome {
6638+
if let OutboundHTLCOutcome::Success(ref payment_preimage, ..) = outcome {
66396639
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
66406640
if payment_hash != htlc.payment_hash {
66416641
return Err(ChannelError::close(format!("Remote tried to fulfill HTLC ({}) with an incorrect preimage", htlc_id)));
@@ -6677,7 +6677,7 @@ where
66776677
));
66786678
}
66796679

6680-
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage);
6680+
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage, None);
66816681
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
66826682
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
66836683
})
@@ -7037,9 +7037,9 @@ where
70377037
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
70387038
&htlc.payment_hash, &self.context.channel_id);
70397039
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
7040-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
7040+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
70417041
mem::swap(outcome, &mut reason);
7042-
if let OutboundHTLCOutcome::Success(preimage) = reason {
7042+
if let OutboundHTLCOutcome::Success(preimage, _) = reason {
70437043
// If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
70447044
// upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
70457045
// have a `Success(None)` reason. In this case we could forget some HTLC
@@ -7591,7 +7591,7 @@ where
75917591
{
75927592
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
75937593
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
7594-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
7594+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
75957595
mem::swap(outcome, &mut reason);
75967596
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
75977597
require_commitment = true;
@@ -10718,7 +10718,7 @@ where
1071810718
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
1071910719
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
1072010720
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
10721-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
10721+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
1072210722
mem::swap(outcome, &mut reason);
1072310723
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
1072410724
}
@@ -12489,6 +12489,7 @@ where
1248912489
// The elements of this vector will always be `Some` starting in 0.2,
1249012490
// but we still serialize the option to maintain backwards compatibility
1249112491
let mut preimages: Vec<Option<&PaymentPreimage>> = vec![];
12492+
let mut fulfill_attribution_data = vec![];
1249212493
let mut pending_outbound_skimmed_fees: Vec<Option<u64>> = Vec::new();
1249312494
let mut pending_outbound_blinding_points: Vec<Option<PublicKey>> = Vec::new();
1249412495

@@ -12514,16 +12515,18 @@ where
1251412515
},
1251512516
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
1251612517
3u8.write(writer)?;
12517-
if let OutboundHTLCOutcome::Success(preimage) = outcome {
12518+
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
1251812519
preimages.push(Some(preimage));
12520+
fulfill_attribution_data.push(attribution_data);
1251912521
}
1252012522
let reason: Option<&HTLCFailReason> = outcome.into();
1252112523
reason.write(writer)?;
1252212524
},
1252312525
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
1252412526
4u8.write(writer)?;
12525-
if let OutboundHTLCOutcome::Success(preimage) = outcome {
12527+
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
1252612528
preimages.push(Some(preimage));
12529+
fulfill_attribution_data.push(attribution_data);
1252712530
}
1252812531
let reason: Option<&HTLCFailReason> = outcome.into();
1252912532
reason.write(writer)?;
@@ -12807,6 +12810,7 @@ where
1280712810
(58, self.interactive_tx_signing_session, option), // Added in 0.2
1280812811
(59, self.funding.minimum_depth_override, option), // Added in 0.2
1280912812
(60, self.context.historical_scids, optional_vec), // Added in 0.2
12813+
(61, fulfill_attribution_data, optional_vec),
1281012814
});
1281112815

1281212816
Ok(())
@@ -12928,7 +12932,7 @@ where
1292812932
let outcome = match option {
1292912933
Some(r) => OutboundHTLCOutcome::Failure(r),
1293012934
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12931-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12935+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1293212936
};
1293312937
OutboundHTLCState::RemoteRemoved(outcome)
1293412938
},
@@ -12937,7 +12941,7 @@ where
1293712941
let outcome = match option {
1293812942
Some(r) => OutboundHTLCOutcome::Failure(r),
1293912943
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12940-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12944+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1294112945
};
1294212946
OutboundHTLCState::AwaitingRemoteRevokeToRemove(outcome)
1294312947
},
@@ -12946,7 +12950,7 @@ where
1294612950
let outcome = match option {
1294712951
Some(r) => OutboundHTLCOutcome::Failure(r),
1294812952
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12949-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12953+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1295012954
};
1295112955
OutboundHTLCState::AwaitingRemovedRemoteRevoke(outcome)
1295212956
},
@@ -13122,6 +13126,7 @@ where
1312213126
// Starting in 0.2, all the elements in this vector will be `Some`, but they are still
1312313127
// serialized as options to maintain backwards compatibility
1312413128
let mut preimages: Vec<Option<PaymentPreimage>> = Vec::new();
13129+
let mut fulfill_attribution_data: Option<Vec<Option<AttributionData>>> = None;
1312513130

1312613131
// If we read an old Channel, for simplicity we just treat it as "we never sent an
1312713132
// AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
@@ -13207,23 +13212,32 @@ where
1320713212
(58, interactive_tx_signing_session, option), // Added in 0.2
1320813213
(59, minimum_depth_override, option), // Added in 0.2
1320913214
(60, historical_scids, optional_vec), // Added in 0.2
13215+
(61, fulfill_attribution_data, optional_vec),
1321013216
});
1321113217

1321213218
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
1321313219

1321413220
let mut iter = preimages.into_iter();
13221+
let mut fulfill_attribution_data_iter = fulfill_attribution_data.map(Vec::into_iter);
1321513222
for htlc in pending_outbound_htlcs.iter_mut() {
1321613223
match &mut htlc.state {
1321713224
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(
1321813225
ref mut preimage,
13226+
ref mut attribution_data,
1321913227
))
1322013228
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(
1322113229
ref mut preimage,
13230+
ref mut attribution_data,
1322213231
)) => {
1322313232
// This variant was initialized like this further above
1322413233
debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
1322513234
// Flatten and unwrap the preimage; they are always set starting in 0.2.
1322613235
*preimage = iter.next().flatten().ok_or(DecodeError::InvalidValue)?;
13236+
13237+
*attribution_data = fulfill_attribution_data_iter
13238+
.as_mut()
13239+
.and_then(Iterator::next)
13240+
.ok_or(DecodeError::InvalidValue)?;
1322713241
},
1322813242
_ => {},
1322913243
}

0 commit comments

Comments
 (0)