Bitcoin Core  27.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 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include <addresstype.h>
13 #include <wallet/db.h>
14 
15 #include <memory>
16 
17 class ArgsManager;
18 class CChain;
19 class CKey;
20 enum class OutputType;
21 namespace interfaces {
22 class Chain;
23 } // namespace interfaces
24 
25 namespace wallet {
26 class CWallet;
27 class WalletDatabase;
28 struct WalletContext;
29 
30 static const DatabaseFormat DATABASE_FORMATS[] = {
31 #ifdef USE_SQLITE
33 #endif
34 #ifdef USE_BDB
36 #endif
37 };
38 
39 const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
40 
41 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
42 
43 std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
44 std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
45 void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
46 
47 // Creates a copy of the provided database
48 std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
49 
51 std::string getnewaddress(CWallet& w);
54 
55 using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
56 
58 {
59 public:
60  MockableData::const_iterator m_cursor;
61  MockableData::const_iterator m_cursor_end;
62  bool m_pass;
63 
64  explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
65  MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
67 
68  Status Next(DataStream& key, DataStream& value) override;
69 };
70 
72 {
73 private:
75  bool m_pass;
76 
77  bool ReadKey(DataStream&& key, DataStream& value) override;
78  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
79  bool EraseKey(DataStream&& key) override;
80  bool HasKey(DataStream&& key) override;
82 
83 public:
84  explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
86 
87  void Flush() override {}
88  void Close() override {}
89 
90  std::unique_ptr<DatabaseCursor> GetNewCursor() override
91  {
92  return std::make_unique<MockableCursor>(m_records, m_pass);
93  }
94  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
95  return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
96  }
97  bool TxnBegin() override { return m_pass; }
98  bool TxnCommit() override { return m_pass; }
99  bool TxnAbort() override { return m_pass; }
100 };
101 
105 {
106 public:
108  bool m_pass{true};
109 
110  MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
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 
130 std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
131 
132 MockableDatabase& GetMockableDatabase(CWallet& wallet);
133 } // namespace wallet
134 
135 #endif // BITCOIN_WALLET_TEST_UTIL_H
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:131
An in-memory indexed chain of blocks.
Definition: chain.h:418
An encapsulated private key.
Definition: key.h:33
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:124
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:301
RAII class that provides access to a WalletDatabase.
Definition: db.h:45
void Flush() override
Definition: util.h:87
bool TxnAbort() override
Definition: util.h:99
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:124
bool TxnBegin() override
Definition: util.h:97
void Close() override
Definition: util.h:88
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: util.h:94
bool EraseKey(DataStream &&key) override
Definition: util.cpp:154
bool TxnCommit() override
Definition: util.h:98
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:90
bool HasKey(DataStream &&key) override
Definition: util.cpp:164
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: util.cpp:173
MockableData & m_records
Definition: util.h:74
MockableBatch(MockableData &records, bool pass)
Definition: util.h:84
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:139
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:64
MockableData::const_iterator m_cursor_end
Definition: util.h:61
MockableData::const_iterator m_cursor
Definition: util.h:60
Status Next(DataStream &key, DataStream &value) override
Definition: util.cpp:107
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
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
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: util.h:127
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:124
WalletDatabase()
Create dummy DB handle.
Definition: db.h:127
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:39
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:49
DatabaseFormat
Definition: db.h:177
static const DatabaseFormat DATABASE_FORMATS[]
Definition: util.h:30
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:72
MockableDatabase & GetMockableDatabase(CWallet &wallet)
Definition: util.cpp:195
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:190
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database)
Definition: util.cpp:80
RPCHelpMan getnewaddress()
Definition: addresses.cpp:23
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:55
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:91
OutputType
Definition: outputtype.h:17
const char * prefix
Definition: rest.cpp:1007
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:36