X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=rust-lightning;a=blobdiff_plain;f=lightning%2Fsrc%2Frouting%2Fnetwork_graph.rs;h=add57c25c145ac9155ecebb0fca734c750d1997f;hp=f14ab42f79b8f2c3d5dd9a32a36be2de5a070d61;hb=7ec98e6206f59330327b25d5460048579b47b9ce;hpb=8467223a5d7ea744c2af8557ed2e6a7a20991bdd diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs index f14ab42f..add57c25 100644 --- a/lightning/src/routing/network_graph.rs +++ b/lightning/src/routing/network_graph.rs @@ -13,7 +13,7 @@ use chain::chaininterface::{ChainError, ChainWatchInterface}; use ln::features::{ChannelFeatures, NodeFeatures}; use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress}; use ln::msgs; -use util::ser::{Writeable, Readable, Writer, ReadableArgs}; +use util::ser::{Writeable, Readable, Writer}; use util::logger::Logger; use std::cmp; @@ -34,7 +34,8 @@ pub struct NetGraphMsgHandler { } impl NetGraphMsgHandler { - /// Creates a new tracker of the actual state of the network of channels and nodes. + /// Creates a new tracker of the actual state of the network of channels and nodes, + /// assuming fresh Network Graph pub fn new(chain_monitor: Arc, logger: Arc) -> Self { NetGraphMsgHandler { secp_ctx: Secp256k1::verification_only(), @@ -48,6 +49,18 @@ impl NetGraphMsgHandler { } } + /// Creates a new tracker of the actual state of the network of channels and nodes, + /// assuming an existing Network Graph. + pub fn from_net_graph(chain_monitor: Arc, logger: Arc, network_graph: RwLock) -> Self { + NetGraphMsgHandler { + secp_ctx: Secp256k1::verification_only(), + network_graph: network_graph, + full_syncs_requested: AtomicUsize::new(0), + chain_monitor, + logger: logger.clone(), + } + } + /// Get network addresses by node id pub fn get_addresses(&self, pubkey: &PublicKey) -> Option> { let network = self.network_graph.read().unwrap(); @@ -198,53 +211,6 @@ impl RoutingMessageHandler for NetGraphMsgHandler { } } - -const SERIALIZATION_VERSION: u8 = 1; -const MIN_SERIALIZATION_VERSION: u8 = 1; - -impl Writeable for NetGraphMsgHandler { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - writer.write_all(&[SERIALIZATION_VERSION; 1])?; - writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?; - - let network = self.network_graph.read().unwrap(); - network.write(writer)?; - Ok(()) - } -} - -/// Arguments for the creation of a NetGraphMsgHandler that are not deserialized. -/// At a high-level, the process for deserializing a NetGraphMsgHandler and resuming normal operation is: -/// 1) Deserialize the NetGraphMsgHandler by filling in this struct and calling ::read(reaser, args). -/// 2) Register the new NetGraphMsgHandler with your ChainWatchInterface -pub struct NetGraphMsgHandlerReadArgs { - /// The ChainWatchInterface for use in the NetGraphMsgHandler in the future. - /// - /// No calls to the ChainWatchInterface will be made during deserialization. - pub chain_monitor: Arc, - /// The Logger for use in the ChannelManager and which may be used to log information during - /// deserialization. - pub logger: Arc, -} - -impl ReadableArgs for NetGraphMsgHandler { - fn read(reader: &mut R, args: NetGraphMsgHandlerReadArgs) -> Result { - let _ver: u8 = Readable::read(reader)?; - let min_ver: u8 = Readable::read(reader)?; - if min_ver > SERIALIZATION_VERSION { - return Err(DecodeError::UnknownVersion); - } - let network_graph = Readable::read(reader)?; - Ok(NetGraphMsgHandler { - secp_ctx: Secp256k1::verification_only(), - network_graph: RwLock::new(network_graph), - chain_monitor: args.chain_monitor, - full_syncs_requested: AtomicUsize::new(0), - logger: args.logger.clone(), - }) - } -} - #[derive(PartialEq, Debug)] /// Details regarding one direction of a channel pub struct DirectionalChannelInfo {