@@ -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 ;
@@ -35,6 +36,7 @@ use crate::lsps2::msgs::LSPS2Message;
3536use crate :: lsps2:: service:: { LSPS2ServiceConfig , LSPS2ServiceHandler } ;
3637use crate :: prelude:: { new_hash_map, new_hash_set, HashMap , HashSet } ;
3738use crate :: sync:: { Arc , Mutex , RwLock } ;
39+ use crate :: utils:: async_poll:: dummy_waker;
3840#[ cfg( feature = "time" ) ]
3941use crate :: utils:: time:: DefaultTimeProvider ;
4042use crate :: utils:: time:: TimeProvider ;
@@ -57,6 +59,7 @@ use bitcoin::secp256k1::PublicKey;
5759use core:: future:: Future as StdFuture ;
5860use core:: ops:: Deref ;
5961use core:: pin:: Pin ;
62+ use core:: task;
6063
6164const LSPS_FEATURE_BIT : usize = 729 ;
6265
@@ -281,12 +284,14 @@ where
281284 C :: Target : Filter ,
282285{
283286 /// Constructor for the [`LiquidityManager`] using the default system clock
284- pub fn new (
287+ ///
288+ /// Will read persisted service states from the given [`KVStore`].
289+ pub async fn new (
285290 entropy_source : ES , node_signer : NS , channel_manager : CM , chain_source : Option < C > ,
286291 chain_params : Option < ChainParameters > , kv_store : Arc < dyn KVStore + Send + Sync > ,
287292 service_config : Option < LiquidityServiceConfig > ,
288293 client_config : Option < LiquidityClientConfig > ,
289- ) -> Self {
294+ ) -> Result < Self , lightning :: io :: Error > {
290295 let time_provider = Arc :: new ( DefaultTimeProvider ) ;
291296 Self :: new_with_custom_time_provider (
292297 entropy_source,
@@ -299,6 +304,7 @@ where
299304 client_config,
300305 time_provider,
301306 )
307+ . await
302308 }
303309}
304310
@@ -318,16 +324,18 @@ where
318324{
319325 /// Constructor for the [`LiquidityManager`] with a custom time provider.
320326 ///
327+ /// Will read persisted service states from the given [`KVStore`].
328+ ///
321329 /// This should be used on non-std platforms where access to the system time is not
322330 /// available.
323331 /// Sets up the required protocol message handlers based on the given
324332 /// [`LiquidityClientConfig`] and [`LiquidityServiceConfig`].
325- pub fn new_with_custom_time_provider (
333+ pub async fn new_with_custom_time_provider (
326334 entropy_source : ES , node_signer : NS , channel_manager : CM , chain_source : Option < C > ,
327335 chain_params : Option < ChainParameters > , kv_store : Arc < dyn KVStore + Send + Sync > ,
328336 service_config : Option < LiquidityServiceConfig > ,
329337 client_config : Option < LiquidityClientConfig > , time_provider : TP ,
330- ) -> Self {
338+ ) -> Result < Self , lightning :: io :: Error > {
331339 let pending_messages = Arc :: new ( MessageQueue :: new ( ) ) ;
332340 let pending_events = Arc :: new ( EventQueue :: new ( Arc :: clone ( & kv_store) ) ) ;
333341 let ignored_peers = RwLock :: new ( new_hash_set ( ) ) ;
@@ -344,22 +352,30 @@ where
344352 )
345353 } )
346354 } ) ;
347- let lsps2_service_handler = service_config. as_ref ( ) . and_then ( |config| {
348- config. lsps2_service_config . as_ref ( ) . map ( |config| {
355+
356+ let lsps2_service_handler = if let Some ( service_config) = service_config. as_ref ( ) {
357+ if let Some ( lsps2_service_config) = service_config. lsps2_service_config . as_ref ( ) {
349358 if let Some ( number) =
350359 <LSPS2ServiceHandler < CM > as LSPSProtocolMessageHandler >:: PROTOCOL_NUMBER
351360 {
352361 supported_protocols. push ( number) ;
353362 }
354- LSPS2ServiceHandler :: new (
363+
364+ let peer_states = read_lsps2_service_peer_states ( Arc :: clone ( & kv_store) ) . await ?;
365+ Some ( LSPS2ServiceHandler :: new (
366+ peer_states,
355367 Arc :: clone ( & pending_messages) ,
356368 Arc :: clone ( & pending_events) ,
357369 channel_manager. clone ( ) ,
358370 Arc :: clone ( & kv_store) ,
359- config. clone ( ) ,
360- )
361- } )
362- } ) ;
371+ lsps2_service_config. clone ( ) ,
372+ ) )
373+ } else {
374+ None
375+ }
376+ } else {
377+ None
378+ } ;
363379
364380 let lsps5_client_handler = client_config. as_ref ( ) . and_then ( |config| {
365381 config. lsps5_client_config . as_ref ( ) . map ( |config| {
@@ -434,7 +450,7 @@ where
434450 None
435451 } ;
436452
437- Self {
453+ Ok ( Self {
438454 pending_messages,
439455 pending_events,
440456 request_id_to_method_map : Mutex :: new ( new_hash_map ( ) ) ,
@@ -452,7 +468,7 @@ where
452468 _client_config : client_config,
453469 best_block : RwLock :: new ( chain_params. map ( |chain_params| chain_params. best_block ) ) ,
454470 _chain_source : chain_source,
455- }
471+ } )
456472 }
457473
458474 /// Returns a reference to the LSPS0 client-side handler.
@@ -990,9 +1006,10 @@ where
9901006 chain_params : Option < ChainParameters > , kv_store_sync : Arc < dyn KVStoreSync + Send + Sync > ,
9911007 service_config : Option < LiquidityServiceConfig > ,
9921008 client_config : Option < LiquidityClientConfig > ,
993- ) -> Self {
1009+ ) -> Result < Self , lightning :: io :: Error > {
9941010 let kv_store = Arc :: new ( KVStoreSyncWrapper ( kv_store_sync) ) ;
995- let inner = Arc :: new ( LiquidityManager :: new (
1011+
1012+ let mut fut = Box :: pin ( LiquidityManager :: new (
9961013 entropy_source,
9971014 node_signer,
9981015 channel_manager,
@@ -1002,7 +1019,17 @@ where
10021019 service_config,
10031020 client_config,
10041021 ) ) ;
1005- Self { inner }
1022+
1023+ let mut waker = dummy_waker ( ) ;
1024+ let mut ctx = task:: Context :: from_waker ( & mut waker) ;
1025+ let inner = match fut. as_mut ( ) . poll ( & mut ctx) {
1026+ task:: Poll :: Ready ( result) => result,
1027+ task:: Poll :: Pending => {
1028+ // In a sync context, we can't wait for the future to complete.
1029+ unreachable ! ( "LiquidityManager::new should not be pending in a sync context" ) ;
1030+ } ,
1031+ } ?;
1032+ Ok ( Self { inner : Arc :: new ( inner) } )
10061033 }
10071034}
10081035
@@ -1028,9 +1055,9 @@ where
10281055 chain_params : Option < ChainParameters > , kv_store_sync : Arc < dyn KVStoreSync + Send + Sync > ,
10291056 service_config : Option < LiquidityServiceConfig > ,
10301057 client_config : Option < LiquidityClientConfig > , time_provider : TP ,
1031- ) -> Self {
1058+ ) -> Result < Self , lightning :: io :: Error > {
10321059 let kv_store = Arc :: new ( KVStoreSyncWrapper ( kv_store_sync) ) ;
1033- let inner = Arc :: new ( LiquidityManager :: new_with_custom_time_provider (
1060+ let mut fut = Box :: pin ( LiquidityManager :: new_with_custom_time_provider (
10341061 entropy_source,
10351062 node_signer,
10361063 channel_manager,
@@ -1041,7 +1068,17 @@ where
10411068 client_config,
10421069 time_provider,
10431070 ) ) ;
1044- Self { inner }
1071+
1072+ let mut waker = dummy_waker ( ) ;
1073+ let mut ctx = task:: Context :: from_waker ( & mut waker) ;
1074+ let inner = match fut. as_mut ( ) . poll ( & mut ctx) {
1075+ task:: Poll :: Ready ( result) => result,
1076+ task:: Poll :: Pending => {
1077+ // In a sync context, we can't wait for the future to complete.
1078+ unreachable ! ( "LiquidityManager::new should not be pending in a sync context" ) ;
1079+ } ,
1080+ } ?;
1081+ Ok ( Self { inner : Arc :: new ( inner) } )
10451082 }
10461083
10471084 /// Returns a reference to the LSPS0 client-side handler.
0 commit comments