@@ -2944,24 +2944,9 @@ macro_rules! handle_error {
2944
2944
/// [`ChannelMonitor`]/channel funding transaction) to begin with.
2945
2945
macro_rules! locked_close_channel {
2946
2946
($self: ident, $peer_state: expr, $channel_context: expr, $shutdown_res_mut: expr) => {{
2947
- if let Some((counterparty_node_id, funding_txo, channel_id, update)) = $shutdown_res_mut.monitor_update.take() {
2948
- if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
2949
- handle_new_monitor_update!($self, funding_txo, update, $peer_state,
2950
- $channel_context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
2951
- } else {
2952
- let in_flight_updates = $peer_state.in_flight_monitor_updates.entry(funding_txo)
2953
- .or_insert_with(Vec::new);
2954
- if !in_flight_updates.contains(&update) {
2955
- in_flight_updates.push(update.clone());
2956
- }
2957
- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
2958
- counterparty_node_id,
2959
- funding_txo,
2960
- channel_id,
2961
- update,
2962
- };
2963
- $self.pending_background_events.lock().unwrap().push(event);
2964
- }
2947
+ if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
2948
+ handle_new_monitor_update!($self, funding_txo, update, $peer_state,
2949
+ $channel_context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
2965
2950
}
2966
2951
// If there's a possibility that we need to generate further monitor updates for this
2967
2952
// channel, we need to store the last update_id of it. However, we don't want to insert
@@ -3290,8 +3275,8 @@ macro_rules! handle_new_monitor_update {
3290
3275
};
3291
3276
(
3292
3277
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
3293
- $chan_id: expr, $in_flight_updates: ident, $update_idx: ident, _internal_outer ,
3294
- $completed: expr
3278
+ $chan_id: expr, $counterparty_node_id: expr, $ in_flight_updates: ident, $update_idx: ident,
3279
+ _internal_outer, $completed: expr
3295
3280
) => { {
3296
3281
$in_flight_updates = $peer_state.in_flight_monitor_updates.entry($funding_txo)
3297
3282
.or_insert_with(Vec::new);
@@ -3303,31 +3288,47 @@ macro_rules! handle_new_monitor_update {
3303
3288
$in_flight_updates.push($update);
3304
3289
$in_flight_updates.len() - 1
3305
3290
});
3306
- let update_res = $self.chain_monitor.update_channel($funding_txo, &$in_flight_updates[$update_idx]);
3307
- handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3291
+ if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
3292
+ let update_res = $self.chain_monitor.update_channel($funding_txo, &$in_flight_updates[$update_idx]);
3293
+ handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3294
+ } else {
3295
+ // We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
3296
+ // fail to persist it. This is a fairly safe assumption, however, since anything we do
3297
+ // during the startup sequence should be replayed exactly if we immediately crash.
3298
+ let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
3299
+ counterparty_node_id: $counterparty_node_id,
3300
+ funding_txo: $funding_txo,
3301
+ channel_id: $chan_id,
3302
+ update: $in_flight_updates[$update_idx].clone(),
3303
+ };
3304
+ $self.pending_background_events.lock().unwrap().push(event);
3305
+ false
3306
+ }
3308
3307
} };
3309
3308
(
3310
3309
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr,
3311
3310
REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER
3312
3311
) => { {
3313
3312
let logger = WithChannelContext::from(&$self.logger, &$chan_context, None);
3314
3313
let chan_id = $chan_context.channel_id();
3314
+ let counterparty_node_id = $chan_context.get_counterparty_node_id();
3315
3315
let in_flight_updates;
3316
3316
let idx;
3317
3317
handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3318
- in_flight_updates, idx, _internal_outer,
3318
+ counterparty_node_id, in_flight_updates, idx, _internal_outer,
3319
3319
{
3320
3320
let _ = in_flight_updates.remove(idx);
3321
3321
})
3322
3322
} };
3323
3323
(
3324
3324
$self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3325
- $per_peer_state_lock: expr, $logger : expr, $channel_id: expr, POST_CHANNEL_CLOSE
3325
+ $per_peer_state_lock: expr, $counterparty_node_id : expr, $channel_id: expr, POST_CHANNEL_CLOSE
3326
3326
) => { {
3327
+ let logger = WithContext::from(&$self.logger, Some($counterparty_node_id), Some($channel_id), None);
3327
3328
let in_flight_updates;
3328
3329
let idx;
3329
- handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, $ logger,
3330
- $channel_id, in_flight_updates, idx, _internal_outer,
3330
+ handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger,
3331
+ $channel_id, $counterparty_node_id, in_flight_updates, idx, _internal_outer,
3331
3332
{
3332
3333
let _ = in_flight_updates.remove(idx);
3333
3334
if in_flight_updates.is_empty() {
@@ -3347,10 +3348,11 @@ macro_rules! handle_new_monitor_update {
3347
3348
) => { {
3348
3349
let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3349
3350
let chan_id = $chan.context.channel_id();
3351
+ let counterparty_node_id = $chan.context.get_counterparty_node_id();
3350
3352
let in_flight_updates;
3351
3353
let idx;
3352
3354
handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3353
- in_flight_updates, idx, _internal_outer,
3355
+ counterparty_node_id, in_flight_updates, idx, _internal_outer,
3354
3356
{
3355
3357
let _ = in_flight_updates.remove(idx);
3356
3358
if in_flight_updates.is_empty() && $chan.blocked_monitor_updates_pending() == 0 {
@@ -3980,11 +3982,10 @@ where
3980
3982
},
3981
3983
hash_map::Entry::Vacant(_) => {},
3982
3984
}
3983
- let logger = WithContext::from(&self.logger, Some(counterparty_node_id), Some(channel_id), None);
3984
3985
3985
3986
handle_new_monitor_update!(
3986
3987
self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state,
3987
- logger , channel_id, POST_CHANNEL_CLOSE
3988
+ counterparty_node_id , channel_id, POST_CHANNEL_CLOSE
3988
3989
);
3989
3990
}
3990
3991
@@ -7169,7 +7170,6 @@ where
7169
7170
let peer_state = &mut **peer_state_lock;
7170
7171
if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(chan_id) {
7171
7172
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
7172
- let counterparty_node_id = chan.context.get_counterparty_node_id();
7173
7173
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
7174
7174
let fulfill_res = chan.get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, payment_info, &&logger);
7175
7175
@@ -7184,21 +7184,8 @@ where
7184
7184
if let Some(raa_blocker) = raa_blocker_opt {
7185
7185
peer_state.actions_blocking_raa_monitor_updates.entry(chan_id).or_insert_with(Vec::new).push(raa_blocker);
7186
7186
}
7187
- if !during_init {
7188
- handle_new_monitor_update!(self, prev_hop.funding_txo, monitor_update, peer_state_opt,
7189
- peer_state, per_peer_state, chan);
7190
- } else {
7191
- // If we're running during init we cannot update a monitor directly -
7192
- // they probably haven't actually been loaded yet. Instead, push the
7193
- // monitor update as a background event.
7194
- self.pending_background_events.lock().unwrap().push(
7195
- BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7196
- counterparty_node_id,
7197
- funding_txo: prev_hop.funding_txo,
7198
- channel_id: prev_hop.channel_id,
7199
- update: monitor_update.clone(),
7200
- });
7201
- }
7187
+ handle_new_monitor_update!(self, prev_hop.funding_txo, monitor_update, peer_state_opt,
7188
+ peer_state, per_peer_state, chan);
7202
7189
}
7203
7190
UpdateFulfillCommitFetch::DuplicateClaim {} => {
7204
7191
let (action_opt, raa_blocker_opt) = completion_action(None, true);
@@ -7313,26 +7300,10 @@ where
7313
7300
peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
7314
7301
}
7315
7302
7316
- if !during_init {
7317
- handle_new_monitor_update!(self, prev_hop.funding_txo, preimage_update, peer_state, peer_state, per_peer_state, logger, chan_id, POST_CHANNEL_CLOSE);
7318
- } else {
7319
- // If we're running during init we cannot update a monitor directly - they probably
7320
- // haven't actually been loaded yet. Instead, push the monitor update as a background
7321
- // event.
7322
-
7323
- let in_flight_updates = peer_state.in_flight_monitor_updates
7324
- .entry(prev_hop.funding_txo)
7325
- .or_insert_with(Vec::new);
7326
- in_flight_updates.push(preimage_update.clone());
7327
-
7328
- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7329
- counterparty_node_id,
7330
- funding_txo: prev_hop.funding_txo,
7331
- channel_id: prev_hop.channel_id,
7332
- update: preimage_update,
7333
- };
7334
- self.pending_background_events.lock().unwrap().push(event);
7335
- }
7303
+ handle_new_monitor_update!(
7304
+ self, prev_hop.funding_txo, preimage_update, peer_state, peer_state, per_peer_state,
7305
+ counterparty_node_id, chan_id, POST_CHANNEL_CLOSE
7306
+ );
7336
7307
}
7337
7308
7338
7309
fn finalize_claims(&self, sources: Vec<HTLCSource>) {
@@ -13725,14 +13696,20 @@ where
13725
13696
}
13726
13697
}
13727
13698
}
13699
+ let mut per_peer_state = per_peer_state.get(counterparty_node_id)
13700
+ .expect("If we have pending updates for a channel it has to have an entry")
13701
+ .lock().unwrap();
13728
13702
if updated_id {
13729
- per_peer_state.get(counterparty_node_id)
13730
- .expect("If we have pending updates for a channel it has to have an entry")
13731
- .lock().unwrap()
13703
+ per_peer_state
13732
13704
.closed_channel_monitor_update_ids.entry(*channel_id)
13733
13705
.and_modify(|v| *v = cmp::max(update.update_id, *v))
13734
13706
.or_insert(update.update_id);
13735
13707
}
13708
+ let in_flight_updates = per_peer_state.in_flight_monitor_updates
13709
+ .entry(*funding_txo)
13710
+ .or_insert_with(Vec::new);
13711
+ debug_assert!(!in_flight_updates.iter().any(|upd| upd == update));
13712
+ in_flight_updates.push(update.clone());
13736
13713
}
13737
13714
pending_background_events.push(new_event);
13738
13715
}
0 commit comments