Skip to content

Commit 389dfe4

Browse files
Support rebuilding decode_update_add_htlcs from Channels
XXX doesn't use it yet
1 parent bb5a5a3 commit 389dfe4

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7731,6 +7731,20 @@ where
77317731
Ok(())
77327732
}
77337733

7734+
/// Useful for reconstructing forwarded HTLCs when deserializing the `ChannelManager`.
7735+
pub(super) fn get_inbound_committed_update_adds(&self) -> Vec<msgs::UpdateAddHTLC> {
7736+
self.context
7737+
.pending_inbound_htlcs
7738+
.iter()
7739+
.filter_map(|htlc| match htlc.state {
7740+
InboundHTLCState::Committed { ref update_add_htlc_opt } => {
7741+
update_add_htlc_opt.clone()
7742+
},
7743+
_ => None,
7744+
})
7745+
.collect()
7746+
}
7747+
77347748
/// Marks an outbound HTLC which we have received update_fail/fulfill/malformed
77357749
#[inline]
77367750
#[rustfmt::skip]

lightning/src/ln/channelmanager.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17157,7 +17157,8 @@ where
1715717157
let mut in_flight_monitor_updates: Option<
1715817158
HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1715917159
> = None;
17160-
let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
17160+
let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17161+
None;
1716117162
let mut inbound_payment_id_secret = None;
1716217163
let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
1716317164
let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
@@ -17174,13 +17175,15 @@ where
1717417175
(10, legacy_in_flight_monitor_updates, option),
1717517176
(11, probing_cookie_secret, option),
1717617177
(13, claimable_htlc_onion_fields, optional_vec),
17177-
(14, decode_update_add_htlcs, option),
17178+
(14, decode_update_add_htlcs_legacy, option),
1717817179
(15, inbound_payment_id_secret, option),
1717917180
(17, in_flight_monitor_updates, option),
1718017181
(19, peer_storage_dir, optional_vec),
1718117182
(21, async_receive_offer_cache, (default_value, async_receive_offer_cache)),
1718217183
});
17183-
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
17184+
let mut decode_update_add_htlcs_legacy =
17185+
decode_update_add_htlcs_legacy.unwrap_or_else(|| new_hash_map());
17186+
let mut decode_update_add_htlcs = new_hash_map();
1718417187
let peer_storage_dir: Vec<(PublicKey, Vec<u8>)> = peer_storage_dir.unwrap_or_else(Vec::new);
1718517188
if fake_scid_rand_bytes.is_none() {
1718617189
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
@@ -17491,7 +17494,25 @@ where
1749117494
if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
1749217495
let mut peer_state_lock = peer_state_mtx.lock().unwrap();
1749317496
let peer_state = &mut *peer_state_lock;
17494-
is_channel_closed = !peer_state.channel_by_id.contains_key(channel_id);
17497+
is_channel_closed = match peer_state.channel_by_id.get(channel_id) {
17498+
Some(chan) => {
17499+
if let Some(funded_chan) = chan.as_funded() {
17500+
let inbound_committed_update_adds =
17501+
funded_chan.get_inbound_committed_update_adds();
17502+
if !inbound_committed_update_adds.is_empty() {
17503+
// Reconstruct `ChannelManager::decode_update_add_htlcs` from the serialized
17504+
// `Channel`. We are moving away from writing the `decode_update_add_htlcs` map as
17505+
// part of getting rid of `ChannelManager` persistence.
17506+
decode_update_add_htlcs.insert(
17507+
funded_chan.context.outbound_scid_alias(),
17508+
inbound_committed_update_adds,
17509+
);
17510+
}
17511+
}
17512+
false
17513+
},
17514+
None => true,
17515+
};
1749517516
}
1749617517

1749717518
if is_channel_closed {
@@ -17559,6 +17580,12 @@ where
1755917580
"HTLC was forwarded to the closed channel",
1756017581
&args.logger,
1756117582
);
17583+
dedup_decode_update_add_htlcs(
17584+
&mut decode_update_add_htlcs_legacy,
17585+
&prev_hop_data,
17586+
"HTLC was forwarded to the closed channel",
17587+
&args.logger,
17588+
);
1756217589
forward_htlcs.retain(|_, forwards| {
1756317590
forwards.retain(|forward| {
1756417591
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
@@ -17793,6 +17820,17 @@ where
1779317820
}
1779417821
}
1779517822

17823+
for (src, _, _, _, _, _) in failed_htlcs.iter() {
17824+
if let HTLCSource::PreviousHopData(prev_hop_data) = src {
17825+
dedup_decode_update_add_htlcs(
17826+
&mut decode_update_add_htlcs,
17827+
prev_hop_data,
17828+
"HTLC was failed backwards during manager read",
17829+
&args.logger,
17830+
);
17831+
}
17832+
}
17833+
1779617834
let expanded_inbound_key = args.node_signer.get_expanded_key();
1779717835

1779817836
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
@@ -18043,6 +18081,17 @@ where
1804318081
}
1804418082
}
1804518083

18084+
for htlcs in claimable_payments.values().map(|pmt| &pmt.htlcs) {
18085+
for prev_hop_data in htlcs.iter().map(|h| &h.prev_hop) {
18086+
dedup_decode_update_add_htlcs(
18087+
&mut decode_update_add_htlcs,
18088+
prev_hop_data,
18089+
"HTLC was already decoded and marked as a claimable payment",
18090+
&args.logger,
18091+
);
18092+
}
18093+
}
18094+
1804618095
let best_block = BestBlock::new(best_block_hash, best_block_height);
1804718096
let flow = OffersMessageFlow::new(
1804818097
chain_hash,
@@ -18072,7 +18121,7 @@ where
1807218121
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
1807318122

1807418123
forward_htlcs: Mutex::new(forward_htlcs),
18075-
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
18124+
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs_legacy),
1807618125
claimable_payments: Mutex::new(ClaimablePayments {
1807718126
claimable_payments,
1807818127
pending_claiming_payments: pending_claiming_payments.unwrap(),

0 commit comments

Comments
 (0)