Coverage Report

Created: 2026-07-14 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/dbwrapper.cpp
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
#include <dbwrapper.h>
6
7
#include <leveldb/cache.h>
8
#include <leveldb/db.h>
9
#include <leveldb/env.h>
10
#include <leveldb/filter_policy.h>
11
#include <leveldb/helpers/memenv/memenv.h>
12
#include <leveldb/iterator.h>
13
#include <leveldb/options.h>
14
#include <leveldb/slice.h>
15
#include <leveldb/status.h>
16
#include <leveldb/write_batch.h>
17
#include <random.h>
18
#include <serialize.h>
19
#include <span.h>
20
#include <streams.h>
21
#include <util/byte_units.h>
22
#include <util/fs.h>
23
#include <util/fs_helpers.h>
24
#include <util/log.h>
25
#include <util/obfuscation.h>
26
#include <util/strencodings.h>
27
28
#include <algorithm>
29
#include <cassert>
30
#include <cstdarg>
31
#include <cstdint>
32
#include <cstdio>
33
#include <memory>
34
#include <optional>
35
#include <utility>
36
37
126M
static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
38
39
bool DestroyDB(const std::string& path_str)
40
0
{
41
0
    return leveldb::DestroyDB(path_str, {}).ok();
42
0
}
43
44
/** Handle database error by throwing dbwrapper_error exception.
45
 */
46
static void HandleError(const leveldb::Status& status)
47
831k
{
48
831k
    if (status.ok())
  Branch (48:9): [True: 831k, False: 0]
49
831k
        return;
50
0
    const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
51
0
    LogError("%s", errmsg);
52
0
    LogInfo("You can use -debug=leveldb to get more complete diagnostic messages");
53
0
    throw dbwrapper_error(errmsg);
54
831k
}
55
56
class CBitcoinLevelDBLogger : public leveldb::Logger {
57
public:
58
    // This code is adapted from posix_logger.h, which is why it is using vsprintf.
59
    // Please do not do this in normal code
60
145
    void Logv(const char * format, va_list ap) override {
61
145
            if (!util::log::ShouldDebugLog(BCLog::LEVELDB)) {
  Branch (61:17): [True: 145, False: 0]
62
145
                return;
63
145
            }
64
0
            char buffer[500];
65
0
            for (int iter = 0; iter < 2; iter++) {
  Branch (65:32): [True: 0, False: 0]
66
0
                char* base;
67
0
                int bufsize;
68
0
                if (iter == 0) {
  Branch (68:21): [True: 0, False: 0]
69
0
                    bufsize = sizeof(buffer);
70
0
                    base = buffer;
71
0
                }
72
0
                else {
73
0
                    bufsize = 30000;
74
0
                    base = new char[bufsize];
75
0
                }
76
0
                char* p = base;
77
0
                char* limit = base + bufsize;
78
79
                // Print the message
80
0
                if (p < limit) {
  Branch (80:21): [True: 0, False: 0]
81
0
                    va_list backup_ap;
82
0
                    va_copy(backup_ap, ap);
83
                    // Do not use vsnprintf elsewhere in bitcoin source code, see above.
84
0
                    p += vsnprintf(p, limit - p, format, backup_ap);
85
0
                    va_end(backup_ap);
86
0
                }
87
88
                // Truncate to available space if necessary
89
0
                if (p >= limit) {
  Branch (89:21): [True: 0, False: 0]
90
0
                    if (iter == 0) {
  Branch (90:25): [True: 0, False: 0]
91
0
                        continue;       // Try again with larger buffer
92
0
                    }
93
0
                    else {
94
0
                        p = limit - 1;
95
0
                    }
96
0
                }
97
98
                // Add newline if necessary
99
0
                if (p == base || p[-1] != '\n') {
  Branch (99:21): [True: 0, False: 0]
  Branch (99:34): [True: 0, False: 0]
100
0
                    *p++ = '\n';
101
0
                }
102
103
0
                assert(p <= limit);
  Branch (103:17): [True: 0, False: 0]
104
0
                base[std::min(bufsize - 1, (int)(p - base))] = '\0';
105
0
                LogDebug(BCLog::LEVELDB, "%s\n", util::RemoveSuffixView(base, "\n"));
106
0
                if (base != buffer) {
  Branch (106:21): [True: 0, False: 0]
107
0
                    delete[] base;
108
0
                }
109
0
                break;
110
0
            }
111
0
    }
112
};
113
114
81
static void SetMaxOpenFiles(leveldb::Options *options) {
115
    // On most platforms the default setting of max_open_files (which is 1000)
116
    // is optimal. On Windows using a large file count is OK because the handles
117
    // do not interfere with select() loops. On 64-bit Unix hosts this value is
118
    // also OK, because up to that amount LevelDB will use an mmap
119
    // implementation that does not use extra file descriptors (the fds are
120
    // closed after being mmap'ed).
121
    //
122
    // Increasing the value beyond the default is dangerous because LevelDB will
123
    // fall back to a non-mmap implementation when the file count is too large.
124
    // On 32-bit Unix host we should decrease the value because the handles use
125
    // up real fds, and we want to avoid fd exhaustion issues.
126
    //
127
    // See PR #12495 for further discussion.
128
129
81
    int default_open_files = options->max_open_files;
130
81
#ifndef WIN32
131
81
    if (sizeof(void*) < 8) {
  Branch (131:9): [Folded - Ignored]
132
0
        options->max_open_files = 64;
133
0
    }
134
81
#endif
135
81
    LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
136
81
             options->max_open_files, default_open_files);
137
81
}
138
139
static leveldb::Options GetOptions(size_t nCacheSize)
140
81
{
141
81
    leveldb::Options options;
142
81
    options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
143
81
    options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
144
81
    options.filter_policy = leveldb::NewBloomFilterPolicy(10);
145
81
    options.compression = leveldb::kNoCompression;
146
81
    options.info_log = new CBitcoinLevelDBLogger();
147
81
    if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
  Branch (147:9): [Folded - Ignored]
  Branch (147:40): [Folded - Ignored]
  Branch (147:71): [Folded - Ignored]
148
        // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
149
        // on corruption in later versions.
150
81
        options.paranoid_checks = true;
151
81
    }
152
81
    SetMaxOpenFiles(&options);
153
81
    return options;
154
81
}
155
156
struct CDBBatch::WriteBatchImpl {
157
    leveldb::WriteBatch batch;
158
};
159
160
CDBBatch::CDBBatch(const CDBWrapper& _parent)
161
831k
    : parent{_parent},
162
831k
      m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
163
831k
{
164
831k
    m_key_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
165
831k
    m_value_scratch.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
166
831k
    Clear();
167
831k
};
168
169
831k
CDBBatch::~CDBBatch() = default;
170
171
void CDBBatch::Clear()
172
831k
{
173
831k
    m_impl_batch->batch.Clear();
174
831k
    assert(m_key_scratch.empty());
  Branch (174:5): [True: 831k, False: 0]
175
831k
    assert(m_value_scratch.empty());
  Branch (175:5): [True: 831k, False: 0]
176
831k
}
177
178
void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& value)
179
61.7M
{
180
61.7M
    leveldb::Slice slKey(CharCast(key.data()), key.size());
181
61.7M
    dbwrapper_private::GetObfuscation(parent)(value);
182
61.7M
    leveldb::Slice slValue(CharCast(value.data()), value.size());
183
61.7M
    m_impl_batch->batch.Put(slKey, slValue);
184
61.7M
}
185
186
void CDBBatch::EraseImpl(std::span<const std::byte> key)
187
633k
{
188
633k
    leveldb::Slice slKey(CharCast(key.data()), key.size());
189
633k
    m_impl_batch->batch.Delete(slKey);
190
633k
}
191
192
size_t CDBBatch::ApproximateSize() const
193
30.3M
{
194
30.3M
    return m_impl_batch->batch.ApproximateSize();
195
30.3M
}
196
197
struct LevelDBContext {
198
    //! custom environment this database is using (may be nullptr in case of default environment)
199
    leveldb::Env* penv;
200
201
    //! database options used
202
    leveldb::Options options;
203
204
    //! options used when reading from the database
205
    leveldb::ReadOptions readoptions;
206
207
    //! options used when iterating over values of the database
208
    leveldb::ReadOptions iteroptions;
209
210
    //! options used when writing to the database
211
    leveldb::WriteOptions writeoptions;
212
213
    //! options used when sync writing to the database
214
    leveldb::WriteOptions syncoptions;
215
216
    //! the database itself
217
    leveldb::DB* pdb;
218
};
219
220
CDBWrapper::CDBWrapper(const DBParams& params)
221
81
    : m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}
222
81
{
223
81
    DBContext().penv = nullptr;
224
81
    DBContext().readoptions.verify_checksums = true;
225
81
    DBContext().iteroptions.verify_checksums = true;
226
81
    DBContext().iteroptions.fill_cache = false;
227
81
    DBContext().syncoptions.sync = true;
228
81
    DBContext().options = GetOptions(params.cache_bytes);
229
81
    DBContext().options.create_if_missing = true;
230
81
    DBContext().options.max_file_size = params.max_file_size;
231
81
    assert(!(params.testing_env && params.memory_only));
  Branch (231:5): [True: 0, False: 81]
  Branch (231:5): [True: 0, False: 0]
  Branch (231:5): [True: 81, False: 0]
232
81
    if (params.testing_env) {
  Branch (232:9): [True: 0, False: 81]
233
0
        DBContext().options.env = params.testing_env;
234
81
    } else if (params.memory_only) {
  Branch (234:16): [True: 0, False: 81]
235
0
        DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
236
0
        DBContext().options.env = DBContext().penv;
237
0
    }
238
81
    if (!params.memory_only) {
  Branch (238:9): [True: 81, False: 0]
239
81
        if (params.wipe_data) {
  Branch (239:13): [True: 0, False: 81]
240
0
            LogInfo("Wiping LevelDB in %s", fs::PathToString(params.path));
241
0
            leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
242
0
            HandleError(result);
243
0
        }
244
81
        if (!params.testing_env) {
  Branch (244:13): [True: 81, False: 0]
245
81
            TryCreateDirectories(params.path);
246
81
        }
247
81
        LogInfo("Opening LevelDB in %s", fs::PathToString(params.path));
248
81
    }
249
    // PathToString() return value is safe to pass to leveldb open function,
250
    // because on POSIX leveldb passes the byte string directly to ::open(), and
251
    // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
252
    // (see env_posix.cc and env_windows.cc).
253
81
    leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
254
81
    HandleError(status);
255
81
    LogInfo("Opened LevelDB successfully");
256
257
81
    if (params.options.force_compact) {
  Branch (257:9): [True: 0, False: 81]
258
0
        LogInfo("Starting database compaction of %s", fs::PathToString(params.path));
259
0
        CompactFull();
260
0
        LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
261
0
    }
262
263
81
    if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
  Branch (263:9): [True: 81, False: 0]
  Branch (263:50): [True: 27, False: 54]
  Branch (263:70): [True: 27, False: 0]
264
        // Generate and write the new obfuscation key.
265
27
        const Obfuscation obfuscation{FastRandomContext{}.randbytes<Obfuscation::KEY_SIZE>()};
266
27
        assert(!m_obfuscation); // Make sure the key is written without obfuscation.
  Branch (266:9): [True: 27, False: 0]
267
27
        Write(OBFUSCATION_KEY, obfuscation);
268
27
        m_obfuscation = obfuscation;
269
27
        LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
270
27
    }
271
81
    LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
272
81
}
273
274
CDBWrapper::~CDBWrapper()
275
450k
{
276
450k
    delete DBContext().pdb;
277
450k
    DBContext().pdb = nullptr;
278
450k
    delete DBContext().options.filter_policy;
279
450k
    DBContext().options.filter_policy = nullptr;
280
450k
    delete DBContext().options.info_log;
281
450k
    DBContext().options.info_log = nullptr;
282
450k
    delete DBContext().options.block_cache;
283
450k
    DBContext().options.block_cache = nullptr;
284
450k
    delete DBContext().penv;
285
450k
    DBContext().options.env = nullptr;
286
450k
}
287
288
void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
289
831k
{
290
831k
    const bool log_memory = util::log::ShouldDebugLog(BCLog::LEVELDB);
291
831k
    double mem_before = 0;
292
831k
    if (log_memory) {
  Branch (292:9): [True: 0, False: 831k]
293
0
        mem_before = DynamicMemoryUsage() / double(1_MiB);
294
0
    }
295
831k
    leveldb::Status status = DBContext().pdb->Write(fSync ? DBContext().syncoptions : DBContext().writeoptions, &batch.m_impl_batch->batch);
  Branch (295:53): [True: 305k, False: 525k]
296
831k
    HandleError(status);
297
831k
    if (log_memory) {
  Branch (297:9): [True: 0, False: 831k]
298
0
        double mem_after{DynamicMemoryUsage() / double(1_MiB)};
299
0
        LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
300
0
                 m_name, mem_before, mem_after);
301
0
    }
302
831k
}
303
304
std::optional<std::string> CDBWrapper::GetProperty(const std::string& property) const
305
0
{
306
0
    if (std::string value; DBContext().pdb->GetProperty(property, &value)) return value;
  Branch (306:28): [True: 0, False: 0]
307
0
    return std::nullopt;
308
0
}
309
310
16
void CDBWrapper::CompactFull() { DBContext().pdb->CompactRange(nullptr, nullptr); }
311
312
size_t CDBWrapper::DynamicMemoryUsage() const
313
0
{
314
0
    std::optional<size_t> parsed;
315
0
    if (auto memory{GetProperty("leveldb.approximate-memory-usage")}; !memory || !(parsed = ToIntegral<size_t>(*memory))) {
  Branch (315:71): [True: 0, False: 0]
  Branch (315:71): [True: 0, False: 0]
  Branch (315:82): [True: 0, False: 0]
316
0
        LogDebug(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
317
0
        return 0;
318
0
    }
319
0
    return parsed.value();
320
0
}
321
322
std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
323
2.52M
{
324
2.52M
    leveldb::Slice slKey(CharCast(key.data()), key.size());
325
2.52M
    std::string strValue;
326
2.52M
    leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
327
2.52M
    if (!status.ok()) {
  Branch (327:9): [True: 2.34M, False: 178k]
328
2.34M
        if (status.IsNotFound())
  Branch (328:13): [True: 2.34M, False: 18.4E]
329
2.34M
            return std::nullopt;
330
18.4E
        LogError("LevelDB read failure: %s", status.ToString());
331
18.4E
        HandleError(status);
332
18.4E
    }
333
177k
    return strValue;
334
2.52M
}
335
336
bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
337
54
{
338
54
    leveldb::Slice slKey(CharCast(key.data()), key.size());
339
340
54
    std::string strValue;
341
54
    leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
342
54
    if (!status.ok()) {
  Branch (342:9): [True: 54, False: 0]
343
54
        if (status.IsNotFound())
  Branch (343:13): [True: 54, False: 0]
344
54
            return false;
345
0
        LogError("LevelDB read failure: %s", status.ToString());
346
0
        HandleError(status);
347
0
    }
348
0
    return true;
349
54
}
350
351
size_t CDBWrapper::EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const
352
0
{
353
0
    leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
354
0
    leveldb::Slice slKey2(CharCast(key2.data()), key2.size());
355
0
    uint64_t size = 0;
356
0
    leveldb::Range range(slKey1, slKey2);
357
0
    DBContext().pdb->GetApproximateSizes(&range, 1, &size);
358
0
    return size;
359
0
}
360
361
bool CDBWrapper::IsEmpty()
362
27
{
363
27
    std::unique_ptr<CDBIterator> it(NewIterator());
364
27
    it->SeekToFirst();
365
27
    return !(it->Valid());
366
27
}
367
368
struct CDBIterator::IteratorImpl {
369
    const std::unique_ptr<leveldb::Iterator> iter;
370
371
24.8k
    explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
372
};
373
374
24.8k
CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent),
375
24.8k
                                                                                            m_impl_iter(std::move(_piter))
376
24.8k
{
377
24.8k
    m_scratch.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
378
24.8k
}
379
380
CDBIterator* CDBWrapper::NewIterator()
381
24.8k
{
382
24.8k
    return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(DBContext().pdb->NewIterator(DBContext().iteroptions))};
383
24.8k
}
384
385
void CDBIterator::SeekImpl(std::span<const std::byte> key)
386
24.8k
{
387
24.8k
    leveldb::Slice slKey(CharCast(key.data()), key.size());
388
24.8k
    m_impl_iter->iter->Seek(slKey);
389
24.8k
}
390
391
std::span<const std::byte> CDBIterator::GetKeyImpl() const
392
369k
{
393
    // The returned span borrows from the current iterator entry and is only
394
    // valid until the iterator is advanced.
395
369k
    return MakeByteSpan(m_impl_iter->iter->key());
396
369k
}
397
398
std::span<const std::byte> CDBIterator::GetValueImpl() const
399
369k
{
400
369k
    return MakeByteSpan(m_impl_iter->iter->value());
401
369k
}
402
403
24.8k
CDBIterator::~CDBIterator() = default;
404
347k
bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
405
27
void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
406
347k
void CDBIterator::Next() { m_impl_iter->iter->Next(); }
407
408
namespace dbwrapper_private {
409
410
const Obfuscation& GetObfuscation(const CDBWrapper& w)
411
62.1M
{
412
62.1M
    return w.m_obfuscation;
413
62.1M
}
414
415
} // namespace dbwrapper_private