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