Skip to content

Commit 3d8a282

Browse files
committed
wallet: decouple IsSpentKey(scriptPubKey) from IsSpentKey(hash, n)
This will be used in a follow-up commit to prevent extra 'GetWalletTx' lookups if the function caller already have the wtx and can just provide the scriptPubKey directly.
1 parent a06fa94 commit 3d8a282

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/wallet/wallet.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -910,31 +910,36 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
910910
{
911911
AssertLockHeld(cs_wallet);
912912
const CWalletTx* srctx = GetWalletTx(hash);
913-
if (srctx) {
914-
assert(srctx->tx->vout.size() > n);
915-
CTxDestination dest;
916-
if (!ExtractDestination(srctx->tx->vout[n].scriptPubKey, dest)) {
917-
return false;
918-
}
919-
if (IsAddressUsed(dest)) {
920-
return true;
921-
}
922-
if (IsLegacy()) {
923-
LegacyScriptPubKeyMan* spk_man = GetLegacyScriptPubKeyMan();
924-
assert(spk_man != nullptr);
925-
for (const auto& keyid : GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
926-
WitnessV0KeyHash wpkh_dest(keyid);
927-
if (IsAddressUsed(wpkh_dest)) {
928-
return true;
929-
}
930-
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
931-
if (IsAddressUsed(sh_wpkh_dest)) {
932-
return true;
933-
}
934-
PKHash pkh_dest(keyid);
935-
if (IsAddressUsed(pkh_dest)) {
936-
return true;
937-
}
913+
if (!srctx) return false;
914+
assert(srctx->tx->vout.size() > n);
915+
return IsSpentKey(srctx->tx->vout[n].scriptPubKey);
916+
}
917+
918+
bool CWallet::IsSpentKey(const CScript& scriptPubKey) const
919+
{
920+
AssertLockHeld(cs_wallet);
921+
CTxDestination dest;
922+
if (!ExtractDestination(scriptPubKey, dest)) {
923+
return false;
924+
}
925+
if (IsAddressUsed(dest)) {
926+
return true;
927+
}
928+
if (IsLegacy()) {
929+
LegacyScriptPubKeyMan* spk_man = GetLegacyScriptPubKeyMan();
930+
assert(spk_man != nullptr);
931+
for (const auto& keyid : GetAffectedKeys(scriptPubKey, *spk_man)) {
932+
WitnessV0KeyHash wpkh_dest(keyid);
933+
if (IsAddressUsed(wpkh_dest)) {
934+
return true;
935+
}
936+
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
937+
if (IsAddressUsed(sh_wpkh_dest)) {
938+
return true;
939+
}
940+
PKHash pkh_dest(keyid);
941+
if (IsAddressUsed(pkh_dest)) {
942+
return true;
938943
}
939944
}
940945
}

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
445445

446446
// Whether this or any known UTXO with the same single key has been spent.
447447
bool IsSpentKey(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
448+
bool IsSpentKey(const CScript& scriptPubKey) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
448449
void SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
449450

450451
/** Display address on an external signer. Returns false if external signer support is not compiled */

0 commit comments

Comments
 (0)