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