@@ -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.
40574052void 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
0 commit comments