Skip to content

Commit 275b006

Browse files
committed
Rename convert_err methods
Make the names more descriptive and link shared documentation.
1 parent 7fb84e6 commit 275b006

File tree

1 file changed

+68
-56
lines changed

1 file changed

+68
-56
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,7 +3584,7 @@ macro_rules! break_channel_entry {
35843584
match $res {
35853585
Ok(res) => res,
35863586
Err(e) => {
3587-
let (drop, res) = $self.convert_channel_err(
3587+
let (drop, res) = $self.locked_handle_force_close(
35883588
&mut $peer_state.closed_channel_monitor_update_ids,
35893589
&mut $peer_state.in_flight_monitor_updates,
35903590
e,
@@ -3604,7 +3604,7 @@ macro_rules! try_channel_entry {
36043604
match $res {
36053605
Ok(res) => res,
36063606
Err(e) => {
3607-
let (drop, res) = $self.convert_channel_err(
3607+
let (drop, res) = $self.locked_handle_force_close(
36083608
&mut $peer_state.closed_channel_monitor_update_ids,
36093609
&mut $peer_state.in_flight_monitor_updates,
36103610
e,
@@ -4225,7 +4225,7 @@ where
42254225
let reason = ClosureReason::LocallyCoopClosedUnfundedChannel;
42264226
let err = ChannelError::Close((reason.to_string(), reason));
42274227
let mut chan = chan_entry.remove();
4228-
let (_, mut e) = self.convert_channel_err(
4228+
let (_, mut e) = self.locked_handle_force_close(
42294229
&mut peer_state.closed_channel_monitor_update_ids,
42304230
&mut peer_state.in_flight_monitor_updates,
42314231
err,
@@ -4367,8 +4367,11 @@ where
43674367
}
43684368

43694369
/// When a channel is removed, two things need to happen:
4370-
/// (a) [`ChannelManager::convert_channel_err`] must be called in the same `per_peer_state` lock as the
4371-
/// channel-closing action,
4370+
/// (a) Handle the initial within-lock closure for the channel via one of the following methods:
4371+
/// [`ChannelManager::locked_handle_unfunded_close`],
4372+
/// [`ChannelManager::locked_handle_funded_coop_close`],
4373+
/// [`ChannelManager::locked_handle_funded_force_close`] or
4374+
/// [`ChannelManager::locked_handle_force_close`].
43724375
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
43734376
/// [`ChannelManager::total_consistency_lock`]), which then calls this.
43744377
fn finish_close_channel(&self, mut shutdown_res: ShutdownResult) {
@@ -4437,7 +4440,7 @@ where
44374440
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
44384441
let reason = ClosureReason::FundingBatchClosure;
44394442
let err = ChannelError::Close((reason.to_string(), reason));
4440-
let (_, e) = self.convert_channel_err(
4443+
let (_, e) = self.locked_handle_force_close(
44414444
&mut peer_state.closed_channel_monitor_update_ids,
44424445
&mut peer_state.in_flight_monitor_updates,
44434446
err,
@@ -4534,7 +4537,7 @@ where
45344537
if let Some(mut chan) = peer_state.channel_by_id.remove(channel_id) {
45354538
log_error!(logger, "Force-closing channel");
45364539
let err = ChannelError::Close((message, reason));
4537-
let (_, mut e) = self.convert_channel_err(
4540+
let (_, mut e) = self.locked_handle_force_close(
45384541
&mut peer_state.closed_channel_monitor_update_ids,
45394542
&mut peer_state.in_flight_monitor_updates,
45404543
err,
@@ -4683,7 +4686,12 @@ where
46834686
})
46844687
}
46854688

4686-
fn convert_funded_channel_err_internal(
4689+
/// Handle the initial within-lock closure for a funded channel that is either force-closed or cooperatively
4690+
/// closed (as indicated by `coop_close_shutdown_res`).
4691+
///
4692+
/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
4693+
/// error)`.
4694+
fn locked_handle_funded_close_internal(
46874695
&self, closed_channel_monitor_update_ids: &mut BTreeMap<ChannelId, u64>,
46884696
in_flight_monitor_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
46894697
coop_close_shutdown_res: Option<ShutdownResult>, err: ChannelError,
@@ -4745,7 +4753,13 @@ where
47454753
})
47464754
}
47474755

4748-
fn convert_unfunded_channel_err_internal(
4756+
/// Handle the initial within-lock closure for an unfunded channel.
4757+
///
4758+
/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
4759+
/// error)`.
4760+
///
4761+
/// The same closure semantics as described in [`ChannelManager::locked_handle_force_close`] apply.
4762+
fn locked_handle_unfunded_close(
47494763
&self, err: ChannelError, chan: &mut Channel<SP>,
47504764
) -> (bool, MsgHandleErrInternal)
47514765
where
@@ -4771,21 +4785,19 @@ where
47714785
})
47724786
}
47734787

4774-
/// When a cooperatively closed channel is removed, two things need to happen:
4775-
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
4776-
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
4777-
/// [`ChannelManager::total_consistency_lock`]), which then calls
4778-
/// [`ChannelManager::finish_close_channel`].
4788+
/// Handle the initial within-lock closure for a channel that is cooperatively closed.
47794789
///
47804790
/// Returns a mapped error.
4781-
fn convert_channel_err_coop(
4791+
///
4792+
/// The same closure semantics as described in [`ChannelManager::locked_handle_force_close`] apply.
4793+
fn locked_handle_funded_coop_close(
47824794
&self, closed_update_ids: &mut BTreeMap<ChannelId, u64>,
47834795
in_flight_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
47844796
shutdown_result: ShutdownResult, funded_channel: &mut FundedChannel<SP>,
47854797
) -> MsgHandleErrInternal {
47864798
let reason =
47874799
ChannelError::Close(("Coop Closed".to_owned(), shutdown_result.closure_reason.clone()));
4788-
let (close, mut err) = self.convert_funded_channel_err_internal(
4800+
let (close, mut err) = self.locked_handle_funded_close_internal(
47894801
closed_update_ids,
47904802
in_flight_updates,
47914803
Some(shutdown_result),
@@ -4797,20 +4809,18 @@ where
47974809
err
47984810
}
47994811

4800-
/// When a funded channel is removed, two things need to happen:
4801-
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
4802-
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
4803-
/// [`ChannelManager::total_consistency_lock`]), which then calls
4804-
/// [`ChannelManager::finish_close_channel`].
4812+
/// Handle the initial within-lock closure for a funded channel that is force-closed.
48054813
///
48064814
/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
48074815
/// error)`.
4808-
fn convert_channel_err_funded(
4816+
///
4817+
/// The same closure semantics as described in [`ChannelManager::locked_handle_force_close`] apply.
4818+
fn locked_handle_funded_force_close(
48094819
&self, closed_update_ids: &mut BTreeMap<ChannelId, u64>,
48104820
in_flight_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
48114821
err: ChannelError, funded_channel: &mut FundedChannel<SP>,
48124822
) -> (bool, MsgHandleErrInternal) {
4813-
self.convert_funded_channel_err_internal(
4823+
self.locked_handle_funded_close_internal(
48144824
closed_update_ids,
48154825
in_flight_updates,
48164826
None,
@@ -4819,31 +4829,32 @@ where
48194829
)
48204830
}
48214831

4822-
/// When a channel that can be funded or unfunded is removed, two things need to happen:
4823-
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
4824-
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
4825-
/// [`ChannelManager::total_consistency_lock`]), which then calls
4826-
/// [`ChannelManager::finish_close_channel`].
4827-
///
4828-
/// Note that this step can be skipped if the channel was never opened (through the creation of a
4829-
/// [`ChannelMonitor`]/channel funding transaction) to begin with.
4832+
/// Handle the initial within-lock closure for a channel that is force-closed.
48304833
///
48314834
/// Returns `(boolean indicating if we should remove the Channel object from memory, a mapped
48324835
/// error)`.
4833-
fn convert_channel_err(
4836+
///
4837+
/// # Closure semantics
4838+
///
4839+
/// Two things need to happen:
4840+
/// (a) This method must be called in the same `per_peer_state` lock as the channel-closing action,
4841+
/// (b) [`ChannelManager::handle_error`] needs to be called without holding any locks (except
4842+
/// [`ChannelManager::total_consistency_lock`]), which then calls
4843+
/// [`ChannelManager::finish_close_channel`].
4844+
fn locked_handle_force_close(
48344845
&self, closed_update_ids: &mut BTreeMap<ChannelId, u64>,
48354846
in_flight_updates: &mut BTreeMap<ChannelId, (OutPoint, Vec<ChannelMonitorUpdate>)>,
48364847
err: ChannelError, channel: &mut Channel<SP>,
48374848
) -> (bool, MsgHandleErrInternal) {
48384849
match channel.as_funded_mut() {
4839-
Some(funded_channel) => self.convert_funded_channel_err_internal(
4850+
Some(funded_channel) => self.locked_handle_funded_close_internal(
48404851
closed_update_ids,
48414852
in_flight_updates,
48424853
None,
48434854
err,
48444855
funded_channel,
48454856
),
4846-
None => self.convert_unfunded_channel_err_internal(err, channel),
4857+
None => self.locked_handle_unfunded_close(err, channel),
48474858
}
48484859
}
48494860

@@ -6566,7 +6577,7 @@ where
65666577
let reason = ClosureReason::ProcessingError { err: e.clone() };
65676578
let err = ChannelError::Close((e.clone(), reason));
65686579
let peer_state = &mut *peer_state_lock;
6569-
let (_, e) = self.convert_channel_err(
6580+
let (_, e) = self.locked_handle_force_close(
65706581
&mut peer_state.closed_channel_monitor_update_ids,
65716582
&mut peer_state.in_flight_monitor_updates,
65726583
err,
@@ -8333,7 +8344,7 @@ where
83338344
if chan_needs_persist == NotifyOption::DoPersist { should_persist = NotifyOption::DoPersist; }
83348345

83358346
if let Err(e) = funded_chan.timer_check_closing_negotiation_progress() {
8336-
let (needs_close, err) = self.convert_channel_err_funded(&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, e, funded_chan);
8347+
let (needs_close, err) = self.locked_handle_funded_force_close(&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, e, funded_chan);
83378348
handle_errors.push((Err(err), counterparty_node_id));
83388349
if needs_close { return false; }
83398350
}
@@ -8410,7 +8421,7 @@ where
84108421
let reason = ClosureReason::FundingTimedOut;
84118422
let msg = "Force-closing pending channel due to timeout awaiting establishment handshake".to_owned();
84128423
let err = ChannelError::Close((msg, reason));
8413-
let (_, e) = self.convert_channel_err(
8424+
let (_, e) = self.locked_handle_force_close(
84148425
&mut peer_state.closed_channel_monitor_update_ids,
84158426
&mut peer_state.in_flight_monitor_updates,
84168427
err,
@@ -10614,7 +10625,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1061410625
// concerning this channel as it is safe to do so.
1061510626
debug_assert!(matches!(err, ChannelError::Close(_)));
1061610627
let mut chan = Channel::from(inbound_chan);
10617-
return Err(self.convert_channel_err(
10628+
return Err(self.locked_handle_force_close(
1061810629
&mut peer_state.closed_channel_monitor_update_ids,
1061910630
&mut peer_state.in_flight_monitor_updates,
1062010631
err,
@@ -10626,7 +10637,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1062610637
Some(Err(mut chan)) => {
1062710638
let err_msg = format!("Got an unexpected funding_created message from peer with counterparty_node_id {}", counterparty_node_id);
1062810639
let err = ChannelError::close(err_msg);
10629-
return Err(self.convert_channel_err(
10640+
return Err(self.locked_handle_force_close(
1063010641
&mut peer_state.closed_channel_monitor_update_ids,
1063110642
&mut peer_state.in_flight_monitor_updates,
1063210643
err,
@@ -10647,7 +10658,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1064710658
let err = ChannelError::close($err.to_owned());
1064810659
chan.unset_funding_info();
1064910660
let mut chan = Channel::from(chan);
10650-
return Err(self.convert_unfunded_channel_err_internal(err, &mut chan).1);
10661+
return Err(self.locked_handle_unfunded_close(err, &mut chan).1);
1065110662
}};
1065210663
}
1065310664

@@ -11267,7 +11278,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1126711278
let reason = ClosureReason::CounterpartyCoopClosedUnfundedChannel;
1126811279
let err = ChannelError::Close((reason.to_string(), reason));
1126911280
let mut chan = chan_entry.remove();
11270-
let (_, mut e) = self.convert_channel_err(
11281+
let (_, mut e) = self.locked_handle_force_close(
1127111282
&mut peer_state.closed_channel_monitor_update_ids,
1127211283
&mut peer_state.in_flight_monitor_updates,
1127311284
err,
@@ -11332,7 +11343,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1133211343
// also implies there are no pending HTLCs left on the channel, so we can
1133311344
// fully delete it from tracking (the channel monitor is still around to
1133411345
// watch for old state broadcasts)!
11335-
let err = self.convert_channel_err_coop(&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, close_res, chan);
11346+
let err = self.locked_handle_funded_coop_close(&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, close_res, chan);
1133611347
chan_entry.remove();
1133711348
Some((tx, Err(err)))
1133811349
} else {
@@ -12421,7 +12432,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1242112432
};
1242212433
let err = ChannelError::Close((reason.to_string(), reason));
1242312434
let mut chan = chan_entry.remove();
12424-
let (_, e) = self.convert_channel_err(
12435+
let (_, e) = self.locked_handle_force_close(
1242512436
&mut peer_state.closed_channel_monitor_update_ids,
1242612437
&mut peer_state.in_flight_monitor_updates,
1242712438
err,
@@ -12442,7 +12453,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1244212453
let reason = ClosureReason::CommitmentTxConfirmed;
1244312454
let err = ChannelError::Close((reason.to_string(), reason));
1244412455
let mut chan = chan_entry.remove();
12445-
let (_, e) = self.convert_channel_err(
12456+
let (_, e) = self.locked_handle_force_close(
1244612457
&mut peer_state.closed_channel_monitor_update_ids,
1244712458
&mut peer_state.in_flight_monitor_updates,
1244812459
err,
@@ -12639,7 +12650,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1263912650
_ => match unblock_chan(chan, &mut peer_state.pending_msg_events) {
1264012651
Ok(shutdown_result) => shutdown_result,
1264112652
Err(err) => {
12642-
let (_, err) = self.convert_channel_err(
12653+
let (_, err) = self.locked_handle_force_close(
1264312654
&mut peer_state.closed_channel_monitor_update_ids,
1264412655
&mut peer_state.in_flight_monitor_updates,
1264512656
err,
@@ -12655,7 +12666,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1265512666
let logger = WithChannelContext::from(&self.logger, context, None);
1265612667
log_trace!(logger, "Removing channel now that the signer is unblocked");
1265712668
let (remove, err) = if let Some(funded) = chan.as_funded_mut() {
12658-
let err = self.convert_channel_err_coop(
12669+
let err = self.locked_handle_funded_coop_close(
1265912670
&mut peer_state.closed_channel_monitor_update_ids,
1266012671
&mut peer_state.in_flight_monitor_updates,
1266112672
shutdown,
@@ -12666,7 +12677,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1266612677
debug_assert!(false);
1266712678
let reason = shutdown.closure_reason.clone();
1266812679
let err = ChannelError::Close((reason.to_string(), reason));
12669-
self.convert_unfunded_channel_err_internal(err, chan)
12680+
self.locked_handle_unfunded_close(err, chan)
1267012681
};
1267112682
debug_assert!(remove);
1267212683
shutdown_results.push((Err(err), *cp_id));
@@ -12725,7 +12736,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1272512736
if let Some((tx, shutdown_res)) = tx_shutdown_result_opt {
1272612737
// We're done with this channel. We got a closing_signed and sent back
1272712738
// a closing_signed with a closing transaction to broadcast.
12728-
let err = self.convert_channel_err_coop(
12739+
let err = self.locked_handle_funded_coop_close(
1272912740
&mut peer_state.closed_channel_monitor_update_ids,
1273012741
&mut peer_state.in_flight_monitor_updates,
1273112742
shutdown_res,
@@ -12742,12 +12753,13 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1274212753
},
1274312754
Err(e) => {
1274412755
has_update = true;
12745-
let (close_channel, res) = self.convert_channel_err_funded(
12746-
&mut peer_state.closed_channel_monitor_update_ids,
12747-
&mut peer_state.in_flight_monitor_updates,
12748-
e,
12749-
funded_chan,
12750-
);
12756+
let (close_channel, res) = self
12757+
.locked_handle_funded_force_close(
12758+
&mut peer_state.closed_channel_monitor_update_ids,
12759+
&mut peer_state.in_flight_monitor_updates,
12760+
e,
12761+
funded_chan,
12762+
);
1275112763
handle_errors.push((
1275212764
funded_chan.context.get_counterparty_node_id(),
1275312765
Err(res),
@@ -14117,7 +14129,7 @@ where
1411714129
// Clean up for removal.
1411814130
let reason = ClosureReason::DisconnectedPeer;
1411914131
let err = ChannelError::Close((reason.to_string(), reason));
14120-
let (_, e) = self.convert_channel_err(
14132+
let (_, e) = self.locked_handle_force_close(
1412114133
&mut peer_state.closed_channel_monitor_update_ids,
1412214134
&mut peer_state.in_flight_monitor_updates,
1412314135
err,
@@ -14874,7 +14886,7 @@ where
1487414886
// It looks like our counterparty went on-chain or funding transaction was
1487514887
// reorged out of the main chain. Close the channel.
1487614888
let err = ChannelError::Close((reason.to_string(), reason));
14877-
let (_, e) = self.convert_channel_err_funded(
14889+
let (_, e) = self.locked_handle_funded_force_close(
1487814890
&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates,
1487914891
err,
1488014892
funded_channel

0 commit comments

Comments
 (0)