Skip to content

Commit 272da58

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 8714486 commit 272da58

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
}
@@ -4168,7 +4168,7 @@ where
41684168
let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = {
41694169
let mut removed_outbound_total_msat = 0;
41704170
for htlc in self.pending_outbound_htlcs.iter() {
4171-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
4171+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
41724172
removed_outbound_total_msat += htlc.amount_msat;
41734173
}
41744174
}
@@ -4398,7 +4398,7 @@ where
43984398
if !funding.is_outbound() {
43994399
let mut removed_outbound_total_msat = 0;
44004400
for htlc in self.pending_outbound_htlcs.iter() {
4401-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) = htlc.state {
4401+
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
44024402
removed_outbound_total_msat += htlc.amount_msat;
44034403
}
44044404
}
@@ -6648,7 +6648,7 @@ where
66486648
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, outcome: OutboundHTLCOutcome) -> Result<&OutboundHTLCOutput, ChannelError> {
66496649
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
66506650
if htlc.htlc_id == htlc_id {
6651-
if let OutboundHTLCOutcome::Success(ref payment_preimage) = outcome {
6651+
if let OutboundHTLCOutcome::Success(ref payment_preimage, ..) = outcome {
66526652
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
66536653
if payment_hash != htlc.payment_hash {
66546654
return Err(ChannelError::close(format!("Remote tried to fulfill HTLC ({}) with an incorrect preimage", htlc_id)));
@@ -6690,7 +6690,7 @@ where
66906690
));
66916691
}
66926692

6693-
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage);
6693+
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage, None);
66946694
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
66956695
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
66966696
})
@@ -7052,9 +7052,9 @@ where
70527052
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
70537053
&htlc.payment_hash, &self.context.channel_id);
70547054
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
7055-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
7055+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
70567056
mem::swap(outcome, &mut reason);
7057-
if let OutboundHTLCOutcome::Success(preimage) = reason {
7057+
if let OutboundHTLCOutcome::Success(preimage, _) = reason {
70587058
// If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
70597059
// upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
70607060
// have a `Success(None)` reason. In this case we could forget some HTLC
@@ -7606,7 +7606,7 @@ where
76067606
{
76077607
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
76087608
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
7609-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
7609+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
76107610
mem::swap(outcome, &mut reason);
76117611
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
76127612
require_commitment = true;
@@ -10745,7 +10745,7 @@ where
1074510745
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
1074610746
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
1074710747
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
10748-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]));
10748+
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
1074910749
mem::swap(outcome, &mut reason);
1075010750
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
1075110751
}
@@ -12519,6 +12519,7 @@ where
1251912519
// The elements of this vector will always be `Some` starting in 0.2,
1252012520
// but we still serialize the option to maintain backwards compatibility
1252112521
let mut preimages: Vec<Option<&PaymentPreimage>> = vec![];
12522+
let mut success_attribution_data = vec![];
1252212523
let mut pending_outbound_skimmed_fees: Vec<Option<u64>> = Vec::new();
1252312524
let mut pending_outbound_blinding_points: Vec<Option<PublicKey>> = Vec::new();
1252412525

@@ -12544,16 +12545,18 @@ where
1254412545
},
1254512546
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
1254612547
3u8.write(writer)?;
12547-
if let OutboundHTLCOutcome::Success(preimage) = outcome {
12548+
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
1254812549
preimages.push(Some(preimage));
12550+
success_attribution_data.push(attribution_data);
1254912551
}
1255012552
let reason: Option<&HTLCFailReason> = outcome.into();
1255112553
reason.write(writer)?;
1255212554
},
1255312555
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
1255412556
4u8.write(writer)?;
12555-
if let OutboundHTLCOutcome::Success(preimage) = outcome {
12557+
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
1255612558
preimages.push(Some(preimage));
12559+
success_attribution_data.push(attribution_data);
1255712560
}
1255812561
let reason: Option<&HTLCFailReason> = outcome.into();
1255912562
reason.write(writer)?;
@@ -12837,6 +12840,7 @@ where
1283712840
(58, self.interactive_tx_signing_session, option), // Added in 0.2
1283812841
(59, self.funding.minimum_depth_override, option), // Added in 0.2
1283912842
(60, self.context.historical_scids, optional_vec), // Added in 0.2
12843+
(61, success_attribution_data, optional_vec),
1284012844
});
1284112845

1284212846
Ok(())
@@ -12958,7 +12962,7 @@ where
1295812962
let outcome = match option {
1295912963
Some(r) => OutboundHTLCOutcome::Failure(r),
1296012964
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12961-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12965+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1296212966
};
1296312967
OutboundHTLCState::RemoteRemoved(outcome)
1296412968
},
@@ -12967,7 +12971,7 @@ where
1296712971
let outcome = match option {
1296812972
Some(r) => OutboundHTLCOutcome::Failure(r),
1296912973
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12970-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12974+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1297112975
};
1297212976
OutboundHTLCState::AwaitingRemoteRevokeToRemove(outcome)
1297312977
},
@@ -12976,7 +12980,7 @@ where
1297612980
let outcome = match option {
1297712981
Some(r) => OutboundHTLCOutcome::Failure(r),
1297812982
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
12979-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
12983+
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
1298012984
};
1298112985
OutboundHTLCState::AwaitingRemovedRemoteRevoke(outcome)
1298212986
},
@@ -13152,6 +13156,7 @@ where
1315213156
// Starting in 0.2, all the elements in this vector will be `Some`, but they are still
1315313157
// serialized as options to maintain backwards compatibility
1315413158
let mut preimages: Vec<Option<PaymentPreimage>> = Vec::new();
13159+
let mut success_attribution_data: Option<Vec<Option<AttributionData>>> = None;
1315513160

1315613161
// If we read an old Channel, for simplicity we just treat it as "we never sent an
1315713162
// AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
@@ -13237,23 +13242,32 @@ where
1323713242
(58, interactive_tx_signing_session, option), // Added in 0.2
1323813243
(59, minimum_depth_override, option), // Added in 0.2
1323913244
(60, historical_scids, optional_vec), // Added in 0.2
13245+
(61, success_attribution_data, optional_vec),
1324013246
});
1324113247

1324213248
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
1324313249

1324413250
let mut iter = preimages.into_iter();
13251+
let mut success_attribution_data_iter = success_attribution_data.map(Vec::into_iter);
1324513252
for htlc in pending_outbound_htlcs.iter_mut() {
1324613253
match &mut htlc.state {
1324713254
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(
1324813255
ref mut preimage,
13256+
ref mut attribution_data,
1324913257
))
1325013258
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(
1325113259
ref mut preimage,
13260+
ref mut attribution_data,
1325213261
)) => {
1325313262
// This variant was initialized like this further above
1325413263
debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
1325513264
// Flatten and unwrap the preimage; they are always set starting in 0.2.
1325613265
*preimage = iter.next().flatten().ok_or(DecodeError::InvalidValue)?;
13266+
13267+
*attribution_data = success_attribution_data_iter
13268+
.as_mut()
13269+
.and_then(Iterator::next)
13270+
.ok_or(DecodeError::InvalidValue)?;
1325713271
},
1325813272
_ => {},
1325913273
}

0 commit comments

Comments
 (0)