Bitcoin Core 29.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 <bitcoin-build-config.h> // IWYU pragma: keep
9
10#include <addresstype.h>
11#include <wallet/db.h>
13
14#include <memory>
15
16class ArgsManager;
17class CChain;
18class CKey;
19enum class OutputType;
20namespace interfaces {
21class Chain;
22} // namespace interfaces
23
24namespace wallet {
25class CWallet;
26class WalletDatabase;
27struct WalletContext;
28
31#ifdef USE_BDB
33#endif
34};
35
36const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
37
38std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
39
40std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
41std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
42void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
43
44// Creates a copy of the provided database
45std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
46
48std::string getnewaddress(CWallet& w);
51
52using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
53
55{
56public:
57 MockableData::const_iterator m_cursor;
58 MockableData::const_iterator m_cursor_end;
59 bool m_pass;
60
61 explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
62 MockableCursor(const MockableData& records, bool pass, std::span<const std::byte> prefix);
63 ~MockableCursor() = default;
64
65 Status Next(DataStream& key, DataStream& value) override;
66};
67
69{
70private:
72 bool m_pass;
73
74 bool ReadKey(DataStream&& key, DataStream& value) override;
75 bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
76 bool EraseKey(DataStream&& key) override;
77 bool HasKey(DataStream&& key) override;
78 bool ErasePrefix(std::span<const std::byte> prefix) override;
79
80public:
81 explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
82 ~MockableBatch() = default;
83
84 void Flush() override {}
85 void Close() override {}
86
87 std::unique_ptr<DatabaseCursor> GetNewCursor() override
88 {
89 return std::make_unique<MockableCursor>(m_records, m_pass);
90 }
91 std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override {
92 return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
93 }
94 bool TxnBegin() override { return m_pass; }
95 bool TxnCommit() override { return m_pass; }
96 bool TxnAbort() override { return m_pass; }
97 bool HasActiveTxn() override { return false; }
98};
99
103{
104public:
106 bool m_pass{true};
107
109 ~MockableDatabase() = default;
110
111 void Open() override {}
112 void AddRef() override {}
113 void RemoveRef() override {}
114
115 bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
116 bool Backup(const std::string& strDest) const override { return m_pass; }
117 void Flush() override {}
118 void Close() override {}
119 bool PeriodicFlush() override { return m_pass; }
120 void IncrementUpdateCounter() override {}
121 void ReloadDbEnv() override {}
122
123 std::string Filename() override { return "mockable"; }
124 std::string Format() override { return "mock"; }
125 std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
126};
127
128std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
129MockableDatabase& GetMockableDatabase(CWallet& wallet);
130
131ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success);
132} // namespace wallet
133
134#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:140
An in-memory indexed chain of blocks.
Definition: chain.h:417
An encapsulated private key.
Definition: key.h:35
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:129
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:300
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
void Flush() override
Definition: util.h:84
bool TxnAbort() override
Definition: util.h:96
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:120
bool TxnBegin() override
Definition: util.h:94
void Close() override
Definition: util.h:85
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:87
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(std::span< const std::byte > prefix) override
Definition: util.h:91
bool HasActiveTxn() override
Definition: util.h:97
bool EraseKey(DataStream &&key) override
Definition: util.cpp:150
bool TxnCommit() override
Definition: util.h:95
bool ErasePrefix(std::span< const std::byte > prefix) override
Definition: util.cpp:169
bool HasKey(DataStream &&key) override
Definition: util.cpp:160
MockableData & m_records
Definition: util.h:71
MockableBatch(MockableData &records, bool pass)
Definition: util.h:81
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:135
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:61
MockableData::const_iterator m_cursor_end
Definition: util.h:58
MockableData::const_iterator m_cursor
Definition: util.h:57
Status Next(DataStream &key, DataStream &value) override
Definition: util.cpp:103
A WalletDatabase whose contents and return values can be modified as needed for testing.
Definition: util.h:103
MockableDatabase(MockableData records={})
Definition: util.h:108
void Open() override
Open the database if it is not already opened.
Definition: util.h:111
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
Definition: util.h:115
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: util.h:116
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: util.h:125
void Close() override
Flush to the database file and close the database.
Definition: util.h:118
std::string Format() override
Definition: util.h:124
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: util.h:123
void IncrementUpdateCounter() override
Definition: util.h:120
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: util.h:113
void AddRef() override
Indicate the a new database user has began using the database.
Definition: util.h:112
void ReloadDbEnv() override
Definition: util.h:121
bool PeriodicFlush() override
Definition: util.h:119
MockableData m_records
Definition: util.h:105
void Flush() override
Make sure all changes are flushed to database file.
Definition: util.h:117
An instance of this class represents one database.
Definition: db.h:131
WalletDatabase()
Create dummy DB handle.
Definition: db.h:134
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:36
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
DatabaseFormat
Definition: db.h:184
static const DatabaseFormat DATABASE_FORMATS[]
Definition: util.h:29
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:73
MockableDatabase & GetMockableDatabase(CWallet &wallet)
Definition: util.cpp:191
wallet::ScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition: util.cpp:196
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:186
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database)
Definition: util.cpp:81
RPCHelpMan getnewaddress()
Definition: addresses.cpp:21
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:52
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:92
OutputType
Definition: outputtype.h:17
const char * prefix
Definition: rest.cpp:1009
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:36