Skip to content

Commit 0b4eb68

Browse files
Rebuild manager forwarded htlcs maps from Channels
XXX
1 parent 389dfe4 commit 0b4eb68

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17035,7 +17035,11 @@ where
1703517035

1703617036
const MAX_ALLOC_SIZE: usize = 1024 * 64;
1703717037
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));
17038+
// This map is read but may no longer be used because we'll attempt to rebuild `forward_htlcs`
17039+
// from the `Channel{Monitor}`s instead, as a step towards getting rid of `ChannelManager`
17040+
// persistence.
17041+
let mut forward_htlcs_legacy: HashMap<u64, Vec<HTLCForwardInfo>> =
17042+
hash_map_with_capacity(cmp::min(forward_htlcs_count as usize, 128));
1703917043
for _ in 0..forward_htlcs_count {
1704017044
let short_channel_id = Readable::read(reader)?;
1704117045
let pending_forwards_count: u64 = Readable::read(reader)?;
@@ -17046,7 +17050,7 @@ where
1704617050
for _ in 0..pending_forwards_count {
1704717051
pending_forwards.push(Readable::read(reader)?);
1704817052
}
17049-
forward_htlcs.insert(short_channel_id, pending_forwards);
17053+
forward_htlcs_legacy.insert(short_channel_id, pending_forwards);
1705017054
}
1705117055

1705217056
let claimable_htlcs_count: u64 = Readable::read(reader)?;
@@ -17134,12 +17138,18 @@ where
1713417138
};
1713517139
}
1713617140

17141+
// Some maps are read but may no longer be used because we attempt to rebuild pending HTLC
17142+
// forwards from the `Channel{Monitor}`s instead, as a step towards getting rid of
17143+
// `ChannelManager` persistence.
17144+
let mut pending_intercepted_htlcs_legacy: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17145+
Some(new_hash_map());
17146+
let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17147+
None;
17148+
1713717149
// pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
1713817150
let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> =
1713917151
None;
1714017152
let mut pending_outbound_payments = None;
17141-
let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> =
17142-
Some(new_hash_map());
1714317153
let mut received_network_pubkey: Option<PublicKey> = None;
1714417154
let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
1714517155
let mut probing_cookie_secret: Option<[u8; 32]> = None;
@@ -17157,14 +17167,12 @@ where
1715717167
let mut in_flight_monitor_updates: Option<
1715817168
HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1715917169
> = None;
17160-
let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17161-
None;
1716217170
let mut inbound_payment_id_secret = None;
1716317171
let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
1716417172
let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
1716517173
read_tlv_fields!(reader, {
1716617174
(1, pending_outbound_payments_no_retry, option),
17167-
(2, pending_intercepted_htlcs, option),
17175+
(2, pending_intercepted_htlcs_legacy, option),
1716817176
(3, pending_outbound_payments, option),
1716917177
(4, pending_claiming_payments, option),
1717017178
(5, received_network_pubkey, option),
@@ -17586,7 +17594,7 @@ where
1758617594
"HTLC was forwarded to the closed channel",
1758717595
&args.logger,
1758817596
);
17589-
forward_htlcs.retain(|_, forwards| {
17597+
forward_htlcs_legacy.retain(|_, forwards| {
1759017598
forwards.retain(|forward| {
1759117599
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
1759217600
if pending_forward_matches_htlc(&htlc_info) {
@@ -17598,7 +17606,7 @@ where
1759817606
});
1759917607
!forwards.is_empty()
1760017608
});
17601-
pending_intercepted_htlcs.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
17609+
pending_intercepted_htlcs_legacy.as_mut().unwrap().retain(|intercepted_id, htlc_info| {
1760217610
if pending_forward_matches_htlc(&htlc_info) {
1760317611
log_info!(logger, "Removing pending intercepted HTLC with hash {} as it was forwarded to the closed channel {}",
1760417612
&htlc.payment_hash, &monitor.channel_id());
@@ -18106,6 +18114,22 @@ where
1810618114
)
1810718115
.with_async_payments_offers_cache(async_receive_offer_cache);
1810818116

18117+
// If we are reading from a `ChannelManager` that was last serialized on LDK 0.2 or earlier, we
18118+
// won't have been able to rebuild `decode_update_add_htlcs` from `Channel`s and should use
18119+
// the legacy serialized maps instead.
18120+
// TODO: if we read an upgraded channel but there just happened to be no committed update_adds
18121+
// present, we'll use the old maps here. Maybe that's fine but we might want to add a flag in
18122+
// the `Channel` that indicates it is upgraded and will serialize committed update_adds.
18123+
let (forward_htlcs, decode_update_add_htlcs, pending_intercepted_htlcs) =
18124+
if decode_update_add_htlcs.is_empty() {
18125+
(
18126+
forward_htlcs_legacy,
18127+
decode_update_add_htlcs_legacy,
18128+
pending_intercepted_htlcs_legacy.unwrap(),
18129+
)
18130+
} else {
18131+
(new_hash_map(), decode_update_add_htlcs, new_hash_map())
18132+
};
1810918133
let channel_manager = ChannelManager {
1811018134
chain_hash,
1811118135
fee_estimator: bounded_fee_estimator,
@@ -18118,10 +18142,10 @@ where
1811818142

1811918143
inbound_payment_key: expanded_inbound_key,
1812018144
pending_outbound_payments: pending_outbounds,
18121-
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
18145+
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs),
1812218146

1812318147
forward_htlcs: Mutex::new(forward_htlcs),
18124-
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs_legacy),
18148+
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
1812518149
claimable_payments: Mutex::new(ClaimablePayments {
1812618150
claimable_payments,
1812718151
pending_claiming_payments: pending_claiming_payments.unwrap(),

0 commit comments

Comments
 (0)