]>
Commit | Line | Data |
---|---|---|
0a61b0df | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
fa94b9d5 | 3 | // Distributed under the MIT software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 5 | |
a21df620 PK |
6 | #ifndef BITCOIN_WALLET_DB_H |
7 | #define BITCOIN_WALLET_DB_H | |
223b6f1b | 8 | |
71697f97 | 9 | #include "clientversion.h" |
336fe971 | 10 | #include "serialize.h" |
fa736190 | 11 | #include "streams.h" |
51ed9ec9 BD |
12 | #include "sync.h" |
13 | #include "version.h" | |
223b6f1b WL |
14 | |
15 | #include <map> | |
16 | #include <string> | |
17 | #include <vector> | |
18 | ||
51ed9ec9 | 19 | #include <boost/filesystem/path.hpp> |
611116d4 | 20 | |
223b6f1b | 21 | #include <db_cxx.h> |
0a61b0df | 22 | |
0a61b0df | 23 | extern unsigned int nWalletDBUpdated; |
0a61b0df | 24 | |
cd9696fc JG |
25 | class CDBEnv |
26 | { | |
27 | private: | |
cd9696fc | 28 | bool fDbEnvInit; |
148e107d | 29 | bool fMockDb; |
daf956b7 PK |
30 | // Don't change into boost::filesystem::path, as that can result in |
31 | // shutdown problems/crashes caused by a static initialized internal pointer. | |
32 | std::string strPath; | |
cd9696fc JG |
33 | |
34 | void EnvShutdown(); | |
35 | ||
36 | public: | |
37 | mutable CCriticalSection cs_db; | |
51598b26 | 38 | DbEnv *dbenv; |
ffe8b77a JG |
39 | std::map<std::string, int> mapFileUseCount; |
40 | std::map<std::string, Db*> mapDb; | |
cd9696fc JG |
41 | |
42 | CDBEnv(); | |
43 | ~CDBEnv(); | |
51598b26 PW |
44 | void Reset(); |
45 | ||
148e107d | 46 | void MakeMock(); |
c74bae0f | 47 | bool IsMock() { return fMockDb; } |
eed1785f | 48 | |
fa94b9d5 | 49 | /** |
eed1785f GA |
50 | * Verify that database file strFile is OK. If it is not, |
51 | * call the callback to try to recover. | |
52 | * This must be called BEFORE strFile is opened. | |
53 | * Returns true if strFile is OK. | |
54 | */ | |
20e01b1a PW |
55 | enum VerifyResult { VERIFY_OK, |
56 | RECOVER_OK, | |
57 | RECOVER_FAIL }; | |
341e2385 | 58 | VerifyResult Verify(const std::string& strFile, bool (*recoverFunc)(CDBEnv& dbenv, const std::string& strFile)); |
fa94b9d5 | 59 | /** |
eed1785f GA |
60 | * Salvage data from a file that Verify says is bad. |
61 | * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation). | |
62 | * Appends binary key/value pairs to vResult, returns true if successful. | |
63 | * NOTE: reads the entire database into memory, so cannot be used | |
64 | * for huge databases. | |
65 | */ | |
66 | typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair; | |
341e2385 | 67 | bool Salvage(const std::string& strFile, bool fAggressive, std::vector<KeyValPair>& vResult); |
eed1785f | 68 | |
20e01b1a | 69 | bool Open(const boost::filesystem::path& path); |
cd9696fc JG |
70 | void Close(); |
71 | void Flush(bool fShutdown); | |
22d7e701 | 72 | void CheckpointLSN(const std::string& strFile); |
24b57e3c | 73 | |
ffe8b77a | 74 | void CloseDb(const std::string& strFile); |
eed1785f | 75 | bool RemoveDb(const std::string& strFile); |
ffe8b77a | 76 | |
20e01b1a | 77 | DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC) |
24b57e3c JG |
78 | { |
79 | DbTxn* ptxn = NULL; | |
51598b26 | 80 | int ret = dbenv->txn_begin(NULL, &ptxn, flags); |
24b57e3c JG |
81 | if (!ptxn || ret != 0) |
82 | return NULL; | |
83 | return ptxn; | |
84 | } | |
cd9696fc JG |
85 | }; |
86 | ||
87 | extern CDBEnv bitdb; | |
88 | ||
89 | ||
6b8de05d | 90 | /** RAII class that provides access to a Berkeley database */ |
0a61b0df | 91 | class CDB |
92 | { | |
93 | protected: | |
94 | Db* pdb; | |
223b6f1b | 95 | std::string strFile; |
20e01b1a | 96 | DbTxn* activeTxn; |
0a61b0df | 97 | bool fReadOnly; |
44bc988e | 98 | bool fFlushOnClose; |
0a61b0df | 99 | |
44bc988e | 100 | explicit CDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnCloseIn=true); |
0a61b0df | 101 | ~CDB() { Close(); } |
22d7e701 | 102 | |
0a61b0df | 103 | public: |
ae8bfd12 | 104 | void Flush(); |
0a61b0df | 105 | void Close(); |
22d7e701 | 106 | |
0a61b0df | 107 | private: |
108 | CDB(const CDB&); | |
109 | void operator=(const CDB&); | |
110 | ||
111 | protected: | |
20e01b1a | 112 | template <typename K, typename T> |
0a61b0df | 113 | bool Read(const K& key, T& value) |
114 | { | |
115 | if (!pdb) | |
116 | return false; | |
117 | ||
118 | // Key | |
6b6aaa16 | 119 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 120 | ssKey.reserve(1000); |
121 | ssKey << key; | |
122 | Dbt datKey(&ssKey[0], ssKey.size()); | |
123 | ||
124 | // Read | |
125 | Dbt datValue; | |
126 | datValue.set_flags(DB_DBT_MALLOC); | |
8b1202c5 | 127 | int ret = pdb->get(activeTxn, &datKey, &datValue, 0); |
0a61b0df | 128 | memset(datKey.get_data(), 0, datKey.get_size()); |
129 | if (datValue.get_data() == NULL) | |
130 | return false; | |
131 | ||
132 | // Unserialize value | |
f94b64c2 JG |
133 | try { |
134 | CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION); | |
135 | ssValue >> value; | |
20e01b1a | 136 | } catch (const std::exception&) { |
f94b64c2 JG |
137 | return false; |
138 | } | |
0a61b0df | 139 | |
140 | // Clear and free memory | |
141 | memset(datValue.get_data(), 0, datValue.get_size()); | |
142 | free(datValue.get_data()); | |
143 | return (ret == 0); | |
144 | } | |
145 | ||
20e01b1a PW |
146 | template <typename K, typename T> |
147 | bool Write(const K& key, const T& value, bool fOverwrite = true) | |
0a61b0df | 148 | { |
149 | if (!pdb) | |
150 | return false; | |
151 | if (fReadOnly) | |
ecf1c79a | 152 | assert(!"Write called on database in read-only mode"); |
0a61b0df | 153 | |
154 | // Key | |
6b6aaa16 | 155 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 156 | ssKey.reserve(1000); |
157 | ssKey << key; | |
158 | Dbt datKey(&ssKey[0], ssKey.size()); | |
159 | ||
160 | // Value | |
6b6aaa16 | 161 | CDataStream ssValue(SER_DISK, CLIENT_VERSION); |
0a61b0df | 162 | ssValue.reserve(10000); |
163 | ssValue << value; | |
164 | Dbt datValue(&ssValue[0], ssValue.size()); | |
165 | ||
166 | // Write | |
8b1202c5 | 167 | int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE)); |
0a61b0df | 168 | |
169 | // Clear memory in case it was a private key | |
170 | memset(datKey.get_data(), 0, datKey.get_size()); | |
171 | memset(datValue.get_data(), 0, datValue.get_size()); | |
172 | return (ret == 0); | |
173 | } | |
174 | ||
20e01b1a | 175 | template <typename K> |
0a61b0df | 176 | bool Erase(const K& key) |
177 | { | |
178 | if (!pdb) | |
179 | return false; | |
180 | if (fReadOnly) | |
ecf1c79a | 181 | assert(!"Erase called on database in read-only mode"); |
0a61b0df | 182 | |
183 | // Key | |
6b6aaa16 | 184 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 185 | ssKey.reserve(1000); |
186 | ssKey << key; | |
187 | Dbt datKey(&ssKey[0], ssKey.size()); | |
188 | ||
189 | // Erase | |
8b1202c5 | 190 | int ret = pdb->del(activeTxn, &datKey, 0); |
0a61b0df | 191 | |
192 | // Clear memory | |
193 | memset(datKey.get_data(), 0, datKey.get_size()); | |
194 | return (ret == 0 || ret == DB_NOTFOUND); | |
195 | } | |
196 | ||
20e01b1a | 197 | template <typename K> |
0a61b0df | 198 | bool Exists(const K& key) |
199 | { | |
200 | if (!pdb) | |
201 | return false; | |
202 | ||
203 | // Key | |
6b6aaa16 | 204 | CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
0a61b0df | 205 | ssKey.reserve(1000); |
206 | ssKey << key; | |
207 | Dbt datKey(&ssKey[0], ssKey.size()); | |
208 | ||
209 | // Exists | |
8b1202c5 | 210 | int ret = pdb->exists(activeTxn, &datKey, 0); |
0a61b0df | 211 | |
212 | // Clear memory | |
213 | memset(datKey.get_data(), 0, datKey.get_size()); | |
214 | return (ret == 0); | |
215 | } | |
216 | ||
217 | Dbc* GetCursor() | |
218 | { | |
219 | if (!pdb) | |
220 | return NULL; | |
221 | Dbc* pcursor = NULL; | |
222 | int ret = pdb->cursor(NULL, &pcursor, 0); | |
223 | if (ret != 0) | |
224 | return NULL; | |
225 | return pcursor; | |
226 | } | |
227 | ||
20e01b1a | 228 | int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags = DB_NEXT) |
0a61b0df | 229 | { |
230 | // Read at cursor | |
231 | Dbt datKey; | |
20e01b1a | 232 | if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) { |
0a61b0df | 233 | datKey.set_data(&ssKey[0]); |
234 | datKey.set_size(ssKey.size()); | |
235 | } | |
236 | Dbt datValue; | |
20e01b1a | 237 | if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE) { |
0a61b0df | 238 | datValue.set_data(&ssValue[0]); |
239 | datValue.set_size(ssValue.size()); | |
240 | } | |
241 | datKey.set_flags(DB_DBT_MALLOC); | |
242 | datValue.set_flags(DB_DBT_MALLOC); | |
243 | int ret = pcursor->get(&datKey, &datValue, fFlags); | |
244 | if (ret != 0) | |
245 | return ret; | |
246 | else if (datKey.get_data() == NULL || datValue.get_data() == NULL) | |
247 | return 99999; | |
248 | ||
249 | // Convert to streams | |
250 | ssKey.SetType(SER_DISK); | |
251 | ssKey.clear(); | |
252 | ssKey.write((char*)datKey.get_data(), datKey.get_size()); | |
253 | ssValue.SetType(SER_DISK); | |
254 | ssValue.clear(); | |
255 | ssValue.write((char*)datValue.get_data(), datValue.get_size()); | |
256 | ||
257 | // Clear and free memory | |
258 | memset(datKey.get_data(), 0, datKey.get_size()); | |
259 | memset(datValue.get_data(), 0, datValue.get_size()); | |
260 | free(datKey.get_data()); | |
261 | free(datValue.get_data()); | |
262 | return 0; | |
263 | } | |
264 | ||
0a61b0df | 265 | public: |
266 | bool TxnBegin() | |
267 | { | |
8b1202c5 | 268 | if (!pdb || activeTxn) |
0a61b0df | 269 | return false; |
8b1202c5 | 270 | DbTxn* ptxn = bitdb.TxnBegin(); |
24b57e3c | 271 | if (!ptxn) |
0a61b0df | 272 | return false; |
8b1202c5 | 273 | activeTxn = ptxn; |
0a61b0df | 274 | return true; |
275 | } | |
276 | ||
277 | bool TxnCommit() | |
278 | { | |
8b1202c5 | 279 | if (!pdb || !activeTxn) |
0a61b0df | 280 | return false; |
8b1202c5 JG |
281 | int ret = activeTxn->commit(0); |
282 | activeTxn = NULL; | |
0a61b0df | 283 | return (ret == 0); |
284 | } | |
285 | ||
286 | bool TxnAbort() | |
287 | { | |
8b1202c5 | 288 | if (!pdb || !activeTxn) |
0a61b0df | 289 | return false; |
8b1202c5 JG |
290 | int ret = activeTxn->abort(); |
291 | activeTxn = NULL; | |
0a61b0df | 292 | return (ret == 0); |
293 | } | |
294 | ||
295 | bool ReadVersion(int& nVersion) | |
296 | { | |
297 | nVersion = 0; | |
223b6f1b | 298 | return Read(std::string("version"), nVersion); |
0a61b0df | 299 | } |
300 | ||
301 | bool WriteVersion(int nVersion) | |
302 | { | |
223b6f1b | 303 | return Write(std::string("version"), nVersion); |
0a61b0df | 304 | } |
9e9869d0 | 305 | |
d764d916 | 306 | bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL); |
0a61b0df | 307 | }; |
308 | ||
a21df620 | 309 | #endif // BITCOIN_WALLET_DB_H |