Skip to content

Commit 7ec98e6

Browse files
committed
Remove NetGraphMsgHandler serialization
1 parent 8467223 commit 7ec98e6

File tree

2 files changed

+25
-57
lines changed

2 files changed

+25
-57
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use chain::keysinterface::KeysInterface;
77
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure};
88
use ln::channelmonitor::{ChannelMonitor, ManyChannelMonitor};
99
use routing::router::{Route, get_route};
10-
use routing::network_graph::{NetGraphMsgHandler, NetGraphMsgHandlerReadArgs};
10+
use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
1111
use ln::features::InitFeatures;
1212
use ln::msgs;
1313
use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler};
@@ -18,7 +18,7 @@ use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsPro
1818
use util::errors::APIError;
1919
use util::logger::Logger;
2020
use util::config::UserConfig;
21-
use util::ser::{ReadableArgs, Writeable};
21+
use util::ser::{ReadableArgs, Writeable, Readable};
2222

2323
use bitcoin::util::hash::BitcoinHash;
2424
use bitcoin::blockdata::block::BlockHeader;
@@ -36,7 +36,7 @@ use rand::{thread_rng,Rng};
3636

3737
use std::cell::RefCell;
3838
use std::rc::Rc;
39-
use std::sync::{Arc, Mutex};
39+
use std::sync::{Arc, Mutex, RwLock};
4040
use std::mem;
4141
use std::collections::HashMap;
4242

@@ -102,11 +102,13 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
102102
// Check that if we serialize the Router, we can deserialize it again.
103103
{
104104
let mut w = test_utils::TestVecWriter(Vec::new());
105-
self.net_graph_msg_handler.write(&mut w).unwrap();
106-
let net_graph_msg_handler = NetGraphMsgHandler::read(&mut ::std::io::Cursor::new(&w.0), NetGraphMsgHandlerReadArgs {
107-
chain_monitor: Arc::clone(&self.chain_monitor) as Arc<chaininterface::ChainWatchInterface>,
108-
logger: Arc::clone(&self.logger) as Arc<Logger>
109-
}).unwrap();
105+
let network_graph_ser = self.net_graph_msg_handler.network_graph.read().unwrap();
106+
network_graph_ser.write(&mut w).unwrap();
107+
let network_graph_deser = <NetworkGraph>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap();
108+
let net_graph_msg_handler = NetGraphMsgHandler::from_net_graph(
109+
Arc::clone(&self.chain_monitor) as Arc<chaininterface::ChainWatchInterface>,
110+
Arc::clone(&self.logger) as Arc<Logger>, RwLock::new(network_graph_deser)
111+
);
110112
let mut chan_progress = 0;
111113
loop {
112114
let orig_announcements = self.net_graph_msg_handler.get_next_channel_announcements(chan_progress, 255);

lightning/src/routing/network_graph.rs

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use chain::chaininterface::{ChainError, ChainWatchInterface};
1313
use ln::features::{ChannelFeatures, NodeFeatures};
1414
use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress};
1515
use ln::msgs;
16-
use util::ser::{Writeable, Readable, Writer, ReadableArgs};
16+
use util::ser::{Writeable, Readable, Writer};
1717
use util::logger::Logger;
1818

1919
use std::cmp;
@@ -34,7 +34,8 @@ pub struct NetGraphMsgHandler {
3434
}
3535

3636
impl NetGraphMsgHandler {
37-
/// Creates a new tracker of the actual state of the network of channels and nodes.
37+
/// Creates a new tracker of the actual state of the network of channels and nodes,
38+
/// assuming fresh Network Graph
3839
pub fn new(chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Self {
3940
NetGraphMsgHandler {
4041
secp_ctx: Secp256k1::verification_only(),
@@ -48,6 +49,18 @@ impl NetGraphMsgHandler {
4849
}
4950
}
5051

52+
/// Creates a new tracker of the actual state of the network of channels and nodes,
53+
/// assuming an existing Network Graph.
54+
pub fn from_net_graph(chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>, network_graph: RwLock<NetworkGraph>) -> Self {
55+
NetGraphMsgHandler {
56+
secp_ctx: Secp256k1::verification_only(),
57+
network_graph: network_graph,
58+
full_syncs_requested: AtomicUsize::new(0),
59+
chain_monitor,
60+
logger: logger.clone(),
61+
}
62+
}
63+
5164
/// Get network addresses by node id
5265
pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
5366
let network = self.network_graph.read().unwrap();
@@ -198,53 +211,6 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
198211
}
199212
}
200213

201-
202-
const SERIALIZATION_VERSION: u8 = 1;
203-
const MIN_SERIALIZATION_VERSION: u8 = 1;
204-
205-
impl Writeable for NetGraphMsgHandler {
206-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
207-
writer.write_all(&[SERIALIZATION_VERSION; 1])?;
208-
writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
209-
210-
let network = self.network_graph.read().unwrap();
211-
network.write(writer)?;
212-
Ok(())
213-
}
214-
}
215-
216-
/// Arguments for the creation of a NetGraphMsgHandler that are not deserialized.
217-
/// At a high-level, the process for deserializing a NetGraphMsgHandler and resuming normal operation is:
218-
/// 1) Deserialize the NetGraphMsgHandler by filling in this struct and calling <NetGraphMsgHandler>::read(reaser, args).
219-
/// 2) Register the new NetGraphMsgHandler with your ChainWatchInterface
220-
pub struct NetGraphMsgHandlerReadArgs {
221-
/// The ChainWatchInterface for use in the NetGraphMsgHandler in the future.
222-
///
223-
/// No calls to the ChainWatchInterface will be made during deserialization.
224-
pub chain_monitor: Arc<ChainWatchInterface>,
225-
/// The Logger for use in the ChannelManager and which may be used to log information during
226-
/// deserialization.
227-
pub logger: Arc<Logger>,
228-
}
229-
230-
impl ReadableArgs<NetGraphMsgHandlerReadArgs> for NetGraphMsgHandler {
231-
fn read<R: ::std::io::Read>(reader: &mut R, args: NetGraphMsgHandlerReadArgs) -> Result<NetGraphMsgHandler, DecodeError> {
232-
let _ver: u8 = Readable::read(reader)?;
233-
let min_ver: u8 = Readable::read(reader)?;
234-
if min_ver > SERIALIZATION_VERSION {
235-
return Err(DecodeError::UnknownVersion);
236-
}
237-
let network_graph = Readable::read(reader)?;
238-
Ok(NetGraphMsgHandler {
239-
secp_ctx: Secp256k1::verification_only(),
240-
network_graph: RwLock::new(network_graph),
241-
chain_monitor: args.chain_monitor,
242-
full_syncs_requested: AtomicUsize::new(0),
243-
logger: args.logger.clone(),
244-
})
245-
}
246-
}
247-
248214
#[derive(PartialEq, Debug)]
249215
/// Details regarding one direction of a channel
250216
pub struct DirectionalChannelInfo {

0 commit comments

Comments
 (0)