Bitcoin Core  22.99.0
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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/test/util.h>
6 
7 #include <chain.h>
8 #include <key.h>
10 #include <wallet/wallet.h>
11 #include <wallet/walletdb.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 #include <memory>
16 
17 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
18 {
19  auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockWalletDatabase());
20  {
21  LOCK2(wallet->cs_wallet, ::cs_main);
22  wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
23  }
24  wallet->LoadWallet();
25  {
26  auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
27  LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
28  spk_man->AddKeyPubKey(key, key.GetPubKey());
29  }
30  WalletRescanReserver reserver(*wallet);
31  reserver.reserve();
32  CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
35  BOOST_CHECK_EQUAL(*result.last_scanned_height, cchain.Height());
37  return wallet;
38 }
CWallet::ScanResult::last_failed_block
uint256 last_failed_block
Height of the most recent block that could not be scanned due to read errors or pruning.
Definition: wallet.h:527
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:227
wallet.h
setup_common.h
CWallet::ScanResult::last_scanned_height
std::optional< int > last_scanned_height
Definition: wallet.h:521
CWallet::ScanResult::last_scanned_block
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: wallet.h:520
wallet
Definition: interfaces.cpp:50
WalletRescanReserver::reserve
bool reserve()
Definition: wallet.h:897
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:403
interfaces::Chain
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:92
CWallet::ScanResult::status
enum CWallet::ScanResult::@17 status
WalletRescanReserver
RAII object to check and reserve a wallet rescan.
Definition: wallet.h:889
CWallet::ScanResult::SUCCESS
@ SUCCESS
Definition: wallet.h:515
CWallet::ScanResult
Definition: wallet.h:514
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:246
CChain::Height
int Height() const
Return the maximal height in the chain.
Definition: chain.h:428
util.h
key.h
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:392
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:31
CKey
An encapsulated private key.
Definition: key.h:26
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:116
CreateMockWalletDatabase
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1156
walletdb.h
CreateSyncedWallet
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
Definition: util.cpp:17
CChain::Genesis
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:398
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18