Skip to content

Commit 46865ec

Browse files
committed
Refactor: Move MarkUnusedAddresses code out of CWallet::AddToWalletIfInvolvingMe
This commit does not change behavior.
1 parent a18edd7 commit 46865ec

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,23 @@ bool LegacyScriptPubKeyMan::TopUp(unsigned int size)
287287
return TopUpKeyPool(size);
288288
}
289289

290+
void LegacyScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
291+
{
292+
AssertLockHeld(cs_wallet);
293+
// extract addresses and check if they match with an unused keypool key
294+
for (const auto& keyid : GetAffectedKeys(script, *this)) {
295+
std::map<CKeyID, int64_t>::const_iterator mi = m_pool_key_to_index.find(keyid);
296+
if (mi != m_pool_key_to_index.end()) {
297+
WalletLogPrintf("%s: Detected a used keypool key, mark all keypool key up to this key as used\n", __func__);
298+
MarkReserveKeysAsUsed(mi->second);
299+
300+
if (!TopUpKeyPool()) {
301+
WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
302+
}
303+
}
304+
}
305+
}
306+
290307
void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
291308
{
292309
AssertLockHeld(cs_wallet);

src/wallet/scriptpubkeyman.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class WalletStorage
3737
//! Default for -keypool
3838
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
3939

40+
std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& provider);
41+
4042
/** A key from a CWallet's keypool
4143
*
4244
* The wallet holds one (for pre HD-split wallets) or several keypools. These
@@ -154,6 +156,9 @@ class ScriptPubKeyMan
154156

155157
virtual bool TopUp(unsigned int size = 0) { return false; }
156158

159+
//! Mark unused addresses as being used
160+
virtual void MarkUnusedAddresses(const CScript& script) {}
161+
157162
/* Returns true if HD is enabled */
158163
virtual bool IsHDEnabled() const { return false; }
159164

@@ -259,6 +264,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
259264

260265
bool TopUp(unsigned int size = 0) override;
261266

267+
void MarkUnusedAddresses(const CScript& script) override;
268+
262269
//! Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo
263270
void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
264271

src/wallet/wallet.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ std::string COutput::ToString() const
236236
return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString(), i, nDepth, FormatMoney(tx->tx->vout[i].nValue));
237237
}
238238

239-
std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& provider);
240-
241239
const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
242240
{
243241
LOCK(cs_wallet);
@@ -876,17 +874,8 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, CWalletTx::St
876874

877875
// loop though all outputs
878876
for (const CTxOut& txout: tx.vout) {
879-
// extract addresses and check if they match with an unused keypool key
880-
for (const auto& keyid : GetAffectedKeys(txout.scriptPubKey, *m_spk_man)) {
881-
std::map<CKeyID, int64_t>::const_iterator mi = m_spk_man->m_pool_key_to_index.find(keyid);
882-
if (mi != m_spk_man->m_pool_key_to_index.end()) {
883-
WalletLogPrintf("%s: Detected a used keypool key, mark all keypool key up to this key as used\n", __func__);
884-
MarkReserveKeysAsUsed(mi->second);
885-
886-
if (!m_spk_man->TopUp()) {
887-
WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
888-
}
889-
}
877+
if (auto spk_man = m_spk_man.get()) {
878+
spk_man->MarkUnusedAddresses(txout.scriptPubKey);
890879
}
891880
}
892881

src/wallet/wallet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
11351135
std::set<int64_t>& setExternalKeyPool GUARDED_BY(cs_wallet) = m_spk_man->setExternalKeyPool;
11361136
int64_t& nTimeFirstKey GUARDED_BY(cs_wallet) = m_spk_man->nTimeFirstKey;
11371137
void MarkPreSplitKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(m_spk_man->cs_wallet); m_spk_man->MarkPreSplitKeys(); }
1138-
void MarkReserveKeysAsUsed(int64_t keypool_id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(m_spk_man->cs_wallet); m_spk_man->MarkReserveKeysAsUsed(keypool_id); }
11391138
using CryptedKeyMap = LegacyScriptPubKeyMan::CryptedKeyMap;
11401139
};
11411140

0 commit comments

Comments
 (0)