]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Implement HandleError event with ErrorAction field
authorAntoine Riard <ariard@student.42.fr>
Wed, 1 Aug 2018 16:34:03 +0000 (16:34 +0000)
committerAntoine Riard <ariard@student.42.fr>
Sun, 12 Aug 2018 00:36:48 +0000 (00:36 +0000)
src/ln/channelmanager.rs
src/ln/peer_handler.rs
src/util/events.rs

index 29d260ba6886c8826ff4510f8215a3c097e27d2e..fe18560caf34b45f8b6ee33f3c078368be145d40 100644 (file)
@@ -785,11 +785,10 @@ impl ChannelManager {
                                                },
                                                Err(e) => {
                                                        mem::drop(channel_state);
-                                                       add_pending_event!(events::Event::DisconnectPeer {
+                                                       add_pending_event!(events::Event::HandleError {
                                                                node_id: chan.get_their_node_id(),
-                                                               msg: if let Some(msgs::ErrorAction::DisconnectPeer { msg } ) = e.action { msg } else { None },
+                                                               action: e.action,
                                                        });
-
                                                        return;
                                                },
                                        }
@@ -1004,6 +1003,7 @@ impl ChannelManager {
                                                }
 
                                                let mut pending_events = self.pending_events.lock().unwrap();
+                                               //TODO: replace by HandleError ? UpdateFailHTLC in handle_update_add_htlc need also to build a CommitmentSigned
                                                pending_events.push(events::Event::SendFailHTLC {
                                                        node_id,
                                                        msg: msg,
@@ -1157,12 +1157,10 @@ impl ChainListener for ChannelManager {
                                        });
                                        short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
                                } else if let Err(e) = chan_res {
-                                       if let Some(msgs::ErrorAction::DisconnectPeer { msg }) = e.action {
-                                               new_events.push(events::Event::DisconnectPeer {
-                                                       node_id: channel.get_their_node_id(),
-                                                       msg: msg
-                                               });
-                                       } else { unreachable!(); }
+                                       new_events.push(events::Event::HandleError {
+                                               node_id: channel.get_their_node_id(),
+                                               action: e.action,
+                                       });
                                        if channel.is_shutdown() {
                                                return false;
                                        }
index bf0052d0236cdb69a469b0fa53a456c3bde8c55c..45e39846acf3645ab7b2a5f43431b3023baa2114 100644 (file)
@@ -734,21 +734,45 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                }
                                                continue;
                                        },
-                                       Event::DisconnectPeer { ref node_id, ref msg } => {
-                                               if let Some(mut descriptor) = peers.node_id_to_descriptor.remove(node_id) {
-                                                       if let Some(mut peer) = peers.peers.remove(&descriptor) {
-                                                               if let Some(ref msg) = *msg {
+                                       Event::HandleError { ref node_id, ref action } => {
+                                               if let Some(ref action) = *action {
+                                                       match *action {
+                                                               msgs::ErrorAction::UpdateFailHTLC { ref msg } => {
+                                                                       let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                                       });
+                                                                       peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 131)));
+                                                                       Self::do_attempt_write_data(&mut descriptor, peer);
+
+                                                               },
+                                                               msgs::ErrorAction::DisconnectPeer { ref msg } => {
+                                                                       if let Some(mut descriptor) = peers.node_id_to_descriptor.remove(node_id) {
+                                                                               if let Some(mut peer) = peers.peers.remove(&descriptor) {
+                                                                                       if let Some(ref msg) = *msg {
+                                                                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
+                                                                                               // This isn't guaranteed to work, but if there is enough free
+                                                                                               // room in the send buffer, put the error message there...
+                                                                                               Self::do_attempt_write_data(&mut descriptor, &mut peer);
+                                                                                       }
+                                                                               }
+                                                                               descriptor.disconnect_socket();
+                                                                               self.message_handler.chan_handler.peer_disconnected(&node_id, false);
+                                                                       }
+                                                               },
+                                                               msgs::ErrorAction::IgnoreError => {
+                                                                       continue;
+                                                               },
+                                                               msgs::ErrorAction::SendErrorMessage { ref msg } => {
+                                                                       let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                                               //TODO: Do whatever we're gonna do for handling dropped messages
+                                                                       });
                                                                        peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
-                                                                       // This isn't guaranteed to work, but if there is enough free
-                                                                       // room in the send buffer, put the error message there...
-                                                                       Self::do_attempt_write_data(&mut descriptor, &mut peer);
-                                                               }
+                                                                       Self::do_attempt_write_data(&mut descriptor, peer);
+                                                               },
                                                        }
-                                                       descriptor.disconnect_socket();
-                                                       self.message_handler.chan_handler.peer_disconnected(&node_id, false);
                                                }
                                                continue;
-                                       },
+                                       }
                                }
 
                                upstream_events.push(event);
@@ -799,6 +823,7 @@ impl<Descriptor: SocketDescriptor> EventsProvider for PeerManager<Descriptor> {
 #[cfg(test)]
 mod tests {
        use ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor};
+       use ln::msgs;
        use util::events;
        use util::test_utils;
 
@@ -864,9 +889,9 @@ mod tests {
                let their_id = PublicKey::from_secret_key(&secp_ctx, &peers[1].our_node_secret).unwrap();
 
                let chan_handler = test_utils::TestChannelMessageHandler::new();
-               chan_handler.pending_events.lock().unwrap().push(events::Event::DisconnectPeer {
+               chan_handler.pending_events.lock().unwrap().push(events::Event::HandleError {
                        node_id: their_id,
-                       msg: None,
+                       action: Some(msgs::ErrorAction::DisconnectPeer { msg: None }),
                });
                assert_eq!(chan_handler.pending_events.lock().unwrap().len(), 1);
                peers[0].message_handler.chan_handler = Arc::new(chan_handler);
index e8330b7e29ba9877449bda22641f6faa726c737b..589c4be10e61b331e6443ef4514a66e6d754af80 100644 (file)
@@ -105,11 +105,11 @@ pub enum Event {
                msg: msgs::ChannelUpdate,
        },
 
-       // Events indicating the network loop should change the state of connection with peer:
-       /// Disconnect the given peer, possibly making an attempt to send an ErrorMessage first.
-       DisconnectPeer  {
+       //Error handling
+       /// Broadcast an error downstream to be handled
+       HandleError {
                node_id: PublicKey,
-               msg: Option<msgs::ErrorMessage>,
+               action: Option<msgs::ErrorAction>
        }
 }