Rewrite monitor_update_failed as it didn't capture all the options
authorMatt Corallo <git@bluematt.me>
Tue, 8 Jan 2019 04:10:51 +0000 (23:10 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 22 Jan 2019 19:34:18 +0000 (14:34 -0500)
Primarily this fixes the case where we receive an RAA which does
not require a response, allowing us to call monitor_update_failed
without generating pending messages.

src/ln/channel.rs
src/ln/channelmanager.rs
src/ln/functional_tests.rs

index 42d4caa36a84da209bb59b4cc5807493570ab1b3..77ea03994a810a06b7d00ecb8f23d496ef623a6a 100644 (file)
@@ -2187,23 +2187,15 @@ impl Channel {
        /// commitment update or a revoke_and_ack generation). The messages which were generated from
        /// that original call must *not* have been sent to the remote end, and must instead have been
        /// dropped. They will be regenerated when monitor_updating_restored is called.
-       pub fn monitor_update_failed(&mut self, order: RAACommitmentOrder, mut pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, raa_first_dropped_cs: bool) {
+       pub fn monitor_update_failed(&mut self, order: RAACommitmentOrder, resend_raa: bool, resend_commitment: bool, mut pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>) {
                assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
-               match order {
-                       RAACommitmentOrder::CommitmentFirst => {
-                               self.monitor_pending_revoke_and_ack = false;
-                               self.monitor_pending_commitment_signed = true;
-                       },
-                       RAACommitmentOrder::RevokeAndACKFirst => {
-                               self.monitor_pending_revoke_and_ack = true;
-                               self.monitor_pending_commitment_signed = raa_first_dropped_cs;
-                       },
-               }
+               self.monitor_pending_revoke_and_ack = resend_raa;
+               self.monitor_pending_commitment_signed = resend_commitment;
+               self.monitor_pending_order = Some(order);
                assert!(self.monitor_pending_forwards.is_empty());
                mem::swap(&mut pending_forwards, &mut self.monitor_pending_forwards);
                assert!(self.monitor_pending_failures.is_empty());
                mem::swap(&mut pending_fails, &mut self.monitor_pending_failures);
-               self.monitor_pending_order = Some(order);
                self.channel_state |= ChannelState::MonitorUpdateFailed as u32;
        }
 
index 3beebe1cfdd350ef78a0b632e4d43c22419f2c86..ba2bfefccebaad2f68d1d2fee27681eee6a21229 100644 (file)
@@ -439,17 +439,10 @@ macro_rules! try_chan_entry {
 }
 
 macro_rules! return_monitor_err {
-       ($self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path) => {
-               return_monitor_err!($self, $err, $channel_state, $entry, $action_type, Vec::new(), Vec::new())
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
+               return_monitor_err!($self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new())
        };
-       ($self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $raa_first_dropped_cs: expr) => {
-               if $action_type != RAACommitmentOrder::RevokeAndACKFirst { panic!("Bad return_monitor_err call!"); }
-               return_monitor_err!($self, $err, $channel_state, $entry, $action_type, Vec::new(), Vec::new(), $raa_first_dropped_cs)
-       };
-       ($self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $failed_forwards: expr, $failed_fails: expr) => {
-               return_monitor_err!($self, $err, $channel_state, $entry, $action_type, $failed_forwards, $failed_fails, false)
-       };
-       ($self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $failed_forwards: expr, $failed_fails: expr, $raa_first_dropped_cs: expr) => {
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
                match $err {
                        ChannelMonitorUpdateErr::PermanentFailure => {
                                let (channel_id, mut chan) = $entry.remove_entry();
@@ -468,7 +461,7 @@ macro_rules! return_monitor_err {
                                return Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()))
                        },
                        ChannelMonitorUpdateErr::TemporaryFailure => {
-                               $entry.get_mut().monitor_update_failed($action_type, $failed_forwards, $failed_fails, $raa_first_dropped_cs);
+                               $entry.get_mut().monitor_update_failed($action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
                                return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor"), *$entry.key()));
                        },
                }
@@ -477,7 +470,7 @@ macro_rules! return_monitor_err {
 
 // Does not break in case of TemporaryFailure!
 macro_rules! maybe_break_monitor_err {
-       ($self: expr, $err: expr, $channel_state: expr, $entry: expr, $action_type: path) => {
+       ($self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
                match $err {
                        ChannelMonitorUpdateErr::PermanentFailure => {
                                let (channel_id, mut chan) = $entry.remove_entry();
@@ -487,7 +480,7 @@ macro_rules! maybe_break_monitor_err {
                                break Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure", channel_id, chan.force_shutdown(), $self.get_channel_update(&chan).ok()))
                        },
                        ChannelMonitorUpdateErr::TemporaryFailure => {
-                               $entry.get_mut().monitor_update_failed($action_type, Vec::new(), Vec::new(), false);
+                               $entry.get_mut().monitor_update_failed($action_type, $resend_raa, $resend_commitment, Vec::new(), Vec::new());
                        },
                }
        }
@@ -1018,7 +1011,7 @@ impl ChannelManager {
                                } {
                                        Some((update_add, commitment_signed, chan_monitor)) => {
                                                if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                                       maybe_break_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst);
+                                                       maybe_break_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, true);
                                                        // Note that MonitorUpdateFailed here indicates (per function docs)
                                                        // that we will resent the commitment update once we unfree monitor
                                                        // updating, so we have to take special care that we don't return
@@ -1962,7 +1955,7 @@ impl ChannelManager {
                                let (revoke_and_ack, commitment_signed, closing_signed, chan_monitor) =
                                        try_chan_entry!(self, chan.get_mut().commitment_signed(&msg, &*self.fee_estimator), channel_state, chan);
                                if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                       return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, commitment_signed.is_some());
+                                       return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::RevokeAndACKFirst, true, commitment_signed.is_some());
                                        //TODO: Rebroadcast closing_signed if present on monitor update restoration
                                }
                                channel_state.pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK {
@@ -2040,7 +2033,7 @@ impl ChannelManager {
                                        let (commitment_update, pending_forwards, pending_failures, closing_signed, chan_monitor) =
                                                try_chan_entry!(self, chan.get_mut().revoke_and_ack(&msg, &*self.fee_estimator), channel_state, chan);
                                        if let Err(e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
-                                               return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, pending_forwards, pending_failures);
+                                               return_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, commitment_update.is_some(), pending_forwards, pending_failures);
                                        }
                                        if let Some(updates) = commitment_update {
                                                channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
@@ -2147,7 +2140,7 @@ impl ChannelManager {
                                                if commitment_update.is_none() {
                                                        order = RAACommitmentOrder::RevokeAndACKFirst;
                                                }
-                                               return_monitor_err!(self, e, channel_state, chan, order);
+                                               return_monitor_err!(self, e, channel_state, chan, order, revoke_and_ack.is_some(), commitment_update.is_some());
                                                //TODO: Resend the funding_locked if needed once we get the monitor running again
                                        }
                                }
index c98ef2e2e1471580cbb80f88b1540fceb2961636..e8b6873b34e6a6918ed63fe585f180bb590be6f0 100644 (file)
@@ -4552,6 +4552,49 @@ fn test_monitor_update_fail_cs() {
        claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
 }
 
+#[test]
+fn test_monitor_update_fail_no_rebroadcast() {
+       // Tests handling of a monitor update failure when no message rebroadcasting on
+       // test_restore_channel_monitor() is required. Backported from
+       // chanmon_fail_consistency fuzz tests.
+       let mut nodes = create_network(2);
+       create_announced_chan_between_nodes(&nodes, 0, 1);
+
+       let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
+       let (payment_preimage_1, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
+       nodes[0].node.send_payment(route, our_payment_hash).unwrap();
+       check_added_monitors!(nodes[0], 1);
+
+       let send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap();
+       let bs_raa = commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false, true, false, true);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure);
+       if let msgs::HandleError { err, action: Some(msgs::ErrorAction::IgnoreError) } = nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_raa).unwrap_err() {
+               assert_eq!(err, "Failed to update ChannelMonitor");
+       } else { panic!(); }
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+
+       *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(());
+       nodes[1].node.test_restore_channel_monitor();
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       check_added_monitors!(nodes[1], 1);
+       expect_pending_htlcs_forwardable!(nodes[1]);
+
+       let events = nodes[1].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 1);
+       match events[0] {
+               Event::PaymentReceived { payment_hash, .. } => {
+                       assert_eq!(payment_hash, our_payment_hash);
+               },
+               _ => panic!("Unexpected event"),
+       }
+
+       claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_1);
+}
+
 fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
        // Tests handling of a monitor update failure when processing an incoming RAA
        let mut nodes = create_network(3);