@@ -364,9 +364,12 @@ impl OutboundHTLCState {
364
364
365
365
fn preimage(&self) -> Option<PaymentPreimage> {
366
366
match self {
367
- OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(p)) |
368
- OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(p)) |
369
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(p)) => p.as_ref().copied(),
367
+ OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage)) |
368
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage)) |
369
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage)) => {
370
+ debug_assert!(preimage.is_some());
371
+ *preimage
372
+ },
370
373
_ => None,
371
374
}
372
375
}
@@ -375,7 +378,10 @@ impl OutboundHTLCState {
375
378
#[derive(Clone)]
376
379
#[cfg_attr(test, derive(Debug, PartialEq))]
377
380
enum OutboundHTLCOutcome {
378
- /// LDK version 0.0.105+ will always fill in the preimage here.
381
+ /// Except briefly during deserialization and state transitions between success states,
382
+ /// we require all success states to hold their corresponding preimage.
383
+ /// We started always filling in the preimages here in 0.0.105, and the requirement
384
+ /// that the preimages always be filled in was added in 0.2.
379
385
Success(Option<PaymentPreimage>),
380
386
Failure(HTLCFailReason),
381
387
}
@@ -3876,7 +3882,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3876
3882
}
3877
3883
remote_htlc_total_msat += htlc.amount_msat;
3878
3884
} else {
3879
- if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_preimage)) = htlc.state {
3885
+ if htlc.state.preimage().is_some() {
3880
3886
value_to_self_msat_offset += htlc.amount_msat as i64;
3881
3887
}
3882
3888
}
@@ -3889,10 +3895,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3889
3895
}
3890
3896
local_htlc_total_msat += htlc.amount_msat;
3891
3897
} else {
3892
- if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) |
3893
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) |
3894
- OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(_))
3895
- = htlc.state {
3898
+ if htlc.state.preimage().is_some() {
3896
3899
value_to_self_msat_offset -= htlc.amount_msat as i64;
3897
3900
}
3898
3901
}
@@ -5801,6 +5804,7 @@ impl<SP: Deref> FundedChannel<SP> where
5801
5804
#[inline]
5802
5805
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentPreimage>, fail_reason: Option<HTLCFailReason>) -> Result<&OutboundHTLCOutput, ChannelError> {
5803
5806
assert!(!(check_preimage.is_some() && fail_reason.is_some()), "cannot fail while we have a preimage");
5807
+ assert!(!(check_preimage.is_none() && fail_reason.is_none()), "success states must hold their corresponding preimage");
5804
5808
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
5805
5809
if htlc.htlc_id == htlc_id {
5806
5810
let outcome = match check_preimage {
@@ -10667,6 +10671,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10667
10671
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
10668
10672
3u8.write(writer)?;
10669
10673
if let OutboundHTLCOutcome::Success(preimage) = outcome {
10674
+ debug_assert!(preimage.is_some(), "success states must hold their corresponding preimage");
10670
10675
preimages.push(preimage);
10671
10676
}
10672
10677
let reason: Option<&HTLCFailReason> = outcome.into();
@@ -10675,6 +10680,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10675
10680
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
10676
10681
4u8.write(writer)?;
10677
10682
if let OutboundHTLCOutcome::Success(preimage) = outcome {
10683
+ debug_assert!(preimage.is_some(), "success states must hold their corresponding preimage");
10678
10684
preimages.push(preimage);
10679
10685
}
10680
10686
let reason: Option<&HTLCFailReason> = outcome.into();
@@ -11169,7 +11175,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11169
11175
// only, so we default to that if none was written.
11170
11176
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
11171
11177
let mut channel_creation_height = 0u32;
11172
- let mut preimages_opt: Option< Vec<Option<PaymentPreimage>>> = None ;
11178
+ let mut preimages: Vec<Option<PaymentPreimage>> = Vec::new() ;
11173
11179
11174
11180
// If we read an old Channel, for simplicity we just treat it as "we never sent an
11175
11181
// AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
@@ -11223,7 +11229,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11223
11229
(10, monitor_pending_update_adds, option), // Added in 0.0.122
11224
11230
(11, monitor_pending_finalized_fulfills, optional_vec),
11225
11231
(13, channel_creation_height, required),
11226
- (15, preimages_opt, optional_vec),
11232
+ (15, preimages, required_vec), // The preimages transitioned from optional to required in 0.2
11227
11233
(17, announcement_sigs_state, required),
11228
11234
(19, latest_inbound_scid_alias, option),
11229
11235
(21, outbound_scid_alias, required),
@@ -11251,23 +11257,21 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11251
11257
11252
11258
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
11253
11259
11254
- if let Some(preimages) = preimages_opt {
11255
- let mut iter = preimages.into_iter();
11256
- for htlc in pending_outbound_htlcs.iter_mut() {
11257
- match &htlc.state {
11258
- OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(None)) => {
11259
- htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11260
- }
11261
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(None)) => {
11262
- htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11263
- }
11264
- _ => {}
11260
+ let mut iter = preimages.into_iter();
11261
+ for htlc in pending_outbound_htlcs.iter_mut() {
11262
+ match &htlc.state {
11263
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(None)) => {
11264
+ htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11265
11265
}
11266
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(None)) => {
11267
+ htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11268
+ }
11269
+ _ => {}
11266
11270
}
11267
- // We expect all preimages to be consumed above
11268
- if iter.next().is_some() {
11269
- return Err(DecodeError::InvalidValue);
11270
- }
11271
+ }
11272
+ // We expect all preimages to be consumed above
11273
+ if iter.next().is_some() {
11274
+ return Err(DecodeError::InvalidValue);
11271
11275
}
11272
11276
11273
11277
let chan_features = channel_type.unwrap();
0 commit comments