Support responding to Ping messages in peer_handler
[rust-lightning] / src / ln / router.rs
index 2419106fdd844e3b2bd357c02bc88b63c500cabf..e4473fa41838d87072d87cb13747c24f984cc442 100644 (file)
@@ -3,7 +3,7 @@ use secp256k1::{Secp256k1,Message};
 
 use bitcoin::util::hash::Sha256dHash;
 
-use ln::msgs::{HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
+use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
 use ln::msgs;
 
 use std::cmp;
@@ -12,6 +12,7 @@ use std::collections::{HashMap,BinaryHeap};
 use std::collections::hash_map::Entry;
 
 /// A hop in a route
+#[derive(Clone)]
 pub struct RouteHop {
        pub pubkey: PublicKey,
        /// The channel that should be used from the previous hop to reach this node.
@@ -24,6 +25,7 @@ pub struct RouteHop {
 }
 
 /// A route from us through the network to a destination
+#[derive(Clone)]
 pub struct Route {
        /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
        /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
@@ -49,9 +51,9 @@ struct ChannelInfo {
 
 struct NodeInfo {
        #[cfg(feature = "non_bitcoin_chain_hash_routing")]
-    channels: Vec<(u64, Sha256dHash)>,
+       channels: Vec<(u64, Sha256dHash)>,
        #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
-    channels: Vec<u64>,
+       channels: Vec<u64>,
 
        lowest_inbound_channel_fee_base_msat: u32,
        lowest_inbound_channel_fee_proportional_millionths: u32,
@@ -120,10 +122,10 @@ impl RoutingMessageHandler for Router {
 
                let mut network = self.network_map.write().unwrap();
                match network.nodes.get_mut(&msg.contents.node_id) {
-                       None => Err(HandleError{err: "No existing channels for node_announcement", msg: None}),
+                       None => Err(HandleError{err: "No existing channels for node_announcement", msg: Some(ErrorAction::IgnoreError)}),
                        Some(node) => {
                                if node.last_update >= msg.contents.timestamp {
-                                       return Err(HandleError{err: "Update older than last processed update", msg: None});
+                                       return Err(HandleError{err: "Update older than last processed update", msg: Some(ErrorAction::IgnoreError)});
                                }
 
                                node.features = msg.contents.features.clone();
@@ -157,7 +159,7 @@ impl RoutingMessageHandler for Router {
                                //TODO: because asking the blockchain if short_channel_id is valid is only optional
                                //in the blockchain API, we need to handle it smartly here, though its unclear
                                //exactly how...
-                               return Err(HandleError{err: "Already have knowledge of channel", msg: None})
+                               return Err(HandleError{err: "Already have knowledge of channel", msg: Some(ErrorAction::IgnoreError)})
                        },
                        Entry::Vacant(entry) => {
                                entry.insert(ChannelInfo {
@@ -212,6 +214,18 @@ impl RoutingMessageHandler for Router {
                Ok(!msg.contents.features.supports_unknown_bits())
        }
 
+       fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
+               match update {
+                       &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
+                               let _ = self.handle_channel_update(msg);
+                       },
+                       &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id } => {
+                               let mut network = self.network_map.write().unwrap();
+                               network.channels.remove(short_channel_id);
+                       },
+               }
+       }
+
        fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<(), HandleError> {
                let mut network = self.network_map.write().unwrap();
                let dest_node_id;
@@ -219,12 +233,12 @@ impl RoutingMessageHandler for Router {
                let chan_was_enabled;
 
                match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
-                       None => return Err(HandleError{err: "Couldn't find channel for update", msg: None}),
+                       None => return Err(HandleError{err: "Couldn't find channel for update", msg: Some(ErrorAction::IgnoreError)}),
                        Some(channel) => {
                                macro_rules! maybe_update_channel_info {
                                        ( $target: expr) => {
                                                if $target.last_update >= msg.contents.timestamp {
-                                                       return Err(HandleError{err: "Update older than last processed update", msg: None});
+                                                       return Err(HandleError{err: "Update older than last processed update", msg: Some(ErrorAction::IgnoreError)});
                                                }
                                                chan_was_enabled = $target.enabled;
                                                $target.last_update = msg.contents.timestamp;