Coverage Report

Created: 2025-02-21 14:36

/root/bitcoin/src/wallet/test/util.h
Line
Count
Source (jump to first uncovered line)
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
31
       DatabaseFormat::SQLITE,
32
#endif
33
#ifdef USE_BDB
34
       DatabaseFormat::BERKELEY,
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
49
/** Returns a new encoded destination from the wallet (hardcoded to BECH32) */
50
std::string getnewaddress(CWallet& w);
51
/** Returns a new destination, of an specific type, from the wallet */
52
CTxDestination getNewDestination(CWallet& w, OutputType output_type);
53
54
using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
55
56
class MockableCursor: public DatabaseCursor
57
{
58
public:
59
    MockableData::const_iterator m_cursor;
60
    MockableData::const_iterator m_cursor_end;
61
    bool m_pass;
62
63
0
    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
70
class MockableBatch : public DatabaseBatch
71
{
72
private:
73
    MockableData& m_records;
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;
80
    bool ErasePrefix(Span<const std::byte> prefix) override;
81
82
public:
83
0
    explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
84
    ~MockableBatch() = default;
85
86
0
    void Flush() override {}
87
0
    void Close() override {}
88
89
    std::unique_ptr<DatabaseCursor> GetNewCursor() override
90
0
    {
91
0
        return std::make_unique<MockableCursor>(m_records, m_pass);
92
0
    }
93
0
    std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
94
0
        return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
95
0
    }
96
0
    bool TxnBegin() override { return m_pass; }
97
0
    bool TxnCommit() override { return m_pass; }
98
0
    bool TxnAbort() override { return m_pass; }
99
0
    bool HasActiveTxn() override { return false; }
100
};
101
102
/** A WalletDatabase whose contents and return values can be modified as needed for testing
103
 **/
104
class MockableDatabase : public WalletDatabase
105
{
106
public:
107
    MockableData m_records;
108
    bool m_pass{true};
109
110
0
    MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
111
0
    ~MockableDatabase() = default;
112
113
0
    void Open() override {}
114
0
    void AddRef() override {}
115
0
    void RemoveRef() override {}
116
117
0
    bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
118
0
    bool Backup(const std::string& strDest) const override { return m_pass; }
119
0
    void Flush() override {}
120
0
    void Close() override {}
121
0
    bool PeriodicFlush() override { return m_pass; }
122
0
    void IncrementUpdateCounter() override {}
123
0
    void ReloadDbEnv() override {}
124
125
0
    std::string Filename() override { return "mockable"; }
126
0
    std::string Format() override { return "mock"; }
127
0
    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