9
9
10
10
//! Types and utils for persistence.
11
11
12
+ use crate :: events:: { EventQueueDeserWrapper , LiquidityEvent } ;
12
13
use crate :: lsps2:: service:: PeerState as LSPS2ServicePeerState ;
13
14
use crate :: lsps5:: service:: PeerState as LSPS5ServicePeerState ;
14
15
@@ -20,6 +21,7 @@ use bitcoin::secp256k1::PublicKey;
20
21
21
22
use core:: ops:: Deref ;
22
23
use core:: str:: FromStr ;
24
+ use std:: collections:: VecDeque ;
23
25
24
26
/// The primary namespace under which the [`LiquidityManager`] will be persisted.
25
27
///
@@ -46,6 +48,40 @@ pub const LSPS2_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE: &str = "lsps2_service";
46
48
/// [`LSPS5ServiceHandler`]: crate::lsps5::service::LSPS5ServiceHandler
47
49
pub const LSPS5_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE : & str = "lsps5_service" ;
48
50
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
+
49
85
pub ( crate ) async fn read_lsps2_service_peer_states < K : Deref > (
50
86
kv_store : K ,
51
87
) -> Result < Vec < ( PublicKey , LSPS2ServicePeerState ) > , lightning:: io:: Error >
0 commit comments