Coverage Report

Created: 2025-03-18 19:34

/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
       DatabaseFormat::SQLITE,
31
#ifdef USE_BDB
32
       DatabaseFormat::BERKELEY,
33
#endif
34
};
35
36
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
37
38
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
39
40
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
41
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
42
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
43
44
// Creates a copy of the provided database
45
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
46
47
/** Returns a new encoded destination from the wallet (hardcoded to BECH32) */
48
std::string getnewaddress(CWallet& w);
49
/** Returns a new destination, of an specific type, from the wallet */
50
CTxDestination getNewDestination(CWallet& w, OutputType output_type);
51
52
using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
53
54
class MockableCursor: public DatabaseCursor
55
{
56
public:
57
    MockableData::const_iterator m_cursor;
58
    MockableData::const_iterator m_cursor_end;
59
    bool m_pass;
60
61
0
    explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
62
    MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
63
    ~MockableCursor() = default;
64
65
    Status Next(DataStream& key, DataStream& value) override;
66
};
67
68
class MockableBatch : public DatabaseBatch
69
{
70
private:
71
    MockableData& m_records;
72
    bool m_pass;
73
74
    bool ReadKey(DataStream&& key, DataStream& value) override;
75
    bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
76
    bool EraseKey(DataStream&& key) override;
77
    bool HasKey(DataStream&& key) override;
78
    bool ErasePrefix(Span<const std::byte> prefix) override;
79
80
public:
81
0
    explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
82
    ~MockableBatch() = default;
83
84
0
    void Flush() override {}
85
0
    void Close() override {}
86
87
    std::unique_ptr<DatabaseCursor> GetNewCursor() override
88
0
    {
89
0
        return std::make_unique<MockableCursor>(m_records, m_pass);
90
0
    }
91
0
    std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
92
0
        return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
93
0
    }
94
0
    bool TxnBegin() override { return m_pass; }
95
0
    bool TxnCommit() override { return m_pass; }
96
0
    bool TxnAbort() override { return m_pass; }
97
0
    bool HasActiveTxn() override { return false; }
98
};
99
100
/** A WalletDatabase whose contents and return values can be modified as needed for testing
101
 **/
102
class MockableDatabase : public WalletDatabase
103
{
104
public:
105
    MockableData m_records;
106
    bool m_pass{true};
107
108
0
    MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
109
0
    ~MockableDatabase() = default;
110
111
0
    void Open() override {}
112
0
    void AddRef() override {}
113
0
    void RemoveRef() override {}
114
115
0
    bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
116
0
    bool Backup(const std::string& strDest) const override { return m_pass; }
117
0
    void Flush() override {}
118
0
    void Close() override {}
119
0
    bool PeriodicFlush() override { return m_pass; }
120
0
    void IncrementUpdateCounter() override {}
121
0
    void ReloadDbEnv() override {}
122
123
0
    std::string Filename() override { return "mockable"; }
124
0
    std::string Format() override { return "mock"; }
125
0
    std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
126
};
127
128
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
129
MockableDatabase& GetMockableDatabase(CWallet& wallet);
130
131
ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success);
132
} // namespace wallet
133
134
#endif // BITCOIN_WALLET_TEST_UTIL_H