Skip to content

Commit

Permalink
Add various small log fixes
Browse files Browse the repository at this point in the history
Add options to configure the logging level and ldk node number (to
identify each ldk node in lndk's integration tests). Also add a
couple helpful logs.
  • Loading branch information
orbitalturtle committed Dec 6, 2023
1 parent 3911ea1 commit 9d37866
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bitcoin = "0.29.0"
bitcoin-bech32 = "0.12"
bech32 = "0.8"
libc = "0.2"
log = "0.4.17"
tempfile = "3.5.0"

chrono = { version = "0.4", default-features = false, features = ["clock"] }
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bitcoin::network::constants::Network;
use lightning::ln::msgs::SocketAddress;
use lightning::util::logger::Level;
use std::fs;
use std::path::PathBuf;
use tempfile::{Builder, TempDir};
Expand All @@ -14,6 +15,8 @@ pub struct LdkUserInfo {
pub ldk_announced_listen_addr: Vec<SocketAddress>,
pub ldk_announced_node_name: [u8; 32],
pub network: Network,
pub log_level: Level,
pub node_num: u8,
}

// Here we initialize three layers of directories needed for our tests. We won't persist ldk data, but we'll persist
Expand Down
16 changes: 11 additions & 5 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bitcoin::secp256k1::PublicKey;
use bitcoin::Network;
use chrono::Utc;
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters};
use lightning::util::logger::{Logger, Record};
use lightning::util::logger::{Logger, Record, Level};
use lightning::util::ser::{Readable, ReadableArgs, Writer};
use std::collections::HashMap;
use std::fs;
Expand All @@ -18,15 +18,21 @@ pub(crate) const OUTBOUND_PAYMENTS_FNAME: &str = "outbound_payments";

pub(crate) struct FilesystemLogger {
logs_dir: String,
level: Level,
// Specifies which ldk node this is in the integration tests.
node_num: u8,
}
impl FilesystemLogger {
pub(crate) fn new(logs_dir: String) -> Self {
fs::create_dir_all(logs_dir.clone()).unwrap();
Self { logs_dir }
pub(crate) fn new(logs_dir: String, level: Level, node_num: u8) -> Self {
fs::create_dir_all(logs_dir.clone()).unwrap();
Self { logs_dir, level, node_num }
}
}
impl Logger for FilesystemLogger {
fn log(&self, record: &Record) {
if record.level < self.level {
return;
}
let raw_log = record.args.to_string();
let log = format!(
"{} {:<5} [{}:{}] {}\n",
Expand All @@ -39,7 +45,7 @@ impl Logger for FilesystemLogger {
record.line,
raw_log
);
let logs_file_path = format!("{}/logs.txt", self.logs_dir.clone());
let logs_file_path = format!("{}/logs-ldk{}.txt", self.logs_dir.clone(), self.node_num);
fs::OpenOptions::new()
.create(true)
.append(true)
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use bitcoin::network::constants::Network;
use bitcoin::BlockHash;
use bitcoin_bech32::WitnessProgram;
use disk::{INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME};
use lightning::log_info;
use lightning::chain::{chainmonitor, ChannelMonitorUpdateStatus};
use lightning::chain::{Filter, Watch};
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
Expand All @@ -37,6 +38,7 @@ use lightning::routing::router::DefaultRouter;
use lightning::routing::scoring::ProbabilisticScoringFeeParameters;
use lightning::sign::{EntropySource, InMemorySigner, KeysManager, SpendableOutputDescriptor};
use lightning::util::config::UserConfig;
use lightning::util::logger::Logger;
use lightning::util::persist::{KVStore, MonitorUpdatingPersister};
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
use lightning::{chain, impl_writeable_tlv_based, impl_writeable_tlv_based_enum};
Expand Down Expand Up @@ -159,7 +161,7 @@ pub(crate) type GossipVerifier = lightning_block_sync::gossip::GossipVerifier<

pub(crate) type P2PGossipSyncType = lightning::routing::gossip::P2PGossipSync<
Arc<NetworkGraph>,
GossipVerifier, // Arc<BitcoindClient>,
GossipVerifier,
Arc<FilesystemLogger>,
>;

Expand All @@ -186,7 +188,7 @@ pub(crate) type OnionMessengerType = OnionMessenger<
Arc<KeysManager>,
Arc<FilesystemLogger>,
Arc<DefaultMessageRouter>,
IgnoringMessageHandler,
IgnoringMessageHandler,
Arc<OnionMessageHandler>,
>;

Expand Down Expand Up @@ -548,7 +550,8 @@ pub async fn start_ldk(args: config::LdkUserInfo, test_name: &str) -> node_api::

// ## Setup
// Step 1: Initialize the Logger
let logger = Arc::new(FilesystemLogger::new(ldk_log_dir.clone()));
let logger = Arc::new(FilesystemLogger::new(ldk_log_dir.clone(), args.log_level, args.node_num));
log_info!(logger, "LDK node is starting up.");

// Initialize our bitcoind client.
let bitcoind_client = match BitcoindClient::new(
Expand Down Expand Up @@ -787,7 +790,7 @@ pub async fn start_ldk(args: config::LdkUserInfo, test_name: &str) -> node_api::

// Step 15: Initialize the PeerManager
let onion_message_handler =
Arc::new(OnionMessageHandler { messages: Arc::new(Mutex::new(VecDeque::new())) });
Arc::new(OnionMessageHandler { messages: Arc::new(Mutex::new(VecDeque::new())), logger: Arc::clone(&logger) });
let channel_manager: Arc<ChannelManager> = Arc::new(channel_manager);
let onion_messenger: Arc<OnionMessengerType> = Arc::new(OnionMessenger::new(
Arc::clone(&keys_manager),
Expand Down
6 changes: 5 additions & 1 deletion src/onion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::disk::FilesystemLogger;
use lightning::log_info;
use lightning::io::Read;
use lightning::ln::msgs::DecodeError;
use lightning::onion_message::{CustomOnionMessageHandler, OnionMessageContents, PendingOnionMessage};
use lightning::util::logger::Logger;
use lightning::util::ser::{Writeable, Writer};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
Expand All @@ -27,13 +30,14 @@ impl Writeable for UserOnionMessageContents {
#[derive(Clone)]
pub struct OnionMessageHandler {
pub messages: Arc<Mutex<VecDeque<UserOnionMessageContents>>>,
pub(crate) logger: Arc<FilesystemLogger>,
}

impl CustomOnionMessageHandler for OnionMessageHandler {
type CustomMessage = UserOnionMessageContents;

fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<UserOnionMessageContents> {
println!("Received a new custom message!");
log_info!(self.logger, "Received a new custom message!");
self.messages.lock().unwrap().push_back(msg.clone());
Some(msg)
}
Expand Down

0 comments on commit 9d37866

Please sign in to comment.