Bitcoin Core  24.1.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(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
44  BOOST_CHECK_EQUAL(*result.last_scanned_height, cchain.Height());
46  return wallet;
47 }
48 } // namespace wallet
uint256 last_failed_block
Height of the most recent block that could not be scanned due to read errors or pruning.
Definition: wallet.h:538
enum wallet::CWallet::ScanResult::@17 status
assert(!tx.IsCoinBase())
std::optional< int > last_scanned_height
Definition: wallet.h:532
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: wallet.h:531
An in-memory indexed chain of blocks.
Definition: chain.h:422
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, ArgsManager &args, const CKey &key)
Definition: util.cpp:19
int Height() const
Return the maximal height in the chain.
Definition: chain.h:468
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:433
RAII object to check and reserve a wallet rescan.
Definition: wallet.h:951
bool IsNull() const
Definition: uint256.h:34
ArgsManager args
uint256 GetBlockHash() const
Definition: chain.h:264
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
#define LOCK2(cs1, cs2)
Definition: sync.h:262
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
#define LOCK(cs)
Definition: sync.h:261
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
Definition: node.h:39
Descriptor with some wallet metadata.
Definition: walletutil.h:76
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:117
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:121
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:439
An encapsulated private key.
Definition: key.h:26
std::shared_ptr< CWallet > wallet
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:216
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
#define BOOST_CHECK(expr)
Definition: object.cpp:16