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