Skip to content

Commit a18edd7

Browse files
committed
Refactor: Move GetMetadata code out of getaddressinfo
Easier to review ignoring whitespace: git log -p -n1 -w This commit does not change behavior.
1 parent 9716bbe commit a18edd7

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,26 +3756,24 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
37563756
ret.pushKV("label", pwallet->mapAddressBook[dest].name);
37573757
}
37583758
ret.pushKV("ischange", pwallet->IsChange(scriptPubKey));
3759-
const CKeyMetadata* meta = nullptr;
3760-
CKeyID key_id = GetKeyForDestination(*provider, dest);
3761-
if (!key_id.IsNull()) {
3762-
auto it = pwallet->mapKeyMetadata.find(key_id);
3763-
if (it != pwallet->mapKeyMetadata.end()) {
3764-
meta = &it->second;
3759+
3760+
ScriptPubKeyMan* spk_man = pwallet->GetScriptPubKeyMan();
3761+
if (spk_man) {
3762+
CKeyID key_id = GetKeyForDestination(*provider, dest);
3763+
const CKeyMetadata* meta = nullptr;
3764+
if (!key_id.IsNull()) {
3765+
meta = spk_man->GetMetadata(key_id);
37653766
}
3766-
}
3767-
if (!meta) {
3768-
auto it = pwallet->m_script_metadata.find(CScriptID(scriptPubKey));
3769-
if (it != pwallet->m_script_metadata.end()) {
3770-
meta = &it->second;
3767+
if (!meta) {
3768+
meta = spk_man->GetMetadata(CScriptID(scriptPubKey));
37713769
}
3772-
}
3773-
if (meta) {
3774-
ret.pushKV("timestamp", meta->nCreateTime);
3775-
if (meta->has_key_origin) {
3776-
ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path));
3777-
ret.pushKV("hdseedid", meta->hd_seed_id.GetHex());
3778-
ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint, meta->key_origin.fingerprint + 4));
3770+
if (meta) {
3771+
ret.pushKV("timestamp", meta->nCreateTime);
3772+
if (meta->has_key_origin) {
3773+
ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path));
3774+
ret.pushKV("hdseedid", meta->hd_seed_id.GetHex());
3775+
ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint, meta->key_origin.fingerprint + 4));
3776+
}
37793777
}
37803778
}
37813779

src/wallet/scriptpubkeyman.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,21 @@ size_t LegacyScriptPubKeyMan::KeypoolCountExternalKeys()
382382
return setExternalKeyPool.size() + set_pre_split_keypool.size();
383383
}
384384

385+
const CKeyMetadata* LegacyScriptPubKeyMan::GetMetadata(uint160 id) const
386+
{
387+
AssertLockHeld(cs_wallet);
388+
auto it = mapKeyMetadata.find(CKeyID(id));
389+
if (it != mapKeyMetadata.end()) {
390+
return &it->second;
391+
} else {
392+
auto it2 = m_script_metadata.find(CScriptID(id));
393+
if (it2 != m_script_metadata.end()) {
394+
return &it2->second;
395+
}
396+
}
397+
return nullptr;
398+
}
399+
385400
/**
386401
* Update wallet first key creation time. This should be called whenever keys
387402
* are added to the wallet, with the oldest key creation time.

src/wallet/scriptpubkeyman.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class ScriptPubKeyMan
163163
virtual int64_t GetOldestKeyPoolTime() { return GetTime(); }
164164

165165
virtual size_t KeypoolCountExternalKeys() { return 0; }
166+
167+
virtual const CKeyMetadata* GetMetadata(uint160 id) const { return nullptr; }
166168
};
167169

168170
class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProvider
@@ -265,6 +267,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
265267
int64_t GetOldestKeyPoolTime() override;
266268
size_t KeypoolCountExternalKeys() override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
267269

270+
const CKeyMetadata* GetMetadata(uint160 id) const override;
271+
268272
bool CanGetAddresses(bool internal = false) override;
269273

270274
// Map from Key ID to key metadata.

src/wallet/wallet.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,6 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
11341134
std::set<int64_t>& setInternalKeyPool GUARDED_BY(cs_wallet) = m_spk_man->setInternalKeyPool;
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;
1137-
std::map<CKeyID, CKeyMetadata>& mapKeyMetadata GUARDED_BY(cs_wallet) = m_spk_man->mapKeyMetadata;
1138-
std::map<CScriptID, CKeyMetadata>& m_script_metadata GUARDED_BY(cs_wallet) = m_spk_man->m_script_metadata;
11391137
void MarkPreSplitKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(m_spk_man->cs_wallet); m_spk_man->MarkPreSplitKeys(); }
11401138
void MarkReserveKeysAsUsed(int64_t keypool_id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(m_spk_man->cs_wallet); m_spk_man->MarkReserveKeysAsUsed(keypool_id); }
11411139
using CryptedKeyMap = LegacyScriptPubKeyMan::CryptedKeyMap;

0 commit comments

Comments
 (0)