Bitcoin Core 30.99.0
P2P Digital Currency
util.h
Go to the documentation of this file.
1// Copyright (c) 2021-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#ifndef BITCOIN_WALLET_TEST_UTIL_H
6#define BITCOIN_WALLET_TEST_UTIL_H
7
8#include <addresstype.h>
9#include <wallet/db.h>
11
12#include <memory>
13
14class ArgsManager;
15class CChain;
16class CKey;
17enum class OutputType;
18namespace interfaces {
19class Chain;
20} // namespace interfaces
21
22namespace wallet {
23class CWallet;
24class WalletDatabase;
25struct WalletContext;
26
29};
30
31const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
32
33std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
34
35std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context);
36std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
37std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
38std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context);
39void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
40
41// Creates a copy of the provided database
42std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
43
45std::string getnewaddress(CWallet& w);
48
49using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
50
52{
53public:
54 MockableData::const_iterator m_cursor;
55 MockableData::const_iterator m_cursor_end;
56 bool m_pass;
57
58 explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
59 MockableCursor(const MockableData& records, bool pass, std::span<const std::byte> prefix);
60 ~MockableCursor() = default;
61
62 Status Next(DataStream& key, DataStream& value) override;
63};
64
66{
67private:
69 bool m_pass;
70
71 bool ReadKey(DataStream&& key, DataStream& value) override;
72 bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
73 bool EraseKey(DataStream&& key) override;
74 bool HasKey(DataStream&& key) override;
75 bool ErasePrefix(std::span<const std::byte> prefix) override;
76
77public:
78 explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
79 ~MockableBatch() = default;
80
81 void Close() override {}
82
83 std::unique_ptr<DatabaseCursor> GetNewCursor() override
84 {
85 return std::make_unique<MockableCursor>(m_records, m_pass);
86 }
87 std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override {
88 return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
89 }
90 bool TxnBegin() override { return m_pass; }
91 bool TxnCommit() override { return m_pass; }
92 bool TxnAbort() override { return m_pass; }
93 bool HasActiveTxn() override { return false; }
94};
95
99{
100public:
102 bool m_pass{true};
103
105 ~MockableDatabase() = default;
106
107 void Open() override {}
108
109 bool Rewrite() override { return m_pass; }
110 bool Backup(const std::string& strDest) const override { return m_pass; }
111 void Close() override {}
112
113 std::string Filename() override { return "mockable"; }
114 std::vector<fs::path> Files() override { return {}; }
115 std::string Format() override { return "mock"; }
116 std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<MockableBatch>(m_records, m_pass); }
117};
118
119std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
120MockableDatabase& GetMockableDatabase(CWallet& wallet);
121
122DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, bool success);
123} // namespace wallet
124
125#endif // BITCOIN_WALLET_TEST_UTIL_H
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
An in-memory indexed chain of blocks.
Definition: chain.h:380
An encapsulated private key.
Definition: key.h:36
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:133
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:118
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:310
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
bool TxnAbort() override
Definition: util.h:92
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:145
bool TxnBegin() override
Definition: util.h:90
void Close() override
Definition: util.h:81
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:83
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(std::span< const std::byte > prefix) override
Definition: util.h:87
bool HasActiveTxn() override
Definition: util.h:93
bool EraseKey(DataStream &&key) override
Definition: util.cpp:175
bool TxnCommit() override
Definition: util.h:91
bool ErasePrefix(std::span< const std::byte > prefix) override
Definition: util.cpp:194
bool HasKey(DataStream &&key) override
Definition: util.cpp:185
MockableData & m_records
Definition: util.h:68
MockableBatch(MockableData &records, bool pass)
Definition: util.h:78
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:160
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:58
MockableData::const_iterator m_cursor_end
Definition: util.h:55
MockableData::const_iterator m_cursor
Definition: util.h:54
Status Next(DataStream &key, DataStream &value) override
Definition: util.cpp:128
A WalletDatabase whose contents and return values can be modified as needed for testing.
Definition: util.h:99
bool Rewrite() override
Rewrite the entire database on disk.
Definition: util.h:109
MockableDatabase(MockableData records={})
Definition: util.h:104
void Open() override
Open the database if it is not already opened.
Definition: util.h:107
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: util.h:110
void Close() override
Flush to the database file and close the database.
Definition: util.h:111
std::string Format() override
Definition: util.h:115
std::unique_ptr< DatabaseBatch > MakeBatch() override
Make a DatabaseBatch connected to this database.
Definition: util.h:116
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: util.h:113
std::vector< fs::path > Files() override
Return paths to all database created files.
Definition: util.h:114
MockableData m_records
Definition: util.h:101
An instance of this class represents one database.
Definition: db.h:130
WalletDatabase()=default
Create dummy DB handle.
wallet::DescriptorScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition: util.cpp:221
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:31
DatabaseFormat
Definition: db.h:167
static const DatabaseFormat DATABASE_FORMATS[]
Definition: util.h:27
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
Definition: util.cpp:20
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:98
MockableDatabase & GetMockableDatabase(CWallet &wallet)
Definition: util.cpp:216
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:211
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database)
Definition: util.cpp:106
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context)
Definition: util.cpp:75
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
RPCHelpMan getnewaddress()
Definition: addresses.cpp:21
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:49
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:117
OutputType
Definition: outputtype.h:18
const char * prefix
Definition: rest.cpp:1141
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:36