1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::node::{PaymentState, RoutedPayment};
use crate::prelude::*;
use bitcoin::hashes::hex;
use bitcoin::hashes::hex::ToHex;
use bitcoin::util::address::Payload;
use bitcoin::{Address, Network, Script};
use lightning::chain::keysinterface::InMemorySigner;
use lightning::ln::chan_utils::{
BuiltCommitmentTransaction, ChannelPublicKeys, CommitmentTransaction, HTLCOutputInCommitment,
TxCreationKeys,
};
use lightning::ln::PaymentHash;
pub struct DebugChannelPublicKeys<'a>(pub &'a ChannelPublicKeys);
impl<'a> core::fmt::Debug for DebugChannelPublicKeys<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("ChannelPublicKeys")
.field("funding_pubkey", &self.0.funding_pubkey)
.field("revocation_basepoint", &self.0.revocation_basepoint)
.field("payment_point", &self.0.payment_point)
.field("delayed_payment_basepoint", &self.0.delayed_payment_basepoint)
.field("htlc_basepoint", &self.0.htlc_basepoint)
.finish()
}
}
macro_rules! log_channel_public_keys {
($obj: expr) => {
&crate::util::debug_utils::DebugChannelPublicKeys(&$obj)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! trace_enforcement_state {
($estate: expr) => {
#[cfg(not(feature = "debug_enforcement_state"))]
trace!("{}:\n{:#?}", function!(), $estate);
#[cfg(feature = "debug_enforcement_state")]
debug!("{}:\n{:#?}", function!(), $estate);
};
}
pub struct DebugPayload<'a>(pub &'a Payload);
impl<'a> core::fmt::Debug for DebugPayload<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
match *self.0 {
Payload::PubkeyHash(ref hash) => hex::format_hex(hash, f),
Payload::ScriptHash(ref hash) => hex::format_hex(hash, f),
Payload::WitnessProgram { version: ver, program: ref prog } => f
.debug_struct("WitnessProgram")
.field("version", &ver)
.field("program", &prog.to_hex())
.finish(),
}
}
}
pub struct DebugHTLCOutputInCommitment<'a>(pub &'a HTLCOutputInCommitment);
impl<'a> core::fmt::Debug for DebugHTLCOutputInCommitment<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("HTLCOutputInCommitment")
.field("offered", &self.0.offered)
.field("amount_msat", &self.0.amount_msat)
.field("cltv_expiry", &self.0.cltv_expiry)
.field("payment_hash", &self.0.payment_hash.0[..].to_hex())
.field("transaction_output_index", &self.0.transaction_output_index)
.finish()
}
}
pub struct DebugVecHTLCOutputInCommitment<'a>(pub &'a Vec<HTLCOutputInCommitment>);
impl<'a> core::fmt::Debug for DebugVecHTLCOutputInCommitment<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_list().entries(self.0.iter().map(|vv| DebugHTLCOutputInCommitment(&vv))).finish()
}
}
pub struct DebugTxCreationKeys<'a>(pub &'a TxCreationKeys);
impl<'a> core::fmt::Debug for DebugTxCreationKeys<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("TxCreationKeys")
.field("per_commitment_point", &self.0.per_commitment_point)
.field("revocation_key", &self.0.revocation_key)
.field("broadcaster_htlc_key", &self.0.broadcaster_htlc_key)
.field("countersignatory_htlc_key", &self.0.countersignatory_htlc_key)
.field("broadcaster_delayed_payment_key", &self.0.broadcaster_delayed_payment_key)
.finish()
}
}
pub struct DebugInMemorySigner<'a>(pub &'a InMemorySigner);
impl<'a> core::fmt::Debug for DebugInMemorySigner<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("InMemorySigner")
.field("funding_key", &self.0.funding_key)
.field("revocation_base_key", &self.0.revocation_base_key)
.field("payment_key", &self.0.payment_key)
.field("delayed_payment_base_key", &self.0.delayed_payment_base_key)
.field("htlc_base_key", &self.0.htlc_base_key)
.field("commitment_seed", &DebugBytes(&self.0.commitment_seed))
.finish()
}
}
pub struct DebugBuiltCommitmentTransaction<'a>(pub &'a BuiltCommitmentTransaction);
impl<'a> core::fmt::Debug for DebugBuiltCommitmentTransaction<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("BuiltCommitmentTransaction")
.field("transaction", &self.0.transaction)
.field("txid", &self.0.txid)
.finish()
}
}
pub struct DebugCommitmentTransaction<'a>(pub &'a CommitmentTransaction);
impl<'a> core::fmt::Debug for DebugCommitmentTransaction<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_struct("CommitmentTransaction")
.field("commitment_number", &self.0.commitment_number())
.field("to_broadcaster_value_sat", &self.0.to_broadcaster_value_sat())
.field("to_countersignatory_value_sat", &self.0.to_countersignatory_value_sat())
.field("feerate_per_kw", &self.0.feerate_per_kw())
.field("htlcs", &DebugVecHTLCOutputInCommitment(&self.0.htlcs()))
.field("opt_anchors", &self.0.trust().opt_anchors())
.field("keys", &DebugTxCreationKeys(&self.0.trust().keys()))
.field("built", &DebugBuiltCommitmentTransaction(&self.0.trust().built_transaction()))
.finish()
}
}
pub struct DebugBytes<'a>(pub &'a [u8]);
impl<'a> core::fmt::Debug for DebugBytes<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
for i in self.0 {
write!(f, "{:02x}", i)?;
}
Ok(())
}
}
pub struct DebugVecVecU8<'a>(pub &'a [Vec<u8>]);
impl<'a> core::fmt::Debug for DebugVecVecU8<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_list().entries(self.0.iter().map(|vv| DebugBytes(&vv[..]))).finish()
}
}
pub struct DebugWitness<'a>(pub &'a (Vec<u8>, Vec<u8>));
impl<'a> core::fmt::Debug for DebugWitness<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_tuple("Witness")
.field(&DebugBytes(&self.0 .0))
.field(&DebugBytes(&self.0 .1))
.finish()
}
}
pub struct DebugWitVec<'a>(pub &'a Vec<(Vec<u8>, Vec<u8>)>);
impl<'a> core::fmt::Debug for DebugWitVec<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_list().entries(self.0.iter().map(|ww| DebugWitness(ww))).finish()
}
}
pub struct DebugMapPaymentState<'a>(pub &'a Map<PaymentHash, PaymentState>);
impl<'a> core::fmt::Debug for DebugMapPaymentState<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_map().entries(self.0.iter().map(|(k, v)| (DebugBytes(&k.0), v))).finish()
}
}
pub struct DebugMapRoutedPayment<'a>(pub &'a Map<PaymentHash, RoutedPayment>);
impl<'a> core::fmt::Debug for DebugMapRoutedPayment<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
f.debug_map().entries(self.0.iter().map(|(k, v)| (DebugBytes(&k.0), v))).finish()
}
}
pub fn script_debug(script: &Script, network: Network) -> String {
format!(
"script={} {}={}",
script.to_hex(),
network,
match Address::from_script(script, network) {
Ok(addr) => addr.to_string(),
Err(_) => "<bad-address>".to_string(),
},
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! debug_vals {
( $($arg:tt)* ) => {
if log::log_enabled!(log::Level::Debug) {
debug!("{}: {}", short_function!(), vals_str!($($arg)*));
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! debug_failed_vals {
( $($arg:tt)* ) => {
if log::log_enabled!(log::Level::Debug) {
debug!("{} failed: {}", short_function!(), vals_str!($($arg)*));
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! scoped_debug_return {
( $($arg:tt)* ) => {{
let should_debug = true;
scopeguard::guard(should_debug, |should_debug| {
if should_debug {
if log::log_enabled!(log::Level::Debug) {
debug!("{} failed: {}", containing_function!(), vals_str!($($arg)*),
);
}
}
})
}};
}