Fix travis failing to fetch keys from the sks keyserver pool
[bitcoinplatinum.git] / src / wallet / db.h
blob19c54e314c918554a468241f7e8694de33c51f78
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_WALLET_DB_H
7 #define BITCOIN_WALLET_DB_H
9 #include "clientversion.h"
10 #include "serialize.h"
11 #include "streams.h"
12 #include "sync.h"
13 #include "version.h"
15 #include <map>
16 #include <string>
17 #include <vector>
19 #include <boost/filesystem/path.hpp>
21 #include <db_cxx.h>
23 static const unsigned int DEFAULT_WALLET_DBLOGSIZE = 100;
24 static const bool DEFAULT_WALLET_PRIVDB = true;
26 class CDBEnv
28 private:
29 bool fDbEnvInit;
30 bool fMockDb;
31 // Don't change into boost::filesystem::path, as that can result in
32 // shutdown problems/crashes caused by a static initialized internal pointer.
33 std::string strPath;
35 void EnvShutdown();
37 public:
38 mutable CCriticalSection cs_db;
39 DbEnv *dbenv;
40 std::map<std::string, int> mapFileUseCount;
41 std::map<std::string, Db*> mapDb;
43 CDBEnv();
44 ~CDBEnv();
45 void Reset();
47 void MakeMock();
48 bool IsMock() { return fMockDb; }
50 /**
51 * Verify that database file strFile is OK. If it is not,
52 * call the callback to try to recover.
53 * This must be called BEFORE strFile is opened.
54 * Returns true if strFile is OK.
56 enum VerifyResult { VERIFY_OK,
57 RECOVER_OK,
58 RECOVER_FAIL };
59 VerifyResult Verify(const std::string& strFile, bool (*recoverFunc)(const std::string& strFile));
60 /**
61 * Salvage data from a file that Verify says is bad.
62 * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
63 * Appends binary key/value pairs to vResult, returns true if successful.
64 * NOTE: reads the entire database into memory, so cannot be used
65 * for huge databases.
67 typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
68 bool Salvage(const std::string& strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
70 bool Open(const boost::filesystem::path& path);
71 void Close();
72 void Flush(bool fShutdown);
73 void CheckpointLSN(const std::string& strFile);
75 void CloseDb(const std::string& strFile);
76 bool RemoveDb(const std::string& strFile);
78 DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
80 DbTxn* ptxn = NULL;
81 int ret = dbenv->txn_begin(NULL, &ptxn, flags);
82 if (!ptxn || ret != 0)
83 return NULL;
84 return ptxn;
88 extern CDBEnv bitdb;
91 /** RAII class that provides access to a Berkeley database */
92 class CDB
94 protected:
95 Db* pdb;
96 std::string strFile;
97 DbTxn* activeTxn;
98 bool fReadOnly;
99 bool fFlushOnClose;
101 explicit CDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
102 ~CDB() { Close(); }
104 public:
105 void Flush();
106 void Close();
107 static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue));
109 /* flush the wallet passively (TRY_LOCK)
110 ideal to be called periodically */
111 static bool PeriodicFlush(std::string strFile);
112 /* verifies the database environment */
113 static bool VerifyEnvironment(const std::string& walletFile, const boost::filesystem::path& dataDir, std::string& errorStr);
114 /* verifies the database file */
115 static bool VerifyDatabaseFile(const std::string& walletFile, const boost::filesystem::path& dataDir, std::string& warningStr, std::string& errorStr, bool (*recoverFunc)(const std::string& strFile));
117 private:
118 CDB(const CDB&);
119 void operator=(const CDB&);
121 protected:
122 template <typename K, typename T>
123 bool Read(const K& key, T& value)
125 if (!pdb)
126 return false;
128 // Key
129 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
130 ssKey.reserve(1000);
131 ssKey << key;
132 Dbt datKey(ssKey.data(), ssKey.size());
134 // Read
135 Dbt datValue;
136 datValue.set_flags(DB_DBT_MALLOC);
137 int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
138 memset(datKey.get_data(), 0, datKey.get_size());
139 if (datValue.get_data() == NULL)
140 return false;
142 // Unserialize value
143 try {
144 CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
145 ssValue >> value;
146 } catch (const std::exception&) {
147 return false;
150 // Clear and free memory
151 memset(datValue.get_data(), 0, datValue.get_size());
152 free(datValue.get_data());
153 return (ret == 0);
156 template <typename K, typename T>
157 bool Write(const K& key, const T& value, bool fOverwrite = true)
159 if (!pdb)
160 return false;
161 if (fReadOnly)
162 assert(!"Write called on database in read-only mode");
164 // Key
165 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
166 ssKey.reserve(1000);
167 ssKey << key;
168 Dbt datKey(ssKey.data(), ssKey.size());
170 // Value
171 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
172 ssValue.reserve(10000);
173 ssValue << value;
174 Dbt datValue(ssValue.data(), ssValue.size());
176 // Write
177 int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
179 // Clear memory in case it was a private key
180 memset(datKey.get_data(), 0, datKey.get_size());
181 memset(datValue.get_data(), 0, datValue.get_size());
182 return (ret == 0);
185 template <typename K>
186 bool Erase(const K& key)
188 if (!pdb)
189 return false;
190 if (fReadOnly)
191 assert(!"Erase called on database in read-only mode");
193 // Key
194 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
195 ssKey.reserve(1000);
196 ssKey << key;
197 Dbt datKey(ssKey.data(), ssKey.size());
199 // Erase
200 int ret = pdb->del(activeTxn, &datKey, 0);
202 // Clear memory
203 memset(datKey.get_data(), 0, datKey.get_size());
204 return (ret == 0 || ret == DB_NOTFOUND);
207 template <typename K>
208 bool Exists(const K& key)
210 if (!pdb)
211 return false;
213 // Key
214 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
215 ssKey.reserve(1000);
216 ssKey << key;
217 Dbt datKey(ssKey.data(), ssKey.size());
219 // Exists
220 int ret = pdb->exists(activeTxn, &datKey, 0);
222 // Clear memory
223 memset(datKey.get_data(), 0, datKey.get_size());
224 return (ret == 0);
227 Dbc* GetCursor()
229 if (!pdb)
230 return NULL;
231 Dbc* pcursor = NULL;
232 int ret = pdb->cursor(NULL, &pcursor, 0);
233 if (ret != 0)
234 return NULL;
235 return pcursor;
238 int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, bool setRange = false)
240 // Read at cursor
241 Dbt datKey;
242 unsigned int fFlags = DB_NEXT;
243 if (setRange) {
244 datKey.set_data(ssKey.data());
245 datKey.set_size(ssKey.size());
246 fFlags = DB_SET_RANGE;
248 Dbt datValue;
249 datKey.set_flags(DB_DBT_MALLOC);
250 datValue.set_flags(DB_DBT_MALLOC);
251 int ret = pcursor->get(&datKey, &datValue, fFlags);
252 if (ret != 0)
253 return ret;
254 else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
255 return 99999;
257 // Convert to streams
258 ssKey.SetType(SER_DISK);
259 ssKey.clear();
260 ssKey.write((char*)datKey.get_data(), datKey.get_size());
261 ssValue.SetType(SER_DISK);
262 ssValue.clear();
263 ssValue.write((char*)datValue.get_data(), datValue.get_size());
265 // Clear and free memory
266 memset(datKey.get_data(), 0, datKey.get_size());
267 memset(datValue.get_data(), 0, datValue.get_size());
268 free(datKey.get_data());
269 free(datValue.get_data());
270 return 0;
273 public:
274 bool TxnBegin()
276 if (!pdb || activeTxn)
277 return false;
278 DbTxn* ptxn = bitdb.TxnBegin();
279 if (!ptxn)
280 return false;
281 activeTxn = ptxn;
282 return true;
285 bool TxnCommit()
287 if (!pdb || !activeTxn)
288 return false;
289 int ret = activeTxn->commit(0);
290 activeTxn = NULL;
291 return (ret == 0);
294 bool TxnAbort()
296 if (!pdb || !activeTxn)
297 return false;
298 int ret = activeTxn->abort();
299 activeTxn = NULL;
300 return (ret == 0);
303 bool ReadVersion(int& nVersion)
305 nVersion = 0;
306 return Read(std::string("version"), nVersion);
309 bool WriteVersion(int nVersion)
311 return Write(std::string("version"), nVersion);
314 bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
317 #endif // BITCOIN_WALLET_DB_H