@@ -38,6 +38,7 @@ use crate::util::config::UserConfig;
3838use crate :: util:: test_channel_signer:: { TestChannelSigner , EnforcementState } ;
3939use crate :: util:: logger:: { Logger , Level , Record } ;
4040use crate :: util:: ser:: { Readable , ReadableArgs , Writer , Writeable } ;
41+ use crate :: util:: persist:: KVStore ;
4142
4243use bitcoin:: EcdsaSighashType ;
4344use bitcoin:: blockdata:: constants:: ChainHash ;
@@ -425,6 +426,73 @@ impl<Signer: sign::WriteableEcdsaChannelSigner> chainmonitor::Persist<Signer> fo
425426 }
426427}
427428
429+ pub ( crate ) struct TestStore {
430+ persisted_bytes : Mutex < HashMap < String , HashMap < String , Vec < u8 > > > > ,
431+ read_only : bool ,
432+ }
433+
434+ impl TestStore {
435+ pub fn new ( read_only : bool ) -> Self {
436+ let persisted_bytes = Mutex :: new ( HashMap :: new ( ) ) ;
437+ Self { persisted_bytes, read_only }
438+ }
439+ }
440+
441+ impl KVStore for TestStore {
442+ fn read ( & self , namespace : & str , key : & str ) -> io:: Result < Vec < u8 > > {
443+ let persisted_lock = self . persisted_bytes . lock ( ) . unwrap ( ) ;
444+ if let Some ( outer_ref) = persisted_lock. get ( namespace) {
445+ if let Some ( inner_ref) = outer_ref. get ( key) {
446+ let bytes = inner_ref. clone ( ) ;
447+ Ok ( bytes)
448+ } else {
449+ Err ( io:: Error :: new ( io:: ErrorKind :: NotFound , "Key not found" ) )
450+ }
451+ } else {
452+ Err ( io:: Error :: new ( io:: ErrorKind :: NotFound , "Namespace not found" ) )
453+ }
454+ }
455+
456+ fn write ( & self , namespace : & str , key : & str , buf : & [ u8 ] ) -> io:: Result < ( ) > {
457+ if self . read_only {
458+ return Err ( io:: Error :: new (
459+ io:: ErrorKind :: PermissionDenied ,
460+ "Cannot modify read-only store" ,
461+ ) ) ;
462+ }
463+ let mut persisted_lock = self . persisted_bytes . lock ( ) . unwrap ( ) ;
464+ let outer_e = persisted_lock. entry ( namespace. to_string ( ) ) . or_insert ( HashMap :: new ( ) ) ;
465+ let mut bytes = Vec :: new ( ) ;
466+ bytes. write_all ( buf) ?;
467+ outer_e. insert ( key. to_string ( ) , bytes) ;
468+ Ok ( ( ) )
469+ }
470+
471+ fn remove ( & self , namespace : & str , key : & str ) -> io:: Result < ( ) > {
472+ if self . read_only {
473+ return Err ( io:: Error :: new (
474+ io:: ErrorKind :: PermissionDenied ,
475+ "Cannot modify read-only store" ,
476+ ) ) ;
477+ }
478+
479+ let mut persisted_lock = self . persisted_bytes . lock ( ) . unwrap ( ) ;
480+ if let Some ( outer_ref) = persisted_lock. get_mut ( namespace) {
481+ outer_ref. remove ( & key. to_string ( ) ) ;
482+ }
483+
484+ Ok ( ( ) )
485+ }
486+
487+ fn list ( & self , namespace : & str ) -> io:: Result < Vec < String > > {
488+ let mut persisted_lock = self . persisted_bytes . lock ( ) . unwrap ( ) ;
489+ match persisted_lock. entry ( namespace. to_string ( ) ) {
490+ hash_map:: Entry :: Occupied ( e) => Ok ( e. get ( ) . keys ( ) . cloned ( ) . collect ( ) ) ,
491+ hash_map:: Entry :: Vacant ( _) => Ok ( Vec :: new ( ) ) ,
492+ }
493+ }
494+ }
495+
428496pub struct TestBroadcaster {
429497 pub txn_broadcasted : Mutex < Vec < Transaction > > ,
430498 pub blocks : Arc < Mutex < Vec < ( Block , u32 ) > > > ,
0 commit comments