Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/release-notes-24914.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Wallet
------

- Wallet loading has changed in this release. Wallets with some corrupted records that could be
previously loaded (with warnings) may no longer load. For example, wallets with corrupted
address book entries may no longer load. If this happens, it is recommended
load the wallet in a previous version of Dash Core and import the data into a new wallet.
Please also report an issue to help improve the software and make wallet loading more robust
in these cases.
28 changes: 15 additions & 13 deletions src/wallet/salvage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ static const char *HEADER_END = "HEADER=END";
static const char *DATA_END = "DATA=END";
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;

static bool KeyFilter(const std::string& type)
{
return WalletBatch::IsKeyType(type) || type == DBKeys::HDCHAIN;
}

class DummyCursor : public DatabaseCursor
{
Status Next(DataStream& key, DataStream& value) override { return Status::FAIL; }
Expand Down Expand Up @@ -190,17 +185,24 @@ bool RecoverDatabaseFile(const ArgsManager& args, const fs::path& file_path, bil
{
/* Filter for only private key type KV pairs to be added to the salvaged wallet */
DataStream ssKey{row.first};
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
DataStream ssValue(row.second);
std::string strType, strErr;
bool fReadOK;
{
// Required in LoadKeyMetadata():
LOCK(dummyWallet.cs_wallet);
fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, strType, strErr, KeyFilter);
}
if (!KeyFilter(strType)) {

// We only care about KEY, MASTER_KEY, CRYPTED_KEY, HDCHAIN, and CRYPTED_HDCHAIN types
ssKey >> strType;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
bool fReadOK = false;
if (strType == DBKeys::KEY) {
fReadOK = LoadKey(&dummyWallet, ssKey, ssValue, strErr);
} else if (strType == DBKeys::CRYPTED_KEY) {
fReadOK = LoadCryptedKey(&dummyWallet, ssKey, ssValue, strErr);
} else if (strType == DBKeys::MASTER_KEY) {
fReadOK = LoadEncryptionKey(&dummyWallet, ssKey, ssValue, strErr);
} else if (strType == DBKeys::HDCHAIN || strType == DBKeys::CRYPTED_HDCHAIN) {
fReadOK = LoadHDChain(&dummyWallet, ssValue, strErr, /*crypted=*/strType == DBKeys::CRYPTED_HDCHAIN);
} else {
continue;
}

if (!fReadOK)
{
warnings.push_back(strprintf(Untranslated("WARNING: WalletBatch::Recover skipping %s: %s"), strType, strErr));
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,7 +2914,7 @@ bool DescriptorScriptPubKeyMan::AddKey(const CKeyID& key_id, const CKey& key, co

CExtKey master_key_tmp;
master_key_tmp.SetSeed(MakeByteSpan(seed_key_tmp));
assert(key == master_key_tmp.key);
if (key != master_key_tmp.key) return false;
}

m_map_keys[key_id] = key;
Expand Down
164 changes: 134 additions & 30 deletions src/wallet/test/walletdb_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

#include <test/util/setup_common.h>
#include <clientversion.h>
#include <hash.h>
#include <key_io.h>
#include <script/descriptor.h>
#include <streams.h>
#include <uint256.h>
#include <util/message.h>
#include <util/strencodings.h>
#include <wallet/bip39.h>
#include <wallet/crypter.h>
#include <wallet/hdchain.h>
#include <wallet/test/util.h>
#include <wallet/test/wallet_test_fixture.h>
Expand All @@ -14,6 +21,11 @@

#include <boost/test/unit_test.hpp>

#include <functional>
#include <string>
#include <utility>
#include <vector>

namespace wallet {
BOOST_FIXTURE_TEST_SUITE(walletdb_tests, WalletTestingSetup)

Expand All @@ -32,20 +44,16 @@ BOOST_AUTO_TEST_CASE(walletdb_readkeyvalue)
BOOST_CHECK_THROW(ssValue >> dummy, std::ios_base::failure);
}

// Helper: build a key/value stream pair for an HD chain DB record and run ReadKeyValue.
// Helper: build the value stream for an HD chain DB record and run LoadHDChain.
static bool TryReadHDChainRecord(CWallet& wallet, const std::string& dbKey, bool fCrypted, std::string& strErrOut)
{
CHDChain chain;
chain.SetCrypted(fCrypted);

CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssKey << dbKey;
ssValue << chain;

std::string strType;
LOCK(wallet.cs_wallet);
return ReadKeyValue(&wallet, ssKey, ssValue, strType, strErrOut);
return LoadHDChain(&wallet, ssValue, strErrOut, /*crypted=*/dbKey == DBKeys::CRYPTED_HDCHAIN);
}

BOOST_AUTO_TEST_CASE(walletdb_hdchain_type_mismatch)
Expand Down Expand Up @@ -86,30 +94,6 @@ BOOST_AUTO_TEST_CASE(walletdb_platform_data_records)
records = m_wallet.GetPlatformData("platform/identity/");
BOOST_CHECK_EQUAL(records.size(), 1U);
BOOST_CHECK_EQUAL(records.count("platform/identity/0"), 0U);

// The wallet-load path (ReadKeyValue) must populate the same map.
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssKey << std::make_pair(DBKeys::PLATFORM_DATA, std::string{"platform/loaded"});
ssValue << value_a;
std::string strType, strErr;
BOOST_CHECK(ReadKeyValue(&m_wallet, ssKey, ssValue, strType, strErr));
BOOST_CHECK_EQUAL(strType, DBKeys::PLATFORM_DATA);
BOOST_CHECK(m_wallet.GetPlatformData("platform/loaded").at("platform/loaded") == value_a);

// A truncated value must fail the read and identify PLATFORM_DATA as the
// failing type so LoadWallet() can classify the damage as noncritical.
CDataStream ssGood(SER_DISK, CLIENT_VERSION);
ssGood << value_a;
CDataStream ssBadKey(SER_DISK, CLIENT_VERSION);
CDataStream ssBadValue(SER_DISK, CLIENT_VERSION);
ssBadKey << std::make_pair(DBKeys::PLATFORM_DATA, std::string{"platform/corrupt"});
ssBadValue.write({ssGood.data(), ssGood.size() - 1});
strType.clear();
strErr.clear();
BOOST_CHECK(!ReadKeyValue(&m_wallet, ssBadKey, ssBadValue, strType, strErr));
BOOST_CHECK_EQUAL(strType, DBKeys::PLATFORM_DATA);
BOOST_CHECK(m_wallet.GetPlatformData("platform/corrupt").empty());
}

//! Load a wallet whose database holds a PLATFORM_DATA record under
Expand Down Expand Up @@ -150,6 +134,73 @@ BOOST_AUTO_TEST_CASE(walletdb_platform_data_corruption_policy)
BOOST_CHECK(platform_data.at("platform/intact") == std::vector<unsigned char>({0x01, 0x02}));
}

BOOST_AUTO_TEST_CASE(walletdb_dash_record_load_policy)
{
// Damage to an hdpubkey or g_object record is noncritical. An HD chain
// whose encryption state disagrees with its record type, or with the
// wallet's, is corrupt.
CHDChain plain_chain;
BOOST_REQUIRE(plain_chain.SetSeed(SecureVector(32, 0x01), /*fUpdateID=*/true));
CHDChain crypted_chain{plain_chain};
crypted_chain.SetCrypted(true);
const std::pair<std::string, unsigned int> mkey_key{DBKeys::MASTER_KEY, 1};
const std::vector<unsigned char> truncated{0x05};
CKey key;
key.MakeNewKey(/*fCompressed=*/true);

using WriteRecords = std::function<bool(DatabaseBatch&)>;
const std::vector<std::pair<WriteRecords, DBErrors>> rows{
{[&](DatabaseBatch& batch) { return batch.Write(DBKeys::HDCHAIN, plain_chain); }, DBErrors::LOAD_OK},
{[&](DatabaseBatch& batch) {
return batch.Write(DBKeys::CRYPTED_HDCHAIN, crypted_chain) && batch.Write(mkey_key, CMasterKey{});
}, DBErrors::LOAD_OK},
{[&](DatabaseBatch& batch) {
return batch.Write(DBKeys::HDCHAIN, plain_chain) && batch.Write(mkey_key, CMasterKey{});
}, DBErrors::CORRUPT},
{[&](DatabaseBatch& batch) { return batch.Write(DBKeys::CRYPTED_HDCHAIN, crypted_chain); }, DBErrors::CORRUPT},
{[&](DatabaseBatch& batch) {
return batch.Write(DBKeys::HDCHAIN, crypted_chain) && batch.Write(mkey_key, CMasterKey{});
}, DBErrors::CORRUPT},
{[&](DatabaseBatch& batch) {
return batch.Write(std::make_pair(DBKeys::HDPUBKEY, key.GetPubKey()), Span{truncated});
}, DBErrors::NONCRITICAL_ERROR},
{[&](DatabaseBatch& batch) {
return batch.Write(std::make_pair(DBKeys::G_OBJECT, uint256::ONE), Span{truncated});
}, DBErrors::NONCRITICAL_ERROR},
};
for (size_t i{0}; i < rows.size(); ++i) {
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
BOOST_REQUIRE(rows[i].first(*wallet.GetDatabase().MakeBatch()));
BOOST_CHECK_MESSAGE(WalletBatch(wallet.GetDatabase()).LoadWallet(&wallet) == rows[i].second, "row " << i);
}
}

BOOST_AUTO_TEST_CASE(walletdb_descriptor_orphan_records)
{
// A descriptor cache, key or encrypted key record whose descriptor ID has
// no descriptor record, or is the legacy manager's ID, is not loaded. An
// interrupted importdescriptors can leave such records behind.
CExtKey root;
const std::vector<unsigned char> seed(32, 1);
root.SetSeed(MakeByteSpan(seed));
const CPubKey pubkey{root.key.GetPubKey()};
for (const bool legacy_owner : {false, true}) {
for (int record = 0; record < 3; ++record) {
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
const uint256 owner{legacy_owner ? wallet.GetOrCreateLegacyScriptPubKeyMan()->GetID() : uint256::TWO};
{
WalletBatch batch(wallet.GetDatabase());
switch (record) {
case 0: BOOST_REQUIRE(batch.WriteDescriptorParentCache(root.Neuter(), owner, 0)); break;
case 1: BOOST_REQUIRE(batch.WriteDescriptorKey(owner, pubkey, root.key.GetPrivKey(), "", "")); break;
case 2: BOOST_REQUIRE(batch.WriteCryptedDescriptorKey(owner, pubkey, {1}, {}, {})); break;
}
}
BOOST_CHECK(wallet.LoadWallet() == DBErrors::LOAD_OK);
}
}
}

BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock)
{
// Exercises a db read write operation that shouldn't deadlock.
Expand All @@ -176,5 +227,58 @@ BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock)
}
}

BOOST_AUTO_TEST_CASE(walletdb_unreadable_tx_record)
{
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
const std::vector<unsigned char> truncated{0x05};
BOOST_REQUIRE(wallet.GetDatabase().MakeBatch()->Write(std::make_pair(DBKeys::TX, uint256::ONE), Span{truncated}));
BOOST_CHECK(wallet.LoadWallet() == DBErrors::CORRUPT);
}

BOOST_AUTO_TEST_CASE(walletdb_descriptor_mnemonic_consistency)
{
const SecureString mnemonic{"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"};
const SecureString passphrase{"recovery passphrase"};
SecureVector seed;
CMnemonic::ToSeed(mnemonic, passphrase, seed);
CExtKey root;
root.SetSeed(MakeByteSpan(seed));
const CPubKey pubkey{root.key.GetPubKey()};
const CPrivKey privkey{root.key.GetPrivKey()};
enum class Record { NO_MNEMONIC, MATCHING, MISMATCHED };
for (const Record record : {Record::NO_MNEMONIC, Record::MATCHING, Record::MISMATCHED}) {
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
FlatSigningProvider provider;
std::string error;
auto parsed{Parse("pkh(" + HexStr(pubkey) + ")", provider, error, false)};
BOOST_REQUIRE_MESSAGE(parsed, error);
WalletDescriptor descriptor(std::move(parsed), 0, 0, 1, 0);
{
WalletBatch batch(wallet.GetDatabase());
BOOST_REQUIRE(batch.WriteWalletFlags(WALLET_FLAG_DESCRIPTORS));
BOOST_REQUIRE(batch.WriteDescriptor(descriptor.id, descriptor));
if (record == Record::NO_MNEMONIC) {
// Records written before mnemonics were stored end after the key hash.
std::vector<unsigned char> key_bytes(pubkey.begin(), pubkey.end());
key_bytes.insert(key_bytes.end(), privkey.begin(), privkey.end());
BOOST_REQUIRE(wallet.GetDatabase().MakeBatch()->Write(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(descriptor.id, pubkey)), std::make_pair(privkey, Hash(key_bytes))));
} else {
BOOST_REQUIRE(batch.WriteDescriptorKey(descriptor.id, pubkey, privkey, mnemonic, record == Record::MATCHING ? passphrase : SecureString{"wrong passphrase"}));
}
}
const auto result{wallet.LoadWallet()};
if (record == Record::MISMATCHED) {
BOOST_CHECK(result == DBErrors::CORRUPT);
continue;
}
BOOST_REQUIRE(result == DBErrors::LOAD_OK);
LOCK(wallet.cs_wallet);
BOOST_CHECK(wallet.IsMine(GetScriptForDestination(PKHash(pubkey))) == ISMINE_SPENDABLE);
std::string signature;
BOOST_CHECK(wallet.SignMessage("descriptor load regression", PKHash(pubkey), signature) == SigningResult::OK);
BOOST_CHECK(MessageVerify(EncodeDestination(PKHash(pubkey)), signature, "descriptor load regression") == MessageVerificationResult::OK);
}
}

BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet
5 changes: 3 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2684,7 +2684,8 @@ DBErrors CWallet::LoadWallet()
assert(m_internal_spk_managers == nullptr);
}

if (HaveChain()) {
// A transaction record that could not be read leaves an entry without a transaction.
if (nLoadWalletRet != DBErrors::CORRUPT && HaveChain()) {
const std::optional<int> tip_height = chain().getHeight();
if (tip_height) {
SetLastBlockProcessedInMem(*tip_height, chain().getBlockHash(*tip_height));
Expand Down Expand Up @@ -3433,7 +3434,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
else if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR)
{
warnings.push_back(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
" or address book entries might be missing or incorrect."),
" or address metadata may be missing or incorrect."),
walletFile));
}
else if (nLoadWalletRet == DBErrors::TOO_NEW) {
Expand Down
Loading
Loading