@@ -3597,96 +3597,6 @@ fn convert_channel_err_internal<
35973597 }
35983598}
35993599
3600- fn convert_funded_channel_err_internal<SP: Deref, CM: AChannelManager<SP = SP>>(
3601- cm: &CM, closed_channel_monitor_update_ids: &mut BTreeMap<ChannelId, u64>,
3602- in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
3603- coop_close_shutdown_res: Option<ShutdownResult>, err: ChannelError,
3604- chan: &mut FundedChannel<SP>,
3605- ) -> (bool, MsgHandleErrInternal)
3606- where
3607- SP::Target: SignerProvider,
3608- CM::Watch: Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
3609- {
3610- let chan_id = chan.context.channel_id();
3611- convert_channel_err_internal(err, chan_id, |reason, msg| {
3612- let cm = cm.get_cm();
3613- let logger = WithChannelContext::from(&cm.logger, &chan.context, None);
3614-
3615- let mut shutdown_res =
3616- if let Some(res) = coop_close_shutdown_res { res } else { chan.force_shutdown(reason) };
3617- let chan_update = cm.get_channel_update_for_broadcast(chan).ok();
3618-
3619- log_error!(logger, "Closed channel due to close-required error: {}", msg);
3620-
3621- if let Some((_, funding_txo, _, update)) = shutdown_res.monitor_update.take() {
3622- handle_new_monitor_update_locked_actions_handled_by_caller!(
3623- cm,
3624- funding_txo,
3625- update,
3626- in_flight_monitor_updates,
3627- chan.context
3628- );
3629- }
3630- // If there's a possibility that we need to generate further monitor updates for this
3631- // channel, we need to store the last update_id of it. However, we don't want to insert
3632- // into the map (which prevents the `PeerState` from being cleaned up) for channels that
3633- // never even got confirmations (which would open us up to DoS attacks).
3634- let update_id = chan.context.get_latest_monitor_update_id();
3635- let funding_confirmed = chan.funding.get_funding_tx_confirmation_height().is_some();
3636- let chan_zero_conf = chan.context.minimum_depth(&chan.funding) == Some(0);
3637- if funding_confirmed || chan_zero_conf || update_id > 1 {
3638- closed_channel_monitor_update_ids.insert(chan_id, update_id);
3639- }
3640- let mut short_to_chan_info = cm.short_to_chan_info.write().unwrap();
3641- if let Some(short_id) = chan.funding.get_short_channel_id() {
3642- short_to_chan_info.remove(&short_id);
3643- } else {
3644- // If the channel was never confirmed on-chain prior to its closure, remove the
3645- // outbound SCID alias we used for it from the collision-prevention set. While we
3646- // generally want to avoid ever re-using an outbound SCID alias across all channels, we
3647- // also don't want a counterparty to be able to trivially cause a memory leak by simply
3648- // opening a million channels with us which are closed before we ever reach the funding
3649- // stage.
3650- let outbound_alias = chan.context.outbound_scid_alias();
3651- let alias_removed = cm.outbound_scid_aliases.lock().unwrap().remove(&outbound_alias);
3652- debug_assert!(alias_removed);
3653- }
3654- short_to_chan_info.remove(&chan.context.outbound_scid_alias());
3655- for scid in chan.context.historical_scids() {
3656- short_to_chan_info.remove(scid);
3657- }
3658-
3659- (shutdown_res, chan_update)
3660- })
3661- }
3662-
3663- fn convert_unfunded_channel_err_internal<SP: Deref, CM: AChannelManager>(
3664- cm: &CM, err: ChannelError, chan: &mut Channel<SP>,
3665- ) -> (bool, MsgHandleErrInternal)
3666- where
3667- SP::Target: SignerProvider,
3668- {
3669- let chan_id = chan.context().channel_id();
3670- convert_channel_err_internal(err, chan_id, |reason, msg| {
3671- let cm = cm.get_cm();
3672- let logger = WithChannelContext::from(&cm.logger, chan.context(), None);
3673-
3674- let shutdown_res = chan.force_shutdown(reason);
3675- log_error!(logger, "Closed channel due to close-required error: {}", msg);
3676- cm.short_to_chan_info.write().unwrap().remove(&chan.context().outbound_scid_alias());
3677- // If the channel was never confirmed on-chain prior to its closure, remove the
3678- // outbound SCID alias we used for it from the collision-prevention set. While we
3679- // generally want to avoid ever re-using an outbound SCID alias across all channels, we
3680- // also don't want a counterparty to be able to trivially cause a memory leak by simply
3681- // opening a million channels with us which are closed before we ever reach the funding
3682- // stage.
3683- let outbound_alias = chan.context().outbound_scid_alias();
3684- let alias_removed = cm.outbound_scid_aliases.lock().unwrap().remove(&outbound_alias);
3685- debug_assert!(alias_removed);
3686- (shutdown_res, None)
3687- })
3688- }
3689-
36903600/// When a channel is removed, two things need to happen:
36913601/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
36923602/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
@@ -3706,28 +3616,28 @@ macro_rules! convert_channel_err {
37063616 let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
37073617 let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
37083618 let (close, mut err) =
3709- convert_funded_channel_err_internal( $self, closed_update_ids, in_flight_updates, Some($shutdown_result), reason, $funded_channel);
3619+ $self.convert_funded_channel_err_internal( closed_update_ids, in_flight_updates, Some($shutdown_result), reason, $funded_channel);
37103620 err.dont_send_error_message();
37113621 debug_assert!(close);
37123622 err
37133623 } };
37143624 ($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, FUNDED_CHANNEL) => { {
37153625 let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
37163626 let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3717- convert_funded_channel_err_internal( $self, closed_update_ids, in_flight_updates, None, $err, $funded_channel)
3627+ $self.convert_funded_channel_err_internal( closed_update_ids, in_flight_updates, None, $err, $funded_channel)
37183628 } };
37193629 ($self: ident, $peer_state: expr, $err: expr, $channel: expr, UNFUNDED_CHANNEL) => { {
3720- convert_unfunded_channel_err_internal( $self, $err, $channel)
3630+ $self.convert_unfunded_channel_err_internal( $err, $channel)
37213631 } };
37223632 ($self: ident, $peer_state: expr, $err: expr, $channel: expr) => {
37233633 match $channel.as_funded_mut() {
37243634 Some(funded_channel) => {
37253635 let closed_update_ids = &mut $peer_state.closed_channel_monitor_update_ids;
37263636 let in_flight_updates = &mut $peer_state.in_flight_monitor_updates;
3727- convert_funded_channel_err_internal( $self, closed_update_ids, in_flight_updates, None, $err, funded_channel)
3637+ $self.convert_funded_channel_err_internal( closed_update_ids, in_flight_updates, None, $err, funded_channel)
37283638 },
37293639 None => {
3730- convert_unfunded_channel_err_internal( $self, $err, $channel)
3640+ $self.convert_unfunded_channel_err_internal( $err, $channel)
37313641 },
37323642 }
37333643 };
@@ -4034,6 +3944,94 @@ where
40343944 })
40353945 }
40363946
3947+ fn convert_funded_channel_err_internal(
3948+ &self, closed_channel_monitor_update_ids: &mut BTreeMap<ChannelId, u64>,
3949+ in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
3950+ coop_close_shutdown_res: Option<ShutdownResult>, err: ChannelError,
3951+ chan: &mut FundedChannel<SP>,
3952+ ) -> (bool, MsgHandleErrInternal) {
3953+ let chan_id = chan.context.channel_id();
3954+ convert_channel_err_internal(err, chan_id, |reason, msg| {
3955+ let logger = WithChannelContext::from(&self.logger, &chan.context, None);
3956+
3957+ let mut shutdown_res = if let Some(res) = coop_close_shutdown_res {
3958+ res
3959+ } else {
3960+ chan.force_shutdown(reason)
3961+ };
3962+ let chan_update = self.get_channel_update_for_broadcast(chan).ok();
3963+
3964+ log_error!(logger, "Closed channel due to close-required error: {}", msg);
3965+
3966+ if let Some((_, funding_txo, _, update)) = shutdown_res.monitor_update.take() {
3967+ handle_new_monitor_update_locked_actions_handled_by_caller!(
3968+ self,
3969+ funding_txo,
3970+ update,
3971+ in_flight_monitor_updates,
3972+ chan.context
3973+ );
3974+ }
3975+ // If there's a possibility that we need to generate further monitor updates for this
3976+ // channel, we need to store the last update_id of it. However, we don't want to insert
3977+ // into the map (which prevents the `PeerState` from being cleaned up) for channels that
3978+ // never even got confirmations (which would open us up to DoS attacks).
3979+ let update_id = chan.context.get_latest_monitor_update_id();
3980+ let funding_confirmed = chan.funding.get_funding_tx_confirmation_height().is_some();
3981+ let chan_zero_conf = chan.context.minimum_depth(&chan.funding) == Some(0);
3982+ if funding_confirmed || chan_zero_conf || update_id > 1 {
3983+ closed_channel_monitor_update_ids.insert(chan_id, update_id);
3984+ }
3985+ let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
3986+ if let Some(short_id) = chan.funding.get_short_channel_id() {
3987+ short_to_chan_info.remove(&short_id);
3988+ } else {
3989+ // If the channel was never confirmed on-chain prior to its closure, remove the
3990+ // outbound SCID alias we used for it from the collision-prevention set. While we
3991+ // generally want to avoid ever re-using an outbound SCID alias across all channels, we
3992+ // also don't want a counterparty to be able to trivially cause a memory leak by simply
3993+ // opening a million channels with us which are closed before we ever reach the funding
3994+ // stage.
3995+ let outbound_alias = chan.context.outbound_scid_alias();
3996+ let alias_removed =
3997+ self.outbound_scid_aliases.lock().unwrap().remove(&outbound_alias);
3998+ debug_assert!(alias_removed);
3999+ }
4000+ short_to_chan_info.remove(&chan.context.outbound_scid_alias());
4001+ for scid in chan.context.historical_scids() {
4002+ short_to_chan_info.remove(scid);
4003+ }
4004+
4005+ (shutdown_res, chan_update)
4006+ })
4007+ }
4008+
4009+ fn convert_unfunded_channel_err_internal(
4010+ &self, err: ChannelError, chan: &mut Channel<SP>,
4011+ ) -> (bool, MsgHandleErrInternal)
4012+ where
4013+ SP::Target: SignerProvider,
4014+ {
4015+ let chan_id = chan.context().channel_id();
4016+ convert_channel_err_internal(err, chan_id, |reason, msg| {
4017+ let logger = WithChannelContext::from(&self.logger, chan.context(), None);
4018+
4019+ let shutdown_res = chan.force_shutdown(reason);
4020+ log_error!(logger, "Closed channel due to close-required error: {}", msg);
4021+ self.short_to_chan_info.write().unwrap().remove(&chan.context().outbound_scid_alias());
4022+ // If the channel was never confirmed on-chain prior to its closure, remove the
4023+ // outbound SCID alias we used for it from the collision-prevention set. While we
4024+ // generally want to avoid ever re-using an outbound SCID alias across all channels, we
4025+ // also don't want a counterparty to be able to trivially cause a memory leak by simply
4026+ // opening a million channels with us which are closed before we ever reach the funding
4027+ // stage.
4028+ let outbound_alias = chan.context().outbound_scid_alias();
4029+ let alias_removed = self.outbound_scid_aliases.lock().unwrap().remove(&outbound_alias);
4030+ debug_assert!(alias_removed);
4031+ (shutdown_res, None)
4032+ })
4033+ }
4034+
40374035 /// Gets the current [`UserConfig`] which controls some global behavior and includes the
40384036 /// default configuration applied to all new channels.
40394037 pub fn get_current_config(&self) -> UserConfig {
0 commit comments