@@ -3169,22 +3169,33 @@ macro_rules! handle_error {
3169
3169
/// directly avoids duplicate error messages.
3170
3170
#[rustfmt::skip]
3171
3171
macro_rules! locked_close_channel {
3172
- ($self: ident, $peer_state: expr, $channel_context: expr, $channel_funding: expr, $shutdown_res_mut: expr) => {{
3172
+ ($self: ident, $chan_context: expr, UNFUNDED) => {{
3173
+ $self.short_to_chan_info.write().unwrap().remove(&$chan_context.outbound_scid_alias());
3174
+ // If the channel was never confirmed on-chain prior to its closure, remove the
3175
+ // outbound SCID alias we used for it from the collision-prevention set. While we
3176
+ // generally want to avoid ever re-using an outbound SCID alias across all channels, we
3177
+ // also don't want a counterparty to be able to trivially cause a memory leak by simply
3178
+ // opening a million channels with us which are closed before we ever reach the funding
3179
+ // stage.
3180
+ let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$chan_context.outbound_scid_alias());
3181
+ debug_assert!(alias_removed);
3182
+ }};
3183
+ ($self: ident, $peer_state: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
3173
3184
if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
3174
3185
handle_new_monitor_update!($self, funding_txo, update, $peer_state,
3175
- $channel_context , REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
3186
+ $funded_chan.context , REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
3176
3187
}
3177
3188
// If there's a possibility that we need to generate further monitor updates for this
3178
3189
// channel, we need to store the last update_id of it. However, we don't want to insert
3179
3190
// into the map (which prevents the `PeerState` from being cleaned up) for channels that
3180
3191
// never even got confirmations (which would open us up to DoS attacks).
3181
- let update_id = $channel_context .get_latest_monitor_update_id();
3182
- if $channel_funding. get_funding_tx_confirmation_height().is_some() || $channel_context. minimum_depth($channel_funding ) == Some(0) || update_id > 1 {
3183
- let chan_id = $channel_context .channel_id();
3192
+ let update_id = $funded_chan.context .get_latest_monitor_update_id();
3193
+ if $funded_chan.funding. get_funding_tx_confirmation_height().is_some() || $funded_chan.context. minimum_depth(&$funded_chan.funding ) == Some(0) || update_id > 1 {
3194
+ let chan_id = $funded_chan.context .channel_id();
3184
3195
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
3185
3196
}
3186
3197
let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3187
- if let Some(short_id) = $channel_funding .get_short_channel_id() {
3198
+ if let Some(short_id) = $funded_chan.funding .get_short_channel_id() {
3188
3199
short_to_chan_info.remove(&short_id);
3189
3200
} else {
3190
3201
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3193,11 +3204,11 @@ macro_rules! locked_close_channel {
3193
3204
// also don't want a counterparty to be able to trivially cause a memory leak by simply
3194
3205
// opening a million channels with us which are closed before we ever reach the funding
3195
3206
// stage.
3196
- let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$channel_context .outbound_scid_alias());
3207
+ let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$funded_chan.context .outbound_scid_alias());
3197
3208
debug_assert!(alias_removed);
3198
3209
}
3199
- short_to_chan_info.remove(&$channel_context .outbound_scid_alias());
3200
- for scid in $channel_context .historical_scids() {
3210
+ short_to_chan_info.remove(&$funded_chan.context .outbound_scid_alias());
3211
+ for scid in $funded_chan.context .historical_scids() {
3201
3212
short_to_chan_info.remove(scid);
3202
3213
}
3203
3214
}}
@@ -3206,7 +3217,7 @@ macro_rules! locked_close_channel {
3206
3217
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
3207
3218
#[rustfmt::skip]
3208
3219
macro_rules! convert_channel_err {
3209
- ($self: ident, $peer_state: expr, $err: expr, $context : expr, $funding : expr, $channel_id : expr, MANUAL_CHANNEL_UPDATE, $channel_update : expr) => {
3220
+ ($self: ident, $peer_state: expr, $err: expr, $chan : expr, $close : expr, $locked_close : expr, $channel_id : expr, _internal ) => {
3210
3221
match $err {
3211
3222
ChannelError::Warn(msg) => {
3212
3223
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), *$channel_id))
@@ -3218,33 +3229,43 @@ macro_rules! convert_channel_err {
3218
3229
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
3219
3230
},
3220
3231
ChannelError::Close((msg, reason)) => {
3221
- let logger = WithChannelContext::from(&$self.logger, &$context, None );
3222
- log_error!( logger, "Closing channel {} due to close-required error: {}", $channel_id, msg );
3223
- let mut shutdown_res = $context.force_shutdown($funding, true, reason );
3224
- locked_close_channel!($self, $peer_state, $context, $funding, &mut shutdown_res);
3232
+ let (mut shutdown_res, chan_update) = $close(reason );
3233
+ let logger = WithChannelContext::from(&$self.logger, &$chan.context(), None );
3234
+ log_error!(logger, "Closed channel {} due to close-required error: {}", $channel_id, msg );
3235
+ $locked_close( &mut shutdown_res, $chan );
3225
3236
let err =
3226
- MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $channel_update );
3237
+ MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, chan_update );
3227
3238
(true, err)
3228
3239
},
3229
3240
ChannelError::SendError(msg) => {
3230
3241
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::SendError(msg), *$channel_id))
3231
3242
},
3232
3243
}
3233
3244
};
3234
- ($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, $channel_id: expr, FUNDED_CHANNEL) => {
3235
- convert_channel_err!($self, $peer_state, $err, $funded_channel.context, &$funded_channel.funding, $channel_id, MANUAL_CHANNEL_UPDATE, { $self.get_channel_update_for_broadcast(&$funded_channel).ok() })
3236
- };
3237
- ($self: ident, $peer_state: expr, $err: expr, $context: expr, $funding: expr, $channel_id: expr, UNFUNDED_CHANNEL) => {
3238
- convert_channel_err!($self, $peer_state, $err, $context, $funding, $channel_id, MANUAL_CHANNEL_UPDATE, None)
3239
- };
3245
+ ($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, $channel_id: expr, FUNDED_CHANNEL) => { {
3246
+ let mut do_close = |reason| {
3247
+ (
3248
+ $funded_channel.force_shutdown(reason, true),
3249
+ $self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3250
+ )
3251
+ };
3252
+ let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3253
+ locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3254
+ };
3255
+ convert_channel_err!($self, $peer_state, $err, $funded_channel, do_close, locked_close, $channel_id, _internal)
3256
+ } };
3257
+ ($self: ident, $peer_state: expr, $err: expr, $channel: expr, $channel_id: expr, UNFUNDED_CHANNEL) => { {
3258
+ let mut do_close = |reason| { ($channel.force_shutdown(true, reason), None) };
3259
+ let locked_close = |_, chan: &mut Channel<_>| { locked_close_channel!($self, chan.context(), UNFUNDED); };
3260
+ convert_channel_err!($self, $peer_state, $err, $channel, do_close, locked_close, $channel_id, _internal)
3261
+ } };
3240
3262
($self: ident, $peer_state: expr, $err: expr, $channel: expr, $channel_id: expr) => {
3241
3263
match $channel.as_funded_mut() {
3242
3264
Some(funded_channel) => {
3243
3265
convert_channel_err!($self, $peer_state, $err, funded_channel, $channel_id, FUNDED_CHANNEL)
3244
3266
},
3245
3267
None => {
3246
- let (funding, context) = $channel.funding_and_context_mut();
3247
- convert_channel_err!($self, $peer_state, $err, context, funding, $channel_id, UNFUNDED_CHANNEL)
3268
+ convert_channel_err!($self, $peer_state, $err, $channel, $channel_id, UNFUNDED_CHANNEL)
3248
3269
},
3249
3270
}
3250
3271
};
@@ -4116,7 +4137,7 @@ where
4116
4137
let reason = ClosureReason::LocallyCoopClosedUnfundedChannel;
4117
4138
let err = ChannelError::Close((reason.to_string(), reason));
4118
4139
let mut chan = chan_entry.remove();
4119
- let (_, mut e) = convert_channel_err!(self, peer_state, err, chan, chan_id);
4140
+ let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan, chan_id);
4120
4141
e.dont_send_error_message();
4121
4142
shutdown_result = Err(e);
4122
4143
}
@@ -4313,7 +4334,7 @@ where
4313
4334
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
4314
4335
let reason = ClosureReason::FundingBatchClosure;
4315
4336
let err = ChannelError::Close((reason.to_string(), reason));
4316
- let (_, e) = convert_channel_err!(self, peer_state, err, chan, &channel_id);
4337
+ let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan, &channel_id);
4317
4338
shutdown_results.push((Err(e), counterparty_node_id));
4318
4339
}
4319
4340
}
@@ -4377,7 +4398,7 @@ where
4377
4398
if let Some(mut chan) = peer_state.channel_by_id.remove(channel_id) {
4378
4399
log_error!(logger, "Force-closing channel {}", channel_id);
4379
4400
let err = ChannelError::Close((message, reason));
4380
- let (_, mut e) = convert_channel_err!(self, peer_state, err, chan, channel_id);
4401
+ let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan, channel_id);
4381
4402
mem::drop(peer_state_lock);
4382
4403
mem::drop(per_peer_state);
4383
4404
if is_from_counterparty {
@@ -5834,7 +5855,7 @@ where
5834
5855
let reason = ClosureReason::ProcessingError { err: e.clone() };
5835
5856
let err = ChannelError::Close((e.clone(), reason));
5836
5857
let (_, e) =
5837
- convert_channel_err!(self, peer_state, err, chan, &channel_id);
5858
+ convert_channel_err!(self, peer_state, err, &mut chan, &channel_id);
5838
5859
shutdown_results.push((Err(e), counterparty_node_id));
5839
5860
});
5840
5861
}
@@ -8655,15 +8676,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8655
8676
let logger = WithChannelContext::from(&self.logger, &inbound_chan.context, None);
8656
8677
match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &&logger) {
8657
8678
Ok(res) => res,
8658
- Err((mut inbound_chan, err)) => {
8679
+ Err((inbound_chan, err)) => {
8659
8680
// We've already removed this inbound channel from the map in `PeerState`
8660
8681
// above so at this point we just need to clean up any lingering entries
8661
8682
// concerning this channel as it is safe to do so.
8662
8683
debug_assert!(matches!(err, ChannelError::Close(_)));
8663
8684
// Really we should be returning the channel_id the peer expects based
8664
8685
// on their funding info here, but they're horribly confused anyway, so
8665
8686
// there's not a lot we can do to save them.
8666
- return Err(convert_channel_err!(self, peer_state, err, inbound_chan.context, &inbound_chan.funding, &msg.temporary_channel_id, UNFUNDED_CHANNEL).1);
8687
+ let mut chan = Channel::from(inbound_chan);
8688
+ return Err(convert_channel_err!(self, peer_state, err, &mut chan, &msg.temporary_channel_id, UNFUNDED_CHANNEL).1);
8667
8689
},
8668
8690
}
8669
8691
},
@@ -8685,7 +8707,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8685
8707
// Thus, we must first unset the funding outpoint on the channel.
8686
8708
let err = ChannelError::close($err.to_owned());
8687
8709
chan.unset_funding_info();
8688
- return Err(convert_channel_err!(self, peer_state, err, chan.context, &chan.funding, &funded_channel_id, UNFUNDED_CHANNEL).1);
8710
+ let mut chan = Channel::from(chan);
8711
+ return Err(convert_channel_err!(self, peer_state, err, &mut chan, &funded_channel_id, UNFUNDED_CHANNEL).1);
8689
8712
} } }
8690
8713
8691
8714
match peer_state.channel_by_id.entry(funded_channel_id) {
@@ -9226,7 +9249,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9226
9249
let err = ChannelError::Close((reason.to_string(), reason));
9227
9250
let mut chan = chan_entry.remove();
9228
9251
let (_, mut e) =
9229
- convert_channel_err!(self, peer_state, err, chan, &msg.channel_id);
9252
+ convert_channel_err!(self, peer_state, err, &mut chan, &msg.channel_id);
9230
9253
e.dont_send_error_message();
9231
9254
return Err(e);
9232
9255
},
@@ -9283,7 +9306,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9283
9306
// fully delete it from tracking (the channel monitor is still around to
9284
9307
// watch for old state broadcasts)!
9285
9308
debug_assert!(tx.is_some());
9286
- locked_close_channel!(self, peer_state, chan.context, &chan.funding, close_res );
9309
+ locked_close_channel!(self, peer_state, chan, close_res, FUNDED );
9287
9310
(tx, Some(chan_entry.remove()), Some(close_res))
9288
9311
} else {
9289
9312
debug_assert!(tx.is_none());
@@ -10257,7 +10280,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10257
10280
};
10258
10281
let err = ChannelError::Close((reason.to_string(), reason));
10259
10282
let mut chan = chan_entry.remove();
10260
- let (_, e) = convert_channel_err!(self, peer_state, err, chan, &channel_id);
10283
+ let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan, &channel_id);
10261
10284
failed_channels.push((Err(e), counterparty_node_id));
10262
10285
}
10263
10286
}
@@ -10446,10 +10469,14 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10446
10469
_ => unblock_chan(chan, &mut peer_state.pending_msg_events),
10447
10470
};
10448
10471
if let Some(mut shutdown_result) = shutdown_result {
10449
- let context = & chan.context();
10472
+ let context = chan.context();
10450
10473
let logger = WithChannelContext::from(&self.logger, context, None);
10451
10474
log_trace!(logger, "Removing channel {} now that the signer is unblocked", context.channel_id());
10452
- locked_close_channel!(self, peer_state, context, chan.funding(), shutdown_result);
10475
+ if let Some(funded_channel) = chan.as_funded_mut() {
10476
+ locked_close_channel!(self, peer_state, funded_channel, shutdown_result, FUNDED);
10477
+ } else {
10478
+ locked_close_channel!(self, chan.context(), UNFUNDED);
10479
+ }
10453
10480
shutdown_results.push(shutdown_result);
10454
10481
false
10455
10482
} else {
@@ -10492,7 +10519,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10492
10519
}
10493
10520
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10494
10521
if let Some(mut shutdown_result) = shutdown_result_opt {
10495
- locked_close_channel!(self, peer_state, & funded_chan.context, &funded_chan.funding, shutdown_result );
10522
+ locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED );
10496
10523
shutdown_results.push(shutdown_result);
10497
10524
}
10498
10525
if let Some(tx) = tx_opt {
0 commit comments