[Wallet] Bugfix: FRT: don't terminate when keypool is empty
[bitcoinplatinum.git] / src / dbwrapper.cpp
blob4fa06135d286f6ea8447638c2fa714ddcfbf75dc
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.
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 static leveldb::Options GetOptions(size_t nCacheSize)
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;
31 return options;
34 CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
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);
52 TryCreateDirectory(path);
53 LogPrintf("Opening LevelDB in %s\n", path.string());
55 leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
56 dbwrapper_private::HandleError(status);
57 LogPrintf("Opened LevelDB successfully\n");
59 // The base-case obfuscation key, which is a noop.
60 obfuscate_key = std::vector<unsigned char>(OBFUSCATE_KEY_NUM_BYTES, '\000');
62 bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
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();
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;
73 LogPrintf("Wrote new obfuscate key for %s: %s\n", path.string(), HexStr(obfuscate_key));
76 LogPrintf("Using obfuscation key for %s: %s\n", path.string(), HexStr(obfuscate_key));
79 CDBWrapper::~CDBWrapper()
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;
91 bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
93 leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
94 dbwrapper_private::HandleError(status);
95 return true;
98 // Prefixed with null character to avoid collisions with other keys
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);
104 const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
107 * Returns a string (consisting of 8 random bytes) suitable for use as an
108 * obfuscating XOR key.
110 std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
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]);
118 bool CDBWrapper::IsEmpty()
120 std::unique_ptr<CDBIterator> it(NewIterator());
121 it->SeekToFirst();
122 return !(it->Valid());
125 CDBIterator::~CDBIterator() { delete piter; }
126 bool CDBIterator::Valid() { return piter->Valid(); }
127 void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
128 void CDBIterator::Next() { piter->Next(); }
130 namespace dbwrapper_private {
132 void HandleError(const leveldb::Status& status)
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");
146 const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
148 return w.obfuscate_key;