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.
10 #include <boost/filesystem.hpp>
12 #include <leveldb/cache.h>
13 #include <leveldb/env.h>
14 #include <leveldb/filter_policy.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;
34 CDBWrapper::CDBWrapper(const boost::filesystem::path
& path
, size_t nCacheSize
, bool fMemory
, bool fWipe
, bool obfuscate
)
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;
44 penv
= leveldb::NewMemEnv(leveldb::Env::Default());
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()
83 delete options
.filter_policy
;
84 options
.filter_policy
= NULL
;
85 delete options
.block_cache
;
86 options
.block_cache
= 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
);
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());
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
)
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
;