1- use crate :: io:: sqlite_store:: SqliteStore ;
2- use lightning_persister:: fs_store:: FilesystemStore ;
3-
41use lightning:: ln:: functional_test_utils:: {
52 connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
63 create_network, create_node_cfgs, create_node_chanmgrs, send_payment,
@@ -9,12 +6,22 @@ use lightning::util::persist::{read_channel_monitors, KVStore, KVSTORE_NAMESPACE
96
107use lightning:: chain:: channelmonitor:: CLOSED_CHANNEL_UPDATE_ID ;
118use lightning:: events:: ClosureReason ;
12- use lightning:: util:: test_utils:: { self , TestStore } ;
9+ use lightning:: util:: test_utils;
1310use lightning:: { check_added_monitors, check_closed_broadcast, check_closed_event} ;
1411
12+ use rand:: distributions:: Alphanumeric ;
13+ use rand:: { thread_rng, Rng } ;
14+
1515use std:: panic:: RefUnwindSafe ;
1616use std:: path:: PathBuf ;
17- use std:: sync:: RwLock ;
17+
18+ pub ( crate ) fn random_storage_path ( ) -> PathBuf {
19+ let mut temp_path = std:: env:: temp_dir ( ) ;
20+ let mut rng = thread_rng ( ) ;
21+ let rand_dir: String = ( 0 ..7 ) . map ( |_| rng. sample ( Alphanumeric ) as char ) . collect ( ) ;
22+ temp_path. push ( rand_dir) ;
23+ temp_path
24+ }
1825
1926pub ( crate ) fn do_read_write_remove_list_persist < K : KVStore + RefUnwindSafe > ( kv_store : & K ) {
2027 let data = [ 42u8 ; 32 ] ;
@@ -173,148 +180,3 @@ pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
173180 // Make sure everything is persisted as expected after close.
174181 check_persisted_data ! ( CLOSED_CHANNEL_UPDATE_ID ) ;
175182}
176-
177- // A `KVStore` impl for testing purposes that wraps all our `KVStore`s and asserts their synchronicity.
178- pub ( crate ) struct TestSyncStore {
179- serializer : RwLock < ( ) > ,
180- test_store : TestStore ,
181- fs_store : FilesystemStore ,
182- sqlite_store : SqliteStore ,
183- }
184-
185- impl TestSyncStore {
186- pub ( crate ) fn new ( dest_dir : PathBuf ) -> Self {
187- let serializer = RwLock :: new ( ( ) ) ;
188- let mut fs_dir = dest_dir. clone ( ) ;
189- fs_dir. push ( "fs_store" ) ;
190- let fs_store = FilesystemStore :: new ( fs_dir) ;
191- let mut sql_dir = dest_dir. clone ( ) ;
192- sql_dir. push ( "sqlite_store" ) ;
193- let sqlite_store = SqliteStore :: new (
194- sql_dir,
195- Some ( "test_sync_db" . to_string ( ) ) ,
196- Some ( "test_sync_table" . to_string ( ) ) ,
197- )
198- . unwrap ( ) ;
199- let test_store = TestStore :: new ( false ) ;
200- Self { serializer, fs_store, sqlite_store, test_store }
201- }
202-
203- fn do_list (
204- & self , primary_namespace : & str , secondary_namespace : & str ,
205- ) -> std:: io:: Result < Vec < String > > {
206- let fs_res = self . fs_store . list ( primary_namespace, secondary_namespace) ;
207- let sqlite_res = self . sqlite_store . list ( primary_namespace, secondary_namespace) ;
208- let test_res = self . test_store . list ( primary_namespace, secondary_namespace) ;
209-
210- match fs_res {
211- Ok ( mut list) => {
212- list. sort ( ) ;
213-
214- let mut sqlite_list = sqlite_res. unwrap ( ) ;
215- sqlite_list. sort ( ) ;
216- assert_eq ! ( list, sqlite_list) ;
217-
218- let mut test_list = test_res. unwrap ( ) ;
219- test_list. sort ( ) ;
220- assert_eq ! ( list, test_list) ;
221-
222- Ok ( list)
223- }
224- Err ( e) => {
225- assert ! ( sqlite_res. is_err( ) ) ;
226- assert ! ( test_res. is_err( ) ) ;
227- Err ( e)
228- }
229- }
230- }
231- }
232-
233- impl KVStore for TestSyncStore {
234- fn read (
235- & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
236- ) -> std:: io:: Result < Vec < u8 > > {
237- let _guard = self . serializer . read ( ) . unwrap ( ) ;
238-
239- let fs_res = self . fs_store . read ( primary_namespace, secondary_namespace, key) ;
240- let sqlite_res = self . sqlite_store . read ( primary_namespace, secondary_namespace, key) ;
241- let test_res = self . test_store . read ( primary_namespace, secondary_namespace, key) ;
242-
243- match fs_res {
244- Ok ( read) => {
245- assert_eq ! ( read, sqlite_res. unwrap( ) ) ;
246- assert_eq ! ( read, test_res. unwrap( ) ) ;
247- Ok ( read)
248- }
249- Err ( e) => {
250- assert ! ( sqlite_res. is_err( ) ) ;
251- assert_eq ! ( e. kind( ) , unsafe { sqlite_res. unwrap_err_unchecked( ) . kind( ) } ) ;
252- assert ! ( test_res. is_err( ) ) ;
253- assert_eq ! ( e. kind( ) , unsafe { test_res. unwrap_err_unchecked( ) . kind( ) } ) ;
254- Err ( e)
255- }
256- }
257- }
258-
259- fn write (
260- & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ,
261- ) -> std:: io:: Result < ( ) > {
262- let _guard = self . serializer . write ( ) . unwrap ( ) ;
263- let fs_res = self . fs_store . write ( primary_namespace, secondary_namespace, key, buf) ;
264- let sqlite_res = self . sqlite_store . write ( primary_namespace, secondary_namespace, key, buf) ;
265- let test_res = self . test_store . write ( primary_namespace, secondary_namespace, key, buf) ;
266-
267- assert ! ( self
268- . do_list( primary_namespace, secondary_namespace)
269- . unwrap( )
270- . contains( & key. to_string( ) ) ) ;
271-
272- match fs_res {
273- Ok ( ( ) ) => {
274- assert ! ( sqlite_res. is_ok( ) ) ;
275- assert ! ( test_res. is_ok( ) ) ;
276- Ok ( ( ) )
277- }
278- Err ( e) => {
279- assert ! ( sqlite_res. is_err( ) ) ;
280- assert ! ( test_res. is_err( ) ) ;
281- Err ( e)
282- }
283- }
284- }
285-
286- fn remove (
287- & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
288- ) -> std:: io:: Result < ( ) > {
289- let _guard = self . serializer . write ( ) . unwrap ( ) ;
290- let fs_res = self . fs_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
291- let sqlite_res =
292- self . sqlite_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
293- let test_res = self . test_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
294-
295- assert ! ( !self
296- . do_list( primary_namespace, secondary_namespace)
297- . unwrap( )
298- . contains( & key. to_string( ) ) ) ;
299-
300- match fs_res {
301- Ok ( ( ) ) => {
302- assert ! ( sqlite_res. is_ok( ) ) ;
303- assert ! ( test_res. is_ok( ) ) ;
304- Ok ( ( ) )
305- }
306- Err ( e) => {
307- assert ! ( sqlite_res. is_err( ) ) ;
308- assert ! ( test_res. is_err( ) ) ;
309- Err ( e)
310- }
311- }
312- }
313-
314- fn list (
315- & self , primary_namespace : & str , secondary_namespace : & str ,
316- ) -> std:: io:: Result < Vec < String > > {
317- let _guard = self . serializer . read ( ) . unwrap ( ) ;
318- self . do_list ( primary_namespace, secondary_namespace)
319- }
320- }
0 commit comments