Skip to content

Commit ac06a6a

Browse files
authored
Merge pull request #2766 from TheBlueMatt/2023-12-2314-cleanups-2
Marginally optimize test logging
2 parents 6b43153 + ec1a66e commit ac06a6a

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

lightning-background-processor/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1375,9 +1375,9 @@ mod tests {
13751375
let desired_log_1 = "Calling ChannelManager's timer_tick_occurred".to_string();
13761376
let desired_log_2 = "Calling PeerManager's timer_tick_occurred".to_string();
13771377
let desired_log_3 = "Rebroadcasting monitor's pending claims".to_string();
1378-
if log_entries.get(&("lightning_background_processor".to_string(), desired_log_1)).is_some() &&
1379-
log_entries.get(&("lightning_background_processor".to_string(), desired_log_2)).is_some() &&
1380-
log_entries.get(&("lightning_background_processor".to_string(), desired_log_3)).is_some() {
1378+
if log_entries.get(&("lightning_background_processor", desired_log_1)).is_some() &&
1379+
log_entries.get(&("lightning_background_processor", desired_log_2)).is_some() &&
1380+
log_entries.get(&("lightning_background_processor", desired_log_3)).is_some() {
13811381
break
13821382
}
13831383
}
@@ -1556,7 +1556,7 @@ mod tests {
15561556
loop {
15571557
let log_entries = nodes[0].logger.lines.lock().unwrap();
15581558
let expected_log = "Persisting scorer".to_string();
1559-
if log_entries.get(&("lightning_background_processor".to_string(), expected_log)).is_some() {
1559+
if log_entries.get(&("lightning_background_processor", expected_log)).is_some() {
15601560
break
15611561
}
15621562
}
@@ -1580,7 +1580,7 @@ mod tests {
15801580
$sleep;
15811581
let log_entries = $nodes[0].logger.lines.lock().unwrap();
15821582
let loop_counter = "Calling ChannelManager's timer_tick_occurred".to_string();
1583-
if *log_entries.get(&("lightning_background_processor".to_string(), loop_counter))
1583+
if *log_entries.get(&("lightning_background_processor", loop_counter))
15841584
.unwrap_or(&0) > 1
15851585
{
15861586
// Wait until the loop has gone around at least twice.
@@ -1792,7 +1792,7 @@ mod tests {
17921792

17931793
let log_entries = nodes[0].logger.lines.lock().unwrap();
17941794
let expected_log = "Persisting scorer after update".to_string();
1795-
assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
1795+
assert_eq!(*log_entries.get(&("lightning_background_processor", expected_log)).unwrap(), 5);
17961796
}
17971797

17981798
#[tokio::test]
@@ -1838,7 +1838,7 @@ mod tests {
18381838

18391839
let log_entries = nodes[0].logger.lines.lock().unwrap();
18401840
let expected_log = "Persisting scorer after update".to_string();
1841-
assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
1841+
assert_eq!(*log_entries.get(&("lightning_background_processor", expected_log)).unwrap(), 5);
18421842
});
18431843

18441844
let (r1, r2) = tokio::join!(t1, t2);

lightning/src/ln/functional_tests.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ fn test_update_fee_that_funder_cannot_afford() {
693693
*feerate_lock += 4;
694694
}
695695
nodes[0].node.timer_tick_occurred();
696-
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot afford to send new feerate at {}", feerate + 4), 1);
696+
nodes[0].logger.assert_log("lightning::ln::channel", format!("Cannot afford to send new feerate at {}", feerate + 4), 1);
697697
check_added_monitors!(nodes[0], 0);
698698

699699
const INITIAL_COMMITMENT_NUMBER: u64 = 281474976710654;
@@ -768,7 +768,7 @@ fn test_update_fee_that_funder_cannot_afford() {
768768
//check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
769769
//Should produce and error.
770770
nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
771-
nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Funding remote cannot afford proposed new fee".to_string(), 1);
771+
nodes[1].logger.assert_log("lightning::ln::channelmanager", "Funding remote cannot afford proposed new fee".to_string(), 1);
772772
check_added_monitors!(nodes[1], 1);
773773
check_closed_broadcast!(nodes[1], true);
774774
check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: String::from("Funding remote cannot afford proposed new fee") },
@@ -1529,7 +1529,7 @@ fn test_fee_spike_violation_fails_htlc() {
15291529
},
15301530
_ => panic!("Unexpected event"),
15311531
};
1532-
nodes[1].logger.assert_log("lightning::ln::channel".to_string(),
1532+
nodes[1].logger.assert_log("lightning::ln::channel",
15331533
format!("Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", raa_msg.channel_id), 1);
15341534

15351535
check_added_monitors!(nodes[1], 2);
@@ -1617,7 +1617,7 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
16171617

16181618
nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &msg);
16191619
// Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1620-
nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_string(), 1);
1620+
nodes[0].logger.assert_log("lightning::ln::channelmanager", "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_string(), 1);
16211621
assert_eq!(nodes[0].node.list_channels().len(), 0);
16221622
let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
16231623
assert_eq!(err_msg.data, "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value");
@@ -1796,7 +1796,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
17961796

17971797
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
17981798
// Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1799-
nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote HTLC add would put them under remote reserve value".to_string(), 1);
1799+
nodes[1].logger.assert_log("lightning::ln::channelmanager", "Remote HTLC add would put them under remote reserve value".to_string(), 1);
18001800
assert_eq!(nodes[1].node.list_channels().len(), 1);
18011801
let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
18021802
assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
@@ -5930,7 +5930,7 @@ fn test_fail_holding_cell_htlc_upon_free() {
59305930
// us to surface its failure to the user.
59315931
chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
59325932
assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
5933-
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 1 HTLC updates in channel {}", chan.2), 1);
5933+
nodes[0].logger.assert_log("lightning::ln::channel", format!("Freeing holding cell with 1 HTLC updates in channel {}", chan.2), 1);
59345934

59355935
// Check that the payment failed to be sent out.
59365936
let events = nodes[0].node.get_and_clear_pending_events();
@@ -6018,7 +6018,7 @@ fn test_free_and_fail_holding_cell_htlcs() {
60186018
// to surface its failure to the user. The first payment should succeed.
60196019
chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
60206020
assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
6021-
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 2 HTLC updates in channel {}", chan.2), 1);
6021+
nodes[0].logger.assert_log("lightning::ln::channel", format!("Freeing holding cell with 2 HTLC updates in channel {}", chan.2), 1);
60226022

60236023
// Check that the second payment failed to be sent out.
60246024
let events = nodes[0].node.get_and_clear_pending_events();
@@ -6292,7 +6292,7 @@ fn test_update_add_htlc_bolt2_receiver_zero_value_msat() {
62926292
updates.update_add_htlcs[0].amount_msat = 0;
62936293

62946294
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6295-
nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote side tried to send a 0-msat HTLC".to_string(), 1);
6295+
nodes[1].logger.assert_log("lightning::ln::channelmanager", "Remote side tried to send a 0-msat HTLC".to_string(), 1);
62966296
check_closed_broadcast!(nodes[1], true).unwrap();
62976297
check_added_monitors!(nodes[1], 1);
62986298
check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote side tried to send a 0-msat HTLC".to_string() },
@@ -9843,10 +9843,10 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
98439843
// Outbound dust balance: 6399 sats
98449844
let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * (dust_inbound_htlc_on_holder_tx + 1);
98459845
let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * dust_outbound_htlc_on_holder_tx + dust_inbound_htlc_on_holder_tx_msat;
9846-
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, max_dust_htlc_exposure_msat), 1);
9846+
nodes[0].logger.assert_log("lightning::ln::channel", format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, max_dust_htlc_exposure_msat), 1);
98479847
} else {
98489848
// Outbound dust balance: 5200 sats
9849-
nodes[0].logger.assert_log("lightning::ln::channel".to_string(),
9849+
nodes[0].logger.assert_log("lightning::ln::channel",
98509850
format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
98519851
dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx - 1) + dust_htlc_on_counterparty_tx_msat + 4,
98529852
max_dust_htlc_exposure_msat), 1);

lightning/src/ln/reload_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ fn do_test_data_loss_protect(reconnect_panicing: bool, substantially_old: bool,
526526
// `not_stale` to test the boundary condition.
527527
let pay_params = PaymentParameters::for_keysend(nodes[1].node.get_our_node_id(), 100, false);
528528
let route_params = RouteParameters::from_payment_params_and_value(pay_params, 40000);
529-
nodes[0].node.send_spontaneous_payment_with_retry(None, RecipientOnionFields::spontaneous_empty(), PaymentId([0; 32]), route_params, Retry::Attempts(0));
529+
nodes[0].node.send_spontaneous_payment_with_retry(None, RecipientOnionFields::spontaneous_empty(), PaymentId([0; 32]), route_params, Retry::Attempts(0)).unwrap();
530530
check_added_monitors(&nodes[0], 1);
531531
let update_add_commit = SendEvent::from_node(&nodes[0]);
532532

lightning/src/util/test_utils.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,8 @@ impl events::MessageSendEventsProvider for TestRoutingMessageHandler {
930930
pub struct TestLogger {
931931
level: Level,
932932
pub(crate) id: String,
933-
pub lines: Mutex<HashMap<(String, String), usize>>,
934-
pub context: Mutex<HashMap<(String, Option<PublicKey>, Option<ChannelId>), usize>>,
933+
pub lines: Mutex<HashMap<(&'static str, String), usize>>,
934+
pub context: Mutex<HashMap<(&'static str, Option<PublicKey>, Option<ChannelId>), usize>>,
935935
}
936936

937937
impl TestLogger {
@@ -949,7 +949,7 @@ impl TestLogger {
949949
pub fn enable(&mut self, level: Level) {
950950
self.level = level;
951951
}
952-
pub fn assert_log(&self, module: String, line: String, count: usize) {
952+
pub fn assert_log(&self, module: &str, line: String, count: usize) {
953953
let log_entries = self.lines.lock().unwrap();
954954
assert_eq!(log_entries.get(&(module, line)), Some(&count));
955955
}
@@ -961,7 +961,7 @@ impl TestLogger {
961961
pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) {
962962
let log_entries = self.lines.lock().unwrap();
963963
let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
964-
m == module && l.contains(line)
964+
*m == module && l.contains(line)
965965
}).map(|(_, c) | { c }).sum();
966966
assert_eq!(l, count)
967967
}
@@ -974,7 +974,7 @@ impl TestLogger {
974974
pub fn assert_log_regex(&self, module: &str, pattern: regex::Regex, count: usize) {
975975
let log_entries = self.lines.lock().unwrap();
976976
let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
977-
m == module && pattern.is_match(&l)
977+
*m == module && pattern.is_match(&l)
978978
}).map(|(_, c) | { c }).sum();
979979
assert_eq!(l, count)
980980
}
@@ -983,18 +983,15 @@ impl TestLogger {
983983
&self, module: &str, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>, count: usize
984984
) {
985985
let context_entries = self.context.lock().unwrap();
986-
let l: usize = context_entries.iter()
987-
.filter(|&(&(ref m, ref p, ref c), _)| m == module && *p == peer_id && *c == channel_id)
988-
.map(|(_, c) | c)
989-
.sum();
990-
assert_eq!(l, count)
986+
let l = context_entries.get(&(module, peer_id, channel_id)).unwrap();
987+
assert_eq!(*l, count)
991988
}
992989
}
993990

994991
impl Logger for TestLogger {
995992
fn log(&self, record: Record) {
996-
*self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
997-
*self.context.lock().unwrap().entry((record.module_path.to_string(), record.peer_id, record.channel_id)).or_insert(0) += 1;
993+
*self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1;
994+
*self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1;
998995
if record.level >= self.level {
999996
#[cfg(all(not(ldk_bench), feature = "std"))] {
1000997
let pfx = format!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line);

0 commit comments

Comments
 (0)