Skip to content

Commit 1fdb052

Browse files
authored
Merge pull request #2118 from TheBlueMatt/2023-03-no-useless-strings
Remove unnecessary heap allocations in log-entry-matching tests
2 parents 86e94c4 + 535dcc7 commit 1fdb052

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4012,7 +4012,7 @@ mod tests {
40124012
use crate::ln::functional_test_utils::*;
40134013
use crate::ln::script::ShutdownScript;
40144014
use crate::util::errors::APIError;
4015-
use crate::util::events::{ClosureReason, MessageSendEventsProvider};
4015+
use crate::util::events::ClosureReason;
40164016
use crate::util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
40174017
use crate::util::ser::{ReadableArgs, Writeable};
40184018
use crate::sync::{Arc, Mutex};

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn test_monitor_and_persister_update_fail() {
149149
// because the update is bogus, ultimately the error that's returned
150150
// should be a PermanentFailure.
151151
if let ChannelMonitorUpdateStatus::PermanentFailure = chain_mon.chain_monitor.update_channel(outpoint, &update) {} else { panic!("Expected monitor error to be permanent"); }
152-
logger.assert_log_regex("lightning::chain::chainmonitor".to_string(), regex::Regex::new("Persistence of ChannelMonitorUpdate for channel [0-9a-f]* in progress").unwrap(), 1);
152+
logger.assert_log_regex("lightning::chain::chainmonitor", regex::Regex::new("Persistence of ChannelMonitorUpdate for channel [0-9a-f]* in progress").unwrap(), 1);
153153
assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
154154
} else { assert!(false); }
155155
}

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,7 +8195,7 @@ mod tests {
81958195
assert!(updates.update_fee.is_none());
81968196
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
81978197

8198-
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Payment preimage didn't match payment hash".to_string(), 1);
8198+
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Payment preimage didn't match payment hash", 1);
81998199
}
82008200

82018201
#[test]
@@ -8238,7 +8238,7 @@ mod tests {
82388238
assert!(updates.update_fee.is_none());
82398239
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
82408240

8241-
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "We don't support MPP keysend payments".to_string(), 1);
8241+
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "We don't support MPP keysend payments", 1);
82428242
}
82438243

82448244
#[test]
@@ -8266,7 +8266,8 @@ mod tests {
82668266

82678267
match nodes[0].node.send_payment(&route, payment_hash, &None, PaymentId(payment_hash.0)).unwrap_err() {
82688268
PaymentSendFailure::ParameterError(APIError::APIMisuseError { ref err }) => {
8269-
assert!(regex::Regex::new(r"Payment secret is required for multi-path payments").unwrap().is_match(err)) },
8269+
assert!(regex::Regex::new(r"Payment secret is required for multi-path payments").unwrap().is_match(err))
8270+
},
82708271
_ => panic!("unexpected error")
82718272
}
82728273
}
@@ -8326,7 +8327,7 @@ mod tests {
83268327
match inbound_payment::verify(bad_payment_hash, &payment_data, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger) {
83278328
Ok(_) => panic!("Unexpected ok"),
83288329
Err(()) => {
8329-
nodes[0].logger.assert_log_contains("lightning::ln::inbound_payment".to_string(), "Failing HTLC with user-generated payment_hash".to_string(), 1);
8330+
nodes[0].logger.assert_log_contains("lightning::ln::inbound_payment", "Failing HTLC with user-generated payment_hash", 1);
83308331
}
83318332
}
83328333

lightning/src/ln/functional_tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn test_insane_channel_opens() {
9494
if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
9595
match action {
9696
&ErrorAction::SendErrorMessage { .. } => {
97-
nodes[1].logger.assert_log_regex("lightning::ln::channelmanager".to_string(), expected_regex, 1);
97+
nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", expected_regex, 1);
9898
},
9999
_ => panic!("unexpected event!"),
100100
}
@@ -1118,7 +1118,7 @@ fn holding_cell_htlc_counting() {
11181118
unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)), true, APIError::ChannelUnavailable { ref err },
11191119
assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
11201120
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1121-
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot push more than their max accepted HTLCs".to_string(), 1);
1121+
nodes[1].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot push more than their max accepted HTLCs", 1);
11221122
}
11231123

11241124
// This should also be true if we try to forward a payment.
@@ -1346,7 +1346,7 @@ fn test_basic_channel_reserve() {
13461346
_ => panic!("Unexpected error variant"),
13471347
}
13481348
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1349-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put our balance under counterparty-announced channel reserve value".to_string(), 1);
1349+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put our balance under counterparty-announced channel reserve value", 1);
13501350

13511351
send_payment(&nodes[0], &vec![&nodes[1]], max_can_send);
13521352
}
@@ -1811,7 +1811,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
18111811
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)), true, APIError::ChannelUnavailable { ref err },
18121812
assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
18131813
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1814-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put us over the max HTLC value in flight our peer will accept".to_string(), 1);
1814+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put us over the max HTLC value in flight our peer will accept", 1);
18151815
}
18161816

18171817
// channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
@@ -1906,7 +1906,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
19061906
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)), true, APIError::ChannelUnavailable { ref err },
19071907
assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
19081908
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1909-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put our balance under counterparty-announced channel reserve value".to_string(), 2);
1909+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put our balance under counterparty-announced channel reserve value", 2);
19101910
}
19111911

19121912
let (route_22, our_payment_hash_22, our_payment_preimage_22, our_payment_secret_22) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
@@ -5987,7 +5987,7 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
59875987
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)), true, APIError::ChannelUnavailable { ref err },
59885988
assert!(regex::Regex::new(r"Cannot send less than their minimum HTLC value \(\d+\)").unwrap().is_match(err)));
59895989
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5990-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send less than their minimum HTLC value".to_string(), 1);
5990+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send less than their minimum HTLC value", 1);
59915991
}
59925992

59935993
#[test]
@@ -6005,7 +6005,7 @@ fn test_update_add_htlc_bolt2_sender_zero_value_msat() {
60056005
assert_eq!(err, "Cannot send 0-msat HTLC"));
60066006

60076007
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6008-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send 0-msat HTLC".to_string(), 1);
6008+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send 0-msat HTLC", 1);
60096009
}
60106010

60116011
#[test]
@@ -6088,7 +6088,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment()
60886088
assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
60896089

60906090
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6091-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot push more than their max accepted HTLCs".to_string(), 1);
6091+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot push more than their max accepted HTLCs", 1);
60926092
}
60936093

60946094
#[test]
@@ -6112,7 +6112,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
61126112
assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
61136113

61146114
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6115-
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put us over the max HTLC value in flight our peer will accept".to_string(), 1);
6115+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send value that would put us over the max HTLC value in flight our peer will accept", 1);
61166116

61176117
send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
61186118
}
@@ -9502,7 +9502,7 @@ fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_e
95029502
}
95039503
nodes[0].node.timer_tick_occurred();
95049504
check_added_monitors!(nodes[0], 1);
9505-
nodes[0].logger.assert_log_contains("lightning::ln::channel".to_string(), "Cannot afford to send new feerate at 2530 without infringing max dust htlc exposure".to_string(), 1);
9505+
nodes[0].logger.assert_log_contains("lightning::ln::channel", "Cannot afford to send new feerate at 2530 without infringing max dust htlc exposure", 1);
95069506
}
95079507

95089508
let _ = nodes[0].node.get_and_clear_pending_msg_events();

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ fn test_inbound_scid_privacy() {
423423
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
424424
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, true, true);
425425

426-
nodes[1].logger.assert_log_regex("lightning::ln::channelmanager".to_string(), regex::Regex::new(r"Refusing to forward over real channel SCID as our counterparty requested").unwrap(), 1);
426+
nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", regex::Regex::new(r"Refusing to forward over real channel SCID as our counterparty requested").unwrap(), 1);
427427

428428
let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
429429
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);

lightning/src/onion_message/functional_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ fn pass_along_path(path: &Vec<MessengerNode>, expected_path_id: Option<[u8; 32]>
104104
node.messenger.handle_onion_message(&prev_node.get_node_pk(), &onion_msg);
105105
if idx == num_nodes - 1 {
106106
node.logger.assert_log_contains(
107-
"lightning::onion_message::messenger".to_string(),
108-
format!("Received an onion message with path_id: {:02x?}", expected_path_id).to_string(), 1);
107+
"lightning::onion_message::messenger",
108+
&format!("Received an onion message with path_id: {:02x?}", expected_path_id), 1);
109109
}
110110
prev_node = node;
111111
}
@@ -218,8 +218,8 @@ fn reply_path() {
218218
pass_along_path(&nodes, None);
219219
// Make sure the last node successfully decoded the reply path.
220220
nodes[3].logger.assert_log_contains(
221-
"lightning::onion_message::messenger".to_string(),
222-
format!("Received an onion message with path_id None and a reply_path").to_string(), 1);
221+
"lightning::onion_message::messenger",
222+
&format!("Received an onion message with path_id None and a reply_path"), 1);
223223

224224
// Destination::BlindedPath
225225
let blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
@@ -228,8 +228,8 @@ fn reply_path() {
228228
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
229229
pass_along_path(&nodes, None);
230230
nodes[3].logger.assert_log_contains(
231-
"lightning::onion_message::messenger".to_string(),
232-
format!("Received an onion message with path_id None and a reply_path").to_string(), 2);
231+
"lightning::onion_message::messenger",
232+
&format!("Received an onion message with path_id None and a reply_path"), 2);
233233
}
234234

235235
#[test]

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,10 @@ impl TestLogger {
642642
/// 1. belongs to the specified module and
643643
/// 2. contains `line` in it.
644644
/// And asserts if the number of occurrences is the same with the given `count`
645-
pub fn assert_log_contains(&self, module: String, line: String, count: usize) {
645+
pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) {
646646
let log_entries = self.lines.lock().unwrap();
647647
let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
648-
m == &module && l.contains(line.as_str())
648+
m == module && l.contains(line)
649649
}).map(|(_, c) | { c }).sum();
650650
assert_eq!(l, count)
651651
}
@@ -654,10 +654,10 @@ impl TestLogger {
654654
/// 1. belong to the specified module and
655655
/// 2. match the given regex pattern.
656656
/// Assert that the number of occurrences equals the given `count`
657-
pub fn assert_log_regex(&self, module: String, pattern: regex::Regex, count: usize) {
657+
pub fn assert_log_regex(&self, module: &str, pattern: regex::Regex, count: usize) {
658658
let log_entries = self.lines.lock().unwrap();
659659
let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
660-
m == &module && pattern.is_match(&l)
660+
m == module && pattern.is_match(&l)
661661
}).map(|(_, c) | { c }).sum();
662662
assert_eq!(l, count)
663663
}

0 commit comments

Comments
 (0)