99
1010//! Types and utils for persistence.
1111
12+ use crate :: events:: { EventQueueDeserWrapper , LiquidityEvent } ;
1213use crate :: lsps2:: service:: PeerState as LSPS2ServicePeerState ;
1314use crate :: lsps5:: service:: PeerState as LSPS5ServicePeerState ;
1415use crate :: prelude:: { new_hash_map, HashMap } ;
@@ -20,6 +21,8 @@ use lightning::util::ser::Readable;
2021
2122use bitcoin:: secp256k1:: PublicKey ;
2223
24+ use alloc:: collections:: VecDeque ;
25+
2326use core:: ops:: Deref ;
2427use core:: str:: FromStr ;
2528
@@ -48,6 +51,40 @@ pub const LSPS2_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE: &str = "lsps2_service";
4851/// [`LSPS5ServiceHandler`]: crate::lsps5::service::LSPS5ServiceHandler
4952pub const LSPS5_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE : & str = "lsps5_service" ;
5053
54+ pub ( crate ) async fn read_event_queue < K : Deref > (
55+ kv_store : K ,
56+ ) -> Result < Option < VecDeque < LiquidityEvent > > , lightning:: io:: Error >
57+ where
58+ K :: Target : KVStore ,
59+ {
60+ let read_fut = kv_store. read (
61+ LIQUIDITY_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE ,
62+ LIQUIDITY_MANAGER_EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE ,
63+ LIQUIDITY_MANAGER_EVENT_QUEUE_PERSISTENCE_KEY ,
64+ ) ;
65+
66+ let mut reader = match read_fut. await {
67+ Ok ( r) => Cursor :: new ( r) ,
68+ Err ( e) => {
69+ if e. kind ( ) == lightning:: io:: ErrorKind :: NotFound {
70+ // Key wasn't found, no error but first time running.
71+ return Ok ( None ) ;
72+ } else {
73+ return Err ( e) ;
74+ }
75+ } ,
76+ } ;
77+
78+ let queue: EventQueueDeserWrapper = Readable :: read ( & mut reader) . map_err ( |_| {
79+ lightning:: io:: Error :: new (
80+ lightning:: io:: ErrorKind :: InvalidData ,
81+ "Failed to deserialize liquidity event queue" ,
82+ )
83+ } ) ?;
84+
85+ Ok ( Some ( queue. 0 ) )
86+ }
87+
5188pub ( crate ) async fn read_lsps2_service_peer_states < K : Deref > (
5289 kv_store : K ,
5390) -> Result < HashMap < PublicKey , Mutex < LSPS2ServicePeerState > > , lightning:: io:: Error >
0 commit comments