Skip to content

Commit c856883

Browse files
committed
Merge bitcoin/bitcoin#26021: wallet: bugfix, load a wallet with an unknown/corrupt descriptor causes a fatal error
e066763 wallet: coverage for loading an unknown descriptor (furszy) d26c3cc wallet: bugfix, load wallet with an unknown descriptor cause fatal error (furszy) Pull request description: Fixes #26015 If the descriptor entry is unrecognized (due a soft downgrade) or corrupt, the unserialization fails and `LoadWallet`, instead of stop there and return the error, continues reading all the db records. As other records tied to the unrecognized or corrupt descriptor are scanned, a fatal error is being thrown. This fixes it by catching the descriptor parse failure and return which wallet failed. Logging its name/path, so the user can remove it from the settings file, to prevent its load at startup. Note: added the test in a separate file intentionally. Will continue adding coverage for the wallet load process in follow-up PRs. ACKs for top commit: achow101: ACK e066763 Sjors: re-utACK e066763 Tree-SHA512: d1f1a5d7e944c89c97a33b25b4411a36a11edae172c22f8524f69c84a035f84c570b284679f901fe60f1300f781b76a6c17b015a8e7ad44ebd25a0c295ef260f
2 parents 3a7e0a2 + e066763 commit c856883

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

src/Makefile.test.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ BITCOIN_TESTS += \
174174
wallet/test/availablecoins_tests.cpp \
175175
wallet/test/init_tests.cpp \
176176
wallet/test/ismine_tests.cpp \
177-
wallet/test/scriptpubkeyman_tests.cpp
177+
wallet/test/scriptpubkeyman_tests.cpp \
178+
wallet/test/walletload_tests.cpp
178179

179180
FUZZ_SUITE_LD_COMMON +=\
180181
$(SQLITE_LIBS) \

src/wallet/test/walletload_tests.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <wallet/wallet.h>
6+
#include <test/util/setup_common.h>
7+
8+
#include <boost/test/unit_test.hpp>
9+
10+
namespace wallet {
11+
12+
BOOST_AUTO_TEST_SUITE(walletload_tests)
13+
14+
class DummyDescriptor final : public Descriptor {
15+
private:
16+
std::string desc;
17+
public:
18+
explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {};
19+
~DummyDescriptor() = default;
20+
21+
std::string ToString() const override { return desc; }
22+
std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; }
23+
24+
bool IsRange() const override { return false; }
25+
bool IsSolvable() const override { return false; }
26+
bool IsSingleType() const override { return true; }
27+
bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; }
28+
bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; }
29+
bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; };
30+
bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; }
31+
void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {}
32+
};
33+
34+
BOOST_FIXTURE_TEST_CASE(wallet_load_unknown_descriptor, TestingSetup)
35+
{
36+
std::unique_ptr<WalletDatabase> database = CreateMockWalletDatabase();
37+
{
38+
// Write unknown active descriptor
39+
WalletBatch batch(*database, false);
40+
std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
41+
WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
42+
BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
43+
BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false));
44+
}
45+
46+
{
47+
// Now try to load the wallet and verify the error.
48+
const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, std::move(database)));
49+
BOOST_CHECK_EQUAL(wallet->LoadWallet(), DBErrors::UNKNOWN_DESCRIPTOR);
50+
}
51+
}
52+
53+
BOOST_AUTO_TEST_SUITE_END()
54+
} // namespace wallet

src/wallet/wallet.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,8 +2819,12 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
28192819
warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
28202820
" Rescanning wallet."), walletFile));
28212821
rescan_required = true;
2822-
}
2823-
else {
2822+
} else if (nLoadWalletRet == DBErrors::UNKNOWN_DESCRIPTOR) {
2823+
error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
2824+
"The wallet might had been created on a newer version.\n"
2825+
"Please try running the latest software version.\n"), walletFile);
2826+
return nullptr;
2827+
} else {
28242828
error = strprintf(_("Error loading %s"), walletFile);
28252829
return nullptr;
28262830
}

src/wallet/walletdb.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class CWalletScanState {
314314
std::map<std::pair<uint256, CKeyID>, std::pair<CPubKey, std::vector<unsigned char>>> m_descriptor_crypt_keys;
315315
std::map<uint160, CHDChain> m_hd_chains;
316316
bool tx_corrupt{false};
317+
bool descriptor_unknown{false};
317318

318319
CWalletScanState() = default;
319320
};
@@ -627,7 +628,13 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
627628
uint256 id;
628629
ssKey >> id;
629630
WalletDescriptor desc;
630-
ssValue >> desc;
631+
try {
632+
ssValue >> desc;
633+
} catch (const std::ios_base::failure& e) {
634+
strErr = e.what();
635+
wss.descriptor_unknown = true;
636+
return false;
637+
}
631638
if (wss.m_descriptor_caches.count(id) == 0) {
632639
wss.m_descriptor_caches[id] = DescriptorCache();
633640
}
@@ -767,6 +774,12 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
767774
DBErrors result = DBErrors::LOAD_OK;
768775

769776
LOCK(pwallet->cs_wallet);
777+
778+
// Last client version to open this wallet
779+
int last_client = CLIENT_VERSION;
780+
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
781+
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
782+
770783
try {
771784
int nMinVersion = 0;
772785
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
@@ -832,6 +845,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
832845
// Set tx_corrupt back to false so that the error is only printed once (per corrupt tx)
833846
wss.tx_corrupt = false;
834847
result = DBErrors::CORRUPT;
848+
} else if (wss.descriptor_unknown) {
849+
strErr = strprintf("Error: Unrecognized descriptor found in wallet %s. ", pwallet->GetName());
850+
strErr += (last_client > CLIENT_VERSION) ? "The wallet might had been created on a newer version. " :
851+
"The database might be corrupted or the software version is not compatible with one of your wallet descriptors. ";
852+
strErr += "Please try running the latest software version";
853+
pwallet->WalletLogPrintf("%s\n", strErr);
854+
return DBErrors::UNKNOWN_DESCRIPTOR;
835855
} else {
836856
// Leave other errors alone, if we try to fix them we might make things worse.
837857
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
@@ -884,11 +904,6 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
884904
if (result != DBErrors::LOAD_OK)
885905
return result;
886906

887-
// Last client version to open this wallet
888-
int last_client = CLIENT_VERSION;
889-
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
890-
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
891-
892907
pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
893908
wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);
894909

src/wallet/walletdb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ enum class DBErrors
5151
EXTERNAL_SIGNER_SUPPORT_REQUIRED,
5252
LOAD_FAIL,
5353
NEED_REWRITE,
54-
NEED_RESCAN
54+
NEED_RESCAN,
55+
UNKNOWN_DESCRIPTOR
5556
};
5657

5758
namespace DBKeys {

0 commit comments

Comments
 (0)