diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index f53023a0635..be1fac442e5 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -593,14 +593,14 @@ use futures_util::{dummy_waker, Joiner, OptionalSelector, Selector, SelectorOutp /// # struct StoreSync {} /// # impl lightning::util::persist::KVStoreSync for StoreSync { /// # fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> io::Result> { Ok(Vec::new()) } -/// # fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> { Ok(()) } +/// # fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec) -> io::Result<()> { Ok(()) } /// # fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> io::Result<()> { Ok(()) } /// # fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result> { Ok(Vec::new()) } /// # } /// # struct Store {} /// # impl lightning::util::persist::KVStore for Store { /// # fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> Pin, io::Error>> + 'static + Send>> { todo!() } -/// # fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> Pin> + 'static + Send>> { todo!() } +/// # fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec) -> Pin> + 'static + Send>> { todo!() } /// # fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> Pin> + 'static + Send>> { todo!() } /// # fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> Pin, io::Error>> + 'static + Send>> { todo!() } /// # } @@ -796,7 +796,7 @@ where SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, - &scorer.encode(), + scorer.encode(), ) .await { @@ -932,7 +932,7 @@ where CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_KEY, - &channel_manager.get_cm().encode(), + channel_manager.get_cm().encode(), ) .await }; @@ -977,7 +977,7 @@ where NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_KEY, - &network_graph.encode(), + network_graph.encode(), ) .await { @@ -1020,7 +1020,7 @@ where SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, - &scorer.encode(), + scorer.encode(), ) .await { @@ -1128,7 +1128,7 @@ where CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_KEY, - &channel_manager.get_cm().encode(), + channel_manager.get_cm().encode(), ) .await?; if let Some(ref scorer) = scorer { @@ -1137,7 +1137,7 @@ where SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, - &scorer.encode(), + scorer.encode(), ) .await?; } @@ -1147,7 +1147,7 @@ where NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_KEY, - &network_graph.encode(), + network_graph.encode(), ) .await?; } @@ -1352,7 +1352,7 @@ impl BackgroundProcessor { SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, - &scorer.encode(), + scorer.encode(), ) { log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e) } @@ -1452,7 +1452,7 @@ impl BackgroundProcessor { CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_KEY, - &channel_manager.get_cm().encode(), + channel_manager.get_cm().encode(), ))?; log_trace!(logger, "Done persisting ChannelManager."); } @@ -1484,7 +1484,7 @@ impl BackgroundProcessor { NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_KEY, - &network_graph.encode(), + network_graph.encode(), ) { log_error!(logger, "Error: Failed to persist network graph, check your disk and permissions {}",e); } @@ -1513,7 +1513,7 @@ impl BackgroundProcessor { SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, - &scorer.encode(), + scorer.encode(), ) { log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", @@ -1556,14 +1556,14 @@ impl BackgroundProcessor { CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_KEY, - &channel_manager.get_cm().encode(), + channel_manager.get_cm().encode(), )?; if let Some(ref scorer) = scorer { kv_store.write( SCORER_PERSISTENCE_PRIMARY_NAMESPACE, SCORER_PERSISTENCE_SECONDARY_NAMESPACE, SCORER_PERSISTENCE_KEY, - &scorer.encode(), + scorer.encode(), )?; } if let Some(network_graph) = gossip_sync.network_graph() { @@ -1571,7 +1571,7 @@ impl BackgroundProcessor { NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_KEY, - &network_graph.encode(), + network_graph.encode(), )?; } Ok(()) @@ -1916,7 +1916,7 @@ mod tests { } fn write( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, ) -> lightning::io::Result<()> { if primary_namespace == CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE && secondary_namespace == CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE diff --git a/lightning-persister/Cargo.toml b/lightning-persister/Cargo.toml index ec34fa8a88d..5a515fbab22 100644 --- a/lightning-persister/Cargo.toml +++ b/lightning-persister/Cargo.toml @@ -13,9 +13,13 @@ edition = "2021" all-features = true rustdoc-args = ["--cfg", "docsrs"] +[features] +tokio = ["dep:tokio"] + [dependencies] bitcoin = "0.32.2" lightning = { version = "0.2.0", path = "../lightning" } +tokio = { version = "1.35", optional = true, features = [ "macros", "rt-multi-thread" ] } [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.48.0", default-features = false, features = ["Win32_Storage_FileSystem", "Win32_Foundation"] } diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index 5fac0cce617..d27add4e592 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -8,9 +8,18 @@ use std::collections::HashMap; use std::fs; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; +#[cfg(feature = "tokio")] +use std::sync::atomic::AtomicU64; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, RwLock}; +#[cfg(feature = "tokio")] +use core::future::Future; +#[cfg(feature = "tokio")] +use core::pin::Pin; +#[cfg(feature = "tokio")] +use lightning::util::persist::KVStore; + #[cfg(target_os = "windows")] use {std::ffi::OsStr, std::os::windows::ffi::OsStrExt}; @@ -30,19 +39,29 @@ fn path_to_windows_str>(path: &T) -> Vec { path.as_ref().encode_wide().chain(Some(0)).collect() } -// The number of read/write/remove/list operations after which we clean up our `locks` HashMap. -const GC_LOCK_INTERVAL: usize = 25; - // The number of times we retry listing keys in `FilesystemStore::list` before we give up reaching // a consistent view and error out. const LIST_DIR_CONSISTENCY_RETRIES: usize = 10; -/// A [`KVStoreSync`] implementation that writes to and reads from the file system. -pub struct FilesystemStore { +struct FilesystemStoreInner { data_dir: PathBuf, tmp_file_counter: AtomicUsize, - gc_counter: AtomicUsize, - locks: Mutex>>>, + + // Per path lock that ensures that we don't have concurrent writes to the same file. The lock also encapsulates the + // latest written version per key. + locks: Mutex>>>, +} + +/// A [`KVStore`] and [`KVStoreSync`] implementation that writes to and reads from the file system. +/// +/// [`KVStore`]: lightning::util::persist::KVStore +pub struct FilesystemStore { + inner: Arc, + + // Version counter to ensure that writes are applied in the correct order. It is assumed that read, list and remove + // operations aren't sensitive to the order of execution. + #[cfg(feature = "tokio")] + version_counter: AtomicU64, } impl FilesystemStore { @@ -50,27 +69,70 @@ impl FilesystemStore { pub fn new(data_dir: PathBuf) -> Self { let locks = Mutex::new(HashMap::new()); let tmp_file_counter = AtomicUsize::new(0); - let gc_counter = AtomicUsize::new(1); - Self { data_dir, tmp_file_counter, gc_counter, locks } + Self { + inner: Arc::new(FilesystemStoreInner { data_dir, tmp_file_counter, locks }), + #[cfg(feature = "tokio")] + version_counter: AtomicU64::new(0), + } } /// Returns the data directory. pub fn get_data_dir(&self) -> PathBuf { - self.data_dir.clone() + self.inner.data_dir.clone() } +} - fn garbage_collect_locks(&self) { - let gc_counter = self.gc_counter.fetch_add(1, Ordering::AcqRel); +impl KVStoreSync for FilesystemStore { + fn read( + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, + ) -> Result, lightning::io::Error> { + let path = self.inner.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + Some(key), + "read", + )?; + self.inner.read(path) + } - if gc_counter % GC_LOCK_INTERVAL == 0 { - // Take outer lock for the cleanup. - let mut outer_lock = self.locks.lock().unwrap(); + fn write( + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, + ) -> Result<(), lightning::io::Error> { + let path = self.inner.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + Some(key), + "write", + )?; + self.inner.write_version(path, buf, None) + } - // Garbage collect all lock entries that are not referenced anymore. - outer_lock.retain(|_, v| Arc::strong_count(&v) > 1); - } + fn remove( + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool, + ) -> Result<(), lightning::io::Error> { + let path = self.inner.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + Some(key), + "remove", + )?; + self.inner.remove(path, lazy) + } + + fn list( + &self, primary_namespace: &str, secondary_namespace: &str, + ) -> Result, lightning::io::Error> { + let path = self.inner.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + None, + "list", + )?; + self.inner.list(path) } +} +impl FilesystemStoreInner { fn get_dest_dir_path( &self, primary_namespace: &str, secondary_namespace: &str, ) -> std::io::Result { @@ -94,17 +156,22 @@ impl FilesystemStore { Ok(dest_dir_path) } -} -impl KVStoreSync for FilesystemStore { - fn read( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, - ) -> lightning::io::Result> { - check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "read")?; + fn get_checked_dest_file_path( + &self, primary_namespace: &str, secondary_namespace: &str, key: Option<&str>, + operation: &str, + ) -> lightning::io::Result { + check_namespace_key_validity(primary_namespace, secondary_namespace, key, operation)?; let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; - dest_file_path.push(key); + if let Some(key) = key { + dest_file_path.push(key); + } + Ok(dest_file_path) + } + + fn read(&self, dest_file_path: PathBuf) -> lightning::io::Result> { let mut buf = Vec::new(); { let inner_lock_ref = { @@ -117,19 +184,14 @@ impl KVStoreSync for FilesystemStore { f.read_to_end(&mut buf)?; } - self.garbage_collect_locks(); - Ok(buf) } - fn write( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], + /// Writes a specific version of a key to the filesystem. If a newer version has been written already, this function + /// returns early without writing. + fn write_version( + &self, dest_file_path: PathBuf, buf: Vec, version: Option, ) -> lightning::io::Result<()> { - check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "write")?; - - let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; - dest_file_path.push(key); - let parent_directory = dest_file_path.parent().ok_or_else(|| { let msg = format!("Could not retrieve parent directory of {}.", dest_file_path.display()); @@ -157,7 +219,18 @@ impl KVStoreSync for FilesystemStore { let mut outer_lock = self.locks.lock().unwrap(); Arc::clone(&outer_lock.entry(dest_file_path.clone()).or_default()) }; - let _guard = inner_lock_ref.write().unwrap(); + let mut last_written_version = inner_lock_ref.write().unwrap(); + + // If a version is provided, we check if we already have a newer version written. This is used in async + // contexts to realize eventual consistency. + if let Some(version) = version { + if version <= *last_written_version { + // If the version is not greater, we don't write the file. + return Ok(()); + } + + *last_written_version = version; + } #[cfg(not(target_os = "windows"))] { @@ -204,19 +277,10 @@ impl KVStoreSync for FilesystemStore { } }; - self.garbage_collect_locks(); - res } - fn remove( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool, - ) -> lightning::io::Result<()> { - check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "remove")?; - - let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; - dest_file_path.push(key); - + fn remove(&self, dest_file_path: PathBuf, lazy: bool) -> lightning::io::Result<()> { if !dest_file_path.is_file() { return Ok(()); } @@ -299,18 +363,10 @@ impl KVStoreSync for FilesystemStore { } } - self.garbage_collect_locks(); - Ok(()) } - fn list( - &self, primary_namespace: &str, secondary_namespace: &str, - ) -> lightning::io::Result> { - check_namespace_key_validity(primary_namespace, secondary_namespace, None, "list")?; - - let prefixed_dest = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; - + fn list(&self, prefixed_dest: PathBuf) -> lightning::io::Result> { if !Path::new(&prefixed_dest).exists() { return Ok(Vec::new()); } @@ -351,12 +407,106 @@ impl KVStoreSync for FilesystemStore { break 'retry_list; } - self.garbage_collect_locks(); - Ok(keys) } } +#[cfg(feature = "tokio")] +impl KVStore for FilesystemStore { + fn read( + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, + ) -> Pin, lightning::io::Error>> + 'static + Send>> { + let this = Arc::clone(&self.inner); + let path = match this.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + Some(key), + "read", + ) { + Ok(path) => path, + Err(e) => return Box::pin(async move { Err(e) }), + }; + + Box::pin(async move { + tokio::task::spawn_blocking(move || this.read(path)).await.unwrap_or_else(|e| { + Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e)) + }) + }) + } + + fn write( + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, + ) -> Pin> + 'static + Send>> { + let this = Arc::clone(&self.inner); + let path = match this.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + Some(key), + "write", + ) { + Ok(path) => path, + Err(e) => return Box::pin(async move { Err(e) }), + }; + + // Obtain a version number to retain the call sequence. + let version = self.version_counter.fetch_add(1, Ordering::Relaxed); + if version == u64::MAX { + panic!("FilesystemStore version counter overflowed"); + } + + Box::pin(async move { + tokio::task::spawn_blocking(move || this.write_version(path, buf, Some(version))) + .await + .unwrap_or_else(|e| { + Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e)) + }) + }) + } + + fn remove( + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool, + ) -> Pin> + 'static + Send>> { + let this = Arc::clone(&self.inner); + let path = match this.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + Some(key), + "remove", + ) { + Ok(path) => path, + Err(e) => return Box::pin(async move { Err(e) }), + }; + + Box::pin(async move { + tokio::task::spawn_blocking(move || this.remove(path, lazy)).await.unwrap_or_else(|e| { + Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e)) + }) + }) + } + + fn list( + &self, primary_namespace: &str, secondary_namespace: &str, + ) -> Pin, lightning::io::Error>> + 'static + Send>> { + let this = Arc::clone(&self.inner); + + let path = match this.get_checked_dest_file_path( + primary_namespace, + secondary_namespace, + None, + "list", + ) { + Ok(path) => path, + Err(e) => return Box::pin(async move { Err(e) }), + }; + + Box::pin(async move { + tokio::task::spawn_blocking(move || this.list(path)).await.unwrap_or_else(|e| { + Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e)) + }) + }) + } +} + fn dir_entry_is_key(dir_entry: &fs::DirEntry) -> Result { let p = dir_entry.path(); if let Some(ext) = p.extension() { @@ -447,7 +597,7 @@ fn get_key_from_dir_entry_path(p: &Path, base_path: &Path) -> Result Result, lightning::io::Error> { - let prefixed_dest = &self.data_dir; + let prefixed_dest = &self.inner.data_dir; if !prefixed_dest.exists() { return Ok(Vec::new()); } @@ -534,7 +684,7 @@ mod tests { fn drop(&mut self) { // We test for invalid directory names, so it's OK if directory removal // fails. - match fs::remove_dir_all(&self.data_dir) { + match fs::remove_dir_all(&self.inner.data_dir) { Err(e) => println!("Failed to remove test persister directory: {}", e), _ => {}, } @@ -549,6 +699,48 @@ mod tests { do_read_write_remove_list_persist(&fs_store); } + #[cfg(feature = "tokio")] + #[tokio::test] + async fn read_write_remove_list_persist_async() { + use crate::fs_store::FilesystemStore; + use lightning::util::persist::KVStore; + use std::sync::Arc; + + let mut temp_path = std::env::temp_dir(); + temp_path.push("test_read_write_remove_list_persist_async"); + let fs_store: Arc = Arc::new(FilesystemStore::new(temp_path)); + + let data1 = vec![42u8; 32]; + let data2 = vec![43u8; 32]; + + let primary_namespace = "testspace"; + let secondary_namespace = "testsubspace"; + let key = "testkey"; + + // Test writing the same key twice with different data. Execute the asynchronous part out of order to ensure + // that eventual consistency works. + let fut1 = fs_store.write(primary_namespace, secondary_namespace, key, data1); + let fut2 = fs_store.write(primary_namespace, secondary_namespace, key, data2.clone()); + + fut2.await.unwrap(); + fut1.await.unwrap(); + + // Test list. + let listed_keys = fs_store.list(primary_namespace, secondary_namespace).await.unwrap(); + assert_eq!(listed_keys.len(), 1); + assert_eq!(listed_keys[0], key); + + // Test read. We expect to read data2, as the write call was initiated later. + let read_data = fs_store.read(primary_namespace, secondary_namespace, key).await.unwrap(); + assert_eq!(data2, &*read_data); + + // Test remove. + fs_store.remove(primary_namespace, secondary_namespace, key, false).await.unwrap(); + + let listed_keys = fs_store.list(primary_namespace, secondary_namespace).await.unwrap(); + assert_eq!(listed_keys.len(), 0); + } + #[test] fn test_data_migration() { let mut source_temp_path = std::env::temp_dir(); diff --git a/lightning-persister/src/test_utils.rs b/lightning-persister/src/test_utils.rs index 18d643c7443..636967a6937 100644 --- a/lightning-persister/src/test_utils.rs +++ b/lightning-persister/src/test_utils.rs @@ -13,22 +13,23 @@ use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event use std::panic::RefUnwindSafe; pub(crate) fn do_read_write_remove_list_persist(kv_store: &K) { - let data = [42u8; 32]; + let data = vec![42u8; 32]; let primary_namespace = "testspace"; let secondary_namespace = "testsubspace"; let key = "testkey"; // Test the basic KVStore operations. - kv_store.write(primary_namespace, secondary_namespace, key, &data).unwrap(); + kv_store.write(primary_namespace, secondary_namespace, key, data.clone()).unwrap(); // Test empty primary_namespace/secondary_namespace is allowed, but not empty primary_namespace // and non-empty secondary_namespace, and not empty key. - kv_store.write("", "", key, &data).unwrap(); - let res = std::panic::catch_unwind(|| kv_store.write("", secondary_namespace, key, &data)); + kv_store.write("", "", key, data.clone()).unwrap(); + let res = + std::panic::catch_unwind(|| kv_store.write("", secondary_namespace, key, data.clone())); assert!(res.is_err()); let res = std::panic::catch_unwind(|| { - kv_store.write(primary_namespace, secondary_namespace, "", &data) + kv_store.write(primary_namespace, secondary_namespace, "", data.clone()) }); assert!(res.is_err()); @@ -47,7 +48,7 @@ pub(crate) fn do_read_write_remove_list_persist( // Ensure we have no issue operating with primary_namespace/secondary_namespace/key being // KVSTORE_NAMESPACE_KEY_MAX_LEN let max_chars = "A".repeat(KVSTORE_NAMESPACE_KEY_MAX_LEN); - kv_store.write(&max_chars, &max_chars, &max_chars, &data).unwrap(); + kv_store.write(&max_chars, &max_chars, &max_chars, data.clone()).unwrap(); let listed_keys = kv_store.list(&max_chars, &max_chars).unwrap(); assert_eq!(listed_keys.len(), 1); @@ -66,7 +67,7 @@ pub(crate) fn do_test_data_migration source_store: &mut S, target_store: &mut T, ) { // We fill the source with some bogus keys. - let dummy_data = [42u8; 32]; + let dummy_data = vec![42u8; 32]; let num_primary_namespaces = 3; let num_secondary_namespaces = 3; let num_keys = 3; @@ -87,7 +88,7 @@ pub(crate) fn do_test_data_migration let key = format!("testkey{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(k).unwrap()); source_store - .write(&primary_namespace, &secondary_namespace, &key, &dummy_data) + .write(&primary_namespace, &secondary_namespace, &key, dummy_data.clone()) .unwrap(); expected_keys.push((primary_namespace.clone(), secondary_namespace.clone(), key)); } @@ -107,7 +108,7 @@ pub(crate) fn do_test_data_migration assert_eq!(target_list, expected_keys); for (p, s, k) in expected_keys.iter() { - assert_eq!(target_store.read(p, s, k).unwrap(), dummy_data); + assert_eq!(target_store.read(p, s, k).unwrap(), dummy_data.clone()); } } diff --git a/lightning/src/util/persist.rs b/lightning/src/util/persist.rs index 631b3313b2a..6b2ceaf4c34 100644 --- a/lightning/src/util/persist.rs +++ b/lightning/src/util/persist.rs @@ -118,7 +118,7 @@ pub trait KVStoreSync { ) -> Result, io::Error>; /// A synchronous version of the [`KVStore::write`] method. fn write( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, ) -> Result<(), io::Error>; /// A synchronous version of the [`KVStore::remove`] method. fn remove( @@ -159,7 +159,7 @@ where } fn write( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, ) -> Pin> + 'static + Send>> { let res = self.0.write(primary_namespace, secondary_namespace, key, buf); @@ -233,7 +233,7 @@ pub trait KVStore { /// /// Will create the given `primary_namespace` and `secondary_namespace` if not already present in the store. fn write( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, ) -> Pin> + 'static + Send>>; /// Removes any data that had previously been persisted under the given `key`. /// @@ -291,7 +291,7 @@ pub fn migrate_kv_store_data( for (primary_namespace, secondary_namespace, key) in &keys_to_migrate { let data = source_store.read(primary_namespace, secondary_namespace, key)?; - target_store.write(primary_namespace, secondary_namespace, key, &data)?; + target_store.write(primary_namespace, secondary_namespace, key, data)?; } Ok(()) @@ -310,7 +310,7 @@ impl Persist chain::ChannelMonitorUpdateStatus::Completed, Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError, @@ -325,7 +325,7 @@ impl Persist chain::ChannelMonitorUpdateStatus::Completed, Err(_) => chain::ChannelMonitorUpdateStatus::UnrecoverableError, @@ -346,7 +346,7 @@ impl Persist {}, Err(_e) => return, @@ -763,7 +763,7 @@ where CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE, CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE, monitor_key.as_str(), - &monitor_bytes, + monitor_bytes, ) { Ok(_) => chain::ChannelMonitorUpdateStatus::Completed, Err(e) => { @@ -804,7 +804,7 @@ where CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE, monitor_key.as_str(), update_name.as_str(), - &update.encode(), + update.encode(), ) { Ok(()) => chain::ChannelMonitorUpdateStatus::Completed, Err(e) => { @@ -876,7 +876,7 @@ where ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE, ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE, monitor_key.as_str(), - &monitor.encode(), + monitor.encode(), ) { Ok(()) => {}, Err(_e) => return, @@ -1487,7 +1487,7 @@ mod tests { CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE, &monitor_name.to_string(), UpdateName::from(1).as_str(), - &[0u8; 1], + vec![0u8; 1], ) .unwrap(); diff --git a/lightning/src/util/sweep.rs b/lightning/src/util/sweep.rs index bec72112c69..b72dddbcc7c 100644 --- a/lightning/src/util/sweep.rs +++ b/lightning/src/util/sweep.rs @@ -659,7 +659,7 @@ where OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE, OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE, OUTPUT_SWEEPER_PERSISTENCE_KEY, - &encoded, + encoded, ) } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 776d876bbc8..e5388b76049 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -858,7 +858,7 @@ impl KVStoreSync for TestStore { } fn write( - &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec, ) -> io::Result<()> { if self.read_only { return Err(io::Error::new( @@ -875,7 +875,7 @@ impl KVStoreSync for TestStore { }; let outer_e = persisted_lock.entry(prefixed).or_insert(new_hash_map()); let mut bytes = Vec::new(); - bytes.write_all(buf)?; + bytes.write_all(&buf)?; outer_e.insert(key.to_string(), bytes); Ok(()) }