@@ -3696,6 +3696,59 @@ macro_rules! handle_initial_monitor {
36963696 };
36973697}
36983698
3699+ macro_rules! handle_new_monitor_update_internal {
3700+ (
3701+ $self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
3702+ $chan_id: expr, $counterparty_node_id: expr, $all_completed: expr
3703+ ) => {{
3704+ let in_flight_updates = &mut $peer_state
3705+ .in_flight_monitor_updates
3706+ .entry($chan_id)
3707+ .or_insert_with(|| ($funding_txo, Vec::new()))
3708+ .1;
3709+ // During startup, we push monitor updates as background events through to here in
3710+ // order to replay updates that were in-flight when we shut down. Thus, we have to
3711+ // filter for uniqueness here.
3712+ let update_idx =
3713+ in_flight_updates.iter().position(|upd| upd == &$update).unwrap_or_else(|| {
3714+ in_flight_updates.push($update);
3715+ in_flight_updates.len() - 1
3716+ });
3717+ if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
3718+ let update_res =
3719+ $self.chain_monitor.update_channel($chan_id, &in_flight_updates[update_idx]);
3720+ let update_completed = handle_monitor_update_res($self, update_res, $chan_id, $logger);
3721+ if update_completed {
3722+ let _ = in_flight_updates.remove(update_idx);
3723+ if in_flight_updates.is_empty() {
3724+ $all_completed;
3725+ }
3726+ }
3727+ update_completed
3728+ } else {
3729+ // We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
3730+ // fail to persist it. This is a fairly safe assumption, however, since anything we do
3731+ // during the startup sequence should be replayed exactly if we immediately crash.
3732+ let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
3733+ counterparty_node_id: $counterparty_node_id,
3734+ funding_txo: $funding_txo,
3735+ channel_id: $chan_id,
3736+ update: in_flight_updates[update_idx].clone(),
3737+ };
3738+ // We want to track the in-flight update both in `in_flight_monitor_updates` and in
3739+ // `pending_background_events` to avoid a race condition during
3740+ // `pending_background_events` processing where we complete one
3741+ // `ChannelMonitorUpdate` (but there are more pending as background events) but we
3742+ // conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
3743+ // run post-completion actions.
3744+ // We could work around that with some effort, but its simpler to just track updates
3745+ // twice.
3746+ $self.pending_background_events.lock().unwrap().push(event);
3747+ false
3748+ }
3749+ }};
3750+ }
3751+
36993752macro_rules! handle_post_close_monitor_update {
37003753 (
37013754 $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
@@ -3755,59 +3808,6 @@ macro_rules! handle_new_monitor_update_locked_actions_handled_by_caller {
37553808 }};
37563809}
37573810
3758- macro_rules! handle_new_monitor_update_internal {
3759- (
3760- $self: ident, $funding_txo: expr, $update: expr, $peer_state: expr, $logger: expr,
3761- $chan_id: expr, $counterparty_node_id: expr, $all_completed: expr
3762- ) => {{
3763- let in_flight_updates = &mut $peer_state
3764- .in_flight_monitor_updates
3765- .entry($chan_id)
3766- .or_insert_with(|| ($funding_txo, Vec::new()))
3767- .1;
3768- // During startup, we push monitor updates as background events through to here in
3769- // order to replay updates that were in-flight when we shut down. Thus, we have to
3770- // filter for uniqueness here.
3771- let update_idx =
3772- in_flight_updates.iter().position(|upd| upd == &$update).unwrap_or_else(|| {
3773- in_flight_updates.push($update);
3774- in_flight_updates.len() - 1
3775- });
3776- if $self.background_events_processed_since_startup.load(Ordering::Acquire) {
3777- let update_res =
3778- $self.chain_monitor.update_channel($chan_id, &in_flight_updates[update_idx]);
3779- let update_completed = handle_monitor_update_res($self, update_res, $chan_id, $logger);
3780- if update_completed {
3781- let _ = in_flight_updates.remove(update_idx);
3782- if in_flight_updates.is_empty() {
3783- $all_completed;
3784- }
3785- }
3786- update_completed
3787- } else {
3788- // We blindly assume that the ChannelMonitorUpdate will be regenerated on startup if we
3789- // fail to persist it. This is a fairly safe assumption, however, since anything we do
3790- // during the startup sequence should be replayed exactly if we immediately crash.
3791- let event = BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
3792- counterparty_node_id: $counterparty_node_id,
3793- funding_txo: $funding_txo,
3794- channel_id: $chan_id,
3795- update: in_flight_updates[update_idx].clone(),
3796- };
3797- // We want to track the in-flight update both in `in_flight_monitor_updates` and in
3798- // `pending_background_events` to avoid a race condition during
3799- // `pending_background_events` processing where we complete one
3800- // `ChannelMonitorUpdate` (but there are more pending as background events) but we
3801- // conclude that all pending `ChannelMonitorUpdate`s have completed and its safe to
3802- // run post-completion actions.
3803- // We could work around that with some effort, but its simpler to just track updates
3804- // twice.
3805- $self.pending_background_events.lock().unwrap().push(event);
3806- false
3807- }
3808- }};
3809- }
3810-
38113811macro_rules! handle_new_monitor_update {
38123812 (
38133813 $self: ident, $funding_txo: expr, $update: expr, $peer_state_lock: expr, $peer_state: expr,
0 commit comments