@@ -362,9 +362,12 @@ impl OutboundHTLCState {
362
362
363
363
fn preimage(&self) -> Option<PaymentPreimage> {
364
364
match self {
365
- OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(p)) |
366
- OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(p)) |
367
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(p)) => p.as_ref().copied(),
365
+ OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(preimage)) |
366
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(preimage)) |
367
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(preimage)) => {
368
+ debug_assert!(preimage.is_some());
369
+ *preimage
370
+ },
368
371
_ => None,
369
372
}
370
373
}
@@ -373,7 +376,10 @@ impl OutboundHTLCState {
373
376
#[derive(Clone)]
374
377
#[cfg_attr(test, derive(Debug, PartialEq))]
375
378
enum OutboundHTLCOutcome {
376
- /// LDK version 0.0.105+ will always fill in the preimage here.
379
+ /// Except briefly during deserialization and state transitions between success states,
380
+ /// we require all success states to hold their corresponding preimage.
381
+ /// We started always filling in the preimages here in 0.0.105, and the requirement
382
+ /// that the preimages always be filled in was added in 0.2.
377
383
Success(Option<PaymentPreimage>),
378
384
Failure(HTLCFailReason),
379
385
}
@@ -3807,7 +3813,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3807
3813
}
3808
3814
remote_htlc_total_msat += htlc.amount_msat;
3809
3815
} else {
3810
- if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_preimage)) = htlc.state {
3816
+ if htlc.state.preimage().is_some() {
3811
3817
value_to_self_msat_offset += htlc.amount_msat as i64;
3812
3818
}
3813
3819
}
@@ -3820,10 +3826,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3820
3826
}
3821
3827
local_htlc_total_msat += htlc.amount_msat;
3822
3828
} else {
3823
- if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) |
3824
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) |
3825
- OutboundHTLCState::RemoteRemoved(OutboundHTLCOutcome::Success(_))
3826
- = htlc.state {
3829
+ if htlc.state.preimage().is_some() {
3827
3830
value_to_self_msat_offset -= htlc.amount_msat as i64;
3828
3831
}
3829
3832
}
@@ -5735,6 +5738,7 @@ impl<SP: Deref> FundedChannel<SP> where
5735
5738
#[inline]
5736
5739
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentPreimage>, fail_reason: Option<HTLCFailReason>) -> Result<&OutboundHTLCOutput, ChannelError> {
5737
5740
assert!(!(check_preimage.is_some() && fail_reason.is_some()), "cannot fail while we have a preimage");
5741
+ assert!(!(check_preimage.is_none() && fail_reason.is_none()), "success states must hold their corresponding preimage");
5738
5742
for htlc in self.context.pending_outbound_htlcs.iter_mut() {
5739
5743
if htlc.htlc_id == htlc_id {
5740
5744
let outcome = match check_preimage {
@@ -10483,6 +10487,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10483
10487
&OutboundHTLCState::AwaitingRemoteRevokeToRemove(ref outcome) => {
10484
10488
3u8.write(writer)?;
10485
10489
if let OutboundHTLCOutcome::Success(preimage) = outcome {
10490
+ debug_assert!(preimage.is_some(), "success states must hold their corresponding preimage");
10486
10491
preimages.push(preimage);
10487
10492
}
10488
10493
let reason: Option<&HTLCFailReason> = outcome.into();
@@ -10491,6 +10496,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
10491
10496
&OutboundHTLCState::AwaitingRemovedRemoteRevoke(ref outcome) => {
10492
10497
4u8.write(writer)?;
10493
10498
if let OutboundHTLCOutcome::Success(preimage) = outcome {
10499
+ debug_assert!(preimage.is_some(), "success states must hold their corresponding preimage");
10494
10500
preimages.push(preimage);
10495
10501
}
10496
10502
let reason: Option<&HTLCFailReason> = outcome.into();
@@ -10984,7 +10990,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
10984
10990
// only, so we default to that if none was written.
10985
10991
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
10986
10992
let mut channel_creation_height = 0u32;
10987
- let mut preimages_opt: Option< Vec<Option<PaymentPreimage>>> = None ;
10993
+ let mut preimages: Vec<Option<PaymentPreimage>> = Vec::new() ;
10988
10994
10989
10995
// If we read an old Channel, for simplicity we just treat it as "we never sent an
10990
10996
// AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
@@ -11036,7 +11042,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11036
11042
(10, monitor_pending_update_adds, option), // Added in 0.0.122
11037
11043
(11, monitor_pending_finalized_fulfills, optional_vec),
11038
11044
(13, channel_creation_height, required),
11039
- (15, preimages_opt, optional_vec),
11045
+ (15, preimages, required_vec), // The preimages transitioned from optional to required in 0.2
11040
11046
(17, announcement_sigs_state, required),
11041
11047
(19, latest_inbound_scid_alias, option),
11042
11048
(21, outbound_scid_alias, required),
@@ -11063,23 +11069,21 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
11063
11069
11064
11070
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
11065
11071
11066
- if let Some(preimages) = preimages_opt {
11067
- let mut iter = preimages.into_iter();
11068
- for htlc in pending_outbound_htlcs.iter_mut() {
11069
- match &htlc.state {
11070
- OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(None)) => {
11071
- htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11072
- }
11073
- OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(None)) => {
11074
- htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11075
- }
11076
- _ => {}
11072
+ let mut iter = preimages.into_iter();
11073
+ for htlc in pending_outbound_htlcs.iter_mut() {
11074
+ match &htlc.state {
11075
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(None)) => {
11076
+ htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11077
11077
}
11078
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(None)) => {
11079
+ htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
11080
+ }
11081
+ _ => {}
11078
11082
}
11079
- // We expect all preimages to be consumed above
11080
- if iter.next().is_some() {
11081
- return Err(DecodeError::InvalidValue);
11082
- }
11083
+ }
11084
+ // We expect all preimages to be consumed above
11085
+ if iter.next().is_some() {
11086
+ return Err(DecodeError::InvalidValue);
11083
11087
}
11084
11088
11085
11089
let chan_features = channel_type.unwrap();
0 commit comments