Remove NetGraphMsgHandler serialization
authorGleb Naumenko <naumenko.gs@gmail.com>
Wed, 6 May 2020 22:34:37 +0000 (18:34 -0400)
committerGleb Naumenko <naumenko.gs@gmail.com>
Tue, 12 May 2020 13:27:11 +0000 (09:27 -0400)
lightning/src/ln/functional_test_utils.rs
lightning/src/routing/network_graph.rs

index 53f86c2795395a5710f0f08c8bd7ec675d317339..9475728d08c022ae45a02c8c06b8d8e9fba7f4c6 100644 (file)
@@ -7,7 +7,7 @@ use chain::keysinterface::KeysInterface;
 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure};
 use ln::channelmonitor::{ChannelMonitor, ManyChannelMonitor};
 use routing::router::{Route, get_route};
-use routing::network_graph::{NetGraphMsgHandler, NetGraphMsgHandlerReadArgs};
+use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
 use ln::features::InitFeatures;
 use ln::msgs;
 use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler};
@@ -18,7 +18,7 @@ use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsPro
 use util::errors::APIError;
 use util::logger::Logger;
 use util::config::UserConfig;
-use util::ser::{ReadableArgs, Writeable};
+use util::ser::{ReadableArgs, Writeable, Readable};
 
 use bitcoin::util::hash::BitcoinHash;
 use bitcoin::blockdata::block::BlockHeader;
@@ -36,7 +36,7 @@ use rand::{thread_rng,Rng};
 
 use std::cell::RefCell;
 use std::rc::Rc;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, Mutex, RwLock};
 use std::mem;
 use std::collections::HashMap;
 
@@ -102,11 +102,13 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
                        // Check that if we serialize the Router, we can deserialize it again.
                        {
                                let mut w = test_utils::TestVecWriter(Vec::new());
-                               self.net_graph_msg_handler.write(&mut w).unwrap();
-                               let net_graph_msg_handler = NetGraphMsgHandler::read(&mut ::std::io::Cursor::new(&w.0), NetGraphMsgHandlerReadArgs {
-                                       chain_monitor: Arc::clone(&self.chain_monitor) as Arc<chaininterface::ChainWatchInterface>,
-                                       logger: Arc::clone(&self.logger) as Arc<Logger>
-                               }).unwrap();
+                               let network_graph_ser = self.net_graph_msg_handler.network_graph.read().unwrap();
+                               network_graph_ser.write(&mut w).unwrap();
+                               let network_graph_deser = <NetworkGraph>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap();
+                               let net_graph_msg_handler = NetGraphMsgHandler::from_net_graph(
+                                  Arc::clone(&self.chain_monitor) as Arc<chaininterface::ChainWatchInterface>,
+                                  Arc::clone(&self.logger) as Arc<Logger>, RwLock::new(network_graph_deser)
+                               );
                                let mut chan_progress = 0;
                                loop {
                                        let orig_announcements = self.net_graph_msg_handler.get_next_channel_announcements(chan_progress, 255);
index f14ab42f79b8f2c3d5dd9a32a36be2de5a070d61..add57c25c145ac9155ecebb0fca734c750d1997f 100644 (file)
@@ -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<ChainWatchInterface>, logger: Arc<Logger>) -> 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<ChainWatchInterface>, logger: Arc<Logger>, network_graph: RwLock<NetworkGraph>) -> 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<Vec<NetAddress>> {
                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<W: Writer>(&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 <NetGraphMsgHandler>::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<ChainWatchInterface>,
-       /// The Logger for use in the ChannelManager and which may be used to log information during
-       /// deserialization.
-       pub logger: Arc<Logger>,
-}
-
-impl ReadableArgs<NetGraphMsgHandlerReadArgs> for NetGraphMsgHandler {
-       fn read<R: ::std::io::Read>(reader: &mut R, args: NetGraphMsgHandlerReadArgs) -> Result<NetGraphMsgHandler, DecodeError> {
-               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 {