66// accordance with one or both of these licenses.
77
88use crate :: io:: utils:: check_namespace_key_validity;
9+ use crate :: logger:: Logger ;
10+ use crate :: runtime:: Runtime ;
11+
912use bitcoin:: hashes:: { sha256, Hash , HashEngine , Hmac , HmacEngine } ;
1013use lightning:: io:: { self , Error , ErrorKind } ;
1114use lightning:: util:: persist:: KVStore ;
@@ -15,7 +18,6 @@ use rand::RngCore;
1518use std:: panic:: RefUnwindSafe ;
1619use std:: sync:: Arc ;
1720use std:: time:: Duration ;
18- use tokio:: runtime:: Runtime ;
1921use vss_client:: client:: VssClient ;
2022use vss_client:: error:: VssError ;
2123use vss_client:: headers:: VssHeaderProvider ;
@@ -49,9 +51,10 @@ pub struct VssStore {
4951impl VssStore {
5052 pub ( crate ) fn new (
5153 base_url : String , store_id : String , vss_seed : [ u8 ; 32 ] ,
52- header_provider : Arc < dyn VssHeaderProvider > ,
54+ header_provider : Arc < dyn VssHeaderProvider > , logger : Arc < Logger > ,
5355 ) -> io:: Result < Self > {
54- let runtime = tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) ?;
56+ let runtime = Runtime :: new ( logger) ;
57+ runtime. start ( ) ?;
5558 let ( data_encryption_key, obfuscation_master_key) =
5659 derive_data_encryption_and_obfuscation_keys ( & vss_seed) ;
5760 let key_obfuscator = KeyObfuscator :: new ( obfuscation_master_key) ;
@@ -136,19 +139,16 @@ impl KVStore for VssStore {
136139 store_id : self . store_id . clone ( ) ,
137140 key : self . build_key ( primary_namespace, secondary_namespace, key) ?,
138141 } ;
139-
140- let resp =
141- tokio:: task:: block_in_place ( || self . runtime . block_on ( self . client . get_object ( & request) ) )
142- . map_err ( |e| {
143- let msg = format ! (
144- "Failed to read from key {}/{}/{}: {}" ,
145- primary_namespace, secondary_namespace, key, e
146- ) ;
147- match e {
148- VssError :: NoSuchKeyError ( ..) => Error :: new ( ErrorKind :: NotFound , msg) ,
149- _ => Error :: new ( ErrorKind :: Other , msg) ,
150- }
151- } ) ?;
142+ let resp = self . runtime . block_on ( self . client . get_object ( & request) ) ?. map_err ( |e| {
143+ let msg = format ! (
144+ "Failed to read from key {}/{}/{}: {}" ,
145+ primary_namespace, secondary_namespace, key, e
146+ ) ;
147+ match e {
148+ VssError :: NoSuchKeyError ( ..) => Error :: new ( ErrorKind :: NotFound , msg) ,
149+ _ => Error :: new ( ErrorKind :: Other , msg) ,
150+ }
151+ } ) ?;
152152 // unwrap safety: resp.value must be always present for a non-erroneous VSS response, otherwise
153153 // it is an API-violation which is converted to [`VssError::InternalServerError`] in [`VssClient`]
154154 let storable = Storable :: decode ( & resp. value . unwrap ( ) . value [ ..] ) . map_err ( |e| {
@@ -179,14 +179,13 @@ impl KVStore for VssStore {
179179 delete_items : vec ! [ ] ,
180180 } ;
181181
182- tokio:: task:: block_in_place ( || self . runtime . block_on ( self . client . put_object ( & request) ) )
183- . map_err ( |e| {
184- let msg = format ! (
185- "Failed to write to key {}/{}/{}: {}" ,
186- primary_namespace, secondary_namespace, key, e
187- ) ;
188- Error :: new ( ErrorKind :: Other , msg)
189- } ) ?;
182+ self . runtime . block_on ( self . client . put_object ( & request) ) ?. map_err ( |e| {
183+ let msg = format ! (
184+ "Failed to write to key {}/{}/{}: {}" ,
185+ primary_namespace, secondary_namespace, key, e
186+ ) ;
187+ Error :: new ( ErrorKind :: Other , msg)
188+ } ) ?;
190189
191190 Ok ( ( ) )
192191 }
@@ -204,30 +203,29 @@ impl KVStore for VssStore {
204203 } ) ,
205204 } ;
206205
207- tokio:: task:: block_in_place ( || self . runtime . block_on ( self . client . delete_object ( & request) ) )
208- . map_err ( |e| {
209- let msg = format ! (
210- "Failed to delete key {}/{}/{}: {}" ,
211- primary_namespace, secondary_namespace, key, e
212- ) ;
213- Error :: new ( ErrorKind :: Other , msg)
214- } ) ?;
206+ self . runtime . block_on ( self . client . delete_object ( & request) ) ?. map_err ( |e| {
207+ let msg = format ! (
208+ "Failed to delete key {}/{}/{}: {}" ,
209+ primary_namespace, secondary_namespace, key, e
210+ ) ;
211+ Error :: new ( ErrorKind :: Other , msg)
212+ } ) ?;
215213 Ok ( ( ) )
216214 }
217215
218216 fn list ( & self , primary_namespace : & str , secondary_namespace : & str ) -> io:: Result < Vec < String > > {
219217 check_namespace_key_validity ( primary_namespace, secondary_namespace, None , "list" ) ?;
220218
221- let keys = tokio :: task :: block_in_place ( || {
222- self . runtime . block_on ( self . list_all_keys ( primary_namespace , secondary_namespace ) )
223- } )
224- . map_err ( |e| {
225- let msg = format ! (
226- "Failed to retrieve keys in namespace: {}/{} : {}" ,
227- primary_namespace, secondary_namespace, e
228- ) ;
229- Error :: new ( ErrorKind :: Other , msg)
230- } ) ?;
219+ let keys = self
220+ . runtime
221+ . block_on ( self . list_all_keys ( primary_namespace , secondary_namespace ) ) ?
222+ . map_err ( |e| {
223+ let msg = format ! (
224+ "Failed to retrieve keys in namespace: {}/{} : {}" ,
225+ primary_namespace, secondary_namespace, e
226+ ) ;
227+ Error :: new ( ErrorKind :: Other , msg)
228+ } ) ?;
231229
232230 Ok ( keys)
233231 }
@@ -269,7 +267,21 @@ mod tests {
269267 use vss_client:: headers:: FixedHeaders ;
270268
271269 #[ test]
272- fn read_write_remove_list_persist ( ) {
270+ fn vss_read_write_remove_list_persist ( ) {
271+ let vss_base_url = std:: env:: var ( "TEST_VSS_BASE_URL" ) . unwrap ( ) ;
272+ let mut rng = thread_rng ( ) ;
273+ let rand_store_id: String = ( 0 ..7 ) . map ( |_| rng. sample ( Alphanumeric ) as char ) . collect ( ) ;
274+ let mut vss_seed = [ 0u8 ; 32 ] ;
275+ rng. fill_bytes ( & mut vss_seed) ;
276+ let header_provider = Arc :: new ( FixedHeaders :: new ( HashMap :: new ( ) ) ) ;
277+ let vss_store =
278+ VssStore :: new ( vss_base_url, rand_store_id, vss_seed, header_provider) . unwrap ( ) ;
279+
280+ do_read_write_remove_list_persist ( & vss_store) ;
281+ }
282+
283+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
284+ async fn vss_read_write_remove_list_persist_in_runtime_context ( ) {
273285 let vss_base_url = std:: env:: var ( "TEST_VSS_BASE_URL" ) . unwrap ( ) ;
274286 let mut rng = thread_rng ( ) ;
275287 let rand_store_id: String = ( 0 ..7 ) . map ( |_| rng. sample ( Alphanumeric ) as char ) . collect ( ) ;
0 commit comments