What changed, and why it matters
This commit is purely a code-formatting cleanup. It uses rustfmt to rewrap long function signatures, break up long macro invocations, and adjust indentation in a single Rust source file. No logic, behavior, or security properties of the code were changed.
No security action needed. Treat as a normal formatting commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff shows only whitespace and line-break changes produced by rustfmt in lightning/src/ln/channelmanager.rs. Function signatures, macro calls, struct literals, closures, and control-flow expressions were reformatted to fit style guidelines. No tokens were added, removed, or reordered in a way that changes semantics. No bug fixes, validation changes, or cryptographic modifications are present.
Changed components
lightning/src/ln/channelmanager.rsInspect captured patch +353 / −204
diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs
index c78a469..4a2ebf7 100644
--- a/lightning/src/ln/channelmanager.rs
+++ b/lightning/src/ln/channelmanager.rs
@@ -4364,7 +4364,11 @@ where
.collect()
}
- fn close_channel_internal(&self, chan_id: &ChannelId, counterparty_node_id: &PublicKey, target_feerate_sats_per_1000_weight: Option<u32>, override_shutdown_script: Option<ShutdownScript>) -> Result<(), APIError> {
+ fn close_channel_internal(
+ &self, chan_id: &ChannelId, counterparty_node_id: &PublicKey,
+ target_feerate_sats_per_1000_weight: Option<u32>,
+ override_shutdown_script: Option<ShutdownScript>,
+ ) -> Result<(), APIError> {
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)> = Vec::new();
@@ -4390,8 +4394,12 @@ where
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
let funding_txo_opt = chan.funding.get_funding_txo();
let their_features = &peer_state.latest_features;
- let (shutdown_msg, mut monitor_update_opt, htlcs) =
- chan.get_shutdown(&self.signer_provider, their_features, target_feerate_sats_per_1000_weight, override_shutdown_script)?;
+ let (shutdown_msg, mut monitor_update_opt, htlcs) = chan.get_shutdown(
+ &self.signer_provider,
+ their_features,
+ target_feerate_sats_per_1000_weight,
+ override_shutdown_script,
+ )?;
failed_htlcs = htlcs;
// We can send the `shutdown` message before updating the `ChannelMonitor`
@@ -4402,13 +4410,22 @@ where
msg: shutdown_msg,
});
- debug_assert!(monitor_update_opt.is_none() || !chan.is_shutdown(),
- "We can't both complete shutdown and generate a monitor update");
+ debug_assert!(
+ monitor_update_opt.is_none() || !chan.is_shutdown(),
+ "We can't both complete shutdown and generate a monitor update"
+ );
// Update the monitor with the shutdown script if necessary.
if let Some(monitor_update) = monitor_update_opt.take() {
- handle_new_monitor_update!(self, funding_txo_opt.unwrap(), monitor_update,
- peer_state_lock, peer_state, per_peer_state, chan);
+ handle_new_monitor_update!(
+ self,
+ funding_txo_opt.unwrap(),
+ monitor_update,
+ peer_state_lock,
+ peer_state,
+ per_peer_state,
+ chan
+ );
}
} else {
let reason = ClosureReason::LocallyCoopClosedUnfundedChannel;
@@ -4430,7 +4447,7 @@ where
err: format!(
"Channel with id {} not found for the passed counterparty node_id {}",
chan_id, counterparty_node_id,
- )
+ ),
});
},
}
@@ -4439,7 +4456,10 @@ where
for htlc_source in failed_htlcs.drain(..) {
let failure_reason = LocalHTLCFailureReason::ChannelClosed;
let reason = HTLCFailReason::from_failure_code(failure_reason);
- let receiver = HTLCHandlingFailureType::Forward { node_id: Some(*counterparty_node_id), channel_id: *chan_id };
+ let receiver = HTLCHandlingFailureType::Forward {
+ node_id: Some(*counterparty_node_id),
+ channel_id: *chan_id,
+ };
let (source, hash) = htlc_source;
self.fail_htlc_backwards_internal(&source, &hash, &reason, receiver, None);
}
@@ -4565,21 +4585,36 @@ where
}
let logger = WithContext::from(
- &self.logger, Some(shutdown_res.counterparty_node_id), Some(shutdown_res.channel_id), None
+ &self.logger,
+ Some(shutdown_res.counterparty_node_id),
+ Some(shutdown_res.channel_id),
+ None,
);
- log_debug!(logger, "Finishing closure of channel due to {} with {} HTLCs to fail",
- shutdown_res.closure_reason, shutdown_res.dropped_outbound_htlcs.len());
+ log_debug!(
+ logger,
+ "Finishing closure of channel due to {} with {} HTLCs to fail",
+ shutdown_res.closure_reason,
+ shutdown_res.dropped_outbound_htlcs.len()
+ );
for htlc_source in shutdown_res.dropped_outbound_htlcs.drain(..) {
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
let failure_reason = LocalHTLCFailureReason::ChannelClosed;
let reason = HTLCFailReason::from_failure_code(failure_reason);
- let receiver = HTLCHandlingFailureType::Forward { node_id: Some(counterparty_node_id), channel_id };
+ let receiver = HTLCHandlingFailureType::Forward {
+ node_id: Some(counterparty_node_id),
+ channel_id,
+ };
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver, None);
}
if let Some((_, funding_txo, _channel_id, monitor_update)) = shutdown_res.monitor_update {
debug_assert!(false, "This should have been handled in `convert_channel_err`");
- self.apply_post_close_monitor_update(shutdown_res.counterparty_node_id, shutdown_res.channel_id, funding_txo, monitor_update);
+ self.apply_post_close_monitor_update(
+ shutdown_res.counterparty_node_id,
+ shutdown_res.channel_id,
+ funding_txo,
+ monitor_update,
+ );
}
if self.background_events_processed_since_startup.load(Ordering::Acquire) {
// If a `ChannelMonitorUpdate` was applied (i.e. any time we have a funding txo and are
@@ -4588,7 +4623,11 @@ where
// TODO: If we do the `in_flight_monitor_updates.is_empty()` check in
// `convert_channel_err` we can skip the locks here.
if shutdown_res.channel_funding_txo.is_some() {
- self.channel_monitor_updated(&shutdown_res.channel_id, None, &shutdown_res.counterparty_node_id);
+ self.channel_monitor_updated(
+ &shutdown_res.channel_id,
+ None,
+ &shutdown_res.counterparty_node_id,
+ );
}
}
let mut shutdown_results: Vec<(Result<Infallible, _>, _)> = Vec::new();
@@ -4613,7 +4652,8 @@ where
shutdown_results.push((Err(e), counterparty_node_id));
}
}
- has_uncompleted_channel = Some(has_uncompleted_channel.map_or(!state, |v| v || !state));
+ has_uncompleted_channel =
+ Some(has_uncompleted_channel.map_or(!state, |v| v || !state));
}
debug_assert!(
has_uncompleted_channel.unwrap_or(true),
@@ -4623,26 +4663,32 @@ where
{
let mut pending_events = self.pending_events.lock().unwrap();
- pending_events.push_back((events::Event::ChannelClosed {
- channel_id: shutdown_res.channel_id,
- user_channel_id: shutdown_res.user_channel_id,
- reason: shutdown_res.closure_reason,
- counterparty_node_id: Some(shutdown_res.counterparty_node_id),
- channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
- channel_funding_txo: shutdown_res.channel_funding_txo,
- last_local_balance_msat: Some(shutdown_res.last_local_balance_msat),
- }, None));
-
- if let Some(splice_funding_failed) = shutdown_res.splice_funding_failed.take() {
- pending_events.push_back((events::Event::SpliceFailed {
+ pending_events.push_back((
+ events::Event::ChannelClosed {
channel_id: shutdown_res.channel_id,
- counterparty_node_id: shutdown_res.counterparty_node_id,
user_channel_id: shutdown_res.user_channel_id,
- abandoned_funding_txo: splice_funding_failed.funding_txo,
- channel_type: splice_funding_failed.channel_type,
- contributed_inputs: splice_funding_failed.contributed_inputs,
- contributed_outputs: splice_funding_failed.contributed_outputs,
- }, None));
+ reason: shutdown_res.closure_reason,
+ counterparty_node_id: Some(shutdown_res.counterparty_node_id),
+ channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
+ channel_funding_txo: shutdown_res.channel_funding_txo,
+ last_local_balance_msat: Some(shutdown_res.last_local_balance_msat),
+ },
+ None,
+ ));
+
+ if let Some(splice_funding_failed) = shutdown_res.splice_funding_failed.take() {
+ pending_events.push_back((
+ events::Event::SpliceFailed {
+ channel_id: shutdown_res.channel_id,
+ counterparty_node_id: shutdown_res.counterparty_node_id,
+ user_channel_id: shutdown_res.user_channel_id,
+ abandoned_funding_txo: splice_funding_failed.funding_txo,
+ channel_type: splice_funding_failed.channel_type,
+ contributed_inputs: splice_funding_failed.contributed_inputs,
+ contributed_outputs: splice_funding_failed.contributed_outputs,
+ },
+ None,
+ ));
}
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
@@ -4652,11 +4698,15 @@ where
.expect("We had an unbroadcasted funding tx, so should also have had a funding outpoint"),
}
} else {
- FundingInfo::Tx{ transaction }
+ FundingInfo::Tx { transaction }
};
- pending_events.push_back((events::Event::DiscardFunding {
- channel_id: shutdown_res.channel_id, funding_info
- }, None));
+ pending_events.push_back((
+ events::Event::DiscardFunding {
+ channel_id: shutdown_res.channel_id,
+ funding_info,
+ },
+ None,
+ ));
}
}
for (err, counterparty_node_id) in shutdown_results.drain(..) {
@@ -4666,11 +4716,17 @@ where
/// `peer_msg` should be set when we receive a message from a peer, but not set when the
/// user closes, which will be re-exposed as the `ChannelClosed` reason.
- fn force_close_channel_with_peer(&self, channel_id: &ChannelId, peer_node_id: &PublicKey, reason: ClosureReason)
- -> Result<(), APIError> {
+ fn force_close_channel_with_peer(
+ &self, channel_id: &ChannelId, peer_node_id: &PublicKey, reason: ClosureReason,
+ ) -> Result<(), APIError> {
let per_peer_state = self.per_peer_state.read().unwrap();
- let peer_state_mutex = per_peer_state.get(peer_node_id)
- .ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", peer_node_id) })?;
+ let peer_state_mutex =
+ per_peer_state.get(peer_node_id).ok_or_else(|| APIError::ChannelUnavailable {
+ err: format!(
+ "Can't find a peer matching the passed counterparty node_id {}",
+ peer_node_id
+ ),
+ })?;
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
let peer_state = &mut *peer_state_lock;
let logger = WithContext::from(&self.logger, Some(*peer_node_id), Some(*channel_id), None);
@@ -4702,21 +4758,24 @@ where
} else if peer_state.inbound_channel_request_by_id.remove(channel_id).is_some() {
log_error!(logger, "Force-closing inbound channel request");
if !is_from_counterparty && peer_state.is_connected {
- peer_state.pending_msg_events.push(
- MessageSendEvent::HandleError {
- node_id: *peer_node_id,
- action: msgs::ErrorAction::SendErrorMessage {
- msg: msgs::ErrorMessage { channel_id: *channel_id, data: message }
- },
- }
- );
+ peer_state.pending_msg_events.push(MessageSendEvent::HandleError {
+ node_id: *peer_node_id,
+ action: msgs::ErrorAction::SendErrorMessage {
+ msg: msgs::ErrorMessage { channel_id: *channel_id, data: message },
+ },
+ });
}
// N.B. that we don't send any channel close event here: we
// don't have a user_channel_id, and we never sent any opening
// events anyway.
Ok(())
} else {
- Err(APIError::ChannelUnavailable{ err: format!("Channel with id {} not found for the passed counterparty node_id {}", channel_id, peer_node_id) })
+ Err(APIError::ChannelUnavailable {
+ err: format!(
+ "Channel with id {} not found for the passed counterparty node_id {}",
+ channel_id, peer_node_id
+ ),
+ })
}
}
@@ -6351,16 +6410,20 @@ where
self.batch_funding_transaction_generated_intern(temporary_channels, funding_type)
}
- fn batch_funding_transaction_generated_intern(&self, temporary_channels: &[(&ChannelId, &PublicKey)], funding: FundingType) -> Result<(), APIError> {
+ fn batch_funding_transaction_generated_intern(
+ &self, temporary_channels: &[(&ChannelId, &PublicKey)], funding: FundingType,
+ ) -> Result<(), APIError> {
let mut result = Ok(());
- if let FundingType::Checked(funding_transaction) |
- FundingType::CheckedManualBroadcast(funding_transaction) = &funding
+ if let FundingType::Checked(funding_transaction)
+ | FundingType::CheckedManualBroadcast(funding_transaction) = &funding
{
if !funding_transaction.is_coinbase() {
for inp in funding_transaction.input.iter() {
if inp.witness.is_empty() {
result = result.and(Err(APIError::APIMisuseError {
- err: "Funding transaction must be fully signed and spend Segwit outputs".to_owned()
+ err:
+ "Funding transaction must be fully signed and spend Segwit outputs"
+ .to_owned(),
}));
}
}
@@ -6368,7 +6431,8 @@ where
if funding_transaction.output.len() > u16::max_value() as usize {
result = result.and(Err(APIError::APIMisuseError {
- err: "Transaction had more than 2^16 outputs, which is not supported".to_owned()
+ err: "Transaction had more than 2^16 outputs, which is not supported"
+ .to_owned(),
}));
}
let height = self.best_block.read().unwrap().height;
@@ -6376,97 +6440,109 @@ where
// lower than the next block height. However, the modules constituting our Lightning
// node might not have perfect sync about their blockchain views. Thus, if the wallet
// module is ahead of LDK, only allow one more block of headroom.
- if !funding_transaction.input.iter().all(|input| input.sequence == Sequence::MAX) &&
- funding_transaction.lock_time.is_block_height() &&
- funding_transaction.lock_time.to_consensus_u32() > height + 1
+ if !funding_transaction.input.iter().all(|input| input.sequence == Sequence::MAX)
+ && funding_transaction.lock_time.is_block_height()
+ && funding_transaction.lock_time.to_consensus_u32() > height + 1
{
result = result.and(Err(APIError::APIMisuseError {
- err: "Funding transaction absolute timelock is non-final".to_owned()
+ err: "Funding transaction absolute timelock is non-final".to_owned(),
}));
}
}
let txid = funding.txid();
let is_batch_funding = temporary_channels.len() > 1;
- let mut funding_batch_states = if is_batch_funding {
- Some(self.funding_batch_states.lock().unwrap())
- } else {
- None
- };
- let mut funding_batch_state = funding_batch_states.as_mut().and_then(|states| {
- match states.entry(txid) {
- btree_map::Entry::Occupied(_) => {
- result = result.clone().and(Err(APIError::APIMisuseError {
- err: "Batch funding transaction with the same txid already exists".to_owned()
- }));
- None
- },
- btree_map::Entry::Vacant(vacant) => Some(vacant.insert(Vec::new())),
- }
+ let mut funding_batch_states =
+ if is_batch_funding { Some(self.funding_batch_states.lock().unwrap()) } else { None };
+ let mut funding_batch_state = funding_batch_states.as_mut().and_then(|states| match states
+ .entry(txid)
+ {
+ btree_map::Entry::Occupied(_) => {
+ result = result.clone().and(Err(APIError::APIMisuseError {
+ err: "Batch funding transaction with the same txid already exists".to_owned(),
+ }));
+ None
+ },
+ btree_map::Entry::Vacant(vacant) => Some(vacant.insert(Vec::new())),
});
let is_manual_broadcast = funding.is_manual_broadcast();
for &(temporary_channel_id, counterparty_node_id) in temporary_channels {
- result = result.and_then(|_| self.funding_transaction_generated_intern(
- *temporary_channel_id,
- *counterparty_node_id,
- funding.transaction_or_dummy(),
- is_batch_funding,
- |chan| {
- let mut output_index = None;
- let expected_spk = chan.funding.get_funding_redeemscript().to_p2wsh();
- let outpoint = match &funding {
- FundingType::Checked(tx) | FundingType::CheckedManualBroadcast(tx) => {
- for (idx, outp) in tx.output.iter().enumerate() {
- if outp.script_pubkey == expected_spk && outp.value.to_sat() == chan.funding.get_value_satoshis() {
- if output_index.is_some() {
- return Err("Multiple outputs matched the expected script and value");
+ result = result.and_then(|_| {
+ self.funding_transaction_generated_intern(
+ *temporary_channel_id,
+ *counterparty_node_id,
+ funding.transaction_or_dummy(),
+ is_batch_funding,
+ |chan| {
+ let mut output_index = None;
+ let expected_spk = chan.funding.get_funding_redeemscript().to_p2wsh();
+ let outpoint = match &funding {
+ FundingType::Checked(tx) | FundingType::CheckedManualBroadcast(tx) => {
+ for (idx, outp) in tx.output.iter().enumerate() {
+ if outp.script_pubkey == expected_spk
+ && outp.value.to_sat() == chan.funding.get_value_satoshis()
+ {
+ if output_index.is_some() {
+ return Err("Multiple outputs matched the expected script and value");
+ }
+ output_index = Some(idx as u16);
}
- output_index = Some(idx as u16);
}
- }
- if output_index.is_none() {
- return Err("No output matched the script_pubkey and value in the FundingGenerationReady event");
- }
- OutPoint { txid, index: output_index.unwrap() }
- },
- FundingType::Unchecked(outpoint) => outpoint.clone(),
- };
- if let Some(funding_batch_state) = funding_batch_state.as_mut() {
- // TODO(dual_funding): We only do batch funding for V1 channels at the moment, but we'll probably
- // need to fix this somehow to not rely on using the outpoint for the channel ID if we
- // want to support V2 batching here as well.
- funding_batch_state.push((ChannelId::v1_from_funding_outpoint(outpoint), *counterparty_node_id, false));
- }
- Ok(outpoint)
- },
- is_manual_broadcast)
- );
+ if output_index.is_none() {
+ return Err("No output matched the script_pubkey and value in the FundingGenerationReady event");
+ }
+ OutPoint { txid, index: output_index.unwrap() }
+ },
+ FundingType::Unchecked(outpoint) => outpoint.clone(),
+ };
+ if let Some(funding_batch_state) = funding_batch_state.as_mut() {
+ // TODO(dual_funding): We only do batch funding for V1 channels at the moment, but we'll probably
+ // need to fix this somehow to not rely on using the outpoint for the channel ID if we
+ // want to support V2 batching here as well.
+ funding_batch_state.push((
+ ChannelId::v1_from_funding_outpoint(outpoint),
+ *counterparty_node_id,
+ false,
+ ));
+ }
+ Ok(outpoint)
+ },
+ is_manual_broadcast,
+ )
+ });
}
if let Err(ref e) = result {
// Remaining channels need to be removed on any error.
let e = format!("Error in transaction funding: {:?}", e);
let mut channels_to_remove = Vec::new();
- channels_to_remove.extend(funding_batch_states.as_mut()
- .and_then(|states| states.remove(&txid))
- .into_iter().flatten()
- .map(|(chan_id, node_id, _state)| (chan_id, node_id))
- );
- channels_to_remove.extend(temporary_channels.iter()
- .map(|(&chan_id, &node_id)| (chan_id, node_id))
+ channels_to_remove.extend(
+ funding_batch_states
+ .as_mut()
+ .and_then(|states| states.remove(&txid))
+ .into_iter()
+ .flatten()
+ .map(|(chan_id, node_id, _state)| (chan_id, node_id)),
);
+ channels_to_remove
+ .extend(temporary_channels.iter().map(|(&chan_id, &node_id)| (chan_id, node_id)));
let mut shutdown_results: Vec<(Result<Infallible, _>, _)> = Vec::new();
{
let per_peer_state = self.per_peer_state.read().unwrap();
for (channel_id, counterparty_node_id) in channels_to_remove {
- per_peer_state.get(&counterparty_node_id)
+ per_peer_state
+ .get(&counterparty_node_id)
.map(|peer_state_mutex| peer_state_mutex.lock().unwrap())
- .and_then(|mut peer_state| peer_state.channel_by_id.remove(&channel_id).map(|chan| (chan, peer_state)))
+ .and_then(|mut peer_state| {
+ peer_state
+ .channel_by_id
+ .remove(&channel_id)
+ .map(|chan| (chan, peer_state))
+ })
.map(|(mut chan, mut peer_state_lock)| {
let reason = ClosureReason::ProcessingError { err: e.clone() };
let err = ChannelError::Close((e.clone(), reason));
let peer_state = &mut *peer_state_lock;
- let (_, e) =
- self.convert_channel_err(
+ let (_, e) = self.convert_channel_err(
&mut peer_state.closed_channel_monitor_update_ids,
&mut peer_state.in_flight_monitor_updates,
err,
@@ -10486,7 +10562,9 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
Ok(())
}
- fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
+ fn internal_funding_created(
+ &self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated,
+ ) -> Result<(), MsgHandleErrInternal> {
let best_block = *self.best_block.read().unwrap();
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -10536,16 +10614,18 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
let funded_channel_id = chan.context.channel_id();
- macro_rules! fail_chan { ($err: expr) => { {
- // Note that at this point we've filled in the funding outpoint on our channel, but its
- // actually in conflict with another channel. Thus, if we call `convert_channel_err`
- // immediately, we'll remove the existing channel from `outpoint_to_peer`.
- // Thus, we must first unset the funding outpoint on the channel.
- let err = ChannelError::close($err.to_owned());
- chan.unset_funding_info();
- let mut chan = Channel::from(chan);
- return Err(self.convert_unfunded_channel_err_internal(err, &mut chan).1);
- } } }
+ macro_rules! fail_chan {
+ ($err: expr) => {{
+ // Note that at this point we've filled in the funding outpoint on our channel, but its
+ // actually in conflict with another channel. Thus, if we call `convert_channel_err`
+ // immediately, we'll remove the existing channel from `outpoint_to_peer`.
+ // Thus, we must first unset the funding outpoint on the channel.
+ let err = ChannelError::close($err.to_owned());
+ chan.unset_funding_info();
+ let mut chan = Channel::from(chan);
+ return Err(self.convert_unfunded_channel_err_internal(err, &mut chan).1);
+ }};
+ }
match peer_state.channel_by_id.entry(funded_channel_id) {
hash_map::Entry::Occupied(_) => {
@@ -10566,8 +10646,14 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
}
if let Some(funded_chan) = e.insert(Channel::from(chan)).as_funded_mut() {
- handle_initial_monitor!(self, persist_state, peer_state_lock, peer_state,
- per_peer_state, funded_chan);
+ handle_initial_monitor!(
+ self,
+ persist_state,
+ peer_state_lock,
+ peer_state,
+ per_peer_state,
+ funded_chan
+ );
} else {
unreachable!("This must be a funded channel as we just inserted it.");
}
@@ -10577,7 +10663,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
log_error!(logger, "Persisting initial ChannelMonitor failed, implying the channel ID was duplicated");
fail_chan!("Duplicate channel ID");
}
- }
+ },
}
}
@@ -12442,48 +12528,46 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
// Returns whether we should remove this channel as it's just been closed.
- let unblock_chan = |chan: &mut Channel<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| -> Result<Option<ShutdownResult>, ChannelError> {
+ let unblock_chan = |chan: &mut Channel<SP>,
+ pending_msg_events: &mut Vec<MessageSendEvent>|
+ -> Result<Option<ShutdownResult>, ChannelError> {
let channel_id = chan.context().channel_id();
let outbound_scid_alias = chan.context().outbound_scid_alias();
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
let node_id = chan.context().get_counterparty_node_id();
- let cbp = |htlc_id| self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &channel_id, &node_id);
+ let cbp = |htlc_id| {
+ self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &channel_id, &node_id)
+ };
let msgs = chan.signer_maybe_unblocked(self.chain_hash, &&logger, cbp)?;
if let Some(msgs) = msgs {
if chan.context().is_connected() {
if let Some(msg) = msgs.open_channel {
- pending_msg_events.push(MessageSendEvent::SendOpenChannel {
- node_id,
- msg,
- });
+ pending_msg_events.push(MessageSendEvent::SendOpenChannel { node_id, msg });
}
if let Some(msg) = msgs.funding_created {
- pending_msg_events.push(MessageSendEvent::SendFundingCreated {
- node_id,
- msg,
- });
+ pending_msg_events
+ .push(MessageSendEvent::SendFundingCreated { node_id, msg });
}
if let Some(msg) = msgs.accept_channel {
- pending_msg_events.push(MessageSendEvent::SendAcceptChannel {
- node_id,
- msg,
- });
+ pending_msg_events
+ .push(MessageSendEvent::SendAcceptChannel { node_id, msg });
}
- let cu_msg = msgs.commitment_update.map(|updates| MessageSendEvent::UpdateHTLCs {
- node_id,
- channel_id,
- updates,
- });
- let raa_msg = msgs.revoke_and_ack.map(|msg| MessageSendEvent::SendRevokeAndACK {
- node_id,
- msg,
+ let cu_msg = msgs.commitment_update.map(|updates| {
+ MessageSendEvent::UpdateHTLCs { node_id, channel_id, updates }
});
+ let raa_msg = msgs
+ .revoke_and_ack
+ .map(|msg| MessageSendEvent::SendRevokeAndACK { node_id, msg });
match (cu_msg, raa_msg) {
- (Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::CommitmentFirst => {
+ (Some(cu), Some(raa))
+ if msgs.order == RAACommitmentOrder::CommitmentFirst =>
+ {
pending_msg_events.push(cu);
pending_msg_events.push(raa);
},
- (Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::RevokeAndACKFirst => {
+ (Some(cu), Some(raa))
+ if msgs.order == RAACommitmentOrder::RevokeAndACKFirst =>
+ {
pending_msg_events.push(raa);
pending_msg_events.push(cu);
},
@@ -12492,16 +12576,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
(_, _) => {},
}
if let Some(msg) = msgs.funding_signed {
- pending_msg_events.push(MessageSendEvent::SendFundingSigned {
- node_id,
- msg,
- });
+ pending_msg_events
+ .push(MessageSendEvent::SendFundingSigned { node_id, msg });
}
if let Some(msg) = msgs.closing_signed {
- pending_msg_events.push(MessageSendEvent::SendClosingSigned {
- node_id,
- msg,
- });
+ pending_msg_events
+ .push(MessageSendEvent::SendClosingSigned { node_id, msg });
}
}
if let Some(funded_chan) = chan.as_funded() {
@@ -12529,7 +12609,9 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
let per_peer_state_iter = per_peer_state.iter().filter(|(cp_id, _)| {
if let Some((counterparty_node_id, _)) = channel_opt {
**cp_id == counterparty_node_id
- } else { true }
+ } else {
+ true
+ }
});
for (cp_id, peer_state_mutex) in per_peer_state_iter {
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
@@ -12541,7 +12623,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
Ok(shutdown_result) => shutdown_result,
Err(err) => {
let (_, err) = self.convert_channel_err(
- &mut peer_state.closed_channel_monitor_update_ids,
+ &mut peer_state.closed_channel_monitor_update_ids,
&mut peer_state.in_flight_monitor_updates,
err,
chan,
@@ -12556,8 +12638,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
let logger = WithChannelContext::from(&self.logger, context, None);
log_trace!(logger, "Removing channel now that the signer is unblocked");
let (remove, err) = if let Some(funded) = chan.as_funded_mut() {
- let err =
- self.convert_channel_err_coop(&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, shutdown, funded);
+ let err = self.convert_channel_err_coop(
+ &mut peer_state.closed_channel_monitor_update_ids,
+ &mut peer_state.in_flight_monitor_updates,
+ shutdown,
+ funded,
+ );
(true, err)
} else {
debug_assert!(false);
@@ -12598,34 +12684,59 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
}
match chan.as_funded_mut() {
Some(funded_chan) => {
- let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
- match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
+ let logger =
+ WithChannelContext::from(&self.logger, &funded_chan.context, None);
+ match funded_chan
+ .maybe_propose_closing_signed(&self.fee_estimator, &&logger)
+ {
Ok((msg_opt, tx_shutdown_result_opt)) => {
if let Some(msg) = msg_opt {
has_update = true;
- pending_msg_events.push(MessageSendEvent::SendClosingSigned {
- node_id: funded_chan.context.get_counterparty_node_id(), msg,
- });
+ pending_msg_events.push(
+ MessageSendEvent::SendClosingSigned {
+ node_id: funded_chan
+ .context
+ .get_counterparty_node_id(),
+ msg,
+ },
+ );
}
- debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
+ debug_assert_eq!(
+ tx_shutdown_result_opt.is_some(),
+ funded_chan.is_shutdown()
+ );
if let Some((tx, shutdown_res)) = tx_shutdown_result_opt {
// We're done with this channel. We got a closing_signed and sent back
// a closing_signed with a closing transaction to broadcast.
- let err = self.convert_channel_err_coop(&mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, shutdown_res, funded_chan);
+ let err = self.convert_channel_err_coop(
+ &mut peer_state.closed_channel_monitor_update_ids,
+ &mut peer_state.in_flight_monitor_updates,
+ shutdown_res,
+ funded_chan,
+ );
handle_errors.push((*cp_id, Err(err)));
log_info!(logger, "Broadcasting {}", log_tx!(tx));
self.tx_broadcaster.broadcast_transactions(&[&tx]);
false
- } else { true }
+ } else {
+ true
+ }
},
Err(e) => {
has_update = true;
let (close_channel, res) = self.convert_channel_err_funded(
- &mut peer_state.closed_channel_monitor_update_ids, &mut peer_state.in_flight_monitor_updates, e, funded_chan);
- handle_errors.push((funded_chan.context.get_counterparty_node_id(), Err(res)));
+ &mut peer_state.closed_channel_monitor_update_ids,
+ &mut peer_state.in_flight_monitor_updates,
+ e,
+ funded_chan,
+ );
+ handle_errors.push((
+ funded_chan.context.get_counterparty_node_id(),
+ Err(res),
+ ));
!close_channel
- }
+ },
}
},
None => true, // Retain unfunded channels if present.
@@ -14573,8 +14684,20 @@ where
/// Calls a function which handles an on-chain event (blocks dis/connected, transactions
/// un/confirmed, etc) on each channel, handling any resulting errors or messages generated by
/// the function.
- fn do_chain_event<FN: Fn(&mut FundedChannel<SP>) -> Result<(Option<FundingConfirmedMessage>, Vec<(HTLCSource, PaymentHash)>, Option<msgs::AnnouncementSignatures>), ClosureReason>>
- (&self, height_opt: Option<u32>, f: FN) {
+ fn do_chain_event<
+ FN: Fn(
+ &mut FundedChannel<SP>,
+ ) -> Result<
+ (
+ Option<FundingConfirmedMessage>,
+ Vec<(HTLCSource, PaymentHash)>,
+ Option<msgs::AnnouncementSignatures>,
+ ),
+ ClosureReason,
+ >,
+ >(
+ &self, height_opt: Option<u32>, f: FN,
+ ) {
// Note that we MUST NOT end up calling methods on self.chain_monitor here - we're called
// during initialization prior to the chain_monitor being fully configured in some cases.
// See the docs for `ChannelManagerReadArgs` for more.
@@ -14754,22 +14877,34 @@ where
}
if let Some(height) = height_opt {
- self.claimable_payments.lock().unwrap().claimable_payments.retain(|payment_hash, payment| {
- payment.htlcs.retain(|htlc| {
- // If height is approaching the number of blocks we think it takes us to get
- // our commitment transaction confirmed before the HTLC expires, plus the
- // number of blocks we generally consider it to take to do a commitment update,
- // just give up on it and fail the HTLC.
- if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
- let reason = LocalHTLCFailureReason::PaymentClaimBuffer;
- timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
- HTLCFailReason::reason(reason, invalid_payment_err_data(htlc.value, height)),
- HTLCHandlingFailureType::Receive { payment_hash: payment_hash.clone() }));
- false
- } else { true }
- });
- !payment.htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
- });
+ self.claimable_payments.lock().unwrap().claimable_payments.retain(
+ |payment_hash, payment| {
+ payment.htlcs.retain(|htlc| {
+ // If height is approaching the number of blocks we think it takes us to get
+ // our commitment transaction confirmed before the HTLC expires, plus the
+ // number of blocks we generally consider it to take to do a commitment update,
+ // just give up on it and fail the HTLC.
+ if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
+ let reason = LocalHTLCFailureReason::PaymentClaimBuffer;
+ timed_out_htlcs.push((
+ HTLCSource::PreviousHopData(htlc.prev_hop.clone()),
+ payment_hash.clone(),
+ HTLCFailReason::reason(
+ reason,
+ invalid_payment_err_data(htlc.value, height),
+ ),
+ HTLCHandlingFailureType::Receive {
+ payment_hash: payment_hash.clone(),
+ },
+ ));
+ false
+ } else {
+ true
+ }
+ });
+ !payment.htlcs.is_empty() // Only retain this entry if htlcs has at least one entry.
+ },
+ );
let mut intercepted_htlcs = self.pending_intercepted_htlcs.lock().unwrap();
intercepted_htlcs.retain(|_, htlc| {
@@ -14779,15 +14914,29 @@ where
PendingHTLCRouting::Forward { short_channel_id, .. } => short_channel_id,
_ => unreachable!(),
};
- timed_out_htlcs.push((prev_hop_data, htlc.forward_info.payment_hash,
- HTLCFailReason::from_failure_code(LocalHTLCFailureReason::ForwardExpiryBuffer),
- HTLCHandlingFailureType::InvalidForward { requested_forward_scid }));
+ timed_out_htlcs.push((
+ prev_hop_data,
+ htlc.forward_info.payment_hash,
+ HTLCFailReason::from_failure_code(
+ LocalHTLCFailureReason::ForwardExpiryBuffer,
+ ),
+ HTLCHandlingFailureType::InvalidForward { requested_forward_scid },
+ ));
let logger = WithContext::from(
- &self.logger, None, Some(htlc.prev_channel_id), Some(htlc.forward_info.payment_hash)
+ &self.logger,
+ None,
+ Some(htlc.prev_channel_id),
+ Some(htlc.forward_info.payment_hash),
+ );
+ log_trace!(
+ logger,
+ "Timing out intercepted HTLC with requested forward scid {}",
+ requested_forward_scid
);
- log_trace!(logger, "Timing out intercepted HTLC with requested forward scid {}", requested_forward_scid);
false
- } else { true }
+ } else {
+ true
+ }
});
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.