Dogecoin Core  1.14.2
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 "util.h"
8 #include "random.h"
9 
10 #include <boost/filesystem.hpp>
11 
12 #include <leveldb/cache.h>
13 #include <leveldb/env.h>
14 #include <leveldb/filter_policy.h>
15 #include <memenv.h>
16 #include <stdint.h>
17 
18 static leveldb::Options GetOptions(size_t nCacheSize)
19 {
20  leveldb::Options options;
21  options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
22  options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
23  options.filter_policy = leveldb::NewBloomFilterPolicy(10);
24  options.compression = leveldb::kNoCompression;
25  options.max_open_files = 64;
26  if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
27  // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
28  // on corruption in later versions.
29  options.paranoid_checks = true;
30  }
31  return options;
32 }
33 
34 CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
35 {
36  penv = NULL;
37  readoptions.verify_checksums = true;
38  iteroptions.verify_checksums = true;
39  iteroptions.fill_cache = false;
40  syncoptions.sync = true;
41  options = GetOptions(nCacheSize);
42  options.create_if_missing = true;
43  if (fMemory) {
44  penv = leveldb::NewMemEnv(leveldb::Env::Default());
45  options.env = penv;
46  } else {
47  if (fWipe) {
48  LogPrintf("Wiping LevelDB in %s\n", path.string());
49  leveldb::Status result = leveldb::DestroyDB(path.string(), options);
51  }
52  TryCreateDirectory(path);
53  LogPrintf("Opening LevelDB in %s\n", path.string());
54  }
55  leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
57  LogPrintf("Opened LevelDB successfully\n");
58 
59  // The base-case obfuscation key, which is a noop.
60  obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');
61 
62  bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
63 
64  if (!key_exists && obfuscate && IsEmpty()) {
65  // Initialize non-degenerate obfuscation if it won't upset
66  // existing, non-obfuscated data.
67  std::vector<unsigned char> new_key = CreateObfuscateKey();
68 
69  // Write `new_key` so we don't obfuscate the key with itself
70  Write(OBFUSCATE_KEY_KEY, new_key);
71  obfuscate_key = new_key;
72 
73  LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), HexStr(obfuscate_key));
74  }
75 
76  LogPrintf("Using obfuscation key for %s: %s\n", path.string(), HexStr(obfuscate_key));
77 }
78 
80 {
81  delete pdb;
82  pdb = NULL;
83  delete options.filter_policy;
84  options.filter_policy = NULL;
85  delete options.block_cache;
86  options.block_cache = NULL;
87  delete penv;
88  options.env = NULL;
89 }
90 
91 bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
92 {
93  leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
95  return true;
96 }
97 
98 // Prefixed with null character to avoid collisions with other keys
99 //
100 // We must use a string constructor which specifies length so that we copy
101 // past the null-terminator.
102 const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
103 
104 const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
105 
110 std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
111 {
112  unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
114  return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
115 
116 }
117 
119 {
120  std::unique_ptr<CDBIterator> it(NewIterator());
121  it->SeekToFirst();
122  return !(it->Valid());
123 }
124 
126 bool CDBIterator::Valid() { return piter->Valid(); }
127 void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
128 void CDBIterator::Next() { piter->Next(); }
129 
130 namespace dbwrapper_private {
131 
132 void HandleError(const leveldb::Status& status)
133 {
134  if (status.ok())
135  return;
136  LogPrintf("%s\n", status.ToString());
137  if (status.IsCorruption())
138  throw dbwrapper_error("Database corrupted");
139  if (status.IsIOError())
140  throw dbwrapper_error("Database I/O error");
141  if (status.IsNotFound())
142  throw dbwrapper_error("Database entry missing");
143  throw dbwrapper_error("Unknown database error");
144 }
145 
146 const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
147 {
148  return w.obfuscate_key;
149 }
150 
151 };
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:49
leveldb::WriteBatch batch
Definition: dbwrapper.h:54
bool Valid()
Definition: dbwrapper.cpp:126
leveldb::Iterator * piter
Definition: dbwrapper.h:98
void SeekToFirst()
Definition: dbwrapper.cpp:127
void Next()
Definition: dbwrapper.cpp:128
CDBIterator * NewIterator()
Definition: dbwrapper.h:280
CDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
Definition: dbwrapper.cpp:34
leveldb::Env * penv
custom environment this database is using (may be NULL in case of default environment)
Definition: dbwrapper.h:162
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:91
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:206
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:232
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
Definition: dbwrapper.h:183
leveldb::Options options
database options used
Definition: dbwrapper.h:165
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
Definition: dbwrapper.h:189
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
Definition: dbwrapper.h:186
leveldb::WriteOptions writeoptions
options used when writing to the database
Definition: dbwrapper.h:174
leveldb::WriteOptions syncoptions
options used when sync writing to the database
Definition: dbwrapper.h:177
leveldb::DB * pdb
the database itself
Definition: dbwrapper.h:180
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:110
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
Definition: dbwrapper.h:171
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:118
leveldb::ReadOptions readoptions
options used when reading from the database
Definition: dbwrapper.h:168
These should be considered an implementation detail of the specific database.
Definition: dbwrapper.cpp:130
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
Definition: dbwrapper.cpp:132
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Definition: dbwrapper.cpp:146
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:125
bool TryCreateDirectory(const boost::filesystem::path &p)
Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
Definition: util.cpp:621
#define LogPrintf(...)
Definition: util.h:82
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)