@@ -2954,31 +2954,9 @@ macro_rules! handle_error {
29542954/// [`ChannelMonitor`]/channel funding transaction) to begin with.
29552955macro_rules! locked_close_channel {
29562956 ($self: ident, $peer_state: expr, $channel_context: expr, $shutdown_res_mut: expr) => {{
2957- if let Some((counterparty_node_id, funding_txo, channel_id, update)) = $shutdown_res_mut.monitor_update.take() {
2958- if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
2959- handle_new_monitor_update!($self, funding_txo, update, $peer_state,
2960- $channel_context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
2961- } else {
2962- // We want to track the in-flight update both in `in_flight_monitor_updates` and in
2963- // `pending_background_events` to avoid a race condition during
2964- // `pending_background_events` processing where we complete one
2965- // `ChannelMonitorUpdate` (but there are more pending as background events) but we
2966- // conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
2967- // run post-completion actions. We could work around that with some effort, but its
2968- // simpler to just track updates twice.
2969- let in_flight_updates = $peer_state.in_flight_monitor_updates.entry(funding_txo)
2970- .or_insert_with(Vec::new);
2971- if !in_flight_updates.contains(&update) {
2972- in_flight_updates.push(update.clone());
2973- }
2974- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
2975- counterparty_node_id,
2976- funding_txo,
2977- channel_id,
2978- update,
2979- };
2980- $self.pending_background_events.lock().unwrap().push(event);
2981- }
2957+ if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
2958+ handle_new_monitor_update!($self, funding_txo, update, $peer_state,
2959+ $channel_context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
29822960 }
29832961 // If there's a possibility that we need to generate further monitor updates for this
29842962 // channel, we need to store the last update_id of it. However, we don't want to insert
@@ -3307,8 +3285,8 @@ macro_rules! handle_new_monitor_update {
33073285 };
33083286 (
33093287 $self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
3310- $chan_id: expr, $in_flight_updates: ident, $update_idx: ident, _internal_outer ,
3311- $completed: expr
3288+ $chan_id: expr, $counterparty_node_id: expr, $ in_flight_updates: ident, $update_idx: ident,
3289+ _internal_outer, $completed: expr
33123290 ) => { {
33133291 $in_flight_updates = $peer_state.in_flight_monitor_updates.entry($funding_txo)
33143292 .or_insert_with(Vec::new);
@@ -3320,31 +3298,55 @@ macro_rules! handle_new_monitor_update {
33203298 $in_flight_updates.push($update);
33213299 $in_flight_updates.len() - 1
33223300 });
3323- let update_res = $self.chain_monitor.update_channel($funding_txo, &$in_flight_updates[$update_idx]);
3324- handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3301+ if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
3302+ let update_res = $self.chain_monitor.update_channel($funding_txo, &$in_flight_updates[$update_idx]);
3303+ handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3304+ } else {
3305+ // We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
3306+ // fail to persist it. This is a fairly safe assumption, however, since anything we do
3307+ // during the startup sequence should be replayed exactly if we immediately crash.
3308+ let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
3309+ counterparty_node_id: $counterparty_node_id,
3310+ funding_txo: $funding_txo,
3311+ channel_id: $chan_id,
3312+ update: $in_flight_updates[$update_idx].clone(),
3313+ };
3314+ // We want to track the in-flight update both in `in_flight_monitor_updates` and in
3315+ // `pending_background_events` to avoid a race condition during
3316+ // `pending_background_events` processing where we complete one
3317+ // `ChannelMonitorUpdate` (but there are more pending as background events) but we
3318+ // conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
3319+ // run post-completion actions.
3320+ // We could work around that with some effort, but its simpler to just track updates
3321+ // twice.
3322+ $self.pending_background_events.lock().unwrap().push(event);
3323+ false
3324+ }
33253325 } };
33263326 (
33273327 $self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr,
33283328 REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER
33293329 ) => { {
33303330 let logger = WithChannelContext::from(&$self.logger, &$chan_context, None);
33313331 let chan_id = $chan_context.channel_id();
3332+ let counterparty_node_id = $chan_context.get_counterparty_node_id();
33323333 let in_flight_updates;
33333334 let idx;
33343335 handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3335- in_flight_updates, idx, _internal_outer,
3336+ counterparty_node_id, in_flight_updates, idx, _internal_outer,
33363337 {
33373338 let _ = in_flight_updates.remove(idx);
33383339 })
33393340 } };
33403341 (
33413342 $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3342- $per_peer_state_lock: expr, $logger : expr, $channel_id: expr, POST_CHANNEL_CLOSE
3343+ $per_peer_state_lock: expr, $counterparty_node_id : expr, $channel_id: expr, POST_CHANNEL_CLOSE
33433344 ) => { {
3345+ let logger = WithContext::from(&$self.logger, Some($counterparty_node_id), Some($channel_id), None);
33443346 let in_flight_updates;
33453347 let idx;
3346- handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, $ logger,
3347- $channel_id, in_flight_updates, idx, _internal_outer,
3348+ handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger,
3349+ $channel_id, $counterparty_node_id, in_flight_updates, idx, _internal_outer,
33483350 {
33493351 let _ = in_flight_updates.remove(idx);
33503352 if in_flight_updates.is_empty() {
@@ -3364,10 +3366,11 @@ macro_rules! handle_new_monitor_update {
33643366 ) => { {
33653367 let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
33663368 let chan_id = $chan.context.channel_id();
3369+ let counterparty_node_id = $chan.context.get_counterparty_node_id();
33673370 let in_flight_updates;
33683371 let idx;
33693372 handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3370- in_flight_updates, idx, _internal_outer,
3373+ counterparty_node_id, in_flight_updates, idx, _internal_outer,
33713374 {
33723375 let _ = in_flight_updates.remove(idx);
33733376 if in_flight_updates.is_empty() && $chan.blocked_monitor_updates_pending() == 0 {
@@ -3997,11 +4000,10 @@ where
39974000 },
39984001 hash_map::Entry::Vacant(_) => {},
39994002 }
4000- let logger = WithContext::from(&self.logger, Some(counterparty_node_id), Some(channel_id), None);
40014003
40024004 handle_new_monitor_update!(
40034005 self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state,
4004- logger , channel_id, POST_CHANNEL_CLOSE
4006+ counterparty_node_id , channel_id, POST_CHANNEL_CLOSE
40054007 );
40064008 }
40074009
@@ -7188,7 +7190,6 @@ where
71887190 let peer_state = &mut **peer_state_lock;
71897191 if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(chan_id) {
71907192 if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
7191- let counterparty_node_id = chan.context.get_counterparty_node_id();
71927193 let logger = WithChannelContext::from(&self.logger, &chan.context, None);
71937194 let fulfill_res = chan.get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, payment_info, &&logger);
71947195
@@ -7203,21 +7204,8 @@ where
72037204 if let Some(raa_blocker) = raa_blocker_opt {
72047205 peer_state.actions_blocking_raa_monitor_updates.entry(chan_id).or_insert_with(Vec::new).push(raa_blocker);
72057206 }
7206- if !during_init {
7207- handle_new_monitor_update!(self, prev_hop.funding_txo, monitor_update, peer_state_opt,
7208- peer_state, per_peer_state, chan);
7209- } else {
7210- // If we're running during init we cannot update a monitor directly -
7211- // they probably haven't actually been loaded yet. Instead, push the
7212- // monitor update as a background event.
7213- self.pending_background_events.lock().unwrap().push(
7214- BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7215- counterparty_node_id,
7216- funding_txo: prev_hop.funding_txo,
7217- channel_id: prev_hop.channel_id,
7218- update: monitor_update.clone(),
7219- });
7220- }
7207+ handle_new_monitor_update!(self, prev_hop.funding_txo, monitor_update, peer_state_opt,
7208+ peer_state, per_peer_state, chan);
72217209 }
72227210 UpdateFulfillCommitFetch::DuplicateClaim {} => {
72237211 let (action_opt, raa_blocker_opt) = completion_action(None, true);
@@ -7332,26 +7320,10 @@ where
73327320 peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
73337321 }
73347322
7335- if !during_init {
7336- handle_new_monitor_update!(self, prev_hop.funding_txo, preimage_update, peer_state, peer_state, per_peer_state, logger, chan_id, POST_CHANNEL_CLOSE);
7337- } else {
7338- // If we're running during init we cannot update a monitor directly - they probably
7339- // haven't actually been loaded yet. Instead, push the monitor update as a background
7340- // event.
7341-
7342- let in_flight_updates = peer_state.in_flight_monitor_updates
7343- .entry(prev_hop.funding_txo)
7344- .or_insert_with(Vec::new);
7345- in_flight_updates.push(preimage_update.clone());
7346-
7347- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7348- counterparty_node_id,
7349- funding_txo: prev_hop.funding_txo,
7350- channel_id: prev_hop.channel_id,
7351- update: preimage_update,
7352- };
7353- self.pending_background_events.lock().unwrap().push(event);
7354- }
7323+ handle_new_monitor_update!(
7324+ self, prev_hop.funding_txo, preimage_update, peer_state, peer_state, per_peer_state,
7325+ counterparty_node_id, chan_id, POST_CHANNEL_CLOSE
7326+ );
73557327 }
73567328
73577329 fn finalize_claims(&self, sources: Vec<HTLCSource>) {
@@ -13743,14 +13715,20 @@ where
1374313715 }
1374413716 }
1374513717 }
13718+ let mut per_peer_state = per_peer_state.get(counterparty_node_id)
13719+ .expect("If we have pending updates for a channel it has to have an entry")
13720+ .lock().unwrap();
1374613721 if updated_id {
13747- per_peer_state.get(counterparty_node_id)
13748- .expect("If we have pending updates for a channel it has to have an entry")
13749- .lock().unwrap()
13722+ per_peer_state
1375013723 .closed_channel_monitor_update_ids.entry(*channel_id)
1375113724 .and_modify(|v| *v = cmp::max(update.update_id, *v))
1375213725 .or_insert(update.update_id);
1375313726 }
13727+ let in_flight_updates = per_peer_state.in_flight_monitor_updates
13728+ .entry(*funding_txo)
13729+ .or_insert_with(Vec::new);
13730+ debug_assert!(!in_flight_updates.iter().any(|upd| upd == update));
13731+ in_flight_updates.push(update.clone());
1375413732 }
1375513733 pending_background_events.push(new_event);
1375613734 }
0 commit comments