Coverage Report

Created: 2026-04-29 19:21

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