Skip to content

Commit 67af5c8

Browse files
committed
fix(cbf): replace lock().unwrap() with expect("lock")
Codebase convention is .expect("lock") rather than .unwrap() for Mutex/RwLock guards. Brings 28 sites in src/chain/cbf.rs and src/wallet/mod.rs in line with the rest of the crate and unblocks the clippy::unwrap_used lint in CI.
1 parent 0ee80ff commit 67af5c8

3 files changed

Lines changed: 36 additions & 29 deletions

File tree

src/builder.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,11 @@ impl ArcedNodeBuilder {
948948
&self, peers: Vec<String>, sync_config: Option<CbfSyncConfig>,
949949
fee_source_config: Option<FeeSourceConfig>,
950950
) {
951-
self.inner.write().unwrap().set_chain_source_cbf(peers, sync_config, fee_source_config);
951+
self.inner.write().expect("lock").set_chain_source_cbf(
952+
peers,
953+
sync_config,
954+
fee_source_config,
955+
);
952956
}
953957

954958
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.

src/chain/cbf.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl CbfChainSource {
181181
///
182182
/// Delegates to [`Self::build_cbf_node_static`], passing all needed fields.
183183
fn build_cbf_node(&self) -> (CbfNode, Client) {
184-
let wallet = self.onchain_wallet.lock().unwrap().clone();
184+
let wallet = self.onchain_wallet.lock().expect("lock").clone();
185185
Self::build_cbf_node_static(
186186
&self.peers,
187187
&self.sync_config,
@@ -262,14 +262,14 @@ impl CbfChainSource {
262262
/// processing tasks — up to [`MAX_RESTART_RETRIES`] consecutive failures
263263
/// with exponential backoff starting at [`INITIAL_BACKOFF_MS`].
264264
pub(crate) fn start(&self, runtime: Arc<Runtime>, onchain_wallet: Arc<Wallet>) {
265-
let mut status = self.cbf_runtime_status.lock().unwrap();
265+
let mut status = self.cbf_runtime_status.lock().expect("lock");
266266
if matches!(*status, CbfRuntimeStatus::Started { .. }) {
267267
debug_assert!(false, "We shouldn't call start if we're already started");
268268
return;
269269
}
270270

271271
// Store the wallet reference for future restarts.
272-
*self.onchain_wallet.lock().unwrap() = Some(Arc::clone(&onchain_wallet));
272+
*self.onchain_wallet.lock().expect("lock") = Some(Arc::clone(&onchain_wallet));
273273

274274
let (node, client) = self.build_cbf_node();
275275

@@ -338,7 +338,7 @@ impl CbfChainSource {
338338
retries,
339339
e,
340340
);
341-
*restart_status.lock().unwrap() = CbfRuntimeStatus::Stopped;
341+
*restart_status.lock().expect("lock") = CbfRuntimeStatus::Stopped;
342342
break;
343343
}
344344
log_error!(
@@ -375,7 +375,7 @@ impl CbfChainSource {
375375
} = new_client;
376376

377377
// Swap the requester so callers pick up the new handle.
378-
*restart_status.lock().unwrap() =
378+
*restart_status.lock().expect("lock") =
379379
CbfRuntimeStatus::Started { requester: new_requester };
380380

381381
current_node = new_node;
@@ -390,7 +390,7 @@ impl CbfChainSource {
390390

391391
/// Shut down the bip157 node and stop all background tasks.
392392
pub(crate) fn stop(&self) {
393-
let mut status = self.cbf_runtime_status.lock().unwrap();
393+
let mut status = self.cbf_runtime_status.lock().expect("lock");
394394
match &*status {
395395
CbfRuntimeStatus::Started { requester } => {
396396
let _ = requester.shutdown();
@@ -428,7 +428,7 @@ impl CbfChainSource {
428428
tip.height,
429429
tip.hash,
430430
);
431-
if let Some(tx) = state.sync_completion_tx.lock().unwrap().take() {
431+
if let Some(tx) = state.sync_completion_tx.lock().expect("lock").take() {
432432
let _ = tx.send(sync_update);
433433
}
434434
},
@@ -457,12 +457,12 @@ impl CbfChainSource {
457457
if skip_height > 0 && indexed_filter.height() <= skip_height {
458458
continue;
459459
}
460-
let scripts = state.watched_scripts.read().unwrap();
460+
let scripts = state.watched_scripts.read().expect("lock");
461461
if !scripts.is_empty() && indexed_filter.contains_any(scripts.iter()) {
462462
state
463463
.matched_block_hashes
464464
.lock()
465-
.unwrap()
465+
.expect("lock")
466466
.push((indexed_filter.height(), indexed_filter.block_hash()));
467467
}
468468
log_trace!(logger, "CBF received filter at height {}", indexed_filter.height(),);
@@ -472,7 +472,7 @@ impl CbfChainSource {
472472
}
473473

474474
fn requester(&self) -> Result<Requester, Error> {
475-
let status = self.cbf_runtime_status.lock().unwrap();
475+
let status = self.cbf_runtime_status.lock().expect("lock");
476476
match &*status {
477477
CbfRuntimeStatus::Started { requester } if requester.is_running() => {
478478
Ok(requester.clone())
@@ -500,21 +500,21 @@ impl CbfChainSource {
500500
/// leaks between scans. The success path performs inline cleanup instead.
501501
fn cleanup_scan_state(&self) {
502502
self.filter_skip_height.store(0, Ordering::Release);
503-
self.watched_scripts.write().unwrap().clear();
504-
self.matched_block_hashes.lock().unwrap().clear();
505-
if let Some(tx) = self.sync_completion_tx.lock().unwrap().take() {
503+
self.watched_scripts.write().expect("lock").clear();
504+
self.matched_block_hashes.lock().expect("lock").clear();
505+
if let Some(tx) = self.sync_completion_tx.lock().expect("lock").take() {
506506
drop(tx);
507507
}
508508
}
509509

510510
/// Register a transaction script for Lightning channel monitoring.
511511
pub(crate) fn register_tx(&self, _txid: &Txid, script_pubkey: &Script) {
512-
self.registered_scripts.lock().unwrap().push(script_pubkey.to_owned());
512+
self.registered_scripts.lock().expect("lock").push(script_pubkey.to_owned());
513513
}
514514

515515
/// Register a watched output script for Lightning channel monitoring.
516516
pub(crate) fn register_output(&self, output: WatchedOutput) {
517-
self.registered_scripts.lock().unwrap().push(output.script_pubkey.clone());
517+
self.registered_scripts.lock().expect("lock").push(output.script_pubkey.clone());
518518
}
519519

520520
/// Run a CBF filter scan: set watched scripts, trigger a rescan, wait for
@@ -530,11 +530,11 @@ impl CbfChainSource {
530530
let _scan_guard = self.scan_lock.lock().await;
531531

532532
self.filter_skip_height.store(skip_before_height.unwrap_or(0), Ordering::Release);
533-
self.matched_block_hashes.lock().unwrap().clear();
534-
*self.watched_scripts.write().unwrap() = scripts;
533+
self.matched_block_hashes.lock().expect("lock").clear();
534+
*self.watched_scripts.write().expect("lock") = scripts;
535535

536536
let (tx, rx) = oneshot::channel();
537-
*self.sync_completion_tx.lock().unwrap() = Some(tx);
537+
*self.sync_completion_tx.lock().expect("lock") = Some(tx);
538538

539539
if let Err(e) = requester.rescan().map_err(|e| {
540540
log_error!(self.logger, "Failed to trigger CBF rescan: {:?}", e);
@@ -547,8 +547,8 @@ impl CbfChainSource {
547547
match rx.await {
548548
Ok(sync_update) => {
549549
self.filter_skip_height.store(0, Ordering::Release);
550-
self.watched_scripts.write().unwrap().clear();
551-
let matched = std::mem::take(&mut *self.matched_block_hashes.lock().unwrap());
550+
self.watched_scripts.write().expect("lock").clear();
551+
let matched = std::mem::take(&mut *self.matched_block_hashes.lock().expect("lock"));
552552
Ok((sync_update, matched))
553553
},
554554
Err(e) => {
@@ -564,7 +564,7 @@ impl CbfChainSource {
564564
&self, onchain_wallet: Arc<Wallet>,
565565
) -> Result<(), Error> {
566566
let receiver_res = {
567-
let mut status_lock = self.onchain_wallet_sync_status.lock().unwrap();
567+
let mut status_lock = self.onchain_wallet_sync_status.lock().expect("lock");
568568
status_lock.register_or_subscribe_pending_sync()
569569
};
570570
if let Some(mut sync_receiver) = receiver_res {
@@ -638,7 +638,7 @@ impl CbfChainSource {
638638
}
639639
.await;
640640

641-
self.onchain_wallet_sync_status.lock().unwrap().propagate_result_to_subscribers(res);
641+
self.onchain_wallet_sync_status.lock().expect("lock").propagate_result_to_subscribers(res);
642642

643643
res
644644
}
@@ -660,7 +660,7 @@ impl CbfChainSource {
660660
// unknown. This mirrors what the Bitcoind chain source does in
661661
// `Wallet::block_connected` by inserting registered tx outputs.
662662
let mut all_scripts = scripts;
663-
all_scripts.extend(self.registered_scripts.lock().unwrap().iter().cloned());
663+
all_scripts.extend(self.registered_scripts.lock().expect("lock").iter().cloned());
664664
let skip_height =
665665
onchain_wallet.latest_checkpoint().height().checked_sub(REORG_SAFETY_BLOCKS);
666666
let (sync_update, matched) = self.run_filter_scan(all_scripts, skip_height).await?;
@@ -710,7 +710,7 @@ impl CbfChainSource {
710710
output_sweeper: Arc<Sweeper>,
711711
) -> Result<(), Error> {
712712
let receiver_res = {
713-
let mut status_lock = self.lightning_wallet_sync_status.lock().unwrap();
713+
let mut status_lock = self.lightning_wallet_sync_status.lock().expect("lock");
714714
status_lock.register_or_subscribe_pending_sync()
715715
};
716716
if let Some(mut sync_receiver) = receiver_res {
@@ -726,7 +726,7 @@ impl CbfChainSource {
726726
let requester = self.requester()?;
727727
let now = Instant::now();
728728

729-
let scripts: Vec<ScriptBuf> = self.registered_scripts.lock().unwrap().clone();
729+
let scripts: Vec<ScriptBuf> = self.registered_scripts.lock().expect("lock").clone();
730730
if scripts.is_empty() {
731731
log_debug!(self.logger, "No registered scripts for CBF lightning sync.");
732732
} else {
@@ -771,7 +771,10 @@ impl CbfChainSource {
771771
}
772772
.await;
773773

774-
self.lightning_wallet_sync_status.lock().unwrap().propagate_result_to_subscribers(res);
774+
self.lightning_wallet_sync_status
775+
.lock()
776+
.expect("lock")
777+
.propagate_result_to_subscribers(res);
775778

776779
res
777780
}

src/wallet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Wallet {
123123
}
124124

125125
pub(crate) fn get_spks_for_cbf_sync(&self, stop_gap: usize) -> Vec<ScriptBuf> {
126-
let wallet = self.inner.lock().unwrap();
126+
let wallet = self.inner.lock().expect("lock");
127127
let mut scripts: Vec<ScriptBuf> =
128128
wallet.spk_index().revealed_spks(..).map(|((_, _), spk)| spk).collect();
129129

@@ -141,7 +141,7 @@ impl Wallet {
141141
}
142142

143143
pub(crate) fn latest_checkpoint(&self) -> bdk_chain::CheckPoint {
144-
self.inner.lock().unwrap().latest_checkpoint()
144+
self.inner.lock().expect("lock").latest_checkpoint()
145145
}
146146

147147
pub(crate) fn get_cached_txs(&self) -> Vec<Arc<Transaction>> {

0 commit comments

Comments
 (0)