Coverage Report

Created: 2026-04-29 19:21

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/fs.h>
15
16
#include <cstddef>
17
#include <exception>
18
#include <memory>
19
#include <optional>
20
#include <stdexcept>
21
#include <string>
22
23
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
24
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
25
static const size_t DBWRAPPER_MAX_FILE_SIZE{32_MiB};
26
27
//! User-controlled performance and debug options.
28
struct DBOptions {
29
    //! Compact database on startup.
30
    bool force_compact = false;
31
};
32
33
//! Application-specific storage settings.
34
struct DBParams {
35
    //! Location in the filesystem where leveldb data will be stored.
36
    fs::path path;
37
    //! Configures various leveldb cache settings.
38
    size_t cache_bytes;
39
    //! If true, use leveldb's memory environment.
40
    bool memory_only = false;
41
    //! If true, remove all existing data.
42
    bool wipe_data = false;
43
    //! If true, store data obfuscated via simple XOR. If false, XOR with a
44
    //! zero'd byte array.
45
    bool obfuscate = false;
46
    //! Passed-through options.
47
    DBOptions options{};
48
};
49
50
class dbwrapper_error : public std::runtime_error
51
{
52
public:
53
10
    explicit dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {}
54
};
55
56
class CDBWrapper;
57
58
/** These should be considered an implementation detail of the specific database.
59
 */
60
namespace dbwrapper_private {
61
62
/** Work around circular dependency, as well as for testing in dbwrapper_tests.
63
 * Database obfuscation should be considered an implementation detail of the
64
 * specific database.
65
 */
66
const Obfuscation& GetObfuscation(const CDBWrapper&);
67
}; // namespace dbwrapper_private
68
69
bool DestroyDB(const std::string& path_str);
70
71
/** Batch of changes queued to be written to a CDBWrapper */
72
class CDBBatch
73
{
74
    friend class CDBWrapper;
75
76
private:
77
    const CDBWrapper &parent;
78
79
    struct WriteBatchImpl;
80
    const std::unique_ptr<WriteBatchImpl> m_impl_batch;
81
82
    DataStream ssKey{};
83
    DataStream ssValue{};
84
85
    void WriteImpl(std::span<const std::byte> key, DataStream& ssValue);
86
    void EraseImpl(std::span<const std::byte> key);
87
88
public:
89
    /**
90
     * @param[in] _parent   CDBWrapper that this batch is to be submitted to
91
     */
92
    explicit CDBBatch(const CDBWrapper& _parent);
93
    ~CDBBatch();
94
    void Clear();
95
96
    template <typename K, typename V>
97
    void Write(const K& key, const V& value)
98
420k
    {
99
420k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
420k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
420k
        ssKey << key;
102
420k
        ssValue << value;
103
420k
        WriteImpl(ssKey, ssValue);
104
420k
        ssKey.clear();
105
420k
        ssValue.clear();
106
420k
    }
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
98
8
    {
99
8
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
8
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
8
        ssKey << key;
102
8
        ssValue << value;
103
8
        WriteImpl(ssKey, ssValue);
104
8
        ssKey.clear();
105
8
        ssValue.clear();
106
8
    }
void CDBBatch::Write<unsigned char, unsigned int>(unsigned char const&, unsigned int const&)
Line
Count
Source
98
258
    {
99
258
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
258
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
258
        ssKey << key;
102
258
        ssValue << value;
103
258
        WriteImpl(ssKey, ssValue);
104
258
        ssKey.clear();
105
258
        ssValue.clear();
106
258
    }
void CDBBatch::Write<unsigned char, bool>(unsigned char const&, bool const&)
Line
Count
Source
98
2
    {
99
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
2
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
2
        ssKey << key;
102
2
        ssValue << value;
103
2
        WriteImpl(ssKey, ssValue);
104
2
        ssKey.clear();
105
2
        ssValue.clear();
106
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
98
2
    {
99
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
2
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
2
        ssKey << key;
102
2
        ssValue << value;
103
2
        WriteImpl(ssKey, ssValue);
104
2
        ssKey.clear();
105
2
        ssValue.clear();
106
2
    }
void CDBBatch::Write<unsigned char, uint256>(unsigned char const&, uint256 const&)
Line
Count
Source
98
3.70k
    {
99
3.70k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
3.70k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
3.70k
        ssKey << key;
102
3.70k
        ssValue << value;
103
3.70k
        WriteImpl(ssKey, ssValue);
104
3.70k
        ssKey.clear();
105
3.70k
        ssValue.clear();
106
3.70k
    }
void CDBBatch::Write<dbwrapper_tests::StringContentsSerializer, unsigned int>(dbwrapper_tests::StringContentsSerializer const&, unsigned int const&)
Line
Count
Source
98
100
    {
99
100
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
100
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
100
        ssKey << key;
102
100
        ssValue << value;
103
100
        WriteImpl(ssKey, ssValue);
104
100
        ssKey.clear();
105
100
        ssValue.clear();
106
100
    }
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
98
500
    {
99
500
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
500
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
500
        ssKey << key;
102
500
        ssValue << value;
103
500
        WriteImpl(ssKey, ssValue);
104
500
        ssKey.clear();
105
500
        ssValue.clear();
106
500
    }
void CDBBatch::Write<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator const&)
Line
Count
Source
98
284
    {
99
284
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
284
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
284
        ssKey << key;
102
284
        ssValue << value;
103
284
        WriteImpl(ssKey, ssValue);
104
284
        ssKey.clear();
105
284
        ssValue.clear();
106
284
    }
void CDBBatch::Write<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos const&)
Line
Count
Source
98
184
    {
99
184
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
184
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
184
        ssKey << key;
102
184
        ssValue << value;
103
184
        WriteImpl(ssKey, ssValue);
104
184
        ssKey.clear();
105
184
        ssValue.clear();
106
184
    }
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
98
7.54k
    {
99
7.54k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
7.54k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
7.54k
        ssKey << key;
102
7.54k
        ssValue << value;
103
7.54k
        WriteImpl(ssKey, ssValue);
104
7.54k
        ssKey.clear();
105
7.54k
        ssValue.clear();
106
7.54k
    }
blockfilterindex.cpp:void CDBBatch::Write<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal const&)
Line
Count
Source
98
111
    {
99
111
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
111
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
111
        ssKey << key;
102
111
        ssValue << value;
103
111
        WriteImpl(ssKey, ssValue);
104
111
        ssKey.clear();
105
111
        ssValue.clear();
106
111
    }
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
98
3.86k
    {
99
3.86k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
3.86k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
3.86k
        ssKey << key;
102
3.86k
        ssValue << value;
103
3.86k
        WriteImpl(ssKey, ssValue);
104
3.86k
        ssKey.clear();
105
3.86k
        ssValue.clear();
106
3.86k
    }
coinstatsindex.cpp:void CDBBatch::Write<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal const&)
Line
Count
Source
98
121
    {
99
121
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
121
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
121
        ssKey << key;
102
121
        ssValue << value;
103
121
        WriteImpl(ssKey, ssValue);
104
121
        ssKey.clear();
105
121
        ssValue.clear();
106
121
    }
void CDBBatch::Write<unsigned char, MuHash3072>(unsigned char const&, MuHash3072 const&)
Line
Count
Source
98
123
    {
99
123
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
123
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
123
        ssKey << key;
102
123
        ssValue << value;
103
123
        WriteImpl(ssKey, ssValue);
104
123
        ssKey.clear();
105
123
        ssValue.clear();
106
123
    }
void CDBBatch::Write<std::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos const&)
Line
Count
Source
98
3.74k
    {
99
3.74k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
3.74k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
3.74k
        ssKey << key;
102
3.74k
        ssValue << value;
103
3.74k
        WriteImpl(ssKey, ssValue);
104
3.74k
        ssKey.clear();
105
3.74k
        ssValue.clear();
106
3.74k
    }
void CDBBatch::Write<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long> const&)
Line
Count
Source
98
6
    {
99
6
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
6
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
6
        ssKey << key;
102
6
        ssValue << value;
103
6
        WriteImpl(ssKey, ssValue);
104
6
        ssKey.clear();
105
6
        ssValue.clear();
106
6
    }
void CDBBatch::Write<DBKey, char [1]>(DBKey const&, char const (&) [1])
Line
Count
Source
98
38
    {
99
38
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
38
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
38
        ssKey << key;
102
38
        ssValue << value;
103
38
        WriteImpl(ssKey, ssValue);
104
38
        ssKey.clear();
105
38
        ssValue.clear();
106
38
    }
void CDBBatch::Write<unsigned char, unsigned char>(unsigned char const&, unsigned char const&)
Line
Count
Source
98
14
    {
99
14
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
14
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
14
        ssKey << key;
102
14
        ssValue << value;
103
14
        WriteImpl(ssKey, ssValue);
104
14
        ssKey.clear();
105
14
        ssValue.clear();
106
14
    }
void CDBBatch::Write<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo const&)
Line
Count
Source
98
1.61k
    {
99
1.61k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
1.61k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
1.61k
        ssKey << key;
102
1.61k
        ssValue << value;
103
1.61k
        WriteImpl(ssKey, ssValue);
104
1.61k
        ssKey.clear();
105
1.61k
        ssValue.clear();
106
1.61k
    }
void CDBBatch::Write<unsigned char, int>(unsigned char const&, int const&)
Line
Count
Source
98
3.36k
    {
99
3.36k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
3.36k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
3.36k
        ssKey << key;
102
3.36k
        ssValue << value;
103
3.36k
        WriteImpl(ssKey, ssValue);
104
3.36k
        ssKey.clear();
105
3.36k
        ssValue.clear();
106
3.36k
    }
void CDBBatch::Write<std::pair<unsigned char, uint256>, CDiskBlockIndex>(std::pair<unsigned char, uint256> const&, CDiskBlockIndex const&)
Line
Count
Source
98
118k
    {
99
118k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
118k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
118k
        ssKey << key;
102
118k
        ssValue << value;
103
118k
        WriteImpl(ssKey, ssValue);
104
118k
        ssKey.clear();
105
118k
        ssValue.clear();
106
118k
    }
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
98
7
    {
99
7
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
7
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
7
        ssKey << key;
102
7
        ssValue << value;
103
7
        WriteImpl(ssKey, ssValue);
104
7
        ssKey.clear();
105
7
        ssValue.clear();
106
7
    }
void CDBBatch::Write<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>> const&)
Line
Count
Source
98
3.67k
    {
99
3.67k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
3.67k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
3.67k
        ssKey << key;
102
3.67k
        ssValue << value;
103
3.67k
        WriteImpl(ssKey, ssValue);
104
3.67k
        ssKey.clear();
105
3.67k
        ssValue.clear();
106
3.67k
    }
txdb.cpp:void CDBBatch::Write<(anonymous namespace)::CoinEntry, Coin>((anonymous namespace)::CoinEntry const&, Coin const&)
Line
Count
Source
98
272k
    {
99
272k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100
272k
        ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
101
272k
        ssKey << key;
102
272k
        ssValue << value;
103
272k
        WriteImpl(ssKey, ssValue);
104
272k
        ssKey.clear();
105
272k
        ssValue.clear();
106
272k
    }
107
108
    template <typename K>
109
    void Erase(const K& key)
110
40.8k
    {
111
40.8k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
112
40.8k
        ssKey << key;
113
40.8k
        EraseImpl(ssKey);
114
40.8k
        ssKey.clear();
115
40.8k
    }
void CDBBatch::Erase<unsigned char>(unsigned char const&)
Line
Count
Source
110
7.36k
    {
111
7.36k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
112
7.36k
        ssKey << key;
113
7.36k
        EraseImpl(ssKey);
114
7.36k
        ssKey.clear();
115
7.36k
    }
void CDBBatch::Erase<DBKey>(DBKey const&)
Line
Count
Source
110
6
    {
111
6
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
112
6
        ssKey << key;
113
6
        EraseImpl(ssKey);
114
6
        ssKey.clear();
115
6
    }
txdb.cpp:void CDBBatch::Erase<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry const&)
Line
Count
Source
110
33.4k
    {
111
33.4k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
112
33.4k
        ssKey << key;
113
33.4k
        EraseImpl(ssKey);
114
33.4k
        ssKey.clear();
115
33.4k
    }
116
117
    size_t ApproximateSize() const;
118
};
119
120
class CDBIterator
121
{
122
public:
123
    struct IteratorImpl;
124
125
private:
126
    const CDBWrapper &parent;
127
    const std::unique_ptr<IteratorImpl> m_impl_iter;
128
129
    void SeekImpl(std::span<const std::byte> key);
130
    std::span<const std::byte> GetKeyImpl() const;
131
    std::span<const std::byte> GetValueImpl() const;
132
133
public:
134
135
    /**
136
     * @param[in] _parent          Parent CDBWrapper instance.
137
     * @param[in] _piter           The original leveldb iterator.
138
     */
139
    CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter);
140
    ~CDBIterator();
141
142
    bool Valid() const;
143
144
    void SeekToFirst();
145
146
4.29k
    template<typename K> void Seek(const K& key) {
147
4.29k
        DataStream ssKey{};
148
4.29k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
149
4.29k
        ssKey << key;
150
4.29k
        SeekImpl(ssKey);
151
4.29k
    }
void CDBIterator::Seek<unsigned char>(unsigned char const&)
Line
Count
Source
146
1.22k
    template<typename K> void Seek(const K& key) {
147
1.22k
        DataStream ssKey{};
148
1.22k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
149
1.22k
        ssKey << key;
150
1.22k
        SeekImpl(ssKey);
151
1.22k
    }
void CDBIterator::Seek<dbwrapper_tests::StringContentsSerializer>(dbwrapper_tests::StringContentsSerializer const&)
Line
Count
Source
146
2
    template<typename K> void Seek(const K& key) {
147
2
        DataStream ssKey{};
148
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
149
2
        ssKey << key;
150
2
        SeekImpl(ssKey);
151
2
    }
void CDBIterator::Seek<index_util::DBHeightKey>(index_util::DBHeightKey const&)
Line
Count
Source
146
680
    template<typename K> void Seek(const K& key) {
147
680
        DataStream ssKey{};
148
680
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
149
680
        ssKey << key;
150
680
        SeekImpl(ssKey);
151
680
    }
void CDBIterator::Seek<std::pair<unsigned char, unsigned long>>(std::pair<unsigned char, unsigned long> const&)
Line
Count
Source
146
35
    template<typename K> void Seek(const K& key) {
147
35
        DataStream ssKey{};
148
35
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
149
35
        ssKey << key;
150
35
        SeekImpl(ssKey);
151
35
    }
void CDBIterator::Seek<std::pair<unsigned char, uint256>>(std::pair<unsigned char, uint256> const&)
Line
Count
Source
146
2.34k
    template<typename K> void Seek(const K& key) {
147
2.34k
        DataStream ssKey{};
148
2.34k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
149
2.34k
        ssKey << key;
150
2.34k
        SeekImpl(ssKey);
151
2.34k
    }
152
153
    void Next();
154
155
358k
    template<typename K> bool GetKey(K& key) {
156
358k
        try {
157
358k
            SpanReader ssKey{GetKeyImpl()};
158
358k
            ssKey >> key;
159
358k
        } catch (const std::exception&) {
160
735
            return false;
161
735
        }
162
357k
        return true;
163
358k
    }
bool CDBIterator::GetKey<unsigned short>(unsigned short&)
Line
Count
Source
155
2
    template<typename K> bool GetKey(K& key) {
156
2
        try {
157
2
            SpanReader ssKey{GetKeyImpl()};
158
2
            ssKey >> key;
159
2
        } catch (const std::exception&) {
160
2
            return false;
161
2
        }
162
0
        return true;
163
2
    }
bool CDBIterator::GetKey<unsigned char>(unsigned char&)
Line
Count
Source
155
386
    template<typename K> bool GetKey(K& key) {
156
386
        try {
157
386
            SpanReader ssKey{GetKeyImpl()};
158
386
            ssKey >> key;
159
386
        } catch (const std::exception&) {
160
0
            return false;
161
0
        }
162
386
        return true;
163
386
    }
bool CDBIterator::GetKey<dbwrapper_tests::StringContentsSerializer>(dbwrapper_tests::StringContentsSerializer&)
Line
Count
Source
155
150
    template<typename K> bool GetKey(K& key) {
156
150
        try {
157
150
            SpanReader ssKey{GetKeyImpl()};
158
150
            ssKey >> key;
159
150
        } catch (const std::exception&) {
160
0
            return false;
161
0
        }
162
150
        return true;
163
150
    }
bool CDBIterator::GetKey<index_util::DBHeightKey>(index_util::DBHeightKey&)
Line
Count
Source
155
3.09k
    template<typename K> bool GetKey(K& key) {
156
3.09k
        try {
157
3.09k
            SpanReader ssKey{GetKeyImpl()};
158
3.09k
            ssKey >> key;
159
3.09k
        } catch (const std::exception&) {
160
0
            return false;
161
0
        }
162
3.09k
        return true;
163
3.09k
    }
bool CDBIterator::GetKey<DBKey>(DBKey&)
Line
Count
Source
155
25
    template<typename K> bool GetKey(K& key) {
156
25
        try {
157
25
            SpanReader ssKey{GetKeyImpl()};
158
25
            ssKey >> key;
159
25
        } catch (const std::exception&) {
160
0
            return false;
161
0
        }
162
25
        return true;
163
25
    }
bool CDBIterator::GetKey<std::pair<unsigned char, uint256>>(std::pair<unsigned char, uint256>&)
Line
Count
Source
155
133k
    template<typename K> bool GetKey(K& key) {
156
133k
        try {
157
133k
            SpanReader ssKey{GetKeyImpl()};
158
133k
            ssKey >> key;
159
133k
        } catch (const std::exception&) {
160
733
            return false;
161
733
        }
162
132k
        return true;
163
133k
    }
txdb.cpp:bool CDBIterator::GetKey<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry&)
Line
Count
Source
155
221k
    template<typename K> bool GetKey(K& key) {
156
221k
        try {
157
221k
            SpanReader ssKey{GetKeyImpl()};
158
221k
            ssKey >> key;
159
221k
        } catch (const std::exception&) {
160
0
            return false;
161
0
        }
162
221k
        return true;
163
221k
    }
164
165
357k
    template<typename V> bool GetValue(V& value) {
166
357k
        try {
167
357k
            DataStream ssValue{GetValueImpl()};
168
357k
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
357k
            ssValue >> value;
170
357k
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
357k
        return true;
174
357k
    }
bool CDBIterator::GetValue<uint256>(uint256&)
Line
Count
Source
165
4
    template<typename V> bool GetValue(V& value) {
166
4
        try {
167
4
            DataStream ssValue{GetValueImpl()};
168
4
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
4
            ssValue >> value;
170
4
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
4
        return true;
174
4
    }
bool CDBIterator::GetValue<unsigned int>(unsigned int&)
Line
Count
Source
165
342
    template<typename V> bool GetValue(V& value) {
166
342
        try {
167
342
            DataStream ssValue{GetValueImpl()};
168
342
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
342
            ssValue >> value;
170
342
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
342
        return true;
174
342
    }
blockfilterindex.cpp:bool CDBIterator::GetValue<std::pair<uint256, (anonymous namespace)::DBVal>>(std::pair<uint256, (anonymous namespace)::DBVal>&)
Line
Count
Source
165
2.97k
    template<typename V> bool GetValue(V& value) {
166
2.97k
        try {
167
2.97k
            DataStream ssValue{GetValueImpl()};
168
2.97k
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
2.97k
            ssValue >> value;
170
2.97k
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
2.97k
        return true;
174
2.97k
    }
coinstatsindex.cpp:bool CDBIterator::GetValue<std::pair<uint256, (anonymous namespace)::DBVal>>(std::pair<uint256, (anonymous namespace)::DBVal>&)
Line
Count
Source
165
121
    template<typename V> bool GetValue(V& value) {
166
121
        try {
167
121
            DataStream ssValue{GetValueImpl()};
168
121
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
121
            ssValue >> value;
170
121
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
121
        return true;
174
121
    }
bool CDBIterator::GetValue<CDiskBlockIndex>(CDiskBlockIndex&)
Line
Count
Source
165
132k
    template<typename V> bool GetValue(V& value) {
166
132k
        try {
167
132k
            DataStream ssValue{GetValueImpl()};
168
132k
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
132k
            ssValue >> value;
170
132k
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
132k
        return true;
174
132k
    }
bool CDBIterator::GetValue<Coin>(Coin&)
Line
Count
Source
165
221k
    template<typename V> bool GetValue(V& value) {
166
221k
        try {
167
221k
            DataStream ssValue{GetValueImpl()};
168
221k
            dbwrapper_private::GetObfuscation(parent)(ssValue);
169
221k
            ssValue >> value;
170
221k
        } catch (const std::exception&) {
171
0
            return false;
172
0
        }
173
221k
        return true;
174
221k
    }
175
};
176
177
struct LevelDBContext;
178
179
class CDBWrapper
180
{
181
    friend const Obfuscation& dbwrapper_private::GetObfuscation(const CDBWrapper&);
182
private:
183
    //! holds all leveldb-specific fields of this class
184
    std::unique_ptr<LevelDBContext> m_db_context;
185
186
    //! the name of this database
187
    std::string m_name;
188
189
    //! optional XOR-obfuscation of the database
190
    Obfuscation m_obfuscation;
191
192
    //! obfuscation key storage key, null-prefixed to avoid collisions
193
    inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0
194
195
    std::optional<std::string> ReadImpl(std::span<const std::byte> key) const;
196
    bool ExistsImpl(std::span<const std::byte> key) const;
197
    size_t EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const;
198
13.5M
    auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); }
199
200
public:
201
    CDBWrapper(const DBParams& params);
202
    ~CDBWrapper();
203
204
    CDBWrapper(const CDBWrapper&) = delete;
205
    CDBWrapper& operator=(const CDBWrapper&) = delete;
206
207
    template <typename K, typename V>
208
    bool Read(const K& key, V& value) const
209
6.73M
    {
210
6.73M
        DataStream ssKey{};
211
6.73M
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
6.73M
        ssKey << key;
213
6.73M
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
6.73M
        if (!strValue) {
215
6.63M
            return false;
216
6.63M
        }
217
93.3k
        try {
218
93.3k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
93.3k
            m_obfuscation(ssValue);
220
93.3k
            SpanReader{ssValue} >> value;
221
93.3k
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
93.3k
        return true;
225
93.3k
    }
bool CDBWrapper::Read<unsigned char, uint256>(unsigned char const&, uint256&) const
Line
Count
Source
209
7.57k
    {
210
7.57k
        DataStream ssKey{};
211
7.57k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
7.57k
        ssKey << key;
213
7.57k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
7.57k
        if (!strValue) {
215
1.66k
            return false;
216
1.66k
        }
217
5.90k
        try {
218
5.90k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
5.90k
            m_obfuscation(ssValue);
220
5.90k
            SpanReader{ssValue} >> value;
221
5.90k
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
5.90k
        return true;
225
5.90k
    }
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
209
8
    {
210
8
        DataStream ssKey{};
211
8
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
8
        ssKey << key;
213
8
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
8
        if (!strValue) {
215
0
            return false;
216
0
        }
217
8
        try {
218
8
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
8
            m_obfuscation(ssValue);
220
8
            SpanReader{ssValue} >> value;
221
8
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
8
        return true;
225
8
    }
bool CDBWrapper::Read<unsigned char, unsigned int>(unsigned char const&, unsigned int&) const
Line
Count
Source
209
2
    {
210
2
        DataStream ssKey{};
211
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
2
        ssKey << key;
213
2
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
2
        if (!strValue) {
215
0
            return false;
216
0
        }
217
2
        try {
218
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
2
            m_obfuscation(ssValue);
220
2
            SpanReader{ssValue} >> value;
221
2
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
2
        return true;
225
2
    }
bool CDBWrapper::Read<unsigned char, bool>(unsigned char const&, bool&) const
Line
Count
Source
209
2
    {
210
2
        DataStream ssKey{};
211
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
2
        ssKey << key;
213
2
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
2
        if (!strValue) {
215
0
            return false;
216
0
        }
217
2
        try {
218
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
2
            m_obfuscation(ssValue);
220
2
            SpanReader{ssValue} >> value;
221
2
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
2
        return true;
225
2
    }
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
209
2
    {
210
2
        DataStream ssKey{};
211
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
2
        ssKey << key;
213
2
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
2
        if (!strValue) {
215
0
            return false;
216
0
        }
217
2
        try {
218
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
2
            m_obfuscation(ssValue);
220
2
            SpanReader{ssValue} >> value;
221
2
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
2
        return true;
225
2
    }
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
209
2.70k
    {
210
2.70k
        DataStream ssKey{};
211
2.70k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
2.70k
        ssKey << key;
213
2.70k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
2.70k
        if (!strValue) {
215
1.85k
            return false;
216
1.85k
        }
217
846
        try {
218
846
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
846
            m_obfuscation(ssValue);
220
846
            SpanReader{ssValue} >> value;
221
846
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
845
        return true;
225
846
    }
bool CDBWrapper::Read<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator&) const
Line
Count
Source
209
145
    {
210
145
        DataStream ssKey{};
211
145
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
145
        ssKey << key;
213
145
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
145
        if (!strValue) {
215
52
            return false;
216
52
        }
217
93
        try {
218
93
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
93
            m_obfuscation(ssValue);
220
93
            SpanReader{ssValue} >> value;
221
93
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
93
        return true;
225
93
    }
blockfilterindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const
Line
Count
Source
209
29
    {
210
29
        DataStream ssKey{};
211
29
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
29
        ssKey << key;
213
29
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
29
        if (!strValue) {
215
0
            return false;
216
0
        }
217
29
        try {
218
29
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
29
            m_obfuscation(ssValue);
220
29
            SpanReader{ssValue} >> value;
221
29
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
29
        return true;
225
29
    }
bool CDBWrapper::Read<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos&) const
Line
Count
Source
209
42
    {
210
42
        DataStream ssKey{};
211
42
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
42
        ssKey << key;
213
42
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
42
        if (!strValue) {
215
16
            return false;
216
16
        }
217
26
        try {
218
26
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
26
            m_obfuscation(ssValue);
220
26
            SpanReader{ssValue} >> value;
221
26
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
26
        return true;
225
26
    }
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
209
1.21k
    {
210
1.21k
        DataStream ssKey{};
211
1.21k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
1.21k
        ssKey << key;
213
1.21k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
1.21k
        if (!strValue) {
215
202
            return false;
216
202
        }
217
1.01k
        try {
218
1.01k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
1.01k
            m_obfuscation(ssValue);
220
1.01k
            SpanReader{ssValue} >> value;
221
1.01k
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
1.01k
        return true;
225
1.01k
    }
coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const
Line
Count
Source
209
2
    {
210
2
        DataStream ssKey{};
211
2
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
2
        ssKey << key;
213
2
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
2
        if (!strValue) {
215
0
            return false;
216
0
        }
217
2
        try {
218
2
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
2
            m_obfuscation(ssValue);
220
2
            SpanReader{ssValue} >> value;
221
2
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
2
        return true;
225
2
    }
bool CDBWrapper::Read<unsigned char, MuHash3072>(unsigned char const&, MuHash3072&) const
Line
Count
Source
209
45
    {
210
45
        DataStream ssKey{};
211
45
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
45
        ssKey << key;
213
45
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
45
        if (!strValue) {
215
17
            return false;
216
17
        }
217
28
        try {
218
28
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
28
            m_obfuscation(ssValue);
220
28
            SpanReader{ssValue} >> value;
221
28
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
28
        return true;
225
28
    }
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
209
219
    {
210
219
        DataStream ssKey{};
211
219
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
219
        ssKey << key;
213
219
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
219
        if (!strValue) {
215
1
            return false;
216
1
        }
217
218
        try {
218
218
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
218
            m_obfuscation(ssValue);
220
218
            SpanReader{ssValue} >> value;
221
218
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
218
        return true;
225
218
    }
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::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos&) const
Line
Count
Source
209
243
    {
210
243
        DataStream ssKey{};
211
243
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
243
        ssKey << key;
213
243
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
243
        if (!strValue) {
215
101
            return false;
216
101
        }
217
142
        try {
218
142
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
142
            m_obfuscation(ssValue);
220
142
            SpanReader{ssValue} >> value;
221
142
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
142
        return true;
225
142
    }
bool CDBWrapper::Read<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long>&) const
Line
Count
Source
209
22
    {
210
22
        DataStream ssKey{};
211
22
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
22
        ssKey << key;
213
22
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
22
        if (!strValue) {
215
6
            return false;
216
6
        }
217
16
        try {
218
16
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
16
            m_obfuscation(ssValue);
220
16
            SpanReader{ssValue} >> value;
221
16
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
16
        return true;
225
16
    }
bool CDBWrapper::Read<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo&) const
Line
Count
Source
209
2.34k
    {
210
2.34k
        DataStream ssKey{};
211
2.34k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
2.34k
        ssKey << key;
213
2.34k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
2.34k
        if (!strValue) {
215
1.60k
            return false;
216
1.60k
        }
217
747
        try {
218
747
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
747
            m_obfuscation(ssValue);
220
747
            SpanReader{ssValue} >> value;
221
747
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
747
        return true;
225
747
    }
bool CDBWrapper::Read<unsigned char, int>(unsigned char const&, int&) const
Line
Count
Source
209
1.16k
    {
210
1.16k
        DataStream ssKey{};
211
1.16k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
1.16k
        ssKey << key;
213
1.16k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
1.16k
        if (!strValue) {
215
435
            return false;
216
435
        }
217
731
        try {
218
731
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
731
            m_obfuscation(ssValue);
220
731
            SpanReader{ssValue} >> value;
221
731
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
731
        return true;
225
731
    }
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
209
1.16k
    {
210
1.16k
        DataStream ssKey{};
211
1.16k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
1.16k
        ssKey << key;
213
1.16k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
1.16k
        if (!strValue) {
215
1.16k
            return false;
216
1.16k
        }
217
1
        try {
218
1
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
1
            m_obfuscation(ssValue);
220
1
            SpanReader{ssValue} >> value;
221
1
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
1
        return true;
225
1
    }
txdb.cpp:bool CDBWrapper::Read<(anonymous namespace)::CoinEntry, Coin>((anonymous namespace)::CoinEntry const&, Coin&) const
Line
Count
Source
209
6.71M
    {
210
6.71M
        DataStream ssKey{};
211
6.71M
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
6.71M
        ssKey << key;
213
6.71M
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
6.71M
        if (!strValue) {
215
6.63M
            return false;
216
6.63M
        }
217
83.5k
        try {
218
83.5k
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
83.5k
            m_obfuscation(ssValue);
220
83.5k
            SpanReader{ssValue} >> value;
221
83.5k
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
83.5k
        return true;
225
83.5k
    }
bool CDBWrapper::Read<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>>&) const
Line
Count
Source
209
1.52k
    {
210
1.52k
        DataStream ssKey{};
211
1.52k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
212
1.52k
        ssKey << key;
213
1.52k
        std::optional<std::string> strValue{ReadImpl(ssKey)};
214
1.52k
        if (!strValue) {
215
1.52k
            return false;
216
1.52k
        }
217
0
        try {
218
0
            std::span ssValue{MakeWritableByteSpan(*strValue)};
219
0
            m_obfuscation(ssValue);
220
0
            SpanReader{ssValue} >> value;
221
0
        } catch (const std::exception&) {
222
0
            return false;
223
0
        }
224
0
        return true;
225
0
    }
226
227
    template <typename K, typename V>
228
    void Write(const K& key, const V& value, bool fSync = false)
229
12.3k
    {
230
12.3k
        CDBBatch batch(*this);
231
12.3k
        batch.Write(key, value);
232
12.3k
        WriteBatch(batch, fSync);
233
12.3k
    }
void CDBWrapper::Write<unsigned char, uint256>(unsigned char const&, uint256 const&, bool)
Line
Count
Source
229
30
    {
230
30
        CDBBatch batch(*this);
231
30
        batch.Write(key, value);
232
30
        WriteBatch(batch, fSync);
233
30
    }
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
229
8
    {
230
8
        CDBBatch batch(*this);
231
8
        batch.Write(key, value);
232
8
        WriteBatch(batch, fSync);
233
8
    }
void CDBWrapper::Write<unsigned char, unsigned int>(unsigned char const&, unsigned int const&, bool)
Line
Count
Source
229
258
    {
230
258
        CDBBatch batch(*this);
231
258
        batch.Write(key, value);
232
258
        WriteBatch(batch, fSync);
233
258
    }
void CDBWrapper::Write<unsigned char, bool>(unsigned char const&, bool const&, bool)
Line
Count
Source
229
2
    {
230
2
        CDBBatch batch(*this);
231
2
        batch.Write(key, value);
232
2
        WriteBatch(batch, fSync);
233
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
229
2
    {
230
2
        CDBBatch batch(*this);
231
2
        batch.Write(key, value);
232
2
        WriteBatch(batch, fSync);
233
2
    }
void CDBWrapper::Write<dbwrapper_tests::StringContentsSerializer, unsigned int>(dbwrapper_tests::StringContentsSerializer const&, unsigned int const&, bool)
Line
Count
Source
229
100
    {
230
100
        CDBBatch batch(*this);
231
100
        batch.Write(key, value);
232
100
        WriteBatch(batch, fSync);
233
100
    }
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
229
500
    {
230
500
        CDBBatch batch(*this);
231
500
        batch.Write(key, value);
232
500
        WriteBatch(batch, fSync);
233
500
    }
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
229
7.54k
    {
230
7.54k
        CDBBatch batch(*this);
231
7.54k
        batch.Write(key, value);
232
7.54k
        WriteBatch(batch, fSync);
233
7.54k
    }
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
229
3.86k
    {
230
3.86k
        CDBBatch batch(*this);
231
3.86k
        batch.Write(key, value);
232
3.86k
        WriteBatch(batch, fSync);
233
3.86k
    }
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
229
6
    {
230
6
        CDBBatch batch(*this);
231
6
        batch.Write(key, value);
232
6
        WriteBatch(batch, fSync);
233
6
    }
void CDBWrapper::Write<unsigned char, unsigned char>(unsigned char const&, unsigned char const&, bool)
Line
Count
Source
229
14
    {
230
14
        CDBBatch batch(*this);
231
14
        batch.Write(key, value);
232
14
        WriteBatch(batch, fSync);
233
14
    }
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
229
7
    {
230
7
        CDBBatch batch(*this);
231
7
        batch.Write(key, value);
232
7
        WriteBatch(batch, fSync);
233
7
    }
234
235
    template <typename K>
236
    bool Exists(const K& key) const
237
1.24k
    {
238
1.24k
        DataStream ssKey{};
239
1.24k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
240
1.24k
        ssKey << key;
241
1.24k
        return ExistsImpl(ssKey);
242
1.24k
    }
bool CDBWrapper::Exists<unsigned char>(unsigned char const&) const
Line
Count
Source
237
1.19k
    {
238
1.19k
        DataStream ssKey{};
239
1.19k
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
240
1.19k
        ssKey << key;
241
1.19k
        return ExistsImpl(ssKey);
242
1.19k
    }
txdb.cpp:bool CDBWrapper::Exists<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry const&) const
Line
Count
Source
237
44
    {
238
44
        DataStream ssKey{};
239
44
        ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
240
44
        ssKey << key;
241
44
        return ExistsImpl(ssKey);
242
44
    }
243
244
    template <typename K>
245
    void Erase(const K& key, bool fSync = false)
246
13
    {
247
13
        CDBBatch batch(*this);
248
13
        batch.Erase(key);
249
13
        WriteBatch(batch, fSync);
250
13
    }
251
252
    void WriteBatch(CDBBatch& batch, bool fSync = false);
253
254
    // Get an estimate of LevelDB memory usage (in bytes).
255
    size_t DynamicMemoryUsage() const;
256
257
    CDBIterator* NewIterator();
258
259
    /**
260
     * Return true if the database managed by this class contains no entries.
261
     */
262
    bool IsEmpty();
263
264
    template<typename K>
265
    size_t EstimateSize(const K& key_begin, const K& key_end) const
266
102
    {
267
102
        DataStream ssKey1{}, ssKey2{};
268
102
        ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
269
102
        ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
270
102
        ssKey1 << key_begin;
271
102
        ssKey2 << key_end;
272
102
        return EstimateSizeImpl(ssKey1, ssKey2);
273
102
    }
274
};
275
276
#endif // BITCOIN_DBWRAPPER_H