wallet: Retrieve ID from loaded DescSPKM directly

Instead of iterating m_spk_managers a DescriptorSPKM has been loaded in
order to get it's ID to compare, have LoadDescriptorSPKM return a
reference to the loaded DescriptorSPKM so it can be queried directly.
This commit is contained in:
Ava Chow 2024-02-15 11:43:13 -05:00
parent 39640dd34e
commit e041ed9b75
3 changed files with 9 additions and 8 deletions

View file

@ -3556,15 +3556,16 @@ void CWallet::ConnectScriptPubKeyManNotifiers()
} }
} }
void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc) DescriptorScriptPubKeyMan& CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
{ {
DescriptorScriptPubKeyMan* spk_manager;
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) { if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size)); spk_manager = new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size);
AddScriptPubKeyMan(id, std::move(spk_manager));
} else { } else {
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size)); spk_manager = new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size);
AddScriptPubKeyMan(id, std::move(spk_manager));
} }
AddScriptPubKeyMan(id, std::unique_ptr<ScriptPubKeyMan>(spk_manager));
return *spk_manager;
} }
void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)

View file

@ -994,7 +994,7 @@ public:
void ConnectScriptPubKeyManNotifiers(); void ConnectScriptPubKeyManNotifiers();
//! Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it //! Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it
void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc); DescriptorScriptPubKeyMan& LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc);
//! Adds the active ScriptPubKeyMan for the specified type and internal. Writes it to the wallet file //! Adds the active ScriptPubKeyMan for the specified type and internal. Writes it to the wallet file
//! @param[in] id The unique id for the ScriptPubKeyMan //! @param[in] id The unique id for the ScriptPubKeyMan

View file

@ -804,10 +804,10 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
strErr = strprintf("%s\nDetails: %s", strErr, e.what()); strErr = strprintf("%s\nDetails: %s", strErr, e.what());
return DBErrors::UNKNOWN_DESCRIPTOR; return DBErrors::UNKNOWN_DESCRIPTOR;
} }
pwallet->LoadDescriptorScriptPubKeyMan(id, desc); DescriptorScriptPubKeyMan& spkm = pwallet->LoadDescriptorScriptPubKeyMan(id, desc);
// Prior to doing anything with this spkm, verify ID compatibility // Prior to doing anything with this spkm, verify ID compatibility
if (id != pwallet->GetDescriptorScriptPubKeyMan(desc)->GetID()) { if (id != spkm.GetID()) {
strErr = "The descriptor ID calculated by the wallet differs from the one in DB"; strErr = "The descriptor ID calculated by the wallet differs from the one in DB";
return DBErrors::CORRUPT; return DBErrors::CORRUPT;
} }