Skip to content

Commit 85e7a78

Browse files
UdjinM6claude
andcommitted
refactor: return const set reference from CWallet::ListLockedCoins()
Avoid a pointless set→vector copy: return `const std::set<COutPoint>&` directly from the wallet layer, and `std::set<COutPoint>` through the interface (copied under lock). In buttonLockAllClicked, fetch the locked set once instead of calling isLockedCoin() per visible UTXO (1 lock acquisition instead of N). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 747f88a commit 85e7a78

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

src/interfaces/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <map>
2222
#include <memory>
2323
#include <psbt.h>
24+
#include <set>
2425
#include <string>
2526
#include <tuple>
2627
#include <type_traits>
@@ -170,7 +171,7 @@ class Wallet
170171
virtual bool isLockedCoin(const COutPoint& output) = 0;
171172

172173
//! List locked coins.
173-
virtual std::vector<COutPoint> listLockedCoins() = 0;
174+
virtual std::set<COutPoint> listLockedCoins() = 0;
174175

175176
//! Lock the provided coins in a single batch.
176177
virtual bool lockCoins(const std::vector<COutPoint>& outputs) = 0;

src/qt/coincontroldialog.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ void CoinControlDialog::buttonSelectAllClicked()
189189
// (un)lock all
190190
void CoinControlDialog::buttonLockAllClicked()
191191
{
192+
// Fetch the wallet-wide locked set once (single cs_wallet acquisition)
193+
const std::set<COutPoint> lockedSet{model->wallet().listLockedCoins()};
194+
192195
// Collect all visible UTXOs; track locked ones separately for the unlock path
193196
// (works in both tree and list modes)
194197
std::vector<COutPoint> outputs;
@@ -200,15 +203,15 @@ void CoinControlDialog::buttonLockAllClicked()
200203
const COutPoint outpt(uint256S(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()),
201204
item->data(COLUMN_ADDRESS, VOutRole).toUInt());
202205
outputs.emplace_back(outpt);
203-
if (model->wallet().isLockedCoin(outpt)) lockedOutputs.emplace_back(outpt);
206+
if (lockedSet.count(outpt)) lockedOutputs.emplace_back(outpt);
204207
} else {
205208
// Tree mode: top-level item is an address group; collect children
206209
for (int j = 0; j < item->childCount(); j++) {
207210
QTreeWidgetItem* child = item->child(j);
208211
const COutPoint outpt(uint256S(child->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()),
209212
child->data(COLUMN_ADDRESS, VOutRole).toUInt());
210213
outputs.emplace_back(outpt);
211-
if (model->wallet().isLockedCoin(outpt)) lockedOutputs.emplace_back(outpt);
214+
if (lockedSet.count(outpt)) lockedOutputs.emplace_back(outpt);
212215
}
213216
}
214217
}

src/wallet/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class WalletImpl : public Wallet
321321
LOCK(m_wallet->cs_wallet);
322322
return m_wallet->IsLockedCoin(output);
323323
}
324-
std::vector<COutPoint> listLockedCoins() override
324+
std::set<COutPoint> listLockedCoins() override
325325
{
326326
LOCK(m_wallet->cs_wallet);
327327
return m_wallet->ListLockedCoins();

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,10 +2742,10 @@ bool CWallet::IsLockedCoin(const COutPoint& output) const
27422742
return setLockedCoins.count(output) > 0;
27432743
}
27442744

2745-
std::vector<COutPoint> CWallet::ListLockedCoins() const
2745+
const std::set<COutPoint>& CWallet::ListLockedCoins() const
27462746
{
27472747
AssertLockHeld(cs_wallet);
2748-
return std::vector<COutPoint>(setLockedCoins.begin(), setLockedCoins.end());
2748+
return setLockedCoins;
27492749
}
27502750

27512751
std::vector<COutPoint> CWallet::ListProTxCoins() const { return ListProTxCoins(setWalletUTXO); }

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
582582
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
583583
bool UnlockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
584584
bool UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
585-
std::vector<COutPoint> ListLockedCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
585+
const std::set<COutPoint>& ListLockedCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
586586
std::vector<COutPoint> ListProTxCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
587587
std::vector<COutPoint> ListProTxCoins(const std::set<COutPoint>& utxos) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
588588
void LockProTxCoins(const std::set<COutPoint>& utxos, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

0 commit comments

Comments
 (0)