Bitcoin Core  28.1.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 <config/bitcoin-config.h> // IWYU pragma: keep
9 
10 #include <addresstype.h>
11 #include <wallet/db.h>
12 
13 #include <memory>
14 
15 class ArgsManager;
16 class CChain;
17 class CKey;
18 enum class OutputType;
19 namespace interfaces {
20 class Chain;
21 } // namespace interfaces
22 
23 namespace wallet {
24 class CWallet;
25 class WalletDatabase;
26 struct WalletContext;
27 
28 static const DatabaseFormat DATABASE_FORMATS[] = {
29 #ifdef USE_SQLITE
31 #endif
32 #ifdef USE_BDB
34 #endif
35 };
36 
37 const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
38 
39 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
40 
41 std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
42 std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
43 void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
44 
45 // Creates a copy of the provided database
46 std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
47 
49 std::string getnewaddress(CWallet& w);
52 
53 using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
54 
56 {
57 public:
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 {
71 private:
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 
81 public:
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 };
99 
103 {
104 public:
106  bool m_pass{true};
107 
108  MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
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 
128 std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
129 
130 MockableDatabase& GetMockableDatabase(CWallet& wallet);
131 } // namespace wallet
132 
133 #endif // BITCOIN_WALLET_TEST_UTIL_H
MockableBatch(MockableData &records, bool pass)
Definition: util.h:82
Status Next(DataStream &key, DataStream &value) override
Definition: util.cpp:102
void Close() override
Flush to the database file and close the database.
Definition: util.h:118
bool TxnAbort() override
Definition: util.h:97
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: util.cpp:168
void Flush() override
Definition: util.h:85
MockableData m_records
Definition: util.h:105
An in-memory indexed chain of blocks.
Definition: chain.h:416
static const DatabaseFormat DATABASE_FORMATS[]
Definition: util.h:28
void Flush() override
Make sure all changes are flushed to database file.
Definition: util.h:117
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:53
const char * prefix
Definition: rest.cpp:1007
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed...
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:115
WalletDatabase()
Create dummy DB handle.
Definition: db.h:133
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:62
MockableData::const_iterator m_cursor
Definition: util.h:58
bool TxnCommit() override
Definition: util.h:96
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:91
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database)
Definition: util.cpp:80
MockableData::const_iterator m_cursor_end
Definition: util.h:59
MockableData & m_records
Definition: util.h:72
void ReloadDbEnv() override
Definition: util.h:121
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:49
RAII class that provides access to a WalletDatabase.
Definition: db.h:50
OutputType
Definition: outputtype.h:17
std::string Format() override
Definition: util.h:124
MockableDatabase(MockableData records={})
Definition: util.h:108
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:88
void Close() override
Definition: util.h:86
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: util.h:123
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
bool EraseKey(DataStream &&key) override
Definition: util.cpp:149
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: util.h:125
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:299
MockableDatabase & GetMockableDatabase(CWallet &wallet)
Definition: util.cpp:190
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:134
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: util.h:116
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
Definition: util.cpp:20
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:185
void Open() override
Open the database if it is not already opened.
Definition: util.h:111
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: util.h:92
A WalletDatabase whose contents and return values can be modified as needed for testing.
Definition: util.h:102
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:134
DatabaseFormat
Definition: db.h:183
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:72
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:119
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
WalletContext struct containing references to state shared between CWallet instances, like the reference to the chain interface, and the list of opened wallets.
Definition: context.h:36
bool PeriodicFlush() override
Definition: util.h:119
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:37
An encapsulated private key.
Definition: key.h:34
bool TxnBegin() override
Definition: util.h:95
bool HasKey(DataStream &&key) override
Definition: util.cpp:159
RPCHelpMan getnewaddress()
Definition: addresses.cpp:21
void IncrementUpdateCounter() override
Definition: util.h:120
void AddRef() override
Indicate the a new database user has began using the database.
Definition: util.h:112
An instance of this class represents one database.
Definition: db.h:129