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"
11 #include "serialize.h"
22 static const unsigned int DEFAULT_WALLET_DBLOGSIZE
= 100;
23 static const bool DEFAULT_WALLET_PRIVDB
= true;
30 // Don't change into fs::path, as that can result in
31 // shutdown problems/crashes caused by a static initialized internal pointer.
37 mutable CCriticalSection cs_db
;
39 std::map
<std::string
, int> mapFileUseCount
;
40 std::map
<std::string
, Db
*> mapDb
;
47 bool IsMock() { return fMockDb
; }
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.
55 enum VerifyResult
{ VERIFY_OK
,
58 VerifyResult
Verify(const std::string
& strFile
, bool (*recoverFunc
)(const std::string
& strFile
));
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
66 typedef std::pair
<std::vector
<unsigned char>, std::vector
<unsigned char> > KeyValPair
;
67 bool Salvage(const std::string
& strFile
, bool fAggressive
, std::vector
<KeyValPair
>& vResult
);
69 bool Open(const fs::path
& path
);
71 void Flush(bool fShutdown
);
72 void CheckpointLSN(const std::string
& strFile
);
74 void CloseDb(const std::string
& strFile
);
76 DbTxn
* TxnBegin(int flags
= DB_TXN_WRITE_NOSYNC
)
79 int ret
= dbenv
->txn_begin(NULL
, &ptxn
, flags
);
80 if (!ptxn
|| ret
!= 0)
88 /** An instance of this class represents one database.
89 * For BerkeleyDB this is just a (env, strFile) tuple.
91 class CWalletDBWrapper
95 /** Create dummy DB handle */
96 CWalletDBWrapper(): env(nullptr)
100 /** Create DB handle to real database */
101 CWalletDBWrapper(CDBEnv
*env_in
, const std::string
&strFile_in
):
102 env(env_in
), strFile(strFile_in
)
106 /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
108 bool Rewrite(const char* pszSkip
=nullptr);
110 /** Back up the entire database to a file.
112 bool Backup(const std::string
& strDest
);
114 /** Get a name for this database, for debugging etc.
116 std::string
GetName() const { return strFile
; }
118 /** Make sure all changes are flushed to disk.
120 void Flush(bool shutdown
);
123 /** BerkeleyDB specific */
127 /** Return whether this database handle is a dummy for testing.
128 * Only to be used at a low level, application should ideally not care
131 bool IsDummy() { return env
== nullptr; }
135 /** RAII class that provides access to a Berkeley database */
147 explicit CDB(CWalletDBWrapper
& dbw
, const char* pszMode
= "r+", bool fFlushOnCloseIn
=true);
152 static bool Recover(const std::string
& filename
, void *callbackDataIn
, bool (*recoverKVcallback
)(void* callbackData
, CDataStream ssKey
, CDataStream ssValue
));
154 /* flush the wallet passively (TRY_LOCK)
155 ideal to be called periodically */
156 static bool PeriodicFlush(CWalletDBWrapper
& dbw
);
157 /* verifies the database environment */
158 static bool VerifyEnvironment(const std::string
& walletFile
, const fs::path
& dataDir
, std::string
& errorStr
);
159 /* verifies the database file */
160 static bool VerifyDatabaseFile(const std::string
& walletFile
, const fs::path
& dataDir
, std::string
& warningStr
, std::string
& errorStr
, bool (*recoverFunc
)(const std::string
& strFile
));
164 void operator=(const CDB
&);
167 template <typename K
, typename T
>
168 bool Read(const K
& key
, T
& value
)
174 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
177 Dbt
datKey(ssKey
.data(), ssKey
.size());
181 datValue
.set_flags(DB_DBT_MALLOC
);
182 int ret
= pdb
->get(activeTxn
, &datKey
, &datValue
, 0);
183 memset(datKey
.get_data(), 0, datKey
.get_size());
184 if (datValue
.get_data() == NULL
)
189 CDataStream
ssValue((char*)datValue
.get_data(), (char*)datValue
.get_data() + datValue
.get_size(), SER_DISK
, CLIENT_VERSION
);
191 } catch (const std::exception
&) {
195 // Clear and free memory
196 memset(datValue
.get_data(), 0, datValue
.get_size());
197 free(datValue
.get_data());
201 template <typename K
, typename T
>
202 bool Write(const K
& key
, const T
& value
, bool fOverwrite
= true)
207 assert(!"Write called on database in read-only mode");
210 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
213 Dbt
datKey(ssKey
.data(), ssKey
.size());
216 CDataStream
ssValue(SER_DISK
, CLIENT_VERSION
);
217 ssValue
.reserve(10000);
219 Dbt
datValue(ssValue
.data(), ssValue
.size());
222 int ret
= pdb
->put(activeTxn
, &datKey
, &datValue
, (fOverwrite
? 0 : DB_NOOVERWRITE
));
224 // Clear memory in case it was a private key
225 memset(datKey
.get_data(), 0, datKey
.get_size());
226 memset(datValue
.get_data(), 0, datValue
.get_size());
230 template <typename K
>
231 bool Erase(const K
& key
)
236 assert(!"Erase called on database in read-only mode");
239 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
242 Dbt
datKey(ssKey
.data(), ssKey
.size());
245 int ret
= pdb
->del(activeTxn
, &datKey
, 0);
248 memset(datKey
.get_data(), 0, datKey
.get_size());
249 return (ret
== 0 || ret
== DB_NOTFOUND
);
252 template <typename K
>
253 bool Exists(const K
& key
)
259 CDataStream
ssKey(SER_DISK
, CLIENT_VERSION
);
262 Dbt
datKey(ssKey
.data(), ssKey
.size());
265 int ret
= pdb
->exists(activeTxn
, &datKey
, 0);
268 memset(datKey
.get_data(), 0, datKey
.get_size());
277 int ret
= pdb
->cursor(NULL
, &pcursor
, 0);
283 int ReadAtCursor(Dbc
* pcursor
, CDataStream
& ssKey
, CDataStream
& ssValue
, bool setRange
= false)
287 unsigned int fFlags
= DB_NEXT
;
289 datKey
.set_data(ssKey
.data());
290 datKey
.set_size(ssKey
.size());
291 fFlags
= DB_SET_RANGE
;
294 datKey
.set_flags(DB_DBT_MALLOC
);
295 datValue
.set_flags(DB_DBT_MALLOC
);
296 int ret
= pcursor
->get(&datKey
, &datValue
, fFlags
);
299 else if (datKey
.get_data() == NULL
|| datValue
.get_data() == NULL
)
302 // Convert to streams
303 ssKey
.SetType(SER_DISK
);
305 ssKey
.write((char*)datKey
.get_data(), datKey
.get_size());
306 ssValue
.SetType(SER_DISK
);
308 ssValue
.write((char*)datValue
.get_data(), datValue
.get_size());
310 // Clear and free memory
311 memset(datKey
.get_data(), 0, datKey
.get_size());
312 memset(datValue
.get_data(), 0, datValue
.get_size());
313 free(datKey
.get_data());
314 free(datValue
.get_data());
321 if (!pdb
|| activeTxn
)
323 DbTxn
* ptxn
= bitdb
.TxnBegin();
332 if (!pdb
|| !activeTxn
)
334 int ret
= activeTxn
->commit(0);
341 if (!pdb
|| !activeTxn
)
343 int ret
= activeTxn
->abort();
348 bool ReadVersion(int& nVersion
)
351 return Read(std::string("version"), nVersion
);
354 bool WriteVersion(int nVersion
)
356 return Write(std::string("version"), nVersion
);
359 bool static Rewrite(CWalletDBWrapper
& dbw
, const char* pszSkip
= NULL
);
362 #endif // BITCOIN_WALLET_DB_H