Merge pull request #84 from savil/sort_outputs
[rust-lightning] / src / util / events.rs
1 use ln::msgs;
2 use chain::transaction::OutPoint;
3
4 use bitcoin::blockdata::script::Script;
5
6 use secp256k1::key::PublicKey;
7
8 use std::time::Instant;
9
10 pub enum Event {
11         // Events a user will probably have to handle
12         /// Used to indicate that the client should generate a funding transaction with the given
13         /// parameters and then call ChannelManager::funding_transaction_generated.
14         /// Generated in ChannelManager message handling.
15         FundingGenerationReady {
16                 temporary_channel_id: [u8; 32],
17                 channel_value_satoshis: u64,
18                 output_script: Script,
19                 /// The value passed in to ChannelManager::create_channel
20                 user_channel_id: u64,
21         },
22         /// Used to indicate that the client may now broadcast the funding transaction it created for a
23         /// channel. Broadcasting such a transaction prior to this event may lead to our counterparty
24         /// trivially stealing all funds in the funding transaction!
25         FundingBroadcastSafe {
26                 funding_txo: OutPoint,
27                 /// The value passed in to ChannelManager::create_channel
28                 user_channel_id: u64,
29         },
30         /// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
31         /// ChannelManager::claim_funds to get it....
32         /// Note that if the preimage is not known, you must call ChannelManager::fail_htlc_backwards
33         /// to free up resources for this HTLC.
34         PaymentReceived {
35                 payment_hash: [u8; 32],
36                 amt: u64,
37         },
38         /// Indicates an outbound payment we made succeeded (ie it made it all the way to its target
39         /// and we got back the payment preimage for it). payment_preimage serves as a payment receipt,
40         /// if you wish to have such a thing, you must store it somehow!
41         PaymentSent {
42                 payment_preimage: [u8; 32],
43         },
44         /// Indicates an outbound payment we made failed. Probably some intermediary node dropped
45         /// something. You may wish to retry with a different route.
46         PaymentFailed {
47                 payment_hash: [u8; 32],
48         },
49         /// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
50         /// time in the future.
51         PendingHTLCsForwardable {
52                 time_forwardable: Instant,
53         },
54
55         // Events indicating the network loop should send a message to a peer:
56         /// Used to indicate that we've initialted a channel open and should send the open_channel
57         /// message provided to the given peer
58         SendOpenChannel {
59                 node_id: PublicKey,
60                 msg: msgs::OpenChannel,
61         },
62         /// Used to indicate that a funding_created message should be sent to the peer with the given node_id.
63         SendFundingCreated {
64                 node_id: PublicKey,
65                 msg: msgs::FundingCreated,
66         },
67         /// Used to indicate that a funding_locked message should be sent to the peer with the given node_id.
68         SendFundingLocked {
69                 node_id: PublicKey,
70                 msg: msgs::FundingLocked,
71                 announcement_sigs: Option<msgs::AnnouncementSignatures>,
72         },
73         /// Used to indicate that a series of update_add_htlc messages, as well as a commitment_signed
74         /// message should be sent to the peer with the given node_id.
75         SendHTLCs {
76                 node_id: PublicKey,
77                 msgs: Vec<msgs::UpdateAddHTLC>,
78                 commitment_msg: msgs::CommitmentSigned,
79         },
80         /// Used to indicate that we're ready to fulfill an htlc from the peer with the given node_id.
81         SendFulfillHTLC {
82                 node_id: PublicKey,
83                 msg: msgs::UpdateFulfillHTLC,
84                 commitment_msg: msgs::CommitmentSigned,
85         },
86         /// Used to indicate that we need to fail an htlc from the peer with the given node_id.
87         SendFailHTLC {
88                 node_id: PublicKey,
89                 msg: msgs::UpdateFailHTLC,
90                 commitment_msg: msgs::CommitmentSigned,
91         },
92         /// Used to indicate that a shutdown message should be sent to the peer with the given node_id.
93         SendShutdown {
94                 node_id: PublicKey,
95                 msg: msgs::Shutdown,
96         },
97         /// Used to indicate that a channel_announcement and channel_update should be broadcast to all
98         /// peers (except the peer with node_id either msg.contents.node_id_1 or msg.contents.node_id_2).
99         BroadcastChannelAnnouncement {
100                 msg: msgs::ChannelAnnouncement,
101                 update_msg: msgs::ChannelUpdate,
102         },
103         /// Used to indicate that a channel_update should be broadcast to all peers.
104         BroadcastChannelUpdate {
105                 msg: msgs::ChannelUpdate,
106         },
107
108         // Events indicating the network loop should change the state of connection with peer:
109         /// Disconnect the given peer, possibly making an attempt to send an ErrorMessage first.
110         DisconnectPeer  {
111                 node_id: PublicKey,
112                 msg: Option<msgs::ErrorMessage>,
113         }
114 }
115
116 pub trait EventsProvider {
117         fn get_and_clear_pending_events(&self) -> Vec<Event>;
118 }