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
GetOptions(size_t nCacheSize)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
CDBWrapper(const boost::filesystem::path & path,size_t nCacheSize,bool fMemory,bool fWipe,bool obfuscate)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);
50 dbwrapper_private::HandleError(result);
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);
56 dbwrapper_private::HandleError(status);
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
~CDBWrapper()79 CDBWrapper::~CDBWrapper()
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
WriteBatch(CDBBatch & batch,bool fSync)91 bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
92 {
93 leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
94 dbwrapper_private::HandleError(status);
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
106 /**
107 * Returns a string (consisting of 8 random bytes) suitable for use as an
108 * obfuscating XOR key.
109 */
CreateObfuscateKey() const110 std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
111 {
112 unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
113 GetRandBytes(buff, OBFUSCATE_KEY_NUM_BYTES);
114 return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
115
116 }
117
IsEmpty()118 bool CDBWrapper::IsEmpty()
119 {
120 boost::scoped_ptr<CDBIterator> it(NewIterator());
121 it->SeekToFirst();
122 return !(it->Valid());
123 }
124
~CDBIterator()125 CDBIterator::~CDBIterator() { delete piter; }
Valid()126 bool CDBIterator::Valid() { return piter->Valid(); }
SeekToFirst()127 void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
Next()128 void CDBIterator::Next() { piter->Next(); }
129
130 namespace dbwrapper_private {
131
HandleError(const leveldb::Status & status)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
GetObfuscateKey(const CDBWrapper & w)146 const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
147 {
148 return w.obfuscate_key;
149 }
150
151 };
152