@@ -3,7 +3,10 @@ use lightning::ln::functional_test_utils::{
3
3
connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
4
4
create_network, create_node_cfgs, create_node_chanmgrs, send_payment,
5
5
} ;
6
- use lightning:: util:: persist:: { read_channel_monitors, KVStore , KVSTORE_NAMESPACE_KEY_MAX_LEN } ;
6
+ use lightning:: util:: persist:: {
7
+ migrate_kv_store_data, read_channel_monitors, KVStore , MigratableKVStore ,
8
+ KVSTORE_NAMESPACE_KEY_ALPHABET , KVSTORE_NAMESPACE_KEY_MAX_LEN ,
9
+ } ;
7
10
use lightning:: util:: test_utils;
8
11
use lightning:: { check_added_monitors, check_closed_broadcast, check_closed_event} ;
9
12
@@ -59,6 +62,55 @@ pub(crate) fn do_read_write_remove_list_persist<K: KVStore + RefUnwindSafe>(kv_s
59
62
assert_eq ! ( listed_keys. len( ) , 0 ) ;
60
63
}
61
64
65
+ pub ( crate ) fn do_test_data_migration < S : MigratableKVStore , T : MigratableKVStore > (
66
+ source_store : & mut S , target_store : & mut T , move_data : bool ,
67
+ ) {
68
+ // We fill the source with some bogus keys.
69
+ let primary_namespace_base = "testspace" . to_string ( ) ;
70
+ let secondary_namespace_base = "testsubspace" . to_string ( ) ;
71
+ let key_base = "testkey" . to_string ( ) ;
72
+ let alphabet_len = KVSTORE_NAMESPACE_KEY_ALPHABET . len ( ) ;
73
+
74
+ let dummy_data = [ 42u8 ; 32 ] ;
75
+
76
+ let num_primary_namespaces = 5 ;
77
+ let num_secondary_namespaces = 5 ;
78
+ let num_keys = 10 ;
79
+ for i in 0 ..num_primary_namespaces {
80
+ let primary_ext = KVSTORE_NAMESPACE_KEY_ALPHABET . chars ( ) . nth ( i % alphabet_len) . unwrap ( ) ;
81
+ let mut primary_namespace = primary_namespace_base. clone ( ) ;
82
+ primary_namespace. push ( primary_ext) ;
83
+ for j in 0 ..num_secondary_namespaces {
84
+ let secondary_ext =
85
+ KVSTORE_NAMESPACE_KEY_ALPHABET . chars ( ) . nth ( j % alphabet_len) . unwrap ( ) ;
86
+ let mut secondary_namespace = secondary_namespace_base. clone ( ) ;
87
+ secondary_namespace. push ( secondary_ext) ;
88
+ for k in 0 ..num_keys {
89
+ let key_ext = KVSTORE_NAMESPACE_KEY_ALPHABET . chars ( ) . nth ( k % alphabet_len) . unwrap ( ) ;
90
+ let mut key = key_base. clone ( ) ;
91
+ key. push ( key_ext) ;
92
+
93
+ source_store
94
+ . write ( & primary_namespace, & secondary_namespace, & key, & dummy_data)
95
+ . unwrap ( ) ;
96
+ }
97
+ }
98
+ }
99
+ let total_num_entries = num_primary_namespaces * num_secondary_namespaces * num_keys;
100
+ let all_keys = source_store. list_all_keys ( ) . unwrap ( ) ;
101
+ assert_eq ! ( all_keys. len( ) , total_num_entries) ;
102
+
103
+ migrate_kv_store_data ( source_store, target_store, move_data) . unwrap ( ) ;
104
+ if move_data {
105
+ assert_eq ! ( source_store. list_all_keys( ) . unwrap( ) . len( ) , 0 ) ;
106
+ }
107
+
108
+ assert_eq ! ( target_store. list_all_keys( ) . unwrap( ) . len( ) , total_num_entries) ;
109
+ for ( p, s, k) in & all_keys {
110
+ assert_eq ! ( target_store. read( p, s, k) . unwrap( ) , dummy_data) ;
111
+ }
112
+ }
113
+
62
114
// Integration-test the given KVStore implementation. Test relaying a few payments and check that
63
115
// the persisted data is updated the appropriate number of times.
64
116
pub ( crate ) fn do_test_store < K : KVStore > ( store_0 : & K , store_1 : & K ) {
0 commit comments