Merge pull request #223 from TheBlueMatt/2018-10-chanmanager-serialize
[rust-lightning] / src / ln / router.rs
index 4c3bcb98074fd8301941739c9352ce6bef593e05..9fe2cfb50012b3619d1b10ef0edcae65f0fddee9 100644 (file)
@@ -13,9 +13,9 @@ use bitcoin::blockdata::opcodes;
 
 use chain::chaininterface::{ChainError, ChainWatchInterface};
 use ln::channelmanager;
-use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
+use ln::msgs::{DecodeError,ErrorAction,HandleError,RoutingMessageHandler,NetAddress,GlobalFeatures};
 use ln::msgs;
-use util::ser::Writeable;
+use util::ser::{Writeable, Readable};
 use util::logger::Logger;
 
 use std::cmp;
@@ -47,6 +47,37 @@ pub struct Route {
        pub hops: Vec<RouteHop>,
 }
 
+impl Writeable for Route {
+       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+               (self.hops.len() as u8).write(writer)?;
+               for hop in self.hops.iter() {
+                       hop.pubkey.write(writer)?;
+                       hop.short_channel_id.write(writer)?;
+                       hop.fee_msat.write(writer)?;
+                       hop.cltv_expiry_delta.write(writer)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R: ::std::io::Read> Readable<R> for Route {
+       fn read(reader: &mut R) -> Result<Route, DecodeError> {
+               let hops_count: u8 = Readable::read(reader)?;
+               let mut hops = Vec::with_capacity(hops_count as usize);
+               for _ in 0..hops_count {
+                       hops.push(RouteHop {
+                               pubkey: Readable::read(reader)?,
+                               short_channel_id: Readable::read(reader)?,
+                               fee_msat: Readable::read(reader)?,
+                               cltv_expiry_delta: Readable::read(reader)?,
+                       });
+               }
+               Ok(Route {
+                       hops
+               })
+       }
+}
+
 struct DirectionalChannelInfo {
        src_node_id: PublicKey,
        last_update: u32,
@@ -349,18 +380,25 @@ impl RoutingMessageHandler for Router {
                        &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
                                let _ = self.handle_channel_update(msg);
                        },
-                       &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, is_permanent:_ } => {
-//XXX
+                       &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
                                let mut network = self.network_map.write().unwrap();
-                               if let Some(chan) = network.channels.remove(short_channel_id) {
-                                       Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
+                               if *is_permanent {
+                                       if let Some(chan) = network.channels.remove(short_channel_id) {
+                                               Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
+                                       }
+                               } else {
+                                       if let Some(chan) = network.channels.get_mut(short_channel_id) {
+                                               chan.one_to_two.enabled = false;
+                                               chan.two_to_one.enabled = false;
+                                       }
                                }
                        },
-                       &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, is_permanent:_ } => {
-//XXX
-                               //let mut network = self.network_map.write().unwrap();
-                               //TODO: check _blamed_upstream_node
-                               self.mark_node_bad(node_id, false);
+                       &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
+                               if *is_permanent {
+                                       //TODO: Wholly remove the node
+                               } else {
+                                       self.mark_node_bad(node_id, false);
+                               }
                        },
                }
        }