Fix ZMQ Notification initialization and shutdown
[bitcoinplatinum.git] / src / dbwrapper.cpp
blobb6307cf0bf37a98c76e5be6b97c4658de894b222
1 // Copyright (c) 2012-2014 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.
5 #include "dbwrapper.h"
7 #include "util.h"
8 #include "random.h"
10 #include <boost/filesystem.hpp>
12 #include <leveldb/cache.h>
13 #include <leveldb/env.h>
14 #include <leveldb/filter_policy.h>
15 #include <memenv.h>
16 #include <stdint.h>
18 void HandleError(const leveldb::Status& status) throw(dbwrapper_error)
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");
32 static leveldb::Options GetOptions(size_t nCacheSize)
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;
45 return options;
48 CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
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);
64 HandleError(result);
66 TryCreateDirectory(path);
67 LogPrintf("Opening LevelDB in %s\n", path.string());
69 leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
70 HandleError(status);
71 LogPrintf("Opened LevelDB successfully\n");
73 // The base-case obfuscation key, which is a noop.
74 obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');
76 bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
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();
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;
87 LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), GetObfuscateKeyHex());
90 LogPrintf("Using obfuscation key for %s: %s\n", path.string(), GetObfuscateKeyHex());
93 CDBWrapper::~CDBWrapper()
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;
105 bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) throw(dbwrapper_error)
107 leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
108 HandleError(status);
109 return true;
112 // Prefixed with null character to avoid collisions with other keys
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);
118 const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
121 * Returns a string (consisting of 8 random bytes) suitable for use as an
122 * obfuscating XOR key.
124 std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
126 unsigned char buff[OBFUSCATE_KEY_NUM_BYTES];
127 GetRandBytes(buff, OBFUSCATE_KEY_NUM_BYTES);
128 return std::vector<unsigned char>(&buff[0], &buff[OBFUSCATE_KEY_NUM_BYTES]);
132 bool CDBWrapper::IsEmpty()
134 boost::scoped_ptr<CDBIterator> it(NewIterator());
135 it->SeekToFirst();
136 return !(it->Valid());
139 const std::vector<unsigned char>& CDBWrapper::GetObfuscateKey() const
141 return obfuscate_key;
144 std::string CDBWrapper::GetObfuscateKeyHex() const
146 return HexStr(obfuscate_key);
149 CDBIterator::~CDBIterator() { delete piter; }
150 bool CDBIterator::Valid() { return piter->Valid(); }
151 void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
152 void CDBIterator::Next() { piter->Next(); }