Bitcoin ABC 0.30.5
P2P Digital Currency
dbwrapper.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-2016 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 <random.h>
8#include <util/fs_helpers.h>
9
10#include <leveldb/cache.h>
11#include <leveldb/env.h>
12#include <leveldb/filter_policy.h>
13#include <memenv.h>
14
15#include <algorithm>
16#include <cstdint>
17#include <memory>
18
19class CBitcoinLevelDBLogger : public leveldb::Logger {
20public:
21 // This code is adapted from posix_logger.h, which is why it is using
22 // vsprintf.
23 // Please do not do this in normal code
24 void Logv(const char *format, va_list ap) override {
26 return;
27 }
28 char buffer[500];
29 for (int iter = 0; iter < 2; iter++) {
30 char *base;
31 int bufsize;
32 if (iter == 0) {
33 bufsize = sizeof(buffer);
34 base = buffer;
35 } else {
36 bufsize = 30000;
37 base = new char[bufsize];
38 }
39 char *p = base;
40 char *limit = base + bufsize;
41
42 // Print the message
43 if (p < limit) {
44 va_list backup_ap;
45 va_copy(backup_ap, ap);
46 // Do not use vsnprintf elsewhere in bitcoin source code, see
47 // above.
48 p += vsnprintf(p, limit - p, format, backup_ap);
49 va_end(backup_ap);
50 }
51
52 // Truncate to available space if necessary
53 if (p >= limit) {
54 if (iter == 0) {
55 continue; // Try again with larger buffer
56 } else {
57 p = limit - 1;
58 }
59 }
60
61 // Add newline if necessary
62 if (p == base || p[-1] != '\n') {
63 *p++ = '\n';
64 }
65
66 assert(p <= limit);
67 base[std::min(bufsize - 1, (int)(p - base))] = '\0';
68 LogPrintfToBeContinued("leveldb: %s", base);
69 if (base != buffer) {
70 delete[] base;
71 }
72 break;
73 }
74 }
75};
76
77static void SetMaxOpenFiles(leveldb::Options *options) {
78 // On most platforms the default setting of max_open_files (which is 1000)
79 // is optimal. On Windows using a large file count is OK because the handles
80 // do not interfere with select() loops. On 64-bit Unix hosts this value is
81 // also OK, because up to that amount LevelDB will use an mmap
82 // implementation that does not use extra file descriptors (the fds are
83 // closed after being mmaped).
84 //
85 // Increasing the value beyond the default is dangerous because LevelDB will
86 // fall back to a non-mmap implementation when the file count is too large.
87 // On 32-bit Unix host we should decrease the value because the handles use
88 // up real fds, and we want to avoid fd exhaustion issues.
89 //
90 // See PR #12495 for further discussion.
91
92 int default_open_files = options->max_open_files;
93#ifndef WIN32
94 if (sizeof(void *) < 8) {
95 options->max_open_files = 64;
96 }
97#endif
98 LogPrint(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
99 options->max_open_files, default_open_files);
100}
101
102static leveldb::Options GetOptions(size_t nCacheSize) {
103 leveldb::Options options;
104 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
105 // up to two write buffers may be held in memory simultaneously
106 options.write_buffer_size = nCacheSize / 4;
107 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
108 options.compression = leveldb::kNoCompression;
109 options.info_log = new CBitcoinLevelDBLogger();
110 if (leveldb::kMajorVersion > 1 ||
111 (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
112 // LevelDB versions before 1.16 consider short writes to be corruption.
113 // Only trigger error on corruption in later versions.
114 options.paranoid_checks = true;
115 }
116 SetMaxOpenFiles(&options);
117 return options;
118}
119
121 : m_name{fs::PathToString(params.path.stem())}, m_path{params.path},
122 m_is_memory{params.memory_only} {
123 penv = nullptr;
124 readoptions.verify_checksums = true;
125 iteroptions.verify_checksums = true;
126 iteroptions.fill_cache = false;
127 syncoptions.sync = true;
129 options.create_if_missing = true;
130 if (params.memory_only) {
131 penv = leveldb::NewMemEnv(leveldb::Env::Default());
132 options.env = penv;
133 } else {
134 if (params.wipe_data) {
135 LogPrintf("Wiping LevelDB in %s\n", fs::PathToString(params.path));
136 leveldb::Status result =
137 leveldb::DestroyDB(fs::PathToString(params.path), options);
139 }
141 LogPrintf("Opening LevelDB in %s\n", fs::PathToString(params.path));
142 }
143 // PathToString() return value is safe to pass to leveldb open function,
144 // because on POSIX leveldb passes the byte string directly to ::open(), and
145 // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
146 // (see env_posix.cc and env_windows.cc).
147 leveldb::Status status =
148 leveldb::DB::Open(options, fs::PathToString(params.path), &pdb);
150 LogPrintf("Opened LevelDB successfully\n");
151
152 if (params.options.force_compact) {
153 LogPrintf("Starting database compaction of %s\n",
154 fs::PathToString(params.path));
155 pdb->CompactRange(nullptr, nullptr);
156 LogPrintf("Finished database compaction of %s\n",
157 fs::PathToString(params.path));
158 }
159
160 // The base-case obfuscation key, which is a noop.
161 obfuscate_key = std::vector<uint8_t>(OBFUSCATE_KEY_NUM_BYTES, '\000');
162
163 bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
164
165 if (!key_exists && params.obfuscate && IsEmpty()) {
166 // Initialize non-degenerate obfuscation if it won't upset existing,
167 // non-obfuscated data.
168 std::vector<uint8_t> new_key = CreateObfuscateKey();
169
170 // Write `new_key` so we don't obfuscate the key with itself
171 Write(OBFUSCATE_KEY_KEY, new_key);
172 obfuscate_key = new_key;
173
174 LogPrintf("Wrote new obfuscate key for %s: %s\n",
176 }
177
178 LogPrintf("Using obfuscation key for %s: %s\n",
180}
181
183 delete pdb;
184 pdb = nullptr;
185 delete options.filter_policy;
186 options.filter_policy = nullptr;
187 delete options.info_log;
188 options.info_log = nullptr;
189 delete options.block_cache;
190 options.block_cache = nullptr;
191 delete penv;
192 options.env = nullptr;
193}
194
195bool CDBWrapper::WriteBatch(CDBBatch &batch, bool fSync) {
196 const bool log_memory = LogAcceptCategory(BCLog::LEVELDB);
197 double mem_before = 0;
198 if (log_memory) {
199 mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
200 }
201 leveldb::Status status =
202 pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
204 if (log_memory) {
205 double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;
206 LogPrint(
208 "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
209 m_name, mem_before, mem_after);
210 }
211 return true;
212}
213
215 std::string memory;
216 if (!pdb->GetProperty("leveldb.approximate-memory-usage", &memory)) {
218 "Failed to get approximate-memory-usage property\n");
219 return 0;
220 }
221 return stoul(memory);
222}
223
224// Prefixed with null character to avoid collisions with other keys
225//
226// We must use a string constructor which specifies length so that we copy past
227// the null-terminator.
228const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
229
230const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
231
236std::vector<uint8_t> CDBWrapper::CreateObfuscateKey() const {
237 std::vector<uint8_t> ret(OBFUSCATE_KEY_NUM_BYTES);
238 GetRandBytes(ret);
239 return ret;
240}
241
243 std::unique_ptr<CDBIterator> it(NewIterator());
244 it->SeekToFirst();
245 return !(it->Valid());
246}
247
249 delete piter;
250}
251bool CDBIterator::Valid() const {
252 return piter->Valid();
253}
255 piter->SeekToFirst();
256}
258 piter->Next();
259}
260
262
263void HandleError(const leveldb::Status &status) {
264 if (status.ok()) {
265 return;
266 }
267 const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
268 LogPrintf("%s\n", errmsg);
269 LogPrintf("You can use -debug=leveldb to get more complete diagnostic "
270 "messages\n");
271 throw dbwrapper_error(errmsg);
272}
273
274const std::vector<uint8_t> &GetObfuscateKey(const CDBWrapper &w) {
275 return w.obfuscate_key;
276}
277}; // namespace dbwrapper_private
void Logv(const char *format, va_list ap) override
Definition: dbwrapper.cpp:24
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:78
leveldb::WriteBatch batch
Definition: dbwrapper.h:83
leveldb::Iterator * piter
Definition: dbwrapper.h:148
bool Valid() const
Definition: dbwrapper.cpp:251
void SeekToFirst()
Definition: dbwrapper.cpp:254
void Next()
Definition: dbwrapper.cpp:257
size_t DynamicMemoryUsage() const
Definition: dbwrapper.cpp:214
leveldb::Env * penv
custom environment this database is using (may be nullptr in case of default environment)
Definition: dbwrapper.h:207
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:195
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:254
std::vector< uint8_t > CreateObfuscateKey() const
Returns a string (consisting of 8 random bytes) suitable for use as an obfuscating XOR key.
Definition: dbwrapper.cpp:236
CDBIterator * NewIterator()
Definition: dbwrapper.h:320
std::string m_name
the name of this database
Definition: dbwrapper.h:228
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:279
std::vector< uint8_t > obfuscate_key
a key used for optional XOR-obfuscation of the database
Definition: dbwrapper.h:231
CDBWrapper(const DBParams &params)
Definition: dbwrapper.cpp:120
leveldb::Options options
database options used
Definition: dbwrapper.h:210
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
Definition: dbwrapper.h:237
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
Definition: dbwrapper.h:234
leveldb::WriteOptions writeoptions
options used when writing to the database
Definition: dbwrapper.h:219
leveldb::WriteOptions syncoptions
options used when sync writing to the database
Definition: dbwrapper.h:222
leveldb::DB * pdb
the database itself
Definition: dbwrapper.h:225
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
Definition: dbwrapper.h:216
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:242
leveldb::ReadOptions readoptions
options used when reading from the database
Definition: dbwrapper.h:213
static leveldb::Options GetOptions(size_t nCacheSize)
Definition: dbwrapper.cpp:102
static void SetMaxOpenFiles(leveldb::Options *options)
Definition: dbwrapper.cpp:77
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
Definition: fs_helpers.cpp:287
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:177
#define LogPrint(category,...)
Definition: logging.h:211
#define LogPrintfToBeContinued
These are aliases used to explicitly state that the message should not end with a newline character.
Definition: logging.h:223
#define LogPrintf(...)
Definition: logging.h:207
@ LEVELDB
Definition: logging.h:60
These should be considered an implementation detail of the specific database.
Definition: dbwrapper.cpp:261
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
Definition: dbwrapper.cpp:263
const std::vector< uint8_t > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Definition: dbwrapper.cpp:274
Filesystem operations and types.
Definition: fs.h:20
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
void format(std::ostream &out, const char *fmt, const Args &...args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1112
void GetRandBytes(Span< uint8_t > bytes) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:639
bool force_compact
Compact database on startup.
Definition: dbwrapper.h:28
Application-specific storage settings.
Definition: dbwrapper.h:32
DBOptions options
Passed-through options.
Definition: dbwrapper.h:45
bool obfuscate
If true, store data obfuscated via simple XOR.
Definition: dbwrapper.h:43
bool wipe_data
If true, remove all existing data.
Definition: dbwrapper.h:40
size_t cache_bytes
Configures various leveldb cache settings.
Definition: dbwrapper.h:36
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:34
bool memory_only
If true, use leveldb's memory environment.
Definition: dbwrapper.h:38
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
assert(!tx.IsCoinBase())