77// You may not use this file except in accordance with one or both of these
88// licenses.
99
10+ use alloc:: boxed:: Box ;
1011use alloc:: string:: ToString ;
1112use alloc:: vec:: Vec ;
1213
@@ -34,6 +35,7 @@ use crate::lsps2::msgs::LSPS2Message;
3435use crate :: lsps2:: service:: { LSPS2ServiceConfig , LSPS2ServiceHandler } ;
3536use crate :: prelude:: { new_hash_map, new_hash_set, HashMap , HashSet } ;
3637use crate :: sync:: { Arc , Mutex , RwLock } ;
38+ use crate :: utils:: async_poll:: dummy_waker;
3739#[ cfg( feature = "time" ) ]
3840use crate :: utils:: time:: DefaultTimeProvider ;
3941use crate :: utils:: time:: TimeProvider ;
@@ -53,7 +55,9 @@ use lightning_types::features::{InitFeatures, NodeFeatures};
5355
5456use bitcoin:: secp256k1:: PublicKey ;
5557
58+ use core:: future:: Future as StdFuture ;
5659use core:: ops:: Deref ;
60+ use core:: task;
5761
5862const LSPS_FEATURE_BIT : usize = 729 ;
5963
@@ -297,15 +301,14 @@ pub struct LiquidityManager<
297301 #[ cfg( lsps1_service) ]
298302 lsps1_service_handler : Option < LSPS1ServiceHandler < ES , CM , C > > ,
299303 lsps1_client_handler : Option < LSPS1ClientHandler < ES > > ,
300- lsps2_service_handler : Option < LSPS2ServiceHandler < CM > > ,
304+ lsps2_service_handler : Option < LSPS2ServiceHandler < CM , K > > ,
301305 lsps2_client_handler : Option < LSPS2ClientHandler < ES > > ,
302306 lsps5_service_handler : Option < LSPS5ServiceHandler < CM , NS , TP > > ,
303307 lsps5_client_handler : Option < LSPS5ClientHandler < ES > > ,
304308 service_config : Option < LiquidityServiceConfig > ,
305309 _client_config : Option < LiquidityClientConfig > ,
306310 best_block : RwLock < Option < BestBlock > > ,
307311 _chain_source : Option < C > ,
308- kv_store : K ,
309312}
310313
311314#[ cfg( feature = "time" ) ]
@@ -392,14 +395,15 @@ where
392395 let lsps2_service_handler = service_config. as_ref ( ) . and_then ( |config| {
393396 config. lsps2_service_config . as_ref ( ) . map ( |config| {
394397 if let Some ( number) =
395- <LSPS2ServiceHandler < CM > as LSPSProtocolMessageHandler >:: PROTOCOL_NUMBER
398+ <LSPS2ServiceHandler < CM , K > as LSPSProtocolMessageHandler >:: PROTOCOL_NUMBER
396399 {
397400 supported_protocols. push ( number) ;
398401 }
399402 LSPS2ServiceHandler :: new (
400403 Arc :: clone ( & pending_messages) ,
401404 Arc :: clone ( & pending_events) ,
402405 channel_manager. clone ( ) ,
406+ kv_store. clone ( ) ,
403407 config. clone ( ) ,
404408 )
405409 } )
@@ -495,7 +499,6 @@ where
495499 _client_config : client_config,
496500 best_block : RwLock :: new ( chain_params. map ( |chain_params| chain_params. best_block ) ) ,
497501 _chain_source : chain_source,
498- kv_store,
499502 }
500503 }
501504
@@ -534,8 +537,8 @@ where
534537
535538 /// Returns a reference to the LSPS2 server-side handler.
536539 ///
537- /// The returned handler allows to initiate the LSPS2 service-side flow.
538- pub fn lsps2_service_handler ( & self ) -> Option < & LSPS2ServiceHandler < CM > > {
540+ /// The returned hendler allows to initiate the LSPS2 service-side flow.
541+ pub fn lsps2_service_handler ( & self ) -> Option < & LSPS2ServiceHandler < CM , K > > {
539542 self . lsps2_service_handler . as_ref ( )
540543 }
541544
@@ -610,6 +613,19 @@ where
610613 self . pending_events . get_and_clear_pending_events ( )
611614 }
612615
616+ /// Persists the state of the service handlers towards the given [`KVStore`] implementation.
617+ ///
618+ /// This will be regularly called by LDK's background processor if necessary and only needs to
619+ /// be called manually if it's not utilized.
620+ pub async fn persist ( & self ) -> Result < ( ) , lightning:: io:: Error > {
621+ // TODO: We should eventually persist in parallel.
622+ if let Some ( lsps2_service_handler) = self . lsps2_service_handler . as_ref ( ) {
623+ lsps2_service_handler. persist ( ) . await ?;
624+ }
625+
626+ Ok ( ( ) )
627+ }
628+
613629 fn handle_lsps_message (
614630 & self , msg : LSPSMessage , sender_node_id : & PublicKey ,
615631 ) -> Result < ( ) , lightning:: ln:: msgs:: LightningError > {
@@ -1110,7 +1126,9 @@ where
11101126 /// Returns a reference to the LSPS2 server-side handler.
11111127 ///
11121128 /// Wraps [`LiquidityManager::lsps2_service_handler`].
1113- pub fn lsps2_service_handler ( & self ) -> Option < & LSPS2ServiceHandler < CM > > {
1129+ pub fn lsps2_service_handler (
1130+ & self ,
1131+ ) -> Option < & LSPS2ServiceHandler < CM , Arc < KVStoreSyncWrapper < KS > > > > {
11141132 self . inner . lsps2_service_handler ( )
11151133 }
11161134
@@ -1163,6 +1181,21 @@ where
11631181 pub fn get_and_clear_pending_events ( & self ) -> Vec < LiquidityEvent > {
11641182 self . inner . get_and_clear_pending_events ( )
11651183 }
1184+
1185+ /// Persists the state of the service handlers towards the given [`KVStoreSync`] implementation.
1186+ ///
1187+ /// Wraps [`LiquidityManager::persist`].
1188+ pub fn persist ( & self ) -> Result < ( ) , lightning:: io:: Error > {
1189+ let mut waker = dummy_waker ( ) ;
1190+ let mut ctx = task:: Context :: from_waker ( & mut waker) ;
1191+ match Box :: pin ( self . inner . persist ( ) ) . as_mut ( ) . poll ( & mut ctx) {
1192+ task:: Poll :: Ready ( result) => result,
1193+ task:: Poll :: Pending => {
1194+ // In a sync context, we can't wait for the future to complete.
1195+ unreachable ! ( "LiquidityManager::persist should not be pending in a sync context" ) ;
1196+ } ,
1197+ }
1198+ }
11661199}
11671200
11681201impl <
0 commit comments