Skip to content

Commit 9a7c3f5

Browse files
Support rebuilding decode_update_add_htlcs from Channels
XXX doesn't use it yet
1 parent d023f37 commit 9a7c3f5

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
@@ -7756,6 +7756,20 @@ where
77567756
Ok(())
77577757
}
77587758

7759+
/// Useful for reconstructing forwarded HTLCs when deserializing the `ChannelManager`.
7760+
pub(super) fn get_inbound_committed_update_adds(&self) -> Vec<msgs::UpdateAddHTLC> {
7761+
self.context
7762+
.pending_inbound_htlcs
7763+
.iter()
7764+
.filter_map(|htlc| match htlc.state {
7765+
InboundHTLCState::Committed { ref update_add_htlc_opt } => {
7766+
update_add_htlc_opt.clone()
7767+
},
7768+
_ => None,
7769+
})
7770+
.collect()
7771+
}
7772+
77597773
/// Marks an outbound HTLC which we have received update_fail/fulfill/malformed
77607774
#[inline]
77617775
#[rustfmt::skip]

lightning/src/ln/channelmanager.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17275,7 +17275,8 @@ where
1727517275
let mut in_flight_monitor_updates: Option<
1727617276
HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1727717277
> = None;
17278-
let mut decode_update_add_htlcs: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> = None;
17278+
let mut decode_update_add_htlcs_legacy: Option<HashMap<u64, Vec<msgs::UpdateAddHTLC>>> =
17279+
None;
1727917280
let mut inbound_payment_id_secret = None;
1728017281
let mut peer_storage_dir: Option<Vec<(PublicKey, Vec<u8>)>> = None;
1728117282
let mut async_receive_offer_cache: AsyncReceiveOfferCache = AsyncReceiveOfferCache::new();
@@ -17292,13 +17293,15 @@ where
1729217293
(10, legacy_in_flight_monitor_updates, option),
1729317294
(11, probing_cookie_secret, option),
1729417295
(13, claimable_htlc_onion_fields, optional_vec),
17295-
(14, decode_update_add_htlcs, option),
17296+
(14, decode_update_add_htlcs_legacy, option),
1729617297
(15, inbound_payment_id_secret, option),
1729717298
(17, in_flight_monitor_updates, option),
1729817299
(19, peer_storage_dir, optional_vec),
1729917300
(21, async_receive_offer_cache, (default_value, async_receive_offer_cache)),
1730017301
});
17301-
let mut decode_update_add_htlcs = decode_update_add_htlcs.unwrap_or_else(|| new_hash_map());
17302+
let mut decode_update_add_htlcs_legacy =
17303+
decode_update_add_htlcs_legacy.unwrap_or_else(|| new_hash_map());
17304+
let mut decode_update_add_htlcs = new_hash_map();
1730217305
let peer_storage_dir: Vec<(PublicKey, Vec<u8>)> = peer_storage_dir.unwrap_or_else(Vec::new);
1730317306
if fake_scid_rand_bytes.is_none() {
1730417307
fake_scid_rand_bytes = Some(args.entropy_source.get_secure_random_bytes());
@@ -17609,7 +17612,25 @@ where
1760917612
if let Some(peer_state_mtx) = per_peer_state.get(&counterparty_node_id) {
1761017613
let mut peer_state_lock = peer_state_mtx.lock().unwrap();
1761117614
let peer_state = &mut *peer_state_lock;
17612-
is_channel_closed = !peer_state.channel_by_id.contains_key(channel_id);
17615+
is_channel_closed = match peer_state.channel_by_id.get(channel_id) {
17616+
Some(chan) => {
17617+
if let Some(funded_chan) = chan.as_funded() {
17618+
let inbound_committed_update_adds =
17619+
funded_chan.get_inbound_committed_update_adds();
17620+
if !inbound_committed_update_adds.is_empty() {
17621+
// Reconstruct `ChannelManager::decode_update_add_htlcs` from the serialized
17622+
// `Channel`. We are moving away from writing the `decode_update_add_htlcs` map as
17623+
// part of getting rid of `ChannelManager` persistence.
17624+
decode_update_add_htlcs.insert(
17625+
funded_chan.context.outbound_scid_alias(),
17626+
inbound_committed_update_adds,
17627+
);
17628+
}
17629+
}
17630+
false
17631+
},
17632+
None => true,
17633+
};
1761317634
}
1761417635

1761517636
if is_channel_closed {
@@ -17677,6 +17698,12 @@ where
1767717698
"HTLC was forwarded to the closed channel",
1767817699
&args.logger,
1767917700
);
17701+
dedup_decode_update_add_htlcs(
17702+
&mut decode_update_add_htlcs_legacy,
17703+
&prev_hop_data,
17704+
"HTLC was forwarded to the closed channel",
17705+
&args.logger,
17706+
);
1768017707
forward_htlcs.retain(|_, forwards| {
1768117708
forwards.retain(|forward| {
1768217709
if let HTLCForwardInfo::AddHTLC(htlc_info) = forward {
@@ -17911,6 +17938,17 @@ where
1791117938
}
1791217939
}
1791317940

17941+
for (src, _, _, _, _, _) in failed_htlcs.iter() {
17942+
if let HTLCSource::PreviousHopData(prev_hop_data) = src {
17943+
dedup_decode_update_add_htlcs(
17944+
&mut decode_update_add_htlcs,
17945+
prev_hop_data,
17946+
"HTLC was failed backwards during manager read",
17947+
&args.logger,
17948+
);
17949+
}
17950+
}
17951+
1791417952
let expanded_inbound_key = args.node_signer.get_expanded_key();
1791517953

1791617954
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
@@ -18161,6 +18199,17 @@ where
1816118199
}
1816218200
}
1816318201

18202+
for htlcs in claimable_payments.values().map(|pmt| &pmt.htlcs) {
18203+
for prev_hop_data in htlcs.iter().map(|h| &h.prev_hop) {
18204+
dedup_decode_update_add_htlcs(
18205+
&mut decode_update_add_htlcs,
18206+
prev_hop_data,
18207+
"HTLC was already decoded and marked as a claimable payment",
18208+
&args.logger,
18209+
);
18210+
}
18211+
}
18212+
1816418213
let best_block = BestBlock::new(best_block_hash, best_block_height);
1816518214
let flow = OffersMessageFlow::new(
1816618215
chain_hash,
@@ -18190,7 +18239,7 @@ where
1819018239
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
1819118240

1819218241
forward_htlcs: Mutex::new(forward_htlcs),
18193-
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs),
18242+
decode_update_add_htlcs: Mutex::new(decode_update_add_htlcs_legacy),
1819418243
claimable_payments: Mutex::new(ClaimablePayments {
1819518244
claimable_payments,
1819618245
pending_claiming_payments: pending_claiming_payments.unwrap(),

0 commit comments

Comments
 (0)