Bitcoin Core  26.99.0
P2P Digital Currency
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
8 #include <addresstype.h>
9 #include <wallet/db.h>
11 #include <memory>
13 class ArgsManager;
14 class CChain;
15 class CKey;
16 enum class OutputType;
17 namespace interfaces {
18 class Chain;
19 } // namespace interfaces
21 namespace wallet {
22 class CWallet;
23 class WalletDatabase;
24 struct WalletContext;
26 static const DatabaseFormat DATABASE_FORMATS[] = {
27 #ifdef USE_SQLITE
29 #endif
30 #ifdef USE_BDB
32 #endif
33 };
35 const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
37 std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
39 std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
40 std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
41 void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
43 // Creates a copy of the provided database
44 std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
47 std::string getnewaddress(CWallet& w);
51 using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
54 {
55 public:
56  MockableData::const_iterator m_cursor;
57  MockableData::const_iterator m_cursor_end;
58  bool m_pass;
60  explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
61  MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
64  Status Next(DataStream& key, DataStream& value) override;
65 };
68 {
69 private:
71  bool m_pass;
73  bool ReadKey(DataStream&& key, DataStream& value) override;
74  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
75  bool EraseKey(DataStream&& key) override;
76  bool HasKey(DataStream&& key) override;
79 public:
80  explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
83  void Flush() override {}
84  void Close() override {}
86  std::unique_ptr<DatabaseCursor> GetNewCursor() override
87  {
88  return std::make_unique<MockableCursor>(m_records, m_pass);
89  }
90  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
91  return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
92  }
93  bool TxnBegin() override { return m_pass; }
94  bool TxnCommit() override { return m_pass; }
95  bool TxnAbort() override { return m_pass; }
96 };
101 {
102 public:
104  bool m_pass{true};
106  MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
109  void Open() override {}
110  void AddRef() override {}
111  void RemoveRef() override {}
113  bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
114  bool Backup(const std::string& strDest) const override { return m_pass; }
115  void Flush() override {}
116  void Close() override {}
117  bool PeriodicFlush() override { return m_pass; }
118  void IncrementUpdateCounter() override {}
119  void ReloadDbEnv() override {}
121  std::string Filename() override { return "mockable"; }
122  std::string Format() override { return "mock"; }
123  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
124 };
126 std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
128 MockableDatabase& GetMockableDatabase(CWallet& wallet);
129 } // namespace wallet
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:442
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:123
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:46
void Flush() override
Definition: util.h:83
bool TxnAbort() override
Definition: util.h:95
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: util.cpp:124
bool TxnBegin() override
Definition: util.h:93
void Close() override
Definition: util.h:84
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: util.h:90
bool EraseKey(DataStream &&key) override
Definition: util.cpp:154
bool TxnCommit() override
Definition: util.h:94
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: util.h:86
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:70
MockableBatch(MockableData &records, bool pass)
Definition: util.h:80
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: util.cpp:139
MockableCursor(const MockableData &records, bool pass)
Definition: util.h:60
MockableData::const_iterator m_cursor_end
Definition: util.h:57
MockableData::const_iterator m_cursor
Definition: util.h:56
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:101
MockableDatabase(MockableData records={})
Definition: util.h:106
void Open() override
Open the database if it is not already opened.
Definition: util.h:109
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:113
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: util.h:114
void Close() override
Flush to the database file and close the database.
Definition: util.h:116
std::string Format() override
Definition: util.h:122
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: util.h:121
void IncrementUpdateCounter() override
Definition: util.h:118
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: util.h:111
void AddRef() override
Indicate the a new database user has began using the database.
Definition: util.h:110
void ReloadDbEnv() override
Definition: util.h:119
bool PeriodicFlush() override
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:123
MockableData m_records
Definition: util.h:103
void Flush() override
Make sure all changes are flushed to database file.
Definition: util.h:115
An instance of this class represents one database.
Definition: db.h:125
Create dummy DB handle.
Definition: db.h:128
Definition: util.h:35
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
Definition: db.h:178
static const DatabaseFormat DATABASE_FORMATS[]
Definition: util.h:26
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: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:19
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:51
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition: util.cpp:91
WalletContext context
Definition: outputtype.h:17
const char * prefix
Definition: rest.cpp:1002
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:36