Bitcoin Core  24.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
5 #include <wallet/test/util.h>
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>
14 #include <memory>
16 namespace wallet {
17 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, ArgsManager& args, const CKey& key)
18 {
19  auto wallet = std::make_unique<CWallet>(&chain, "", args, CreateMockWalletDatabase());
20  {
21  LOCK2(wallet->cs_wallet, ::cs_main);
22  wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
23  }
24  wallet->LoadWallet();
25  {
26  LOCK(wallet->cs_wallet);
27  wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
28  wallet->SetupDescriptorScriptPubKeyMans();
30  FlatSigningProvider provider;
31  std::string error;
32  std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
33  assert(desc);
34  WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
35  if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
36  }
37  WalletRescanReserver reserver(*wallet);
38  reserver.reserve();
39  CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
41  assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
42  assert(*result.last_scanned_height == cchain.Height());
44  return wallet;
45 }
47 std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database, DatabaseOptions& options)
48 {
49  auto new_database = CreateMockWalletDatabase(options);
51  // Get a cursor to the original database
52  auto batch = database.MakeBatch();
53  std::unique_ptr<wallet::DatabaseCursor> cursor = batch->GetNewCursor();
55  // Get a batch for the new database
56  auto new_batch = new_database->MakeBatch();
58  // Read all records from the original database and write them to the new one
59  while (true) {
60  DataStream key{};
61  DataStream value{};
62  DatabaseCursor::Status status = cursor->Next(key, value);
64  if (status == DatabaseCursor::Status::DONE) break;
65  new_batch->Write(key, value);
66  }
68  return new_database;
69 }
71 std::string getnewaddress(CWallet& w)
72 {
73  constexpr auto output_type = OutputType::BECH32;
74  return EncodeDestination(getNewDestination(w, output_type));
75 }
78 {
79  return *Assert(w.GetNewDestination(output_type, ""));
80 }
82 } // namespace wallet
#define Assert(val)
Identity function.
Definition: check.h:73
uint256 GetBlockHash() const
Definition: chain.h:259
An in-memory indexed chain of blocks.
Definition: chain.h:436
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:446
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:452
int Height() const
Return the maximal height in the chain.
Definition: chain.h:481
An encapsulated private key.
Definition: key.h:27
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
bool IsNull() const
Definition: uint256.h:34
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:119
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:237
An instance of this class represents one database.
Definition: db.h:123
virtual std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true)=0
Make a DatabaseBatch connected to this database.
Descriptor with some wallet metadata.
Definition: walletutil.h:77
RAII object to check and reserve a wallet rescan.
Definition: wallet.h:953
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
util::Result< CTxDestination > GetNewDestination(const OutputType type, const std::string label)
Definition: wallet.cpp:2492
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:216
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:276
Definition: node.h:39
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database, DatabaseOptions &options)
Definition: util.cpp:47
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, ArgsManager &args, const CKey &key)
Definition: util.cpp:17
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
RPCHelpMan getnewaddress()
Definition: addresses.cpp:17
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:77
std::shared_ptr< CWallet > wallet
ArgsManager args
Definition: outputtype.h:17
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:149
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
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
#define LOCK2(cs1, cs2)
Definition: sync.h:259
#define LOCK(cs)
Definition: sync.h:258
bool error(const char *fmt, const Args &... args)
Definition: system.h:48