Generate docs with features for docs.rs
[rust-lightning] / lightning-persister / src / lib.rs
1 //! Utilities that handle persisting Rust-Lightning data to disk via standard filesystem APIs.
2
3 #![deny(broken_intra_doc_links)]
4 #![deny(missing_docs)]
5
6 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
7
8 #![cfg_attr(all(test, feature = "_bench_unstable"), feature(test))]
9 #[cfg(all(test, feature = "_bench_unstable"))] extern crate test;
10
11 mod util;
12
13 extern crate lightning;
14 extern crate bitcoin;
15 extern crate libc;
16
17 use bitcoin::hash_types::{BlockHash, Txid};
18 use bitcoin::hashes::hex::{FromHex, ToHex};
19 use crate::util::DiskWriteable;
20 use lightning::chain;
21 use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
22 use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate};
23 use lightning::chain::chainmonitor;
24 use lightning::chain::keysinterface::{Sign, KeysInterface};
25 use lightning::chain::transaction::OutPoint;
26 use lightning::ln::channelmanager::ChannelManager;
27 use lightning::util::logger::Logger;
28 use lightning::util::ser::{ReadableArgs, Writeable};
29 use std::fs;
30 use std::io::{Cursor, Error};
31 use std::ops::Deref;
32 use std::path::{Path, PathBuf};
33
34 /// FilesystemPersister persists channel data on disk, where each channel's
35 /// data is stored in a file named after its funding outpoint.
36 ///
37 /// Warning: this module does the best it can with calls to persist data, but it
38 /// can only guarantee that the data is passed to the drive. It is up to the
39 /// drive manufacturers to do the actual persistence properly, which they often
40 /// don't (especially on consumer-grade hardware). Therefore, it is up to the
41 /// user to validate their entire storage stack, to ensure the writes are
42 /// persistent.
43 /// Corollary: especially when dealing with larger amounts of money, it is best
44 /// practice to have multiple channel data backups and not rely only on one
45 /// FilesystemPersister.
46 pub struct FilesystemPersister {
47         path_to_channel_data: String,
48 }
49
50 impl<Signer: Sign> DiskWriteable for ChannelMonitor<Signer> {
51         fn write_to_file(&self, writer: &mut fs::File) -> Result<(), Error> {
52                 self.write(writer)
53         }
54 }
55
56 impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> DiskWriteable for ChannelManager<Signer, M, T, K, F, L>
57 where
58         M::Target: chain::Watch<Signer>,
59         T::Target: BroadcasterInterface,
60         K::Target: KeysInterface<Signer=Signer>,
61         F::Target: FeeEstimator,
62         L::Target: Logger,
63 {
64         fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error> {
65                 self.write(writer)
66         }
67 }
68
69 impl FilesystemPersister {
70         /// Initialize a new FilesystemPersister and set the path to the individual channels'
71         /// files.
72         pub fn new(path_to_channel_data: String) -> Self {
73                 return Self {
74                         path_to_channel_data,
75                 }
76         }
77
78         /// Get the directory which was provided when this persister was initialized.
79         pub fn get_data_dir(&self) -> String {
80                 self.path_to_channel_data.clone()
81         }
82
83         pub(crate) fn path_to_monitor_data(&self) -> PathBuf {
84                 let mut path = PathBuf::from(self.path_to_channel_data.clone());
85                 path.push("monitors");
86                 path
87         }
88
89         /// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
90         /// initialization, within a file called "manager".
91         pub fn persist_manager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
92                 data_dir: String,
93                 manager: &ChannelManager<Signer, M, T, K, F, L>
94         ) -> Result<(), std::io::Error>
95         where
96                 M::Target: chain::Watch<Signer>,
97                 T::Target: BroadcasterInterface,
98                 K::Target: KeysInterface<Signer=Signer>,
99                 F::Target: FeeEstimator,
100                 L::Target: Logger,
101         {
102                 let path = PathBuf::from(data_dir);
103                 util::write_to_file(path, "manager".to_string(), manager)
104         }
105
106         /// Read `ChannelMonitor`s from disk.
107         pub fn read_channelmonitors<Signer: Sign, K: Deref> (
108                 &self, keys_manager: K
109         ) -> Result<Vec<(BlockHash, ChannelMonitor<Signer>)>, std::io::Error>
110                 where K::Target: KeysInterface<Signer=Signer> + Sized,
111         {
112                 let path = self.path_to_monitor_data();
113                 if !Path::new(&path).exists() {
114                         return Ok(Vec::new());
115                 }
116                 let mut res = Vec::new();
117                 for file_option in fs::read_dir(path).unwrap() {
118                         let file = file_option.unwrap();
119                         let owned_file_name = file.file_name();
120                         let filename = owned_file_name.to_str();
121                         if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 {
122                                 return Err(std::io::Error::new(
123                                         std::io::ErrorKind::InvalidData,
124                                         "Invalid ChannelMonitor file name",
125                                 ));
126                         }
127
128                         let txid = Txid::from_hex(filename.unwrap().split_at(64).0);
129                         if txid.is_err() {
130                                 return Err(std::io::Error::new(
131                                         std::io::ErrorKind::InvalidData,
132                                         "Invalid tx ID in filename",
133                                 ));
134                         }
135
136                         let index = filename.unwrap().split_at(65).1.parse();
137                         if index.is_err() {
138                                 return Err(std::io::Error::new(
139                                         std::io::ErrorKind::InvalidData,
140                                         "Invalid tx index in filename",
141                                 ));
142                         }
143
144                         let contents = fs::read(&file.path())?;
145                         let mut buffer = Cursor::new(&contents);
146                         match <(BlockHash, ChannelMonitor<Signer>)>::read(&mut buffer, &*keys_manager) {
147                                 Ok((blockhash, channel_monitor)) => {
148                                         if channel_monitor.get_funding_txo().0.txid != txid.unwrap() || channel_monitor.get_funding_txo().0.index != index.unwrap() {
149                                                 return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "ChannelMonitor was stored in the wrong file"));
150                                         }
151                                         res.push((blockhash, channel_monitor));
152                                 }
153                                 Err(e) => return Err(std::io::Error::new(
154                                         std::io::ErrorKind::InvalidData,
155                                         format!("Failed to deserialize ChannelMonitor: {}", e),
156                                 ))
157                         }
158                 }
159                 Ok(res)
160         }
161 }
162
163 impl<ChannelSigner: Sign> chainmonitor::Persist<ChannelSigner> for FilesystemPersister {
164         // TODO: We really need a way for the persister to inform the user that its time to crash/shut
165         // down once these start returning failure.
166         // A PermanentFailure implies we need to shut down since we're force-closing channels without
167         // even broadcasting!
168
169         fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: chainmonitor::MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
170                 let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
171                 util::write_to_file(self.path_to_monitor_data(), filename, monitor)
172                         .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
173         }
174
175         fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: chainmonitor::MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
176                 let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
177                 util::write_to_file(self.path_to_monitor_data(), filename, monitor)
178                         .map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
179         }
180 }
181
182 #[cfg(test)]
183 mod tests {
184         extern crate lightning;
185         extern crate bitcoin;
186         use crate::FilesystemPersister;
187         use bitcoin::blockdata::block::{Block, BlockHeader};
188         use bitcoin::hashes::hex::FromHex;
189         use bitcoin::Txid;
190         use lightning::chain::ChannelMonitorUpdateErr;
191         use lightning::chain::chainmonitor::Persist;
192         use lightning::chain::transaction::OutPoint;
193         use lightning::{check_closed_broadcast, check_closed_event, check_added_monitors};
194         use lightning::ln::features::InitFeatures;
195         use lightning::ln::functional_test_utils::*;
196         use lightning::util::events::{ClosureReason, MessageSendEventsProvider};
197         use lightning::util::test_utils;
198         use std::fs;
199         #[cfg(target_os = "windows")]
200         use {
201                 lightning::get_event_msg,
202                 lightning::ln::msgs::ChannelMessageHandler,
203         };
204
205         impl Drop for FilesystemPersister {
206                 fn drop(&mut self) {
207                         // We test for invalid directory names, so it's OK if directory removal
208                         // fails.
209                         match fs::remove_dir_all(&self.path_to_channel_data) {
210                                 Err(e) => println!("Failed to remove test persister directory: {}", e),
211                                 _ => {}
212                         }
213                 }
214         }
215
216         // Integration-test the FilesystemPersister. Test relaying a few payments
217         // and check that the persisted data is updated the appropriate number of
218         // times.
219         #[test]
220         fn test_filesystem_persister() {
221                 // Create the nodes, giving them FilesystemPersisters for data persisters.
222                 let persister_0 = FilesystemPersister::new("test_filesystem_persister_0".to_string());
223                 let persister_1 = FilesystemPersister::new("test_filesystem_persister_1".to_string());
224                 let chanmon_cfgs = create_chanmon_cfgs(2);
225                 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
226                 let chain_mon_0 = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &persister_0, &node_cfgs[0].keys_manager);
227                 let chain_mon_1 = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[1].chain_source), &chanmon_cfgs[1].tx_broadcaster, &chanmon_cfgs[1].logger, &chanmon_cfgs[1].fee_estimator, &persister_1, &node_cfgs[1].keys_manager);
228                 node_cfgs[0].chain_monitor = chain_mon_0;
229                 node_cfgs[1].chain_monitor = chain_mon_1;
230                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
231                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
232
233                 // Check that the persisted channel data is empty before any channels are
234                 // open.
235                 let mut persisted_chan_data_0 = persister_0.read_channelmonitors(nodes[0].keys_manager).unwrap();
236                 assert_eq!(persisted_chan_data_0.len(), 0);
237                 let mut persisted_chan_data_1 = persister_1.read_channelmonitors(nodes[1].keys_manager).unwrap();
238                 assert_eq!(persisted_chan_data_1.len(), 0);
239
240                 // Helper to make sure the channel is on the expected update ID.
241                 macro_rules! check_persisted_data {
242                         ($expected_update_id: expr) => {
243                                 persisted_chan_data_0 = persister_0.read_channelmonitors(nodes[0].keys_manager).unwrap();
244                                 assert_eq!(persisted_chan_data_0.len(), 1);
245                                 for (_, mon) in persisted_chan_data_0.iter() {
246                                         assert_eq!(mon.get_latest_update_id(), $expected_update_id);
247                                 }
248                                 persisted_chan_data_1 = persister_1.read_channelmonitors(nodes[1].keys_manager).unwrap();
249                                 assert_eq!(persisted_chan_data_1.len(), 1);
250                                 for (_, mon) in persisted_chan_data_1.iter() {
251                                         assert_eq!(mon.get_latest_update_id(), $expected_update_id);
252                                 }
253                         }
254                 }
255
256                 // Create some initial channel and check that a channel was persisted.
257                 let _ = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
258                 check_persisted_data!(0);
259
260                 // Send a few payments and make sure the monitors are updated to the latest.
261                 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
262                 check_persisted_data!(5);
263                 send_payment(&nodes[1], &vec!(&nodes[0])[..], 4000000);
264                 check_persisted_data!(10);
265
266                 // Force close because cooperative close doesn't result in any persisted
267                 // updates.
268                 nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
269                 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed);
270                 check_closed_broadcast!(nodes[0], true);
271                 check_added_monitors!(nodes[0], 1);
272
273                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
274                 assert_eq!(node_txn.len(), 1);
275
276                 let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
277                 connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[0].clone(), node_txn[0].clone()]});
278                 check_closed_broadcast!(nodes[1], true);
279                 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
280                 check_added_monitors!(nodes[1], 1);
281
282                 // Make sure everything is persisted as expected after close.
283                 check_persisted_data!(11);
284         }
285
286         // Test that if the persister's path to channel data is read-only, writing a
287         // monitor to it results in the persister returning a PermanentFailure.
288         // Windows ignores the read-only flag for folders, so this test is Unix-only.
289         #[cfg(not(target_os = "windows"))]
290         #[test]
291         fn test_readonly_dir_perm_failure() {
292                 let persister = FilesystemPersister::new("test_readonly_dir_perm_failure".to_string());
293                 fs::create_dir_all(&persister.path_to_channel_data).unwrap();
294
295                 // Set up a dummy channel and force close. This will produce a monitor
296                 // that we can then use to test persistence.
297                 let chanmon_cfgs = create_chanmon_cfgs(2);
298                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
299                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
300                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
301                 let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
302                 nodes[1].node.force_close_channel(&chan.2).unwrap();
303                 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
304                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
305                 let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
306                 let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
307
308                 // Set the persister's directory to read-only, which should result in
309                 // returning a permanent failure when we then attempt to persist a
310                 // channel update.
311                 let path = &persister.path_to_channel_data;
312                 let mut perms = fs::metadata(path).unwrap().permissions();
313                 perms.set_readonly(true);
314                 fs::set_permissions(path, perms).unwrap();
315
316                 let test_txo = OutPoint {
317                         txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
318                         index: 0
319                 };
320                 match persister.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
321                         Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
322                         _ => panic!("unexpected result from persisting new channel")
323                 }
324
325                 nodes[1].node.get_and_clear_pending_msg_events();
326                 added_monitors.clear();
327         }
328
329         // Test that if a persister's directory name is invalid, monitor persistence
330         // will fail.
331         #[cfg(target_os = "windows")]
332         #[test]
333         fn test_fail_on_open() {
334                 // Set up a dummy channel and force close. This will produce a monitor
335                 // that we can then use to test persistence.
336                 let chanmon_cfgs = create_chanmon_cfgs(2);
337                 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
338                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
339                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
340                 let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
341                 nodes[1].node.force_close_channel(&chan.2).unwrap();
342                 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
343                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
344                 let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
345                 let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
346
347                 // Create the persister with an invalid directory name and test that the
348                 // channel fails to open because the directories fail to be created. There
349                 // don't seem to be invalid filename characters on Unix that Rust doesn't
350                 // handle, hence why the test is Windows-only.
351                 let persister = FilesystemPersister::new(":<>/".to_string());
352
353                 let test_txo = OutPoint {
354                         txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
355                         index: 0
356                 };
357                 match persister.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
358                         Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
359                         _ => panic!("unexpected result from persisting new channel")
360                 }
361
362                 nodes[1].node.get_and_clear_pending_msg_events();
363                 added_monitors.clear();
364         }
365 }
366
367 #[cfg(all(test, feature = "_bench_unstable"))]
368 pub mod bench {
369         use test::Bencher;
370
371         #[bench]
372         fn bench_sends(bench: &mut Bencher) {
373                 let persister_a = super::FilesystemPersister::new("bench_filesystem_persister_a".to_string());
374                 let persister_b = super::FilesystemPersister::new("bench_filesystem_persister_b".to_string());
375                 lightning::ln::channelmanager::bench::bench_two_sends(bench, persister_a, persister_b);
376         }
377 }