Skip to content

Commit 99c10b0

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 d0de8a0 commit 99c10b0

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

lightning/src/util/test_utils.rs

Lines changed: 60 additions & 30 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
}
@@ -990,10 +1010,11 @@ pub struct TestRoutingMessageHandler {
9901010

9911011
impl TestRoutingMessageHandler {
9921012
pub fn new() -> Self {
1013+
let pending_events = Mutex::new(vec![]);
9931014
TestRoutingMessageHandler {
9941015
chan_upds_recvd: AtomicUsize::new(0),
9951016
chan_anns_recvd: AtomicUsize::new(0),
996-
pending_events: Mutex::new(vec![]),
1017+
pending_events,
9971018
request_full_sync: AtomicBool::new(false),
9981019
}
9991020
}
@@ -1103,10 +1124,12 @@ impl TestLogger {
11031124
Self::with_id("".to_owned())
11041125
}
11051126
pub fn with_id(id: String) -> TestLogger {
1127+
let lines = Mutex::new(new_hash_map());
1128+
let context = Mutex::new(new_hash_map());
11061129
TestLogger {
11071130
id,
1108-
lines: Mutex::new(new_hash_map()),
1109-
context: Mutex::new(new_hash_map()),
1131+
lines,
1132+
context,
11101133
}
11111134
}
11121135
pub fn assert_log(&self, module: &str, line: String, count: usize) {
@@ -1331,13 +1354,17 @@ impl SignerProvider for TestKeysInterface {
13311354
impl TestKeysInterface {
13321355
pub fn new(seed: &[u8; 32], network: Network) -> Self {
13331356
let now = Duration::from_secs(genesis_block(network).header.time as u64);
1357+
let override_random_bytes = Mutex::new(None);
1358+
let enforcement_states = Mutex::new(new_hash_map());
1359+
let expectations = Mutex::new(None);
1360+
let unavailable_signers_ops = Mutex::new(new_hash_map());
13341361
Self {
13351362
backing: sign::PhantomKeysManager::new(seed, now.as_secs(), now.subsec_nanos(), seed),
1336-
override_random_bytes: Mutex::new(None),
1363+
override_random_bytes,
13371364
disable_revocation_policy_check: false,
1338-
enforcement_states: Mutex::new(new_hash_map()),
1339-
expectations: Mutex::new(None),
1340-
unavailable_signers_ops: Mutex::new(new_hash_map()),
1365+
enforcement_states,
1366+
expectations,
1367+
unavailable_signers_ops,
13411368
}
13421369
}
13431370

@@ -1403,12 +1430,15 @@ pub struct TestChainSource {
14031430
impl TestChainSource {
14041431
pub fn new(network: Network) -> Self {
14051432
let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1433+
let utxo_ret = Mutex::new(UtxoResult::Sync(Ok(TxOut { value: Amount::MAX, script_pubkey })));
1434+
let watched_txn = Mutex::new(new_hash_set());
1435+
let watched_outputs = Mutex::new(new_hash_set());
14061436
Self {
14071437
chain_hash: ChainHash::using_genesis_block(network),
1408-
utxo_ret: Mutex::new(UtxoResult::Sync(Ok(TxOut { value: Amount::MAX, script_pubkey }))),
1438+
utxo_ret,
14091439
get_utxo_call_count: AtomicUsize::new(0),
1410-
watched_txn: Mutex::new(new_hash_set()),
1411-
watched_outputs: Mutex::new(new_hash_set()),
1440+
watched_txn,
1441+
watched_outputs,
14121442
}
14131443
}
14141444
pub fn remove_watched_txn_and_outputs(&self, outpoint: OutPoint, script_pubkey: ScriptBuf) {

0 commit comments

Comments
 (0)