Line data Source code
1 : // Copyright (c) 2012-2020 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include <wallet/wallet.h>
6 :
7 : #include <future>
8 : #include <memory>
9 : #include <stdint.h>
10 : #include <vector>
11 :
12 : #include <interfaces/chain.h>
13 : #include <node/context.h>
14 : #include <policy/policy.h>
15 : #include <rpc/server.h>
16 : #include <test/util/logging.h>
17 : #include <test/util/setup_common.h>
18 : #include <util/ref.h>
19 : #include <util/translation.h>
20 : #include <validation.h>
21 : #include <wallet/coincontrol.h>
22 : #include <wallet/test/wallet_test_fixture.h>
23 :
24 : #include <boost/test/unit_test.hpp>
25 : #include <univalue.h>
26 :
27 : RPCHelpMan importmulti();
28 : RPCHelpMan dumpwallet();
29 : RPCHelpMan importwallet();
30 :
31 : // Ensure that fee levels defined in the wallet are at least as high
32 : // as the default levels for node policy.
33 : static_assert(DEFAULT_TRANSACTION_MINFEE >= DEFAULT_MIN_RELAY_TX_FEE, "wallet minimum fee is smaller than default relay fee");
34 : static_assert(WALLET_INCREMENTAL_RELAY_FEE >= DEFAULT_INCREMENTAL_RELAY_FEE, "wallet incremental fee is smaller than default incremental relay fee");
35 :
36 89 : BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
37 :
38 4 : static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain& chain)
39 : {
40 4 : DatabaseOptions options;
41 4 : DatabaseStatus status;
42 4 : bilingual_str error;
43 4 : std::vector<bilingual_str> warnings;
44 4 : auto database = MakeWalletDatabase("", options, status, error);
45 4 : auto wallet = CWallet::Create(chain, "", std::move(database), options.create_flags, error, warnings);
46 4 : wallet->postInitProcess();
47 : return wallet;
48 4 : }
49 :
50 4 : static void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
51 : {
52 4 : SyncWithValidationInterfaceQueue();
53 4 : wallet->m_chain_notifications_handler.reset();
54 4 : UnloadWallet(std::move(wallet));
55 4 : }
56 :
57 5 : static CMutableTransaction TestSimpleSpend(const CTransaction& from, uint32_t index, const CKey& key, const CScript& pubkey)
58 : {
59 5 : CMutableTransaction mtx;
60 5 : mtx.vout.push_back({from.vout[index].nValue - DEFAULT_TRANSACTION_MAXFEE, pubkey});
61 5 : mtx.vin.push_back({CTxIn{from.GetHash(), index}});
62 5 : FillableSigningProvider keystore;
63 5 : keystore.AddKey(key);
64 5 : std::map<COutPoint, Coin> coins;
65 5 : coins[mtx.vin[0].prevout].out = from.vout[index];
66 5 : std::map<int, std::string> input_errors;
67 5 : BOOST_CHECK(SignTransaction(mtx, &keystore, coins, SIGHASH_ALL, input_errors));
68 : return mtx;
69 5 : }
70 :
71 7 : static void AddKey(CWallet& wallet, const CKey& key)
72 : {
73 7 : auto spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan();
74 7 : LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore);
75 7 : spk_man->AddKeyPubKey(key, key.GetPubKey());
76 7 : }
77 :
78 95 : BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
79 : {
80 : // Cap last block file size, and mine new block in a new block file.
81 1 : CBlockIndex* oldTip = ::ChainActive().Tip();
82 1 : GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
83 1 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
84 1 : CBlockIndex* newTip = ::ChainActive().Tip();
85 :
86 1 : NodeContext node;
87 1 : auto chain = interfaces::MakeChain(node);
88 :
89 : // Verify ScanForWalletTransactions fails to read an unknown start block.
90 : {
91 1 : CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
92 : {
93 1 : LOCK(wallet.cs_wallet);
94 1 : wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
95 1 : }
96 1 : AddKey(wallet, coinbaseKey);
97 1 : WalletRescanReserver reserver(wallet);
98 1 : reserver.reserve();
99 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions({} /* start_block */, 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
100 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
101 1 : BOOST_CHECK(result.last_failed_block.IsNull());
102 1 : BOOST_CHECK(result.last_scanned_block.IsNull());
103 1 : BOOST_CHECK(!result.last_scanned_height);
104 1 : BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 0);
105 1 : }
106 :
107 : // Verify ScanForWalletTransactions picks up transactions in both the old
108 : // and new block files.
109 : {
110 1 : CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
111 : {
112 1 : LOCK(wallet.cs_wallet);
113 1 : wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
114 1 : }
115 1 : AddKey(wallet, coinbaseKey);
116 1 : WalletRescanReserver reserver(wallet);
117 1 : reserver.reserve();
118 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */);
119 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
120 1 : BOOST_CHECK(result.last_failed_block.IsNull());
121 1 : BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
122 1 : BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
123 1 : BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 100 * COIN);
124 1 : }
125 :
126 : // Prune the older block file.
127 : {
128 1 : LOCK(cs_main);
129 2 : Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile);
130 1 : }
131 1 : UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
132 :
133 : // Verify ScanForWalletTransactions only picks transactions in the new block
134 : // file.
135 : {
136 1 : CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
137 : {
138 1 : LOCK(wallet.cs_wallet);
139 1 : wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
140 1 : }
141 1 : AddKey(wallet, coinbaseKey);
142 1 : WalletRescanReserver reserver(wallet);
143 1 : reserver.reserve();
144 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */);
145 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
146 1 : BOOST_CHECK_EQUAL(result.last_failed_block, oldTip->GetBlockHash());
147 1 : BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
148 1 : BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
149 1 : BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 50 * COIN);
150 1 : }
151 :
152 : // Prune the remaining block file.
153 : {
154 1 : LOCK(cs_main);
155 2 : Assert(m_node.chainman)->PruneOneBlockFile(newTip->GetBlockPos().nFile);
156 1 : }
157 1 : UnlinkPrunedFiles({newTip->GetBlockPos().nFile});
158 :
159 : // Verify ScanForWalletTransactions scans no blocks.
160 : {
161 1 : CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
162 : {
163 1 : LOCK(wallet.cs_wallet);
164 1 : wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
165 1 : }
166 1 : AddKey(wallet, coinbaseKey);
167 1 : WalletRescanReserver reserver(wallet);
168 1 : reserver.reserve();
169 1 : CWallet::ScanResult result = wallet.ScanForWalletTransactions(oldTip->GetBlockHash(), oldTip->nHeight, {} /* max_height */, reserver, false /* update */);
170 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE);
171 1 : BOOST_CHECK_EQUAL(result.last_failed_block, newTip->GetBlockHash());
172 1 : BOOST_CHECK(result.last_scanned_block.IsNull());
173 1 : BOOST_CHECK(!result.last_scanned_height);
174 1 : BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 0);
175 1 : }
176 1 : }
177 :
178 95 : BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
179 : {
180 : // Cap last block file size, and mine new block in a new block file.
181 1 : CBlockIndex* oldTip = ::ChainActive().Tip();
182 1 : GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
183 1 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
184 1 : CBlockIndex* newTip = ::ChainActive().Tip();
185 :
186 1 : NodeContext node;
187 1 : auto chain = interfaces::MakeChain(node);
188 :
189 : // Prune the older block file.
190 : {
191 1 : LOCK(cs_main);
192 2 : Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile);
193 1 : }
194 1 : UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
195 :
196 : // Verify importmulti RPC returns failure for a key whose creation time is
197 : // before the missing block, and success for a key whose creation time is
198 : // after.
199 : {
200 1 : std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), "", CreateDummyWalletDatabase());
201 1 : wallet->SetupLegacyScriptPubKeyMan();
202 2 : WITH_LOCK(wallet->cs_wallet, wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash()));
203 1 : AddWallet(wallet);
204 1 : UniValue keys;
205 1 : keys.setArray();
206 1 : UniValue key;
207 1 : key.setObject();
208 1 : key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(coinbaseKey.GetPubKey())));
209 1 : key.pushKV("timestamp", 0);
210 1 : key.pushKV("internal", UniValue(true));
211 1 : keys.push_back(key);
212 1 : key.clear();
213 1 : key.setObject();
214 1 : CKey futureKey;
215 1 : futureKey.MakeNewKey(true);
216 1 : key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey())));
217 1 : key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
218 1 : key.pushKV("internal", UniValue(true));
219 1 : keys.push_back(key);
220 1 : util::Ref context;
221 1 : JSONRPCRequest request(context);
222 1 : request.params.setArray();
223 1 : request.params.push_back(keys);
224 :
225 1 : UniValue response = importmulti().HandleRequest(request);
226 1 : BOOST_CHECK_EQUAL(response.write(),
227 : strprintf("[{\"success\":false,\"error\":{\"code\":-1,\"message\":\"Rescan failed for key with creation "
228 : "timestamp %d. There was an error reading a block from time %d, which is after or within %d "
229 : "seconds of key creation, and could contain transactions pertaining to the key. As a result, "
230 : "transactions and coins using this key may not appear in the wallet. This error could be caused "
231 : "by pruning or data corruption (see bitcoind log for details) and could be dealt with by "
232 : "downloading and rescanning the relevant blocks (see -reindex and -rescan "
233 : "options).\"}},{\"success\":true}]",
234 : 0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
235 1 : RemoveWallet(wallet, nullopt);
236 1 : }
237 1 : }
238 :
239 : // Verify importwallet RPC starts rescan at earliest block with timestamp
240 : // greater or equal than key birthday. Previously there was a bug where
241 : // importwallet RPC would start the scan at the latest block with timestamp less
242 : // than or equal to key birthday.
243 95 : BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
244 : {
245 : // Create two blocks with same timestamp to verify that importwallet rescan
246 : // will pick up both blocks, not just the first.
247 1 : const int64_t BLOCK_TIME = ::ChainActive().Tip()->GetBlockTimeMax() + 5;
248 1 : SetMockTime(BLOCK_TIME);
249 1 : m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
250 1 : m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
251 :
252 : // Set key birthday to block time increased by the timestamp window, so
253 : // rescan will start at the block time.
254 1 : const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW;
255 1 : SetMockTime(KEY_TIME);
256 1 : m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
257 :
258 1 : NodeContext node;
259 1 : auto chain = interfaces::MakeChain(node);
260 :
261 1 : std::string backup_file = (GetDataDir() / "wallet.backup").string();
262 :
263 : // Import key into wallet and call dumpwallet to create backup file.
264 : {
265 1 : std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), "", CreateDummyWalletDatabase());
266 : {
267 1 : auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
268 1 : LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
269 1 : spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
270 1 : spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
271 :
272 1 : AddWallet(wallet);
273 1 : wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
274 1 : }
275 1 : util::Ref context;
276 1 : JSONRPCRequest request(context);
277 1 : request.params.setArray();
278 1 : request.params.push_back(backup_file);
279 :
280 1 : ::dumpwallet().HandleRequest(request);
281 1 : RemoveWallet(wallet, nullopt);
282 1 : }
283 :
284 : // Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
285 : // were scanned, and no prior blocks were scanned.
286 : {
287 1 : std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), "", CreateDummyWalletDatabase());
288 1 : LOCK(wallet->cs_wallet);
289 1 : wallet->SetupLegacyScriptPubKeyMan();
290 :
291 1 : util::Ref context;
292 1 : JSONRPCRequest request(context);
293 1 : request.params.setArray();
294 1 : request.params.push_back(backup_file);
295 1 : AddWallet(wallet);
296 1 : wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
297 1 : ::importwallet().HandleRequest(request);
298 1 : RemoveWallet(wallet, nullopt);
299 :
300 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.size(), 3U);
301 1 : BOOST_CHECK_EQUAL(m_coinbase_txns.size(), 103U);
302 104 : for (size_t i = 0; i < m_coinbase_txns.size(); ++i) {
303 103 : bool found = wallet->GetWalletTx(m_coinbase_txns[i]->GetHash());
304 103 : bool expected = i >= 100;
305 103 : BOOST_CHECK_EQUAL(found, expected);
306 103 : }
307 1 : }
308 :
309 1 : SetMockTime(0);
310 1 : }
311 :
312 : // Check that GetImmatureCredit() returns a newly calculated value instead of
313 : // the cached value after a MarkDirty() call.
314 : //
315 : // This is a regression test written to verify a bugfix for the immature credit
316 : // function. Similar tests probably should be written for the other credit and
317 : // debit functions.
318 95 : BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
319 : {
320 1 : NodeContext node;
321 1 : auto chain = interfaces::MakeChain(node);
322 :
323 1 : CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
324 1 : auto spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan();
325 1 : CWalletTx wtx(&wallet, m_coinbase_txns.back());
326 :
327 1 : LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore);
328 1 : wallet.SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
329 :
330 1 : CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, ::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash(), 0);
331 1 : wtx.m_confirm = confirm;
332 :
333 : // Call GetImmatureCredit() once before adding the key to the wallet to
334 : // cache the current immature credit amount, which is 0.
335 1 : BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 0);
336 :
337 : // Invalidate the cached value, add the key, and make sure a new immature
338 : // credit amount is calculated.
339 1 : wtx.MarkDirty();
340 1 : BOOST_CHECK(spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()));
341 1 : BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50*COIN);
342 1 : }
343 :
344 6 : static int64_t AddTx(ChainstateManager& chainman, CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
345 : {
346 6 : CMutableTransaction tx;
347 6 : CWalletTx::Confirmation confirm;
348 6 : tx.nLockTime = lockTime;
349 6 : SetMockTime(mockTime);
350 : CBlockIndex* block = nullptr;
351 6 : if (blockTime > 0) {
352 5 : LOCK(cs_main);
353 5 : auto inserted = chainman.BlockIndex().emplace(GetRandHash(), new CBlockIndex);
354 5 : assert(inserted.second);
355 5 : const uint256& hash = inserted.first->first;
356 5 : block = inserted.first->second;
357 5 : block->nTime = blockTime;
358 5 : block->phashBlock = &hash;
359 5 : confirm = {CWalletTx::Status::CONFIRMED, block->nHeight, hash, 0};
360 5 : }
361 :
362 : // If transaction is already in map, to avoid inconsistencies, unconfirmation
363 : // is needed before confirm again with different block.
364 12 : return wallet.AddToWallet(MakeTransactionRef(tx), confirm, [&](CWalletTx& wtx, bool /* new_tx */) {
365 6 : wtx.setUnconfirmed();
366 6 : return true;
367 6 : })->nTimeSmart;
368 6 : }
369 :
370 : // Simple test to verify assignment of CWalletTx::nSmartTime value. Could be
371 : // expanded to cover more corner cases of smart time logic.
372 95 : BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
373 : {
374 : // New transaction should use clock time if lower than block time.
375 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 100, 120), 100);
376 :
377 : // Test that updating existing transaction does not change smart time.
378 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 1, 200, 220), 100);
379 :
380 : // New transaction should use clock time if there's no block time.
381 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 2, 300, 0), 300);
382 :
383 : // New transaction should use block time if lower than clock time.
384 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 3, 420, 400), 400);
385 :
386 : // New transaction should use latest entry time if higher than
387 : // min(block time, clock time).
388 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 4, 500, 390), 400);
389 :
390 : // If there are future entries, new transaction should use time of the
391 : // newest entry that is no more than 300 seconds ahead of the clock time.
392 1 : BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300);
393 :
394 : // Reset mock time for other tests.
395 1 : SetMockTime(0);
396 1 : }
397 :
398 95 : BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
399 : {
400 1 : CTxDestination dest = PKHash();
401 1 : LOCK(m_wallet.cs_wallet);
402 1 : WalletBatch batch{m_wallet.GetDatabase()};
403 1 : m_wallet.AddDestData(batch, dest, "misc", "val_misc");
404 1 : m_wallet.AddDestData(batch, dest, "rr0", "val_rr0");
405 1 : m_wallet.AddDestData(batch, dest, "rr1", "val_rr1");
406 :
407 1 : auto values = m_wallet.GetDestValues("rr");
408 1 : BOOST_CHECK_EQUAL(values.size(), 2U);
409 1 : BOOST_CHECK_EQUAL(values[0], "val_rr0");
410 1 : BOOST_CHECK_EQUAL(values[1], "val_rr1");
411 1 : }
412 :
413 : // Test some watch-only LegacyScriptPubKeyMan methods by the procedure of loading (LoadWatchOnly),
414 : // checking (HaveWatchOnly), getting (GetWatchPubKey) and removing (RemoveWatchOnly) a
415 : // given PubKey, resp. its corresponding P2PK Script. Results of the the impact on
416 : // the address -> PubKey map is dependent on whether the PubKey is a point on the curve
417 5 : static void TestWatchOnlyPubKey(LegacyScriptPubKeyMan* spk_man, const CPubKey& add_pubkey)
418 : {
419 5 : CScript p2pk = GetScriptForRawPubKey(add_pubkey);
420 5 : CKeyID add_address = add_pubkey.GetID();
421 5 : CPubKey found_pubkey;
422 5 : LOCK(spk_man->cs_KeyStore);
423 :
424 : // all Scripts (i.e. also all PubKeys) are added to the general watch-only set
425 5 : BOOST_CHECK(!spk_man->HaveWatchOnly(p2pk));
426 5 : spk_man->LoadWatchOnly(p2pk);
427 5 : BOOST_CHECK(spk_man->HaveWatchOnly(p2pk));
428 :
429 : // only PubKeys on the curve shall be added to the watch-only address -> PubKey map
430 5 : bool is_pubkey_fully_valid = add_pubkey.IsFullyValid();
431 5 : if (is_pubkey_fully_valid) {
432 2 : BOOST_CHECK(spk_man->GetWatchPubKey(add_address, found_pubkey));
433 2 : BOOST_CHECK(found_pubkey == add_pubkey);
434 : } else {
435 3 : BOOST_CHECK(!spk_man->GetWatchPubKey(add_address, found_pubkey));
436 3 : BOOST_CHECK(found_pubkey == CPubKey()); // passed key is unchanged
437 : }
438 :
439 5 : spk_man->RemoveWatchOnly(p2pk);
440 5 : BOOST_CHECK(!spk_man->HaveWatchOnly(p2pk));
441 :
442 5 : if (is_pubkey_fully_valid) {
443 2 : BOOST_CHECK(!spk_man->GetWatchPubKey(add_address, found_pubkey));
444 2 : BOOST_CHECK(found_pubkey == add_pubkey); // passed key is unchanged
445 : }
446 5 : }
447 :
448 : // Cryptographically invalidate a PubKey whilst keeping length and first byte
449 2 : static void PollutePubKey(CPubKey& pubkey)
450 : {
451 2 : std::vector<unsigned char> pubkey_raw(pubkey.begin(), pubkey.end());
452 2 : std::fill(pubkey_raw.begin()+1, pubkey_raw.end(), 0);
453 2 : pubkey = CPubKey(pubkey_raw);
454 2 : assert(!pubkey.IsFullyValid());
455 2 : assert(pubkey.IsValid());
456 2 : }
457 :
458 : // Test watch-only logic for PubKeys
459 95 : BOOST_AUTO_TEST_CASE(WatchOnlyPubKeys)
460 : {
461 1 : CKey key;
462 1 : CPubKey pubkey;
463 1 : LegacyScriptPubKeyMan* spk_man = m_wallet.GetOrCreateLegacyScriptPubKeyMan();
464 :
465 1 : BOOST_CHECK(!spk_man->HaveWatchOnly());
466 :
467 : // uncompressed valid PubKey
468 1 : key.MakeNewKey(false);
469 1 : pubkey = key.GetPubKey();
470 1 : assert(!pubkey.IsCompressed());
471 1 : TestWatchOnlyPubKey(spk_man, pubkey);
472 :
473 : // uncompressed cryptographically invalid PubKey
474 1 : PollutePubKey(pubkey);
475 1 : TestWatchOnlyPubKey(spk_man, pubkey);
476 :
477 : // compressed valid PubKey
478 1 : key.MakeNewKey(true);
479 1 : pubkey = key.GetPubKey();
480 1 : assert(pubkey.IsCompressed());
481 1 : TestWatchOnlyPubKey(spk_man, pubkey);
482 :
483 : // compressed cryptographically invalid PubKey
484 1 : PollutePubKey(pubkey);
485 1 : TestWatchOnlyPubKey(spk_man, pubkey);
486 :
487 : // invalid empty PubKey
488 1 : pubkey = CPubKey();
489 1 : TestWatchOnlyPubKey(spk_man, pubkey);
490 1 : }
491 :
492 : class ListCoinsTestingSetup : public TestChain100Setup
493 : {
494 : public:
495 1 : ListCoinsTestingSetup()
496 1 : {
497 1 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
498 1 : wallet = MakeUnique<CWallet>(m_chain.get(), "", CreateMockWalletDatabase());
499 : {
500 1 : LOCK2(wallet->cs_wallet, ::cs_main);
501 1 : wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
502 1 : }
503 1 : bool firstRun;
504 1 : wallet->LoadWallet(firstRun);
505 1 : AddKey(*wallet, coinbaseKey);
506 1 : WalletRescanReserver reserver(*wallet);
507 1 : reserver.reserve();
508 1 : CWallet::ScanResult result = wallet->ScanForWalletTransactions(::ChainActive().Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
509 1 : BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
510 1 : BOOST_CHECK_EQUAL(result.last_scanned_block, ::ChainActive().Tip()->GetBlockHash());
511 1 : BOOST_CHECK_EQUAL(*result.last_scanned_height, ::ChainActive().Height());
512 1 : BOOST_CHECK(result.last_failed_block.IsNull());
513 1 : }
514 :
515 1 : ~ListCoinsTestingSetup()
516 : {
517 1 : wallet.reset();
518 1 : }
519 :
520 1 : CWalletTx& AddTx(CRecipient recipient)
521 : {
522 1 : CTransactionRef tx;
523 1 : CAmount fee;
524 1 : int changePos = -1;
525 1 : bilingual_str error;
526 1 : CCoinControl dummy;
527 : {
528 1 : BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, changePos, error, dummy));
529 : }
530 1 : wallet->CommitTransaction(tx, {}, {});
531 1 : CMutableTransaction blocktx;
532 : {
533 1 : LOCK(wallet->cs_wallet);
534 1 : blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetHash()).tx);
535 1 : }
536 1 : CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
537 :
538 1 : LOCK(wallet->cs_wallet);
539 1 : wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, ::ChainActive().Tip()->GetBlockHash());
540 1 : auto it = wallet->mapWallet.find(tx->GetHash());
541 1 : BOOST_CHECK(it != wallet->mapWallet.end());
542 1 : CWalletTx::Confirmation confirm(CWalletTx::Status::CONFIRMED, ::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash(), 1);
543 1 : it->second.m_confirm = confirm;
544 1 : return it->second;
545 1 : }
546 :
547 1 : std::unique_ptr<interfaces::Chain> m_chain = interfaces::MakeChain(m_node);
548 : std::unique_ptr<CWallet> wallet;
549 : };
550 :
551 95 : BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
552 : {
553 1 : std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
554 :
555 : // Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
556 : // address.
557 1 : std::map<CTxDestination, std::vector<COutput>> list;
558 : {
559 1 : LOCK(wallet->cs_wallet);
560 1 : list = wallet->ListCoins();
561 1 : }
562 1 : BOOST_CHECK_EQUAL(list.size(), 1U);
563 1 : BOOST_CHECK_EQUAL(boost::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
564 1 : BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U);
565 :
566 : // Check initial balance from one mature coinbase transaction.
567 1 : BOOST_CHECK_EQUAL(50 * COIN, wallet->GetAvailableBalance());
568 :
569 : // Add a transaction creating a change address, and confirm ListCoins still
570 : // returns the coin associated with the change address underneath the
571 : // coinbaseKey pubkey, even though the change address has a different
572 : // pubkey.
573 1 : AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
574 : {
575 1 : LOCK(wallet->cs_wallet);
576 1 : list = wallet->ListCoins();
577 1 : }
578 1 : BOOST_CHECK_EQUAL(list.size(), 1U);
579 1 : BOOST_CHECK_EQUAL(boost::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
580 1 : BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
581 :
582 : // Lock both coins. Confirm number of available coins drops to 0.
583 : {
584 1 : LOCK(wallet->cs_wallet);
585 1 : std::vector<COutput> available;
586 1 : wallet->AvailableCoins(available);
587 1 : BOOST_CHECK_EQUAL(available.size(), 2U);
588 1 : }
589 2 : for (const auto& group : list) {
590 3 : for (const auto& coin : group.second) {
591 2 : LOCK(wallet->cs_wallet);
592 2 : wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
593 2 : }
594 0 : }
595 : {
596 1 : LOCK(wallet->cs_wallet);
597 1 : std::vector<COutput> available;
598 1 : wallet->AvailableCoins(available);
599 1 : BOOST_CHECK_EQUAL(available.size(), 0U);
600 1 : }
601 : // Confirm ListCoins still returns same result as before, despite coins
602 : // being locked.
603 : {
604 1 : LOCK(wallet->cs_wallet);
605 1 : list = wallet->ListCoins();
606 1 : }
607 1 : BOOST_CHECK_EQUAL(list.size(), 1U);
608 1 : BOOST_CHECK_EQUAL(boost::get<PKHash>(list.begin()->first).ToString(), coinbaseAddress);
609 1 : BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
610 1 : }
611 :
612 95 : BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
613 : {
614 1 : NodeContext node;
615 1 : auto chain = interfaces::MakeChain(node);
616 1 : std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), "", CreateDummyWalletDatabase());
617 1 : wallet->SetupLegacyScriptPubKeyMan();
618 1 : wallet->SetMinVersion(FEATURE_LATEST);
619 1 : wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
620 1 : BOOST_CHECK(!wallet->TopUpKeyPool(1000));
621 1 : CTxDestination dest;
622 1 : std::string error;
623 1 : BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
624 1 : }
625 :
626 : // Explicit calculation which is used to test the wallet constant
627 : // We get the same virtual size due to rounding(weight/4) for both use_max_sig values
628 2 : static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
629 : {
630 : // Generate ephemeral valid pubkey
631 2 : CKey key;
632 2 : key.MakeNewKey(true);
633 2 : CPubKey pubkey = key.GetPubKey();
634 :
635 : // Generate pubkey hash
636 2 : uint160 key_hash(Hash160(pubkey));
637 :
638 : // Create inner-script to enter into keystore. Key hash can't be 0...
639 2 : CScript inner_script = CScript() << OP_0 << std::vector<unsigned char>(key_hash.begin(), key_hash.end());
640 :
641 : // Create outer P2SH script for the output
642 2 : uint160 script_id(Hash160(inner_script));
643 2 : CScript script_pubkey = CScript() << OP_HASH160 << std::vector<unsigned char>(script_id.begin(), script_id.end()) << OP_EQUAL;
644 :
645 : // Add inner-script to key store and key to watchonly
646 2 : FillableSigningProvider keystore;
647 2 : keystore.AddCScript(inner_script);
648 2 : keystore.AddKeyPubKey(key, pubkey);
649 :
650 : // Fill in dummy signatures for fee calculation.
651 2 : SignatureData sig_data;
652 :
653 2 : if (!ProduceSignature(keystore, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, script_pubkey, sig_data)) {
654 : // We're hand-feeding it correct arguments; shouldn't happen
655 0 : assert(false);
656 : }
657 :
658 2 : CTxIn tx_in;
659 2 : UpdateInput(tx_in, sig_data);
660 2 : return (size_t)GetVirtualTransactionInputSize(tx_in);
661 2 : }
662 :
663 95 : BOOST_FIXTURE_TEST_CASE(dummy_input_size_test, TestChain100Setup)
664 : {
665 1 : BOOST_CHECK_EQUAL(CalculateNestedKeyhashInputSize(false), DUMMY_NESTED_P2WPKH_INPUT_SIZE);
666 1 : BOOST_CHECK_EQUAL(CalculateNestedKeyhashInputSize(true), DUMMY_NESTED_P2WPKH_INPUT_SIZE);
667 1 : }
668 :
669 1 : bool malformed_descriptor(std::ios_base::failure e)
670 : {
671 1 : std::string s(e.what());
672 1 : return s.find("Missing checksum") != std::string::npos;
673 1 : }
674 :
675 95 : BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
676 : {
677 1 : std::vector<unsigned char> malformed_record;
678 1 : CVectorWriter vw(0, 0, malformed_record, 0);
679 1 : vw << std::string("notadescriptor");
680 1 : vw << (uint64_t)0;
681 1 : vw << (int32_t)0;
682 1 : vw << (int32_t)0;
683 1 : vw << (int32_t)1;
684 :
685 1 : VectorReader vr(0, 0, malformed_record, 0);
686 1 : WalletDescriptor w_desc;
687 2 : BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor);
688 2 : }
689 :
690 : //! Test CreateWalletFromFile function and its behavior handling potential race
691 : //! conditions if it's called the same time an incoming transaction shows up in
692 : //! the mempool or a new block.
693 : //!
694 : //! It isn't possible to verify there aren't race condition in every case, so
695 : //! this test just checks two specific cases and ensures that timing of
696 : //! notifications in these cases doesn't prevent the wallet from detecting
697 : //! transactions.
698 : //!
699 : //! In the first case, block and mempool transactions are created before the
700 : //! wallet is loaded, but notifications about these transactions are delayed
701 : //! until after it is loaded. The notifications are superfluous in this case, so
702 : //! the test verifies the transactions are detected before they arrive.
703 : //!
704 : //! In the second case, block and mempool transactions are created after the
705 : //! wallet rescan and notifications are immediately synced, to verify the wallet
706 : //! must already have a handler in place for them, and there's no gap after
707 : //! rescanning where new transactions in new blocks could be lost.
708 95 : BOOST_FIXTURE_TEST_CASE(CreateWalletFromFile, TestChain100Setup)
709 : {
710 : // Create new wallet with known key and unload it.
711 1 : auto chain = interfaces::MakeChain(m_node);
712 1 : auto wallet = TestLoadWallet(*chain);
713 1 : CKey key;
714 1 : key.MakeNewKey(true);
715 1 : AddKey(*wallet, key);
716 1 : TestUnloadWallet(std::move(wallet));
717 :
718 :
719 : // Add log hook to detect AddToWallet events from rescans, blockConnected,
720 : // and transactionAddedToMempool notifications
721 1 : int addtx_count = 0;
722 10 : DebugLogHelper addtx_counter("[default wallet] AddToWallet", [&](const std::string* s) {
723 9 : if (s) ++addtx_count;
724 9 : return false;
725 : });
726 :
727 :
728 1 : bool rescan_completed = false;
729 3 : DebugLogHelper rescan_check("[default wallet] Rescan completed", [&](const std::string* s) {
730 2 : if (s) rescan_completed = true;
731 2 : return false;
732 : });
733 :
734 :
735 : // Block the queue to prevent the wallet receiving blockConnected and
736 : // transactionAddedToMempool notifications, and create block and mempool
737 : // transactions paying to the wallet
738 1 : std::promise<void> promise;
739 2 : CallFunctionInValidationInterfaceQueue([&promise] {
740 1 : promise.get_future().wait();
741 1 : });
742 1 : std::string error;
743 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
744 1 : auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
745 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
746 1 : auto mempool_tx = TestSimpleSpend(*m_coinbase_txns[1], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
747 1 : BOOST_CHECK(chain->broadcastTransaction(MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error));
748 :
749 :
750 : // Reload wallet and make sure new transactions are detected despite events
751 : // being blocked
752 1 : wallet = TestLoadWallet(*chain);
753 1 : BOOST_CHECK(rescan_completed);
754 1 : BOOST_CHECK_EQUAL(addtx_count, 2);
755 : {
756 1 : LOCK(wallet->cs_wallet);
757 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetHash()), 1U);
758 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(mempool_tx.GetHash()), 1U);
759 1 : }
760 :
761 :
762 : // Unblock notification queue and make sure stale blockConnected and
763 : // transactionAddedToMempool events are processed
764 1 : promise.set_value();
765 1 : SyncWithValidationInterfaceQueue();
766 1 : BOOST_CHECK_EQUAL(addtx_count, 4);
767 :
768 :
769 1 : TestUnloadWallet(std::move(wallet));
770 :
771 :
772 : // Load wallet again, this time creating new block and mempool transactions
773 : // paying to the wallet as the wallet finishes loading and syncing the
774 : // queue so the events have to be handled immediately. Releasing the wallet
775 : // lock during the sync is a little artificial but is needed to avoid a
776 : // deadlock during the sync and simulates a new block notification happening
777 : // as soon as possible.
778 1 : addtx_count = 0;
779 2 : auto handler = HandleLoadWallet([&](std::unique_ptr<interfaces::Wallet> wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet->wallet()->cs_wallet) {
780 1 : BOOST_CHECK(rescan_completed);
781 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
782 1 : block_tx = TestSimpleSpend(*m_coinbase_txns[2], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
783 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
784 1 : mempool_tx = TestSimpleSpend(*m_coinbase_txns[3], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
785 1 : BOOST_CHECK(chain->broadcastTransaction(MakeTransactionRef(mempool_tx), DEFAULT_TRANSACTION_MAXFEE, false, error));
786 1 : LEAVE_CRITICAL_SECTION(wallet->wallet()->cs_wallet);
787 1 : SyncWithValidationInterfaceQueue();
788 1 : ENTER_CRITICAL_SECTION(wallet->wallet()->cs_wallet);
789 1 : });
790 1 : wallet = TestLoadWallet(*chain);
791 1 : BOOST_CHECK_EQUAL(addtx_count, 4);
792 : {
793 1 : LOCK(wallet->cs_wallet);
794 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_tx.GetHash()), 1U);
795 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(mempool_tx.GetHash()), 1U);
796 1 : }
797 :
798 :
799 1 : TestUnloadWallet(std::move(wallet));
800 1 : }
801 :
802 95 : BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
803 : {
804 1 : auto chain = interfaces::MakeChain(m_node);
805 1 : auto wallet = TestLoadWallet(*chain);
806 1 : CKey key;
807 1 : key.MakeNewKey(true);
808 1 : AddKey(*wallet, key);
809 :
810 1 : std::string error;
811 1 : m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
812 1 : auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
813 1 : CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
814 :
815 1 : SyncWithValidationInterfaceQueue();
816 :
817 : {
818 1 : auto block_hash = block_tx.GetHash();
819 1 : auto prev_hash = m_coinbase_txns[0]->GetHash();
820 :
821 1 : LOCK(wallet->cs_wallet);
822 1 : BOOST_CHECK(wallet->HasWalletSpend(prev_hash));
823 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 1u);
824 :
825 1 : std::vector<uint256> vHashIn{ block_hash }, vHashOut;
826 1 : BOOST_CHECK_EQUAL(wallet->ZapSelectTx(vHashIn, vHashOut), DBErrors::LOAD_OK);
827 :
828 1 : BOOST_CHECK(!wallet->HasWalletSpend(prev_hash));
829 1 : BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 0u);
830 1 : }
831 :
832 1 : TestUnloadWallet(std::move(wallet));
833 1 : }
834 :
835 89 : BOOST_AUTO_TEST_SUITE_END()
|