Dash Core  0.12.2.1
P2P Digital Currency
dbwrapper.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2015 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 void HandleError(const leveldb::Status& status) throw(dbwrapper_error)
19 {
20  if (status.ok())
21  return;
22  LogPrintf("%s\n", status.ToString());
23  if (status.IsCorruption())
24  throw dbwrapper_error("Database corrupted");
25  if (status.IsIOError())
26  throw dbwrapper_error("Database I/O error");
27  if (status.IsNotFound())
28  throw dbwrapper_error("Database entry missing");
29  throw dbwrapper_error("Unknown database error");
30 }
31 
32 static leveldb::Options GetOptions(size_t nCacheSize)
33 {
34  leveldb::Options options;
35  options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
36  options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
37  options.filter_policy = leveldb::NewBloomFilterPolicy(10);
38  options.compression = leveldb::kNoCompression;
39  options.max_open_files = 64;
40  if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
41  // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
42  // on corruption in later versions.
43  options.paranoid_checks = true;
44  }
45  return options;
46 }
47 
48 CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
49 {
50  penv = NULL;
51  readoptions.verify_checksums = true;
52  iteroptions.verify_checksums = true;
53  iteroptions.fill_cache = false;
54  syncoptions.sync = true;
55  options = GetOptions(nCacheSize);
56  options.create_if_missing = true;
57  if (fMemory) {
58  penv = leveldb::NewMemEnv(leveldb::Env::Default());
59  options.env = penv;
60  } else {
61  if (fWipe) {
62  LogPrintf("Wiping LevelDB in %s\n", path.string());
63  leveldb::Status result = leveldb::DestroyDB(path.string(), options);
65  }
66  TryCreateDirectory(path);
67  LogPrintf("Opening LevelDB in %s\n", path.string());
68  }
69  leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
70  HandleError(status);
71  LogPrintf("Opened LevelDB successfully\n");
72 
73  // The base-case obfuscation key, which is a noop.
74  obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');
75 
76  bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
77 
78  if (!key_exists && obfuscate && IsEmpty()) {
79  // Initialize non-degenerate obfuscation if it won't upset
80  // existing, non-obfuscated data.
81  std::vector<unsigned char> new_key = CreateObfuscateKey();
82 
83  // Write `new_key` so we don't obfuscate the key with itself
84  Write(OBFUSCATE_KEY_KEY, new_key);
85  obfuscate_key = new_key;
86 
87  LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), GetObfuscateKeyHex());
88  }
89 
90  LogPrintf("Using obfuscation key for %s: %s\n", path.string(), GetObfuscateKeyHex());
91 }
92 
94 {
95  delete pdb;
96  pdb = NULL;
97  delete options.filter_policy;
98  options.filter_policy = NULL;
99  delete options.block_cache;
100  options.block_cache = NULL;
101  delete penv;
102  options.env = NULL;
103 }
104 
105 bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) throw(dbwrapper_error)
106 {
107  leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
108  HandleError(status);
109  return true;
110 }
111 
112 // Prefixed with null character to avoid collisions with other keys
113 //
114 // We must use a string constructor which specifies length so that we copy
115 // past the null-terminator.
116 const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
117 
118 const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
119 
124 std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
125 {
126  unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
128  return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
129 
130 }
131 
133 {
134  boost::scoped_ptr<CDBIterator> it(NewIterator());
135  it->SeekToFirst();
136  return !(it->Valid());
137 }
138 
139 const std::vector<unsigned char>& CDBWrapper::GetObfuscateKey() const
140 {
141  return obfuscate_key;
142 }
143 
145 {
146  return HexStr(obfuscate_key);
147 }
148 
150 bool CDBIterator::Valid() { return piter->Valid(); }
151 void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
152 void CDBIterator::Next() { piter->Next(); }
leveldb::Env * penv
custom environment this database is using (may be NULL in case of default environment) ...
Definition: dbwrapper.h:139
static const unsigned int OBFUSCATE_KEY_NUM_BYTES
the length of the obfuscate key in number of bytes
Definition: dbwrapper.h:166
bool Valid()
Definition: dbwrapper.cpp:150
void Next()
Definition: dbwrapper.cpp:152
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
static const std::string OBFUSCATE_KEY_KEY
the key under which the obfuscation key is stored
Definition: dbwrapper.h:163
leveldb::DB * pdb
the database itself
Definition: dbwrapper.h:157
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:209
leveldb::Options options
database options used
Definition: dbwrapper.h:142
#define LogPrintf(...)
Definition: util.h:98
std::string GetObfuscateKeyHex() const
Definition: dbwrapper.cpp:144
CDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false, bool obfuscate=false)
Definition: dbwrapper.cpp:48
void HandleError(const leveldb::Status &status)
Definition: dbwrapper.cpp:18
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:105
bool TryCreateDirectory(const boost::filesystem::path &p)
Definition: util.cpp:691
leveldb::WriteOptions syncoptions
options used when sync writing to the database
Definition: dbwrapper.h:154
leveldb::ReadOptions readoptions
options used when reading from the database
Definition: dbwrapper.h:145
leveldb::ReadOptions iteroptions
options used when iterating over values of the database
Definition: dbwrapper.h:148
void GetRandBytes(unsigned char *buf, int num)
Definition: random.cpp:86
CDBIterator * NewIterator()
Definition: dbwrapper.h:257
const std::vector< unsigned char > & GetObfuscateKey() const
Definition: dbwrapper.cpp:139
bool IsEmpty()
Definition: dbwrapper.cpp:132
std::vector< unsigned char > CreateObfuscateKey() const
Definition: dbwrapper.cpp:124
static leveldb::Options GetOptions(size_t nCacheSize)
Definition: dbwrapper.cpp:32
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:183
void SeekToFirst()
Definition: dbwrapper.cpp:151
result
Definition: rpcuser.py:37
leveldb::Iterator * piter
Definition: dbwrapper.h:75
std::vector< unsigned char > obfuscate_key
a key used for optional XOR-obfuscation of the database
Definition: dbwrapper.h:160