1
- //! The events emitted by [`LdkLite`] live here.
2
- //!
3
- //! [`LdkLite`]: [`crate::LdkLite`]
4
-
5
1
use crate :: {
6
2
hex_utils, ChannelManager , Error , FilesystemPersister , LdkLiteChainAccess , LdkLiteConfig ,
7
3
NetworkGraph , PaymentInfo , PaymentInfoStorage , PaymentStatus ,
@@ -39,44 +35,62 @@ pub(crate) const EVENTS_PERSISTENCE_KEY: &str = "events";
39
35
#[ derive( Debug , Clone ) ]
40
36
pub enum LdkLiteEvent {
41
37
/// A payment we sent was successful.
42
- PaymentSuccessful ( PaymentSuccessfulEvent ) ,
38
+ PaymentSuccessful {
39
+ /// The hash of the payment.
40
+ payment_hash : PaymentHash ,
41
+ } ,
43
42
/// A payment we sent has failed.
44
- PaymentFailed ( PaymentFailedEvent ) ,
43
+ PaymentFailed {
44
+ /// The hash of the payment.
45
+ payment_hash : PaymentHash ,
46
+ } ,
45
47
/// A payment has been received.
46
- PaymentReceived ( PaymentReceivedEvent ) ,
48
+ PaymentReceived {
49
+ /// The hash of the payment.
50
+ payment_hash : PaymentHash ,
51
+ /// The value, in thousandths of a satoshi that has been received.
52
+ amount_msat : u64 ,
53
+ } ,
47
54
// TODO: Implement after a corresponding LDK event is added.
48
- //ChannelOpened(ChannelOpenedEvent),
55
+ //ChannelOpened {
56
+ //},
49
57
/// A channel has been closed.
50
- ChannelClosed ( ChannelClosedEvent ) ,
58
+ ChannelClosed {
59
+ /// The channel_id of the channel which has been closed.
60
+ channel_id : [ u8 ; 32 ] ,
61
+ } ,
51
62
// TODO: Implement on-chain events when better integrating with BDK wallet sync.
52
- //OnChainPaymentSent(OnChainPaymentSentEvent),
53
- //OnChainPaymentReceived(OnChainPaymentReceivedEvent),
54
- }
55
-
56
- trait EventType {
57
- const TYPE : u8 ;
58
- }
59
-
60
- fn write_event < T : EventType + Writeable , W : Writer > (
61
- event : & T , writer : & mut W ,
62
- ) -> Result < ( ) , lightning:: io:: Error > {
63
- T :: TYPE . write ( writer) ?;
64
- event. write ( writer) ?;
65
- Ok ( ( ) )
63
+ //OnChainPaymentSent {
64
+ //},
65
+ //OnChainPaymentReceived {
66
+ //}
66
67
}
67
68
68
69
impl Readable for LdkLiteEvent {
69
70
fn read < R : lightning:: io:: Read > (
70
71
reader : & mut R ,
71
72
) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
72
73
match Readable :: read ( reader) ? {
73
- PaymentSuccessfulEvent :: TYPE => Ok ( Self :: PaymentSuccessful ( Readable :: read ( reader) ?) ) ,
74
- PaymentFailedEvent :: TYPE => Ok ( Self :: PaymentFailed ( Readable :: read ( reader) ?) ) ,
75
- PaymentReceivedEvent :: TYPE => Ok ( Self :: PaymentReceived ( Readable :: read ( reader) ?) ) ,
76
- // ChannelOpenedEvent::TYPE => {
74
+ 0u8 => {
75
+ let payment_hash: PaymentHash = Readable :: read ( reader) ?;
76
+ Ok ( Self :: PaymentSuccessful { payment_hash } )
77
+ }
78
+ 1u8 => {
79
+ let payment_hash: PaymentHash = Readable :: read ( reader) ?;
80
+ Ok ( Self :: PaymentFailed { payment_hash } )
81
+ }
82
+ 2u8 => {
83
+ let payment_hash: PaymentHash = Readable :: read ( reader) ?;
84
+ let amount_msat: u64 = Readable :: read ( reader) ?;
85
+ Ok ( Self :: PaymentReceived { payment_hash, amount_msat } )
86
+ }
87
+ //3u8 => {
77
88
// TODO ChannelOpened
78
89
//}
79
- ChannelClosedEvent :: TYPE => Ok ( Self :: ChannelClosed ( Readable :: read ( reader) ?) ) ,
90
+ 4u8 => {
91
+ let channel_id: [ u8 ; 32 ] = Readable :: read ( reader) ?;
92
+ Ok ( Self :: ChannelClosed { channel_id } )
93
+ }
80
94
//5u8 => {
81
95
// TODO OnChainPaymentSent
82
96
//}
@@ -91,126 +105,36 @@ impl Readable for LdkLiteEvent {
91
105
impl Writeable for LdkLiteEvent {
92
106
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
93
107
match self {
94
- Self :: PaymentSuccessful ( event) => write_event ( event, writer) ?,
95
- Self :: PaymentFailed ( event) => write_event ( event, writer) ?,
96
- Self :: PaymentReceived ( event) => write_event ( event, writer) ?,
97
- Self :: ChannelClosed ( event) => write_event ( event, writer) ?,
108
+ Self :: PaymentSuccessful { payment_hash } => {
109
+ 0u8 . write ( writer) ?;
110
+ payment_hash. write ( writer) ?;
111
+ Ok ( ( ) )
112
+ }
113
+ Self :: PaymentFailed { payment_hash } => {
114
+ 1u8 . write ( writer) ?;
115
+ payment_hash. write ( writer) ?;
116
+ Ok ( ( ) )
117
+ }
118
+ Self :: PaymentReceived { payment_hash, amount_msat } => {
119
+ 2u8 . write ( writer) ?;
120
+ payment_hash. write ( writer) ?;
121
+ amount_msat. write ( writer) ?;
122
+ Ok ( ( ) )
123
+ }
124
+ //Self::ChannelOpened { .. } => {
125
+ //TODO
126
+ //}
127
+ Self :: ChannelClosed { channel_id } => {
128
+ 4u8 . write ( writer) ?;
129
+ channel_id. write ( writer) ?;
130
+ Ok ( ( ) )
131
+ } //Self::OnChainPaymentSent { .. } => {
132
+ //TODO
133
+ //}
134
+ //Self::OnChainPaymentReceived { .. } => {
135
+ //TODO
136
+ //}
98
137
}
99
- Ok ( ( ) )
100
- }
101
- }
102
-
103
- /// A payment we sent was successful.
104
- #[ derive( Debug , Clone ) ]
105
- pub struct PaymentSuccessfulEvent {
106
- /// The hash of the payment.
107
- pub payment_hash : PaymentHash ,
108
- }
109
-
110
- impl EventType for PaymentSuccessfulEvent {
111
- const TYPE : u8 = 0u8 ;
112
- }
113
-
114
- impl Readable for PaymentSuccessfulEvent {
115
- fn read < R : lightning:: io:: Read > (
116
- reader : & mut R ,
117
- ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
118
- let payment_hash: PaymentHash = Readable :: read ( reader) ?;
119
- Ok ( Self { payment_hash } )
120
- }
121
- }
122
-
123
- impl Writeable for PaymentSuccessfulEvent {
124
- fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
125
- self . payment_hash . write ( writer) ?;
126
- Ok ( ( ) )
127
- }
128
- }
129
-
130
- /// A payment we sent has failed.
131
- #[ derive( Debug , Clone ) ]
132
- pub struct PaymentFailedEvent {
133
- /// The hash of the payment.
134
- pub payment_hash : PaymentHash ,
135
- }
136
-
137
- impl EventType for PaymentFailedEvent {
138
- const TYPE : u8 = 1u8 ;
139
- }
140
-
141
- impl Readable for PaymentFailedEvent {
142
- fn read < R : lightning:: io:: Read > (
143
- reader : & mut R ,
144
- ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
145
- let payment_hash: PaymentHash = Readable :: read ( reader) ?;
146
- Ok ( Self { payment_hash } )
147
- }
148
- }
149
-
150
- impl Writeable for PaymentFailedEvent {
151
- fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
152
- self . payment_hash . write ( writer) ?;
153
- Ok ( ( ) )
154
- }
155
- }
156
-
157
- /// A payment has been received.
158
- #[ derive( Debug , Clone ) ]
159
- pub struct PaymentReceivedEvent {
160
- /// The hash of the payment.
161
- pub payment_hash : PaymentHash ,
162
- /// The value, in thousandths of a satoshi that has been received.
163
- pub amount_msat : u64 ,
164
- }
165
-
166
- impl EventType for PaymentReceivedEvent {
167
- const TYPE : u8 = 2u8 ;
168
- }
169
-
170
- impl Readable for PaymentReceivedEvent {
171
- fn read < R : lightning:: io:: Read > (
172
- reader : & mut R ,
173
- ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
174
- let payment_hash: PaymentHash = Readable :: read ( reader) ?;
175
- let amount_msat: u64 = Readable :: read ( reader) ?;
176
- Ok ( Self { payment_hash, amount_msat } )
177
- }
178
- }
179
-
180
- impl Writeable for PaymentReceivedEvent {
181
- fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
182
- Self :: TYPE . write ( writer) ?;
183
- self . payment_hash . write ( writer) ?;
184
- self . amount_msat . write ( writer) ?;
185
- Ok ( ( ) )
186
- }
187
- }
188
-
189
- /// A channel has been closed.
190
- #[ derive( Debug , Clone ) ]
191
- pub struct ChannelClosedEvent {
192
- /// The channel_id of the channel which has been closed.
193
- pub channel_id : [ u8 ; 32 ] ,
194
- }
195
-
196
- impl EventType for ChannelClosedEvent {
197
- const TYPE : u8 = 4u8 ;
198
- }
199
-
200
- impl Readable for ChannelClosedEvent {
201
- fn read < R : lightning:: io:: Read > (
202
- reader : & mut R ,
203
- ) -> Result < Self , lightning:: ln:: msgs:: DecodeError > {
204
- let channel_id: [ u8 ; 32 ] = Readable :: read ( reader) ?;
205
- Ok ( Self { channel_id } )
206
- }
207
- }
208
-
209
- impl Writeable for ChannelClosedEvent {
210
- fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , lightning:: io:: Error > {
211
- Self :: TYPE . write ( writer) ?;
212
- self . channel_id . write ( writer) ?;
213
- Ok ( ( ) )
214
138
}
215
139
}
216
140
@@ -378,10 +302,10 @@ impl LdkEventHandler for LdkLiteEventHandler {
378
302
} ;
379
303
self . channel_manager . claim_funds ( payment_preimage. unwrap ( ) ) ;
380
304
self . event_queue
381
- . add_event ( LdkLiteEvent :: PaymentReceived ( PaymentReceivedEvent {
305
+ . add_event ( LdkLiteEvent :: PaymentReceived {
382
306
payment_hash : * payment_hash,
383
307
amount_msat : * amount_msat,
384
- } ) )
308
+ } )
385
309
. unwrap ( ) ;
386
310
}
387
311
LdkEvent :: PaymentClaimed { payment_hash, purpose, amount_msat } => {
@@ -446,9 +370,9 @@ impl LdkEventHandler for LdkLiteEventHandler {
446
370
}
447
371
}
448
372
self . event_queue
449
- . add_event ( LdkLiteEvent :: PaymentSuccessful ( PaymentSuccessfulEvent {
373
+ . add_event ( LdkLiteEvent :: PaymentSuccessful {
450
374
payment_hash : * payment_hash,
451
- } ) )
375
+ } )
452
376
. unwrap ( ) ;
453
377
}
454
378
LdkEvent :: PaymentFailed { payment_hash, .. } => {
@@ -464,9 +388,9 @@ impl LdkEventHandler for LdkLiteEventHandler {
464
388
payment. status = PaymentStatus :: Failed ;
465
389
}
466
390
self . event_queue
467
- . add_event ( LdkLiteEvent :: PaymentFailed ( PaymentFailedEvent {
391
+ . add_event ( LdkLiteEvent :: PaymentFailed {
468
392
payment_hash : * payment_hash,
469
- } ) )
393
+ } )
470
394
. unwrap ( ) ;
471
395
}
472
396
@@ -578,9 +502,9 @@ impl LdkEventHandler for LdkLiteEventHandler {
578
502
reason
579
503
) ;
580
504
self . event_queue
581
- . add_event ( LdkLiteEvent :: ChannelClosed ( ChannelClosedEvent {
505
+ . add_event ( LdkLiteEvent :: ChannelClosed {
582
506
channel_id : * channel_id,
583
- } ) )
507
+ } )
584
508
. unwrap ( ) ;
585
509
}
586
510
LdkEvent :: DiscardFunding { .. } => { }
0 commit comments