Bitcoin Core  29.1.0
P2P Digital Currency
dbwrapper.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <dbwrapper.h>
6 
7 #include <logging.h>
8 #include <random.h>
9 #include <serialize.h>
10 #include <span.h>
11 #include <streams.h>
12 #include <util/fs.h>
13 #include <util/fs_helpers.h>
14 #include <util/strencodings.h>
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <cstdarg>
19 #include <cstdint>
20 #include <cstdio>
21 #include <leveldb/cache.h>
22 #include <leveldb/db.h>
23 #include <leveldb/env.h>
24 #include <leveldb/filter_policy.h>
25 #include <leveldb/helpers/memenv/memenv.h>
26 #include <leveldb/iterator.h>
27 #include <leveldb/options.h>
28 #include <leveldb/slice.h>
29 #include <leveldb/status.h>
30 #include <leveldb/write_batch.h>
31 #include <memory>
32 #include <optional>
33 #include <utility>
34 
35 static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
36 
37 bool DestroyDB(const std::string& path_str)
38 {
39  return leveldb::DestroyDB(path_str, {}).ok();
40 }
41 
44 static void HandleError(const leveldb::Status& status)
45 {
46  if (status.ok())
47  return;
48  const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
49  LogPrintf("%s\n", errmsg);
50  LogPrintf("You can use -debug=leveldb to get more complete diagnostic messages\n");
51  throw dbwrapper_error(errmsg);
52 }
53 
54 class CBitcoinLevelDBLogger : public leveldb::Logger {
55 public:
56  // This code is adapted from posix_logger.h, which is why it is using vsprintf.
57  // Please do not do this in normal code
58  void Logv(const char * format, va_list ap) override {
60  return;
61  }
62  char buffer[500];
63  for (int iter = 0; iter < 2; iter++) {
64  char* base;
65  int bufsize;
66  if (iter == 0) {
67  bufsize = sizeof(buffer);
68  base = buffer;
69  }
70  else {
71  bufsize = 30000;
72  base = new char[bufsize];
73  }
74  char* p = base;
75  char* limit = base + bufsize;
76 
77  // Print the message
78  if (p < limit) {
79  va_list backup_ap;
80  va_copy(backup_ap, ap);
81  // Do not use vsnprintf elsewhere in bitcoin source code, see above.
82  p += vsnprintf(p, limit - p, format, backup_ap);
83  va_end(backup_ap);
84  }
85 
86  // Truncate to available space if necessary
87  if (p >= limit) {
88  if (iter == 0) {
89  continue; // Try again with larger buffer
90  }
91  else {
92  p = limit - 1;
93  }
94  }
95 
96  // Add newline if necessary
97  if (p == base || p[-1] != '\n') {
98  *p++ = '\n';
99  }
100 
101  assert(p <= limit);
102  base[std::min(bufsize - 1, (int)(p - base))] = '\0';
103  LogDebug(BCLog::LEVELDB, "%s\n", util::RemoveSuffixView(base, "\n"));
104  if (base != buffer) {
105  delete[] base;
106  }
107  break;
108  }
109  }
110 };
111 
112 static void SetMaxOpenFiles(leveldb::Options *options) {
113  // On most platforms the default setting of max_open_files (which is 1000)
114  // is optimal. On Windows using a large file count is OK because the handles
115  // do not interfere with select() loops. On 64-bit Unix hosts this value is
116  // also OK, because up to that amount LevelDB will use an mmap
117  // implementation that does not use extra file descriptors (the fds are
118  // closed after being mmap'ed).
119  //
120  // Increasing the value beyond the default is dangerous because LevelDB will
121  // fall back to a non-mmap implementation when the file count is too large.
122  // On 32-bit Unix host we should decrease the value because the handles use
123  // up real fds, and we want to avoid fd exhaustion issues.
124  //
125  // See PR #12495 for further discussion.
126 
127  int default_open_files = options->max_open_files;
128 #ifndef WIN32
129  if (sizeof(void*) < 8) {
130  options->max_open_files = 64;
131  }
132 #endif
133  LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
134  options->max_open_files, default_open_files);
135 }
136 
137 static leveldb::Options GetOptions(size_t nCacheSize)
138 {
139  leveldb::Options options;
140  options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
141  options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
142  options.filter_policy = leveldb::NewBloomFilterPolicy(10);
143  options.compression = leveldb::kNoCompression;
144  options.info_log = new CBitcoinLevelDBLogger();
145  if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
146  // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
147  // on corruption in later versions.
148  options.paranoid_checks = true;
149  }
150  options.max_file_size = std::max(options.max_file_size, DBWRAPPER_MAX_FILE_SIZE);
151  SetMaxOpenFiles(&options);
152  return options;
153 }
154 
156  leveldb::WriteBatch batch;
157 };
158 
160  : parent{_parent},
161  m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()} {};
162 
163 CDBBatch::~CDBBatch() = default;
164 
166 {
167  m_impl_batch->batch.Clear();
168  size_estimate = 0;
169 }
170 
172 {
173  leveldb::Slice slKey(CharCast(key.data()), key.size());
175  leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
176  m_impl_batch->batch.Put(slKey, slValue);
177  // LevelDB serializes writes as:
178  // - byte: header
179  // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
180  // - byte[]: key
181  // - varint: value length
182  // - byte[]: value
183  // The formula below assumes the key and value are both less than 16k.
184  size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
185 }
186 
188 {
189  leveldb::Slice slKey(CharCast(key.data()), key.size());
190  m_impl_batch->batch.Delete(slKey);
191  // LevelDB serializes erases as:
192  // - byte: header
193  // - varint: key length
194  // - byte[]: key
195  // The formula below assumes the key is less than 16kB.
196  size_estimate += 2 + (slKey.size() > 127) + slKey.size();
197 }
198 
201  leveldb::Env* penv;
202 
204  leveldb::Options options;
205 
207  leveldb::ReadOptions readoptions;
208 
210  leveldb::ReadOptions iteroptions;
211 
213  leveldb::WriteOptions writeoptions;
214 
216  leveldb::WriteOptions syncoptions;
217 
219  leveldb::DB* pdb;
220 };
221 
223  : m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
224 {
225  DBContext().penv = nullptr;
226  DBContext().readoptions.verify_checksums = true;
227  DBContext().iteroptions.verify_checksums = true;
228  DBContext().iteroptions.fill_cache = false;
229  DBContext().syncoptions.sync = true;
230  DBContext().options = GetOptions(params.cache_bytes);
231  DBContext().options.create_if_missing = true;
232  if (params.memory_only) {
233  DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
234  DBContext().options.env = DBContext().penv;
235  } else {
236  if (params.wipe_data) {
237  LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(params.path));
238  leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
240  }
241  TryCreateDirectories(params.path);
242  LogPrintf("Opening LevelDB in %s\n", fs::PathToString(params.path));
243  }
244  // PathToString() return value is safe to pass to leveldb open function,
245  // because on POSIX leveldb passes the byte string directly to ::open(), and
246  // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
247  // (see env_posix.cc and env_windows.cc).
248  leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
249  HandleError(status);
250  LogPrintf("Opened LevelDB successfully\n");
251 
252  if (params.options.force_compact) {
253  LogPrintf("Starting database compaction of %s\n", fs::PathToString(params.path));
254  DBContext().pdb->CompactRange(nullptr, nullptr);
255  LogPrintf("Finished database compaction of %s\n", fs::PathToString(params.path));
256  }
257 
258  // The base-case obfuscation key, which is a noop.
259  obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');
260 
261  bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
262 
263  if (!key_exists && params.obfuscate && IsEmpty()) {
264  // Initialize non-degenerate obfuscation if it won't upset
265  // existing, non-obfuscated data.
266  std::vector<unsigned char> new_key = CreateObfuscateKey();
267 
268  // Write `new_key` so we don't obfuscate the key with itself
269  Write(OBFUSCATE_KEY_KEY, new_key);
270  obfuscate_key = new_key;
271 
272  LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
273  }
274 
275  LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
276 }
277 
279 {
280  delete DBContext().pdb;
281  DBContext().pdb = nullptr;
282  delete DBContext().options.filter_policy;
283  DBContext().options.filter_policy = nullptr;
284  delete DBContext().options.info_log;
285  DBContext().options.info_log = nullptr;
286  delete DBContext().options.block_cache;
287  DBContext().options.block_cache = nullptr;
288  delete DBContext().penv;
289  DBContext().options.env = nullptr;
290 }
291 
292 bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
293 {
294  const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug);
295  double mem_before = 0;
296  if (log_memory) {
297  mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
298  }
299  leveldb::Status status = DBContext().pdb->Write(fSync ? DBContext().syncoptions : DBContext().writeoptions, &batch.m_impl_batch->batch);
300  HandleError(status);
301  if (log_memory) {
302  double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;
303  LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
304  m_name, mem_before, mem_after);
305  }
306  return true;
307 }
308 
310 {
311  std::string memory;
312  std::optional<size_t> parsed;
313  if (!DBContext().pdb->GetProperty("leveldb.approximate-memory-usage", &memory) || !(parsed = ToIntegral<size_t>(memory))) {
314  LogDebug(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
315  return 0;
316  }
317  return parsed.value();
318 }
319 
320 // Prefixed with null character to avoid collisions with other keys
321 //
322 // We must use a string constructor which specifies length so that we copy
323 // past the null-terminator.
324 const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
325 
326 const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
327 
332 std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
333 {
334  std::vector<uint8_t> ret(OBFUSCATE_KEY_NUM_BYTES);
335  GetRandBytes(ret);
336  return ret;
337 }
338 
339 std::optional<std::string> CDBWrapper::ReadImpl(Span<const std::byte> key) const
340 {
341  leveldb::Slice slKey(CharCast(key.data()), key.size());
342  std::string strValue;
343  leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
344  if (!status.ok()) {
345  if (status.IsNotFound())
346  return std::nullopt;
347  LogPrintf("LevelDB read failure: %s\n", status.ToString());
348  HandleError(status);
349  }
350  return strValue;
351 }
352 
354 {
355  leveldb::Slice slKey(CharCast(key.data()), key.size());
356 
357  std::string strValue;
358  leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
359  if (!status.ok()) {
360  if (status.IsNotFound())
361  return false;
362  LogPrintf("LevelDB read failure: %s\n", status.ToString());
363  HandleError(status);
364  }
365  return true;
366 }
367 
369 {
370  leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
371  leveldb::Slice slKey2(CharCast(key2.data()), key2.size());
372  uint64_t size = 0;
373  leveldb::Range range(slKey1, slKey2);
374  DBContext().pdb->GetApproximateSizes(&range, 1, &size);
375  return size;
376 }
377 
379 {
380  std::unique_ptr<CDBIterator> it(NewIterator());
381  it->SeekToFirst();
382  return !(it->Valid());
383 }
384 
386  const std::unique_ptr<leveldb::Iterator> iter;
387 
388  explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
389 };
390 
391 CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent),
392  m_impl_iter(std::move(_piter)) {}
393 
395 {
396  return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(DBContext().pdb->NewIterator(DBContext().iteroptions))};
397 }
398 
400 {
401  leveldb::Slice slKey(CharCast(key.data()), key.size());
402  m_impl_iter->iter->Seek(slKey);
403 }
404 
406 {
407  return MakeByteSpan(m_impl_iter->iter->key());
408 }
409 
411 {
412  return MakeByteSpan(m_impl_iter->iter->value());
413 }
414 
415 CDBIterator::~CDBIterator() = default;
416 bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
417 void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
418 void CDBIterator::Next() { m_impl_iter->iter->Next(); }
419 
420 namespace dbwrapper_private {
421 
422 const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
423 {
424  return w.obfuscate_key;
425 }
426 
427 } // namespace dbwrapper_private
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
Definition: fs_helpers.cpp:261
These should be considered an implementation detail of the specific database.
Definition: dbwrapper.cpp:420
int ret
void Clear()
Definition: dbwrapper.cpp:165
static const size_t DBWRAPPER_MAX_FILE_SIZE
Definition: dbwrapper.h:25
assert(!tx.IsCoinBase())
void SeekToFirst()
Definition: dbwrapper.cpp:417
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:73
bool DestroyDB(const std::string &path_str)
Definition: dbwrapper.cpp:37
auto & DBContext() const LIFETIMEBOUND
Definition: dbwrapper.h:212
leveldb::ReadOptions readoptions
options used when reading from the database
Definition: dbwrapper.cpp:207
Span< const std::byte > GetValueImpl() const
Definition: dbwrapper.cpp:410
size_t size_estimate
Definition: dbwrapper.h:86
Span< const std::byte > GetKeyImpl() const
Definition: dbwrapper.cpp:405
constexpr std::size_t size() const noexcept
Definition: span.h:187
std::string_view RemoveSuffixView(std::string_view str, std::string_view suffix)
Definition: string.h:161
const std::unique_ptr< IteratorImpl > m_impl_iter
Definition: dbwrapper.h:130
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
CDBIterator(const CDBWrapper &_parent, std::unique_ptr< IteratorImpl > _piter)
Definition: dbwrapper.cpp:391
std::vector< unsigned char > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key...
Definition: dbwrapper.cpp:332
value_type * data()
Definition: streams.h:188
size_t EstimateSizeImpl(Span< const std::byte > key1, Span< const std::byte > key2) const
Definition: dbwrapper.cpp:368
size_t DynamicMemoryUsage() const
Definition: dbwrapper.cpp:309
DataStream ssValue
Definition: dbwrapper.h:84
void Logv(const char *format, va_list ap) override
Definition: dbwrapper.cpp:58
const CDBWrapper & parent
Definition: dbwrapper.h:78
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
CDBWrapper(const DBParams &params)
Definition: dbwrapper.cpp:222
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
const std::unique_ptr< leveldb::Iterator > iter
Definition: dbwrapper.cpp:386
CDBIterator * NewIterator()
Definition: dbwrapper.cpp:394
leveldb::Options options
database options used
Definition: dbwrapper.cpp:204
leveldb::WriteOptions writeoptions
options used when writing to the database
Definition: dbwrapper.cpp:213
void EraseImpl(Span< const std::byte > key)
Definition: dbwrapper.cpp:187
leveldb::DB * pdb
the database itself
Definition: dbwrapper.cpp:219
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:378
size_type size() const
Definition: streams.h:181
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Definition: dbwrapper.cpp:422
void Next()
Definition: dbwrapper.cpp:418
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
Definition: dbwrapper.h:199
void SeekImpl(Span< const std::byte > key)
Definition: dbwrapper.cpp:399
std::string m_name
the name of this database
Definition: dbwrapper.h:190
auto result
Definition: common-types.h:74
#define LogDebug(category,...)
Definition: logging.h:381
static void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
Definition: dbwrapper.cpp:44
constexpr C * data() const noexcept
Definition: span.h:174
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
Definition: dbwrapper.h:196
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:276
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:269
void WriteImpl(Span< const std::byte > key, DataStream &ssValue)
Definition: dbwrapper.cpp:171
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
Application-specific storage settings.
Definition: dbwrapper.h:34
CDBBatch(const CDBWrapper &_parent)
Definition: dbwrapper.cpp:159
IteratorImpl(leveldb::Iterator *_iter)
Definition: dbwrapper.cpp:388
static void SetMaxOpenFiles(leveldb::Options *options)
Definition: dbwrapper.cpp:112
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
Definition: dbwrapper.cpp:210
bool Valid() const
Definition: dbwrapper.cpp:416
void GetRandBytes(Span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
Definition: random.cpp:603
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
Definition: logging.h:328
static auto CharCast(const std::byte *data)
Definition: dbwrapper.cpp:35
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:292
bool ExistsImpl(Span< const std::byte > key) const
Definition: dbwrapper.cpp:353
static leveldb::Options GetOptions(size_t nCacheSize)
Definition: dbwrapper.cpp:137
const std::unique_ptr< WriteBatchImpl > m_impl_batch
Definition: dbwrapper.h:80
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
Definition: dbwrapper.h:193
#define LogPrintf(...)
Definition: logging.h:361
std::optional< std::string > ReadImpl(Span< const std::byte > key) const
Definition: dbwrapper.cpp:339
leveldb::WriteBatch batch
Definition: dbwrapper.cpp:156
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment) ...
Definition: dbwrapper.cpp:201
leveldb::WriteOptions syncoptions
options used when sync writing to the database
Definition: dbwrapper.cpp:216