Bitcoin Core 31.99.0
P2P Digital Currency
walletload_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present 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/test/util.h>
6#include <wallet/wallet.h>
7#include <test/util/common.h>
8#include <test/util/logging.h>
10
11#include <boost/test/unit_test.hpp>
12
13namespace wallet {
14
15BOOST_AUTO_TEST_SUITE(walletload_tests)
16
17class DummyDescriptor final : public Descriptor {
18private:
19 std::string desc;
20public:
21 explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {};
22 ~DummyDescriptor() = default;
23
24 std::string ToString(bool compat_format) const override { return desc; }
25 std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; }
26
27 bool IsRange() const override { return false; }
28 bool IsSolvable() const override { return false; }
29 bool IsSingleType() const override { return true; }
30 bool HavePrivateKeys(const SigningProvider&) const override { return false; }
31 bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; }
32 bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; }
33 bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; };
34 bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; }
35 void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {}
36 std::optional<int64_t> ScriptSize() const override { return {}; }
37 std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; }
38 std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }
39 void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override {}
40 bool HasScripts() const override { return true; }
41 std::vector<std::string> Warnings() const override { return {}; }
42 uint32_t GetMaxKeyExpr() const override { return 0; }
43 size_t GetKeyCount() const override { return 0; }
44};
45
46BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)
47{
48 bilingual_str _error;
49 std::vector<bilingual_str> _warnings;
50 std::unique_ptr<WalletDatabase> database = CreateMockableWalletDatabase();
51 {
52 // Write unknown active descriptor
53 WalletBatch batch(*database);
54 std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
55 WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
56 BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
57 BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false));
58 }
59
60 {
61 // Now try to load the wallet and verify the error.
62 const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database)));
63 BOOST_CHECK_EQUAL(wallet->PopulateWalletFromDB(_error, _warnings), DBErrors::UNKNOWN_DESCRIPTOR);
64 }
65
66 // Test 2
67 // Now write a valid descriptor with an invalid ID.
68 // As the software produces another ID for the descriptor, the loading process must be aborted.
70
71 // Verify the error
72 bool found = false;
73 DebugLogHelper logHelper("The descriptor ID calculated by the wallet differs from the one in DB", [&](const std::string* s) {
74 found = true;
75 return false;
76 });
77
78 {
79 // Write valid descriptor with invalid ID
80 WalletBatch batch(*database);
81 std::string desc = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu";
82 WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0);
83 BOOST_CHECK(batch.WriteDescriptor(uint256::ONE, wallet_descriptor));
84 }
85
86 {
87 // Now try to load the wallet and verify the error.
88 const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database)));
89 BOOST_CHECK_EQUAL(wallet->PopulateWalletFromDB(_error, _warnings), DBErrors::CORRUPT);
90 BOOST_CHECK(found); // The error must be logged
91 }
92}
93
95} // namespace wallet
node::NodeContext m_node
Definition: bitcoin-gui.cpp:47
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
An interface to be implemented by keystores that support signing.
256-bit opaque blob.
Definition: uint256.h:196
static const uint256 ONE
Definition: uint256.h:205
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:309
bool IsSolvable() const override
Whether this descriptor has all information about signing ignoring lack of private keys.
std::string ToString(bool compat_format) const override
Convert the descriptor back to a string, undoing parsing.
void GetPubKeys(std::set< CPubKey > &pubkeys, std::set< CExtPubKey > &ext_pubs) const override
Return all (extended) public keys for this descriptor, including any from subdescriptors.
bool HavePrivateKeys(const SigningProvider &) const override
Whether the given provider has all private keys required by this descriptor.
bool ToNormalizedString(const SigningProvider &provider, std::string &out, const DescriptorCache *cache=nullptr) const override
Convert the descriptor to a normalized string.
void ExpandPrivate(int pos, const SigningProvider &provider, FlatSigningProvider &out) const override
Expand the private key for a descriptor at a specified position, if possible.
bool ToPrivateString(const SigningProvider &provider, std::string &out) const override
Convert the descriptor to a private string.
bool Expand(int pos, const SigningProvider &provider, std::vector< CScript > &output_scripts, FlatSigningProvider &out, DescriptorCache *write_cache=nullptr) const override
Expand a descriptor at a specified position.
uint32_t GetMaxKeyExpr() const override
Get the maximum key expression index.
std::optional< int64_t > MaxSatisfactionWeight(bool) const override
Get the maximum size of a satisfaction for this descriptor, in weight units.
size_t GetKeyCount() const override
Get the number of key expressions in this descriptor.
bool ExpandFromCache(int pos, const DescriptorCache &read_cache, std::vector< CScript > &output_scripts, FlatSigningProvider &out) const override
Expand a descriptor at a specified position using cached expansion data.
bool IsRange() const override
Whether the expansion of this descriptor depends on the position.
std::optional< OutputType > GetOutputType() const override
bool IsSingleType() const override
Whether this descriptor will return at most one scriptPubKey or multiple (aka is or is not combo)
bool HasScripts() const override
Whether this descriptor produces any scripts with the Expand functions.
std::vector< std::string > Warnings() const override
Semantic/safety warnings (includes subdescriptors).
std::optional< int64_t > MaxSatisfactionElems() const override
Get the maximum size number of stack elements for satisfying this descriptor.
std::optional< int64_t > ScriptSize() const override
Get the size of the scriptPubKey for this descriptor.
DummyDescriptor(const std::string &descriptor)
Access to the wallet database.
Definition: walletdb.h:192
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:234
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:205
Descriptor with some wallet metadata.
Definition: walletutil.h:64
BOOST_AUTO_TEST_SUITE_END()
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition: util.cpp:122
BOOST_FIXTURE_TEST_CASE(wallet_coinsresult_test, BasicTestingSetup)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
Interface for parsed descriptor objects.
Definition: descriptor.h:98
Testing setup that configures a complete environment.
Definition: setup_common.h:118
Bilingual messages:
Definition: translation.h:24
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:78