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
@@ -19,6 +20,7 @@ use lightning::util::ser::Readable;
19
20
use bitcoin:: secp256k1:: PublicKey ;
20
21
21
22
use alloc:: vec:: Vec ;
23
+ use alloc:: collections:: VecDeque ;
22
24
23
25
use core:: ops:: Deref ;
24
26
use core:: str:: FromStr ;
@@ -48,6 +50,40 @@ pub const LSPS2_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE: &str = "lsps2_service";
48
50
/// [`LSPS5ServiceHandler`]: crate::lsps5::service::LSPS5ServiceHandler
49
51
pub const LSPS5_SERVICE_PERSISTENCE_SECONDARY_NAMESPACE : & str = "lsps5_service" ;
50
52
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
+
51
87
pub ( crate ) async fn read_lsps2_service_peer_states < K : Deref > (
52
88
kv_store : K ,
53
89
) -> Result < Vec < ( PublicKey , LSPS2ServicePeerState ) > , lightning:: io:: Error >
0 commit comments