Skip to content

Commit eb9ee69

Browse files
committed
wallet: split off ListProTxCoins to allow supplying your own set
Plus clean up `ListLockedCoins` a bit...
1 parent 0972dfe commit eb9ee69

File tree

8 files changed

+49
-69
lines changed

8 files changed

+49
-69
lines changed

src/interfaces/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ class Wallet
145145
virtual bool isLockedCoin(const COutPoint& output) = 0;
146146

147147
//! List locked coins.
148-
virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
148+
virtual std::vector<COutPoint> listLockedCoins() = 0;
149149

150150
//! List protx coins.
151-
virtual void listProTxCoins(std::vector<COutPoint>& vOutpts) = 0;
151+
virtual std::vector<COutPoint> listProTxCoins() = 0;
152152

153153
//! Create transaction.
154154
virtual CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,

src/qt/coincontroldialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,7 @@ void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
426426
// shows count of locked unspent outputs
427427
void CoinControlDialog::updateLabelLocked()
428428
{
429-
std::vector<COutPoint> vOutpts;
430-
model->wallet().listLockedCoins(vOutpts);
429+
std::vector<COutPoint> vOutpts{model->wallet().listLockedCoins()};
431430
if (vOutpts.size() > 0)
432431
{
433432
ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));

src/qt/masternodelist.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ void MasternodeList::updateDIP3List()
204204

205205
std::set<COutPoint> setOutpts;
206206
if (walletModel && ui->checkBoxMyMasternodesOnly->isChecked()) {
207-
std::vector<COutPoint> vOutpts;
208-
walletModel->wallet().listProTxCoins(vOutpts);
209-
for (const auto& outpt : vOutpts) {
207+
for (const auto& outpt : walletModel->wallet().listProTxCoins()) {
210208
setOutpts.emplace(outpt);
211209
}
212210
}

src/rpc/evo.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,10 +1390,8 @@ static RPCHelpMan protx_list()
13901390
throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid height specified");
13911391
}
13921392

1393-
std::vector<COutPoint> vOutpts;
1394-
wallet->ListProTxCoins(vOutpts);
13951393
std::set<COutPoint> setOutpts;
1396-
for (const auto& outpt : vOutpts) {
1394+
for (const auto& outpt : wallet->ListProTxCoins()) {
13971395
setOutpts.emplace(outpt);
13981396
}
13991397

src/wallet/interfaces.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,13 @@ class WalletImpl : public Wallet
255255
LOCK(m_wallet->cs_wallet);
256256
return m_wallet->IsLockedCoin(output.hash, output.n);
257257
}
258-
void listLockedCoins(std::vector<COutPoint>& outputs) override
258+
std::vector<COutPoint> listLockedCoins() override
259259
{
260-
LOCK(m_wallet->cs_wallet);
261-
return m_wallet->ListLockedCoins(outputs);
260+
return WITH_LOCK(m_wallet->cs_wallet, return m_wallet->ListLockedCoins());
262261
}
263-
void listProTxCoins(std::vector<COutPoint>& outputs) override
262+
std::vector<COutPoint> listProTxCoins() override
264263
{
265-
LOCK(m_wallet->cs_wallet);
266-
return m_wallet->ListProTxCoins(outputs);
264+
return WITH_LOCK(m_wallet->cs_wallet, return m_wallet->ListProTxCoins());
267265
}
268266
CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
269267
const CCoinControl& coin_control,

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,14 +2371,9 @@ static RPCHelpMan listlockunspent()
23712371

23722372
LOCK(pwallet->cs_wallet);
23732373

2374-
std::vector<COutPoint> vOutpts;
2375-
pwallet->ListLockedCoins(vOutpts);
2376-
23772374
UniValue ret(UniValue::VARR);
2378-
2379-
for (const COutPoint& outpt : vOutpts) {
2375+
for (const COutPoint& outpt : pwallet->ListLockedCoins()) {
23802376
UniValue o(UniValue::VOBJ);
2381-
23822377
o.pushKV("txid", outpt.hash.GetHex());
23832378
o.pushKV("vout", (int)outpt.n);
23842379
ret.push_back(o);

src/wallet/wallet.cpp

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -922,18 +922,16 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
922922
wtx.nTimeSmart = ComputeTimeSmart(wtx);
923923
AddToSpends(hash, &batch);
924924

925-
std::vector<std::pair<const CTransactionRef&, unsigned int>> outputs;
926-
for(unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
925+
std::set<COutPoint> candidates;
926+
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
927927
if (IsMine(wtx.tx->vout[i]) && !IsSpent(hash, i)) {
928928
setWalletUTXO.insert(COutPoint(hash, i));
929-
outputs.emplace_back(wtx.tx, i);
929+
candidates.emplace(hash, i);
930930
}
931931
}
932932
// TODO: refactor duplicated code between CWallet::AddToWallet and CWallet::AutoLockMasternodeCollaterals
933-
if (m_chain) {
934-
for (const auto& outPoint : m_chain->listMNCollaterials(outputs)) {
935-
LockCoin(outPoint, &batch);
936-
}
933+
for (const auto& utxo : ListProTxCoins(candidates)) {
934+
LockCoin(utxo, &batch);
937935
}
938936
}
939937

@@ -951,21 +949,19 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
951949
assert(wtx.m_confirm.block_height == confirm.block_height);
952950
}
953951

954-
std::vector<std::pair<const CTransactionRef&, unsigned int>> outputs;
955-
for(unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
952+
std::set<COutPoint> candidates;
953+
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
956954
if (IsMine(wtx.tx->vout[i]) && !IsSpent(hash, i)) {
957955
bool new_utxo = setWalletUTXO.insert(COutPoint(hash, i)).second;
958956
if (new_utxo) {
959-
outputs.emplace_back(wtx.tx, i);
957+
candidates.emplace(hash, i);
960958
fUpdated = true;
961959
}
962960
}
963961
}
964962
// TODO: refactor duplicated code with case fInstertedNew
965-
if (m_chain) {
966-
for (const auto& outPoint : m_chain->listMNCollaterials(outputs)) {
967-
LockCoin(outPoint);
968-
}
963+
for (const auto& utxo : ListProTxCoins(candidates)) {
964+
LockCoin(utxo, &batch);
969965
}
970966
}
971967

@@ -2799,8 +2795,7 @@ std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const
27992795
}
28002796
}
28012797

2802-
std::vector<COutPoint> lockedCoins;
2803-
ListLockedCoins(lockedCoins);
2798+
std::vector<COutPoint> lockedCoins{ListLockedCoins()};
28042799
// Include watch-only for LegacyScriptPubKeyMan wallets without private keys
28052800
const bool include_watch_only = GetLegacyScriptPubKeyMan() && IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
28062801
const isminetype is_mine_filter = include_watch_only ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
@@ -4056,20 +4051,18 @@ DBErrors CWallet::LoadWallet()
40564051
// This avoids accidental spending of collaterals. They can still be unlocked manually if a spend is really intended.
40574052
void CWallet::AutoLockMasternodeCollaterals()
40584053
{
4059-
if (!m_chain) return;
4060-
4061-
std::vector<std::pair<const CTransactionRef&, unsigned int>> outputs;
4062-
40634054
LOCK(cs_wallet);
4064-
for (const auto& pair : mapWallet) {
4065-
for (unsigned int i = 0; i < pair.second.tx->vout.size(); ++i) {
4066-
if (IsMine(pair.second.tx->vout[i]) && !IsSpent(pair.first, i)) {
4067-
outputs.emplace_back(pair.second.tx, i);
4055+
std::set<COutPoint> candidates;
4056+
for (const auto& [txid, wtx] : mapWallet) {
4057+
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
4058+
if (IsMine(wtx.tx->vout[i]) && !IsSpent(txid, i)) {
4059+
candidates.emplace(txid, i);
40684060
}
40694061
}
40704062
}
4071-
for (const auto& outPoint : m_chain->listMNCollaterials(outputs)) {
4072-
LockCoin(outPoint);
4063+
WalletBatch batch(GetDatabase());
4064+
for (const auto& utxo : ListProTxCoins(candidates)) {
4065+
LockCoin(utxo, &batch);
40734066
}
40744067
}
40754068

@@ -4502,34 +4495,32 @@ bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
45024495
return (setLockedCoins.count(outpt) > 0);
45034496
}
45044497

4505-
void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) const
4498+
std::vector<COutPoint> CWallet::ListLockedCoins() const
45064499
{
45074500
AssertLockHeld(cs_wallet);
4508-
for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
4509-
it != setLockedCoins.end(); it++) {
4510-
COutPoint outpt = (*it);
4511-
vOutpts.push_back(outpt);
4501+
std::vector<COutPoint> ret;
4502+
for (const auto& output : setLockedCoins) {
4503+
ret.push_back(output);
45124504
}
4505+
return ret;
45134506
}
45144507

4515-
void CWallet::ListProTxCoins(std::vector<COutPoint>& vOutpts) const
4516-
{
4517-
// TODO: refactor duplicated code between CWallet::AutoLockMasternodeCollaterals and CWallet::ListProTxCoins
4518-
if (!m_chain) {
4519-
vOutpts.clear();
4520-
return;
4521-
}
4522-
std::vector<std::pair<const CTransactionRef&, unsigned int>> outputs;
4508+
std::vector<COutPoint> CWallet::ListProTxCoins() const { return ListProTxCoins(setWalletUTXO); }
45234509

4510+
std::vector<COutPoint> CWallet::ListProTxCoins(const std::set<COutPoint>& utxos) const
4511+
{
45244512
AssertLockHeld(cs_wallet);
4525-
for (const auto &o : setWalletUTXO) {
4526-
auto it = mapWallet.find(o.hash);
4527-
if (it != mapWallet.end()) {
4528-
const auto &ptx = it->second;
4529-
outputs.emplace_back(ptx.tx, o.n);
4513+
4514+
if (!m_chain) return std::vector<COutPoint>();
4515+
4516+
std::vector<std::pair<const CTransactionRef&, unsigned int>> candidates;
4517+
for (const auto& output : utxos) {
4518+
if (auto it = mapWallet.find(output.hash); it != mapWallet.end()) {
4519+
const auto& [hash, wtx] = *it;
4520+
candidates.emplace_back(wtx.tx, output.n);
45304521
}
45314522
}
4532-
vOutpts = m_chain->listMNCollaterials(outputs);
4523+
return m_chain->listMNCollaterials(candidates);
45334524
}
45344525

45354526
/** @} */ // end of Actions

src/wallet/wallet.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10381038
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10391039
bool UnlockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10401040
bool UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1041-
void ListLockedCoins(std::vector<COutPoint>& vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1042-
void ListProTxCoins(std::vector<COutPoint>& vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1041+
std::vector<COutPoint> ListLockedCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1042+
std::vector<COutPoint> ListProTxCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1043+
std::vector<COutPoint> ListProTxCoins(const std::set<COutPoint>& utxos) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10431044

10441045
/*
10451046
* Rescan abort properties

0 commit comments

Comments
 (0)