@@ -3167,22 +3167,33 @@ macro_rules! handle_error {
3167
3167
/// directly avoids duplicate error messages.
3168
3168
#[rustfmt::skip]
3169
3169
macro_rules! locked_close_channel {
3170
- ($self: ident, $peer_state: expr, $channel_context: expr, $channel_funding: expr, $shutdown_res_mut: expr) => {{
3170
+ ($self: ident, $chan_context: expr, UNFUNDED) => {{
3171
+ $self.short_to_chan_info.write().unwrap().remove(&$chan_context.outbound_scid_alias());
3172
+ // If the channel was never confirmed on-chain prior to its closure, remove the
3173
+ // outbound SCID alias we used for it from the collision-prevention set. While we
3174
+ // generally want to avoid ever re-using an outbound SCID alias across all channels, we
3175
+ // also don't want a counterparty to be able to trivially cause a memory leak by simply
3176
+ // opening a million channels with us which are closed before we ever reach the funding
3177
+ // stage.
3178
+ let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$chan_context.outbound_scid_alias());
3179
+ debug_assert!(alias_removed);
3180
+ }};
3181
+ ($self: ident, $peer_state: expr, $funded_chan: expr, $shutdown_res_mut: expr, FUNDED) => {{
3171
3182
if let Some((_, funding_txo, _, update)) = $shutdown_res_mut.monitor_update.take() {
3172
3183
handle_new_monitor_update!($self, funding_txo, update, $peer_state,
3173
- $channel_context , REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
3184
+ $funded_chan.context , REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER);
3174
3185
}
3175
3186
// If there's a possibility that we need to generate further monitor updates for this
3176
3187
// channel, we need to store the last update_id of it. However, we don't want to insert
3177
3188
// into the map (which prevents the `PeerState` from being cleaned up) for channels that
3178
3189
// never even got confirmations (which would open us up to DoS attacks).
3179
- let update_id = $channel_context.get_latest_monitor_update_id();
3180
- if $channel_funding.get_funding_tx_confirmation_height().is_some() || $channel_context.minimum_depth($channel_funding) == Some(0) || update_id > 1 {
3181
- let chan_id = $channel_context.channel_id();
3190
+ let update_id = $funded_chan.context.get_latest_monitor_update_id();
3191
+ let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3192
+ if $funded_chan.funding.get_funding_tx_confirmation_height().is_some() || $funded_chan.context.minimum_depth(&$funded_chan.funding) == Some(0) || update_id > 1 {
3193
+ let chan_id = $funded_chan.context.channel_id();
3182
3194
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
3183
3195
}
3184
- let mut short_to_chan_info = $self.short_to_chan_info.write().unwrap();
3185
- if let Some(short_id) = $channel_funding.get_short_channel_id() {
3196
+ if let Some(short_id) = $funded_chan.funding.get_short_channel_id() {
3186
3197
short_to_chan_info.remove(&short_id);
3187
3198
} else {
3188
3199
// If the channel was never confirmed on-chain prior to its closure, remove the
@@ -3191,11 +3202,11 @@ macro_rules! locked_close_channel {
3191
3202
// also don't want a counterparty to be able to trivially cause a memory leak by simply
3192
3203
// opening a million channels with us which are closed before we ever reach the funding
3193
3204
// stage.
3194
- let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$channel_context .outbound_scid_alias());
3205
+ let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$funded_chan.context .outbound_scid_alias());
3195
3206
debug_assert!(alias_removed);
3196
3207
}
3197
- short_to_chan_info.remove(&$channel_context .outbound_scid_alias());
3198
- for scid in $channel_context .historical_scids() {
3208
+ short_to_chan_info.remove(&$funded_chan.context .outbound_scid_alias());
3209
+ for scid in $funded_chan.context .historical_scids() {
3199
3210
short_to_chan_info.remove(scid);
3200
3211
}
3201
3212
}}
@@ -3204,7 +3215,7 @@ macro_rules! locked_close_channel {
3204
3215
/// Returns (boolean indicating if we should remove the Channel object from memory, a mapped error)
3205
3216
#[rustfmt::skip]
3206
3217
macro_rules! convert_channel_err {
3207
- ($self: ident, $peer_state: expr, $err: expr, $context : expr, $funding : expr, $channel_id : expr, MANUAL_CHANNEL_UPDATE, $channel_update : expr) => {
3218
+ ($self: ident, $peer_state: expr, $err: expr, $chan : expr, $close : expr, $locked_close : expr, $channel_id : expr, _internal ) => {
3208
3219
match $err {
3209
3220
ChannelError::Warn(msg) => {
3210
3221
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(msg), *$channel_id))
@@ -3216,33 +3227,43 @@ macro_rules! convert_channel_err {
3216
3227
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), *$channel_id))
3217
3228
},
3218
3229
ChannelError::Close((msg, reason)) => {
3219
- let logger = WithChannelContext::from(&$self.logger, &$context, None );
3220
- log_error!( logger, "Closing channel {} due to close-required error: {}", $channel_id, msg );
3221
- let mut shutdown_res = $context.force_shutdown($funding, true, reason );
3222
- locked_close_channel!($self, $peer_state, $context, $funding, &mut shutdown_res);
3230
+ let (mut shutdown_res, chan_update) = $close(reason );
3231
+ let logger = WithChannelContext::from(&$self.logger, &$chan.context(), None );
3232
+ log_error!(logger, "Closed channel {} due to close-required error: {}", $channel_id, msg );
3233
+ $locked_close( &mut shutdown_res, $chan );
3223
3234
let err =
3224
- MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $channel_update );
3235
+ MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, chan_update );
3225
3236
(true, err)
3226
3237
},
3227
3238
ChannelError::SendError(msg) => {
3228
3239
(false, MsgHandleErrInternal::from_chan_no_close(ChannelError::SendError(msg), *$channel_id))
3229
3240
},
3230
3241
}
3231
3242
};
3232
- ($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, $channel_id: expr, FUNDED_CHANNEL) => {
3233
- 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() })
3234
- };
3235
- ($self: ident, $peer_state: expr, $err: expr, $context: expr, $funding: expr, $channel_id: expr, UNFUNDED_CHANNEL) => {
3236
- convert_channel_err!($self, $peer_state, $err, $context, $funding, $channel_id, MANUAL_CHANNEL_UPDATE, None)
3237
- };
3243
+ ($self: ident, $peer_state: expr, $err: expr, $funded_channel: expr, $channel_id: expr, FUNDED_CHANNEL) => { {
3244
+ let mut do_close = |reason| {
3245
+ (
3246
+ $funded_channel.force_shutdown(reason, true),
3247
+ $self.get_channel_update_for_broadcast(&$funded_channel).ok(),
3248
+ )
3249
+ };
3250
+ let mut locked_close = |shutdown_res_mut: &mut ShutdownResult, funded_channel: &mut FundedChannel<_>| {
3251
+ locked_close_channel!($self, $peer_state, funded_channel, shutdown_res_mut, FUNDED);
3252
+ };
3253
+ convert_channel_err!($self, $peer_state, $err, $funded_channel, do_close, locked_close, $channel_id, _internal)
3254
+ } };
3255
+ ($self: ident, $peer_state: expr, $err: expr, $channel: expr, $channel_id: expr, UNFUNDED_CHANNEL) => { {
3256
+ let mut do_close = |reason| { ($channel.force_shutdown(true, reason), None) };
3257
+ let locked_close = |_, chan: &mut Channel<_>| { locked_close_channel!($self, chan.context(), UNFUNDED); };
3258
+ convert_channel_err!($self, $peer_state, $err, $channel, do_close, locked_close, $channel_id, _internal)
3259
+ } };
3238
3260
($self: ident, $peer_state: expr, $err: expr, $channel: expr, $channel_id: expr) => {
3239
3261
match $channel.as_funded_mut() {
3240
3262
Some(funded_channel) => {
3241
3263
convert_channel_err!($self, $peer_state, $err, funded_channel, $channel_id, FUNDED_CHANNEL)
3242
3264
},
3243
3265
None => {
3244
- let (funding, context) = $channel.funding_and_context_mut();
3245
- convert_channel_err!($self, $peer_state, $err, context, funding, $channel_id, UNFUNDED_CHANNEL)
3266
+ convert_channel_err!($self, $peer_state, $err, $channel, $channel_id, UNFUNDED_CHANNEL)
3246
3267
},
3247
3268
}
3248
3269
};
@@ -4114,7 +4135,7 @@ where
4114
4135
let reason = ClosureReason::LocallyCoopClosedUnfundedChannel;
4115
4136
let err = ChannelError::Close((reason.to_string(), reason));
4116
4137
let mut chan = chan_entry.remove();
4117
- let (_, mut e) = convert_channel_err!(self, peer_state, err, chan, chan_id);
4138
+ let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan, chan_id);
4118
4139
e.dont_send_error_message();
4119
4140
shutdown_result = Err(e);
4120
4141
}
@@ -4308,7 +4329,7 @@ where
4308
4329
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
4309
4330
let reason = ClosureReason::FundingBatchClosure;
4310
4331
let err = ChannelError::Close((reason.to_string(), reason));
4311
- let (_, e) = convert_channel_err!(self, peer_state, err, chan, &channel_id);
4332
+ let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan, &channel_id);
4312
4333
shutdown_results.push((Err(e), counterparty_node_id));
4313
4334
}
4314
4335
}
@@ -4372,7 +4393,7 @@ where
4372
4393
if let Some(mut chan) = peer_state.channel_by_id.remove(channel_id) {
4373
4394
log_error!(logger, "Force-closing channel {}", channel_id);
4374
4395
let err = ChannelError::Close((message, reason));
4375
- let (_, mut e) = convert_channel_err!(self, peer_state, err, chan, channel_id);
4396
+ let (_, mut e) = convert_channel_err!(self, peer_state, err, &mut chan, channel_id);
4376
4397
mem::drop(peer_state_lock);
4377
4398
mem::drop(per_peer_state);
4378
4399
if is_from_counterparty {
@@ -5805,7 +5826,7 @@ where
5805
5826
let reason = ClosureReason::ProcessingError { err: e.clone() };
5806
5827
let err = ChannelError::Close((e.clone(), reason));
5807
5828
let (_, e) =
5808
- convert_channel_err!(self, peer_state, err, chan, &channel_id);
5829
+ convert_channel_err!(self, peer_state, err, &mut chan, &channel_id);
5809
5830
shutdown_results.push((Err(e), counterparty_node_id));
5810
5831
});
5811
5832
}
@@ -8623,15 +8644,16 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8623
8644
let logger = WithChannelContext::from(&self.logger, &inbound_chan.context, None);
8624
8645
match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &&logger) {
8625
8646
Ok(res) => res,
8626
- Err((mut inbound_chan, err)) => {
8647
+ Err((inbound_chan, err)) => {
8627
8648
// We've already removed this inbound channel from the map in `PeerState`
8628
8649
// above so at this point we just need to clean up any lingering entries
8629
8650
// concerning this channel as it is safe to do so.
8630
8651
debug_assert!(matches!(err, ChannelError::Close(_)));
8631
8652
// Really we should be returning the channel_id the peer expects based
8632
8653
// on their funding info here, but they're horribly confused anyway, so
8633
8654
// there's not a lot we can do to save them.
8634
- return Err(convert_channel_err!(self, peer_state, err, inbound_chan.context, &inbound_chan.funding, &msg.temporary_channel_id, UNFUNDED_CHANNEL).1);
8655
+ let mut chan = Channel::from(inbound_chan);
8656
+ return Err(convert_channel_err!(self, peer_state, err, &mut chan, &msg.temporary_channel_id, UNFUNDED_CHANNEL).1);
8635
8657
},
8636
8658
}
8637
8659
},
@@ -8653,7 +8675,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8653
8675
// Thus, we must first unset the funding outpoint on the channel.
8654
8676
let err = ChannelError::close($err.to_owned());
8655
8677
chan.unset_funding_info();
8656
- return Err(convert_channel_err!(self, peer_state, err, chan.context, &chan.funding, &funded_channel_id, UNFUNDED_CHANNEL).1);
8678
+ let mut chan = Channel::from(chan);
8679
+ return Err(convert_channel_err!(self, peer_state, err, &mut chan, &funded_channel_id, UNFUNDED_CHANNEL).1);
8657
8680
} } }
8658
8681
8659
8682
match peer_state.channel_by_id.entry(funded_channel_id) {
@@ -9194,7 +9217,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9194
9217
let err = ChannelError::Close((reason.to_string(), reason));
9195
9218
let mut chan = chan_entry.remove();
9196
9219
let (_, mut e) =
9197
- convert_channel_err!(self, peer_state, err, chan, &msg.channel_id);
9220
+ convert_channel_err!(self, peer_state, err, &mut chan, &msg.channel_id);
9198
9221
e.dont_send_error_message();
9199
9222
return Err(e);
9200
9223
},
@@ -9251,7 +9274,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
9251
9274
// fully delete it from tracking (the channel monitor is still around to
9252
9275
// watch for old state broadcasts)!
9253
9276
debug_assert!(tx.is_some());
9254
- locked_close_channel!(self, peer_state, chan.context, &chan.funding, close_res );
9277
+ locked_close_channel!(self, peer_state, chan, close_res, FUNDED );
9255
9278
(tx, Some(chan_entry.remove()), Some(close_res))
9256
9279
} else {
9257
9280
debug_assert!(tx.is_none());
@@ -10225,7 +10248,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10225
10248
};
10226
10249
let err = ChannelError::Close((reason.to_string(), reason));
10227
10250
let mut chan = chan_entry.remove();
10228
- let (_, e) = convert_channel_err!(self, peer_state, err, chan, &channel_id);
10251
+ let (_, e) = convert_channel_err!(self, peer_state, err, &mut chan, &channel_id);
10229
10252
failed_channels.push((Err(e), counterparty_node_id));
10230
10253
}
10231
10254
}
@@ -10414,10 +10437,14 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10414
10437
_ => unblock_chan(chan, &mut peer_state.pending_msg_events),
10415
10438
};
10416
10439
if let Some(mut shutdown_result) = shutdown_result {
10417
- let context = & chan.context();
10440
+ let context = chan.context();
10418
10441
let logger = WithChannelContext::from(&self.logger, context, None);
10419
10442
log_trace!(logger, "Removing channel {} now that the signer is unblocked", context.channel_id());
10420
- locked_close_channel!(self, peer_state, context, chan.funding(), shutdown_result);
10443
+ if let Some(funded_channel) = chan.as_funded_mut() {
10444
+ locked_close_channel!(self, peer_state, funded_channel, shutdown_result, FUNDED);
10445
+ } else {
10446
+ locked_close_channel!(self, chan.context(), UNFUNDED);
10447
+ }
10421
10448
shutdown_results.push(shutdown_result);
10422
10449
false
10423
10450
} else {
@@ -10460,7 +10487,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
10460
10487
}
10461
10488
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10462
10489
if let Some(mut shutdown_result) = shutdown_result_opt {
10463
- locked_close_channel!(self, peer_state, & funded_chan.context, &funded_chan.funding, shutdown_result );
10490
+ locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED );
10464
10491
shutdown_results.push(shutdown_result);
10465
10492
}
10466
10493
if let Some(tx) = tx_opt {
0 commit comments