Skip to content

Commit 771bc60

Browse files
committed
wallet: Use LegacyDataSPKM when loading
In SetupLegacyScriptPubKeyMan, a base LegacyDataSPKM will be created if the database has the format "bdb_ro" (i.e. the wallet was opened only for migration purposes). All of the loading functions are now called with a LegacyDataSPKM object instead of LegacyScriptPubKeyMan.
1 parent 61d872f commit 771bc60

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,16 @@ LegacyScriptPubKeyMan* CWallet::GetLegacyScriptPubKeyMan() const
36113611
return dynamic_cast<LegacyScriptPubKeyMan*>(it->second);
36123612
}
36133613

3614+
LegacyDataSPKM* CWallet::GetLegacyDataSPKM() const
3615+
{
3616+
if (IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
3617+
return nullptr;
3618+
}
3619+
auto it = m_internal_spk_managers.find(OutputType::LEGACY);
3620+
if (it == m_internal_spk_managers.end()) return nullptr;
3621+
return dynamic_cast<LegacyDataSPKM*>(it->second);
3622+
}
3623+
36143624
LegacyScriptPubKeyMan* CWallet::GetOrCreateLegacyScriptPubKeyMan()
36153625
{
36163626
SetupLegacyScriptPubKeyMan();
@@ -3627,13 +3637,22 @@ void CWallet::AddScriptPubKeyMan(const uint256& id, std::unique_ptr<ScriptPubKey
36273637
MaybeUpdateBirthTime(spkm->GetTimeFirstKey());
36283638
}
36293639

3640+
LegacyDataSPKM* CWallet::GetOrCreateLegacyDataSPKM()
3641+
{
3642+
SetupLegacyScriptPubKeyMan();
3643+
return GetLegacyDataSPKM();
3644+
}
3645+
36303646
void CWallet::SetupLegacyScriptPubKeyMan()
36313647
{
36323648
if (!m_internal_spk_managers.empty() || !m_external_spk_managers.empty() || !m_spk_managers.empty() || IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
36333649
return;
36343650
}
36353651

3636-
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new LegacyScriptPubKeyMan(*this, m_keypool_size));
3652+
std::unique_ptr<ScriptPubKeyMan> spk_manager = m_database->Format() == "bdb_ro" ?
3653+
std::make_unique<LegacyDataSPKM>(*this) :
3654+
std::make_unique<LegacyScriptPubKeyMan>(*this, m_keypool_size);
3655+
36373656
for (const auto& type : LEGACY_OUTPUT_TYPES) {
36383657
m_internal_spk_managers[type] = spk_manager.get();
36393658
m_external_spk_managers[type] = spk_manager.get();
@@ -4001,7 +4020,7 @@ std::optional<MigrationData> CWallet::GetDescriptorsForLegacy(bilingual_str& err
40014020
{
40024021
AssertLockHeld(cs_wallet);
40034022

4004-
LegacyScriptPubKeyMan* legacy_spkm = GetLegacyScriptPubKeyMan();
4023+
LegacyDataSPKM* legacy_spkm = GetLegacyDataSPKM();
40054024
if (!Assume(legacy_spkm)) {
40064025
// This shouldn't happen
40074026
error = Untranslated(STR_INTERNAL_BUG("Error: Legacy wallet data missing"));
@@ -4020,7 +4039,7 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
40204039
{
40214040
AssertLockHeld(cs_wallet);
40224041

4023-
LegacyScriptPubKeyMan* legacy_spkm = GetLegacyScriptPubKeyMan();
4042+
LegacyDataSPKM* legacy_spkm = GetLegacyDataSPKM();
40244043
if (!Assume(legacy_spkm)) {
40254044
// This shouldn't happen
40264045
error = Untranslated(STR_INTERNAL_BUG("Error: Legacy wallet data missing"));

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
961961
//! Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
962962
LegacyScriptPubKeyMan* GetLegacyScriptPubKeyMan() const;
963963
LegacyScriptPubKeyMan* GetOrCreateLegacyScriptPubKeyMan();
964+
LegacyDataSPKM* GetLegacyDataSPKM() const;
965+
LegacyDataSPKM* GetOrCreateLegacyDataSPKM();
964966

965967
//! Make a Legacy(Data)SPKM and set it for all types, internal, and external.
966968
void SetupLegacyScriptPubKeyMan();

src/wallet/walletdb.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::stri
354354
strErr = "Error reading wallet database: CPrivKey corrupt";
355355
return false;
356356
}
357-
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKey(key, vchPubKey))
357+
if (!pwallet->GetOrCreateLegacyDataSPKM()->LoadKey(key, vchPubKey))
358358
{
359359
strErr = "Error reading wallet database: LegacyDataSPKM::LoadKey failed";
360360
return false;
@@ -393,7 +393,7 @@ bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, st
393393
}
394394
}
395395

396-
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
396+
if (!pwallet->GetOrCreateLegacyDataSPKM()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
397397
{
398398
strErr = "Error reading wallet database: LegacyDataSPKM::LoadCryptedKey failed";
399399
return false;
@@ -440,7 +440,7 @@ bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr)
440440
try {
441441
CHDChain chain;
442442
ssValue >> chain;
443-
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadHDChain(chain);
443+
pwallet->GetOrCreateLegacyDataSPKM()->LoadHDChain(chain);
444444
} catch (const std::exception& e) {
445445
if (strErr.empty()) {
446446
strErr = e.what();
@@ -584,7 +584,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
584584
key >> hash;
585585
CScript script;
586586
value >> script;
587-
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCScript(script))
587+
if (!pwallet->GetOrCreateLegacyDataSPKM()->LoadCScript(script))
588588
{
589589
strErr = "Error reading wallet database: LegacyDataSPKM::LoadCScript failed";
590590
return DBErrors::NONCRITICAL_ERROR;
@@ -607,7 +607,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
607607
key >> vchPubKey;
608608
CKeyMetadata keyMeta;
609609
value >> keyMeta;
610-
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
610+
pwallet->GetOrCreateLegacyDataSPKM()->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
611611

612612
// Extract some CHDChain info from this metadata if it has any
613613
if (keyMeta.nVersion >= CKeyMetadata::VERSION_WITH_HDDATA && !keyMeta.hd_seed_id.IsNull() && keyMeta.hdKeypath.size() > 0) {
@@ -674,7 +674,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
674674

675675
// Set inactive chains
676676
if (!hd_chains.empty()) {
677-
LegacyScriptPubKeyMan* legacy_spkm = pwallet->GetLegacyScriptPubKeyMan();
677+
LegacyDataSPKM* legacy_spkm = pwallet->GetLegacyDataSPKM();
678678
if (legacy_spkm) {
679679
for (const auto& [hd_seed_id, chain] : hd_chains) {
680680
if (hd_seed_id != legacy_spkm->GetHDChain().seed_id) {
@@ -695,7 +695,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
695695
uint8_t fYes;
696696
value >> fYes;
697697
if (fYes == '1') {
698-
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadWatchOnly(script);
698+
pwallet->GetOrCreateLegacyDataSPKM()->LoadWatchOnly(script);
699699
}
700700
return DBErrors::LOAD_OK;
701701
});
@@ -708,7 +708,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
708708
key >> script;
709709
CKeyMetadata keyMeta;
710710
value >> keyMeta;
711-
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadScriptMetadata(CScriptID(script), keyMeta);
711+
pwallet->GetOrCreateLegacyDataSPKM()->LoadScriptMetadata(CScriptID(script), keyMeta);
712712
return DBErrors::LOAD_OK;
713713
});
714714
result = std::max(result, watch_meta_res.m_result);
@@ -720,7 +720,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
720720
key >> nIndex;
721721
CKeyPool keypool;
722722
value >> keypool;
723-
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKeyPool(nIndex, keypool);
723+
pwallet->GetOrCreateLegacyDataSPKM()->LoadKeyPool(nIndex, keypool);
724724
return DBErrors::LOAD_OK;
725725
});
726726
result = std::max(result, pool_res.m_result);
@@ -763,7 +763,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
763763

764764
// nTimeFirstKey is only reliable if all keys have metadata
765765
if (pwallet->IsLegacy() && (key_res.m_records + ckey_res.m_records + watch_script_res.m_records) != (keymeta_res.m_records + watch_meta_res.m_records)) {
766-
auto spk_man = pwallet->GetOrCreateLegacyScriptPubKeyMan();
766+
auto spk_man = pwallet->GetLegacyScriptPubKeyMan();
767767
if (spk_man) {
768768
LOCK(spk_man->cs_KeyStore);
769769
spk_man->UpdateTimeFirstKey(1);

0 commit comments

Comments
 (0)