Skip to content

Commit

Permalink
test: clean up MockLogger and LogWriter setup
Browse files Browse the repository at this point in the history
- Revert MockLogger configuration and assertions
- Remove clutter from setting up LogWriter across all tests
  • Loading branch information
enigbe committed Dec 17, 2024
1 parent 9f87227 commit e07716d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 72 deletions.
23 changes: 3 additions & 20 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,6 @@ impl MockLogger {
}
}

/// [`MockLogger`] as `log` logger - destination for [`Writer::LogFacadeWriter`]
/// to write logs to.
///
/// [`Writer::LogFacadeWriter`]: ldk_node::logger::Writer::LogFacadeWriter
impl Log for MockLogger {
fn log(&self, record: &log::Record) {
let message = format!(
Expand Down Expand Up @@ -333,11 +329,11 @@ pub(crate) use setup_builder;

pub(crate) fn setup_two_nodes(
chain_source: &TestChainSource, allow_0conf: bool, anchor_channels: bool,
anchors_trusted_no_reserve: bool, log_writer: TestLogWriter,
anchors_trusted_no_reserve: bool,
) -> (TestNode, TestNode) {
println!("== Node A ==");
let config_a = random_config(anchor_channels);
let node_a = setup_node(chain_source, config_a, None, log_writer.clone());
let node_a = setup_node(chain_source, config_a, None);

println!("\n== Node B ==");
let mut config_b = random_config(anchor_channels);
Expand All @@ -352,13 +348,12 @@ pub(crate) fn setup_two_nodes(
.trusted_peers_no_reserve
.push(node_a.node_id());
}
let node_b = setup_node(chain_source, config_b, None, log_writer);
let node_b = setup_node(chain_source, config_b, None);
(node_a, node_b)
}

pub(crate) fn setup_node(
chain_source: &TestChainSource, config: Config, seed_bytes: Option<Vec<u8>>,
log_writer: TestLogWriter,
) -> TestNode {
setup_builder!(builder, config);
match chain_source {
Expand All @@ -379,18 +374,6 @@ pub(crate) fn setup_node(
},
}

match log_writer {
TestLogWriter::File(fs_config) => {
builder.set_filesystem_logger(fs_config);
},
TestLogWriter::LogFacade(log_level) => {
builder.set_log_facade_logger(log_level);
},
TestLogWriter::Custom(log_writer) => {
builder.set_custom_logger(log_writer);
},
}

if let Some(seed) = seed_bytes {
builder.set_entropy_seed_bytes(seed).unwrap();
}
Expand Down
71 changes: 19 additions & 52 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ mod common;

use common::{
do_channel_full_cycle, expect_channel_ready_event, expect_event, expect_payment_received_event,
expect_payment_successful_event, generate_blocks_and_wait, init_custom_logger, init_log_logger,
open_channel, premine_and_distribute_funds, random_config, setup_bitcoind_and_electrsd,
setup_builder, setup_node, setup_two_nodes, wait_for_tx, TestChainSource, TestLogWriter,
TestSyncStore,
expect_payment_successful_event, generate_blocks_and_wait, open_channel,
premine_and_distribute_funds, random_config, setup_bitcoind_and_electrsd, setup_builder,
setup_node, setup_two_nodes, wait_for_tx, TestChainSource, TestSyncStore,
};

use ldk_node::config::{EsploraSyncConfig, FilesystemLoggerConfig};
use ldk_node::logger::LogLevel;
use ldk_node::payment::{PaymentKind, QrPaymentResult, SendingParameters};
use ldk_node::{Builder, Event, NodeError};

Expand All @@ -34,62 +32,55 @@ use std::sync::Arc;
fn channel_full_cycle() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
}

#[test]
fn channel_full_cycle_bitcoind() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::BitcoindRpc(&bitcoind);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, false);
}

#[test]
fn channel_full_cycle_force_close() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
}

#[test]
fn channel_full_cycle_force_close_trusted_no_reserve() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, true, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, true);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, true, true);
}

#[test]
fn channel_full_cycle_0conf() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, true, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, true, true, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, true, true, false)
}

#[test]
fn channel_full_cycle_legacy_staticremotekey() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);
do_channel_full_cycle(node_a, node_b, &bitcoind.client, &electrsd.client, false, false, false);
}

#[test]
fn channel_open_fails_when_funds_insufficient() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let addr_a = node_a.onchain_payment().new_address().unwrap();
let addr_b = node_b.onchain_payment().new_address().unwrap();
Expand Down Expand Up @@ -289,8 +280,7 @@ fn start_stop_reinit() {
fn onchain_spend_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let addr_a = node_a.onchain_payment().new_address().unwrap();
let addr_b = node_b.onchain_payment().new_address().unwrap();
Expand Down Expand Up @@ -391,9 +381,7 @@ fn onchain_wallet_recovery() {
let seed_bytes = vec![42u8; 64];

let original_config = random_config(true);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let original_node =
setup_node(&chain_source, original_config, Some(seed_bytes.clone()), log_writer);
let original_node = setup_node(&chain_source, original_config, Some(seed_bytes.clone()));

let premine_amount_sat = 100_000;

Expand Down Expand Up @@ -438,8 +426,7 @@ fn onchain_wallet_recovery() {

// Now we start from scratch, only the seed remains the same.
let recovered_config = random_config(true);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let recovered_node = setup_node(&chain_source, recovered_config, Some(seed_bytes), log_writer);
let recovered_node = setup_node(&chain_source, recovered_config, Some(seed_bytes));

recovered_node.sync_wallets().unwrap();
assert_eq!(
Expand Down Expand Up @@ -482,8 +469,7 @@ fn sign_verify_msg() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let config = random_config(true);
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let node = setup_node(&chain_source, config, None, log_writer);
let node = setup_node(&chain_source, config, None);

// Tests arbitrary message signing and later verification
let msg = "OK computer".as_bytes();
Expand All @@ -501,8 +487,7 @@ fn connection_restart_behavior() {
fn do_connection_restart_behavior(persist: bool) {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, false, false);

let node_id_a = node_a.node_id();
let node_id_b = node_b.node_id();
Expand Down Expand Up @@ -554,8 +539,7 @@ fn do_connection_restart_behavior(persist: bool) {
fn concurrent_connections_succeed() {
let (_bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let node_a = Arc::new(node_a);
let node_b = Arc::new(node_b);
Expand Down Expand Up @@ -586,8 +570,7 @@ fn concurrent_connections_succeed() {
fn simple_bolt12_send_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);
let log_writer = TestLogWriter::File(FilesystemLoggerConfig::default());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premine_amount_sat = 5_000_000;
Expand Down Expand Up @@ -796,10 +779,7 @@ fn generate_bip21_uri() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);

// Setup custom logger.
let mock_logger = init_custom_logger();
let log_writer = TestLogWriter::Custom(mock_logger.clone());
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premined_sats = 5_000_000;
Expand Down Expand Up @@ -835,21 +815,14 @@ fn generate_bip21_uri() {
},
Err(e) => panic!("Failed to generate URI: {:?}", e),
}

let logs = mock_logger.retrieve_logs();
let last_log_entry = logs.last().unwrap();
assert!(last_log_entry.contains("[INFO] Invoice created:"));
}

#[test]
fn unified_qr_send_receive() {
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
let chain_source = TestChainSource::Esplora(&electrsd);

// Setup `log` facade logger.
let mock_logger = init_log_logger(log::LevelFilter::Trace);
let log_writer = TestLogWriter::LogFacade(LogLevel::Trace);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false, log_writer);
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);

let address_a = node_a.onchain_payment().new_address().unwrap();
let premined_sats = 5_000_000;
Expand Down Expand Up @@ -955,10 +928,4 @@ fn unified_qr_send_receive() {

assert_eq!(node_b.list_balances().total_onchain_balance_sats, 800_000);
assert_eq!(node_b.list_balances().total_lightning_balance_sats, 200_000);

assert!(mock_logger
.retrieve_logs()
.last()
.unwrap()
.contains("Incremental sync of on-chain wallet finished"),);
}

0 comments on commit e07716d

Please sign in to comment.