@@ -2952,31 +2952,9 @@ macro_rules! handle_error {
2952
2952
/// [`ChannelMonitor`]/channel funding transaction) to begin with.
2953
2953
macro_rules! locked_close_channel {
2954
2954
($self: ident, $peer_state: expr, $channel_context: expr, $shutdown_res_mut: expr) => {{
2955
- if let Some((counterparty_node_id, funding_txo, channel_id, update)) = $shutdown_res_mut.monitor_update.take() {
2956
- if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
2957
- handle_new_monitor_update!($self, funding_txo, update, $peer_state,
2958
- $channel_context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
2959
- } else {
2960
- // We want to track the in-flight update both in `in_flight_monitor_updates` and in
2961
- // `pending_background_events` to avoid a race condition during
2962
- // `pending_background_events` processing where we complete one
2963
- // `ChannelMonitorUpdate` (but there are more pending as background events) but we
2964
- // conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
2965
- // run post-completion actions. We could work around that with some effort, but its
2966
- // simpler to just track updates twice.
2967
- let in_flight_updates = $peer_state.in_flight_monitor_updates.entry(funding_txo)
2968
- .or_insert_with(Vec::new);
2969
- if !in_flight_updates.contains(&update) {
2970
- in_flight_updates.push(update.clone());
2971
- }
2972
- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
2973
- counterparty_node_id,
2974
- funding_txo,
2975
- channel_id,
2976
- update,
2977
- };
2978
- $self.pending_background_events.lock().unwrap().push(event);
2979
- }
2955
+ if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
2956
+ handle_new_monitor_update!($self, funding_txo, update, $peer_state,
2957
+ $channel_context, REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
2980
2958
}
2981
2959
// If there's a possibility that we need to generate further monitor updates for this
2982
2960
// channel, we need to store the last update_id of it. However, we don't want to insert
@@ -3305,8 +3283,8 @@ macro_rules! handle_new_monitor_update {
3305
3283
};
3306
3284
(
3307
3285
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
3308
- $chan_id: expr, $in_flight_updates: ident, $update_idx: ident, _internal_outer ,
3309
- $completed: expr
3286
+ $chan_id: expr, $counterparty_node_id: expr, $ in_flight_updates: ident, $update_idx: ident,
3287
+ _internal_outer, $completed: expr
3310
3288
) => { {
3311
3289
$in_flight_updates = $peer_state.in_flight_monitor_updates.entry($funding_txo)
3312
3290
.or_insert_with(Vec::new);
@@ -3318,31 +3296,55 @@ macro_rules! handle_new_monitor_update {
3318
3296
$in_flight_updates.push($update);
3319
3297
$in_flight_updates.len() - 1
3320
3298
});
3321
- let update_res = $self.chain_monitor.update_channel($funding_txo, &$in_flight_updates[$update_idx]);
3322
- handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3299
+ if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
3300
+ let update_res = $self.chain_monitor.update_channel($funding_txo, &$in_flight_updates[$update_idx]);
3301
+ handle_new_monitor_update!($self, update_res, $logger, $chan_id, _internal, $completed)
3302
+ } else {
3303
+ // We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
3304
+ // fail to persist it. This is a fairly safe assumption, however, since anything we do
3305
+ // during the startup sequence should be replayed exactly if we immediately crash.
3306
+ let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
3307
+ counterparty_node_id: $counterparty_node_id,
3308
+ funding_txo: $funding_txo,
3309
+ channel_id: $chan_id,
3310
+ update: $in_flight_updates[$update_idx].clone(),
3311
+ };
3312
+ // We want to track the in-flight update both in `in_flight_monitor_updates` and in
3313
+ // `pending_background_events` to avoid a race condition during
3314
+ // `pending_background_events` processing where we complete one
3315
+ // `ChannelMonitorUpdate` (but there are more pending as background events) but we
3316
+ // conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
3317
+ // run post-completion actions.
3318
+ // We could work around that with some effort, but its simpler to just track updates
3319
+ // twice.
3320
+ $self.pending_background_events.lock().unwrap().push(event);
3321
+ false
3322
+ }
3323
3323
} };
3324
3324
(
3325
3325
$self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $chan_context: expr,
3326
3326
REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER
3327
3327
) => { {
3328
3328
let logger = WithChannelContext::from(&$self.logger, &$chan_context, None);
3329
3329
let chan_id = $chan_context.channel_id();
3330
+ let counterparty_node_id = $chan_context.get_counterparty_node_id();
3330
3331
let in_flight_updates;
3331
3332
let idx;
3332
3333
handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3333
- in_flight_updates, idx, _internal_outer,
3334
+ counterparty_node_id, in_flight_updates, idx, _internal_outer,
3334
3335
{
3335
3336
let _ = in_flight_updates.remove(idx);
3336
3337
})
3337
3338
} };
3338
3339
(
3339
3340
$self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
3340
- $per_peer_state_lock: expr, $logger : expr, $channel_id: expr, POST_CHANNEL_CLOSE
3341
+ $per_peer_state_lock: expr, $counterparty_node_id : expr, $channel_id: expr, POST_CHANNEL_CLOSE
3341
3342
) => { {
3343
+ let logger = WithContext::from(&$self.logger, Some($counterparty_node_id), Some($channel_id), None);
3342
3344
let in_flight_updates;
3343
3345
let idx;
3344
- handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, $ logger,
3345
- $channel_id, in_flight_updates, idx, _internal_outer,
3346
+ handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger,
3347
+ $channel_id, $counterparty_node_id, in_flight_updates, idx, _internal_outer,
3346
3348
{
3347
3349
let _ = in_flight_updates.remove(idx);
3348
3350
if in_flight_updates.is_empty() {
@@ -3362,10 +3364,11 @@ macro_rules! handle_new_monitor_update {
3362
3364
) => { {
3363
3365
let logger = WithChannelContext::from(&$self.logger, &$chan.context, None);
3364
3366
let chan_id = $chan.context.channel_id();
3367
+ let counterparty_node_id = $chan.context.get_counterparty_node_id();
3365
3368
let in_flight_updates;
3366
3369
let idx;
3367
3370
handle_new_monitor_update!($self, $funding_txo, $update, $peer_state, logger, chan_id,
3368
- in_flight_updates, idx, _internal_outer,
3371
+ counterparty_node_id, in_flight_updates, idx, _internal_outer,
3369
3372
{
3370
3373
let _ = in_flight_updates.remove(idx);
3371
3374
if in_flight_updates.is_empty() && $chan.blocked_monitor_updates_pending() == 0 {
@@ -3995,11 +3998,10 @@ where
3995
3998
},
3996
3999
hash_map::Entry::Vacant(_) => {},
3997
4000
}
3998
- let logger = WithContext::from(&self.logger, Some(counterparty_node_id), Some(channel_id), None);
3999
4001
4000
4002
handle_new_monitor_update!(
4001
4003
self, funding_txo, monitor_update, peer_state_lock, peer_state, per_peer_state,
4002
- logger , channel_id, POST_CHANNEL_CLOSE
4004
+ counterparty_node_id , channel_id, POST_CHANNEL_CLOSE
4003
4005
);
4004
4006
}
4005
4007
@@ -7129,7 +7131,6 @@ where
7129
7131
let peer_state = &mut **peer_state_lock;
7130
7132
if let hash_map::Entry::Occupied(mut chan_phase_entry) = peer_state.channel_by_id.entry(chan_id) {
7131
7133
if let ChannelPhase::Funded(chan) = chan_phase_entry.get_mut() {
7132
- let counterparty_node_id = chan.context.get_counterparty_node_id();
7133
7134
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
7134
7135
let fulfill_res = chan.get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, payment_info, &&logger);
7135
7136
@@ -7144,21 +7145,8 @@ where
7144
7145
if let Some(raa_blocker) = raa_blocker_opt {
7145
7146
peer_state.actions_blocking_raa_monitor_updates.entry(chan_id).or_insert_with(Vec::new).push(raa_blocker);
7146
7147
}
7147
- if !during_init {
7148
- handle_new_monitor_update!(self, prev_hop.funding_txo, monitor_update, peer_state_opt,
7149
- peer_state, per_peer_state, chan);
7150
- } else {
7151
- // If we're running during init we cannot update a monitor directly -
7152
- // they probably haven't actually been loaded yet. Instead, push the
7153
- // monitor update as a background event.
7154
- self.pending_background_events.lock().unwrap().push(
7155
- BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7156
- counterparty_node_id,
7157
- funding_txo: prev_hop.funding_txo,
7158
- channel_id: prev_hop.channel_id,
7159
- update: monitor_update.clone(),
7160
- });
7161
- }
7148
+ handle_new_monitor_update!(self, prev_hop.funding_txo, monitor_update, peer_state_opt,
7149
+ peer_state, per_peer_state, chan);
7162
7150
}
7163
7151
UpdateFulfillCommitFetch::DuplicateClaim {} => {
7164
7152
let (action_opt, raa_blocker_opt) = completion_action(None, true);
@@ -7273,26 +7261,10 @@ where
7273
7261
peer_state.monitor_update_blocked_actions.entry(chan_id).or_insert(Vec::new()).push(action);
7274
7262
}
7275
7263
7276
- if !during_init {
7277
- handle_new_monitor_update!(self, prev_hop.funding_txo, preimage_update, peer_state, peer_state, per_peer_state, logger, chan_id, POST_CHANNEL_CLOSE);
7278
- } else {
7279
- // If we're running during init we cannot update a monitor directly - they probably
7280
- // haven't actually been loaded yet. Instead, push the monitor update as a background
7281
- // event.
7282
-
7283
- let in_flight_updates = peer_state.in_flight_monitor_updates
7284
- .entry(prev_hop.funding_txo)
7285
- .or_insert_with(Vec::new);
7286
- in_flight_updates.push(preimage_update.clone());
7287
-
7288
- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
7289
- counterparty_node_id,
7290
- funding_txo: prev_hop.funding_txo,
7291
- channel_id: prev_hop.channel_id,
7292
- update: preimage_update,
7293
- };
7294
- self.pending_background_events.lock().unwrap().push(event);
7295
- }
7264
+ handle_new_monitor_update!(
7265
+ self, prev_hop.funding_txo, preimage_update, peer_state, peer_state, per_peer_state,
7266
+ counterparty_node_id, chan_id, POST_CHANNEL_CLOSE
7267
+ );
7296
7268
}
7297
7269
7298
7270
fn finalize_claims(&self, sources: Vec<HTLCSource>) {
@@ -13685,14 +13657,20 @@ where
13685
13657
}
13686
13658
}
13687
13659
}
13660
+ let mut per_peer_state = per_peer_state.get(counterparty_node_id)
13661
+ .expect("If we have pending updates for a channel it has to have an entry")
13662
+ .lock().unwrap();
13688
13663
if updated_id {
13689
- per_peer_state.get(counterparty_node_id)
13690
- .expect("If we have pending updates for a channel it has to have an entry")
13691
- .lock().unwrap()
13664
+ per_peer_state
13692
13665
.closed_channel_monitor_update_ids.entry(*channel_id)
13693
13666
.and_modify(|v| *v = cmp::max(update.update_id, *v))
13694
13667
.or_insert(update.update_id);
13695
13668
}
13669
+ let in_flight_updates = per_peer_state.in_flight_monitor_updates
13670
+ .entry(*funding_txo)
13671
+ .or_insert_with(Vec::new);
13672
+ debug_assert!(!in_flight_updates.iter().any(|upd| upd == update));
13673
+ in_flight_updates.push(update.clone());
13696
13674
}
13697
13675
pending_background_events.push(new_event);
13698
13676
}
0 commit comments