2 use bitcoin::hashes::hex::FromHex;
3 use bitcoin::secp256k1::key::PublicKey;
4 use bitcoin::{BlockHash, Txid};
5 use lightning::chain::channelmonitor::ChannelMonitor;
6 use lightning::chain::keysinterface::{InMemorySigner, KeysManager};
7 use lightning::chain::transaction::OutPoint;
8 use lightning::util::logger::{Logger, Record};
9 use lightning::util::ser::{ReadableArgs, Writer};
10 use std::collections::HashMap;
13 // use std::io::{BufRead, BufReader, Cursor, Write};
14 use std::io::{BufRead, BufReader, Cursor};
15 use std::net::SocketAddr;
18 use time::OffsetDateTime;
20 pub(crate) struct FilesystemLogger {
23 impl FilesystemLogger {
24 pub(crate) fn new(data_dir: String) -> Self {
25 let logs_path = format!("{}/logs", data_dir);
26 fs::create_dir_all(logs_path.clone()).unwrap();
27 Self { data_dir: logs_path }
30 impl Logger for FilesystemLogger {
31 fn log(&self, record: &Record) {
32 let raw_log = record.args.to_string();
34 "{} {:<5} [{}:{}] {}\n",
35 OffsetDateTime::now_utc().format("%F %T"),
36 record.level.to_string(),
41 let logs_file_path = format!("{}/logs.txt", self.data_dir.clone());
42 fs::OpenOptions::new()
47 .write_all(log.as_bytes())
51 pub(crate) fn persist_channel_peer(path: &Path, peer_info: &str) -> std::io::Result<()> {
52 let mut file = fs::OpenOptions::new().create(true).append(true).open(path)?;
53 file.write_all(format!("{}\n", peer_info).as_bytes())
56 pub(crate) fn read_channel_peer_data(
58 ) -> Result<HashMap<PublicKey, SocketAddr>, std::io::Error> {
59 let mut peer_data = HashMap::new();
60 if !Path::new(&path).exists() {
61 return Ok(HashMap::new());
63 let file = File::open(path)?;
64 let reader = BufReader::new(file);
65 for line in reader.lines() {
66 match cli::parse_peer_info(line.unwrap()) {
67 Ok((pubkey, socket_addr)) => {
68 peer_data.insert(pubkey, socket_addr);
70 Err(e) => return Err(e),
76 pub(crate) fn read_channelmonitors(
77 path: String, keys_manager: Arc<KeysManager>,
78 ) -> Result<HashMap<OutPoint, (BlockHash, ChannelMonitor<InMemorySigner>)>, std::io::Error> {
79 if !Path::new(&path).exists() {
80 return Ok(HashMap::new());
82 let mut outpoint_to_channelmonitor = HashMap::new();
83 for file_option in fs::read_dir(path).unwrap() {
84 let file = file_option.unwrap();
85 let owned_file_name = file.file_name();
86 let filename = owned_file_name.to_str();
87 if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 {
88 return Err(std::io::Error::new(
89 std::io::ErrorKind::Other,
90 "Invalid ChannelMonitor file name",
94 let txid = Txid::from_hex(filename.unwrap().split_at(64).0);
96 return Err(std::io::Error::new(
97 std::io::ErrorKind::Other,
98 "Invalid tx ID in filename",
102 let index = filename.unwrap().split_at(65).1.split('.').next().unwrap().parse();
104 return Err(std::io::Error::new(
105 std::io::ErrorKind::Other,
106 "Invalid tx index in filename",
110 let contents = fs::read(&file.path())?;
112 if let Ok((blockhash, channel_monitor)) =
113 <(BlockHash, ChannelMonitor<InMemorySigner>)>::read(
114 &mut Cursor::new(&contents),
117 outpoint_to_channelmonitor.insert(
118 OutPoint { txid: txid.unwrap(), index: index.unwrap() },
119 (blockhash, channel_monitor),
122 return Err(std::io::Error::new(
123 std::io::ErrorKind::Other,
124 "Failed to deserialize ChannelMonitor",
128 Ok(outpoint_to_channelmonitor)