Skip to content

Commit 3ebef3d

Browse files
committed
wallet: extract adding UTXOs from CTransaction into AddWalletUTXOs
1 parent eb9ee69 commit 3ebef3d

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/wallet/wallet.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
10611062
bool 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)) {

src/wallet/wallet.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,12 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
763763
void AddToSpends(const uint256& wtxid, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
764764

765765
std::set<COutPoint> setWalletUTXO;
766+
/** Add new UTXOs to the wallet UTXO set
767+
*
768+
* @param[in] tx Transaction to scan eligible UTXOs from
769+
* @param[in] ret_dups Allow UTXOs already in set to be included in return value
770+
* @returns Set of all new UTXOs (eligible to be) added to set */
771+
std::set<COutPoint> AddWalletUTXOs(CTransactionRef tx, bool ret_dups) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
766772
mutable std::map<COutPoint, int> mapOutpointRoundsCache GUARDED_BY(cs_wallet);
767773

768774
/**

0 commit comments

Comments
 (0)