Bitcoin Core 31.99.0
P2P Digital Currency
wallet_loading.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present 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 <addresstype.h>
6#include <bench/bench.h>
7#include <consensus/amount.h>
8#include <outputtype.h>
11#include <util/check.h>
12#include <wallet/context.h>
13#include <wallet/db.h>
14#include <wallet/test/util.h>
15#include <wallet/transaction.h>
16#include <wallet/wallet.h>
17#include <wallet/walletutil.h>
18
19#include <cstdint>
20#include <memory>
21#include <utility>
22#include <vector>
23
24namespace wallet{
25static void AddTx(CWallet& wallet)
26{
28 mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet.GetNewDestination(OutputType::BECH32, ""))));
29 mtx.vin.emplace_back();
30
31 wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});
32}
33
35{
36 const auto test_setup = MakeNoLogFileContext<TestingSetup>();
37
38 WalletContext context;
39 context.args = &test_setup->m_args;
40 context.chain = test_setup->m_node.chain.get();
41
42 // Setup the wallet
43 // Loading the wallet will also create it
44 uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;
45 DatabaseStatus status;
46 DatabaseOptions options;
48 options.require_create = true;
49 bilingual_str error;
50 auto database = MakeWalletDatabase("", options, status, error);
51 auto wallet = TestCreateWallet(std::move(database), context, create_flags);
52
53 // Generate a bunch of transactions and addresses to put into the wallet
54 for (int i = 0; i < 1000; ++i) {
55 AddTx(*wallet);
56 }
57
58 options.require_create = false;
59 options.require_existing = true;
60
61 bench.epochs(5)
62 .setup([&] {
63 TestUnloadWallet(std::move(wallet));
64 database = MakeWalletDatabase("", options, status, error);
65 })
66 .run([&] {
67 wallet = TestLoadWallet(std::move(database), context);
68 });
69
70 // Cleanup
71 TestUnloadWallet(std::move(wallet));
72}
73
75} // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
#define Assert(val)
Identity function.
Definition: check.h:116
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:633
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
detail::SetupRunner< SetupOp > setup(SetupOp setupOp)
Configure an untimed setup step per epoch (forces single-iteration epochs).
Definition: nanobench.h:1286
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:309
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:99
static void AddTx(CWallet &wallet)
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition: wallet.cpp:2982
BENCHMARK(CoinSelection)
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context)
Definition: util.cpp:76
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:51
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
static void WalletLoadingDescriptors(benchmark::Bench &bench)
DatabaseStatus
Definition: db.h:186
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxOut > vout
Definition: transaction.h:360
std::vector< CTxIn > vin
Definition: transaction.h:359
Bilingual messages:
Definition: translation.h:24
bool require_existing
Definition: db.h:173
std::optional< DatabaseFormat > require_format
Definition: db.h:175
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:59
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:36
interfaces::Chain * chain
Definition: context.h:37
ArgsManager * args
Definition: context.h:39