Update Event::PaymentReceived docs since we require payment secret
[rust-lightning] / lightning / src / util / events.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Events are returned from various bits in the library which indicate some action must be taken
11 //! by the client.
12 //!
13 //! Because we don't have a built-in runtime, it's up to the client to call events at a time in the
14 //! future, as well as generate and broadcast funding transactions handle payment preimages and a
15 //! few other things.
16
17 use chain::keysinterface::SpendableOutputDescriptor;
18 use ln::msgs;
19 use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
20 use routing::network_graph::NetworkUpdate;
21 use util::ser::{Writeable, Writer, MaybeReadable, Readable, VecReadWrapper, VecWriteWrapper};
22
23 use bitcoin::blockdata::script::Script;
24
25 use bitcoin::secp256k1::key::PublicKey;
26
27 use io;
28 use prelude::*;
29 use core::time::Duration;
30 use core::ops::Deref;
31
32 /// Some information provided on receipt of payment depends on whether the payment received is a
33 /// spontaneous payment or a "conventional" lightning payment that's paying an invoice.
34 #[derive(Clone, Debug)]
35 pub enum PaymentPurpose {
36         /// Information for receiving a payment that we generated an invoice for.
37         InvoicePayment {
38                 /// The preimage to the payment_hash, if the payment hash (and secret) were fetched via
39                 /// [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
40                 /// [`ChannelManager::claim_funds`].
41                 ///
42                 /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
43                 /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
44                 payment_preimage: Option<PaymentPreimage>,
45                 /// The "payment secret". This authenticates the sender to the recipient, preventing a
46                 /// number of deanonymization attacks during the routing process.
47                 /// It is provided here for your reference, however its accuracy is enforced directly by
48                 /// [`ChannelManager`] using the values you previously provided to
49                 /// [`ChannelManager::create_inbound_payment`] or
50                 /// [`ChannelManager::create_inbound_payment_for_hash`].
51                 ///
52                 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
53                 /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
54                 /// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
55                 payment_secret: PaymentSecret,
56                 /// This is the `user_payment_id` which was provided to
57                 /// [`ChannelManager::create_inbound_payment_for_hash`] or
58                 /// [`ChannelManager::create_inbound_payment`]. It has no meaning inside of LDK and is
59                 /// simply copied here. It may be used to correlate PaymentReceived events with invoice
60                 /// metadata stored elsewhere.
61                 ///
62                 /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
63                 /// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
64                 user_payment_id: u64,
65         },
66         /// Because this is a spontaneous payment, the payer generated their own preimage rather than us
67         /// (the payee) providing a preimage.
68         SpontaneousPayment(PaymentPreimage),
69 }
70
71 /// An Event which you should probably take some action in response to.
72 ///
73 /// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
74 /// them directly as they don't round-trip exactly (for example FundingGenerationReady is never
75 /// written as it makes no sense to respond to it after reconnecting to peers).
76 #[derive(Clone, Debug)]
77 pub enum Event {
78         /// Used to indicate that the client should generate a funding transaction with the given
79         /// parameters and then call ChannelManager::funding_transaction_generated.
80         /// Generated in ChannelManager message handling.
81         /// Note that *all inputs* in the funding transaction must spend SegWit outputs or your
82         /// counterparty can steal your funds!
83         FundingGenerationReady {
84                 /// The random channel_id we picked which you'll need to pass into
85                 /// ChannelManager::funding_transaction_generated.
86                 temporary_channel_id: [u8; 32],
87                 /// The value, in satoshis, that the output should have.
88                 channel_value_satoshis: u64,
89                 /// The script which should be used in the transaction output.
90                 output_script: Script,
91                 /// The value passed in to ChannelManager::create_channel
92                 user_channel_id: u64,
93         },
94         /// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
95         /// [`ChannelManager::claim_funds`] to get it....
96         /// Note that if the preimage is not known, you should call
97         /// [`ChannelManager::fail_htlc_backwards`] to free up resources for this HTLC and avoid
98         /// network congestion.
99         /// If you fail to call either [`ChannelManager::claim_funds`] or
100         /// [`ChannelManager::fail_htlc_backwards`] within the HTLC's timeout, the HTLC will be
101         /// automatically failed.
102         ///
103         /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
104         /// [`ChannelManager::fail_htlc_backwards`]: crate::ln::channelmanager::ChannelManager::fail_htlc_backwards
105         PaymentReceived {
106                 /// The hash for which the preimage should be handed to the ChannelManager.
107                 payment_hash: PaymentHash,
108                 /// The value, in thousandths of a satoshi, that this payment is for.
109                 amt: u64,
110                 /// Information for claiming this received payment, based on whether the purpose of the
111                 /// payment is to pay an invoice or to send a spontaneous payment.
112                 purpose: PaymentPurpose,
113         },
114         /// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
115         /// and we got back the payment preimage for it).
116         PaymentSent {
117                 /// The preimage to the hash given to ChannelManager::send_payment.
118                 /// Note that this serves as a payment receipt, if you wish to have such a thing, you must
119                 /// store it somehow!
120                 payment_preimage: PaymentPreimage,
121         },
122         /// Indicates an outbound payment we made failed. Probably some intermediary node dropped
123         /// something. You may wish to retry with a different route.
124         PaymentFailed {
125                 /// The hash which was given to ChannelManager::send_payment.
126                 payment_hash: PaymentHash,
127                 /// Indicates the payment was rejected for some reason by the recipient. This implies that
128                 /// the payment has failed, not just the route in question. If this is not set, you may
129                 /// retry the payment via a different route.
130                 rejected_by_dest: bool,
131                 /// Any failure information conveyed via the Onion return packet by a node along the failed
132                 /// payment route.
133                 ///
134                 /// Should be applied to the [`NetworkGraph`] so that routing decisions can take into
135                 /// account the update. [`NetGraphMsgHandler`] is capable of doing this.
136                 ///
137                 /// [`NetworkGraph`]: crate::routing::network_graph::NetworkGraph
138                 /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
139                 network_update: Option<NetworkUpdate>,
140 #[cfg(test)]
141                 error_code: Option<u16>,
142 #[cfg(test)]
143                 error_data: Option<Vec<u8>>,
144         },
145         /// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
146         /// time in the future.
147         PendingHTLCsForwardable {
148                 /// The minimum amount of time that should be waited prior to calling
149                 /// process_pending_htlc_forwards. To increase the effort required to correlate payments,
150                 /// you should wait a random amount of time in roughly the range (now + time_forwardable,
151                 /// now + 5*time_forwardable).
152                 time_forwardable: Duration,
153         },
154         /// Used to indicate that an output which you should know how to spend was confirmed on chain
155         /// and is now spendable.
156         /// Such an output will *not* ever be spent by rust-lightning, and are not at risk of your
157         /// counterparty spending them due to some kind of timeout. Thus, you need to store them
158         /// somewhere and spend them when you create on-chain transactions.
159         SpendableOutputs {
160                 /// The outputs which you should store as spendable by you.
161                 outputs: Vec<SpendableOutputDescriptor>,
162         },
163         /// This event is generated when a payment has been successfully forwarded through us and a
164         /// forwarding fee earned.
165         PaymentForwarded {
166                 /// The fee, in milli-satoshis, which was earned as a result of the payment.
167                 ///
168                 /// Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
169                 /// was pending, the amount the next hop claimed will have been rounded down to the nearest
170                 /// whole satoshi. Thus, the fee calculated here may be higher than expected as we still
171                 /// claimed the full value in millisatoshis from the source. In this case,
172                 /// `claim_from_onchain_tx` will be set.
173                 ///
174                 /// If the channel which sent us the payment has been force-closed, we will claim the funds
175                 /// via an on-chain transaction. In that case we do not yet know the on-chain transaction
176                 /// fees which we will spend and will instead set this to `None`. It is possible duplicate
177                 /// `PaymentForwarded` events are generated for the same payment iff `fee_earned_msat` is
178                 /// `None`.
179                 fee_earned_msat: Option<u64>,
180                 /// If this is `true`, the forwarded HTLC was claimed by our counterparty via an on-chain
181                 /// transaction.
182                 claim_from_onchain_tx: bool,
183         },
184 }
185
186 impl Writeable for Event {
187         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
188                 match self {
189                         &Event::FundingGenerationReady { .. } => {
190                                 0u8.write(writer)?;
191                                 // We never write out FundingGenerationReady events as, upon disconnection, peers
192                                 // drop any channels which have not yet exchanged funding_signed.
193                         },
194                         &Event::PaymentReceived { ref payment_hash, ref amt, ref purpose } => {
195                                 1u8.write(writer)?;
196                                 let mut payment_secret = None;
197                                 let mut user_payment_id = None;
198                                 let payment_preimage;
199                                 match &purpose {
200                                         PaymentPurpose::InvoicePayment { payment_preimage: preimage, payment_secret: secret, user_payment_id: id } => {
201                                                 payment_secret = Some(secret);
202                                                 payment_preimage = *preimage;
203                                                 user_payment_id = Some(id);
204                                         },
205                                         PaymentPurpose::SpontaneousPayment(preimage) => {
206                                                 payment_preimage = Some(*preimage);
207                                         }
208                                 }
209                                 write_tlv_fields!(writer, {
210                                         (0, payment_hash, required),
211                                         (2, payment_secret, option),
212                                         (4, amt, required),
213                                         (6, user_payment_id, option),
214                                         (8, payment_preimage, option),
215                                 });
216                         },
217                         &Event::PaymentSent { ref payment_preimage } => {
218                                 2u8.write(writer)?;
219                                 write_tlv_fields!(writer, {
220                                         (0, payment_preimage, required),
221                                 });
222                         },
223                         &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, ref network_update,
224                                 #[cfg(test)]
225                                 ref error_code,
226                                 #[cfg(test)]
227                                 ref error_data,
228                         } => {
229                                 3u8.write(writer)?;
230                                 #[cfg(test)]
231                                 error_code.write(writer)?;
232                                 #[cfg(test)]
233                                 error_data.write(writer)?;
234                                 write_tlv_fields!(writer, {
235                                         (0, payment_hash, required),
236                                         (1, network_update, option),
237                                         (2, rejected_by_dest, required),
238                                 });
239                         },
240                         &Event::PendingHTLCsForwardable { time_forwardable: _ } => {
241                                 4u8.write(writer)?;
242                                 write_tlv_fields!(writer, {});
243                                 // We don't write the time_fordwardable out at all, as we presume when the user
244                                 // deserializes us at least that much time has elapsed.
245                         },
246                         &Event::SpendableOutputs { ref outputs } => {
247                                 5u8.write(writer)?;
248                                 write_tlv_fields!(writer, {
249                                         (0, VecWriteWrapper(outputs), required),
250                                 });
251                         },
252                         &Event::PaymentForwarded { fee_earned_msat, claim_from_onchain_tx } => {
253                                 7u8.write(writer)?;
254                                 write_tlv_fields!(writer, {
255                                         (0, fee_earned_msat, option),
256                                         (2, claim_from_onchain_tx, required),
257                                 });
258                         },
259                 }
260                 Ok(())
261         }
262 }
263 impl MaybeReadable for Event {
264         fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, msgs::DecodeError> {
265                 match Readable::read(reader)? {
266                         0u8 => Ok(None),
267                         1u8 => {
268                                 let f = || {
269                                         let mut payment_hash = PaymentHash([0; 32]);
270                                         let mut payment_preimage = None;
271                                         let mut payment_secret = None;
272                                         let mut amt = 0;
273                                         let mut user_payment_id = None;
274                                         read_tlv_fields!(reader, {
275                                                 (0, payment_hash, required),
276                                                 (2, payment_secret, option),
277                                                 (4, amt, required),
278                                                 (6, user_payment_id, option),
279                                                 (8, payment_preimage, option),
280                                         });
281                                         let purpose = match payment_secret {
282                                                 Some(secret) => PaymentPurpose::InvoicePayment {
283                                                         payment_preimage,
284                                                         payment_secret: secret,
285                                                         user_payment_id: if let Some(id) = user_payment_id {
286                                                                 id
287                                                         } else { return Err(msgs::DecodeError::InvalidValue) }
288                                                 },
289                                                 None if payment_preimage.is_some() => PaymentPurpose::SpontaneousPayment(payment_preimage.unwrap()),
290                                                 None => return Err(msgs::DecodeError::InvalidValue),
291                                         };
292                                         Ok(Some(Event::PaymentReceived {
293                                                 payment_hash,
294                                                 amt,
295                                                 purpose,
296                                         }))
297                                 };
298                                 f()
299                         },
300                         2u8 => {
301                                 let f = || {
302                                         let mut payment_preimage = PaymentPreimage([0; 32]);
303                                         read_tlv_fields!(reader, {
304                                                 (0, payment_preimage, required),
305                                         });
306                                         Ok(Some(Event::PaymentSent {
307                                                 payment_preimage,
308                                         }))
309                                 };
310                                 f()
311                         },
312                         3u8 => {
313                                 let f = || {
314                                         #[cfg(test)]
315                                         let error_code = Readable::read(reader)?;
316                                         #[cfg(test)]
317                                         let error_data = Readable::read(reader)?;
318                                         let mut payment_hash = PaymentHash([0; 32]);
319                                         let mut rejected_by_dest = false;
320                                         let mut network_update = None;
321                                         read_tlv_fields!(reader, {
322                                                 (0, payment_hash, required),
323                                                 (1, network_update, ignorable),
324                                                 (2, rejected_by_dest, required),
325                                         });
326                                         Ok(Some(Event::PaymentFailed {
327                                                 payment_hash,
328                                                 rejected_by_dest,
329                                                 network_update,
330                                                 #[cfg(test)]
331                                                 error_code,
332                                                 #[cfg(test)]
333                                                 error_data,
334                                         }))
335                                 };
336                                 f()
337                         },
338                         4u8 => {
339                                 let f = || {
340                                         read_tlv_fields!(reader, {});
341                                         Ok(Some(Event::PendingHTLCsForwardable {
342                                                 time_forwardable: Duration::from_secs(0)
343                                         }))
344                                 };
345                                 f()
346                         },
347                         5u8 => {
348                                 let f = || {
349                                         let mut outputs = VecReadWrapper(Vec::new());
350                                         read_tlv_fields!(reader, {
351                                                 (0, outputs, required),
352                                         });
353                                         Ok(Some(Event::SpendableOutputs { outputs: outputs.0 }))
354                                 };
355                                 f()
356                         },
357                         7u8 => {
358                                 let f = || {
359                                         let mut fee_earned_msat = None;
360                                         let mut claim_from_onchain_tx = false;
361                                         read_tlv_fields!(reader, {
362                                                 (0, fee_earned_msat, option),
363                                                 (2, claim_from_onchain_tx, required),
364                                         });
365                                         Ok(Some(Event::PaymentForwarded { fee_earned_msat, claim_from_onchain_tx }))
366                                 };
367                                 f()
368                         },
369                         // Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
370                         x if x % 2 == 1 => Ok(None),
371                         _ => Err(msgs::DecodeError::InvalidValue)
372                 }
373         }
374 }
375
376 /// An event generated by ChannelManager which indicates a message should be sent to a peer (or
377 /// broadcast to most peers).
378 /// These events are handled by PeerManager::process_events if you are using a PeerManager.
379 #[derive(Clone, Debug)]
380 pub enum MessageSendEvent {
381         /// Used to indicate that we've accepted a channel open and should send the accept_channel
382         /// message provided to the given peer.
383         SendAcceptChannel {
384                 /// The node_id of the node which should receive this message
385                 node_id: PublicKey,
386                 /// The message which should be sent.
387                 msg: msgs::AcceptChannel,
388         },
389         /// Used to indicate that we've initiated a channel open and should send the open_channel
390         /// message provided to the given peer.
391         SendOpenChannel {
392                 /// The node_id of the node which should receive this message
393                 node_id: PublicKey,
394                 /// The message which should be sent.
395                 msg: msgs::OpenChannel,
396         },
397         /// Used to indicate that a funding_created message should be sent to the peer with the given node_id.
398         SendFundingCreated {
399                 /// The node_id of the node which should receive this message
400                 node_id: PublicKey,
401                 /// The message which should be sent.
402                 msg: msgs::FundingCreated,
403         },
404         /// Used to indicate that a funding_signed message should be sent to the peer with the given node_id.
405         SendFundingSigned {
406                 /// The node_id of the node which should receive this message
407                 node_id: PublicKey,
408                 /// The message which should be sent.
409                 msg: msgs::FundingSigned,
410         },
411         /// Used to indicate that a funding_locked message should be sent to the peer with the given node_id.
412         SendFundingLocked {
413                 /// The node_id of the node which should receive these message(s)
414                 node_id: PublicKey,
415                 /// The funding_locked message which should be sent.
416                 msg: msgs::FundingLocked,
417         },
418         /// Used to indicate that an announcement_signatures message should be sent to the peer with the given node_id.
419         SendAnnouncementSignatures {
420                 /// The node_id of the node which should receive these message(s)
421                 node_id: PublicKey,
422                 /// The announcement_signatures message which should be sent.
423                 msg: msgs::AnnouncementSignatures,
424         },
425         /// Used to indicate that a series of HTLC update messages, as well as a commitment_signed
426         /// message should be sent to the peer with the given node_id.
427         UpdateHTLCs {
428                 /// The node_id of the node which should receive these message(s)
429                 node_id: PublicKey,
430                 /// The update messages which should be sent. ALL messages in the struct should be sent!
431                 updates: msgs::CommitmentUpdate,
432         },
433         /// Used to indicate that a revoke_and_ack message should be sent to the peer with the given node_id.
434         SendRevokeAndACK {
435                 /// The node_id of the node which should receive this message
436                 node_id: PublicKey,
437                 /// The message which should be sent.
438                 msg: msgs::RevokeAndACK,
439         },
440         /// Used to indicate that a closing_signed message should be sent to the peer with the given node_id.
441         SendClosingSigned {
442                 /// The node_id of the node which should receive this message
443                 node_id: PublicKey,
444                 /// The message which should be sent.
445                 msg: msgs::ClosingSigned,
446         },
447         /// Used to indicate that a shutdown message should be sent to the peer with the given node_id.
448         SendShutdown {
449                 /// The node_id of the node which should receive this message
450                 node_id: PublicKey,
451                 /// The message which should be sent.
452                 msg: msgs::Shutdown,
453         },
454         /// Used to indicate that a channel_reestablish message should be sent to the peer with the given node_id.
455         SendChannelReestablish {
456                 /// The node_id of the node which should receive this message
457                 node_id: PublicKey,
458                 /// The message which should be sent.
459                 msg: msgs::ChannelReestablish,
460         },
461         /// Used to indicate that a channel_announcement and channel_update should be broadcast to all
462         /// peers (except the peer with node_id either msg.contents.node_id_1 or msg.contents.node_id_2).
463         ///
464         /// Note that after doing so, you very likely (unless you did so very recently) want to call
465         /// ChannelManager::broadcast_node_announcement to trigger a BroadcastNodeAnnouncement event.
466         /// This ensures that any nodes which see our channel_announcement also have a relevant
467         /// node_announcement, including relevant feature flags which may be important for routing
468         /// through or to us.
469         BroadcastChannelAnnouncement {
470                 /// The channel_announcement which should be sent.
471                 msg: msgs::ChannelAnnouncement,
472                 /// The followup channel_update which should be sent.
473                 update_msg: msgs::ChannelUpdate,
474         },
475         /// Used to indicate that a node_announcement should be broadcast to all peers.
476         BroadcastNodeAnnouncement {
477                 /// The node_announcement which should be sent.
478                 msg: msgs::NodeAnnouncement,
479         },
480         /// Used to indicate that a channel_update should be broadcast to all peers.
481         BroadcastChannelUpdate {
482                 /// The channel_update which should be sent.
483                 msg: msgs::ChannelUpdate,
484         },
485         /// Used to indicate that a channel_update should be sent to a single peer.
486         /// In contrast to [`Self::BroadcastChannelUpdate`], this is used when the channel is a
487         /// private channel and we shouldn't be informing all of our peers of channel parameters.
488         SendChannelUpdate {
489                 /// The node_id of the node which should receive this message
490                 node_id: PublicKey,
491                 /// The channel_update which should be sent.
492                 msg: msgs::ChannelUpdate,
493         },
494         /// Broadcast an error downstream to be handled
495         HandleError {
496                 /// The node_id of the node which should receive this message
497                 node_id: PublicKey,
498                 /// The action which should be taken.
499                 action: msgs::ErrorAction
500         },
501         /// Query a peer for channels with funding transaction UTXOs in a block range.
502         SendChannelRangeQuery {
503                 /// The node_id of this message recipient
504                 node_id: PublicKey,
505                 /// The query_channel_range which should be sent.
506                 msg: msgs::QueryChannelRange,
507         },
508         /// Request routing gossip messages from a peer for a list of channels identified by
509         /// their short_channel_ids.
510         SendShortIdsQuery {
511                 /// The node_id of this message recipient
512                 node_id: PublicKey,
513                 /// The query_short_channel_ids which should be sent.
514                 msg: msgs::QueryShortChannelIds,
515         },
516         /// Sends a reply to a channel range query. This may be one of several SendReplyChannelRange events
517         /// emitted during processing of the query.
518         SendReplyChannelRange {
519                 /// The node_id of this message recipient
520                 node_id: PublicKey,
521                 /// The reply_channel_range which should be sent.
522                 msg: msgs::ReplyChannelRange,
523         }
524 }
525
526 /// A trait indicating an object may generate message send events
527 pub trait MessageSendEventsProvider {
528         /// Gets the list of pending events which were generated by previous actions, clearing the list
529         /// in the process.
530         fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent>;
531 }
532
533 /// A trait indicating an object may generate events.
534 ///
535 /// Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
536 ///
537 /// # Requirements
538 ///
539 /// See [`process_pending_events`] for requirements around event processing.
540 ///
541 /// When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
542 /// event since the last invocation. The handler must either act upon the event immediately
543 /// or preserve it for later handling.
544 ///
545 /// Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
546 /// consult the provider's documentation on the implication of processing events and how a handler
547 /// may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
548 /// [`ChainMonitor::process_pending_events`]).
549 ///
550 /// (C-not implementable) As there is likely no reason for a user to implement this trait on their
551 /// own type(s).
552 ///
553 /// [`process_pending_events`]: Self::process_pending_events
554 /// [`handle_event`]: EventHandler::handle_event
555 /// [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
556 /// [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
557 pub trait EventsProvider {
558         /// Processes any events generated since the last call using the given event handler.
559         ///
560         /// Subsequent calls must only process new events. However, handlers must be capable of handling
561         /// duplicate events across process restarts. This may occur if the provider was recovered from
562         /// an old state (i.e., it hadn't been successfully persisted after processing pending events).
563         fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler;
564 }
565
566 /// A trait implemented for objects handling events from [`EventsProvider`].
567 pub trait EventHandler {
568         /// Handles the given [`Event`].
569         ///
570         /// See [`EventsProvider`] for details that must be considered when implementing this method.
571         fn handle_event(&self, event: &Event);
572 }
573
574 impl<F> EventHandler for F where F: Fn(&Event) {
575         fn handle_event(&self, event: &Event) {
576                 self(event)
577         }
578 }