Add module and all-pub-things docs and deny missing docs
[rust-lightning] / src / util / events.rs
1 //! Events are returned from various bits in the library which indicate some action must be taken
2 //! by the client.
3 //! Because we don't have a built-in runtime, its up to the client to call events at a time in the
4 //! future, as well as generate and broadcast funding transactions handle payment preimages and a
5 //! few other things.
6 //!
7 //! Note that many events are handled for you by PeerHandler, so in the common design of having a
8 //! PeerManager which marshalls messages to ChannelManager and Router you only need to call
9 //! process_events on the PeerHandler and then get_and_clear_pending_events and handle the events
10 //! that bubble up to the surface. If, however, you do not have a PeerHandler managing a
11 //! ChannelManager you need to handle all of the events which may be generated.
12 //TODO: We need better separation of event types ^
13
14 use ln::msgs;
15 use chain::transaction::OutPoint;
16
17 use bitcoin::blockdata::script::Script;
18
19 use secp256k1::key::PublicKey;
20
21 use std::time::Instant;
22
23 /// An Event which you should probably take some action in response to.
24 pub enum Event {
25         // Events a user will probably have to handle
26         /// Used to indicate that the client should generate a funding transaction with the given
27         /// parameters and then call ChannelManager::funding_transaction_generated.
28         /// Generated in ChannelManager message handling.
29         FundingGenerationReady {
30                 /// The random channel_id we picked which you'll need to pass into
31                 /// ChannelManager::funding_transaction_generated.
32                 temporary_channel_id: [u8; 32],
33                 /// The value, in satoshis, that the output should have.
34                 channel_value_satoshis: u64,
35                 /// The script which should be used in the transaction output.
36                 output_script: Script,
37                 /// The value passed in to ChannelManager::create_channel
38                 user_channel_id: u64,
39         },
40         /// Used to indicate that the client may now broadcast the funding transaction it created for a
41         /// channel. Broadcasting such a transaction prior to this event may lead to our counterparty
42         /// trivially stealing all funds in the funding transaction!
43         FundingBroadcastSafe {
44                 /// The output, which was passed to ChannelManager::funding_transaction_generated, which is
45                 /// now safe to broadcast.
46                 funding_txo: OutPoint,
47                 /// The value passed in to ChannelManager::create_channel
48                 user_channel_id: u64,
49         },
50         /// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
51         /// ChannelManager::claim_funds to get it....
52         /// Note that if the preimage is not known, you must call ChannelManager::fail_htlc_backwards
53         /// to free up resources for this HTLC.
54         PaymentReceived {
55                 /// The hash for which the preimage should be handed to the ChannelManager.
56                 payment_hash: [u8; 32],
57                 /// The value, in thousandths of a satoshi, that this payment is for.
58                 amt: u64,
59         },
60         /// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
61         /// and we got back the payment preimage for it).
62         PaymentSent {
63                 /// The preimage to the hash given to ChannelManager::send_payment.
64                 /// Note that this serves as a payment receipt, if you wish to have such a thing, you must
65                 /// store it somehow!
66                 payment_preimage: [u8; 32],
67         },
68         /// Indicates an outbound payment we made failed. Probably some intermediary node dropped
69         /// something. You may wish to retry with a different route.
70         PaymentFailed {
71                 /// The hash which was given to ChannelManager::send_payment.
72                 payment_hash: [u8; 32],
73         },
74         /// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
75         /// time in the future.
76         PendingHTLCsForwardable {
77                 /// The earliest time at which process_pending_htlc_forwards should be called.
78                 time_forwardable: Instant,
79         },
80
81         // Events indicating the network loop should send a message to a peer:
82         // TODO: Move these into a separate struct and make a top-level enum
83         /// Used to indicate that we've initialted a channel open and should send the open_channel
84         /// message provided to the given peer.
85         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
86         SendOpenChannel {
87                 /// The node_id of the node which should receive this message
88                 node_id: PublicKey,
89                 /// The message which should be sent.
90                 msg: msgs::OpenChannel,
91         },
92         /// Used to indicate that a funding_created message should be sent to the peer with the given node_id.
93         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
94         SendFundingCreated {
95                 /// The node_id of the node which should receive this message
96                 node_id: PublicKey,
97                 /// The message which should be sent.
98                 msg: msgs::FundingCreated,
99         },
100         /// Used to indicate that a funding_locked message should be sent to the peer with the given node_id.
101         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
102         SendFundingLocked {
103                 /// The node_id of the node which should receive these message(s)
104                 node_id: PublicKey,
105                 /// The funding_locked message which should be sent.
106                 msg: msgs::FundingLocked,
107                 /// An optional additional announcement_signatures message which should be sent.
108                 announcement_sigs: Option<msgs::AnnouncementSignatures>,
109         },
110         /// Used to indicate that a series of HTLC update messages, as well as a commitment_signed
111         /// message should be sent to the peer with the given node_id.
112         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
113         UpdateHTLCs {
114                 /// The node_id of the node which should receive these message(s)
115                 node_id: PublicKey,
116                 /// The update messages which should be sent. ALL messages in the struct should be sent!
117                 updates: msgs::CommitmentUpdate,
118         },
119         /// Used to indicate that a shutdown message should be sent to the peer with the given node_id.
120         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
121         SendShutdown {
122                 /// The node_id of the node which should receive this message
123                 node_id: PublicKey,
124                 /// The message which should be sent.
125                 msg: msgs::Shutdown,
126         },
127         /// Used to indicate that a channel_announcement and channel_update should be broadcast to all
128         /// peers (except the peer with node_id either msg.contents.node_id_1 or msg.contents.node_id_2).
129         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
130         BroadcastChannelAnnouncement {
131                 /// The channel_announcement which should be sent.
132                 msg: msgs::ChannelAnnouncement,
133                 /// The followup channel_update which should be sent.
134                 update_msg: msgs::ChannelUpdate,
135         },
136         /// Used to indicate that a channel_update should be broadcast to all peers.
137         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
138         BroadcastChannelUpdate {
139                 /// The channel_update which should be sent.
140                 msg: msgs::ChannelUpdate,
141         },
142
143         //Error handling
144         /// Broadcast an error downstream to be handled
145         /// This event is handled by PeerManager::process_events if you are using a PeerManager.
146         HandleError {
147                 /// The node_id of the node which should receive this message
148                 node_id: PublicKey,
149                 /// The action which should be taken.
150                 action: Option<msgs::ErrorAction>
151         }
152 }
153
154 /// A trait indicating an object may generate events
155 pub trait EventsProvider {
156         /// Gets the list of pending events which were generated by previous actions, clearing the list
157         /// in the process.
158         fn get_and_clear_pending_events(&self) -> Vec<Event>;
159 }