Bitcoin Core  31.0.0
P2P Digital Currency
util.h
Go to the documentation of this file.
1 // Copyright (c) 2021-present 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 <addresstype.h>
9 #include <wallet/db.h>
10 #include <wallet/scriptpubkeyman.h>
11 
12 #include <memory>
13 
14 class ArgsManager;
15 class CChain;
16 class CKey;
17 enum class OutputType;
18 namespace interfaces {
19 class Chain;
20 } // namespace interfaces
21 
22 namespace wallet {
23 class CWallet;
24 class WalletDatabase;
25 struct WalletContext;
26 
27 static const DatabaseFormat DATABASE_FORMATS[] = {
29 };
30 
31 const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
32 
33 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
34 
35 std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context);
36 std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
37 std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
38 std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context);
39 void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
40 
41 // Creates a copy of the provided database
42 std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
43 
45 std::string getnewaddress(CWallet& w);
48 
49 using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
50 
52 {
53 public:
54  MockableData::const_iterator m_cursor;
55  MockableData::const_iterator m_cursor_end;
56  bool m_pass;
57 
58  explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
59  MockableCursor(const MockableData& records, bool pass, std::span<const std::byte> prefix);
60  ~MockableCursor() = default;
61 
62  Status Next(DataStream& key, DataStream& value) override;
63 };
64 
66 {
67 private:
69  bool m_pass;
70 
71  bool ReadKey(DataStream&& key, DataStream& value) override;
72  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
73  bool EraseKey(DataStream&& key) override;
74  bool HasKey(DataStream&& key) override;
75  bool ErasePrefix(std::span<const std::byte> prefix) override;
76 
77 public:
78  explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
79  ~MockableBatch() = default;
80 
81  void Close() override {}
82 
83  std::unique_ptr<DatabaseCursor> GetNewCursor() override
84  {
85  return std::make_unique<MockableCursor>(m_records, m_pass);
86  }
87  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override {
88  return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
89  }
90  bool TxnBegin() override { return m_pass; }
91  bool TxnCommit() override { return m_pass; }
92  bool TxnAbort() override { return m_pass; }
93  bool HasActiveTxn() override { return false; }
94 };
95 
99 {
100 public:
102  bool m_pass{true};
103 
104  MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
105  ~MockableDatabase() = default;
106 
107  void Open() override {}
108 
109  bool Rewrite() override { return m_pass; }
110  bool Backup(const std::string& strDest) const override { return m_pass; }
111  void Close() override {}
112 
113  std::string Filename() override { return "mockable"; }
114  std::vector<fs::path> Files() override { return {}; }
115  std::string Format() override { return "mock"; }
116  std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<MockableBatch>(m_records, m_pass); }
117 };
118 
119 std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
120 MockableDatabase& GetMockableDatabase(CWallet& wallet);
121 
122 DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, bool success);
123 } // namespace wallet
124 
125 #endif // BITCOIN_WALLET_TEST_UTIL_H
MockableBatch(MockableData &records, bool pass)
Definition: util.h:78
Status Next(DataStream &key, DataStream &value) override
Definition: util.cpp:128
void Close() override
Flush to the database file and close the database.
Definition: util.h:111
bool TxnAbort() override
Definition: util.h:92
MockableData m_records
Definition: util.h:101
An in-memory indexed chain of blocks.
Definition: chain.h:379
static const DatabaseFormat DATABASE_FORMATS[]
Definition: util.h:27
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:49
bool Rewrite() override
Rewrite the entire database on disk.
Definition: util.h:109
const char * prefix
Definition: rest.cpp:1141
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:58
MockableData::const_iterator m_cursor
Definition: util.h:54
bool TxnCommit() override
Definition: util.h:91
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context)
Definition: util.cpp:75
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:117
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database)
Definition: util.cpp:106
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(std::span< const std::byte > prefix) override
Definition: util.h:87
MockableData::const_iterator m_cursor_end
Definition: util.h:55
MockableData & m_records
Definition: util.h:68
RAII class that provides access to a WalletDatabase.
Definition: db.h:50
OutputType
Definition: outputtype.h:18
WalletDatabase()=default
Create dummy DB handle.
std::string Format() override
Definition: util.h:115
MockableDatabase(MockableData records={})
Definition: util.h:104
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:83
void Close() override
Definition: util.h:81
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: util.h:113
wallet::DescriptorScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition: util.cpp:221
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
bool EraseKey(DataStream &&key) override
Definition: util.cpp:175
std::unique_ptr< DatabaseBatch > MakeBatch() override
Make a DatabaseBatch connected to this database.
Definition: util.h:116
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:309
MockableDatabase & GetMockableDatabase(CWallet &wallet)
Definition: util.cpp:216
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:160
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: util.h:110
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:211
void Open() override
Open the database if it is not already opened.
Definition: util.h:107
A WalletDatabase whose contents and return values can be modified as needed for testing.
Definition: util.h:98
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:117
DatabaseFormat
Definition: db.h:167
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:98
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:145
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
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:93
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:31
std::vector< fs::path > Files() override
Return paths to all database created files.
Definition: util.h:114
An encapsulated private key.
Definition: key.h:35
bool TxnBegin() override
Definition: util.h:90
bool HasKey(DataStream &&key) override
Definition: util.cpp:185
RPCHelpMan getnewaddress()
Definition: addresses.cpp:21
bool ErasePrefix(std::span< const std::byte > prefix) override
Definition: util.cpp:194
An instance of this class represents one database.
Definition: db.h:129