@@ -11402,6 +11402,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1140211402
1140311403 if !new_intercept_events.is_empty() {
1140411404 let mut events = self.pending_events.lock().unwrap();
11405+ new_intercept_events.retain(|new_ev| !events.contains(new_ev));
1140511406 events.append(&mut new_intercept_events);
1140611407 }
1140711408 }
@@ -17035,7 +17036,11 @@ where
1703517036
1703617037 const MAX_ALLOC_SIZE: usize = 1024 * 64;
1703717038 let forward_htlcs_count: u64 = Readable::read(reader)?;
17038- let mut forward_htlcs = hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
17039+ // This map is read but may no longer be used because we'll attempt to rebuild `forward_htlcs`
17040+ // from the `Channel{Monitor}`s instead, as a step towards getting rid of `ChannelManager`
17041+ // persistence.
17042+ let mut forward_htlcs_legacy: HashMap<u64, Vec<HTLCForwardInfo>> =
17043+ hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
1703917044 for _ in 0..forward_htlcs_count {
1704017045 let short_channel_id = Readable::read(reader)?;
1704117046 let pending_forwards_count: u64 = Readable::read(reader)?;
@@ -17046,7 +17051,7 @@ where
1704617051 for _ in 0..pending_forwards_count {
1704717052 pending_forwards.push(Readable::read(reader)?);
1704817053 }
17049- forward_htlcs .insert(short_channel_id, pending_forwards);
17054+ forward_htlcs_legacy .insert(short_channel_id, pending_forwards);
1705017055 }
1705117056
1705217057 let claimable_htlcs_count: u64 = Readable::read(reader)?;
@@ -17134,12 +17139,18 @@ where
1713417139 };
1713517140 }
1713617141
17142+ // Some maps are read but may no longer be used because we attempt to rebuild pending HTLC
17143+ // forwards from the `Channel{Monitor}`s instead, as a step towards getting rid of
17144+ // `ChannelManager` persistence.
17145+ let mut pending_intercepted_htlcs_legacy: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17146+ Some(new_hash_map());
17147+ let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17148+ None;
17149+
1713717150 // pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
1713817151 let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> =
1713917152 None;
1714017153 let mut pending_outbound_payments = None;
17141- let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17142- Some(new_hash_map());
1714317154 let mut received_network_pubkey: Option<PublicKey> = None;
1714417155 let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
1714517156 let mut probing_cookie_secret: Option<[u8; 32]> = None;
@@ -17157,14 +17168,12 @@ where
1715717168 let mut in_flight_monitor_updates: Option<
1715817169 HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1715917170 > = None;
17160- let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17161- None;
1716217171 let mut inbound_payment_id_secret = None;
1716317172 let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
1716417173 let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
1716517174 read_tlv_fields!(reader, {
1716617175 (1, pending_outbound_payments_no_retry, option),
17167- (2, pending_intercepted_htlcs , option),
17176+ (2, pending_intercepted_htlcs_legacy , option),
1716817177 (3, pending_outbound_payments, option),
1716917178 (4, pending_claiming_payments, option),
1717017179 (5, received_network_pubkey, option),
@@ -17586,7 +17595,7 @@ where
1758617595 "HTLC was forwarded to the closed channel",
1758717596 &args.logger,
1758817597 );
17589- forward_htlcs .retain(|_, forwards| {
17598+ forward_htlcs_legacy .retain(|_, forwards| {
1759017599 forwards.retain(|forward| {
1759117600 if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
1759217601 if pending_forward_matches_htlc(&htlc_info) {
@@ -17598,7 +17607,7 @@ where
1759817607 });
1759917608 !forwards.is_empty()
1760017609 });
17601- pending_intercepted_htlcs .as_mut().unwrap().retain(|intercepted_id, htlc_info| {
17610+ pending_intercepted_htlcs_legacy .as_mut().unwrap().retain(|intercepted_id, htlc_info| {
1760217611 if pending_forward_matches_htlc(&htlc_info) {
1760317612 log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
1760417613 &htlc.payment_hash, &monitor.channel_id());
@@ -18106,6 +18115,22 @@ where
1810618115 )
1810718116 .with_async_payments_offers_cache(async_receive_offer_cache);
1810818117
18118+ // If we are reading from a `ChannelManager` that was last serialized on LDK 0.2 or earlier, we
18119+ // won't have been able to rebuild `decode_update_add_htlcs` from `Channel`s and should use
18120+ // the legacy serialized maps instead.
18121+ // TODO: if we read an upgraded channel but there just happened to be no committed update_adds
18122+ // present, we'll use the old maps here. Maybe that's fine but we might want to add a flag in
18123+ // the `Channel` that indicates it is upgraded and will serialize committed update_adds.
18124+ let (forward_htlcs, decode_update_add_htlcs, pending_intercepted_htlcs) =
18125+ if decode_update_add_htlcs.is_empty() {
18126+ (
18127+ forward_htlcs_legacy,
18128+ decode_update_add_htlcs_legacy,
18129+ pending_intercepted_htlcs_legacy.unwrap(),
18130+ )
18131+ } else {
18132+ (new_hash_map(), decode_update_add_htlcs, new_hash_map())
18133+ };
1810918134 let channel_manager = ChannelManager {
1811018135 chain_hash,
1811118136 fee_estimator: bounded_fee_estimator,
@@ -18118,10 +18143,10 @@ where
1811818143
1811918144 inbound_payment_key: expanded_inbound_key,
1812018145 pending_outbound_payments: pending_outbounds,
18121- pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap() ),
18146+ pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs),
1812218147
1812318148 forward_htlcs: Mutex::new(forward_htlcs),
18124- decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs_legacy ),
18149+ decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs ),
1812518150 claimable_payments: Mutex::new(ClaimablePayments {
1812618151 claimable_payments,
1812718152 pending_claiming_payments: pending_claiming_payments.unwrap(),
0 commit comments