@@ -367,7 +367,7 @@ impl OutboundHTLCState {
367
367
OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage))
368
368
| OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage))
369
369
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage)) => {
370
- preimage.as_ref().copied( )
370
+ Some(*preimage )
371
371
},
372
372
_ => None,
373
373
}
@@ -377,20 +377,12 @@ impl OutboundHTLCState {
377
377
#[derive(Clone)]
378
378
#[cfg_attr(test, derive(Debug, PartialEq))]
379
379
enum OutboundHTLCOutcome {
380
- /// LDK version 0.0.105+ will always fill in the preimage here.
381
- Success(Option<PaymentPreimage>),
380
+ /// We started always filling in the preimages here in 0.0.105, and the requirement
381
+ /// that the preimages always be filled in was added in 0.2.
382
+ Success(PaymentPreimage),
382
383
Failure(HTLCFailReason),
383
384
}
384
385
385
- impl From<Option<HTLCFailReason>> for OutboundHTLCOutcome {
386
- fn from(o: Option<HTLCFailReason>) -> Self {
387
- match o {
388
- None => OutboundHTLCOutcome::Success(None),
389
- Some(r) => OutboundHTLCOutcome::Failure(r)
390
- }
391
- }
392
- }
393
-
394
386
impl<'a> Into<Option<&'a HTLCFailReason>> for &'a OutboundHTLCOutcome {
395
387
fn into(self) -> Option<&'a HTLCFailReason> {
396
388
match self {
@@ -3878,7 +3870,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3878
3870
}
3879
3871
remote_htlc_total_msat += htlc.amount_msat;
3880
3872
} else {
3881
- if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_preimage)) = htlc.state {
3873
+ if htlc.state.preimage().is_some() {
3882
3874
value_to_self_msat_offset += htlc.amount_msat as i64;
3883
3875
}
3884
3876
}
@@ -3891,10 +3883,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3891
3883
}
3892
3884
local_htlc_total_msat += htlc.amount_msat;
3893
3885
} else {
3894
- if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) |
3895
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) |
3896
- OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(_))
3897
- = htlc.state {
3886
+ if htlc.state.preimage().is_some() {
3898
3887
value_to_self_msat_offset -= htlc.amount_msat as i64;
3899
3888
}
3900
3889
}
@@ -5803,16 +5792,18 @@ impl<SP: Deref> FundedChannel<SP> where
5803
5792
#[inline]
5804
5793
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentPreimage>, fail_reason: Option<HTLCFailReason>) -> Result<&OutboundHTLCOutput, ChannelError> {
5805
5794
assert!(!(check_preimage.is_some() && fail_reason.is_some()), "cannot fail while we have a preimage");
5795
+ assert!(!(check_preimage.is_none() && fail_reason.is_none()), "either provide a preimage, or a failure reason");
5806
5796
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
5807
5797
if htlc.htlc_id == htlc_id {
5808
5798
let outcome = match check_preimage {
5809
- None => fail_reason.into(),
5799
+ // We checked that either check_preimage or fail_reason is set above, so we can safely unwrap here
5800
+ None => OutboundHTLCOutcome::Failure(fail_reason.unwrap()),
5810
5801
Some(payment_preimage) => {
5811
5802
let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
5812
5803
if payment_hash != htlc.payment_hash {
5813
5804
return Err(ChannelError::close(format!("Remote tried to fulfill HTLC ({}) with an incorrect preimage", htlc_id)));
5814
5805
}
5815
- OutboundHTLCOutcome::Success(Some( payment_preimage) )
5806
+ OutboundHTLCOutcome::Success(payment_preimage)
5816
5807
}
5817
5808
};
5818
5809
match htlc.state {
@@ -6016,10 +6007,10 @@ impl<SP: Deref> FundedChannel<SP> where
6016
6007
if let &mut OutboundHTLCState::RemoteRemoved(ref mut outcome) = &mut htlc.state {
6017
6008
log_trace!(logger, "Updating HTLC {} to AwaitingRemoteRevokeToRemove due to commitment_signed in channel {}.",
6018
6009
&htlc.payment_hash, &self.context.channel_id);
6019
- // Grab the preimage, if it exists, instead of cloning
6020
- let mut reason = OutboundHTLCOutcome::Success(None );
6010
+ // Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
6011
+ let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]) );
6021
6012
mem::swap(outcome, &mut reason);
6022
- if let OutboundHTLCOutcome::Success(Some( preimage) ) = reason {
6013
+ if let OutboundHTLCOutcome::Success(preimage) = reason {
6023
6014
// If a user (a) receives an HTLC claim using LDK 0.0.104 or before, then (b)
6024
6015
// upgrades to LDK 0.0.114 or later before the HTLC is fully resolved, we could
6025
6016
// have a `Success(None)` reason. In this case we could forget some HTLC
@@ -6437,8 +6428,8 @@ impl<SP: Deref> FundedChannel<SP> where
6437
6428
}
6438
6429
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
6439
6430
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
6440
- // Grab the preimage, if it exists, instead of cloning
6441
- let mut reason = OutboundHTLCOutcome::Success(None );
6431
+ // Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
6432
+ let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]) );
6442
6433
mem::swap(outcome, &mut reason);
6443
6434
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
6444
6435
require_commitment = true;
@@ -9013,8 +9004,8 @@ impl<SP: Deref> FundedChannel<SP> where
9013
9004
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
9014
9005
if let &mut OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref mut outcome) = &mut htlc.state {
9015
9006
log_trace!(logger, " ...promoting outbound AwaitingRemoteRevokeToRemove {} to AwaitingRemovedRemoteRevoke", &htlc.payment_hash);
9016
- // Grab the preimage, if it exists, instead of cloning
9017
- let mut reason = OutboundHTLCOutcome::Success(None );
9007
+ // Swap against a dummy variant to avoid a potentially expensive clone of `OutboundHTLCOutcome::Failure(HTLCFailReason)`
9008
+ let mut reason = OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32]) );
9018
9009
mem::swap(outcome, &mut reason);
9019
9010
htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(reason);
9020
9011
}
@@ -10639,7 +10630,9 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10639
10630
}
10640
10631
}
10641
10632
10642
- let mut preimages: Vec<&Option<PaymentPreimage>> = vec![];
10633
+ // The elements of this vector will always be `Some` starting in 0.2,
10634
+ // but we still serialize the option to maintain backwards compatibility
10635
+ let mut preimages: Vec<Option<&PaymentPreimage>> = vec![];
10643
10636
let mut pending_outbound_skimmed_fees: Vec<Option<u64>> = Vec::new();
10644
10637
let mut pending_outbound_blinding_points: Vec<Option<PublicKey>> = Vec::new();
10645
10638
@@ -10666,15 +10659,15 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10666
10659
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
10667
10660
3u8.write(writer)?;
10668
10661
if let OutboundHTLCOutcome::Success(preimage) = outcome {
10669
- preimages.push(preimage);
10662
+ preimages.push(Some( preimage) );
10670
10663
}
10671
10664
let reason: Option<&HTLCFailReason> = outcome.into();
10672
10665
reason.write(writer)?;
10673
10666
}
10674
10667
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
10675
10668
4u8.write(writer)?;
10676
10669
if let OutboundHTLCOutcome::Success(preimage) = outcome {
10677
- preimages.push(preimage);
10670
+ preimages.push(Some( preimage) );
10678
10671
}
10679
10672
let reason: Option<&HTLCFailReason> = outcome.into();
10680
10673
reason.write(writer)?;
@@ -11013,15 +11006,30 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11013
11006
1 => OutboundHTLCState::Committed,
11014
11007
2 => {
11015
11008
let option: Option<HTLCFailReason> = Readable::read(reader)?;
11016
- OutboundHTLCState::RemoteRemoved(option.into())
11009
+ let outcome = match option {
11010
+ Some(r) => OutboundHTLCOutcome::Failure(r),
11011
+ // Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
11012
+ None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
11013
+ };
11014
+ OutboundHTLCState::RemoteRemoved(outcome)
11017
11015
},
11018
11016
3 => {
11019
11017
let option: Option<HTLCFailReason> = Readable::read(reader)?;
11020
- OutboundHTLCState::AwaitingRemoteRevokeToRemove(option.into())
11018
+ let outcome = match option {
11019
+ Some(r) => OutboundHTLCOutcome::Failure(r),
11020
+ // Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
11021
+ None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
11022
+ };
11023
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(outcome)
11021
11024
},
11022
11025
4 => {
11023
11026
let option: Option<HTLCFailReason> = Readable::read(reader)?;
11024
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(option.into())
11027
+ let outcome = match option {
11028
+ Some(r) => OutboundHTLCOutcome::Failure(r),
11029
+ // Initialize this variant with a dummy preimage, the actual preimage will be filled in further down
11030
+ None => OutboundHTLCOutcome::Success(PaymentPreimage([0u8; 32])),
11031
+ };
11032
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(outcome)
11025
11033
},
11026
11034
_ => return Err(DecodeError::InvalidValue),
11027
11035
},
@@ -11168,7 +11176,9 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11168
11176
// only, so we default to that if none was written.
11169
11177
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
11170
11178
let mut channel_creation_height = 0u32;
11171
- let mut preimages_opt: Option<Vec<Option<PaymentPreimage>>> = None;
11179
+ // Starting in 0.2, all the elements in this vector will be `Some`, but they are still
11180
+ // serialized as options to maintain backwards compatibility
11181
+ let mut preimages: Vec<Option<PaymentPreimage>> = Vec::new();
11172
11182
11173
11183
// If we read an old Channel, for simplicity we just treat it as "we never sent an
11174
11184
// AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
@@ -11222,7 +11232,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11222
11232
(10, monitor_pending_update_adds, option), // Added in 0.0.122
11223
11233
(11, monitor_pending_finalized_fulfills, optional_vec),
11224
11234
(13, channel_creation_height, required),
11225
- (15, preimages_opt, optional_vec),
11235
+ (15, preimages, required_vec), // The preimages transitioned from optional to required in 0.2
11226
11236
(17, announcement_sigs_state, required),
11227
11237
(19, latest_inbound_scid_alias, option),
11228
11238
(21, outbound_scid_alias, required),
@@ -11250,23 +11260,27 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11250
11260
11251
11261
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
11252
11262
11253
- if let Some(preimages) = preimages_opt {
11254
- let mut iter = preimages.into_iter();
11255
- for htlc in pending_outbound_htlcs.iter_mut() {
11256
- match &htlc.state {
11257
- OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(None)) => {
11258
- htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11259
- }
11260
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(None)) => {
11261
- htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11262
- }
11263
- _ => {}
11263
+ let mut iter = preimages.into_iter();
11264
+ for htlc in pending_outbound_htlcs.iter_mut() {
11265
+ match &mut htlc.state {
11266
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(ref mut preimage)) => {
11267
+ // This variant was initialized like this further above
11268
+ debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
11269
+ // Flatten and unwrap the preimage; they are always set starting in 0.2.
11270
+ *preimage = iter.next().flatten().ok_or(DecodeError::InvalidValue)?;
11264
11271
}
11272
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(ref mut preimage)) => {
11273
+ // This variant was initialized like this further above
11274
+ debug_assert_eq!(preimage, &PaymentPreimage([0u8; 32]));
11275
+ // Flatten and unwrap the preimage; they are always set starting in 0.2.
11276
+ *preimage = iter.next().flatten().ok_or(DecodeError::InvalidValue)?;
11277
+ }
11278
+ _ => {}
11265
11279
}
11266
- // We expect all preimages to be consumed above
11267
- if iter.next().is_some() {
11268
- return Err(DecodeError::InvalidValue);
11269
- }
11280
+ }
11281
+ // We expect all preimages to be consumed above
11282
+ if iter.next().is_some() {
11283
+ return Err(DecodeError::InvalidValue);
11270
11284
}
11271
11285
11272
11286
let chan_features = channel_type.unwrap();
0 commit comments