Branch data Line data Source code
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>
9 : : #include <test/util/setup_common.h>
10 : :
11 : : #include <boost/test/unit_test.hpp>
12 : :
13 : : namespace wallet {
14 : :
15 : : BOOST_AUTO_TEST_SUITE(walletload_tests)
16 : :
17 : : class DummyDescriptor final : public Descriptor {
18 : : private:
19 : : std::string desc;
20 : : public:
21 [ - + ]: 4 : explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {};
22 : 2 : ~DummyDescriptor() = default;
23 : :
24 [ - + ]: 4 : std::string ToString(bool compat_format) const override { return desc; }
25 : 0 : std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; }
26 : :
27 : 0 : bool IsRange() const override { return false; }
28 : 0 : bool IsSolvable() const override { return false; }
29 : 0 : bool IsSingleType() const override { return true; }
30 : 0 : bool HavePrivateKeys(const SigningProvider&) const override { return false; }
31 : 0 : bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; }
32 : 0 : bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; }
33 : 0 : bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; };
34 : 0 : bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; }
35 : 0 : void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {}
36 : 0 : std::optional<int64_t> ScriptSize() const override { return {}; }
37 : 0 : std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; }
38 : 0 : std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }
39 : 0 : void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override {}
40 : 0 : std::vector<std::string> Warnings() const override { return {}; }
41 : 0 : uint32_t GetMaxKeyExpr() const override { return 0; }
42 : 0 : size_t GetKeyCount() const override { return 0; }
43 : : };
44 : :
45 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
46 : : {
47 [ + - ]: 1 : bilingual_str _error;
48 : 1 : std::vector<bilingual_str> _warnings;
49 [ + - ]: 1 : std::unique_ptr<WalletDatabase> database = CreateMockableWalletDatabase();
50 : 1 : {
51 : : // Write unknown active descriptor
52 [ + - ]: 1 : WalletBatch batch(*database);
53 [ + - ]: 1 : std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
54 [ + - + - : 2 : WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
- + ]
55 [ + - + - : 2 : BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
+ - + - ]
56 [ + - + - : 2 : BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false));
+ - ]
57 : 1 : }
58 : :
59 : 1 : {
60 : : // Now try to load the wallet and verify the error.
61 [ + - + - : 2 : const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database)));
+ - + - -
- ]
62 [ + - + - : 1 : BOOST_CHECK_EQUAL(wallet->PopulateWalletFromDB(_error, _warnings), DBErrors::UNKNOWN_DESCRIPTOR);
+ - + - ]
63 : 0 : }
64 : :
65 : : // Test 2
66 : : // Now write a valid descriptor with an invalid ID.
67 : : // As the software produces another ID for the descriptor, the loading process must be aborted.
68 [ + - ]: 2 : database = CreateMockableWalletDatabase();
69 : :
70 : : // Verify the error
71 : 1 : bool found = false;
72 [ + - ]: 4 : DebugLogHelper logHelper("The descriptor ID calculated by the wallet differs from the one in DB", [&](const std::string* s) {
73 : 2 : found = true;
74 : 2 : return false;
75 [ + - ]: 1 : });
76 : :
77 : 1 : {
78 : : // Write valid descriptor with invalid ID
79 [ + - ]: 1 : WalletBatch batch(*database);
80 [ + - ]: 1 : std::string desc = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu";
81 [ + - + - : 2 : WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0);
- + ]
82 [ + - + - : 2 : BOOST_CHECK(batch.WriteDescriptor(uint256::ONE, wallet_descriptor));
+ - ]
83 : 1 : }
84 : :
85 : 1 : {
86 : : // Now try to load the wallet and verify the error.
87 [ + - + - : 2 : const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database)));
+ - + - -
- ]
88 [ + - + - : 1 : BOOST_CHECK_EQUAL(wallet->PopulateWalletFromDB(_error, _warnings), DBErrors::CORRUPT);
+ - ]
89 [ + - + - : 2 : BOOST_CHECK(found); // The error must be logged
+ - ]
90 : 0 : }
91 : 2 : }
92 : :
93 : : BOOST_AUTO_TEST_SUITE_END()
94 : : } // namespace wallet
|