@@ -24,6 +24,7 @@ use crate::lsps5::client::{LSPS5ClientConfig, LSPS5ClientHandler};
2424use crate :: lsps5:: msgs:: LSPS5Message ;
2525use crate :: lsps5:: service:: { LSPS5ServiceConfig , LSPS5ServiceHandler } ;
2626use crate :: message_queue:: MessageQueue ;
27+ use crate :: persist:: read_lsps2_service_peer_states;
2728
2829use crate :: lsps1:: client:: { LSPS1ClientConfig , LSPS1ClientHandler } ;
2930use crate :: lsps1:: msgs:: LSPS1Message ;
@@ -327,12 +328,14 @@ where
327328 K :: Target : KVStore ,
328329{
329330 /// Constructor for the [`LiquidityManager`] using the default system clock
330- pub fn new (
331+ ///
332+ /// Will read persisted service states from the given [`KVStore`].
333+ pub async fn new (
331334 entropy_source : ES , node_signer : NS , channel_manager : CM , chain_source : Option < C > ,
332335 chain_params : Option < ChainParameters > , kv_store : K ,
333336 service_config : Option < LiquidityServiceConfig > ,
334337 client_config : Option < LiquidityClientConfig > ,
335- ) -> Self {
338+ ) -> Result < Self , lightning :: io :: Error > {
336339 let time_provider = Arc :: new ( DefaultTimeProvider ) ;
337340 Self :: new_with_custom_time_provider (
338341 entropy_source,
@@ -345,6 +348,7 @@ where
345348 client_config,
346349 time_provider,
347350 )
351+ . await
348352 }
349353}
350354
@@ -366,16 +370,18 @@ where
366370{
367371 /// Constructor for the [`LiquidityManager`] with a custom time provider.
368372 ///
373+ /// Will read persisted service states from the given [`KVStore`].
374+ ///
369375 /// This should be used on non-std platforms where access to the system time is not
370376 /// available.
371377 /// Sets up the required protocol message handlers based on the given
372378 /// [`LiquidityClientConfig`] and [`LiquidityServiceConfig`].
373- pub fn new_with_custom_time_provider (
379+ pub async fn new_with_custom_time_provider (
374380 entropy_source : ES , node_signer : NS , channel_manager : CM , chain_source : Option < C > ,
375381 chain_params : Option < ChainParameters > , kv_store : K ,
376382 service_config : Option < LiquidityServiceConfig > ,
377383 client_config : Option < LiquidityClientConfig > , time_provider : TP ,
378- ) -> Self {
384+ ) -> Result < Self , lightning :: io :: Error > {
379385 let pending_messages = Arc :: new ( MessageQueue :: new ( ) ) ;
380386 let pending_events = Arc :: new ( EventQueue :: new ( kv_store. clone ( ) ) ) ;
381387 let ignored_peers = RwLock :: new ( new_hash_set ( ) ) ;
@@ -392,22 +398,30 @@ where
392398 )
393399 } )
394400 } ) ;
395- let lsps2_service_handler = service_config. as_ref ( ) . and_then ( |config| {
396- config. lsps2_service_config . as_ref ( ) . map ( |config| {
401+
402+ let lsps2_service_handler = if let Some ( service_config) = service_config. as_ref ( ) {
403+ if let Some ( lsps2_service_config) = service_config. lsps2_service_config . as_ref ( ) {
397404 if let Some ( number) =
398405 <LSPS2ServiceHandler < CM , K > as LSPSProtocolMessageHandler >:: PROTOCOL_NUMBER
399406 {
400407 supported_protocols. push ( number) ;
401408 }
402- LSPS2ServiceHandler :: new (
409+
410+ let peer_states = read_lsps2_service_peer_states ( kv_store. clone ( ) ) . await ?;
411+ Some ( LSPS2ServiceHandler :: new (
412+ peer_states,
403413 Arc :: clone ( & pending_messages) ,
404414 Arc :: clone ( & pending_events) ,
405415 channel_manager. clone ( ) ,
406416 kv_store. clone ( ) ,
407- config. clone ( ) ,
408- )
409- } )
410- } ) ;
417+ lsps2_service_config. clone ( ) ,
418+ ) ?)
419+ } else {
420+ None
421+ }
422+ } else {
423+ None
424+ } ;
411425
412426 let lsps5_client_handler = client_config. as_ref ( ) . and_then ( |config| {
413427 config. lsps5_client_config . as_ref ( ) . map ( |config| {
@@ -482,7 +496,7 @@ where
482496 None
483497 } ;
484498
485- Self {
499+ Ok ( Self {
486500 pending_messages,
487501 pending_events,
488502 request_id_to_method_map : Mutex :: new ( new_hash_map ( ) ) ,
@@ -500,7 +514,7 @@ where
500514 _client_config : client_config,
501515 best_block : RwLock :: new ( chain_params. map ( |chain_params| chain_params. best_block ) ) ,
502516 _chain_source : chain_source,
503- }
517+ } )
504518 }
505519
506520 /// Returns a reference to the LSPS0 client-side handler.
@@ -1038,9 +1052,10 @@ where
10381052 chain_params : Option < ChainParameters > , kv_store_sync : KS ,
10391053 service_config : Option < LiquidityServiceConfig > ,
10401054 client_config : Option < LiquidityClientConfig > ,
1041- ) -> Self {
1055+ ) -> Result < Self , lightning :: io :: Error > {
10421056 let kv_store = Arc :: new ( KVStoreSyncWrapper ( kv_store_sync) ) ;
1043- let inner = Arc :: new ( LiquidityManager :: new (
1057+
1058+ let mut fut = Box :: pin ( LiquidityManager :: new (
10441059 entropy_source,
10451060 node_signer,
10461061 channel_manager,
@@ -1050,7 +1065,17 @@ where
10501065 service_config,
10511066 client_config,
10521067 ) ) ;
1053- Self { inner }
1068+
1069+ let mut waker = dummy_waker ( ) ;
1070+ let mut ctx = task:: Context :: from_waker ( & mut waker) ;
1071+ let inner = match fut. as_mut ( ) . poll ( & mut ctx) {
1072+ task:: Poll :: Ready ( result) => result,
1073+ task:: Poll :: Pending => {
1074+ // In a sync context, we can't wait for the future to complete.
1075+ unreachable ! ( "LiquidityManager::new should not be pending in a sync context" ) ;
1076+ } ,
1077+ } ?;
1078+ Ok ( Self { inner : Arc :: new ( inner) } )
10541079 }
10551080}
10561081
@@ -1078,9 +1103,9 @@ where
10781103 chain_params : Option < ChainParameters > , kv_store_sync : KS ,
10791104 service_config : Option < LiquidityServiceConfig > ,
10801105 client_config : Option < LiquidityClientConfig > , time_provider : TP ,
1081- ) -> Self {
1106+ ) -> Result < Self , lightning :: io :: Error > {
10821107 let kv_store = Arc :: new ( KVStoreSyncWrapper ( kv_store_sync) ) ;
1083- let inner = Arc :: new ( LiquidityManager :: new_with_custom_time_provider (
1108+ let mut fut = Box :: pin ( LiquidityManager :: new_with_custom_time_provider (
10841109 entropy_source,
10851110 node_signer,
10861111 channel_manager,
@@ -1091,7 +1116,17 @@ where
10911116 client_config,
10921117 time_provider,
10931118 ) ) ;
1094- Self { inner }
1119+
1120+ let mut waker = dummy_waker ( ) ;
1121+ let mut ctx = task:: Context :: from_waker ( & mut waker) ;
1122+ let inner = match fut. as_mut ( ) . poll ( & mut ctx) {
1123+ task:: Poll :: Ready ( result) => result,
1124+ task:: Poll :: Pending => {
1125+ // In a sync context, we can't wait for the future to complete.
1126+ unreachable ! ( "LiquidityManager::new should not be pending in a sync context" ) ;
1127+ } ,
1128+ } ?;
1129+ Ok ( Self { inner : Arc :: new ( inner) } )
10951130 }
10961131
10971132 /// Returns a reference to the LSPS0 client-side handler.
0 commit comments