diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index 44417a963..487e1a7ac 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -494,7 +494,9 @@ impl MigratableKVStore for FilesystemStore { #[cfg(test)] mod tests { use super::*; - use crate::test_utils::{do_read_write_remove_list_persist, do_test_store}; + use crate::test_utils::{ + do_read_write_remove_list_persist, do_test_data_migration, do_test_store, + }; use bitcoin::Txid; @@ -527,6 +529,19 @@ mod tests { do_read_write_remove_list_persist(&fs_store); } + #[test] + fn test_data_migration() { + let mut source_temp_path = std::env::temp_dir(); + source_temp_path.push("test_data_migration_source"); + let mut source_store = FilesystemStore::new(source_temp_path); + + let mut target_temp_path = std::env::temp_dir(); + target_temp_path.push("test_data_migration_target"); + let mut target_store = FilesystemStore::new(target_temp_path); + + do_test_data_migration(&mut source_store, &mut target_store); + } + #[test] fn test_if_monitors_is_not_dir() { let store = FilesystemStore::new("test_monitors_is_not_dir".into()); diff --git a/lightning-persister/src/test_utils.rs b/lightning-persister/src/test_utils.rs index 735870e49..18492336d 100644 --- a/lightning-persister/src/test_utils.rs +++ b/lightning-persister/src/test_utils.rs @@ -3,7 +3,10 @@ use lightning::ln::functional_test_utils::{ connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block, create_network, create_node_cfgs, create_node_chanmgrs, send_payment, }; -use lightning::util::persist::{read_channel_monitors, KVStore, KVSTORE_NAMESPACE_KEY_MAX_LEN}; +use lightning::util::persist::{ + migrate_kv_store_data, read_channel_monitors, KVStore, MigratableKVStore, + KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN, +}; use lightning::util::test_utils; use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event}; @@ -59,6 +62,41 @@ pub(crate) fn do_read_write_remove_list_persist(kv_s assert_eq!(listed_keys.len(), 0); } +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 num_primary_namespaces = 2; + let num_secondary_namespaces = 2; + let num_keys = 3; + for i in 0..num_primary_namespaces { + let primary_namespace = + format!("testspace{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(i).unwrap()); + for j in 0..num_secondary_namespaces { + let secondary_namespace = + format!("testsubspace{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(j).unwrap()); + for k in 0..num_keys { + let key = + format!("testkey{}", KVSTORE_NAMESPACE_KEY_ALPHABET.chars().nth(k).unwrap()); + source_store + .write(&primary_namespace, &secondary_namespace, &key, &dummy_data) + .unwrap(); + } + } + } + let total_num_entries = num_primary_namespaces * num_secondary_namespaces * num_keys; + let all_keys = source_store.list_all_keys().unwrap(); + assert_eq!(all_keys.len(), total_num_entries); + + migrate_kv_store_data(source_store, target_store).unwrap(); + + assert_eq!(target_store.list_all_keys().unwrap().len(), total_num_entries); + for (p, s, k) in &all_keys { + assert_eq!(target_store.read(p, s, k).unwrap(), dummy_data); + } +} + // Integration-test the given KVStore implementation. Test relaying a few payments and check that // the persisted data is updated the appropriate number of times. pub(crate) fn do_test_store(store_0: &K, store_1: &K) {