Skip to content

Commit b143f49

Browse files
committed
f (marginally) macro-ize common code
1 parent 13931da commit b143f49

File tree

1 file changed

+53
-46
lines changed

1 file changed

+53
-46
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8397,44 +8397,63 @@ where
83978397
// In order to do so we first walk all of our live channels (so that we can check their
83988398
// state immediately after doing the update replays, when we have the `update_id`s
83998399
// available) and then walk any remaining in-flight updates.
8400+
//
8401+
// Because tha actual handling of the in-flight updates is the same, its macro'ized here:
84008402
let mut pending_background_events = Vec::new();
8403+
macro_rules! handle_in_flight_updates {
8404+
($counterparty_node_id: expr, $chan_in_flight_upds: expr, $funding_txo: expr,
8405+
$monitor: expr, $peer_state: expr, $channel_info_log: expr
8406+
) => { {
8407+
let mut max_in_flight_update_id = 0;
8408+
$chan_in_flight_upds.retain(|upd| upd.update_id > $monitor.get_latest_update_id());
8409+
for update in $chan_in_flight_upds.iter() {
8410+
log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for {}channel {}",
8411+
update.update_id, $channel_info_log, log_bytes!($funding_txo.to_channel_id()));
8412+
max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
8413+
pending_background_events.push(
8414+
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8415+
counterparty_node_id: $counterparty_node_id,
8416+
funding_txo: $funding_txo,
8417+
update: update.clone(),
8418+
});
8419+
}
8420+
if $peer_state.in_flight_monitor_updates.insert($funding_txo, $chan_in_flight_upds).is_some() {
8421+
log_error!(args.logger, "Duplicate in-flight monitor update set for the same channel!");
8422+
return Err(DecodeError::InvalidValue);
8423+
}
8424+
max_in_flight_update_id
8425+
} }
8426+
}
8427+
84018428
for (counterparty_id, peer_state_mtx) in per_peer_state.iter_mut() {
84028429
let mut peer_state_lock = peer_state_mtx.lock().unwrap();
84038430
let peer_state = &mut *peer_state_lock;
84048431
for (_, chan) in peer_state.channel_by_id.iter() {
8405-
if let Some(funding_txo) = chan.context.get_funding_txo() {
8406-
let monitor = args.channel_monitors.get(&funding_txo)
8407-
.expect("We already checked for monitor presence when loading channels");
8408-
let mut max_in_flight_update_id = monitor.get_latest_update_id();
8409-
if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
8410-
if let Some(mut chan_in_flight_upds) = in_flight_upds.remove(&(*counterparty_id, funding_txo)) {
8411-
chan_in_flight_upds.retain(|upd| upd.update_id > monitor.get_latest_update_id());
8412-
for update in chan_in_flight_upds.iter() {
8413-
log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for channel {}",
8414-
update.update_id, log_bytes!(funding_txo.to_channel_id()));
8415-
max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
8416-
pending_background_events.push(
8417-
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8418-
counterparty_node_id: *counterparty_id,
8419-
funding_txo, update: update.clone(),
8420-
});
8421-
}
8422-
peer_state.in_flight_monitor_updates.insert(funding_txo, chan_in_flight_upds);
8423-
}
8424-
}
8425-
if chan.get_latest_complete_monitor_update_id() > max_in_flight_update_id {
8426-
// If the channel is ahead of the monitor, return InvalidValue:
8427-
log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
8428-
log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} with update_id through {} in-flight",
8429-
log_bytes!(chan.context.channel_id()), monitor.get_latest_update_id(), max_in_flight_update_id);
8430-
log_error!(args.logger, " but the ChannelManager is at update_id {}.", chan.get_latest_complete_monitor_update_id());
8431-
log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
8432-
log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
8433-
log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
8434-
log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
8435-
return Err(DecodeError::InvalidValue);
8432+
// Channels that were persisted have to be funded, otherwise they should have been
8433+
// discarded.
8434+
let funding_txo = chan.context.get_funding_txo().ok_or(DecodeError::InvalidValue)?;
8435+
let monitor = args.channel_monitors.get(&funding_txo)
8436+
.expect("We already checked for monitor presence when loading channels");
8437+
let mut max_in_flight_update_id = monitor.get_latest_update_id();
8438+
if let Some(in_flight_upds) = &mut in_flight_monitor_updates {
8439+
if let Some(mut chan_in_flight_upds) = in_flight_upds.remove(&(*counterparty_id, funding_txo)) {
8440+
max_in_flight_update_id = cmp::max(max_in_flight_update_id,
8441+
handle_in_flight_updates!(*counterparty_id, chan_in_flight_upds,
8442+
funding_txo, monitor, peer_state, ""));
84368443
}
8437-
} else { debug_assert!(false, "We already loaded a channel, it has to have been funded"); }
8444+
}
8445+
if chan.get_latest_complete_monitor_update_id() > max_in_flight_update_id {
8446+
// If the channel is ahead of the monitor, return InvalidValue:
8447+
log_error!(args.logger, "A ChannelMonitor is stale compared to the current ChannelManager! This indicates a potentially-critical violation of the chain::Watch API!");
8448+
log_error!(args.logger, " The ChannelMonitor for channel {} is at update_id {} with update_id through {} in-flight",
8449+
log_bytes!(chan.context.channel_id()), monitor.get_latest_update_id(), max_in_flight_update_id);
8450+
log_error!(args.logger, " but the ChannelManager is at update_id {}.", chan.get_latest_unblocked_monitor_update_id());
8451+
log_error!(args.logger, " The chain::Watch API *requires* that monitors are persisted durably before returning,");
8452+
log_error!(args.logger, " client applications must ensure that ChannelMonitor data is always available and the latest to avoid funds loss!");
8453+
log_error!(args.logger, " Without the latest ChannelMonitor we cannot continue without risking funds.");
8454+
log_error!(args.logger, " Please ensure the chain::Watch API requirements are met and file a bug report at https://github.com/lightningdevkit/rust-lightning");
8455+
return Err(DecodeError::InvalidValue);
8456+
}
84388457
}
84398458
}
84408459

@@ -8448,20 +8467,8 @@ where
84488467
Mutex::new(peer_state_from_chans(HashMap::new()))
84498468
});
84508469
let mut peer_state = peer_state_mutex.lock().unwrap();
8451-
chan_in_flight_updates.retain(|upd| upd.update_id > monitor.get_latest_update_id());
8452-
for update in chan_in_flight_updates.iter() {
8453-
log_trace!(args.logger, "Replaying ChannelMonitorUpdate {} for closed channel {}",
8454-
update.update_id, log_bytes!(funding_txo.to_channel_id()));
8455-
pending_background_events.push(
8456-
BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8457-
counterparty_node_id: counterparty_id,
8458-
funding_txo, update: update.clone(),
8459-
});
8460-
}
8461-
if peer_state.in_flight_monitor_updates.insert(funding_txo, chan_in_flight_updates).is_some() {
8462-
log_error!(args.logger, "Duplicate in-flight monitor update set for the same channel!");
8463-
return Err(DecodeError::InvalidValue);
8464-
}
8470+
handle_in_flight_updates!(counterparty_id, chan_in_flight_updates,
8471+
funding_txo, monitor, peer_state, "closed ");
84658472
} else {
84668473
log_error!(args.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!");
84678474
log_error!(args.logger, " The ChannelMonitor for channel {} is missing.",

0 commit comments

Comments
 (0)