0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

wallet: IsSpent, 'COutPoint' arg instead of (hash, index)

This commit is contained in:
furszy 2022-04-27 10:52:30 -03:00
parent 91902b7720
commit a06fa94ff8
No known key found for this signature in database
GPG key ID: 5DD23CCC686AA623
7 changed files with 15 additions and 18 deletions

View file

@ -110,7 +110,7 @@ WalletTxOut MakeWalletTxOut(const CWallet& wallet,
result.txout = wtx.tx->vout[n]; result.txout = wtx.tx->vout[n];
result.time = wtx.GetTxTime(); result.time = wtx.GetTxTime();
result.depth_in_main_chain = depth; result.depth_in_main_chain = depth;
result.is_spent = wallet.IsSpent(wtx.GetHash(), n); result.is_spent = wallet.IsSpent(COutPoint(wtx.GetHash(), n));
return result; return result;
} }
@ -121,7 +121,7 @@ WalletTxOut MakeWalletTxOut(const CWallet& wallet,
result.txout = output.txout; result.txout = output.txout;
result.time = output.time; result.time = output.time;
result.depth_in_main_chain = output.depth; result.depth_in_main_chain = output.depth;
result.is_spent = wallet.IsSpent(output.outpoint.hash, output.outpoint.n); result.is_spent = wallet.IsSpent(output.outpoint);
return result; return result;
} }

View file

@ -204,9 +204,8 @@ CAmount CachedTxGetAvailableCredit(const CWallet& wallet, const CWalletTx& wtx,
bool allow_used_addresses = (filter & ISMINE_USED) || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE); bool allow_used_addresses = (filter & ISMINE_USED) || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
CAmount nCredit = 0; CAmount nCredit = 0;
uint256 hashTx = wtx.GetHash(); uint256 hashTx = wtx.GetHash();
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
{ if (!wallet.IsSpent(COutPoint(hashTx, i)) && (allow_used_addresses || !wallet.IsSpentKey(hashTx, i))) {
if (!wallet.IsSpent(hashTx, i) && (allow_used_addresses || !wallet.IsSpentKey(hashTx, i))) {
const CTxOut &txout = wtx.tx->vout[i]; const CTxOut &txout = wtx.tx->vout[i];
nCredit += OutputGetCredit(wallet, txout, filter); nCredit += OutputGetCredit(wallet, txout, filter);
if (!MoneyRange(nCredit)) if (!MoneyRange(nCredit))
@ -371,15 +370,15 @@ std::map<CTxDestination, CAmount> GetAddressBalances(const CWallet& wallet)
if (nDepth < (CachedTxIsFromMe(wallet, wtx, ISMINE_ALL) ? 0 : 1)) if (nDepth < (CachedTxIsFromMe(wallet, wtx, ISMINE_ALL) ? 0 : 1))
continue; continue;
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
{ const auto& output = wtx.tx->vout[i];
CTxDestination addr; CTxDestination addr;
if (!wallet.IsMine(wtx.tx->vout[i])) if (!wallet.IsMine(output))
continue; continue;
if(!ExtractDestination(wtx.tx->vout[i].scriptPubKey, addr)) if(!ExtractDestination(output.scriptPubKey, addr))
continue; continue;
CAmount n = wallet.IsSpent(walletEntry.first, i) ? 0 : wtx.tx->vout[i].nValue; CAmount n = wallet.IsSpent(COutPoint(walletEntry.first, i)) ? 0 : output.nValue;
balances[addr] += n; balances[addr] += n;
} }
} }

View file

@ -341,7 +341,7 @@ RPCHelpMan lockunspent()
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout index out of bounds"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout index out of bounds");
} }
if (pwallet->IsSpent(outpt.hash, outpt.n)) { if (pwallet->IsSpent(outpt)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected unspent output"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected unspent output");
} }

View file

@ -1387,7 +1387,7 @@ RPCHelpMan sendall()
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot combine send_max with specific inputs."); throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot combine send_max with specific inputs.");
} else if (options.exists("inputs")) { } else if (options.exists("inputs")) {
for (const CTxIn& input : rawTx.vin) { for (const CTxIn& input : rawTx.vin) {
if (pwallet->IsSpent(input.prevout.hash, input.prevout.n)) { if (pwallet->IsSpent(input.prevout)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Input not available. UTXO (%s:%d) was already spent.", input.prevout.hash.ToString(), input.prevout.n)); throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Input not available. UTXO (%s:%d) was already spent.", input.prevout.hash.ToString(), input.prevout.n));
} }
const CWalletTx* tx{pwallet->GetWalletTx(input.prevout.hash)}; const CWalletTx* tx{pwallet->GetWalletTx(input.prevout.hash)};

View file

@ -182,7 +182,7 @@ CoinsResult AvailableCoins(const CWallet& wallet,
if (wallet.IsLockedCoin(outpoint)) if (wallet.IsLockedCoin(outpoint))
continue; continue;
if (wallet.IsSpent(wtxid, i)) if (wallet.IsSpent(outpoint))
continue; continue;
isminetype mine = wallet.IsMine(output); isminetype mine = wallet.IsMine(output);

View file

@ -629,14 +629,12 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
* Outpoint is spent if any non-conflicted transaction * Outpoint is spent if any non-conflicted transaction
* spends it: * spends it:
*/ */
bool CWallet::IsSpent(const uint256& hash, unsigned int n) const bool CWallet::IsSpent(const COutPoint& outpoint) const
{ {
const COutPoint outpoint(hash, n);
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range; std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
range = mapTxSpends.equal_range(outpoint); range = mapTxSpends.equal_range(outpoint);
for (TxSpends::const_iterator it = range.first; it != range.second; ++it) for (TxSpends::const_iterator it = range.first; it != range.second; ++it) {
{
const uint256& wtxid = it->second; const uint256& wtxid = it->second;
std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid); std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
if (mit != mapWallet.end()) { if (mit != mapWallet.end()) {

View file

@ -441,7 +441,7 @@ public:
//! check whether we support the named feature //! check whether we support the named feature
bool CanSupportFeature(enum WalletFeature wf) const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return IsFeatureSupported(nWalletVersion, wf); } bool CanSupportFeature(enum WalletFeature wf) const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return IsFeatureSupported(nWalletVersion, wf); }
bool IsSpent(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool IsSpent(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
// Whether this or any known UTXO with the same single key has been spent. // Whether this or any known UTXO with the same single key has been spent.
bool IsSpentKey(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool IsSpentKey(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);