Merge pull request #764 from lightning-signer/revoke-enforcement
[rust-lightning] / lightning-persister / src / lib.rs
1 extern crate lightning;
2 extern crate bitcoin;
3 extern crate libc;
4
5 use bitcoin::hashes::hex::ToHex;
6 use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr};
7 use lightning::chain::channelmonitor;
8 use lightning::chain::keysinterface::ChannelKeys;
9 use lightning::chain::transaction::OutPoint;
10 use lightning::util::ser::Writeable;
11 use std::fs;
12 use std::io::Error;
13 use std::path::{Path, PathBuf};
14
15 #[cfg(test)]
16 use {
17         lightning::chain::keysinterface::KeysInterface,
18         lightning::util::ser::ReadableArgs,
19         bitcoin::{BlockHash, Txid},
20         bitcoin::hashes::hex::FromHex,
21         std::collections::HashMap,
22         std::io::Cursor
23 };
24
25 #[cfg(not(target_os = "windows"))]
26 use std::os::unix::io::AsRawFd;
27
28 /// FilesystemPersister persists channel data on disk, where each channel's
29 /// data is stored in a file named after its funding outpoint.
30 ///
31 /// Warning: this module does the best it can with calls to persist data, but it
32 /// can only guarantee that the data is passed to the drive. It is up to the
33 /// drive manufacturers to do the actual persistence properly, which they often
34 /// don't (especially on consumer-grade hardware). Therefore, it is up to the
35 /// user to validate their entire storage stack, to ensure the writes are
36 /// persistent.
37 /// Corollary: especially when dealing with larger amounts of money, it is best
38 /// practice to have multiple channel data backups and not rely only on one
39 /// FilesystemPersister.
40 pub struct FilesystemPersister {
41         path_to_channel_data: String,
42 }
43
44 trait DiskWriteable {
45         fn write(&self, writer: &mut fs::File) -> Result<(), Error>;
46 }
47
48 impl<ChanSigner: ChannelKeys> DiskWriteable for ChannelMonitor<ChanSigner> {
49         fn write(&self, writer: &mut fs::File) -> Result<(), Error> {
50                 Writeable::write(self, writer)
51         }
52 }
53
54 impl FilesystemPersister {
55         /// Initialize a new FilesystemPersister and set the path to the individual channels'
56         /// files.
57         pub fn new(path_to_channel_data: String) -> Self {
58                 return Self {
59                         path_to_channel_data,
60                 }
61         }
62
63         fn get_full_filepath(&self, funding_txo: OutPoint) -> String {
64                 let mut path = PathBuf::from(&self.path_to_channel_data);
65                 path.push(format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index));
66                 path.to_str().unwrap().to_string()
67         }
68
69         // Utility to write a file to disk.
70         fn write_channel_data(&self, funding_txo: OutPoint, monitor: &dyn DiskWriteable) -> std::io::Result<()> {
71                 fs::create_dir_all(&self.path_to_channel_data)?;
72                 // Do a crazy dance with lots of fsync()s to be overly cautious here...
73                 // We never want to end up in a state where we've lost the old data, or end up using the
74                 // old data on power loss after we've returned.
75                 // The way to atomically write a file on Unix platforms is:
76                 // open(tmpname), write(tmpfile), fsync(tmpfile), close(tmpfile), rename(), fsync(dir)
77                 let filename = self.get_full_filepath(funding_txo);
78                 let tmp_filename = format!("{}.tmp", filename.clone());
79
80                 {
81                         // Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use
82                         // rust stdlib 1.36 or higher.
83                         let mut f = fs::File::create(&tmp_filename)?;
84                         monitor.write(&mut f)?;
85                         f.sync_all()?;
86                 }
87                 fs::rename(&tmp_filename, &filename)?;
88                 // Fsync the parent directory on Unix.
89                 #[cfg(not(target_os = "windows"))]
90                 {
91                         let path = Path::new(&filename).parent().unwrap();
92                         let dir_file = fs::OpenOptions::new().read(true).open(path)?;
93                         unsafe { libc::fsync(dir_file.as_raw_fd()); }
94                 }
95                 Ok(())
96         }
97
98         #[cfg(test)]
99         fn load_channel_data<Keys: KeysInterface>(&self, keys: &Keys) ->
100                 Result<HashMap<OutPoint, ChannelMonitor<Keys::ChanKeySigner>>, ChannelMonitorUpdateErr> {
101                 if let Err(_) = fs::create_dir_all(&self.path_to_channel_data) {
102                         return Err(ChannelMonitorUpdateErr::PermanentFailure);
103                 }
104                 let mut res = HashMap::new();
105                 for file_option in fs::read_dir(&self.path_to_channel_data).unwrap() {
106                         let file = file_option.unwrap();
107                         let owned_file_name = file.file_name();
108                         let filename = owned_file_name.to_str();
109                         if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 {
110                                 return Err(ChannelMonitorUpdateErr::PermanentFailure);
111                         }
112
113                         let txid = Txid::from_hex(filename.unwrap().split_at(64).0);
114                         if txid.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); }
115
116                         let index = filename.unwrap().split_at(65).1.split('.').next().unwrap().parse();
117                         if index.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); }
118
119                         let contents = fs::read(&file.path());
120                         if contents.is_err() { return Err(ChannelMonitorUpdateErr::PermanentFailure); }
121
122                         if let Ok((_, loaded_monitor)) =
123                                 <(BlockHash, ChannelMonitor<Keys::ChanKeySigner>)>::read(&mut Cursor::new(&contents.unwrap()), keys) {
124                                 res.insert(OutPoint { txid: txid.unwrap(), index: index.unwrap() }, loaded_monitor);
125                         } else {
126                                 return Err(ChannelMonitorUpdateErr::PermanentFailure);
127                         }
128                 }
129                 Ok(res)
130         }
131 }
132
133 impl<ChanSigner: ChannelKeys + Send + Sync> channelmonitor::Persist<ChanSigner> for FilesystemPersister {
134         fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
135                 self.write_channel_data(funding_txo, monitor)
136                   .map_err(|_| ChannelMonitorUpdateErr::PermanentFailure)
137         }
138
139         fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &ChannelMonitorUpdate, monitor: &ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
140                 self.write_channel_data(funding_txo, monitor)
141                   .map_err(|_| ChannelMonitorUpdateErr::PermanentFailure)
142         }
143 }
144
145 #[cfg(test)]
146 impl Drop for FilesystemPersister {
147         fn drop(&mut self) {
148                 // We test for invalid directory names, so it's OK if directory removal
149                 // fails.
150                 match fs::remove_dir_all(&self.path_to_channel_data) {
151                         Err(e) => println!("Failed to remove test persister directory: {}", e),
152                         _ => {}
153                 }
154         }
155 }
156
157 #[cfg(test)]
158 mod tests {
159         extern crate lightning;
160         extern crate bitcoin;
161         use crate::FilesystemPersister;
162         use bitcoin::blockdata::block::{Block, BlockHeader};
163         use bitcoin::hashes::hex::FromHex;
164         use bitcoin::Txid;
165         use DiskWriteable;
166         use Error;
167         use lightning::chain::channelmonitor::{Persist, ChannelMonitorUpdateErr};
168         use lightning::chain::transaction::OutPoint;
169         use lightning::{check_closed_broadcast, check_added_monitors};
170         use lightning::ln::features::InitFeatures;
171         use lightning::ln::functional_test_utils::*;
172         use lightning::ln::msgs::ErrorAction;
173         use lightning::util::events::{MessageSendEventsProvider, MessageSendEvent};
174         use lightning::util::ser::Writer;
175         use lightning::util::test_utils;
176         use std::fs;
177         use std::io;
178         #[cfg(target_os = "windows")]
179         use {
180                 lightning::get_event_msg,
181                 lightning::ln::msgs::ChannelMessageHandler,
182         };
183
184         struct TestWriteable{}
185         impl DiskWriteable for TestWriteable {
186                 fn write(&self, writer: &mut fs::File) -> Result<(), Error> {
187                         writer.write_all(&[42; 1])
188                 }
189         }
190
191         // Integration-test the FilesystemPersister. Test relaying a few payments
192         // and check that the persisted data is updated the appropriate number of
193         // times.
194         #[test]
195         fn test_filesystem_persister() {
196                 // Create the nodes, giving them FilesystemPersisters for data persisters.
197                 let persister_0 = FilesystemPersister::new("test_filesystem_persister_0".to_string());
198                 let persister_1 = FilesystemPersister::new("test_filesystem_persister_1".to_string());
199                 let chanmon_cfgs = create_chanmon_cfgs(2);
200                 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
201                 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);
202                 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);
203                 node_cfgs[0].chain_monitor = chain_mon_0;
204                 node_cfgs[1].chain_monitor = chain_mon_1;
205                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
206                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
207
208                 // Check that the persisted channel data is empty before any channels are
209                 // open.
210                 let mut persisted_chan_data_0 = persister_0.load_channel_data(nodes[0].keys_manager).unwrap();
211                 assert_eq!(persisted_chan_data_0.keys().len(), 0);
212                 let mut persisted_chan_data_1 = persister_1.load_channel_data(nodes[1].keys_manager).unwrap();
213                 assert_eq!(persisted_chan_data_1.keys().len(), 0);
214
215                 // Helper to make sure the channel is on the expected update ID.
216                 macro_rules! check_persisted_data {
217                         ($expected_update_id: expr) => {
218                                 persisted_chan_data_0 = persister_0.load_channel_data(nodes[0].keys_manager).unwrap();
219                                 assert_eq!(persisted_chan_data_0.keys().len(), 1);
220                                 for mon in persisted_chan_data_0.values() {
221                                         assert_eq!(mon.get_latest_update_id(), $expected_update_id);
222                                 }
223                                 persisted_chan_data_1 = persister_1.load_channel_data(nodes[1].keys_manager).unwrap();
224                                 assert_eq!(persisted_chan_data_1.keys().len(), 1);
225                                 for mon in persisted_chan_data_1.values() {
226                                         assert_eq!(mon.get_latest_update_id(), $expected_update_id);
227                                 }
228                         }
229                 }
230
231                 // Create some initial channel and check that a channel was persisted.
232                 let _ = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
233                 check_persisted_data!(0);
234
235                 // Send a few payments and make sure the monitors are updated to the latest.
236                 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000, 8_000_000);
237                 check_persisted_data!(5);
238                 send_payment(&nodes[1], &vec!(&nodes[0])[..], 4000000, 4_000_000);
239                 check_persisted_data!(10);
240
241                 // Force close because cooperative close doesn't result in any persisted
242                 // updates.
243                 nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
244                 check_closed_broadcast!(nodes[0], false);
245                 check_added_monitors!(nodes[0], 1);
246
247                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
248                 assert_eq!(node_txn.len(), 1);
249
250                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
251                 connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[0].clone(), node_txn[0].clone()]}, 1);
252                 check_closed_broadcast!(nodes[1], false);
253                 check_added_monitors!(nodes[1], 1);
254
255                 // Make sure everything is persisted as expected after close.
256                 check_persisted_data!(11);
257         }
258
259         // Test that if the persister's path to channel data is read-only, writing
260         // data to it fails. Windows ignores the read-only flag for folders, so this
261         // test is Unix-only.
262         #[cfg(not(target_os = "windows"))]
263         #[test]
264         fn test_readonly_dir() {
265                 let persister = FilesystemPersister::new("test_readonly_dir_persister".to_string());
266                 let test_writeable = TestWriteable{};
267                 let test_txo = OutPoint {
268                         txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
269                         index: 0
270                 };
271                 // Create the persister's directory and set it to read-only.
272                 let path = &persister.path_to_channel_data;
273                 fs::create_dir_all(path).unwrap();
274                 let mut perms = fs::metadata(path).unwrap().permissions();
275                 perms.set_readonly(true);
276                 fs::set_permissions(path, perms).unwrap();
277                 match persister.write_channel_data(test_txo, &test_writeable) {
278                         Err(e) => assert_eq!(e.kind(), io::ErrorKind::PermissionDenied),
279                         _ => panic!("Unexpected error message")
280                 }
281         }
282
283         // Test failure to rename in the process of atomically creating a channel
284         // monitor's file. We induce this failure by making the `tmp` file a
285         // directory.
286         // Explanation: given "from" = the file being renamed, "to" = the
287         // renamee that already exists: Windows should fail because it'll fail
288         // whenever "to" is a directory, and Unix should fail because if "from" is a
289         // file, then "to" is also required to be a file.
290         #[test]
291         fn test_rename_failure() {
292                 let persister = FilesystemPersister::new("test_rename_failure".to_string());
293                 let test_writeable = TestWriteable{};
294                 let txid_hex = "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be";
295                 let outp_idx = 0;
296                 let test_txo = OutPoint {
297                         txid: Txid::from_hex(txid_hex).unwrap(),
298                         index: outp_idx,
299                 };
300                 // Create the channel data file and make it a directory.
301                 let path = &persister.path_to_channel_data;
302                 fs::create_dir_all(format!("{}/{}_{}", path, txid_hex, outp_idx)).unwrap();
303                 match persister.write_channel_data(test_txo, &test_writeable) {
304                         Err(e) => {
305                                 #[cfg(not(target_os = "windows"))]
306                                 assert_eq!(e.kind(), io::ErrorKind::Other);
307                                 #[cfg(target_os = "windows")]
308                                 assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
309                         }
310                         _ => panic!("Unexpected error message")
311                 }
312         }
313
314         // Test failure to create the temporary file in the persistence process.
315         // We induce this failure by having the temp file already exist and be a
316         // directory.
317         #[test]
318         fn test_tmp_file_creation_failure() {
319                 let persister = FilesystemPersister::new("test_tmp_file_creation_failure".to_string());
320                 let test_writeable = TestWriteable{};
321                 let txid_hex = "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be";
322                 let outp_idx = 0;
323                 let test_txo = OutPoint {
324                         txid: Txid::from_hex(txid_hex).unwrap(),
325                         index: outp_idx,
326                 };
327                 // Create the tmp file and make it a directory.
328                 let path = &persister.path_to_channel_data;
329                 fs::create_dir_all(format!("{}/{}_{}.tmp", path, txid_hex, outp_idx)).unwrap();
330                 match persister.write_channel_data(test_txo, &test_writeable) {
331                         Err(e) => {
332                                 #[cfg(not(target_os = "windows"))]
333                                 assert_eq!(e.kind(), io::ErrorKind::Other);
334                                 #[cfg(target_os = "windows")]
335                                 assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
336                         }
337                         _ => panic!("Unexpected error message")
338                 }
339         }
340
341         // Test that if the persister's path to channel data is read-only, writing a
342         // monitor to it results in the persister returning a PermanentFailure.
343         // Windows ignores the read-only flag for folders, so this test is Unix-only.
344         #[cfg(not(target_os = "windows"))]
345         #[test]
346         fn test_readonly_dir_perm_failure() {
347                 let persister = FilesystemPersister::new("test_readonly_dir_perm_failure".to_string());
348                 fs::create_dir_all(&persister.path_to_channel_data).unwrap();
349
350                 // Set up a dummy channel and force close. This will produce a monitor
351                 // that we can then use to test persistence.
352                 let chanmon_cfgs = create_chanmon_cfgs(2);
353                 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
354                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
355                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
356                 let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
357                 nodes[1].node.force_close_channel(&chan.2).unwrap();
358                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
359
360                 // Set the persister's directory to read-only, which should result in
361                 // returning a permanent failure when we then attempt to persist a
362                 // channel update.
363                 let path = &persister.path_to_channel_data;
364                 let mut perms = fs::metadata(path).unwrap().permissions();
365                 perms.set_readonly(true);
366                 fs::set_permissions(path, perms).unwrap();
367
368                 let test_txo = OutPoint {
369                         txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
370                         index: 0
371                 };
372                 match persister.persist_new_channel(test_txo, &added_monitors[0].1) {
373                         Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
374                         _ => panic!("unexpected result from persisting new channel")
375                 }
376
377                 nodes[1].node.get_and_clear_pending_msg_events();
378                 added_monitors.clear();
379         }
380
381         // Test that if a persister's directory name is invalid, monitor persistence
382         // will fail.
383         #[cfg(target_os = "windows")]
384         #[test]
385         fn test_fail_on_open() {
386                 // Set up a dummy channel and force close. This will produce a monitor
387                 // that we can then use to test persistence.
388                 let chanmon_cfgs = create_chanmon_cfgs(2);
389                 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
390                 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
391                 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
392                 let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
393                 nodes[1].node.force_close_channel(&chan.2).unwrap();
394                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
395
396                 // Create the persister with an invalid directory name and test that the
397                 // channel fails to open because the directories fail to be created. There
398                 // don't seem to be invalid filename characters on Unix that Rust doesn't
399                 // handle, hence why the test is Windows-only.
400                 let persister = FilesystemPersister::new(":<>/".to_string());
401
402                 let test_txo = OutPoint {
403                         txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
404                         index: 0
405                 };
406                 match persister.persist_new_channel(test_txo, &added_monitors[0].1) {
407                         Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
408                         _ => panic!("unexpected result from persisting new channel")
409                 }
410
411                 nodes[1].node.get_and_clear_pending_msg_events();
412                 added_monitors.clear();
413         }
414 }