]> git.bitcoin.ninja Git - rust-lightning/commitdiff
impl Readable/Writable for Route
authorMatt Corallo <git@bluematt.me>
Fri, 19 Oct 2018 01:34:41 +0000 (21:34 -0400)
committerMatt Corallo <git@bluematt.me>
Sat, 27 Oct 2018 13:42:04 +0000 (09:42 -0400)
src/ln/router.rs

index 0d049f5a78d6bfcb68bd41a49ab06602e98069c3..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,