Skip to content

Commit 14b02f2

Browse files
new possible solution
1 parent b8750c7 commit 14b02f2

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,32 @@ macro_rules! try_chan_entry {
609609
return Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok()))
610610
}
611611
}
612-
}
612+
};
613+
($self: ident, $res: expr, $channel_state: expr, $entry: expr, $optional: expr) => {
614+
match $res {
615+
Ok(res) => res,
616+
Err(ChannelError::Ignore(msg)) => {
617+
return ($optional, Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone())))
618+
},
619+
Err(ChannelError::Close(msg)) => {
620+
log_trace!($self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
621+
let (channel_id, mut chan) = $entry.remove_entry();
622+
if let Some(short_id) = chan.get_short_channel_id() {
623+
$channel_state.short_to_id.remove(&short_id);
624+
}
625+
return ($optional, Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, chan.force_shutdown(true), $self.get_channel_update(&chan).ok())))
626+
},
627+
Err(ChannelError::CloseDelayBroadcast(msg)) => {
628+
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($entry.key()[..]), msg);
629+
let (channel_id, mut chan) = $entry.remove_entry();
630+
if let Some(short_id) = chan.get_short_channel_id() {
631+
$channel_state.short_to_id.remove(&short_id);
632+
}
633+
let shutdown_res = chan.force_shutdown(false);
634+
return ($optional, Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, $self.get_channel_update(&chan).ok())))
635+
}
636+
}
637+
};
613638
}
614639

615640
macro_rules! handle_monitor_err {
@@ -668,6 +693,9 @@ macro_rules! return_monitor_err {
668693
};
669694
($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
670695
return handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
696+
};
697+
($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $extra_rv: expr) => {
698+
return ($extra_rv, handle_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails));
671699
}
672700
}
673701

@@ -1825,7 +1853,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
18251853
// Fail a list of HTLCs that were just freed from the holding cell. The HTLCs need to be
18261854
// failed backwards or, if they were one of our outgoing HTLCs, then their failure needs to
18271855
// be surfaced to the user.
1828-
fn fail_holding_cell_htlcs(&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: [u8; 32]) -> Result<(), MsgHandleErrInternal> {
1856+
fn fail_holding_cell_htlcs(&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: [u8; 32]) {
18291857
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
18301858
match htlc_src {
18311859
HTLCSource::PreviousHopData(HTLCPreviousHopData { .. }) => {
@@ -1858,7 +1886,6 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
18581886
},
18591887
};
18601888
}
1861-
Ok(())
18621889
}
18631890

18641891
/// Fails an HTLC backwards to the sender of it to us.
@@ -2708,40 +2735,24 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
27082735
}
27092736
}
27102737

2711-
fn internal_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), MsgHandleErrInternal> {
2712-
let (commitment_update, pending_forwards, mut pending_failures, closing_signed, monitor_update, htlcs_to_fail) = {
2738+
fn internal_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> (Vec<(HTLCSource, PaymentHash)>, Result<(), MsgHandleErrInternal>){
2739+
let (pending_forwards, mut pending_failures, short_channel_id, htlcs_to_fail) = {
27132740
let mut channel_state_lock = self.channel_state.lock().unwrap();
27142741
let channel_state = &mut *channel_state_lock;
27152742
match channel_state.by_id.entry(msg.channel_id) {
27162743
hash_map::Entry::Occupied(mut chan) => {
27172744
if chan.get().get_their_node_id() != *their_node_id {
2718-
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id));
2745+
return (Vec::new(), Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.channel_id)));
27192746
}
2720-
try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.fee_estimator, &self.logger), channel_state, chan)
2721-
},
2722-
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2723-
}
2724-
};
2725-
2726-
// Failing back holding cell HTLCs requires acquiring the channel state
2727-
// lock, hence why this processing can't be done above.
2728-
if let Err(e) = self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id) { return Err(e) };
2729-
2730-
// Updating the corresponding ChannelMonitor may result in returning early,
2731-
// hence why we couldn't do this processing when we originally acquired the
2732-
// lock above.
2733-
let short_channel_id = {
2734-
let mut channel_state_lock = self.channel_state.lock().unwrap();
2735-
let channel_state = &mut *channel_state_lock;
2736-
match channel_state.by_id.entry(msg.channel_id) {
2737-
hash_map::Entry::Occupied(mut chan) => {
27382747
let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
2748+
let (commitment_update, pending_forwards, pending_failures, closing_signed, monitor_update, htlcs_to_fail) =
2749+
try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &self.fee_estimator, &self.logger), channel_state, chan, Vec::new());
27392750
if let Err(e) = self.monitor.update_monitor(chan.get().get_funding_txo().unwrap(), monitor_update) {
27402751
if was_frozen_for_monitor {
27412752
assert!(commitment_update.is_none() && closing_signed.is_none() && pending_forwards.is_empty() && pending_failures.is_empty());
2742-
return Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA".to_owned()));
2753+
return (htlcs_to_fail, Err(MsgHandleErrInternal::ignore_no_close("Previous monitor update failure prevented responses to RAA".to_owned())));
27432754
} else {
2744-
return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures);
2755+
return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures, htlcs_to_fail);
27452756
}
27462757
}
27472758
if let Some(updates) = commitment_update {
@@ -2756,17 +2767,17 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
27562767
msg,
27572768
});
27582769
}
2759-
chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel")
2770+
(pending_forwards, pending_failures, chan.get().get_short_channel_id().expect("RAA should only work on a short-id-available channel"), htlcs_to_fail)
27602771
},
2761-
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
2772+
hash_map::Entry::Vacant(_) => return (Vec::new(), Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id)))
27622773
}
27632774
};
27642775
for failure in pending_failures.drain(..) {
27652776
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), failure.0, &failure.1, failure.2);
27662777
}
27672778
self.forward_htlcs(&mut [(short_channel_id, pending_forwards)]);
27682779

2769-
Ok(())
2780+
(htlcs_to_fail, Ok(()))
27702781
}
27712782

27722783
fn internal_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), MsgHandleErrInternal> {
@@ -3267,7 +3278,9 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
32673278

32683279
fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) {
32693280
let _ = self.total_consistency_lock.read().unwrap();
3270-
let _ = handle_error!(self, self.internal_revoke_and_ack(their_node_id, msg), *their_node_id);
3281+
let (htlcs_to_fail, res) = self.internal_revoke_and_ack(their_node_id, msg);
3282+
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id);
3283+
let _ = handle_error!(self, res, *their_node_id);
32713284
}
32723285

32733286
fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) {

0 commit comments

Comments
 (0)