Coverage Report

Created: 2026-11-13 17:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/dbwrapper.h
Line
Count
Source
1
// Copyright (c) 2012-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_DBWRAPPER_H
6
#define BITCOIN_DBWRAPPER_H
7
8
#include <attributes.h>
9
#include <serialize.h>
10
#include <span.h>
11
#include <streams.h>
12
#include <util/byte_units.h>
13
#include <util/check.h>
14
#include <util/expected.h>
15
#include <util/fs.h>
16
#include <util/not_null.h>
17
#include <util/obfuscation.h>
18
19
#include <cstddef>
20
#include <cstdint>
21
#include <exception>
22
#include <memory>
23
#include <optional>
24
#include <span>
25
#include <stdexcept>
26
#include <string>
27
28
namespace leveldb {
29
class Env;
30
} // namespace leveldb
31
32
inline constexpr size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
33
inline constexpr size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
34
inline constexpr size_t DBWRAPPER_MAX_FILE_SIZE{32_MiB};
35
36
//! User-controlled performance and debug options.
37
struct DBOptions {
38
    //! Compact database on startup.
39
    bool force_compact = false;
40
};
41
42
//! Application-specific storage settings.
43
struct DBParams {
44
    //! Location in the filesystem where leveldb data will be stored.
45
    fs::path path;
46
    //! Configures various leveldb cache settings.
47
    uint64_t cache_bytes;
48
    //! If true, use leveldb's memory environment.
49
    bool memory_only = false;
50
    //! If true, remove all existing data.
51
    bool wipe_data = false;
52
    //! If true, store data obfuscated via simple XOR. If false, XOR with a
53
    //! zero'd byte array.
54
    bool obfuscate = false;
55
    //! If true, build a LevelDB bloom filter to accelerate point lookups.
56
    bool bloom_filter = true;
57
    //! Passed-through options.
58
    DBOptions options{};
59
    //! If non-null, use this as the leveldb::Env instead of the default.
60
    //! Caller retains ownership.
61
    leveldb::Env* testing_env = nullptr;
62
    //! Maximum LevelDB SST file size. Larger values reduce the frequency
63
    //! of compactions but increase their duration.
64
    size_t max_file_size = DBWRAPPER_MAX_FILE_SIZE;
65
};
66
67
class dbwrapper_error : public std::runtime_error
68
{
69
public:
70
14
    explicit dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
71
};
72
73
class CDBWrapper;
74
75
/** These should be considered an implementation detail of the specific database.
76
 */
77
namespace dbwrapper_private {
78
79
/** Work around circular dependency, as well as for testing in dbwrapper_tests.
80
 * Database obfuscation should be considered an implementation detail of the
81
 * specific database.
82
 */
83
const Obfuscation& GetObfuscation(const CDBWrapper&);
84
}; // namespace dbwrapper_private
85
86
bool DestroyDB(const std::string& path_str);
87
88
/** Batch of changes queued to be written to a CDBWrapper */
89
class CDBBatch
90
{
91
    friend class CDBWrapper;
92
93
private:
94
    const CDBWrapper &parent;
95
96
    struct WriteBatchImpl;
97
    const std::unique_ptr<WriteBatchImpl> m_impl_batch;
98
99
    DataStream m_key_scratch{};
100
    DataStream m_value_scratch{};
101
102
    void WriteImpl(std::span<const std::byte> key, DataStream& value);
103
    void EraseImpl(std::span<const std::byte> key);
104
105
public:
106
    /**
107
     * @param[in] _parent   CDBWrapper that this batch is to be submitted to
108
     */
109
    explicit CDBBatch(const CDBWrapper& _parent);
110
    ~CDBBatch();
111
    void Clear();
112
113
    template <typename K, typename V>
114
    void Write(const K& key, const V& value)
115
453k
    {
116
453k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
453k
        m_key_scratch << key;
118
453k
        m_value_scratch << value;
119
453k
        WriteImpl(m_key_scratch, m_value_scratch);
120
453k
    }
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256 const&)
Line
Count
Source
115
8
    {
116
8
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
8
        m_key_scratch << key;
118
8
        m_value_scratch << value;
119
8
        WriteImpl(m_key_scratch, m_value_scratch);
120
8
    }
void CDBBatch::Write<unsigned char, unsigned int>(unsigned char const&, unsigned int const&)
Line
Count
Source
115
258
    {
116
258
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
258
        m_key_scratch << key;
118
258
        m_value_scratch << value;
119
258
        WriteImpl(m_key_scratch, m_value_scratch);
120
258
    }
void CDBBatch::Write<unsigned char, bool>(unsigned char const&, bool const&)
Line
Count
Source
115
2
    {
116
2
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
2
        m_key_scratch << key;
118
2
        m_value_scratch << value;
119
2
        WriteImpl(m_key_scratch, m_value_scratch);
120
2
    }
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool const&)
Line
Count
Source
115
2
    {
116
2
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
2
        m_key_scratch << key;
118
2
        m_value_scratch << value;
119
2
        WriteImpl(m_key_scratch, m_value_scratch);
120
2
    }
void CDBBatch::Write<unsigned char, uint256>(unsigned char const&, uint256 const&)
Line
Count
Source
115
4.07k
    {
116
4.07k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.07k
        m_key_scratch << key;
118
4.07k
        m_value_scratch << value;
119
4.07k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.07k
    }
void CDBBatch::Write<unsigned char, unsigned char>(unsigned char const&, unsigned char const&)
Line
Count
Source
115
19
    {
116
19
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
19
        m_key_scratch << key;
118
19
        m_value_scratch << value;
119
19
        WriteImpl(m_key_scratch, m_value_scratch);
120
19
    }
void CDBBatch::Write<dbwrapper_tests::StringContentsSerializer, unsigned int>(dbwrapper_tests::StringContentsSerializer const&, unsigned int const&)
Line
Count
Source
115
100
    {
116
100
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
100
        m_key_scratch << key;
118
100
        m_value_scratch << value;
119
100
        WriteImpl(m_key_scratch, m_value_scratch);
120
100
    }
void CDBBatch::Write<txindex::DBKey, std::array<std::byte, 0ul>>(txindex::DBKey const&, std::array<std::byte, 0ul> const&)
Line
Count
Source
115
4.45k
    {
116
4.45k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.45k
        m_key_scratch << key;
118
4.45k
        m_value_scratch << value;
119
4.45k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.45k
    }
void CDBBatch::Write<std::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos const&)
Line
Count
Source
115
2
    {
116
2
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
2
        m_key_scratch << key;
118
2
        m_value_scratch << value;
119
2
        WriteImpl(m_key_scratch, m_value_scratch);
120
2
    }
void CDBBatch::Write<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator const&)
Line
Count
Source
115
220
    {
116
220
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
220
        m_key_scratch << key;
118
220
        m_value_scratch << value;
119
220
        WriteImpl(m_key_scratch, m_value_scratch);
120
220
    }
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation const&)
Line
Count
Source
115
580
    {
116
580
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
580
        m_key_scratch << key;
118
580
        m_value_scratch << value;
119
580
        WriteImpl(m_key_scratch, m_value_scratch);
120
580
    }
void CDBBatch::Write<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos const&)
Line
Count
Source
115
180
    {
116
180
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
180
        m_key_scratch << key;
118
180
        m_value_scratch << value;
119
180
        WriteImpl(m_key_scratch, m_value_scratch);
120
180
    }
blockfilterindex.cpp:void CDBBatch::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&)
Line
Count
Source
115
7.99k
    {
116
7.99k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
7.99k
        m_key_scratch << key;
118
7.99k
        m_value_scratch << value;
119
7.99k
        WriteImpl(m_key_scratch, m_value_scratch);
120
7.99k
    }
blockfilterindex.cpp:void CDBBatch::Write<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal const&)
Line
Count
Source
115
112
    {
116
112
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
112
        m_key_scratch << key;
118
112
        m_value_scratch << value;
119
112
        WriteImpl(m_key_scratch, m_value_scratch);
120
112
    }
coinstatsindex.cpp:void CDBBatch::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&)
Line
Count
Source
115
4.08k
    {
116
4.08k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.08k
        m_key_scratch << key;
118
4.08k
        m_value_scratch << value;
119
4.08k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.08k
    }
coinstatsindex.cpp:void CDBBatch::Write<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal const&)
Line
Count
Source
115
121
    {
116
121
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
121
        m_key_scratch << key;
118
121
        m_value_scratch << value;
119
121
        WriteImpl(m_key_scratch, m_value_scratch);
120
121
    }
void CDBBatch::Write<unsigned char, MuHash3072>(unsigned char const&, MuHash3072 const&)
Line
Count
Source
115
119
    {
116
119
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
119
        m_key_scratch << key;
118
119
        m_value_scratch << value;
119
119
        WriteImpl(m_key_scratch, m_value_scratch);
120
119
    }
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<unsigned long, unsigned long>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::pair<unsigned long, unsigned long> const&)
Line
Count
Source
115
24
    {
116
24
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
24
        m_key_scratch << key;
118
24
        m_value_scratch << value;
119
24
        WriteImpl(m_key_scratch, m_value_scratch);
120
24
    }
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockLocator>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, CBlockLocator const&)
Line
Count
Source
115
59
    {
116
59
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
59
        m_key_scratch << key;
118
59
        m_value_scratch << value;
119
59
        WriteImpl(m_key_scratch, m_value_scratch);
120
59
    }
void CDBBatch::Write<txindex::BlockHashKey, unsigned int>(txindex::BlockHashKey const&, unsigned int const&)
Line
Count
Source
115
4.30k
    {
116
4.30k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.30k
        m_key_scratch << key;
118
4.30k
        m_value_scratch << value;
119
4.30k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.30k
    }
void CDBBatch::Write<txindex::BlockSeqKey, uint256>(txindex::BlockSeqKey const&, uint256 const&)
Line
Count
Source
115
4.30k
    {
116
4.30k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.30k
        m_key_scratch << key;
118
4.30k
        m_value_scratch << value;
119
4.30k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.30k
    }
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int const&)
Line
Count
Source
115
4.30k
    {
116
4.30k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.30k
        m_key_scratch << key;
118
4.30k
        m_value_scratch << value;
119
4.30k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.30k
    }
void CDBBatch::Write<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long> const&)
Line
Count
Source
115
10
    {
116
10
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
10
        m_key_scratch << key;
118
10
        m_value_scratch << value;
119
10
        WriteImpl(m_key_scratch, m_value_scratch);
120
10
    }
void CDBBatch::Write<DBKey, std::span<std::byte const, 18446744073709551615ul>>(DBKey const&, std::span<std::byte const, 18446744073709551615ul> const&)
Line
Count
Source
115
38
    {
116
38
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
38
        m_key_scratch << key;
118
38
        m_value_scratch << value;
119
38
        WriteImpl(m_key_scratch, m_value_scratch);
120
38
    }
void CDBBatch::Write<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo const&)
Line
Count
Source
115
1.73k
    {
116
1.73k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
1.73k
        m_key_scratch << key;
118
1.73k
        m_value_scratch << value;
119
1.73k
        WriteImpl(m_key_scratch, m_value_scratch);
120
1.73k
    }
void CDBBatch::Write<unsigned char, int>(unsigned char const&, int const&)
Line
Count
Source
115
3.70k
    {
116
3.70k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
3.70k
        m_key_scratch << key;
118
3.70k
        m_value_scratch << value;
119
3.70k
        WriteImpl(m_key_scratch, m_value_scratch);
120
3.70k
    }
void CDBBatch::Write<std::pair<unsigned char, uint256>, CDiskBlockIndex>(std::pair<unsigned char, uint256> const&, CDiskBlockIndex const&)
Line
Count
Source
115
117k
    {
116
117k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
117k
        m_key_scratch << key;
118
117k
        m_value_scratch << value;
119
117k
        WriteImpl(m_key_scratch, m_value_scratch);
120
117k
    }
void CDBBatch::Write<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char const&)
Line
Count
Source
115
8
    {
116
8
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
8
        m_key_scratch << key;
118
8
        m_value_scratch << value;
119
8
        WriteImpl(m_key_scratch, m_value_scratch);
120
8
    }
void CDBBatch::Write<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>> const&)
Line
Count
Source
115
4.03k
    {
116
4.03k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
4.03k
        m_key_scratch << key;
118
4.03k
        m_value_scratch << value;
119
4.03k
        WriteImpl(m_key_scratch, m_value_scratch);
120
4.03k
    }
txdb.cpp:void CDBBatch::Write<(anonymous namespace)::CoinEntry, Coin>((anonymous namespace)::CoinEntry const&, Coin const&)
Line
Count
Source
115
290k
    {
116
290k
        ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch};
117
290k
        m_key_scratch << key;
118
290k
        m_value_scratch << value;
119
290k
        WriteImpl(m_key_scratch, m_value_scratch);
120
290k
    }
121
122
    template <typename K>
123
    void Erase(const K& key)
124
41.3k
    {
125
41.3k
        ScopedDataStreamUsage scoped_key{m_key_scratch};
126
41.3k
        m_key_scratch << key;
127
41.3k
        EraseImpl(m_key_scratch);
128
41.3k
    }
void CDBBatch::Erase<unsigned char>(unsigned char const&)
Line
Count
Source
124
8.08k
    {
125
8.08k
        ScopedDataStreamUsage scoped_key{m_key_scratch};
126
8.08k
        m_key_scratch << key;
127
8.08k
        EraseImpl(m_key_scratch);
128
8.08k
    }
void CDBBatch::Erase<txindex::DBKey>(txindex::DBKey const&)
Line
Count
Source
124
2
    {
125
2
        ScopedDataStreamUsage scoped_key{m_key_scratch};
126
2
        m_key_scratch << key;
127
2
        EraseImpl(m_key_scratch);
128
2
    }
void CDBBatch::Erase<DBKey>(DBKey const&)
Line
Count
Source
124
6
    {
125
6
        ScopedDataStreamUsage scoped_key{m_key_scratch};
126
6
        m_key_scratch << key;
127
6
        EraseImpl(m_key_scratch);
128
6
    }
txdb.cpp:void CDBBatch::Erase<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry const&)
Line
Count
Source
124
33.2k
    {
125
33.2k
        ScopedDataStreamUsage scoped_key{m_key_scratch};
126
33.2k
        m_key_scratch << key;
127
33.2k
        EraseImpl(m_key_scratch);
128
33.2k
    }
129
130
    size_t ApproximateSize() const;
131
};
132
133
class CDBIterator
134
{
135
public:
136
    struct IteratorImpl;
137
138
private:
139
    const CDBWrapper &parent;
140
    const std::unique_ptr<IteratorImpl> m_impl_iter;
141
    DataStream m_scratch{};
142
143
    void SeekImpl(std::span<const std::byte> key);
144
    std::span<const std::byte> GetKeyImpl() const;
145
    std::span<const std::byte> GetValueImpl() const;
146
147
public:
148
149
    /**
150
     * @param[in] _parent          Parent CDBWrapper instance.
151
     * @param[in] _piter           The original leveldb iterator.
152
     */
153
    CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter);
154
    ~CDBIterator();
155
156
    bool Valid() const;
157
158
    void SeekToFirst();
159
160
4.98k
    template<typename K> void Seek(const K& key) {
161
4.98k
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
4.98k
        m_scratch << key;
163
4.98k
        SeekImpl(m_scratch);
164
4.98k
    }
void CDBIterator::Seek<unsigned char>(unsigned char const&)
Line
Count
Source
160
1.27k
    template<typename K> void Seek(const K& key) {
161
1.27k
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
1.27k
        m_scratch << key;
163
1.27k
        SeekImpl(m_scratch);
164
1.27k
    }
void CDBIterator::Seek<dbwrapper_tests::StringContentsSerializer>(dbwrapper_tests::StringContentsSerializer const&)
Line
Count
Source
160
2
    template<typename K> void Seek(const K& key) {
161
2
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
2
        m_scratch << key;
163
2
        SeekImpl(m_scratch);
164
2
    }
void CDBIterator::Seek<txindex::DBKey>(txindex::DBKey const&)
Line
Count
Source
160
271
    template<typename K> void Seek(const K& key) {
161
271
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
271
        m_scratch << key;
163
271
        SeekImpl(m_scratch);
164
271
    }
void CDBIterator::Seek<index_util::DBHeightKey>(index_util::DBHeightKey const&)
Line
Count
Source
160
681
    template<typename K> void Seek(const K& key) {
161
681
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
681
        m_scratch << key;
163
681
        SeekImpl(m_scratch);
164
681
    }
void CDBIterator::Seek<std::pair<unsigned char, unsigned long>>(std::pair<unsigned char, unsigned long> const&)
Line
Count
Source
160
36
    template<typename K> void Seek(const K& key) {
161
36
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
36
        m_scratch << key;
163
36
        SeekImpl(m_scratch);
164
36
    }
void CDBIterator::Seek<std::pair<unsigned char, uint256>>(std::pair<unsigned char, uint256> const&)
Line
Count
Source
160
2.71k
    template<typename K> void Seek(const K& key) {
161
2.71k
        ScopedDataStreamUsage scoped_scratch{m_scratch};
162
2.71k
        m_scratch << key;
163
2.71k
        SeekImpl(m_scratch);
164
2.71k
    }
165
166
    void Next();
167
168
437k
    template<typename K> bool GetKey(K& key) {
169
437k
        try {
170
437k
            SpanReader ssKey{GetKeyImpl()};
171
437k
            ssKey >> key;
172
437k
        } catch (const std::exception&) {
173
842
            return false;
174
842
        }
175
437k
        return true;
176
437k
    }
bool CDBIterator::GetKey<unsigned short>(unsigned short&)
Line
Count
Source
168
2
    template<typename K> bool GetKey(K& key) {
169
2
        try {
170
2
            SpanReader ssKey{GetKeyImpl()};
171
2
            ssKey >> key;
172
2
        } catch (const std::exception&) {
173
2
            return false;
174
2
        }
175
0
        return true;
176
2
    }
bool CDBIterator::GetKey<unsigned char>(unsigned char&)
Line
Count
Source
168
390
    template<typename K> bool GetKey(K& key) {
169
390
        try {
170
390
            SpanReader ssKey{GetKeyImpl()};
171
390
            ssKey >> key;
172
390
        } catch (const std::exception&) {
173
0
            return false;
174
0
        }
175
390
        return true;
176
390
    }
bool CDBIterator::GetKey<dbwrapper_tests::StringContentsSerializer>(dbwrapper_tests::StringContentsSerializer&)
Line
Count
Source
168
150
    template<typename K> bool GetKey(K& key) {
169
150
        try {
170
150
            SpanReader ssKey{GetKeyImpl()};
171
150
            ssKey >> key;
172
150
        } catch (const std::exception&) {
173
0
            return false;
174
0
        }
175
150
        return true;
176
150
    }
bool CDBIterator::GetKey<txindex::DBKey>(txindex::DBKey&)
Line
Count
Source
168
335
    template<typename K> bool GetKey(K& key) {
169
335
        try {
170
335
            SpanReader ssKey{GetKeyImpl()};
171
335
            ssKey >> key;
172
335
        } catch (const std::exception&) {
173
0
            return false;
174
0
        }
175
335
        return true;
176
335
    }
bool CDBIterator::GetKey<index_util::DBHeightKey>(index_util::DBHeightKey&)
Line
Count
Source
168
3.09k
    template<typename K> bool GetKey(K& key) {
169
3.09k
        try {
170
3.09k
            SpanReader ssKey{GetKeyImpl()};
171
3.09k
            ssKey >> key;
172
3.09k
        } catch (const std::exception&) {
173
0
            return false;
174
0
        }
175
3.09k
        return true;
176
3.09k
    }
bool CDBIterator::GetKey<DBKey>(DBKey&)
Line
Count
Source
168
27
    template<typename K> bool GetKey(K& key) {
169
27
        try {
170
27
            SpanReader ssKey{GetKeyImpl()};
171
27
            ssKey >> key;
172
27
        } catch (const std::exception&) {
173
0
            return false;
174
0
        }
175
27
        return true;
176
27
    }
bool CDBIterator::GetKey<std::pair<unsigned char, uint256>>(std::pair<unsigned char, uint256>&)
Line
Count
Source
168
155k
    template<typename K> bool GetKey(K& key) {
169
155k
        try {
170
155k
            SpanReader ssKey{GetKeyImpl()};
171
155k
            ssKey >> key;
172
155k
        } catch (const std::exception&) {
173
840
            return false;
174
840
        }
175
154k
        return true;
176
155k
    }
txdb.cpp:bool CDBIterator::GetKey<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry&)
Line
Count
Source
168
278k
    template<typename K> bool GetKey(K& key) {
169
278k
        try {
170
278k
            SpanReader ssKey{GetKeyImpl()};
171
278k
            ssKey >> key;
172
278k
        } catch (const std::exception&) {
173
0
            return false;
174
0
        }
175
278k
        return true;
176
278k
    }
177
178
436k
    template<typename V> bool GetValue(V& value) {
179
436k
        try {
180
436k
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
436k
            m_scratch.write(GetValueImpl());
182
436k
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
436k
            m_scratch >> value;
184
436k
        } catch (const std::exception&) {
185
2
            return false;
186
2
        }
187
436k
        return true;
188
436k
    }
bool CDBIterator::GetValue<std::pair<uint256, unsigned char>>(std::pair<uint256, unsigned char>&)
Line
Count
Source
178
2
    template<typename V> bool GetValue(V& value) {
179
2
        try {
180
2
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
2
            m_scratch.write(GetValueImpl());
182
2
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
2
            m_scratch >> value;
184
2
        } catch (const std::exception&) {
185
2
            return false;
186
2
        }
187
0
        return true;
188
2
    }
bool CDBIterator::GetValue<uint256>(uint256&)
Line
Count
Source
178
8
    template<typename V> bool GetValue(V& value) {
179
8
        try {
180
8
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
8
            m_scratch.write(GetValueImpl());
182
8
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
8
            m_scratch >> value;
184
8
        } catch (const std::exception&) {
185
0
            return false;
186
0
        }
187
8
        return true;
188
8
    }
bool CDBIterator::GetValue<unsigned int>(unsigned int&)
Line
Count
Source
178
342
    template<typename V> bool GetValue(V& value) {
179
342
        try {
180
342
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
342
            m_scratch.write(GetValueImpl());
182
342
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
342
            m_scratch >> value;
184
342
        } catch (const std::exception&) {
185
0
            return false;
186
0
        }
187
342
        return true;
188
342
    }
blockfilterindex.cpp:bool CDBIterator::GetValue<std::pair<uint256, (anonymous namespace)::DBVal>>(std::pair<uint256, (anonymous namespace)::DBVal>&)
Line
Count
Source
178
2.97k
    template<typename V> bool GetValue(V& value) {
179
2.97k
        try {
180
2.97k
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
2.97k
            m_scratch.write(GetValueImpl());
182
2.97k
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
2.97k
            m_scratch >> value;
184
2.97k
        } catch (const std::exception&) {
185
0
            return false;
186
0
        }
187
2.97k
        return true;
188
2.97k
    }
coinstatsindex.cpp:bool CDBIterator::GetValue<std::pair<uint256, (anonymous namespace)::DBVal>>(std::pair<uint256, (anonymous namespace)::DBVal>&)
Line
Count
Source
178
121
    template<typename V> bool GetValue(V& value) {
179
121
        try {
180
121
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
121
            m_scratch.write(GetValueImpl());
182
121
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
121
            m_scratch >> value;
184
121
        } catch (const std::exception&) {
185
0
            return false;
186
0
        }
187
121
        return true;
188
121
    }
bool CDBIterator::GetValue<CDiskBlockIndex>(CDiskBlockIndex&)
Line
Count
Source
178
154k
    template<typename V> bool GetValue(V& value) {
179
154k
        try {
180
154k
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
154k
            m_scratch.write(GetValueImpl());
182
154k
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
154k
            m_scratch >> value;
184
154k
        } catch (const std::exception&) {
185
0
            return false;
186
0
        }
187
154k
        return true;
188
154k
    }
bool CDBIterator::GetValue<Coin>(Coin&)
Line
Count
Source
178
278k
    template<typename V> bool GetValue(V& value) {
179
278k
        try {
180
278k
            ScopedDataStreamUsage scoped_scratch{m_scratch};
181
278k
            m_scratch.write(GetValueImpl());
182
278k
            dbwrapper_private::GetObfuscation(parent)(m_scratch);
183
278k
            m_scratch >> value;
184
278k
        } catch (const std::exception&) {
185
0
            return false;
186
0
        }
187
278k
        return true;
188
278k
    }
189
};
190
191
struct LevelDBContext;
192
193
class CDBWrapper
194
{
195
    friend const Obfuscation& dbwrapper_private::GetObfuscation(const CDBWrapper&);
196
private:
197
    //! holds all leveldb-specific fields of this class
198
    util::NotNullUniquePtr<LevelDBContext> m_db_context;
199
200
    //! the name of this database
201
    std::string m_name;
202
203
    //! optional XOR-obfuscation of the database
204
    Obfuscation m_obfuscation;
205
206
    //! obfuscation key storage key, null-prefixed to avoid collisions
207
    inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
208
209
    std::optional<std::string> ReadImpl(std::span<const std::byte> key) const;
210
    bool ExistsImpl(std::span<const std::byte> key) const;
211
    size_t EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const;
212
12.8M
    auto& DBContext() const LIFETIMEBOUND { return *m_db_context; }
213
214
public:
215
    CDBWrapper(const DBParams& params);
216
    ~CDBWrapper();
217
218
    CDBWrapper(const CDBWrapper&) = delete;
219
    CDBWrapper& operator=(const CDBWrapper&) = delete;
220
221
    struct ReadFailure {
222
        enum class Code {
223
            DeserializationError,   //!< Key exists but value could not be deserialized.
224
            DatabaseError,          //!< Unexpected internal DB error.
225
        };
226
227
        Code status;
228
        std::string err_msg;
229
    };
230
231
    using ReadStatus = util::Expected<bool, ReadFailure>;
232
233
    /**
234
     * Read and deserialize a value from the database, with explicit error discrimination.
235
     *
236
     * Unlike Read(), this method distinguishes between a missing key, a deserialization
237
     * failure (DeserializationError), and an internal DB error (DatabaseError),
238
     * enabling callers to treat data corruption differently from an absent entry.
239
     *
240
     * @note Callers are expected to provide well-formed keys; key serialization
241
     *       is the only operation that may throw.
242
     *
243
     * @param[in]  key    The key to look up.
244
     * @param[out] value  Populated with the deserialized value when the returned
245
     *                    Expected holds true; indeterminate otherwise.
246
     * @return On success, true if the key was found (value populated) or false if
247
     *         the key was absent. On failure, a ReadFailure describing the error.
248
     */
249
    template <typename K, typename V>
250
    [[nodiscard]] ReadStatus TryRead(const K& key, V& value) const
251
6.33M
    {
252
6.33M
        DataStream ssKey{};
253
6.33M
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
6.33M
        ssKey << key;
257
258
6.33M
        std::optional<std::string> strValue;
259
6.33M
        try {
260
6.33M
            strValue = ReadImpl(ssKey);
261
6.33M
            if (!strValue) {
262
6.18M
                return false; // not found
263
6.18M
            }
264
6.33M
        } catch (const std::exception& e) {
265
3
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
3
        }
267
268
150k
        try {
269
150k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
150k
            m_obfuscation(ssValue);
271
150k
            SpanReader{ssValue} >> value;
272
150k
        } catch (const std::exception& e) {
273
4
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
4
        }
275
276
150k
        return true;
277
150k
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256&) const
Line
Count
Source
251
8
    {
252
8
        DataStream ssKey{};
253
8
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
8
        ssKey << key;
257
258
8
        std::optional<std::string> strValue;
259
8
        try {
260
8
            strValue = ReadImpl(ssKey);
261
8
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
8
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
8
        try {
269
8
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
8
            m_obfuscation(ssValue);
271
8
            SpanReader{ssValue} >> value;
272
8
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
8
        return true;
277
8
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, unsigned int>(unsigned char const&, unsigned int&) const
Line
Count
Source
251
2
    {
252
2
        DataStream ssKey{};
253
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
2
        ssKey << key;
257
258
2
        std::optional<std::string> strValue;
259
2
        try {
260
2
            strValue = ReadImpl(ssKey);
261
2
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
2
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
2
        try {
269
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
2
            m_obfuscation(ssValue);
271
2
            SpanReader{ssValue} >> value;
272
2
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
2
        return true;
277
2
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, bool>(unsigned char const&, bool&) const
Line
Count
Source
251
2
    {
252
2
        DataStream ssKey{};
253
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
2
        ssKey << key;
257
258
2
        std::optional<std::string> strValue;
259
2
        try {
260
2
            strValue = ReadImpl(ssKey);
261
2
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
2
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
2
        try {
269
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
2
            m_obfuscation(ssValue);
271
2
            SpanReader{ssValue} >> value;
272
2
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
2
        return true;
277
2
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool&) const
Line
Count
Source
251
2
    {
252
2
        DataStream ssKey{};
253
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
2
        ssKey << key;
257
258
2
        std::optional<std::string> strValue;
259
2
        try {
260
2
            strValue = ReadImpl(ssKey);
261
2
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
2
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
2
        try {
269
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
2
            m_obfuscation(ssValue);
271
2
            SpanReader{ssValue} >> value;
272
2
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
2
        return true;
277
2
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, uint256>(unsigned char const&, uint256&) const
Line
Count
Source
251
8.43k
    {
252
8.43k
        DataStream ssKey{};
253
8.43k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
8.43k
        ssKey << key;
257
258
8.43k
        std::optional<std::string> strValue;
259
8.43k
        try {
260
8.43k
            strValue = ReadImpl(ssKey);
261
8.43k
            if (!strValue) {
262
1.94k
                return false; // not found
263
1.94k
            }
264
8.43k
        } catch (const std::exception& e) {
265
2
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
2
        }
267
268
6.48k
        try {
269
6.48k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
6.48k
            m_obfuscation(ssValue);
271
6.48k
            SpanReader{ssValue} >> value;
272
6.48k
        } catch (const std::exception& e) {
273
4
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
4
        }
275
276
6.48k
        return true;
277
6.48k
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<unsigned long, unsigned long>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::pair<unsigned long, unsigned long>&) const
Line
Count
Source
251
51
    {
252
51
        DataStream ssKey{};
253
51
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
51
        ssKey << key;
257
258
51
        std::optional<std::string> strValue;
259
51
        try {
260
51
            strValue = ReadImpl(ssKey);
261
51
            if (!strValue) {
262
24
                return false; // not found
263
24
            }
264
51
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
27
        try {
269
27
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
27
            m_obfuscation(ssValue);
271
27
            SpanReader{ssValue} >> value;
272
27
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
27
        return true;
277
27
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator&) const
Line
Count
Source
251
169
    {
252
169
        DataStream ssKey{};
253
169
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
169
        ssKey << key;
257
258
169
        std::optional<std::string> strValue;
259
169
        try {
260
169
            strValue = ReadImpl(ssKey);
261
169
            if (!strValue) {
262
89
                return false; // not found
263
89
            }
264
169
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
80
        try {
269
80
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
80
            m_obfuscation(ssValue);
271
80
            SpanReader{ssValue} >> value;
272
80
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
80
        return true;
277
80
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation&) const
Line
Count
Source
251
3.12k
    {
252
3.12k
        DataStream ssKey{};
253
3.12k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
3.12k
        ssKey << key;
257
258
3.12k
        std::optional<std::string> strValue;
259
3.12k
        try {
260
3.12k
            strValue = ReadImpl(ssKey);
261
3.12k
            if (!strValue) {
262
2.17k
                return false; // not found
263
2.17k
            }
264
3.12k
        } catch (const std::exception& e) {
265
1
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
1
        }
267
268
952
        try {
269
952
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
952
            m_obfuscation(ssValue);
271
952
            SpanReader{ssValue} >> value;
272
952
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
952
        return true;
277
952
    }
blockfilterindex.cpp:util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const
Line
Count
Source
251
33
    {
252
33
        DataStream ssKey{};
253
33
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
33
        ssKey << key;
257
258
33
        std::optional<std::string> strValue;
259
33
        try {
260
33
            strValue = ReadImpl(ssKey);
261
33
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
33
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
33
        try {
269
33
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
33
            m_obfuscation(ssValue);
271
33
            SpanReader{ssValue} >> value;
272
33
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
33
        return true;
277
33
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos&) const
Line
Count
Source
251
54
    {
252
54
        DataStream ssKey{};
253
54
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
54
        ssKey << key;
257
258
54
        std::optional<std::string> strValue;
259
54
        try {
260
54
            strValue = ReadImpl(ssKey);
261
54
            if (!strValue) {
262
26
                return false; // not found
263
26
            }
264
54
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
28
        try {
269
28
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
28
            m_obfuscation(ssValue);
271
28
            SpanReader{ssValue} >> value;
272
28
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
28
        return true;
277
28
    }
blockfilterindex.cpp:util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const
Line
Count
Source
251
1.33k
    {
252
1.33k
        DataStream ssKey{};
253
1.33k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
1.33k
        ssKey << key;
257
258
1.33k
        std::optional<std::string> strValue;
259
1.33k
        try {
260
1.33k
            strValue = ReadImpl(ssKey);
261
1.33k
            if (!strValue) {
262
305
                return false; // not found
263
305
            }
264
1.33k
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
1.02k
        try {
269
1.02k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
1.02k
            m_obfuscation(ssValue);
271
1.02k
            SpanReader{ssValue} >> value;
272
1.02k
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
1.02k
        return true;
277
1.02k
    }
coinstatsindex.cpp:util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const
Line
Count
Source
251
2
    {
252
2
        DataStream ssKey{};
253
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
2
        ssKey << key;
257
258
2
        std::optional<std::string> strValue;
259
2
        try {
260
2
            strValue = ReadImpl(ssKey);
261
2
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
2
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
2
        try {
269
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
2
            m_obfuscation(ssValue);
271
2
            SpanReader{ssValue} >> value;
272
2
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
2
        return true;
277
2
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, MuHash3072>(unsigned char const&, MuHash3072&) const
Line
Count
Source
251
53
    {
252
53
        DataStream ssKey{};
253
53
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
53
        ssKey << key;
257
258
53
        std::optional<std::string> strValue;
259
53
        try {
260
53
            strValue = ReadImpl(ssKey);
261
53
            if (!strValue) {
262
22
                return false; // not found
263
22
            }
264
53
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
31
        try {
269
31
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
31
            m_obfuscation(ssValue);
271
31
            SpanReader{ssValue} >> value;
272
31
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
31
        return true;
277
31
    }
coinstatsindex.cpp:util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const
Line
Count
Source
251
222
    {
252
222
        DataStream ssKey{};
253
222
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
222
        ssKey << key;
257
258
222
        std::optional<std::string> strValue;
259
222
        try {
260
222
            strValue = ReadImpl(ssKey);
261
222
            if (!strValue) {
262
1
                return false; // not found
263
1
            }
264
222
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
221
        try {
269
221
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
221
            m_obfuscation(ssValue);
271
221
            SpanReader{ssValue} >> value;
272
221
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
221
        return true;
277
221
    }
Unexecuted instantiation: coinstatsindex.cpp:util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<index_util::DBHashKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHashKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockLocator>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, CBlockLocator&) const
Line
Count
Source
251
54
    {
252
54
        DataStream ssKey{};
253
54
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
54
        ssKey << key;
257
258
54
        std::optional<std::string> strValue;
259
54
        try {
260
54
            strValue = ReadImpl(ssKey);
261
54
            if (!strValue) {
262
28
                return false; // not found
263
28
            }
264
54
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
26
        try {
269
26
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
26
            m_obfuscation(ssValue);
271
26
            SpanReader{ssValue} >> value;
272
26
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
26
        return true;
277
26
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int&) const
Line
Count
Source
251
4.30k
    {
252
4.30k
        DataStream ssKey{};
253
4.30k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
4.30k
        ssKey << key;
257
258
4.30k
        std::optional<std::string> strValue;
259
4.30k
        try {
260
4.30k
            strValue = ReadImpl(ssKey);
261
4.30k
            if (!strValue) {
262
21
                return false; // not found
263
21
            }
264
4.30k
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
4.27k
        try {
269
4.27k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
4.27k
            m_obfuscation(ssValue);
271
4.27k
            SpanReader{ssValue} >> value;
272
4.27k
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
4.27k
        return true;
277
4.27k
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<txindex::BlockSeqKey, uint256>(txindex::BlockSeqKey const&, uint256&) const
Line
Count
Source
251
160
    {
252
160
        DataStream ssKey{};
253
160
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
160
        ssKey << key;
257
258
160
        std::optional<std::string> strValue;
259
160
        try {
260
160
            strValue = ReadImpl(ssKey);
261
160
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
160
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
160
        try {
269
160
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
160
            m_obfuscation(ssValue);
271
160
            SpanReader{ssValue} >> value;
272
160
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
160
        return true;
277
160
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos&) const
Line
Count
Source
251
3
    {
252
3
        DataStream ssKey{};
253
3
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
3
        ssKey << key;
257
258
3
        std::optional<std::string> strValue;
259
3
        try {
260
3
            strValue = ReadImpl(ssKey);
261
3
            if (!strValue) {
262
0
                return false; // not found
263
0
            }
264
3
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
3
        try {
269
3
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
3
            m_obfuscation(ssValue);
271
3
            SpanReader{ssValue} >> value;
272
3
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
3
        return true;
277
3
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long>&) const
Line
Count
Source
251
29
    {
252
29
        DataStream ssKey{};
253
29
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
29
        ssKey << key;
257
258
29
        std::optional<std::string> strValue;
259
29
        try {
260
29
            strValue = ReadImpl(ssKey);
261
29
            if (!strValue) {
262
10
                return false; // not found
263
10
            }
264
29
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
19
        try {
269
19
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
19
            m_obfuscation(ssValue);
271
19
            SpanReader{ssValue} >> value;
272
19
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
19
        return true;
277
19
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo&) const
Line
Count
Source
251
2.71k
    {
252
2.71k
        DataStream ssKey{};
253
2.71k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
2.71k
        ssKey << key;
257
258
2.71k
        std::optional<std::string> strValue;
259
2.71k
        try {
260
2.71k
            strValue = ReadImpl(ssKey);
261
2.71k
            if (!strValue) {
262
1.86k
                return false; // not found
263
1.86k
            }
264
2.71k
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
856
        try {
269
856
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
856
            m_obfuscation(ssValue);
271
856
            SpanReader{ssValue} >> value;
272
856
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
856
        return true;
277
856
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, int>(unsigned char const&, int&) const
Line
Count
Source
251
1.35k
    {
252
1.35k
        DataStream ssKey{};
253
1.35k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
1.35k
        ssKey << key;
257
258
1.35k
        std::optional<std::string> strValue;
259
1.35k
        try {
260
1.35k
            strValue = ReadImpl(ssKey);
261
1.35k
            if (!strValue) {
262
512
                return false; // not found
263
512
            }
264
1.35k
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
838
        try {
269
838
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
838
            m_obfuscation(ssValue);
271
838
            SpanReader{ssValue} >> value;
272
838
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
838
        return true;
277
838
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char&) const
Line
Count
Source
251
1.34k
    {
252
1.34k
        DataStream ssKey{};
253
1.34k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
1.34k
        ssKey << key;
257
258
1.34k
        std::optional<std::string> strValue;
259
1.34k
        try {
260
1.34k
            strValue = ReadImpl(ssKey);
261
1.34k
            if (!strValue) {
262
1.34k
                return false; // not found
263
1.34k
            }
264
1.34k
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
2
        try {
269
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
2
            m_obfuscation(ssValue);
271
2
            SpanReader{ssValue} >> value;
272
2
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
2
        return true;
277
2
    }
txdb.cpp:util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<(anonymous namespace)::CoinEntry, Coin>((anonymous namespace)::CoinEntry const&, Coin&) const
Line
Count
Source
251
6.30M
    {
252
6.30M
        DataStream ssKey{};
253
6.30M
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
6.30M
        ssKey << key;
257
258
6.30M
        std::optional<std::string> strValue;
259
6.30M
        try {
260
6.30M
            strValue = ReadImpl(ssKey);
261
6.30M
            if (!strValue) {
262
6.17M
                return false; // not found
263
6.17M
            }
264
6.30M
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
135k
        try {
269
135k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
135k
            m_obfuscation(ssValue);
271
135k
            SpanReader{ssValue} >> value;
272
135k
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
135k
        return true;
277
135k
    }
util::Expected<bool, CDBWrapper::ReadFailure> CDBWrapper::TryRead<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>>&) const
Line
Count
Source
251
1.73k
    {
252
1.73k
        DataStream ssKey{};
253
1.73k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
254
        // Key serialization is the only operation that may throw.
255
        // Callers are expected to provide well-formed keys.
256
1.73k
        ssKey << key;
257
258
1.73k
        std::optional<std::string> strValue;
259
1.73k
        try {
260
1.73k
            strValue = ReadImpl(ssKey);
261
1.73k
            if (!strValue) {
262
1.73k
                return false; // not found
263
1.73k
            }
264
1.73k
        } catch (const std::exception& e) {
265
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DatabaseError, e.what()});
266
0
        }
267
268
0
        try {
269
0
            std::span ssValue{MakeWritableByteSpan(*strValue)};
270
0
            m_obfuscation(ssValue);
271
0
            SpanReader{ssValue} >> value;
272
0
        } catch (const std::exception& e) {
273
0
            return util::Unexpected(ReadFailure{ReadFailure::Code::DeserializationError, e.what()});
274
0
        }
275
276
0
        return true;
277
0
    }
278
279
    /**
280
     * Wrapper around TryRead() that preserves the original Read() semantics:
281
     * returns true on success, false if the key is absent or deserialization
282
     * fails, and throws dbwrapper_error on an internal DB error.
283
     *
284
     * Prefer TryRead() when the caller needs to distinguish between a missing
285
     * key and a corrupt value.
286
     */
287
    template <typename K, typename V>
288
    bool Read(const K& key, V& value) const
289
25.1k
    {
290
25.1k
        const ReadStatus res = TryRead(key,value);
291
25.1k
        if (res.has_value()) return res.value();
292
4
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
2
            case ReadFailure::Code::DeserializationError: return false;
294
2
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
4
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
4
    }
bool CDBWrapper::Read<unsigned char, uint256>(unsigned char const&, uint256&) const
Line
Count
Source
289
8.42k
    {
290
8.42k
        const ReadStatus res = TryRead(key,value);
291
8.42k
        if (res.has_value()) return res.value();
292
3
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
2
            case ReadFailure::Code::DeserializationError: return false;
294
1
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
3
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
3
    }
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256&) const
Line
Count
Source
289
8
    {
290
8
        const ReadStatus res = TryRead(key,value);
291
8
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, unsigned int>(unsigned char const&, unsigned int&) const
Line
Count
Source
289
2
    {
290
2
        const ReadStatus res = TryRead(key,value);
291
2
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, bool>(unsigned char const&, bool&) const
Line
Count
Source
289
2
    {
290
2
        const ReadStatus res = TryRead(key,value);
291
2
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool&) const
Line
Count
Source
289
2
    {
290
2
        const ReadStatus res = TryRead(key,value);
291
2
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<unsigned long, unsigned long>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::pair<unsigned long, unsigned long>&) const
Line
Count
Source
289
51
    {
290
51
        const ReadStatus res = TryRead(key,value);
291
51
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator&) const
Line
Count
Source
289
169
    {
290
169
        const ReadStatus res = TryRead(key,value);
291
169
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation&) const
Line
Count
Source
289
3.12k
    {
290
3.12k
        const ReadStatus res = TryRead(key,value);
291
3.12k
        if (res.has_value()) return res.value();
292
1
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
1
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
1
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
1
    }
blockfilterindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const
Line
Count
Source
289
33
    {
290
33
        const ReadStatus res = TryRead(key,value);
291
33
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos&) const
Line
Count
Source
289
54
    {
290
54
        const ReadStatus res = TryRead(key,value);
291
54
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
blockfilterindex.cpp:bool CDBWrapper::Read<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const
Line
Count
Source
289
1.33k
    {
290
1.33k
        const ReadStatus res = TryRead(key,value);
291
1.33k
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const
Line
Count
Source
289
2
    {
290
2
        const ReadStatus res = TryRead(key,value);
291
2
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, MuHash3072>(unsigned char const&, MuHash3072&) const
Line
Count
Source
289
53
    {
290
53
        const ReadStatus res = TryRead(key,value);
291
53
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const
Line
Count
Source
289
222
    {
290
222
        const ReadStatus res = TryRead(key,value);
291
222
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
Unexecuted instantiation: coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHashKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockLocator>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, CBlockLocator&) const
Line
Count
Source
289
54
    {
290
54
        const ReadStatus res = TryRead(key,value);
291
54
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int&) const
Line
Count
Source
289
4.30k
    {
290
4.30k
        const ReadStatus res = TryRead(key,value);
291
4.30k
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<txindex::BlockSeqKey, uint256>(txindex::BlockSeqKey const&, uint256&) const
Line
Count
Source
289
160
    {
290
160
        const ReadStatus res = TryRead(key,value);
291
160
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long>&) const
Line
Count
Source
289
29
    {
290
29
        const ReadStatus res = TryRead(key,value);
291
29
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo&) const
Line
Count
Source
289
2.71k
    {
290
2.71k
        const ReadStatus res = TryRead(key,value);
291
2.71k
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, int>(unsigned char const&, int&) const
Line
Count
Source
289
1.35k
    {
290
1.35k
        const ReadStatus res = TryRead(key,value);
291
1.35k
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char&) const
Line
Count
Source
289
1.34k
    {
290
1.34k
        const ReadStatus res = TryRead(key,value);
291
1.34k
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
bool CDBWrapper::Read<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>>&) const
Line
Count
Source
289
1.73k
    {
290
1.73k
        const ReadStatus res = TryRead(key,value);
291
1.73k
        if (res.has_value()) return res.value();
292
0
        switch (const auto& [err_code, err_msg] = res.error(); err_code) {
293
0
            case ReadFailure::Code::DeserializationError: return false;
294
0
            case ReadFailure::Code::DatabaseError: throw dbwrapper_error(err_msg);
295
0
        } // no default case, so the compiler can warn about missing cases
296
0
        std::abort(); // unreachable
297
0
    }
298
299
    template <typename K, typename V>
300
    void Write(const K& key, const V& value, bool fSync = false)
301
13.1k
    {
302
13.1k
        CDBBatch batch(*this);
303
13.1k
        batch.Write(key, value);
304
13.1k
        WriteBatch(batch, fSync);
305
13.1k
    }
void CDBWrapper::Write<unsigned char, uint256>(unsigned char const&, uint256 const&, bool)
Line
Count
Source
301
33
    {
302
33
        CDBBatch batch(*this);
303
33
        batch.Write(key, value);
304
33
        WriteBatch(batch, fSync);
305
33
    }
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256 const&, bool)
Line
Count
Source
301
8
    {
302
8
        CDBBatch batch(*this);
303
8
        batch.Write(key, value);
304
8
        WriteBatch(batch, fSync);
305
8
    }
void CDBWrapper::Write<unsigned char, unsigned int>(unsigned char const&, unsigned int const&, bool)
Line
Count
Source
301
258
    {
302
258
        CDBBatch batch(*this);
303
258
        batch.Write(key, value);
304
258
        WriteBatch(batch, fSync);
305
258
    }
void CDBWrapper::Write<unsigned char, bool>(unsigned char const&, bool const&, bool)
Line
Count
Source
301
2
    {
302
2
        CDBBatch batch(*this);
303
2
        batch.Write(key, value);
304
2
        WriteBatch(batch, fSync);
305
2
    }
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool const&, bool)
Line
Count
Source
301
2
    {
302
2
        CDBBatch batch(*this);
303
2
        batch.Write(key, value);
304
2
        WriteBatch(batch, fSync);
305
2
    }
void CDBWrapper::Write<unsigned char, unsigned char>(unsigned char const&, unsigned char const&, bool)
Line
Count
Source
301
19
    {
302
19
        CDBBatch batch(*this);
303
19
        batch.Write(key, value);
304
19
        WriteBatch(batch, fSync);
305
19
    }
void CDBWrapper::Write<dbwrapper_tests::StringContentsSerializer, unsigned int>(dbwrapper_tests::StringContentsSerializer const&, unsigned int const&, bool)
Line
Count
Source
301
100
    {
302
100
        CDBBatch batch(*this);
303
100
        batch.Write(key, value);
304
100
        WriteBatch(batch, fSync);
305
100
    }
void CDBWrapper::Write<txindex::DBKey, std::array<std::byte, 0ul>>(txindex::DBKey const&, std::array<std::byte, 0ul> const&, bool)
Line
Count
Source
301
1
    {
302
1
        CDBBatch batch(*this);
303
1
        batch.Write(key, value);
304
1
        WriteBatch(batch, fSync);
305
1
    }
void CDBWrapper::Write<std::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos const&, bool)
Line
Count
Source
301
2
    {
302
2
        CDBBatch batch(*this);
303
2
        batch.Write(key, value);
304
2
        WriteBatch(batch, fSync);
305
2
    }
void CDBWrapper::Write<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator const&, bool)
Line
Count
Source
301
1
    {
302
1
        CDBBatch batch(*this);
303
1
        batch.Write(key, value);
304
1
        WriteBatch(batch, fSync);
305
1
    }
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation const&, bool)
Line
Count
Source
301
580
    {
302
580
        CDBBatch batch(*this);
303
580
        batch.Write(key, value);
304
580
        WriteBatch(batch, fSync);
305
580
    }
blockfilterindex.cpp:void CDBWrapper::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&, bool)
Line
Count
Source
301
7.99k
    {
302
7.99k
        CDBBatch batch(*this);
303
7.99k
        batch.Write(key, value);
304
7.99k
        WriteBatch(batch, fSync);
305
7.99k
    }
coinstatsindex.cpp:void CDBWrapper::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&, bool)
Line
Count
Source
301
4.08k
    {
302
4.08k
        CDBBatch batch(*this);
303
4.08k
        batch.Write(key, value);
304
4.08k
        WriteBatch(batch, fSync);
305
4.08k
    }
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<unsigned long, unsigned long>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::pair<unsigned long, unsigned long> const&, bool)
Line
Count
Source
301
24
    {
302
24
        CDBBatch batch(*this);
303
24
        batch.Write(key, value);
304
24
        WriteBatch(batch, fSync);
305
24
    }
void CDBWrapper::Write<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long> const&, bool)
Line
Count
Source
301
10
    {
302
10
        CDBBatch batch(*this);
303
10
        batch.Write(key, value);
304
10
        WriteBatch(batch, fSync);
305
10
    }
void CDBWrapper::Write<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char const&, bool)
Line
Count
Source
301
8
    {
302
8
        CDBBatch batch(*this);
303
8
        batch.Write(key, value);
304
8
        WriteBatch(batch, fSync);
305
8
    }
306
307
    template <typename K>
308
    bool Exists(const K& key) const
309
6.24k
    {
310
6.24k
        DataStream ssKey{};
311
6.24k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
312
6.24k
        ssKey << key;
313
6.24k
        return ExistsImpl(ssKey);
314
6.24k
    }
bool CDBWrapper::Exists<unsigned char>(unsigned char const&) const
Line
Count
Source
309
1.39k
    {
310
1.39k
        DataStream ssKey{};
311
1.39k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
312
1.39k
        ssKey << key;
313
1.39k
        return ExistsImpl(ssKey);
314
1.39k
    }
bool CDBWrapper::Exists<txindex::BlockHashKey>(txindex::BlockHashKey const&) const
Line
Count
Source
309
4.80k
    {
310
4.80k
        DataStream ssKey{};
311
4.80k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
312
4.80k
        ssKey << key;
313
4.80k
        return ExistsImpl(ssKey);
314
4.80k
    }
txdb.cpp:bool CDBWrapper::Exists<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry const&) const
Line
Count
Source
309
44
    {
310
44
        DataStream ssKey{};
311
44
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
312
44
        ssKey << key;
313
44
        return ExistsImpl(ssKey);
314
44
    }
315
316
    template <typename K>
317
    void Erase(const K& key, bool fSync = false)
318
16
    {
319
16
        CDBBatch batch(*this);
320
16
        batch.Erase(key);
321
16
        WriteBatch(batch, fSync);
322
16
    }
void CDBWrapper::Erase<txindex::DBKey>(txindex::DBKey const&, bool)
Line
Count
Source
318
2
    {
319
2
        CDBBatch batch(*this);
320
2
        batch.Erase(key);
321
2
        WriteBatch(batch, fSync);
322
2
    }
void CDBWrapper::Erase<unsigned char>(unsigned char const&, bool)
Line
Count
Source
318
14
    {
319
14
        CDBBatch batch(*this);
320
14
        batch.Erase(key);
321
14
        WriteBatch(batch, fSync);
322
14
    }
323
324
    void WriteBatch(CDBBatch& batch, bool fSync = false);
325
326
    //! Perform a blocking full compaction of the underlying LevelDB.
327
    void CompactFull();
328
329
    //! Return a LevelDB property value, if available.
330
    std::optional<std::string> GetProperty(const std::string& property) const;
331
332
    // Get an estimate of LevelDB memory usage (in bytes).
333
    size_t DynamicMemoryUsage() const;
334
335
    CDBIterator* NewIterator();
336
337
    /**
338
     * Return true if the database managed by this class contains no entries.
339
     */
340
    bool IsEmpty();
341
342
    //! Probe an unopened database for a key prefix. Return true if a database at
343
    //! path exists and contains at least 1 entry beginning with prefix; missing
344
    //! or empty databases return false, and database errors throw dbwrapper_error.
345
    static bool HasKeyStartingWith(const fs::path& path, uint8_t prefix);
346
347
    template<typename K>
348
    size_t EstimateSize(const K& key_begin, const K& key_end) const
349
102
    {
350
102
        DataStream ssKey1{}, ssKey2{};
351
102
        ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
352
102
        ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
353
102
        ssKey1 << key_begin;
354
102
        ssKey2 << key_end;
355
102
        return EstimateSizeImpl(ssKey1, ssKey2);
356
102
    }
357
};
358
359
#endif // BITCOIN_DBWRAPPER_H