Skip to content

Commit 42c1314

Browse files
wallet, refactor: Decouple into HasLegacyRecords()
The new helper will be used to fix a crash in the wallet migration process (watch-only, non-blank, private keys disabled, empty wallet - no scripts or addresses imported). Co-authored-by: Matias Furszyfer <[email protected]>
1 parent 639279e commit 42c1314

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/wallet/test/walletdb_tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock)
4747
// Create legacy spkm
4848
LOCK(wallet->cs_wallet);
4949
auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan();
50+
BOOST_CHECK(!HasLegacyRecords(*wallet));
5051
BOOST_CHECK(legacy_spkm->SetupGeneration(true));
52+
BOOST_CHECK(HasLegacyRecords(*wallet));
5153
wallet->Flush();
5254

5355
// Now delete all records, which performs a read write operation.
5456
BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords());
57+
BOOST_CHECK(!HasLegacyRecords(*wallet));
5558
}
5659
}
5760

src/wallet/walletdb.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -540,30 +540,45 @@ static LoadResult LoadRecords(CWallet* pwallet, DatabaseBatch& batch, const std:
540540
return LoadRecords(pwallet, batch, key, prefix, load_func);
541541
}
542542

543+
bool HasLegacyRecords(CWallet& wallet)
544+
{
545+
const auto& batch = wallet.GetDatabase().MakeBatch();
546+
return HasLegacyRecords(wallet, *batch);
547+
}
548+
549+
bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch)
550+
{
551+
for (const auto& type : DBKeys::LEGACY_TYPES) {
552+
DataStream key;
553+
DataStream value{};
554+
DataStream prefix;
555+
556+
prefix << type;
557+
std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
558+
if (!cursor) {
559+
// Could only happen on a closed db, which means there is an error in the code flow.
560+
wallet.WalletLogPrintf("Error getting database cursor for '%s' records", type);
561+
throw std::runtime_error(strprintf("Error getting database cursor for '%s' records", type));
562+
}
563+
564+
DatabaseCursor::Status status = cursor->Next(key, value);
565+
if (status != DatabaseCursor::Status::DONE) {
566+
return true;
567+
}
568+
}
569+
return false;
570+
}
571+
543572
static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch, int last_client) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
544573
{
545574
AssertLockHeld(pwallet->cs_wallet);
546575
DBErrors result = DBErrors::LOAD_OK;
547576

548577
// Make sure descriptor wallets don't have any legacy records
549578
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
550-
for (const auto& type : DBKeys::LEGACY_TYPES) {
551-
DataStream key;
552-
DataStream value{};
553-
554-
DataStream prefix;
555-
prefix << type;
556-
std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
557-
if (!cursor) {
558-
pwallet->WalletLogPrintf("Error getting database cursor for '%s' records\n", type);
559-
return DBErrors::CORRUPT;
560-
}
561-
562-
DatabaseCursor::Status status = cursor->Next(key, value);
563-
if (status != DatabaseCursor::Status::DONE) {
564-
pwallet->WalletLogPrintf("Error: Unexpected legacy entry found in descriptor wallet %s. The wallet might have been tampered with or created with malicious intent.\n", pwallet->GetName());
565-
return DBErrors::UNEXPECTED_LEGACY_ENTRY;
566-
}
579+
if (HasLegacyRecords(*pwallet, batch)) {
580+
pwallet->WalletLogPrintf("Error: Unexpected legacy entry found in descriptor wallet %s. The wallet might have been tampered with or created with malicious intent.\n", pwallet->GetName());
581+
return DBErrors::UNEXPECTED_LEGACY_ENTRY;
567582
}
568583

569584
return DBErrors::LOAD_OK;

src/wallet/walletdb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::stri
333333
bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
334334
bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
335335
bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr);
336+
337+
//! Returns true if there are any DBKeys::LEGACY_TYPES record in the wallet db
338+
bool HasLegacyRecords(CWallet& wallet);
339+
bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch);
336340
} // namespace wallet
337341

338342
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)