Bitcoin Core 28.99.0
P2P Digital Currency
util.h
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 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
30#ifdef USE_SQLITE
32#endif
33#ifdef USE_BDB
35#endif
36};
37
38const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
39
40std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
41
42std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
43std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
44void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
45
46// Creates a copy of the provided database
47std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
48
50std::string getnewaddress(CWallet& w);
53
54using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
55
57{
58public:
59 MockableData::const_iterator m_cursor;
60 MockableData::const_iterator m_cursor_end;
61 bool m_pass;
62
63 explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
64 MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
65 ~MockableCursor() = default;
66
67 Status Next(DataStream& key, DataStream& value) override;
68};
69
71{
72private:
74 bool m_pass;
75
76 bool ReadKey(DataStream&& key, DataStream& value) override;
77 bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
78 bool EraseKey(DataStream&& key) override;
79 bool HasKey(DataStream&& key) override;
81
82public:
83 explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
84 ~MockableBatch() = default;
85
86 void Flush() override {}
87 void Close() override {}
88
89 std::unique_ptr<DatabaseCursor> GetNewCursor() override
90 {
91 return std::make_unique<MockableCursor>(m_records, m_pass);
92 }
93 std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
94 return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
95 }
96 bool TxnBegin() override { return m_pass; }
97 bool TxnCommit() override { return m_pass; }
98 bool TxnAbort() override { return m_pass; }
99 bool HasActiveTxn() override { return false; }
100};
101
105{
106public:
108 bool m_pass{true};
109
111 ~MockableDatabase() = default;
112
113 void Open() override {}
114 void AddRef() override {}
115 void RemoveRef() override {}
116
117 bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
118 bool Backup(const std::string& strDest) const override { return m_pass; }
119 void Flush() override {}
120 void Close() override {}
121 bool PeriodicFlush() override { return m_pass; }
122 void IncrementUpdateCounter() override {}
123 void ReloadDbEnv() override {}
124
125 std::string Filename() override { return "mockable"; }
126 std::string Format() override { return "mock"; }
127 std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
128};
129
130std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
131MockableDatabase& GetMockableDatabase(CWallet& wallet);
132
133ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success);
134} // namespace wallet
135
136#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:86
bool TxnAbort() override
Definition: util.h:98
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:120
bool TxnBegin() override
Definition: util.h:96
void Close() override
Definition: util.h:87
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: util.h:93
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:89
bool HasActiveTxn() override
Definition: util.h:99
bool EraseKey(DataStream &&key) override
Definition: util.cpp:150
bool TxnCommit() override
Definition: util.h:97
bool HasKey(DataStream &&key) override
Definition: util.cpp:160
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: util.cpp:169
MockableData & m_records
Definition: util.h:73
MockableBatch(MockableData &records, bool pass)
Definition: util.h:83
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:135
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:63
MockableData::const_iterator m_cursor_end
Definition: util.h:60
MockableData::const_iterator m_cursor
Definition: util.h:59
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:105
MockableDatabase(MockableData records={})
Definition: util.h:110
void Open() override
Open the database if it is not already opened.
Definition: util.h:113
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:117
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: util.h:118
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: util.h:127
void Close() override
Flush to the database file and close the database.
Definition: util.h:120
std::string Format() override
Definition: util.h:126
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: util.h:125
void IncrementUpdateCounter() override
Definition: util.h:122
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: util.h:115
void AddRef() override
Indicate the a new database user has began using the database.
Definition: util.h:114
void ReloadDbEnv() override
Definition: util.h:123
bool PeriodicFlush() override
Definition: util.h:121
MockableData m_records
Definition: util.h:107
void Flush() override
Make sure all changes are flushed to database file.
Definition: util.h:119
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:38
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:54
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