Skip to content

Commit b4429bb

Browse files
joostjagerclaude
andcommitted
Simplify in_flight_monitor_updates from Option<HashMap> to HashMap
Since there is no semantic difference between None and Some(empty map) for in_flight_monitor_updates, simplify the type to HashMap and use unwrap_or_default() when reading from TLV fields. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8014428 commit b4429bb

1 file changed

Lines changed: 48 additions & 56 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17237,7 +17237,7 @@ pub(super) struct ChannelManagerData<SP: SignerProvider> {
1723717237
claimable_htlc_purposes: Option<Vec<events::PaymentPurpose>>,
1723817238
probing_cookie_secret: Option<[u8; 32]>,
1723917239
inbound_payment_id_secret: Option<[u8; 32]>,
17240-
in_flight_monitor_updates: Option<HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>>,
17240+
in_flight_monitor_updates: HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1724117241
peer_storage_dir: Vec<(PublicKey, Vec<u8>)>,
1724217242
async_receive_offer_cache: AsyncReceiveOfferCache,
1724317243
// Marked `_legacy` because in versions > 0.2 we are taking steps to remove the requirement of
@@ -17517,7 +17517,7 @@ impl<'a, ES: EntropySource, SP: SignerProvider, L: Logger>
1751717517
decode_update_add_htlcs_legacy: decode_update_add_htlcs_legacy
1751817518
.unwrap_or_else(new_hash_map),
1751917519
inbound_payment_id_secret,
17520-
in_flight_monitor_updates,
17520+
in_flight_monitor_updates: in_flight_monitor_updates.unwrap_or_default(),
1752117521
peer_storage_dir: peer_storage_dir.unwrap_or_default(),
1752217522
async_receive_offer_cache,
1752317523
})
@@ -18205,22 +18205,20 @@ impl<
1820518205
.get(chan_id)
1820618206
.expect("We already checked for monitor presence when loading channels");
1820718207
let mut max_in_flight_update_id = monitor.get_latest_update_id();
18208-
if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
18209-
if let Some(mut chan_in_flight_upds) =
18210-
in_flight_upds.remove(&(*counterparty_id, *chan_id))
18211-
{
18212-
max_in_flight_update_id = cmp::max(
18213-
max_in_flight_update_id,
18214-
handle_in_flight_updates!(
18215-
*counterparty_id,
18216-
chan_in_flight_upds,
18217-
monitor,
18218-
peer_state,
18219-
logger,
18220-
""
18221-
),
18222-
);
18223-
}
18208+
if let Some(mut chan_in_flight_upds) =
18209+
in_flight_monitor_updates.remove(&(*counterparty_id, *chan_id))
18210+
{
18211+
max_in_flight_update_id = cmp::max(
18212+
max_in_flight_update_id,
18213+
handle_in_flight_updates!(
18214+
*counterparty_id,
18215+
chan_in_flight_upds,
18216+
monitor,
18217+
peer_state,
18218+
logger,
18219+
""
18220+
),
18221+
);
1822418222
}
1822518223
if funded_chan.get_latest_unblocked_monitor_update_id()
1822618224
> max_in_flight_update_id
@@ -18249,44 +18247,38 @@ impl<
1824918247
}
1825018248
}
1825118249

18252-
if let Some(in_flight_upds) = in_flight_monitor_updates {
18253-
for ((counterparty_id, channel_id), mut chan_in_flight_updates) in in_flight_upds {
18254-
let logger =
18255-
WithContext::from(&args.logger, Some(counterparty_id), Some(channel_id), None);
18256-
if let Some(monitor) = args.channel_monitors.get(&channel_id) {
18257-
// Now that we've removed all the in-flight monitor updates for channels that are
18258-
// still open, we need to replay any monitor updates that are for closed channels,
18259-
// creating the neccessary peer_state entries as we go.
18260-
let peer_state_mutex = per_peer_state
18261-
.entry(counterparty_id)
18262-
.or_insert_with(|| Mutex::new(empty_peer_state()));
18263-
let mut peer_state = peer_state_mutex.lock().unwrap();
18264-
handle_in_flight_updates!(
18265-
counterparty_id,
18266-
chan_in_flight_updates,
18267-
monitor,
18268-
peer_state,
18269-
logger,
18270-
"closed "
18271-
);
18272-
} else {
18273-
log_error!(logger, "A ChannelMonitor is missing even though we have in-flight updates for it! This indicates a potentially-critical violation of the chain::Watch API!");
18274-
log_error!(
18275-
logger,
18276-
" The ChannelMonitor for channel {} is missing.",
18277-
channel_id
18278-
);
18279-
log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
18280-
log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
18281-
log_error!(logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
18282-
log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
18283-
log_error!(
18284-
logger,
18285-
" Pending in-flight updates are: {:?}",
18286-
chan_in_flight_updates
18287-
);
18288-
return Err(DecodeError::InvalidValue);
18289-
}
18250+
for ((counterparty_id, channel_id), mut chan_in_flight_updates) in in_flight_monitor_updates
18251+
{
18252+
let logger =
18253+
WithContext::from(&args.logger, Some(counterparty_id), Some(channel_id), None);
18254+
if let Some(monitor) = args.channel_monitors.get(&channel_id) {
18255+
// Now that we've removed all the in-flight monitor updates for channels that are
18256+
// still open, we need to replay any monitor updates that are for closed channels,
18257+
// creating the neccessary peer_state entries as we go.
18258+
let peer_state_mutex = per_peer_state
18259+
.entry(counterparty_id)
18260+
.or_insert_with(|| Mutex::new(empty_peer_state()));
18261+
let mut peer_state = peer_state_mutex.lock().unwrap();
18262+
handle_in_flight_updates!(
18263+
counterparty_id,
18264+
chan_in_flight_updates,
18265+
monitor,
18266+
peer_state,
18267+
logger,
18268+
"closed "
18269+
);
18270+
} else {
18271+
log_error!(logger, "A ChannelMonitor is missing even though we have in-flight updates for it! This indicates a potentially-critical violation of the chain::Watch API!");
18272+
log_error!(logger, " The ChannelMonitor for channel {} is missing.", channel_id);
18273+
log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
18274+
log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
18275+
log_error!(
18276+
logger,
18277+
" Without the latest ChannelMonitor we cannot continue without risking funds."
18278+
);
18279+
log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
18280+
log_error!(logger, " Pending in-flight updates are: {:?}", chan_in_flight_updates);
18281+
return Err(DecodeError::InvalidValue);
1829018282
}
1829118283
}
1829218284

0 commit comments

Comments
 (0)