From 05234038878531196b2fc0ab13b95b7671ecb524 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 23 Aug 2018 13:23:00 -0400 Subject: [PATCH] Add Display trait on network structs for routing bug track --- src/ln/router.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/util/macro_logger.rs | 19 +++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/ln/router.rs b/src/ln/router.rs index f4bf32a0..9ef5b7a7 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -12,6 +12,7 @@ use std::cmp; use std::sync::{RwLock,Arc}; use std::collections::{HashMap,BinaryHeap}; use std::collections::hash_map::Entry; +use std; /// A hop in a route #[derive(Clone)] @@ -45,12 +46,28 @@ struct DirectionalChannelInfo { fee_proportional_millionths: u32, } +impl std::fmt::Display for DirectionalChannelInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, " node id {} last_update {} enabled {} cltv_expiry_delta {} htlc_minimum_msat {} fee_base_msat {} fee_proportional_millionths {}\n", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?; + Ok(()) + } +} + struct ChannelInfo { features: GlobalFeatures, one_to_two: DirectionalChannelInfo, two_to_one: DirectionalChannelInfo, } +impl std::fmt::Display for ChannelInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + //TODO: GlobalFeatures + write!(f, " one_to_two {}", self.one_to_two)?; + write!(f, " two_to_one {}", self.two_to_one)?; + Ok(()) + } +} + struct NodeInfo { #[cfg(feature = "non_bitcoin_chain_hash_routing")] channels: Vec<(u64, Sha256dHash)>, @@ -67,6 +84,19 @@ struct NodeInfo { addresses: Vec, } +impl std::fmt::Display for NodeInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, " Channels\n")?; + for c in self.channels.iter() { + write!(f, " {}\n", c)?; + } + write!(f, " lowest_inbound_channel_fee_base_msat {}\n", self.lowest_inbound_channel_fee_base_msat)?; + write!(f, " lowest_inbound_channel_fee_proportional_millionths {}\n", self.lowest_inbound_channel_fee_proportional_millionths)?; + //TODO: GlobalFeatures, last_update, rgb, alias, addresses + Ok(()) + } +} + struct NetworkMap { #[cfg(feature = "non_bitcoin_chain_hash_routing")] channels: HashMap<(u64, Sha256dHash), ChannelInfo>, @@ -77,6 +107,20 @@ struct NetworkMap { nodes: HashMap, } +impl std::fmt::Display for NetworkMap { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?; + for (key, val) in self.channels.iter() { + write!(f, " {} :\n {}\n", key, val)?; + } + write!(f, "[Nodes]\n")?; + for (key, val) in self.nodes.iter() { + write!(f, " {} :\n {}\n", log_pubkey!(key), val)?; + } + Ok(()) + } +} + impl NetworkMap { #[cfg(feature = "non_bitcoin_chain_hash_routing")] #[inline] diff --git a/src/util/macro_logger.rs b/src/util/macro_logger.rs index 19f0294f..465126fa 100644 --- a/src/util/macro_logger.rs +++ b/src/util/macro_logger.rs @@ -3,6 +3,8 @@ use chain::transaction::OutPoint; use bitcoin::util::hash::Sha256dHash; use secp256k1::key::PublicKey; +use ln::router::Route; + use std; pub(crate) struct DebugPubKey<'a>(pub &'a PublicKey); @@ -50,6 +52,23 @@ macro_rules! log_funding_channel_id { } } +#[allow(dead_code)] +pub(crate) struct DebugRoute<'a>(pub &'a Route); +impl<'a> std::fmt::Display for DebugRoute<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + for (i,h) in self.0.hops.iter().enumerate() { + write!(f, "Hop {}\n pubkey {}\n short_channel_id {}\n fee_msat {}\n cltv_expiry_delta {}\n\n", i, log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?; + } + Ok(()) + } +} +#[allow(unused_macros)] +macro_rules! log_route { + ($obj: expr) => { + ::util::macro_logger::DebugRoute(&$obj) + } +} + macro_rules! log_internal { ($self: ident, $lvl:expr, $($arg:tt)+) => ( &$self.logger.log(&::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())); -- 2.30.2