@@ -922,14 +922,8 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
922922 wtx.nTimeSmart = ComputeTimeSmart (wtx);
923923 AddToSpends (hash, &batch);
924924
925- std::set<COutPoint> candidates;
926- for (unsigned int i = 0 ; i < wtx.tx ->vout .size (); ++i) {
927- if (IsMine (wtx.tx ->vout [i]) && !IsSpent (hash, i)) {
928- setWalletUTXO.insert (COutPoint (hash, i));
929- candidates.emplace (hash, i);
930- }
931- }
932925 // TODO: refactor duplicated code between CWallet::AddToWallet and CWallet::AutoLockMasternodeCollaterals
926+ auto candidates{AddWalletUTXOs (wtx.tx , /* ret_dups=*/ true )};
933927 for (const auto & utxo : ListProTxCoins (candidates)) {
934928 LockCoin (utxo, &batch);
935929 }
@@ -949,17 +943,9 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
949943 assert (wtx.m_confirm .block_height == confirm.block_height );
950944 }
951945
952- std::set<COutPoint> candidates;
953- for (unsigned int i = 0 ; i < wtx.tx ->vout .size (); ++i) {
954- if (IsMine (wtx.tx ->vout [i]) && !IsSpent (hash, i)) {
955- bool new_utxo = setWalletUTXO.insert (COutPoint (hash, i)).second ;
956- if (new_utxo) {
957- candidates.emplace (hash, i);
958- fUpdated = true ;
959- }
960- }
961- }
962946 // TODO: refactor duplicated code with case fInstertedNew
947+ auto candidates{AddWalletUTXOs (wtx.tx , /* ret_dups=*/ false )};
948+ if (!candidates.empty ()) fUpdated = true ;
963949 for (const auto & utxo : ListProTxCoins (candidates)) {
964950 LockCoin (utxo, &batch);
965951 }
@@ -1058,6 +1044,21 @@ bool CWallet::LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx
10581044 return true ;
10591045}
10601046
1047+ std::set<COutPoint> CWallet::AddWalletUTXOs (CTransactionRef tx, bool ret_dups)
1048+ {
1049+ AssertLockHeld (cs_wallet);
1050+ std::set<COutPoint> ret;
1051+ uint256 hash{tx->GetHash ()};
1052+ for (size_t idx = 0 ; idx < tx->vout .size (); ++idx) {
1053+ if (IsMine (tx->vout [idx]) && !IsSpent (hash, idx)) {
1054+ if (auto [_, inserted] = setWalletUTXO.emplace (hash, idx); inserted || ret_dups) {
1055+ ret.emplace (hash, idx);
1056+ }
1057+ }
1058+ }
1059+ return ret;
1060+ }
1061+
10611062bool CWallet::AddToWalletIfInvolvingMe (const CTransactionRef& ptx, CWalletTx::Confirmation confirm, WalletBatch& batch, bool fUpdate )
10621063{
10631064 const CTransaction& tx = *ptx;
@@ -4054,11 +4055,8 @@ void CWallet::AutoLockMasternodeCollaterals()
40544055 LOCK (cs_wallet);
40554056 std::set<COutPoint> candidates;
40564057 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);
4060- }
4061- }
4058+ auto tx_utxos{AddWalletUTXOs (wtx.tx , /* ret_dups=*/ true )};
4059+ candidates.insert (tx_utxos.begin (), tx_utxos.end ());
40624060 }
40634061 WalletBatch batch (GetDatabase ());
40644062 for (const auto & utxo : ListProTxCoins (candidates)) {
0 commit comments