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>
11#include <wallet/wallet.h>
12#include <wallet/walletdb.h>
13
14#include <boost/test/unit_test.hpp>
15
16#include <memory>
17
18std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
19{
20 auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockWalletDatabase());
21 {
22 LOCK2(wallet->cs_wallet, ::cs_main);
23 wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
24 }
25 wallet->LoadWallet();
26 {
27 LOCK(wallet->cs_wallet);
28 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
29 wallet->SetupDescriptorScriptPubKeyMans();
30
31 FlatSigningProvider provider;
32 std::string error;
33 std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
34 assert(desc);
35 WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
36 if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
37 }
39 reserver.reserve();
40 CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
45 return wallet;
46}
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
uint256 GetBlockHash() const
Definition: chain.h:254
An in-memory indexed chain of blocks.
Definition: chain.h:410
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:421
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:416
int Height() const
Return the maximal height in the chain.
Definition: chain.h:446
An encapsulated private key.
Definition: key.h:27
Descriptor with some wallet metadata.
Definition: walletutil.h:76
RAII object to check and reserve a wallet rescan.
Definition: wallet.h:890
bool IsNull() const
Definition: uint256.h:31
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:93
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:196
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::optional< int > last_scanned_height
Definition: wallet.h:522
uint256 last_failed_block
Height of the most recent block that could not be scanned due to read errors or pruning.
Definition: wallet.h:528
enum CWallet::ScanResult::@17 status
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: wallet.h:521
#define LOCK2(cs1, cs2)
Definition: sync.h:227
#define LOCK(cs)
Definition: sync.h:226
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
assert(!tx.IsCoinBase())
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
Definition: util.cpp:18
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1189
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:65