Skip to content

Commit f12b91a

Browse files
committed
Convert OutboundHTLCOutcome::Success fields to struct
1 parent ff290db commit f12b91a

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)
@@ -7774,8 +7777,8 @@ where
77747777
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, outcome: OutboundHTLCOutcome) -> Result<&OutboundHTLCOutput, ChannelError> {
77757778
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
77767779
if htlc.htlc_id == htlc_id {
7777-
if let OutboundHTLCOutcome::Success(ref payment_preimage, ..) = outcome {
7778-
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
7780+
if let OutboundHTLCOutcome::Success { ref preimage, .. } = outcome {
7781+
let payment_hash = PaymentHash(Sha256::hash(&preimage.0[..]).to_byte_array());
77797782
if payment_hash != htlc.payment_hash {
77807783
return Err(ChannelError::close(format!("Remote tried to fulfill HTLC ({}) with an incorrect preimage", htlc_id)));
77817784
}
@@ -7816,8 +7819,10 @@ where
78167819
));
78177820
}
78187821

7819-
let outcome =
7820-
OutboundHTLCOutcome::Success(msg.payment_preimage, msg.attribution_data.clone());
7822+
let outcome = OutboundHTLCOutcome::Success {
7823+
preimage: msg.payment_preimage,
7824+
attribution_data: msg.attribution_data.clone(),
7825+
};
78217826
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
78227827
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
78237828
})
@@ -8212,9 +8217,12 @@ where
82128217
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
82138218
&htlc.payment_hash, &self.context.channel_id);
82148219
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
8215-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
8220+
let mut reason = OutboundHTLCOutcome::Success {
8221+
preimage: PaymentPreimage([0u8; 32]),
8222+
attribution_data: None,
8223+
};
82168224
mem::swap(outcome, &mut reason);
8217-
if let OutboundHTLCOutcome::Success(preimage, _) = reason {
8225+
if let OutboundHTLCOutcome::Success { preimage, .. } = reason {
82188226
// If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
82198227
// upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
82208228
// have a `Success(None)` reason. In this case we could forget some HTLC
@@ -8711,7 +8719,7 @@ where
87118719
});
87128720
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
87138721
},
8714-
OutboundHTLCOutcome::Success(_, attribution_data) => {
8722+
OutboundHTLCOutcome::Success { attribution_data, .. } => {
87158723
// Even though a fast track was taken for fulfilled HTLCs to the incoming side, we still
87168724
// pass along attribution data here so that we can include hold time information in the
87178725
// final PaymentPathSuccessful events.
@@ -8820,7 +8828,10 @@ where
88208828
{
88218829
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
88228830
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
8823-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
8831+
let mut reason = OutboundHTLCOutcome::Success {
8832+
preimage: PaymentPreimage([0u8; 32]),
8833+
attribution_data: None,
8834+
};
88248835
mem::swap(outcome, &mut reason);
88258836
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
88268837
require_commitment = true;
@@ -12767,7 +12778,7 @@ where
1276712778
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
1276812779
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
1276912780
// Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
12770-
let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None);
12781+
let mut reason = OutboundHTLCOutcome::Success { preimage:PaymentPreimage([0u8; 32]), attribution_data:None };
1277112782
mem::swap(outcome, &mut reason);
1277212783
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
1277312784
}
@@ -14600,7 +14611,7 @@ where
1460014611
},
1460114612
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
1460214613
3u8.write(writer)?;
14603-
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
14614+
if let OutboundHTLCOutcome::Success { preimage, attribution_data } = outcome {
1460414615
preimages.push(Some(preimage));
1460514616
fulfill_attribution_data.push(attribution_data);
1460614617
}
@@ -14609,7 +14620,7 @@ where
1460914620
},
1461014621
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
1461114622
4u8.write(writer)?;
14612-
if let OutboundHTLCOutcome::Success(preimage, attribution_data) = outcome {
14623+
if let OutboundHTLCOutcome::Success { preimage, attribution_data } = outcome {
1461314624
preimages.push(Some(preimage));
1461414625
fulfill_attribution_data.push(attribution_data);
1461514626
}
@@ -15043,7 +15054,10 @@ where
1504315054
let outcome = match option {
1504415055
Some(r) => OutboundHTLCOutcome::Failure(r),
1504515056
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
15046-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
15057+
None => OutboundHTLCOutcome::Success {
15058+
preimage: PaymentPreimage([0u8; 32]),
15059+
attribution_data: None,
15060+
},
1504715061
};
1504815062
OutboundHTLCState::RemoteRemoved(outcome)
1504915063
},
@@ -15052,7 +15066,10 @@ where
1505215066
let outcome = match option {
1505315067
Some(r) => OutboundHTLCOutcome::Failure(r),
1505415068
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
15055-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
15069+
None => OutboundHTLCOutcome::Success {
15070+
preimage: PaymentPreimage([0u8; 32]),
15071+
attribution_data: None,
15072+
},
1505615073
};
1505715074
OutboundHTLCState::AwaitingRemoteRevokeToRemove(outcome)
1505815075
},
@@ -15061,7 +15078,10 @@ where
1506115078
let outcome = match option {
1506215079
Some(r) => OutboundHTLCOutcome::Failure(r),
1506315080
// Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
15064-
None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]), None),
15081+
None => OutboundHTLCOutcome::Success {
15082+
preimage: PaymentPreimage([0u8; 32]),
15083+
attribution_data: None,
15084+
},
1506515085
};
1506615086
OutboundHTLCState::AwaitingRemovedRemoteRevoke(outcome)
1506715087
},
@@ -15348,14 +15368,14 @@ where
1534815368
let mut fulfill_attribution_data_iter = fulfill_attribution_data.map(Vec::into_iter);
1534915369
for htlc in pending_outbound_htlcs.iter_mut() {
1535015370
match &mut htlc.state {
15351-
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(
15371+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success {
1535215372
ref mut preimage,
1535315373
ref mut attribution_data,
15354-
))
15355-
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(
15374+
})
15375+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success {
1535615376
ref mut preimage,
1535715377
ref mut attribution_data,
15358-
)) => {
15378+
}) => {
1535915379
// This variant was initialized like this further above
1536015380
debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
1536115381
// Flatten and unwrap the preimage; they are always set starting in 0.2.

0 commit comments

Comments
 (0)