Bitcoin Core 29.99.0
P2P Digital Currency
walletload_tests.cpp
Go to the documentation of this file.
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/test/util.h>
6#include <wallet/wallet.h>
7#include <test/util/logging.h>
9
10#include <boost/test/unit_test.hpp>
11
12namespace wallet {
13
14BOOST_AUTO_TEST_SUITE(walletload_tests)
15
16class DummyDescriptor final : public Descriptor {
17private:
18 std::string desc;
19public:
20 explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {};
21 ~DummyDescriptor() = default;
22
23 std::string ToString(bool compat_format) const override { return desc; }
24 std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; }
25
26 bool IsRange() const override { return false; }
27 bool IsSolvable() const override { return false; }
28 bool IsSingleType() const override { return true; }
29 bool IsSingleKey() const override { return true; }
30 bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; }
31 bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; }
32 bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; };
33 bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; }
34 void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {}
35 std::optional<int64_t> ScriptSize() const override { return {}; }
36 std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; }
37 std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }
38 void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override {}
39};
40
41BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)
42{
43 std::unique_ptr<WalletDatabase> database = CreateMockableWalletDatabase();
44 {
45 // Write unknown active descriptor
46 WalletBatch batch(*database, false);
47 std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
48 WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
49 BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
50 BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false));
51 }
52
53 {
54 // Now try to load the wallet and verify the error.
55 const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database)));
57 }
58
59 // Test 2
60 // Now write a valid descriptor with an invalid ID.
61 // As the software produces another ID for the descriptor, the loading process must be aborted.
63
64 // Verify the error
65 bool found = false;
66 DebugLogHelper logHelper("The descriptor ID calculated by the wallet differs from the one in DB", [&](const std::string* s) {
67 found = true;
68 return false;
69 });
70
71 {
72 // Write valid descriptor with invalid ID
73 WalletBatch batch(*database, false);
74 std::string desc = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu";
75 WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0);
76 BOOST_CHECK(batch.WriteDescriptor(uint256::ONE, wallet_descriptor));
77 }
78
79 {
80 // Now try to load the wallet and verify the error.
81 const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(database)));
83 BOOST_CHECK(found); // The error must be logged
84 }
85}
86
87bool HasAnyRecordOfType(WalletDatabase& db, const std::string& key)
88{
89 std::unique_ptr<DatabaseBatch> batch = db.MakeBatch(false);
90 BOOST_CHECK(batch);
91 std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
92 BOOST_CHECK(cursor);
93 while (true) {
94 DataStream ssKey{};
95 DataStream ssValue{};
96 DatabaseCursor::Status status = cursor->Next(ssKey, ssValue);
98 if (status == DatabaseCursor::Status::DONE) break;
99 std::string type;
100 ssKey >> type;
101 if (type == key) return true;
102 }
103 return false;
104}
105
106template<typename... Args>
108{
109 DataStream s{};
110 SerializeMany(s, args...);
111 return {s.begin(), s.end()};
112}
113
114
116{
117 SerializeData ckey_record_key;
118 SerializeData ckey_record_value;
119 MockableData records;
120
121 {
122 // Context setup.
123 // Create and encrypt legacy wallet
124 std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", CreateMockableWalletDatabase()));
125 LOCK(wallet->cs_wallet);
126 auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan();
127 BOOST_CHECK(legacy_spkm->SetupGeneration(true));
128
129 // Retrieve a key
130 CTxDestination dest = *Assert(legacy_spkm->GetNewDestination(OutputType::LEGACY));
131 CKeyID key_id = GetKeyForDestination(*legacy_spkm, dest);
132 CKey first_key;
133 BOOST_CHECK(legacy_spkm->GetKey(key_id, first_key));
134
135 // Encrypt the wallet
136 BOOST_CHECK(wallet->EncryptWallet("encrypt"));
137 wallet->Flush();
138
139 // Store a copy of all the records
140 records = GetMockableDatabase(*wallet).m_records;
141
142 // Get the record for the retrieved key
143 ckey_record_key = MakeSerializeData(DBKeys::CRYPTED_KEY, first_key.GetPubKey());
144 ckey_record_value = records.at(ckey_record_key);
145 }
146
147 {
148 // First test case:
149 // Erase all the crypted keys from db and unlock the wallet.
150 // The wallet will only re-write the crypted keys to db if any checksum is missing at load time.
151 // So, if any 'ckey' record re-appears on db, then the checksums were not properly calculated, and we are re-writing
152 // the records every time that 'CWallet::Unlock' gets called, which is not good.
153
154 // Load the wallet and check that is encrypted
155 std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", CreateMockableWalletDatabase(records)));
157 BOOST_CHECK(wallet->IsCrypted());
159
160 // Now delete all records and check that the 'Unlock' function doesn't re-write them
161 BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords());
163 BOOST_CHECK(wallet->Unlock("encrypt"));
165 }
166
167 {
168 // Second test case:
169 // Verify that loading up a 'ckey' with no checksum triggers a complete re-write of the crypted keys.
170
171 // Cut off the 32 byte checksum from a ckey record
172 records[ckey_record_key].resize(ckey_record_value.size() - 32);
173
174 // Load the wallet and check that is encrypted
175 std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", CreateMockableWalletDatabase(records)));
177 BOOST_CHECK(wallet->IsCrypted());
179
180 // Now delete all ckey records and check that the 'Unlock' function re-writes them
181 // (this is because the wallet, at load time, found a ckey record with no checksum)
182 BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords());
184 BOOST_CHECK(wallet->Unlock("encrypt"));
186 }
187
188 {
189 // Third test case:
190 // Verify that loading up a 'ckey' with an invalid checksum throws an error.
191
192 // Cut off the 32 byte checksum from a ckey record
193 records[ckey_record_key].resize(ckey_record_value.size() - 32);
194 // Fill in the checksum space with 0s
195 records[ckey_record_key].resize(ckey_record_value.size());
196
197 std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", CreateMockableWalletDatabase(records)));
199 }
200
201 {
202 // Fourth test case:
203 // Verify that loading up a 'ckey' with an invalid pubkey throws an error
204 CPubKey invalid_key;
205 BOOST_CHECK(!invalid_key.IsValid());
207 records[key] = ckey_record_value;
208
209 std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", CreateMockableWalletDatabase(records)));
211 }
212}
213
215} // namespace wallet
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
node::NodeContext m_node
Definition: bitcoin-gui.cpp:42
ArgsManager & args
Definition: bitcoind.cpp:277
#define Assert(val)
Identity function.
Definition: check.h:85
An encapsulated private key.
Definition: key.h:35
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
An encapsulated public key.
Definition: pubkey.h:34
bool IsValid() const
Definition: pubkey.h:189
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
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:300
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 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.
std::optional< int64_t > MaxSatisfactionWeight(bool) const override
Get the maximum size of a satisfaction for this descriptor, in weight units.
bool IsSingleKey() const override
Whether this descriptor only produces single key scripts (i.e.
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 one scriptPubKey or multiple (aka is or is not combo)
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)
MockableData m_records
Definition: util.h:105
Access to the wallet database.
Definition: walletdb.h:196
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:261
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:229
An instance of this class represents one database.
Definition: db.h:131
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
Descriptor with some wallet metadata.
Definition: walletutil.h:85
BOOST_AUTO_TEST_SUITE_END()
const std::string CRYPTED_KEY
Definition: walletdb.cpp:39
BOOST_FIXTURE_TEST_CASE(wallet_coinsresult_test, BasicTestingSetup)
bool HasAnyRecordOfType(WalletDatabase &db, const std::string &key)
MockableDatabase & GetMockableDatabase(CWallet &wallet)
Definition: util.cpp:191
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:186
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:52
SerializeData MakeSerializeData(const Args &... args)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
void SerializeMany(Stream &s, const Args &... args)
Support for (un)serializing many things at once.
Definition: serialize.h:994
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
Interface for parsed descriptor objects.
Definition: descriptor.h:98
Testing setup that configures a complete environment.
Definition: setup_common.h:121
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:76
#define LOCK(cs)
Definition: sync.h:257
assert(!tx.IsCoinBase())
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:49