Move shutdown-related tests into a new module
[rust-lightning] / lightning / src / ln / mod.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 //! High level lightning structs and impls live here.
11 //!
12 //! You probably want to create a channelmanager::ChannelManager, and a routing::NetGraphMsgHandler first.
13 //! Then, you probably want to pass them both on to a peer_handler::PeerManager and use that to
14 //! create/manage connections and call get_and_clear_pending_events after each action, handling
15 //! them appropriately.
16 //!
17 //! When you want to open/close a channel or send a payment, call into your ChannelManager and when
18 //! you want to learn things about the network topology (eg get a route for sending a payment),
19 //! call into your NetGraphMsgHandler.
20
21 #[cfg(any(test, feature = "_test_utils"))]
22 #[macro_use]
23 pub mod functional_test_utils;
24
25 pub mod channelmanager;
26 pub mod msgs;
27 pub mod peer_handler;
28 pub mod chan_utils;
29 pub mod features;
30 pub mod script;
31
32 #[cfg(feature = "fuzztarget")]
33 pub mod peer_channel_encryptor;
34 #[cfg(not(feature = "fuzztarget"))]
35 pub(crate) mod peer_channel_encryptor;
36
37 #[cfg(feature = "fuzztarget")]
38 pub mod channel;
39 #[cfg(not(feature = "fuzztarget"))]
40 mod channel;
41
42 mod onion_utils;
43 mod wire;
44
45 // Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro
46 // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
47 // about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
48
49 #[cfg(test)]
50 #[allow(unused_mut)]
51 mod functional_tests;
52 #[cfg(test)]
53 #[allow(unused_mut)]
54 mod chanmon_update_fail_tests;
55 #[cfg(test)]
56 #[allow(unused_mut)]
57 mod reorg_tests;
58 #[cfg(test)]
59 #[allow(unused_mut)]
60 mod onion_route_tests;
61 #[cfg(test)]
62 #[allow(unused_mut)]
63 mod monitor_tests;
64 #[cfg(test)]
65 #[allow(unused_mut)]
66 mod shutdown_tests;
67
68 pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
69
70 /// payment_hash type, use to cross-lock hop
71 /// (C-not exported) as we just use [u8; 32] directly
72 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
73 pub struct PaymentHash(pub [u8;32]);
74 /// payment_preimage type, use to route payment between hop
75 /// (C-not exported) as we just use [u8; 32] directly
76 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
77 pub struct PaymentPreimage(pub [u8;32]);
78 /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
79 /// (C-not exported) as we just use [u8; 32] directly
80 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
81 pub struct PaymentSecret(pub [u8;32]);
82
83 use prelude::*;
84 use bitcoin::bech32;
85 use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};
86
87 impl FromBase32 for PaymentSecret {
88         type Err = bech32::Error;
89
90         fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
91                 if field_data.len() != 52 {
92                         return Err(bech32::Error::InvalidLength)
93                 } else {
94                         let data_bytes = Vec::<u8>::from_base32(field_data)?;
95                         let mut payment_secret = [0; 32];
96                         payment_secret.copy_from_slice(&data_bytes);
97                         Ok(PaymentSecret(payment_secret))
98                 }
99         }
100 }
101
102 impl ToBase32 for PaymentSecret {
103         fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
104                 (&self.0[..]).write_base32(writer)
105         }
106 }
107
108 impl Base32Len for PaymentSecret {
109         fn base32_len(&self) -> usize {
110                 52
111         }
112 }