|
1 | 1 | use crate::cli;
|
2 | 2 | use bitcoin::secp256k1::key::PublicKey;
|
| 3 | +use bitcoin::BlockHash; |
| 4 | +use lightning::routing::network_graph::NetworkGraph; |
3 | 5 | use lightning::util::logger::{Logger, Record};
|
4 |
| -use lightning::util::ser::Writer; |
| 6 | +use lightning::util::ser::{Readable, Writeable, Writer}; |
5 | 7 | use std::collections::HashMap;
|
6 | 8 | use std::fs;
|
7 | 9 | use std::fs::File;
|
8 |
| -use std::io::{BufRead, BufReader}; |
| 10 | +use std::io::{BufRead, BufReader, BufWriter}; |
9 | 11 | use std::net::SocketAddr;
|
10 | 12 | use std::path::Path;
|
11 | 13 | use time::OffsetDateTime;
|
@@ -65,3 +67,25 @@ pub(crate) fn read_channel_peer_data(
|
65 | 67 | }
|
66 | 68 | Ok(peer_data)
|
67 | 69 | }
|
| 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 | +} |
0 commit comments