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>
9 #include <key_io.h>
10 #include <test/util/setup_common.h>
11 #include <wallet/wallet.h>
12 #include <wallet/walletdb.h>
13 
14 #include <boost/test/unit_test.hpp>
15 
16 #include <memory>
17 
18 namespace wallet {
19 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, ArgsManager& args, const CKey& key)
20 {
21  auto wallet = std::make_unique<CWallet>(&chain, "", args, CreateMockWalletDatabase());
22  {
23  LOCK2(wallet->cs_wallet, ::cs_main);
24  wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
25  }
26  wallet->LoadWallet();
27  {
28  LOCK(wallet->cs_wallet);
29  wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
30  wallet->SetupDescriptorScriptPubKeyMans();
31 
32  FlatSigningProvider provider;
33  std::string error;
34  std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
35  assert(desc);
36  WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
37  if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
38  }
39  WalletRescanReserver reserver(*wallet);
40  reserver.reserve();
41  CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
44  BOOST_CHECK_EQUAL(*result.last_scanned_height, cchain.Height());
46  return wallet;
47 }
48 } // namespace wallet
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:227
Parse
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Definition: descriptor.cpp:1394
assert
assert(!tx.IsCoinBase())
wallet.h
wallet::WalletRescanReserver
RAII object to check and reserve a wallet rescan.
Definition: wallet.h:902
key_io.h
setup_common.h
wallet::CWallet::ScanResult::last_scanned_block
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: wallet.h:529
wallet::WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
wallet
Definition: node.h:38
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:434
wallet
std::shared_ptr< CWallet > wallet
Definition: notifications.cpp:38
wallet::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:536
interfaces::Chain
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:94
wallet::CWallet::ScanResult
Definition: wallet.h:523
EncodeSecret
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:216
wallet::CWallet::ScanResult::SUCCESS
@ SUCCESS
Definition: wallet.h:524
wallet::CreateMockWalletDatabase
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1190
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:259
CChain::Height
int Height() const
Return the maximal height in the chain.
Definition: chain.h:463
util.h
key.h
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:417
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:138
wallet::CreateSyncedWallet
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, ArgsManager &args, const CKey &key)
Definition: util.cpp:19
ArgsManager
Definition: system.h:158
LOCK
#define LOCK(cs)
Definition: sync.h:226
wallet::CWallet::ScanResult::last_scanned_height
std::optional< int > last_scanned_height
Definition: wallet.h:530
wallet::WalletRescanReserver::reserve
bool reserve()
Definition: wallet.h:910
wallet::CWallet::ScanResult::status
enum wallet::CWallet::ScanResult::@17 status
wallet::WalletDescriptor
Descriptor with some wallet metadata.
Definition: walletutil.h:76
walletdb.h
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
CChain::Genesis
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:428
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
FlatSigningProvider
Definition: signingprovider.h:72
args
ArgsManager args
Definition: notifications.cpp:36