X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fdisk.rs;h=2c1834a6f0b7ac92caec7c712a659dd25786c718;hb=e87a9e18f6739988b93b6bc38c1ae178c0bdaeb8;hp=13ac5e7680e8d0aac87a664e25a4c600cd5f8f89;hpb=d9e9c01d33f291cf4846c0406a3e00c9ecbd543d;p=ldk-sample diff --git a/src/disk.rs b/src/disk.rs index 13ac5e7..2c1834a 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -1,20 +1,15 @@ use crate::cli; -use bitcoin::hashes::hex::FromHex; use bitcoin::secp256k1::key::PublicKey; -use bitcoin::{BlockHash, Txid}; -use lightning::chain::channelmonitor::ChannelMonitor; -use lightning::chain::keysinterface::{InMemorySigner, KeysManager}; -use lightning::chain::transaction::OutPoint; +use bitcoin::BlockHash; +use lightning::routing::network_graph::NetworkGraph; use lightning::util::logger::{Logger, Record}; -use lightning::util::ser::{ReadableArgs, Writer}; +use lightning::util::ser::{Readable, Writeable, Writer}; use std::collections::HashMap; use std::fs; use std::fs::File; -// use std::io::{BufRead, BufReader, Cursor, Write}; -use std::io::{BufRead, BufReader, Cursor}; +use std::io::{BufRead, BufReader, BufWriter}; use std::net::SocketAddr; use std::path::Path; -use std::sync::Arc; use time::OffsetDateTime; pub(crate) struct FilesystemLogger { @@ -73,57 +68,24 @@ pub(crate) fn read_channel_peer_data( Ok(peer_data) } -pub(crate) fn read_channelmonitors( - path: String, keys_manager: Arc, -) -> Result)>, std::io::Error> { - if !Path::new(&path).exists() { - return Ok(HashMap::new()); +pub(crate) fn persist_network(path: &Path, network_graph: &NetworkGraph) -> std::io::Result<()> { + let mut tmp_path = path.to_path_buf().into_os_string(); + tmp_path.push(".tmp"); + let file = fs::OpenOptions::new().write(true).create(true).open(&tmp_path)?; + let write_res = network_graph.write(&mut BufWriter::new(file)); + if let Err(e) = write_res.and_then(|_| fs::rename(&tmp_path, path)) { + let _ = fs::remove_file(&tmp_path); + Err(e) + } else { + Ok(()) } - let mut outpoint_to_channelmonitor = HashMap::new(); - for file_option in fs::read_dir(path).unwrap() { - let file = file_option.unwrap(); - let owned_file_name = file.file_name(); - let filename = owned_file_name.to_str(); - if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Invalid ChannelMonitor file name", - )); - } - - let txid = Txid::from_hex(filename.unwrap().split_at(64).0); - if txid.is_err() { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Invalid tx ID in filename", - )); - } - - let index = filename.unwrap().split_at(65).1.split('.').next().unwrap().parse(); - if index.is_err() { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Invalid tx index in filename", - )); - } - - let contents = fs::read(&file.path())?; +} - if let Ok((blockhash, channel_monitor)) = - <(BlockHash, ChannelMonitor)>::read( - &mut Cursor::new(&contents), - &*keys_manager, - ) { - outpoint_to_channelmonitor.insert( - OutPoint { txid: txid.unwrap(), index: index.unwrap() }, - (blockhash, channel_monitor), - ); - } else { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Failed to deserialize ChannelMonitor", - )); +pub(crate) fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph { + if let Ok(file) = File::open(path) { + if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file)) { + return graph; } } - Ok(outpoint_to_channelmonitor) + NetworkGraph::new(genesis_hash) }