+ #[cfg(any(test, feature = "fuzztarget"))]
+ let mut historical_inbound_htlc_fulfills = HashSet::new();
+ #[cfg(any(test, feature = "fuzztarget"))]
+ {
+ let htlc_fulfills_len: u64 = Readable::read(reader)?;
+ for _ in 0..htlc_fulfills_len {
+ assert!(historical_inbound_htlc_fulfills.insert(Readable::read(reader)?));
+ }
+ }
+
+ let pending_update_fee = if let Some(feerate) = pending_update_fee_value {
+ Some((feerate, if channel_parameters.is_outbound_from_holder {
+ FeeUpdateState::Outbound
+ } else {
+ FeeUpdateState::AwaitingRemoteRevokeToAnnounce
+ }))
+ } else {
+ None
+ };
+
+ let mut announcement_sigs = None;
+ let mut target_closing_feerate_sats_per_kw = None;
+ let mut monitor_pending_finalized_fulfills = Some(Vec::new());
+ let mut holder_selected_channel_reserve_satoshis = Some(Self::get_holder_selected_channel_reserve_satoshis(channel_value_satoshis));
+ let mut holder_max_htlc_value_in_flight_msat = Some(Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis));
+ // Prior to supporting channel type negotiation, all of our channels were static_remotekey
+ // only, so we default to that if none was written.
+ let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
+ let mut channel_creation_height = Some(serialized_height);
+ let mut preimages_opt: Option<Vec<Option<PaymentPreimage>>> = None;
+
+ // If we read an old Channel, for simplicity we just treat it as "we never sent an
+ // AnnouncementSignatures" which implies we'll re-send it on reconnect, but that's fine.
+ let mut announcement_sigs_state = Some(AnnouncementSigsState::NotSent);
+
+ read_tlv_fields!(reader, {
+ (0, announcement_sigs, option),
+ (1, minimum_depth, option),
+ (2, channel_type, option),
+ (3, counterparty_selected_channel_reserve_satoshis, option),
+ (4, holder_selected_channel_reserve_satoshis, option),
+ (5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
+ (6, holder_max_htlc_value_in_flight_msat, option),
+ (7, shutdown_scriptpubkey, option),
+ (9, target_closing_feerate_sats_per_kw, option),
+ (11, monitor_pending_finalized_fulfills, vec_type),
+ (13, channel_creation_height, option),
+ (15, preimages_opt, vec_type),
+ (17, announcement_sigs_state, option),
+ });
+
+ if let Some(preimages) = preimages_opt {
+ let mut iter = preimages.into_iter();
+ for htlc in pending_outbound_htlcs.iter_mut() {
+ match &htlc.state {
+ OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(None)) => {
+ htlc.state = OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
+ }
+ OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(None)) => {
+ htlc.state = OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(iter.next().ok_or(DecodeError::InvalidValue)?));
+ }
+ _ => {}
+ }
+ }
+ // We expect all preimages to be consumed above
+ if iter.next().is_some() {
+ return Err(DecodeError::InvalidValue);
+ }
+ }
+
+ let chan_features = channel_type.as_ref().unwrap();
+ if chan_features.supports_unknown_bits() || chan_features.requires_unknown_bits() {
+ // If the channel was written by a new version and negotiated with features we don't
+ // understand yet, refuse to read it.
+ return Err(DecodeError::UnknownRequiredFeature);
+ }
+
+ if channel_parameters.opt_anchors.is_some() {
+ // Relax this check when ChannelTypeFeatures supports anchors.
+ return Err(DecodeError::InvalidValue);
+ }
+