Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <streams.h>
10#include <wallet/db.h>
12#include <wallet/sqlite.h>
13
14#include <memory>
15#include <optional>
16#include <string>
17#include <utility>
18
19class ArgsManager;
20class CChain;
21class CKey;
22enum class OutputType;
23namespace interfaces {
24class Chain;
25} // namespace interfaces
26
27namespace wallet {
28class CWallet;
29class WalletDatabase;
30struct WalletContext;
31
35
36inline const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
37
38std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
39
40void AddKey(CWallet& wallet, const CKey& key);
41
42std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context);
43std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
44std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
45std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context);
46void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
47
49std::string getnewaddress(CWallet& w);
52
53using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
54
55
57{
58public:
61};
62
66{
67public:
69
70 bool Backup(const std::string& strDest) const override { return true; }
71
72 std::string Filename() override { return "mockable"; }
73 std::string Format() override { return "sqlite-mock"; }
74 std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<MockableSQLiteBatch>(*this); }
75};
76
79{
80public:
81 void FailNextWrite(std::string record_type, size_t match_skip_count = 0)
82 {
83 m_fail_write = {std::move(record_type), match_skip_count};
84 }
85 void FailNextErase(std::string record_type) { m_fail_erase = {std::move(record_type)}; }
86 void FailNextCommit() { m_fail_commit = true; }
87
88 std::optional<SerializeData> GetRecordValue(const std::string& record_type)
89 {
90 auto batch{MakeBatch()};
91 if (auto cursor{batch->GetNewPrefixCursor(DataStream() << record_type)}) {
92 DataStream key, value;
93 if (cursor->Next(key, value) == DatabaseCursor::Status::MORE) {
94 return SerializeData{value.begin(), value.end()};
95 }
96 }
97 return std::nullopt;
98 }
99
100 bool HasRecordType(const std::string& record_type) { return GetRecordValue(record_type).has_value(); }
101
102 std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<Batch>(*this); }
103
104private:
105 struct Failure {
106 std::string record_type;
108 };
109
110 static bool ShouldFail(std::optional<Failure>& failure, const DataStream& key)
111 {
112 if (!failure) return false;
113 std::string record_type;
114 SpanReader{MakeByteSpan(key)} >> record_type;
115 if (failure->record_type != record_type) return false;
116 if (failure->match_skip_count > 0) {
117 --failure->match_skip_count;
118 return false;
119 }
120 failure.reset();
121 return true;
122 }
123
126
127 bool TxnCommit() override
128 {
129 if (std::exchange(m_owner.m_fail_commit, false)) return false;
130 return SQLiteBatch::TxnCommit();
131 }
132
133 bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override
134 {
135 if (ShouldFail(m_owner.m_fail_write, key)) return false;
136 return SQLiteBatch::WriteKey(std::move(key), std::move(value), overwrite);
137 }
138
139 bool EraseKey(DataStream&& key) override
140 {
141 if (ShouldFail(m_owner.m_fail_erase, key)) return false;
142 return SQLiteBatch::EraseKey(std::move(key));
143 }
144
146 };
147
148 std::optional<Failure> m_fail_write;
149 std::optional<Failure> m_fail_erase;
150 bool m_fail_commit{false};
151};
152
153std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase();
154
155DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, bool success);
156} // namespace wallet
157
158#endif // BITCOIN_WALLET_TEST_UTIL_H
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
An in-memory indexed chain of blocks.
Definition chain.h:380
An encapsulated private key.
Definition key.h:46
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:165
const_iterator begin() const
Definition streams.h:194
const_iterator end() const
Definition streams.h:196
Minimal stream for reading from an existing byte array by std::span.
Definition streams.h:83
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition chain.h:117
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:318
A SQLite wallet database that can fail selected operations for testing.
Definition util.h:79
void FailNextWrite(std::string record_type, size_t match_skip_count=0)
Definition util.h:81
std::unique_ptr< DatabaseBatch > MakeBatch() override
Make a DatabaseBatch connected to this database.
Definition util.h:102
std::optional< Failure > m_fail_write
Definition util.h:148
std::optional< Failure > m_fail_erase
Definition util.h:149
bool HasRecordType(const std::string &record_type)
Definition util.h:100
static bool ShouldFail(std::optional< Failure > &failure, const DataStream &key)
Definition util.h:110
std::optional< SerializeData > GetRecordValue(const std::string &record_type)
Definition util.h:88
void FailNextErase(std::string record_type)
Definition util.h:85
An in-memory SQLiteDatabase.
Definition sqlite.h:181
A WalletDatabase whose contents and return values can be modified as needed for testing.
Definition util.h:66
std::string Filename() override
Return path to main database file for logs and error messages.
Definition util.h:72
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition util.h:70
std::unique_ptr< DatabaseBatch > MakeBatch() override
Make a DatabaseBatch connected to this database.
Definition util.h:74
std::string Format() override
Definition util.h:73
RAII class that provides access to a WalletDatabase.
Definition sqlite.h:52
bool TxnCommit() override
Definition sqlite.cpp:679
SQLiteBatch(SQLiteDatabase &database)
Definition sqlite.cpp:409
bool EraseKey(DataStream &&key) override
Definition sqlite.cpp:553
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition sqlite.cpp:496
wallet::DescriptorScriptPubKeyMan * CreateDescriptor(CWallet &keystore, const std::string &desc_str, const bool success)
Definition util.cpp:131
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition util.cpp:126
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition util.h:36
DatabaseFormat
Definition db.h:163
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, const CKey &key)
Definition util.cpp:22
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition util.cpp:103
constexpr DatabaseFormat DATABASE_FORMATS[]
Definition util.h:32
void AddKey(CWallet &wallet, const CKey &key)
Definition util.cpp:45
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context)
Definition util.cpp:81
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition util.cpp:57
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition util.h:53
RPCMethod getnewaddress()
Definition addresses.cpp:21
CTxDestination getNewDestination(CWallet &w, OutputType output_type)
Returns a new destination, of an specific type, from the wallet.
Definition util.cpp:117
OutputType
Definition outputtype.h:18
auto MakeByteSpan(const V &v) noexcept
Definition span.h:84
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition util.h:133
Batch(FaultInjectingDatabase &database)
Definition util.h:125
FaultInjectingDatabase & m_owner
Definition util.h:145
bool EraseKey(DataStream &&key) override
Definition util.h:139
WalletContext struct containing references to state shared between CWallet instances,...
Definition context.h:36
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
WalletContext context
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.