1
+ //! The events emitted by [`LdkLite`] live here.
2
+ //!
3
+ //! [`LdkLite`]: [`crate::LdkLite`]
4
+
1
5
use crate :: {
2
6
hex_utils, ChannelManager , Error , FilesystemPersister , LdkLiteChainAccess , LdkLiteConfig ,
3
7
NetworkGraph , PaymentInfo , PaymentInfoStorage , PaymentStatus ,
@@ -27,79 +31,45 @@ use std::time::Duration;
27
31
/// The event queue will be persisted under this key.
28
32
pub ( crate ) const EVENTS_PERSISTENCE_KEY : & str = "events" ;
29
33
30
- /// An event emitted by [`LdkLite`] that should be handled by the user. This will more often than
31
- /// not wrap a LDK event accessible via the `inner` field.
34
+ /// An event emitted by [`LdkLite`] that should be handled by the user.
35
+ ///
36
+ /// This wraps an event of a specific type and will more often than not wrap a LDK event accessible
37
+ /// via the `inner` field.
38
+ ///
39
+ /// [`LdkLite`]: [`crate::LdkLite`]
32
40
#[ derive( Debug , Clone ) ]
33
41
pub enum LdkLiteEvent {
34
42
/// A payment we sent was successful.
35
- PaymentSuccessful {
36
- /// The hash of the payment.
37
- payment_hash : PaymentHash ,
38
- /// The wrapped LDK event.
39
- inner : ldk_events:: Event ,
40
- } ,
43
+ PaymentSuccessful ( PaymentSuccessfulEvent ) ,
41
44
/// A payment we sent has failed.
42
- PaymentFailed {
43
- /// The hash of the payment.
44
- payment_hash : PaymentHash ,
45
- /// The wrapped LDK event.
46
- inner : ldk_events:: Event ,
47
- } ,
45
+ PaymentFailed ( PaymentFailedEvent ) ,
48
46
/// A payment has been received.
49
- PaymentReceived {
50
- /// The hash of the payment.
51
- payment_hash : PaymentHash ,
52
- /// The value, in thousandths of a satoshi that has been received.
53
- amount_msat : u64 ,
54
- /// The wrapped LDK event.
55
- inner : ldk_events:: Event ,
56
- } ,
47
+ PaymentReceived ( PaymentReceivedEvent ) ,
57
48
// TODO: Implement after a corresponding LDK event is added.
58
- //ChannelOpened {
59
- //},
49
+ //ChannelOpened(ChannelOpenedEvent),
60
50
/// A channel has been closed.
61
- ChannelClosed {
62
- /// The channel_id of the channel which has been closed.
63
- channel_id : [ u8 ; 32 ] ,
64
- /// The wrapped LDK event.
65
- inner : ldk_events:: Event ,
66
- } ,
51
+ ChannelClosed ( ChannelClosedEvent ) ,
67
52
// TODO: Implement on-chain events when better integrating with BDK wallet sync.
68
- //OnChainPaymentSent {
69
- //},
70
- //OnChainPaymentReceived {
71
- //}
53
+ //OnChainPaymentSent(OnChainPaymentSentEvent),
54
+ //OnChainPaymentReceived(OnChainPaymentReceivedEvent),
55
+ }
56
+
57
+ trait EventType {
58
+ const TYPE : u8 ;
72
59
}
73
60
74
61
impl Readable for LdkLiteEvent {
75
62
fn read < R : lightning:: io:: Read > (
76
63
reader : & mut R ,
77
64
) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
78
65
match Readable :: read ( reader) ? {
79
- 0u8 => {
80
- let payment_hash: PaymentHash = Readable :: read ( reader) ?;
81
- let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
82
- Ok ( Self :: PaymentSuccessful { payment_hash, inner } )
83
- }
84
- 1u8 => {
85
- let payment_hash: PaymentHash = Readable :: read ( reader) ?;
86
- let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
87
- Ok ( Self :: PaymentFailed { payment_hash, inner } )
88
- }
89
- 2u8 => {
90
- let payment_hash: PaymentHash = Readable :: read ( reader) ?;
91
- let amount_msat: u64 = Readable :: read ( reader) ?;
92
- let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
93
- Ok ( Self :: PaymentReceived { payment_hash, amount_msat, inner } )
94
- }
95
- //3u8 => {
66
+ PaymentSuccessfulEvent :: TYPE => Ok ( Self :: PaymentSuccessful ( Readable :: read ( reader) ?) ) ,
67
+ PaymentFailedEvent :: TYPE => Ok ( Self :: PaymentFailed ( Readable :: read ( reader) ?) ) ,
68
+ PaymentReceivedEvent :: TYPE => Ok ( Self :: PaymentReceived ( Readable :: read ( reader) ?) ) ,
69
+ // ChannelOpenedEvent::TYPE => {
96
70
// TODO ChannelOpened
97
71
//}
98
- 4u8 => {
99
- let channel_id: [ u8 ; 32 ] = Readable :: read ( reader) ?;
100
- let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
101
- Ok ( Self :: ChannelClosed { channel_id, inner } )
102
- }
72
+ ChannelClosedEvent :: TYPE => Ok ( Self :: ChannelClosed ( Readable :: read ( reader) ?) ) ,
103
73
//5u8 => {
104
74
// TODO OnChainPaymentSent
105
75
//}
@@ -114,32 +84,24 @@ impl Readable for LdkLiteEvent {
114
84
impl Writeable for LdkLiteEvent {
115
85
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
116
86
match self {
117
- Self :: PaymentSuccessful { payment_hash, inner } => {
118
- 0u8 . write ( writer) ?;
119
- payment_hash. write ( writer) ?;
120
- inner. write ( writer) ?;
87
+ Self :: PaymentSuccessful ( event) => {
88
+ event. write ( writer) ?;
121
89
Ok ( ( ) )
122
90
}
123
- Self :: PaymentFailed { payment_hash, inner } => {
124
- 1u8 . write ( writer) ?;
125
- payment_hash. write ( writer) ?;
126
- inner. write ( writer) ?;
91
+ Self :: PaymentFailed ( event) => {
92
+ PaymentFailedEvent :: TYPE . write ( writer) ?;
93
+ event. write ( writer) ?;
127
94
Ok ( ( ) )
128
95
}
129
- Self :: PaymentReceived { payment_hash, amount_msat, inner } => {
130
- 2u8 . write ( writer) ?;
131
- payment_hash. write ( writer) ?;
132
- amount_msat. write ( writer) ?;
133
- inner. write ( writer) ?;
96
+ Self :: PaymentReceived ( event) => {
97
+ event. write ( writer) ?;
134
98
Ok ( ( ) )
135
99
}
136
100
//Self::ChannelOpened { .. } => {
137
101
//TODO
138
102
//}
139
- Self :: ChannelClosed { channel_id, inner } => {
140
- 4u8 . write ( writer) ?;
141
- channel_id. write ( writer) ?;
142
- inner. write ( writer) ?;
103
+ Self :: ChannelClosed ( event) => {
104
+ event. write ( writer) ?;
143
105
Ok ( ( ) )
144
106
} //Self::OnChainPaymentSent { .. } => {
145
107
//TODO
@@ -151,6 +113,138 @@ impl Writeable for LdkLiteEvent {
151
113
}
152
114
}
153
115
116
+ /// A payment we sent was successful.
117
+ #[ derive( Debug , Clone ) ]
118
+ pub struct PaymentSuccessfulEvent {
119
+ /// The hash of the payment.
120
+ pub payment_hash : PaymentHash ,
121
+ /// The wrapped LDK event.
122
+ pub inner : ldk_events:: Event ,
123
+ }
124
+
125
+ impl EventType for PaymentSuccessfulEvent {
126
+ const TYPE : u8 = 0u8 ;
127
+ }
128
+
129
+ impl Readable for PaymentSuccessfulEvent {
130
+ fn read < R : lightning:: io:: Read > (
131
+ reader : & mut R ,
132
+ ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
133
+ let payment_hash: PaymentHash = Readable :: read ( reader) ?;
134
+ let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
135
+ Ok ( Self { payment_hash, inner } )
136
+ }
137
+ }
138
+
139
+ impl Writeable for PaymentSuccessfulEvent {
140
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
141
+ Self :: TYPE . write ( writer) ?;
142
+ self . payment_hash . write ( writer) ?;
143
+ self . inner . write ( writer) ?;
144
+ Ok ( ( ) )
145
+ }
146
+ }
147
+
148
+ /// A payment we sent has failed.
149
+ #[ derive( Debug , Clone ) ]
150
+ pub struct PaymentFailedEvent {
151
+ /// The hash of the payment.
152
+ pub payment_hash : PaymentHash ,
153
+ /// The wrapped LDK event.
154
+ pub inner : ldk_events:: Event ,
155
+ }
156
+
157
+ impl EventType for PaymentFailedEvent {
158
+ const TYPE : u8 = 1u8 ;
159
+ }
160
+
161
+ impl Readable for PaymentFailedEvent {
162
+ fn read < R : lightning:: io:: Read > (
163
+ reader : & mut R ,
164
+ ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
165
+ let payment_hash: PaymentHash = Readable :: read ( reader) ?;
166
+ let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
167
+ Ok ( Self { payment_hash, inner } )
168
+ }
169
+ }
170
+
171
+ impl Writeable for PaymentFailedEvent {
172
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
173
+ Self :: TYPE . write ( writer) ?;
174
+ self . payment_hash . write ( writer) ?;
175
+ self . inner . write ( writer) ?;
176
+ Ok ( ( ) )
177
+ }
178
+ }
179
+
180
+ /// A payment has been received.
181
+ #[ derive( Debug , Clone ) ]
182
+ pub struct PaymentReceivedEvent {
183
+ /// The hash of the payment.
184
+ pub payment_hash : PaymentHash ,
185
+ /// The value, in thousandths of a satoshi that has been received.
186
+ pub amount_msat : u64 ,
187
+ /// The wrapped LDK event.
188
+ pub inner : ldk_events:: Event ,
189
+ }
190
+
191
+ impl EventType for PaymentReceivedEvent {
192
+ const TYPE : u8 = 2u8 ;
193
+ }
194
+
195
+ impl Readable for PaymentReceivedEvent {
196
+ fn read < R : lightning:: io:: Read > (
197
+ reader : & mut R ,
198
+ ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
199
+ let payment_hash: PaymentHash = Readable :: read ( reader) ?;
200
+ let amount_msat: u64 = Readable :: read ( reader) ?;
201
+ let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
202
+ Ok ( Self { payment_hash, amount_msat, inner } )
203
+ }
204
+ }
205
+
206
+ impl Writeable for PaymentReceivedEvent {
207
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
208
+ Self :: TYPE . write ( writer) ?;
209
+ self . payment_hash . write ( writer) ?;
210
+ self . amount_msat . write ( writer) ?;
211
+ self . inner . write ( writer) ?;
212
+ Ok ( ( ) )
213
+ }
214
+ }
215
+
216
+ /// A channel has been closed.
217
+ #[ derive( Debug , Clone ) ]
218
+ pub struct ChannelClosedEvent {
219
+ /// The channel_id of the channel which has been closed.
220
+ pub channel_id : [ u8 ; 32 ] ,
221
+ /// The wrapped LDK event.
222
+ pub inner : ldk_events:: Event ,
223
+ }
224
+
225
+ impl EventType for ChannelClosedEvent {
226
+ const TYPE : u8 = 4u8 ;
227
+ }
228
+
229
+ impl Readable for ChannelClosedEvent {
230
+ fn read < R : lightning:: io:: Read > (
231
+ reader : & mut R ,
232
+ ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
233
+ let channel_id: [ u8 ; 32 ] = Readable :: read ( reader) ?;
234
+ let inner: ldk_events:: Event = MaybeReadable :: read ( reader) ?. unwrap ( ) ;
235
+ Ok ( Self { channel_id, inner } )
236
+ }
237
+ }
238
+
239
+ impl Writeable for ChannelClosedEvent {
240
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
241
+ Self :: TYPE . write ( writer) ?;
242
+ self . channel_id . write ( writer) ?;
243
+ self . inner . write ( writer) ?;
244
+ Ok ( ( ) )
245
+ }
246
+ }
247
+
154
248
pub ( crate ) struct LdkLiteEventQueue < K : KVStorePersister > {
155
249
queue : Mutex < EventQueueSerWrapper > ,
156
250
notifier : Condvar ,
@@ -315,11 +409,11 @@ impl ldk_events::EventHandler for LdkLiteEventHandler {
315
409
} ;
316
410
self . channel_manager . claim_funds ( payment_preimage. unwrap ( ) ) ;
317
411
self . event_queue
318
- . add_event ( LdkLiteEvent :: PaymentReceived {
412
+ . add_event ( LdkLiteEvent :: PaymentReceived ( PaymentReceivedEvent {
319
413
payment_hash : * payment_hash,
320
414
amount_msat : * amount_msat,
321
415
inner : event. clone ( ) ,
322
- } )
416
+ } ) )
323
417
. unwrap ( ) ;
324
418
}
325
419
ldk_events:: Event :: PaymentClaimed { payment_hash, purpose, amount_msat } => {
@@ -384,10 +478,10 @@ impl ldk_events::EventHandler for LdkLiteEventHandler {
384
478
}
385
479
}
386
480
self . event_queue
387
- . add_event ( LdkLiteEvent :: PaymentSuccessful {
481
+ . add_event ( LdkLiteEvent :: PaymentSuccessful ( PaymentSuccessfulEvent {
388
482
payment_hash : * payment_hash,
389
483
inner : event. clone ( ) ,
390
- } )
484
+ } ) )
391
485
. unwrap ( ) ;
392
486
}
393
487
ldk_events:: Event :: PaymentFailed { payment_hash, .. } => {
@@ -403,10 +497,10 @@ impl ldk_events::EventHandler for LdkLiteEventHandler {
403
497
payment. status = PaymentStatus :: Failed ;
404
498
}
405
499
self . event_queue
406
- . add_event ( LdkLiteEvent :: PaymentFailed {
500
+ . add_event ( LdkLiteEvent :: PaymentFailed ( PaymentFailedEvent {
407
501
payment_hash : * payment_hash,
408
502
inner : event. clone ( ) ,
409
- } )
503
+ } ) )
410
504
. unwrap ( ) ;
411
505
}
412
506
@@ -518,10 +612,10 @@ impl ldk_events::EventHandler for LdkLiteEventHandler {
518
612
reason
519
613
) ;
520
614
self . event_queue
521
- . add_event ( LdkLiteEvent :: ChannelClosed {
615
+ . add_event ( LdkLiteEvent :: ChannelClosed ( ChannelClosedEvent {
522
616
channel_id : * channel_id,
523
617
inner : event. clone ( ) ,
524
- } )
618
+ } ) )
525
619
. unwrap ( ) ;
526
620
}
527
621
ldk_events:: Event :: DiscardFunding { .. } => { }
0 commit comments