Bound incoming HTLC witnessScript to min/max limits
[rust-lightning] / lightning / src / util / macro_logger.rs
1 use chain::transaction::OutPoint;
2
3 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
4 use bitcoin::blockdata::transaction::Transaction;
5 use secp256k1::key::PublicKey;
6
7 use ln::router::Route;
8 use ln::chan_utils::HTLCType;
9
10 use std;
11
12 pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey);
13 impl<'a> std::fmt::Display for DebugPubKey<'a> {
14         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
15                 for i in self.0.serialize().iter() {
16                         write!(f, "{:02x}", i)?;
17                 }
18                 Ok(())
19         }
20 }
21 macro_rules! log_pubkey {
22         ($obj: expr) => {
23                 ::util::macro_logger::DebugPubKey(&$obj)
24         }
25 }
26
27 pub(crate) struct DebugBytes<'a>(pub &'a [u8]);
28 impl<'a> std::fmt::Display for DebugBytes<'a> {
29         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
30                 for i in self.0 {
31                         write!(f, "{:02x}", i)?;
32                 }
33                 Ok(())
34         }
35 }
36 macro_rules! log_bytes {
37         ($obj: expr) => {
38                 ::util::macro_logger::DebugBytes(&$obj)
39         }
40 }
41
42 pub(crate) struct DebugFundingChannelId<'a>(pub &'a Sha256dHash, pub u16);
43 impl<'a> std::fmt::Display for DebugFundingChannelId<'a> {
44         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
45                 for i in OutPoint::new(self.0.clone(), self.1).to_channel_id().iter() {
46                         write!(f, "{:02x}", i)?;
47                 }
48                 Ok(())
49         }
50 }
51 macro_rules! log_funding_channel_id {
52         ($funding_txid: expr, $funding_txo: expr) => {
53                 ::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
54         }
55 }
56
57 pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a Option<(OutPoint, T)>);
58 impl<'a, T> std::fmt::Display for DebugFundingInfo<'a, T> {
59         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
60                 match self.0.as_ref() {
61                         Some(&(ref funding_output, _)) => DebugBytes(&funding_output.to_channel_id()[..]).fmt(f),
62                         None => write!(f, "without funding output set"),
63                 }
64         }
65 }
66 macro_rules! log_funding_info {
67         ($key_storage: expr) => {
68                 match $key_storage {
69                         Storage::Local { ref funding_info, .. } => {
70                                 ::util::macro_logger::DebugFundingInfo(&funding_info)
71                         },
72                         Storage::Watchtower { .. } => {
73                                 ::util::macro_logger::DebugFundingInfo(&None)
74                         }
75                 }
76         }
77 }
78
79 pub(crate) struct DebugRoute<'a>(pub &'a Route);
80 impl<'a> std::fmt::Display for DebugRoute<'a> {
81         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
82                 for h in self.0.hops.iter() {
83                         write!(f, "node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}\n", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
84                 }
85                 Ok(())
86         }
87 }
88 macro_rules! log_route {
89         ($obj: expr) => {
90                 ::util::macro_logger::DebugRoute(&$obj)
91         }
92 }
93
94 pub(crate) struct DebugTx<'a>(pub &'a Transaction);
95 impl<'a> std::fmt::Display for DebugTx<'a> {
96         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
97                 if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
98                         if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 &&
99                                         (self.0.input[0].sequence >> 8*3) as u8 == 0x80 {
100                                 write!(f, "commitment tx")?;
101                         } else if self.0.input.len() == 1 && self.0.input[0].witness.last().unwrap().len() == 71 {
102                                 write!(f, "closing tx")?;
103                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) &&
104                                         self.0.input[0].witness.len() == 5 {
105                                 write!(f, "HTLC-timeout tx")?;
106                         } else if self.0.input.len() == 1 && HTLCType::scriptlen_to_htlctype(self.0.input[0].witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) &&
107                                         self.0.input[0].witness.len() == 5 {
108                                 write!(f, "HTLC-success tx")?;
109                         } else {
110                                 for inp in &self.0.input {
111                                         if !inp.witness.is_empty() {
112                                                 if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::OfferedHTLC) { write!(f, "preimage-")?; break }
113                                                 else if HTLCType::scriptlen_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
114                                         }
115                                 }
116                                 write!(f, "tx")?;
117                         }
118                 } else {
119                         write!(f, "INVALID TRANSACTION")?;
120                 }
121                 Ok(())
122         }
123 }
124
125 macro_rules! log_tx {
126         ($obj: expr) => {
127                 ::util::macro_logger::DebugTx(&$obj)
128         }
129 }
130
131 macro_rules! log_internal {
132         ($self: ident, $lvl:expr, $($arg:tt)+) => (
133                 &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
134         );
135 }
136
137 macro_rules! log_error {
138         ($self: ident, $($arg:tt)*) => (
139                 #[cfg(not(any(feature = "max_level_off")))]
140                 log_internal!($self, $crate::util::logger::Level::Error, $($arg)*);
141         )
142 }
143
144 macro_rules! log_warn {
145         ($self: ident, $($arg:tt)*) => (
146                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
147                 log_internal!($self, $crate::util::logger::Level::Warn, $($arg)*);
148         )
149 }
150
151 macro_rules! log_info {
152         ($self: ident, $($arg:tt)*) => (
153                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
154                 log_internal!($self, $crate::util::logger::Level::Info, $($arg)*);
155         )
156 }
157
158 macro_rules! log_debug {
159         ($self: ident, $($arg:tt)*) => (
160                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
161                 log_internal!($self, $crate::util::logger::Level::Debug, $($arg)*);
162         )
163 }
164
165 macro_rules! log_trace {
166         ($self: ident, $($arg:tt)*) => (
167                 #[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
168                 log_internal!($self, $crate::util::logger::Level::Trace, $($arg)*);
169         )
170 }