Skip to content

Commit b509c4a

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 07bcba9 commit b509c4a

File tree

1 file changed

+48
-56
lines changed

1 file changed

+48
-56
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17236,7 +17236,7 @@ pub(super) struct ChannelManagerData<SP: SignerProvider> {
1723617236
fake_scid_rand_bytes: Option<[u8; 32]>,
1723717237
probing_cookie_secret: Option<[u8; 32]>,
1723817238
inbound_payment_id_secret: Option<[u8; 32]>,
17239-
in_flight_monitor_updates: Option<HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>>,
17239+
in_flight_monitor_updates: HashMap<(PublicKey, ChannelId), Vec<ChannelMonitorUpdate>>,
1724017240
peer_storage_dir: Vec<(PublicKey, Vec<u8>)>,
1724117241
async_receive_offer_cache: AsyncReceiveOfferCache,
1724217242
// Marked `_legacy` because in versions > 0.2 we are taking steps to remove the requirement of
@@ -17581,7 +17581,7 @@ impl<'a, ES: EntropySource, NS: NodeSigner, SP: SignerProvider, L: Logger>
1758117581
decode_update_add_htlcs_legacy: decode_update_add_htlcs_legacy
1758217582
.unwrap_or_else(new_hash_map),
1758317583
inbound_payment_id_secret,
17584-
in_flight_monitor_updates,
17584+
in_flight_monitor_updates: in_flight_monitor_updates.unwrap_or_default(),
1758517585
peer_storage_dir: peer_storage_dir.unwrap_or_default(),
1758617586
async_receive_offer_cache,
1758717587
})
@@ -18258,22 +18258,20 @@ impl<
1825818258
.get(chan_id)
1825918259
.expect("We already checked for monitor presence when loading channels");
1826018260
let mut max_in_flight_update_id = monitor.get_latest_update_id();
18261-
if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
18262-
if let Some(mut chan_in_flight_upds) =
18263-
in_flight_upds.remove(&(*counterparty_id, *chan_id))
18264-
{
18265-
max_in_flight_update_id = cmp::max(
18266-
max_in_flight_update_id,
18267-
handle_in_flight_updates!(
18268-
*counterparty_id,
18269-
chan_in_flight_upds,
18270-
monitor,
18271-
peer_state,
18272-
logger,
18273-
""
18274-
),
18275-
);
18276-
}
18261+
if let Some(mut chan_in_flight_upds) =
18262+
in_flight_monitor_updates.remove(&(*counterparty_id, *chan_id))
18263+
{
18264+
max_in_flight_update_id = cmp::max(
18265+
max_in_flight_update_id,
18266+
handle_in_flight_updates!(
18267+
*counterparty_id,
18268+
chan_in_flight_upds,
18269+
monitor,
18270+
peer_state,
18271+
logger,
18272+
""
18273+
),
18274+
);
1827718275
}
1827818276
if funded_chan.get_latest_unblocked_monitor_update_id()
1827918277
> max_in_flight_update_id
@@ -18302,44 +18300,38 @@ impl<
1830218300
}
1830318301
}
1830418302

18305-
if let Some(in_flight_upds) = in_flight_monitor_updates {
18306-
for ((counterparty_id, channel_id), mut chan_in_flight_updates) in in_flight_upds {
18307-
let logger =
18308-
WithContext::from(&args.logger, Some(counterparty_id), Some(channel_id), None);
18309-
if let Some(monitor) = args.channel_monitors.get(&channel_id) {
18310-
// Now that we've removed all the in-flight monitor updates for channels that are
18311-
// still open, we need to replay any monitor updates that are for closed channels,
18312-
// creating the neccessary peer_state entries as we go.
18313-
let peer_state_mutex = per_peer_state
18314-
.entry(counterparty_id)
18315-
.or_insert_with(|| Mutex::new(empty_peer_state()));
18316-
let mut peer_state = peer_state_mutex.lock().unwrap();
18317-
handle_in_flight_updates!(
18318-
counterparty_id,
18319-
chan_in_flight_updates,
18320-
monitor,
18321-
peer_state,
18322-
logger,
18323-
"closed "
18324-
);
18325-
} else {
18326-
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!");
18327-
log_error!(
18328-
logger,
18329-
" The ChannelMonitor for channel {} is missing.",
18330-
channel_id
18331-
);
18332-
log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
18333-
log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
18334-
log_error!(logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
18335-
log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
18336-
log_error!(
18337-
logger,
18338-
" Pending in-flight updates are: {:?}",
18339-
chan_in_flight_updates
18340-
);
18341-
return Err(DecodeError::InvalidValue);
18342-
}
18303+
for ((counterparty_id, channel_id), mut chan_in_flight_updates) in in_flight_monitor_updates
18304+
{
18305+
let logger =
18306+
WithContext::from(&args.logger, Some(counterparty_id), Some(channel_id), None);
18307+
if let Some(monitor) = args.channel_monitors.get(&channel_id) {
18308+
// Now that we've removed all the in-flight monitor updates for channels that are
18309+
// still open, we need to replay any monitor updates that are for closed channels,
18310+
// creating the neccessary peer_state entries as we go.
18311+
let peer_state_mutex = per_peer_state
18312+
.entry(counterparty_id)
18313+
.or_insert_with(|| Mutex::new(empty_peer_state()));
18314+
let mut peer_state = peer_state_mutex.lock().unwrap();
18315+
handle_in_flight_updates!(
18316+
counterparty_id,
18317+
chan_in_flight_updates,
18318+
monitor,
18319+
peer_state,
18320+
logger,
18321+
"closed "
18322+
);
18323+
} else {
18324+
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!");
18325+
log_error!(logger, " The ChannelMonitor for channel {} is missing.", channel_id);
18326+
log_error!(logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
18327+
log_error!(logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
18328+
log_error!(
18329+
logger,
18330+
" Without the latest ChannelMonitor we cannot continue without risking funds."
18331+
);
18332+
log_error!(logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
18333+
log_error!(logger, " Pending in-flight updates are: {:?}", chan_in_flight_updates);
18334+
return Err(DecodeError::InvalidValue);
1834318335
}
1834418336
}
1834518337

0 commit comments

Comments
 (0)