From: Antoine Riard Date: Wed, 1 Aug 2018 16:34:03 +0000 (+0000) Subject: Implement HandleError event with ErrorAction field X-Git-Tag: v0.0.12~353^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=e7526112470409c921e08da91dee8eeeaf0b0563;p=rust-lightning Implement HandleError event with ErrorAction field --- diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 29d260ba6..fe18560ca 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -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; } diff --git a/src/ln/peer_handler.rs b/src/ln/peer_handler.rs index bf0052d02..45e39846a 100644 --- a/src/ln/peer_handler.rs +++ b/src/ln/peer_handler.rs @@ -734,21 +734,45 @@ impl PeerManager { } 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 EventsProvider for PeerManager { #[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); diff --git a/src/util/events.rs b/src/util/events.rs index e8330b7e2..589c4be10 100644 --- a/src/util/events.rs +++ b/src/util/events.rs @@ -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, + action: Option } }