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 ;
1415
@@ -19,6 +20,7 @@ use lightning::util::ser::Readable;
1920use bitcoin:: secp256k1:: PublicKey ;
2021
2122use alloc:: vec:: Vec ;
23+ use alloc:: collections:: VecDeque ;
2224
2325use core:: ops:: Deref ;
2426use core:: str:: FromStr ;
@@ -48,6 +50,40 @@ pub const LSPS2_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE: &str = "lsps2_service";
4850/// [`LSPS5ServiceHandler`]: crate::lsps5::service::LSPS5ServiceHandler
4951pub const LSPS5_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE : & str = "lsps5_service" ;
5052
53+ pub ( crate ) async fn read_event_queue < K : Deref > (
54+ kv_store : K ,
55+ ) -> Result < Option < VecDeque < LiquidityEvent > > , lightning:: io:: Error >
56+ where
57+ K :: Target : KVStore ,
58+ {
59+ let read_fut = kv_store. read (
60+ LIQUIDITY_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE ,
61+ LIQUIDITY_MANAGER_EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE ,
62+ LIQUIDITY_MANAGER_EVENT_QUEUE_PERSISTENCE_KEY ,
63+ ) ;
64+
65+ let mut reader = match read_fut. await {
66+ Ok ( r) => Cursor :: new ( r) ,
67+ Err ( e) => {
68+ if e. kind ( ) == lightning:: io:: ErrorKind :: NotFound {
69+ // Key wasn't found, no error but first time running.
70+ return Ok ( None ) ;
71+ } else {
72+ return Err ( e) ;
73+ }
74+ } ,
75+ } ;
76+
77+ let queue: EventQueueDeserWrapper = Readable :: read ( & mut reader) . map_err ( |_| {
78+ lightning:: io:: Error :: new (
79+ lightning:: io:: ErrorKind :: InvalidData ,
80+ "Failed to deserialize liquidity event queue" ,
81+ )
82+ } ) ?;
83+
84+ Ok ( Some ( queue. 0 ) )
85+ }
86+
5187pub ( crate ) async fn read_lsps2_service_peer_states < K : Deref > (
5288 kv_store : K ,
5389) -> Result < Vec < ( PublicKey , LSPS2ServicePeerState ) > , lightning:: io:: Error >
0 commit comments