Skip to content

Commit 5780400

Browse files
committed
Convert OutboundHTLCOutcome::Success fields to struct
1 parent b39f64a commit 5780400

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

lightning/src/ln/channel.rs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
353353
// the state yet.
354354
OutboundHTLCState::RemoteRemoved(_) =>
355355
OutboundHTLCStateDetails::Committed,
356-
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) =>
356+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success{..}) =>
357357
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
358358
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Failure(_)) =>
359359
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
360-
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) =>
360+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success{..}) =>
361361
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
362362
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Failure(_)) =>
363363
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
@@ -392,9 +392,9 @@ impl OutboundHTLCState {
392392
#[rustfmt::skip]
393393
fn preimage(&self) -> Option<PaymentPreimage> {
394394
match self {
395-
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage, _))
396-
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage, _))
397-
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage, _)) => {
395+
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success{preimage, ..})
396+
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success{preimage, ..})
397+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success{preimage, ..}) => {
398398
Some(*preimage)
399399
},
400400
_ => None,
@@ -407,14 +407,17 @@ impl OutboundHTLCState {
407407
enum OutboundHTLCOutcome {
408408
/// We started always filling in the preimages here in 0.0.105, and the requirement
409409
/// that the preimages always be filled in was added in 0.2.
410-
Success(PaymentPreimage, Option<AttributionData>),
410+
Success {
411+
preimage: PaymentPreimage,
412+
attribution_data: Option<AttributionData>,
413+
},
411414
Failure(HTLCFailReason),
412415
}
413416

414417
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
415418
fn into(self) -> Option<&'a HTLCFailReason> {
416419
match self {
417-
OutboundHTLCOutcome::Success(_, _) => None,
420+
OutboundHTLCOutcome::Success { .. } => None,
418421
OutboundHTLCOutcome::Failure(ref r) => Some(r),
419422
}
420423
}
@@ -4604,10 +4607,10 @@ where
46044607
.pending_outbound_htlcs
46054608
.iter()
46064609
.filter(|OutboundHTLCOutput { state, .. }| match (state, local) {
4607-
(OutboundHTLCState::RemoteRemoved(Success(_, _)), true) => true,
4608-
(OutboundHTLCState::RemoteRemoved(Success(_, _)), false) => false,
4609-
(OutboundHTLCState::AwaitingRemoteRevokeToRemove(Success(_, _)), _) => true,
4610-
(OutboundHTLCState::AwaitingRemovedRemoteRevoke(Success(_, _)), _) => true,
4610+
(OutboundHTLCState::RemoteRemoved(Success { .. }), true) => true,
4611+
(OutboundHTLCState::RemoteRemoved(Success { .. }), false) => false,
4612+
(OutboundHTLCState::AwaitingRemoteRevokeToRemove(Success { .. }), _) => true,
4613+
(OutboundHTLCState::AwaitingRemovedRemoteRevoke(Success { .. }), _) => true,
46114614
_ => false,
46124615
})
46134616
.map(|OutboundHTLCOutput { amount_msat, .. }| amount_msat)
@@ -7766,8 +7769,8 @@ where
77667769
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, outcome: OutboundHTLCOutcome) -> Result<&OutboundHTLCOutput, ChannelError> {
77677770
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
77687771
if htlc.htlc_id == htlc_id {
7769-
if let OutboundHTLCOutcome::Success(ref payment_preimage, ..) = outcome {
7770-
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
7772+
if let OutboundHTLCOutcome::Success { ref preimage, .. } = outcome {
7773+
let payment_hash = PaymentHash(Sha256::hash(&preimage.0[..]).to_byte_array());
77717774
if payment_hash != htlc.payment_hash {
77727775
return Err(ChannelError::close(format!("Remote tried to fulfill HTLC ({}) with an incorrect preimage", htlc_id)));
77737776
}
@@ -7808,8 +7811,10 @@ where
78087811
));
78097812
}
78107813

7811-
let outcome =
7812-
OutboundHTLCOutcome::Success(msg.payment_preimage, msg.attribution_data.clone());
7814+
let outcome = OutboundHTLCOutcome::Success {
7815+
preimage: msg.payment_preimage,
7816+
attribution_data: msg.attribution_data.clone(),
7817+
};
78137818
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
78147819
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
78157820
})
@@ -8197,9 +8202,12 @@ where
81978202
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
81988203
&htlc.payment_hash, &self.context.channel_id);
81998204
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
8200-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
8205+
let mut reason = OutboundHTLCOutcome::Success {
8206+
preimage: PaymentPreimage([0u8; 32]),
8207+
attribution_data: None,
8208+
};
82018209
mem::swap(outcome, &mut reason);
8202-
if let OutboundHTLCOutcome::Success(preimage, _) = reason {
8210+
if let OutboundHTLCOutcome::Success { preimage, .. } = reason {
82038211
// If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
82048212
// upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
82058213
// have a `Success(None)` reason. In this case we could forget some HTLC
@@ -8672,7 +8680,7 @@ where
86728680
});
86738681
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
86748682
},
8675-
OutboundHTLCOutcome::Success(_, attribution_data) => {
8683+
OutboundHTLCOutcome::Success { attribution_data, .. } => {
86768684
// Even though a fast track was taken for fulfilled HTLCs to the incoming side, we still
86778685
// pass along attribution data here so that we can include hold time information in the
86788686
// final PaymentPathSuccessful events.
@@ -8781,7 +8789,10 @@ where
87818789
{
87828790
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
87838791
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
8784-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
8792+
let mut reason = OutboundHTLCOutcome::Success {
8793+
preimage: PaymentPreimage([0u8; 32]),
8794+
attribution_data: None,
8795+
};
87858796
mem::swap(outcome, &mut reason);
87868797
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
87878798
require_commitment = true;
@@ -12717,7 +12728,7 @@ where
1271712728
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
1271812729
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
1271912730
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
12720-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
12731+
let mut reason = OutboundHTLCOutcome::Success { preimage:PaymentPreimage([0u8; 32]), attribution_data:None };
1272112732
mem::swap(outcome, &mut reason);
1272212733
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
1272312734
}
@@ -14546,7 +14557,7 @@ where
1454614557
},
1454714558
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
1454814559
3u8.write(writer)?;
14549-
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
14560+
if let OutboundHTLCOutcome::Success { preimage, attribution_data } = outcome {
1455014561
preimages.push(Some(preimage));
1455114562
fulfill_attribution_data.push(attribution_data);
1455214563
}
@@ -14555,7 +14566,7 @@ where
1455514566
},
1455614567
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
1455714568
4u8.write(writer)?;
14558-
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
14569+
if let OutboundHTLCOutcome::Success { preimage, attribution_data } = outcome {
1455914570
preimages.push(Some(preimage));
1456014571
fulfill_attribution_data.push(attribution_data);
1456114572
}
@@ -14989,7 +15000,10 @@ where
1498915000
let outcome = match option {
1499015001
Some(r) => OutboundHTLCOutcome::Failure(r),
1499115002
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
14992-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
15003+
None => OutboundHTLCOutcome::Success {
15004+
preimage: PaymentPreimage([0u8; 32]),
15005+
attribution_data: None,
15006+
},
1499315007
};
1499415008
OutboundHTLCState::RemoteRemoved(outcome)
1499515009
},
@@ -14998,7 +15012,10 @@ where
1499815012
let outcome = match option {
1499915013
Some(r) => OutboundHTLCOutcome::Failure(r),
1500015014
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
15001-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
15015+
None => OutboundHTLCOutcome::Success {
15016+
preimage: PaymentPreimage([0u8; 32]),
15017+
attribution_data: None,
15018+
},
1500215019
};
1500315020
OutboundHTLCState::AwaitingRemoteRevokeToRemove(outcome)
1500415021
},
@@ -15007,7 +15024,10 @@ where
1500715024
let outcome = match option {
1500815025
Some(r) => OutboundHTLCOutcome::Failure(r),
1500915026
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
15010-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
15027+
None => OutboundHTLCOutcome::Success {
15028+
preimage: PaymentPreimage([0u8; 32]),
15029+
attribution_data: None,
15030+
},
1501115031
};
1501215032
OutboundHTLCState::AwaitingRemovedRemoteRevoke(outcome)
1501315033
},
@@ -15294,14 +15314,14 @@ where
1529415314
let mut fulfill_attribution_data_iter = fulfill_attribution_data.map(Vec::into_iter);
1529515315
for htlc in pending_outbound_htlcs.iter_mut() {
1529615316
match &mut htlc.state {
15297-
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(
15317+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success {
1529815318
ref mut preimage,
1529915319
ref mut attribution_data,
15300-
))
15301-
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(
15320+
})
15321+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success {
1530215322
ref mut preimage,
1530315323
ref mut attribution_data,
15304-
)) => {
15324+
}) => {
1530515325
// This variant was initialized like this further above
1530615326
debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
1530715327
// Flatten and unwrap the preimage; they are always set starting in 0.2.

0 commit comments

Comments
 (0)