@@ -11509,6 +11509,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1150911509
1151011510 if !new_intercept_events.is_empty() {
1151111511 let mut events = self.pending_events.lock().unwrap();
11512+ new_intercept_events.retain(|new_ev| !events.contains(new_ev));
1151211513 events.append(&mut new_intercept_events);
1151311514 }
1151411515 }
@@ -17153,7 +17154,11 @@ where
1715317154
1715417155 const MAX_ALLOC_SIZE: usize = 1024 * 64;
1715517156 let forward_htlcs_count: u64 = Readable::read(reader)?;
17156- let mut forward_htlcs = hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
17157+ // This map is read but may no longer be used because we'll attempt to rebuild `forward_htlcs`
17158+ // from the `Channel{Monitor}`s instead, as a step towards getting rid of `ChannelManager`
17159+ // persistence.
17160+ let mut forward_htlcs_legacy: HashMap<u64, Vec<HTLCForwardInfo>> =
17161+ hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
1715717162 for _ in 0..forward_htlcs_count {
1715817163 let short_channel_id = Readable::read(reader)?;
1715917164 let pending_forwards_count: u64 = Readable::read(reader)?;
@@ -17164,7 +17169,7 @@ where
1716417169 for _ in 0..pending_forwards_count {
1716517170 pending_forwards.push(Readable::read(reader)?);
1716617171 }
17167- forward_htlcs .insert(short_channel_id, pending_forwards);
17172+ forward_htlcs_legacy .insert(short_channel_id, pending_forwards);
1716817173 }
1716917174
1717017175 let claimable_htlcs_count: u64 = Readable::read(reader)?;
@@ -17252,12 +17257,18 @@ where
1725217257 };
1725317258 }
1725417259
17260+ // Some maps are read but may no longer be used because we attempt to rebuild pending HTLC
17261+ // forwards from the `Channel{Monitor}`s instead, as a step towards getting rid of
17262+ // `ChannelManager` persistence.
17263+ let mut pending_intercepted_htlcs_legacy: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17264+ Some(new_hash_map());
17265+ let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17266+ None;
17267+
1725517268 // pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
1725617269 let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> =
1725717270 None;
1725817271 let mut pending_outbound_payments = None;
17259- let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17260- Some(new_hash_map());
1726117272 let mut received_network_pubkey: Option<PublicKey> = None;
1726217273 let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
1726317274 let mut probing_cookie_secret: Option<[u8; 32]> = None;
@@ -17275,14 +17286,12 @@ where
1727517286 let mut in_flight_monitor_updates: Option<
1727617287 HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1727717288 > = None;
17278- let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17279- None;
1728017289 let mut inbound_payment_id_secret = None;
1728117290 let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
1728217291 let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
1728317292 read_tlv_fields!(reader, {
1728417293 (1, pending_outbound_payments_no_retry, option),
17285- (2, pending_intercepted_htlcs , option),
17294+ (2, pending_intercepted_htlcs_legacy , option),
1728617295 (3, pending_outbound_payments, option),
1728717296 (4, pending_claiming_payments, option),
1728817297 (5, received_network_pubkey, option),
@@ -17704,7 +17713,7 @@ where
1770417713 "HTLC was forwarded to the closed channel",
1770517714 &args.logger,
1770617715 );
17707- forward_htlcs .retain(|_, forwards| {
17716+ forward_htlcs_legacy .retain(|_, forwards| {
1770817717 forwards.retain(|forward| {
1770917718 if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
1771017719 if pending_forward_matches_htlc(&htlc_info) {
@@ -17716,7 +17725,7 @@ where
1771617725 });
1771717726 !forwards.is_empty()
1771817727 });
17719- pending_intercepted_htlcs .as_mut().unwrap().retain(|intercepted_id, htlc_info| {
17728+ pending_intercepted_htlcs_legacy .as_mut().unwrap().retain(|intercepted_id, htlc_info| {
1772017729 if pending_forward_matches_htlc(&htlc_info) {
1772117730 log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
1772217731 &htlc.payment_hash, &monitor.channel_id());
@@ -18224,6 +18233,22 @@ where
1822418233 )
1822518234 .with_async_payments_offers_cache(async_receive_offer_cache);
1822618235
18236+ // If we are reading from a `ChannelManager` that was last serialized on LDK 0.2 or earlier, we
18237+ // won't have been able to rebuild `decode_update_add_htlcs` from `Channel`s and should use
18238+ // the legacy serialized maps instead.
18239+ // TODO: if we read an upgraded channel but there just happened to be no committed update_adds
18240+ // present, we'll use the old maps here. Maybe that's fine but we might want to add a flag in
18241+ // the `Channel` that indicates it is upgraded and will serialize committed update_adds.
18242+ let (forward_htlcs, decode_update_add_htlcs, pending_intercepted_htlcs) =
18243+ if decode_update_add_htlcs.is_empty() {
18244+ (
18245+ forward_htlcs_legacy,
18246+ decode_update_add_htlcs_legacy,
18247+ pending_intercepted_htlcs_legacy.unwrap(),
18248+ )
18249+ } else {
18250+ (new_hash_map(), decode_update_add_htlcs, new_hash_map())
18251+ };
1822718252 let channel_manager = ChannelManager {
1822818253 chain_hash,
1822918254 fee_estimator: bounded_fee_estimator,
@@ -18236,10 +18261,10 @@ where
1823618261
1823718262 inbound_payment_key: expanded_inbound_key,
1823818263 pending_outbound_payments: pending_outbounds,
18239- pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap() ),
18264+ pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs),
1824018265
1824118266 forward_htlcs: Mutex::new(forward_htlcs),
18242- decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs_legacy ),
18267+ decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs ),
1824318268 claimable_payments: Mutex::new(ClaimablePayments {
1824418269 claimable_payments,
1824518270 pending_claiming_payments: pending_claiming_payments.unwrap(),
0 commit comments