Skip to content

Commit 8403d2d

Browse files
committed
Offload BDK wallet calls to blocking threads
gdb sampling showed a tokio worker pinned at 100% CPU inside BDK's CheckPointIter::next, called from poll_and_update_listeners_inner every 2s. The iterator walks an O(T*C) linked list (txs * checkpoints) under the wallet mutex, blocking the async runtime the entire time. Move get_unconfirmed_txids() and apply_mempool_txs() into spawn_blocking so they run on the blocking thread pool instead. The Electrum chain source already does this for its equivalent BDK calls; bitcoind was the odd one out.
1 parent 735bb99 commit 8403d2d

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

src/chain/bitcoind.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,17 @@ impl BitcoindChainSource {
436436
let cur_height = channel_manager.current_best_block().height;
437437

438438
let now = SystemTime::now();
439-
let bdk_unconfirmed_txids = onchain_wallet.get_unconfirmed_txids();
439+
440+
let wallet_ref = onchain_wallet.clone();
441+
let bdk_unconfirmed_txids = tokio::task::spawn_blocking(move || {
442+
wallet_ref.get_unconfirmed_txids()
443+
})
444+
.await
445+
.map_err(|e| {
446+
log_error!(self.logger, "Failed to retrieve unconfirmed txids: {}", e);
447+
Error::WalletOperationFailed
448+
})?;
449+
440450
match self
441451
.api_client
442452
.get_updated_mempool_transactions(cur_height, bdk_unconfirmed_txids)
@@ -450,11 +460,18 @@ impl BitcoindChainSource {
450460
evicted_txids.len(),
451461
now.elapsed().unwrap().as_millis()
452462
);
453-
onchain_wallet.apply_mempool_txs(unconfirmed_txs, evicted_txids).unwrap_or_else(
454-
|e| {
455-
log_error!(self.logger, "Failed to apply mempool transactions: {:?}", e);
456-
},
457-
);
463+
464+
let apply_res = tokio::task::spawn_blocking(move || {
465+
onchain_wallet.apply_mempool_txs(unconfirmed_txs, evicted_txids)
466+
})
467+
.await
468+
.map_err(|e| {
469+
log_error!(self.logger, "Applying mempool transactions panicked: {}", e);
470+
Error::WalletOperationFailed
471+
})?;
472+
apply_res.unwrap_or_else(|e| {
473+
log_error!(self.logger, "Failed to apply mempool transactions: {:?}", e);
474+
});
458475
},
459476
Err(e) => {
460477
log_error!(self.logger, "Failed to poll for mempool transactions: {:?}", e);

0 commit comments

Comments
 (0)