@@ -24,6 +24,7 @@ use crate::lsps5::client::{LSPS5ClientConfig, LSPS5ClientHandler};
24
24
use crate :: lsps5:: msgs:: LSPS5Message ;
25
25
use crate :: lsps5:: service:: { LSPS5ServiceConfig , LSPS5ServiceHandler } ;
26
26
use crate :: message_queue:: MessageQueue ;
27
+ use crate :: persist:: read_lsps2_service_peer_states;
27
28
28
29
use crate :: lsps1:: client:: { LSPS1ClientConfig , LSPS1ClientHandler } ;
29
30
use crate :: lsps1:: msgs:: LSPS1Message ;
@@ -35,6 +36,7 @@ use crate::lsps2::msgs::LSPS2Message;
35
36
use crate :: lsps2:: service:: { LSPS2ServiceConfig , LSPS2ServiceHandler } ;
36
37
use crate :: prelude:: { new_hash_map, new_hash_set, HashMap , HashSet } ;
37
38
use crate :: sync:: { Arc , Mutex , RwLock } ;
39
+ use crate :: utils:: async_poll:: dummy_waker;
38
40
#[ cfg( feature = "time" ) ]
39
41
use crate :: utils:: time:: DefaultTimeProvider ;
40
42
use crate :: utils:: time:: TimeProvider ;
@@ -57,6 +59,7 @@ use bitcoin::secp256k1::PublicKey;
57
59
use core:: future:: Future as StdFuture ;
58
60
use core:: ops:: Deref ;
59
61
use core:: pin:: Pin ;
62
+ use core:: task;
60
63
61
64
const LSPS_FEATURE_BIT : usize = 729 ;
62
65
@@ -281,12 +284,14 @@ where
281
284
C :: Target : Filter ,
282
285
{
283
286
/// 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 (
285
290
entropy_source : ES , node_signer : NS , channel_manager : CM , chain_source : Option < C > ,
286
291
chain_params : Option < ChainParameters > , kv_store : Arc < dyn KVStore + Send + Sync > ,
287
292
service_config : Option < LiquidityServiceConfig > ,
288
293
client_config : Option < LiquidityClientConfig > ,
289
- ) -> Self {
294
+ ) -> Result < Self , lightning :: io :: Error > {
290
295
let time_provider = Arc :: new ( DefaultTimeProvider ) ;
291
296
Self :: new_with_custom_time_provider (
292
297
entropy_source,
@@ -299,6 +304,7 @@ where
299
304
client_config,
300
305
time_provider,
301
306
)
307
+ . await
302
308
}
303
309
}
304
310
@@ -318,16 +324,18 @@ where
318
324
{
319
325
/// Constructor for the [`LiquidityManager`] with a custom time provider.
320
326
///
327
+ /// Will read persisted service states from the given [`KVStore`].
328
+ ///
321
329
/// This should be used on non-std platforms where access to the system time is not
322
330
/// available.
323
331
/// Sets up the required protocol message handlers based on the given
324
332
/// [`LiquidityClientConfig`] and [`LiquidityServiceConfig`].
325
- pub fn new_with_custom_time_provider (
333
+ pub async fn new_with_custom_time_provider (
326
334
entropy_source : ES , node_signer : NS , channel_manager : CM , chain_source : Option < C > ,
327
335
chain_params : Option < ChainParameters > , kv_store : Arc < dyn KVStore + Send + Sync > ,
328
336
service_config : Option < LiquidityServiceConfig > ,
329
337
client_config : Option < LiquidityClientConfig > , time_provider : TP ,
330
- ) -> Self {
338
+ ) -> Result < Self , lightning :: io :: Error > {
331
339
let pending_messages = Arc :: new ( MessageQueue :: new ( ) ) ;
332
340
let pending_events = Arc :: new ( EventQueue :: new ( Arc :: clone ( & kv_store) ) ) ;
333
341
let ignored_peers = RwLock :: new ( new_hash_set ( ) ) ;
@@ -344,22 +352,30 @@ where
344
352
)
345
353
} )
346
354
} ) ;
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 ( ) {
349
358
if let Some ( number) =
350
359
<LSPS2ServiceHandler < CM > as LSPSProtocolMessageHandler >:: PROTOCOL_NUMBER
351
360
{
352
361
supported_protocols. push ( number) ;
353
362
}
354
- LSPS2ServiceHandler :: new (
363
+
364
+ let peer_states = read_lsps2_service_peer_states ( kv_store. clone ( ) ) . await ?;
365
+ Some ( LSPS2ServiceHandler :: new (
366
+ peer_states,
355
367
Arc :: clone ( & pending_messages) ,
356
368
Arc :: clone ( & pending_events) ,
357
369
channel_manager. clone ( ) ,
358
370
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
+ } ;
363
379
364
380
let lsps5_client_handler = client_config. as_ref ( ) . and_then ( |config| {
365
381
config. lsps5_client_config . as_ref ( ) . map ( |config| {
@@ -434,7 +450,7 @@ where
434
450
None
435
451
} ;
436
452
437
- Self {
453
+ Ok ( Self {
438
454
pending_messages,
439
455
pending_events,
440
456
request_id_to_method_map : Mutex :: new ( new_hash_map ( ) ) ,
@@ -452,7 +468,7 @@ where
452
468
_client_config : client_config,
453
469
best_block : RwLock :: new ( chain_params. map ( |chain_params| chain_params. best_block ) ) ,
454
470
_chain_source : chain_source,
455
- }
471
+ } )
456
472
}
457
473
458
474
/// Returns a reference to the LSPS0 client-side handler.
@@ -990,9 +1006,10 @@ where
990
1006
chain_params : Option < ChainParameters > , kv_store_sync : Arc < dyn KVStoreSync + Send + Sync > ,
991
1007
service_config : Option < LiquidityServiceConfig > ,
992
1008
client_config : Option < LiquidityClientConfig > ,
993
- ) -> Self {
1009
+ ) -> Result < Self , lightning :: io :: Error > {
994
1010
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 (
996
1013
entropy_source,
997
1014
node_signer,
998
1015
channel_manager,
@@ -1002,7 +1019,17 @@ where
1002
1019
service_config,
1003
1020
client_config,
1004
1021
) ) ;
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) } )
1006
1033
}
1007
1034
}
1008
1035
@@ -1028,9 +1055,9 @@ where
1028
1055
chain_params : Option < ChainParameters > , kv_store_sync : Arc < dyn KVStoreSync + Send + Sync > ,
1029
1056
service_config : Option < LiquidityServiceConfig > ,
1030
1057
client_config : Option < LiquidityClientConfig > , time_provider : TP ,
1031
- ) -> Self {
1058
+ ) -> Result < Self , lightning :: io :: Error > {
1032
1059
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 (
1034
1061
entropy_source,
1035
1062
node_signer,
1036
1063
channel_manager,
@@ -1041,7 +1068,17 @@ where
1041
1068
client_config,
1042
1069
time_provider,
1043
1070
) ) ;
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) } )
1045
1082
}
1046
1083
1047
1084
/// Returns a reference to the LSPS0 client-side handler.
0 commit comments