2020-08-10 15:00:09 -04:00
|
|
|
// This file is Copyright its original authors, visible in version control
|
|
|
|
// history.
|
|
|
|
//
|
|
|
|
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
|
|
|
|
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
|
|
|
|
// You may not use this file except in accordance with one or both of these
|
|
|
|
// licenses.
|
|
|
|
|
2022-10-13 02:35:48 -04:00
|
|
|
use crate::chain::transaction::OutPoint;
|
|
|
|
use crate::chain::keysinterface::SpendableOutputDescriptor;
|
2018-08-16 22:30:03 -04:00
|
|
|
|
2020-04-27 17:53:13 +02:00
|
|
|
use bitcoin::hash_types::Txid;
|
2019-11-22 17:44:30 -05:00
|
|
|
use bitcoin::blockdata::transaction::Transaction;
|
2018-08-16 22:30:03 -04:00
|
|
|
|
2022-10-13 02:35:48 -04:00
|
|
|
use crate::routing::router::Route;
|
|
|
|
use crate::ln::chan_utils::HTLCClaim;
|
|
|
|
use crate::util::logger::DebugBytes;
|
2018-08-23 13:23:00 -04:00
|
|
|
|
2022-10-14 10:31:36 -04:00
|
|
|
/// Logs a pubkey in hex format.
|
|
|
|
#[macro_export]
|
2018-08-16 22:30:03 -04:00
|
|
|
macro_rules! log_pubkey {
|
|
|
|
($obj: expr) => {
|
2022-10-14 10:31:36 -04:00
|
|
|
$crate::util::logger::DebugPubKey(&$obj)
|
2018-08-16 22:30:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 23:55:28 -05:00
|
|
|
/// Logs a byte slice in hex format.
|
|
|
|
#[macro_export]
|
2018-08-16 22:30:03 -04:00
|
|
|
macro_rules! log_bytes {
|
|
|
|
($obj: expr) => {
|
2021-08-23 23:55:28 -05:00
|
|
|
$crate::util::logger::DebugBytes(&$obj)
|
2018-08-16 22:30:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 17:53:13 +02:00
|
|
|
pub(crate) struct DebugFundingChannelId<'a>(pub &'a Txid, pub u16);
|
2021-05-23 23:22:46 +00:00
|
|
|
impl<'a> core::fmt::Display for DebugFundingChannelId<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
2020-05-12 13:17:49 -04:00
|
|
|
for i in (OutPoint { txid: self.0.clone(), index: self.1 }).to_channel_id().iter() {
|
2018-08-16 22:30:03 -04:00
|
|
|
write!(f, "{:02x}", i)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
macro_rules! log_funding_channel_id {
|
|
|
|
($funding_txid: expr, $funding_txo: expr) => {
|
2022-10-13 02:35:48 -04:00
|
|
|
$crate::util::macro_logger::DebugFundingChannelId(&$funding_txid, $funding_txo)
|
2018-08-16 22:30:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 21:33:54 -04:00
|
|
|
pub(crate) struct DebugFundingInfo<'a, T: 'a>(pub &'a (OutPoint, T));
|
2021-05-23 23:22:46 +00:00
|
|
|
impl<'a, T> core::fmt::Display for DebugFundingInfo<'a, T> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
2020-04-18 21:33:54 -04:00
|
|
|
DebugBytes(&(self.0).0.to_channel_id()[..]).fmt(f)
|
2018-10-29 13:38:15 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-26 19:50:16 -05:00
|
|
|
macro_rules! log_funding_info {
|
|
|
|
($key_storage: expr) => {
|
2022-10-13 02:35:48 -04:00
|
|
|
$crate::util::macro_logger::DebugFundingInfo(&$key_storage.get_funding_txo())
|
2018-10-29 13:38:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 13:23:00 -04:00
|
|
|
pub(crate) struct DebugRoute<'a>(pub &'a Route);
|
2021-05-23 23:22:46 +00:00
|
|
|
impl<'a> core::fmt::Display for DebugRoute<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
2020-01-03 19:31:40 -05:00
|
|
|
for (idx, p) in self.0.paths.iter().enumerate() {
|
2020-10-08 15:46:43 -07:00
|
|
|
writeln!(f, "path {}:", idx)?;
|
2020-01-03 19:31:40 -05:00
|
|
|
for h in p.iter() {
|
2020-10-08 15:46:43 -07:00
|
|
|
writeln!(f, " node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
|
2020-01-03 19:31:40 -05:00
|
|
|
}
|
2018-08-23 13:23:00 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
macro_rules! log_route {
|
|
|
|
($obj: expr) => {
|
2022-10-13 02:35:48 -04:00
|
|
|
$crate::util::macro_logger::DebugRoute(&$obj)
|
2018-08-23 13:23:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:44:30 -05:00
|
|
|
pub(crate) struct DebugTx<'a>(pub &'a Transaction);
|
2021-05-23 23:22:46 +00:00
|
|
|
impl<'a> core::fmt::Display for DebugTx<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
2020-01-13 13:43:54 -05:00
|
|
|
if self.0.input.len() >= 1 && self.0.input.iter().any(|i| !i.witness.is_empty()) {
|
2022-08-25 13:23:29 -07:00
|
|
|
let first_input = &self.0.input[0];
|
|
|
|
let witness_script_len = first_input.witness.last().unwrap_or(&[]).len();
|
|
|
|
if self.0.input.len() == 1 && witness_script_len == 71 &&
|
|
|
|
(first_input.sequence.0 >> 8*3) as u8 == 0x80 {
|
2021-05-06 16:21:44 +00:00
|
|
|
write!(f, "commitment tx ")?;
|
2022-08-25 13:23:29 -07:00
|
|
|
} else if self.0.input.len() == 1 && witness_script_len == 71 {
|
2021-05-06 16:21:44 +00:00
|
|
|
write!(f, "closing tx ")?;
|
2022-08-25 13:23:29 -07:00
|
|
|
} else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::OfferedTimeout) {
|
2021-05-06 16:21:44 +00:00
|
|
|
write!(f, "HTLC-timeout tx ")?;
|
2022-08-25 13:23:29 -07:00
|
|
|
} else if self.0.input.len() == 1 && HTLCClaim::from_witness(&first_input.witness) == Some(HTLCClaim::AcceptedPreimage) {
|
2021-05-06 16:21:44 +00:00
|
|
|
write!(f, "HTLC-success tx ")?;
|
2020-01-13 13:43:54 -05:00
|
|
|
} else {
|
2022-08-25 13:23:29 -07:00
|
|
|
let mut num_preimage = 0;
|
|
|
|
let mut num_timeout = 0;
|
|
|
|
let mut num_revoked = 0;
|
2020-01-13 13:43:54 -05:00
|
|
|
for inp in &self.0.input {
|
2022-08-25 13:23:29 -07:00
|
|
|
let htlc_claim = HTLCClaim::from_witness(&inp.witness);
|
|
|
|
match htlc_claim {
|
|
|
|
Some(HTLCClaim::AcceptedPreimage)|Some(HTLCClaim::OfferedPreimage) => num_preimage += 1,
|
|
|
|
Some(HTLCClaim::AcceptedTimeout)|Some(HTLCClaim::OfferedTimeout) => num_timeout += 1,
|
|
|
|
Some(HTLCClaim::Revocation) => num_revoked += 1,
|
|
|
|
None => continue,
|
2020-01-13 13:43:54 -05:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 13:23:29 -07:00
|
|
|
if num_preimage > 0 || num_timeout > 0 || num_revoked > 0 {
|
|
|
|
write!(f, "HTLC claim tx ({} preimage, {} timeout, {} revoked)",
|
|
|
|
num_preimage, num_timeout, num_revoked)?;
|
|
|
|
}
|
2019-11-22 17:44:30 -05:00
|
|
|
}
|
2020-01-13 13:43:54 -05:00
|
|
|
} else {
|
2021-05-06 16:21:44 +00:00
|
|
|
debug_assert!(false, "We should never generate unknown transaction types");
|
|
|
|
write!(f, "unknown tx type ").unwrap();
|
2019-11-22 17:44:30 -05:00
|
|
|
}
|
2021-05-06 16:21:44 +00:00
|
|
|
write!(f, "with txid {}", self.0.txid())?;
|
2019-11-22 17:44:30 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! log_tx {
|
|
|
|
($obj: expr) => {
|
2022-10-13 02:35:48 -04:00
|
|
|
$crate::util::macro_logger::DebugTx(&$obj)
|
2019-11-22 17:44:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:41:13 -05:00
|
|
|
pub(crate) struct DebugSpendable<'a>(pub &'a SpendableOutputDescriptor);
|
2021-05-23 23:22:46 +00:00
|
|
|
impl<'a> core::fmt::Display for DebugSpendable<'a> {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
2020-03-03 14:41:13 -05:00
|
|
|
match self.0 {
|
|
|
|
&SpendableOutputDescriptor::StaticOutput { ref outpoint, .. } => {
|
2020-07-30 13:19:11 -04:00
|
|
|
write!(f, "StaticOutput {}:{} marked for spending", outpoint.txid, outpoint.index)?;
|
2020-03-03 14:41:13 -05:00
|
|
|
}
|
2021-02-16 16:15:40 -05:00
|
|
|
&SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) => {
|
|
|
|
write!(f, "DelayedPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
|
2020-03-03 14:41:13 -05:00
|
|
|
}
|
2021-02-16 16:15:40 -05:00
|
|
|
&SpendableOutputDescriptor::StaticPaymentOutput(ref descriptor) => {
|
2021-08-03 19:20:06 -04:00
|
|
|
write!(f, "StaticPaymentOutput {}:{} marked for spending", descriptor.outpoint.txid, descriptor.outpoint.index)?;
|
2020-03-03 14:41:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! log_spendable {
|
|
|
|
($obj: expr) => {
|
2022-10-13 02:35:48 -04:00
|
|
|
$crate::util::macro_logger::DebugSpendable(&$obj)
|
2020-03-03 14:41:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 17:18:17 -05:00
|
|
|
/// Create a new Record and log it. You probably don't want to use this macro directly,
|
|
|
|
/// but it needs to be exported so `log_trace` etc can use it in external crates.
|
2021-08-23 23:55:28 -05:00
|
|
|
#[doc(hidden)]
|
2020-12-02 17:18:17 -05:00
|
|
|
#[macro_export]
|
2018-07-25 02:34:51 +00:00
|
|
|
macro_rules! log_internal {
|
2020-03-02 12:55:53 -05:00
|
|
|
($logger: expr, $lvl:expr, $($arg:tt)+) => (
|
Fix trailing semicolon warnings on latest rustc nightly
Latest rustc nightly compiles are filled with warnings like the
following, which we fix here:
```
warning: trailing semicolon in macro used in expression position
--> lightning/src/util/macro_logger.rs:163:114
|
163 | $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
| ^
|
::: lightning/src/chain/chainmonitor.rs:165:9
|
165 | log_debug!(self.logger, "New best block {} at height {} provided via block_connected", header.block_hash(), height);
| -------------------------------------------------------------------------------------------------------------------- in this macro invocation
|
= note: `#[warn(semicolon_in_expressions_from_macros)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79813 <https://github.com/rust-lang/rust/issues/79813>
= note: this warning originates in the macro `log_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
```
2021-08-18 00:33:56 +00:00
|
|
|
$logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()))
|
2018-07-25 02:34:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-28 20:38:48 +00:00
|
|
|
/// Logs an entry at the given level.
|
2022-08-24 13:59:58 +02:00
|
|
|
#[doc(hidden)]
|
2021-06-28 20:38:48 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! log_given_level {
|
|
|
|
($logger: expr, $lvl:expr, $($arg:tt)+) => (
|
|
|
|
match $lvl {
|
|
|
|
#[cfg(not(any(feature = "max_level_off")))]
|
|
|
|
$crate::util::logger::Level::Error => log_internal!($logger, $lvl, $($arg)*),
|
|
|
|
#[cfg(not(any(feature = "max_level_off", feature = "max_level_error")))]
|
|
|
|
$crate::util::logger::Level::Warn => log_internal!($logger, $lvl, $($arg)*),
|
|
|
|
#[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn")))]
|
|
|
|
$crate::util::logger::Level::Info => log_internal!($logger, $lvl, $($arg)*),
|
|
|
|
#[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info")))]
|
|
|
|
$crate::util::logger::Level::Debug => log_internal!($logger, $lvl, $($arg)*),
|
|
|
|
#[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug")))]
|
|
|
|
$crate::util::logger::Level::Trace => log_internal!($logger, $lvl, $($arg)*),
|
2021-11-22 18:19:08 +01:00
|
|
|
#[cfg(not(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug", feature = "max_level_trace")))]
|
|
|
|
$crate::util::logger::Level::Gossip => log_internal!($logger, $lvl, $($arg)*),
|
2021-06-28 20:38:48 +00:00
|
|
|
|
2021-11-23 23:03:13 +00:00
|
|
|
#[cfg(any(feature = "max_level_off", feature = "max_level_error", feature = "max_level_warn", feature = "max_level_info", feature = "max_level_debug", feature = "max_level_trace"))]
|
2021-06-28 20:38:48 +00:00
|
|
|
_ => {
|
|
|
|
// The level is disabled at compile-time
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:59:58 +02:00
|
|
|
/// Log at the `ERROR` level.
|
2020-12-02 17:18:17 -05:00
|
|
|
#[macro_export]
|
2018-07-25 02:34:51 +00:00
|
|
|
macro_rules! log_error {
|
2020-03-02 12:55:53 -05:00
|
|
|
($logger: expr, $($arg:tt)*) => (
|
2021-06-28 20:38:48 +00:00
|
|
|
log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*);
|
2018-07-25 02:34:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:59:58 +02:00
|
|
|
/// Log at the `WARN` level.
|
|
|
|
#[macro_export]
|
2018-07-25 02:34:51 +00:00
|
|
|
macro_rules! log_warn {
|
2020-03-02 12:55:53 -05:00
|
|
|
($logger: expr, $($arg:tt)*) => (
|
2021-06-28 20:38:48 +00:00
|
|
|
log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*);
|
2018-07-25 02:34:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:59:58 +02:00
|
|
|
/// Log at the `INFO` level.
|
|
|
|
#[macro_export]
|
2018-07-25 02:34:51 +00:00
|
|
|
macro_rules! log_info {
|
2020-03-02 12:55:53 -05:00
|
|
|
($logger: expr, $($arg:tt)*) => (
|
2021-06-28 20:38:48 +00:00
|
|
|
log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*);
|
2018-07-25 02:34:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:59:58 +02:00
|
|
|
/// Log at the `DEBUG` level.
|
|
|
|
#[macro_export]
|
2018-07-25 02:34:51 +00:00
|
|
|
macro_rules! log_debug {
|
2020-03-02 12:55:53 -05:00
|
|
|
($logger: expr, $($arg:tt)*) => (
|
2021-06-28 20:38:48 +00:00
|
|
|
log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*);
|
2018-07-25 02:34:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:59:58 +02:00
|
|
|
/// Log at the `TRACE` level.
|
2020-12-02 17:18:17 -05:00
|
|
|
#[macro_export]
|
2018-07-25 02:34:51 +00:00
|
|
|
macro_rules! log_trace {
|
2020-03-02 12:55:53 -05:00
|
|
|
($logger: expr, $($arg:tt)*) => (
|
2021-10-31 18:19:39 +00:00
|
|
|
log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*)
|
2018-07-25 02:34:51 +00:00
|
|
|
)
|
|
|
|
}
|
2021-11-22 18:19:08 +01:00
|
|
|
|
2022-08-24 13:59:58 +02:00
|
|
|
/// Log at the `GOSSIP` level.
|
|
|
|
#[macro_export]
|
2021-11-22 18:19:08 +01:00
|
|
|
macro_rules! log_gossip {
|
|
|
|
($logger: expr, $($arg:tt)*) => (
|
|
|
|
log_given_level!($logger, $crate::util::logger::Level::Gossip, $($arg)*);
|
|
|
|
)
|
|
|
|
}
|