Skip to content

Commit 22401f1

Browse files
committedAug 26, 2022
Implement LegacyScriptPubKeyMan::DeleteRecords
1 parent 35f428f commit 22401f1

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed
 

‎src/wallet/scriptpubkeyman.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,13 @@ std::optional<MigrationData> LegacyScriptPubKeyMan::MigrateToDescriptor()
19641964
return out;
19651965
}
19661966

1967+
bool LegacyScriptPubKeyMan::DeleteRecords()
1968+
{
1969+
LOCK(cs_KeyStore);
1970+
WalletBatch batch(m_storage.GetDatabase());
1971+
return batch.EraseRecords(DBKeys::LEGACY_TYPES);
1972+
}
1973+
19671974
util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
19681975
{
19691976
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later

‎src/wallet/scriptpubkeyman.h

+2
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
517517
/** Get the DescriptorScriptPubKeyMans (with private keys) that have the same scriptPubKeys as this LegacyScriptPubKeyMan.
518518
* Does not modify this ScriptPubKeyMan. */
519519
std::optional<MigrationData> MigrateToDescriptor();
520+
/** Delete all the records ofthis LegacyScriptPubKeyMan from disk*/
521+
bool DeleteRecords();
520522
};
521523

522524
/** Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr. Does not provide privkeys */

‎src/wallet/walletdb.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const std::string WALLETDESCRIPTORCKEY{"walletdescriptorckey"};
5959
const std::string WALLETDESCRIPTORKEY{"walletdescriptorkey"};
6060
const std::string WATCHMETA{"watchmeta"};
6161
const std::string WATCHS{"watchs"};
62+
const std::unordered_set<std::string> LEGACY_TYPES{CRYPTED_KEY, CSCRIPT, DEFAULTKEY, HDCHAIN, KEYMETA, KEY, OLD_KEY, POOL, WATCHMETA, WATCHS};
6263
} // namespace DBKeys
6364

6465
//
@@ -1083,6 +1084,45 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
10831084
return WriteIC(DBKeys::FLAGS, flags);
10841085
}
10851086

1087+
bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
1088+
{
1089+
// Get cursor
1090+
if (!m_batch->StartCursor())
1091+
{
1092+
return false;
1093+
}
1094+
1095+
// Iterate the DB and look for any records that have the type prefixes
1096+
while (true)
1097+
{
1098+
// Read next record
1099+
CDataStream key(SER_DISK, CLIENT_VERSION);
1100+
CDataStream value(SER_DISK, CLIENT_VERSION);
1101+
bool complete;
1102+
bool ret = m_batch->ReadAtCursor(key, value, complete);
1103+
if (complete) {
1104+
break;
1105+
}
1106+
else if (!ret)
1107+
{
1108+
m_batch->CloseCursor();
1109+
return false;
1110+
}
1111+
1112+
// Make a copy of key to avoid data being deleted by the following read of the type
1113+
Span<const unsigned char> key_data = MakeUCharSpan(key);
1114+
1115+
std::string type;
1116+
key >> type;
1117+
1118+
if (types.count(type) > 0) {
1119+
m_batch->Erase(key_data);
1120+
}
1121+
}
1122+
m_batch->CloseCursor();
1123+
return true;
1124+
}
1125+
10861126
bool WalletBatch::TxnBegin()
10871127
{
10881128
return m_batch->TxnBegin();

‎src/wallet/walletdb.h

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ extern const std::string WALLETDESCRIPTORCKEY;
8484
extern const std::string WALLETDESCRIPTORKEY;
8585
extern const std::string WATCHMETA;
8686
extern const std::string WATCHS;
87+
88+
// Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
89+
extern const std::unordered_set<std::string> LEGACY_TYPES;
8790
} // namespace DBKeys
8891

8992
/* simple HD chain data model */
@@ -276,6 +279,9 @@ class WalletBatch
276279
//! write the hdchain model (external chain child index counter)
277280
bool WriteHDChain(const CHDChain& chain);
278281

282+
//! Delete records of the given types
283+
bool EraseRecords(const std::unordered_set<std::string>& types);
284+
279285
bool WriteWalletFlags(const uint64_t flags);
280286
//! Begin a new transaction
281287
bool TxnBegin();

0 commit comments

Comments
 (0)
Please sign in to comment.