Skip to content

Commit b0e1e3d

Browse files
committed
wallet: extract ProTx UTXO locking into LockProTxCoins
1 parent 3ebef3d commit b0e1e3d

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/wallet/wallet.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -914,19 +914,15 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
914914
CWalletTx& wtx = (*ret.first).second;
915915
bool fInsertedNew = ret.second;
916916
bool fUpdated = update_wtx && update_wtx(wtx, fInsertedNew);
917+
std::set<COutPoint> candidates;
917918
if (fInsertedNew) {
918919
wtx.m_confirm = confirm;
919920
wtx.nTimeReceived = GetTime();
920921
wtx.nOrderPos = IncOrderPosNext(&batch);
921922
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
922923
wtx.nTimeSmart = ComputeTimeSmart(wtx);
923924
AddToSpends(hash, &batch);
924-
925-
// TODO: refactor duplicated code between CWallet::AddToWallet and CWallet::AutoLockMasternodeCollaterals
926-
auto candidates{AddWalletUTXOs(wtx.tx, /*ret_dups=*/true)};
927-
for (const auto& utxo : ListProTxCoins(candidates)) {
928-
LockCoin(utxo, &batch);
929-
}
925+
candidates = AddWalletUTXOs(wtx.tx, /*ret_dups=*/true);
930926
}
931927

932928
if (!fInsertedNew)
@@ -942,15 +938,12 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
942938
assert(wtx.m_confirm.hashBlock == confirm.hashBlock);
943939
assert(wtx.m_confirm.block_height == confirm.block_height);
944940
}
945-
946-
// TODO: refactor duplicated code with case fInstertedNew
947-
auto candidates{AddWalletUTXOs(wtx.tx, /*ret_dups=*/false)};
941+
candidates = AddWalletUTXOs(wtx.tx, /*ret_dups=*/false);
948942
if (!candidates.empty()) fUpdated = true;
949-
for (const auto& utxo : ListProTxCoins(candidates)) {
950-
LockCoin(utxo, &batch);
951-
}
952943
}
953944

945+
LockProTxCoins(candidates, &batch);
946+
954947
//// debug print
955948
WalletLogPrintf("AddToWallet %s %s%s\n", hash.ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
956949

@@ -4059,9 +4052,7 @@ void CWallet::AutoLockMasternodeCollaterals()
40594052
candidates.insert(tx_utxos.begin(), tx_utxos.end());
40604053
}
40614054
WalletBatch batch(GetDatabase());
4062-
for (const auto& utxo : ListProTxCoins(candidates)) {
4063-
LockCoin(utxo, &batch);
4064-
}
4055+
LockProTxCoins(candidates, &batch);
40654056
}
40664057

40674058
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
@@ -4521,6 +4512,14 @@ std::vector<COutPoint> CWallet::ListProTxCoins(const std::set<COutPoint>& utxos)
45214512
return m_chain->listMNCollaterials(candidates);
45224513
}
45234514

4515+
void CWallet::LockProTxCoins(const std::set<COutPoint>& utxos, WalletBatch* batch)
4516+
{
4517+
AssertLockHeld(cs_wallet);
4518+
for (const auto& utxo : ListProTxCoins(utxos)) {
4519+
LockCoin(utxo, batch);
4520+
}
4521+
}
4522+
45244523
/** @} */ // end of Actions
45254524

45264525
void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const {

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10471047
std::vector<COutPoint> ListLockedCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10481048
std::vector<COutPoint> ListProTxCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10491049
std::vector<COutPoint> ListProTxCoins(const std::set<COutPoint>& utxos) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1050+
void LockProTxCoins(const std::set<COutPoint>& utxos, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10501051

10511052
/*
10521053
* Rescan abort properties

0 commit comments

Comments
 (0)