From 7ec98e6206f59330327b25d5460048579b47b9ce Mon Sep 17 00:00:00 2001 From: Gleb Naumenko Date: Wed, 6 May 2020 18:34:37 -0400 Subject: [PATCH] Remove NetGraphMsgHandler serialization --- lightning/src/ln/functional_test_utils.rs | 18 ++++--- lightning/src/routing/network_graph.rs | 64 ++++++----------------- 2 files changed, 25 insertions(+), 57 deletions(-) diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 53f86c27..9475728d 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -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, - logger: Arc::clone(&self.logger) as Arc - }).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 = ::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, + Arc::clone(&self.logger) as Arc, 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); 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 { -- 2.30.2