Skip to content

Commit 4988c78

Browse files
committed
rustfmt: Prepare util/test_utils.rs
.. we pull out `Mutex` field initialization into dedicated variables as they might otherwise land on the same line when formatting, which might lead to lockorder violation false-positives when compiled with the `backtrace` feature.
1 parent 093029b commit 4988c78

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

lightning/src/util/test_utils.rs

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ pub struct TestFeeEstimator {
103103
}
104104
impl TestFeeEstimator {
105105
pub fn new(sat_per_kw: u32) -> Self {
106+
let sat_per_kw = Mutex::new(sat_per_kw);
107+
let target_override = Mutex::new(new_hash_map());
106108
Self {
107-
sat_per_kw: Mutex::new(sat_per_kw),
108-
target_override: Mutex::new(new_hash_map()),
109+
sat_per_kw,
110+
target_override,
109111
}
110112
}
111113
}
@@ -136,11 +138,13 @@ impl<'a> TestRouter<'a> {
136138
scorer: &'a RwLock<TestScorer>,
137139
) -> Self {
138140
let entropy_source = Arc::new(RandomBytes::new([42; 32]));
141+
let next_routes = Mutex::new(VecDeque::new());
142+
let next_blinded_payment_paths = Mutex::new(Vec::new());
139143
Self {
140144
router: DefaultRouter::new(network_graph.clone(), logger, entropy_source, scorer, Default::default()),
141145
network_graph,
142-
next_routes: Mutex::new(VecDeque::new()),
143-
next_blinded_payment_paths: Mutex::new(Vec::new()),
146+
next_routes,
147+
next_blinded_payment_paths,
144148
scorer,
145149
}
146150
}
@@ -351,14 +355,19 @@ pub struct TestChainMonitor<'a> {
351355
}
352356
impl<'a> TestChainMonitor<'a> {
353357
pub fn new(chain_source: Option<&'a TestChainSource>, broadcaster: &'a dyn chaininterface::BroadcasterInterface, logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator, persister: &'a dyn chainmonitor::Persist<TestChannelSigner>, keys_manager: &'a TestKeysInterface) -> Self {
358+
let added_monitors = Mutex::new(Vec::new());
359+
let monitor_updates = Mutex::new(new_hash_map());
360+
let latest_monitor_update_id = Mutex::new(new_hash_map());
361+
let expect_channel_force_closed = Mutex::new(None);
362+
let expect_monitor_round_trip_fail = Mutex::new(None);
354363
Self {
355-
added_monitors: Mutex::new(Vec::new()),
356-
monitor_updates: Mutex::new(new_hash_map()),
357-
latest_monitor_update_id: Mutex::new(new_hash_map()),
364+
added_monitors,
365+
monitor_updates,
366+
latest_monitor_update_id,
358367
chain_monitor: chainmonitor::ChainMonitor::new(chain_source, broadcaster, logger, fee_estimator, persister),
359368
keys_manager,
360-
expect_channel_force_closed: Mutex::new(None),
361-
expect_monitor_round_trip_fail: Mutex::new(None),
369+
expect_channel_force_closed,
370+
expect_monitor_round_trip_fail,
362371
}
363372
}
364373

@@ -450,10 +459,12 @@ pub(crate) struct WatchtowerPersister {
450459
impl WatchtowerPersister {
451460
#[cfg(test)]
452461
pub(crate) fn new(destination_script: ScriptBuf) -> Self {
462+
let unsigned_justice_tx_data = Mutex::new(new_hash_map());
463+
let watchtower_state = Mutex::new(new_hash_map());
453464
WatchtowerPersister {
454465
persister: TestPersister::new(),
455-
unsigned_justice_tx_data: Mutex::new(new_hash_map()),
456-
watchtower_state: Mutex::new(new_hash_map()),
466+
unsigned_justice_tx_data,
467+
watchtower_state,
457468
destination_script,
458469
}
459470
}
@@ -552,10 +563,13 @@ pub struct TestPersister {
552563
}
553564
impl TestPersister {
554565
pub fn new() -> Self {
566+
let update_rets = Mutex::new(VecDeque::new());
567+
let offchain_monitor_updates = Mutex::new(new_hash_map());
568+
let chain_sync_monitor_persistences = Mutex::new(VecDeque::new());
555569
Self {
556-
update_rets: Mutex::new(VecDeque::new()),
557-
offchain_monitor_updates: Mutex::new(new_hash_map()),
558-
chain_sync_monitor_persistences: Mutex::new(VecDeque::new())
570+
update_rets,
571+
offchain_monitor_updates,
572+
chain_sync_monitor_persistences
559573
}
560574
}
561575

@@ -694,14 +708,17 @@ pub struct TestBroadcaster {
694708

695709
impl TestBroadcaster {
696710
pub fn new(network: Network) -> Self {
711+
let txn_broadcasted = Mutex::new(Vec::new());
712+
let blocks = Arc::new(Mutex::new(vec![(genesis_block(network), 0)]));
697713
Self {
698-
txn_broadcasted: Mutex::new(Vec::new()),
699-
blocks: Arc::new(Mutex::new(vec![(genesis_block(network), 0)])),
714+
txn_broadcasted,
715+
blocks,
700716
}
701717
}
702718

703719
pub fn with_blocks(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> Self {
704-
Self { txn_broadcasted: Mutex::new(Vec::new()), blocks }
720+
let txn_broadcasted = Mutex::new(Vec::new());
721+
Self { txn_broadcasted, blocks }
705722
}
706723

707724
pub fn txn_broadcast(&self) -> Vec<Transaction> {
@@ -749,10 +766,13 @@ impl TestChannelMessageHandler {
749766

750767
impl TestChannelMessageHandler {
751768
pub fn new(chain_hash: ChainHash) -> Self {
769+
let pending_events = Mutex::new(Vec::new());
770+
let expected_recv_msgs = Mutex::new(None);
771+
let connected_peers = Mutex::new(new_hash_set());
752772
TestChannelMessageHandler {
753-
pending_events: Mutex::new(Vec::new()),
754-
expected_recv_msgs: Mutex::new(None),
755-
connected_peers: Mutex::new(new_hash_set()),
773+
pending_events,
774+
expected_recv_msgs,
775+
connected_peers,
756776
chain_hash,
757777
}
758778
}
@@ -991,10 +1011,11 @@ pub struct TestRoutingMessageHandler {
9911011

9921012
impl TestRoutingMessageHandler {
9931013
pub fn new() -> Self {
1014+
let pending_events = Mutex::new(vec![]);
9941015
TestRoutingMessageHandler {
9951016
chan_upds_recvd: AtomicUsize::new(0),
9961017
chan_anns_recvd: AtomicUsize::new(0),
997-
pending_events: Mutex::new(vec![]),
1018+
pending_events,
9981019
request_full_sync: AtomicBool::new(false),
9991020
announcement_available_for_sync: AtomicBool::new(false),
10001021
}
@@ -1109,10 +1130,12 @@ impl TestLogger {
11091130
Self::with_id("".to_owned())
11101131
}
11111132
pub fn with_id(id: String) -> TestLogger {
1133+
let lines = Mutex::new(new_hash_map());
1134+
let context = Mutex::new(new_hash_map());
11121135
TestLogger {
11131136
id,
1114-
lines: Mutex::new(new_hash_map()),
1115-
context: Mutex::new(new_hash_map()),
1137+
lines,
1138+
context,
11161139
}
11171140
}
11181141
pub fn assert_log(&self, module: &str, line: String, count: usize) {
@@ -1330,14 +1353,19 @@ impl SignerProvider for TestKeysInterface {
13301353
impl TestKeysInterface {
13311354
pub fn new(seed: &[u8; 32], network: Network) -> Self {
13321355
let now = Duration::from_secs(genesis_block(network).header.time as u64);
1356+
let override_random_bytes = Mutex::new(None);
1357+
let enforcement_states = Mutex::new(new_hash_map());
1358+
let expectations = Mutex::new(None);
1359+
let unavailable_signers_ops = Mutex::new(new_hash_map());
1360+
let next_signer_disabled_ops = Mutex::new(new_hash_set());
13331361
Self {
13341362
backing: sign::PhantomKeysManager::new(seed, now.as_secs(), now.subsec_nanos(), seed),
1335-
override_random_bytes: Mutex::new(None),
1363+
override_random_bytes,
13361364
disable_revocation_policy_check: false,
1337-
enforcement_states: Mutex::new(new_hash_map()),
1338-
expectations: Mutex::new(None),
1339-
unavailable_signers_ops: Mutex::new(new_hash_map()),
1340-
next_signer_disabled_ops: Mutex::new(new_hash_set()),
1365+
enforcement_states,
1366+
expectations,
1367+
unavailable_signers_ops,
1368+
next_signer_disabled_ops,
13411369
}
13421370
}
13431371

@@ -1403,12 +1431,15 @@ pub struct TestChainSource {
14031431
impl TestChainSource {
14041432
pub fn new(network: Network) -> Self {
14051433
let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1434+
let utxo_ret = Mutex::new(UtxoResult::Sync(Ok(TxOut { value: Amount::MAX, script_pubkey })));
1435+
let watched_txn = Mutex::new(new_hash_set());
1436+
let watched_outputs = Mutex::new(new_hash_set());
14061437
Self {
14071438
chain_hash: ChainHash::using_genesis_block(network),
1408-
utxo_ret: Mutex::new(UtxoResult::Sync(Ok(TxOut { value: Amount::MAX, script_pubkey }))),
1439+
utxo_ret,
14091440
get_utxo_call_count: AtomicUsize::new(0),
1410-
watched_txn: Mutex::new(new_hash_set()),
1411-
watched_outputs: Mutex::new(new_hash_set()),
1441+
watched_txn,
1442+
watched_outputs,
14121443
}
14131444
}
14141445
pub fn remove_watched_txn_and_outputs(&self, outpoint: OutPoint, script_pubkey: ScriptBuf) {

0 commit comments

Comments
 (0)