Skip to content

Commit a4a5d74

Browse files
authored
Merge pull request #14 from TheBlueMatt/main
Persist network graph
2 parents a5ff337 + 6200c11 commit a4a5d74

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/disk.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::cli;
22
use bitcoin::secp256k1::key::PublicKey;
3+
use bitcoin::BlockHash;
4+
use lightning::routing::network_graph::NetworkGraph;
35
use lightning::util::logger::{Logger, Record};
4-
use lightning::util::ser::Writer;
6+
use lightning::util::ser::{Readable, Writeable, Writer};
57
use std::collections::HashMap;
68
use std::fs;
79
use std::fs::File;
8-
use std::io::{BufRead, BufReader};
10+
use std::io::{BufRead, BufReader, BufWriter};
911
use std::net::SocketAddr;
1012
use std::path::Path;
1113
use time::OffsetDateTime;
@@ -65,3 +67,25 @@ pub(crate) fn read_channel_peer_data(
6567
}
6668
Ok(peer_data)
6769
}
70+
71+
pub(crate) fn persist_network(path: &Path, network_graph: &NetworkGraph) -> std::io::Result<()> {
72+
let mut tmp_path = path.to_path_buf().into_os_string();
73+
tmp_path.push(".tmp");
74+
let file = fs::OpenOptions::new().write(true).create(true).open(&tmp_path)?;
75+
let write_res = network_graph.write(&mut BufWriter::new(file));
76+
if let Err(e) = write_res.and_then(|_| fs::rename(&tmp_path, path)) {
77+
let _ = fs::remove_file(&tmp_path);
78+
Err(e)
79+
} else {
80+
Ok(())
81+
}
82+
}
83+
84+
pub(crate) fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph {
85+
if let Ok(file) = File::open(path) {
86+
if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file)) {
87+
return graph;
88+
}
89+
}
90+
NetworkGraph::new(genesis_hash)
91+
}

src/main.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,33 @@ async fn start_ldk() {
426426
}
427427

428428
// Step 11: Optional: Initialize the NetGraphMsgHandler
429-
// XXX persist routing data
430429
let genesis = genesis_block(args.network).header.block_hash();
431-
let router = Arc::new(NetGraphMsgHandler::new(
432-
genesis,
430+
let network_graph_path = format!("{}/network_graph", ldk_data_dir.clone());
431+
let network_graph = disk::read_network(Path::new(&network_graph_path), genesis);
432+
let router = Arc::new(NetGraphMsgHandler::from_net_graph(
433433
None::<Arc<dyn chain::Access + Send + Sync>>,
434434
logger.clone(),
435+
network_graph,
435436
));
437+
let router_persist = Arc::clone(&router);
438+
tokio::spawn(async move {
439+
let mut interval = tokio::time::interval(Duration::from_secs(600));
440+
loop {
441+
interval.tick().await;
442+
if disk::persist_network(
443+
Path::new(&network_graph_path),
444+
&*router_persist.network_graph.read().unwrap(),
445+
)
446+
.is_err()
447+
{
448+
// Persistence errors here are non-fatal as we can just fetch the routing graph
449+
// again later, but they may indicate a disk error which could be fatal elsewhere.
450+
eprintln!(
451+
"Warning: Failed to persist network graph, check your disk and permissions"
452+
);
453+
}
454+
}
455+
});
436456

437457
// Step 12: Initialize the PeerManager
438458
let channel_manager: Arc<ChannelManager> = Arc::new(channel_manager);

0 commit comments

Comments
 (0)