2010-08-29 18:58:15 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2012-02-07 17:28:30 +01:00
|
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
2010-08-29 18:58:15 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_DB_H
|
|
|
|
#define BITCOIN_DB_H
|
|
|
|
|
2012-04-15 22:10:54 +02:00
|
|
|
#include "main.h"
|
2011-05-15 09:11:04 +02:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <db_cxx.h>
|
2010-08-29 18:58:15 +02:00
|
|
|
|
2011-11-08 19:20:29 +01:00
|
|
|
class CAddress;
|
2012-01-04 23:39:45 +01:00
|
|
|
class CAddrMan;
|
2011-11-08 19:20:29 +01:00
|
|
|
class CBlockLocator;
|
2010-08-29 18:58:15 +02:00
|
|
|
class CDiskBlockIndex;
|
|
|
|
class CDiskTxPos;
|
2011-11-08 19:20:29 +01:00
|
|
|
class CMasterKey;
|
2010-08-29 18:58:15 +02:00
|
|
|
class COutPoint;
|
2011-11-08 19:20:29 +01:00
|
|
|
class CTxIndex;
|
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
|
|
|
class CWallet;
|
2011-11-08 19:20:29 +01:00
|
|
|
class CWalletTx;
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
extern unsigned int nWalletDBUpdated;
|
|
|
|
|
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 18:28:20 +02:00
|
|
|
void ThreadFlushWalletDB(void* parg);
|
|
|
|
bool BackupWallet(const CWallet& wallet, const std::string& strDest);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
|
2012-05-14 03:37:39 +02:00
|
|
|
class CDBEnv
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool fDetachDB;
|
|
|
|
bool fDbEnvInit;
|
2012-05-22 21:51:13 +02:00
|
|
|
bool fMockDb;
|
2012-05-14 03:37:39 +02:00
|
|
|
boost::filesystem::path pathEnv;
|
|
|
|
|
|
|
|
void EnvShutdown();
|
|
|
|
|
|
|
|
public:
|
|
|
|
mutable CCriticalSection cs_db;
|
|
|
|
DbEnv dbenv;
|
2012-05-18 08:49:50 +02:00
|
|
|
std::map<std::string, int> mapFileUseCount;
|
|
|
|
std::map<std::string, Db*> mapDb;
|
2012-05-14 03:37:39 +02:00
|
|
|
|
|
|
|
CDBEnv();
|
|
|
|
~CDBEnv();
|
2012-05-22 21:51:13 +02:00
|
|
|
void MakeMock();
|
|
|
|
bool IsMock() { return fMockDb; };
|
2012-05-14 03:37:39 +02:00
|
|
|
bool Open(boost::filesystem::path pathEnv_);
|
|
|
|
void Close();
|
|
|
|
void Flush(bool fShutdown);
|
|
|
|
void CheckpointLSN(std::string strFile);
|
|
|
|
void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
|
2012-05-19 04:23:44 +02:00
|
|
|
bool GetDetach() { return fDetachDB; }
|
2012-05-14 18:33:34 +02:00
|
|
|
|
2012-05-18 08:49:50 +02:00
|
|
|
void CloseDb(const std::string& strFile);
|
|
|
|
|
2012-05-14 18:39:29 +02:00
|
|
|
DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
|
2012-05-14 18:33:34 +02:00
|
|
|
{
|
|
|
|
DbTxn* ptxn = NULL;
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = dbenv.txn_begin(NULL, &ptxn, flags);
|
2012-05-14 18:33:34 +02:00
|
|
|
if (!ptxn || ret != 0)
|
|
|
|
return NULL;
|
|
|
|
return ptxn;
|
|
|
|
}
|
2012-05-14 03:37:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern CDBEnv bitdb;
|
|
|
|
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** RAII class that provides access to a Berkeley database */
|
2010-08-29 18:58:15 +02:00
|
|
|
class CDB
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Db* pdb;
|
2011-05-15 09:11:04 +02:00
|
|
|
std::string strFile;
|
2012-05-14 18:39:29 +02:00
|
|
|
DbTxn *activeTxn;
|
2010-08-29 18:58:15 +02:00
|
|
|
bool fReadOnly;
|
|
|
|
|
|
|
|
explicit CDB(const char* pszFile, const char* pszMode="r+");
|
|
|
|
~CDB() { Close(); }
|
|
|
|
public:
|
|
|
|
void Close();
|
|
|
|
private:
|
|
|
|
CDB(const CDB&);
|
|
|
|
void operator=(const CDB&);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template<typename K, typename T>
|
|
|
|
bool Read(const K& key, T& value)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
Dbt datKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
// Read
|
|
|
|
Dbt datValue;
|
|
|
|
datValue.set_flags(DB_DBT_MALLOC);
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
memset(datKey.get_data(), 0, datKey.get_size());
|
|
|
|
if (datValue.get_data() == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Unserialize value
|
2012-05-22 21:12:52 +02:00
|
|
|
try {
|
|
|
|
CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
|
|
|
|
ssValue >> value;
|
|
|
|
}
|
|
|
|
catch (std::exception &e) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear and free memory
|
|
|
|
memset(datValue.get_data(), 0, datValue.get_size());
|
|
|
|
free(datValue.get_data());
|
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename T>
|
|
|
|
bool Write(const K& key, const T& value, bool fOverwrite=true)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
if (fReadOnly)
|
2011-06-24 19:56:23 +02:00
|
|
|
assert(!"Write called on database in read-only mode");
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
Dbt datKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
// Value
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssValue.reserve(10000);
|
|
|
|
ssValue << value;
|
|
|
|
Dbt datValue(&ssValue[0], ssValue.size());
|
|
|
|
|
|
|
|
// Write
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear memory in case it was a private key
|
|
|
|
memset(datKey.get_data(), 0, datKey.get_size());
|
|
|
|
memset(datValue.get_data(), 0, datValue.get_size());
|
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K>
|
|
|
|
bool Erase(const K& key)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
if (fReadOnly)
|
2011-06-24 19:56:23 +02:00
|
|
|
assert(!"Erase called on database in read-only mode");
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
Dbt datKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
// Erase
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->del(activeTxn, &datKey, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear memory
|
|
|
|
memset(datKey.get_data(), 0, datKey.get_size());
|
|
|
|
return (ret == 0 || ret == DB_NOTFOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K>
|
|
|
|
bool Exists(const K& key)
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Key
|
2012-04-16 14:56:45 +02:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
2010-08-29 18:58:15 +02:00
|
|
|
ssKey.reserve(1000);
|
|
|
|
ssKey << key;
|
|
|
|
Dbt datKey(&ssKey[0], ssKey.size());
|
|
|
|
|
|
|
|
// Exists
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = pdb->exists(activeTxn, &datKey, 0);
|
2010-08-29 18:58:15 +02:00
|
|
|
|
|
|
|
// Clear memory
|
|
|
|
memset(datKey.get_data(), 0, datKey.get_size());
|
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dbc* GetCursor()
|
|
|
|
{
|
|
|
|
if (!pdb)
|
|
|
|
return NULL;
|
|
|
|
Dbc* pcursor = NULL;
|
|
|
|
int ret = pdb->cursor(NULL, &pcursor, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
return NULL;
|
|
|
|
return pcursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
|
|
|
|
{
|
|
|
|
// Read at cursor
|
|
|
|
Dbt datKey;
|
|
|
|
if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
|
|
|
|
{
|
|
|
|
datKey.set_data(&ssKey[0]);
|
|
|
|
datKey.set_size(ssKey.size());
|
|
|
|
}
|
|
|
|
Dbt datValue;
|
|
|
|
if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
|
|
|
|
{
|
|
|
|
datValue.set_data(&ssValue[0]);
|
|
|
|
datValue.set_size(ssValue.size());
|
|
|
|
}
|
|
|
|
datKey.set_flags(DB_DBT_MALLOC);
|
|
|
|
datValue.set_flags(DB_DBT_MALLOC);
|
|
|
|
int ret = pcursor->get(&datKey, &datValue, fFlags);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
|
|
|
|
return 99999;
|
|
|
|
|
|
|
|
// Convert to streams
|
|
|
|
ssKey.SetType(SER_DISK);
|
|
|
|
ssKey.clear();
|
|
|
|
ssKey.write((char*)datKey.get_data(), datKey.get_size());
|
|
|
|
ssValue.SetType(SER_DISK);
|
|
|
|
ssValue.clear();
|
|
|
|
ssValue.write((char*)datValue.get_data(), datValue.get_size());
|
|
|
|
|
|
|
|
// Clear and free memory
|
|
|
|
memset(datKey.get_data(), 0, datKey.get_size());
|
|
|
|
memset(datValue.get_data(), 0, datValue.get_size());
|
|
|
|
free(datKey.get_data());
|
|
|
|
free(datValue.get_data());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool TxnBegin()
|
|
|
|
{
|
2012-05-14 18:39:29 +02:00
|
|
|
if (!pdb || activeTxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
DbTxn* ptxn = bitdb.TxnBegin();
|
2012-05-14 18:33:34 +02:00
|
|
|
if (!ptxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
activeTxn = ptxn;
|
2010-08-29 18:58:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TxnCommit()
|
|
|
|
{
|
2012-05-14 18:39:29 +02:00
|
|
|
if (!pdb || !activeTxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = activeTxn->commit(0);
|
|
|
|
activeTxn = NULL;
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TxnAbort()
|
|
|
|
{
|
2012-05-14 18:39:29 +02:00
|
|
|
if (!pdb || !activeTxn)
|
2010-08-29 18:58:15 +02:00
|
|
|
return false;
|
2012-05-14 18:39:29 +02:00
|
|
|
int ret = activeTxn->abort();
|
|
|
|
activeTxn = NULL;
|
2010-08-29 18:58:15 +02:00
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadVersion(int& nVersion)
|
|
|
|
{
|
|
|
|
nVersion = 0;
|
2011-05-15 09:11:04 +02:00
|
|
|
return Read(std::string("version"), nVersion);
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteVersion(int nVersion)
|
|
|
|
{
|
2011-05-15 09:11:04 +02:00
|
|
|
return Write(std::string("version"), nVersion);
|
2010-08-29 18:58:15 +02:00
|
|
|
}
|
2011-11-10 21:29:23 +01:00
|
|
|
|
2011-11-11 03:12:46 +01:00
|
|
|
bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
|
2010-08-29 18:58:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** Access to the transaction database (blkindex.dat) */
|
2010-08-29 18:58:15 +02:00
|
|
|
class CTxDB : public CDB
|
|
|
|
{
|
|
|
|
public:
|
2010-12-05 10:29:30 +01:00
|
|
|
CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
|
2010-08-29 18:58:15 +02:00
|
|
|
private:
|
|
|
|
CTxDB(const CTxDB&);
|
|
|
|
void operator=(const CTxDB&);
|
|
|
|
public:
|
|
|
|
bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
|
|
|
|
bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
|
|
|
|
bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
|
|
|
|
bool EraseTxIndex(const CTransaction& tx);
|
|
|
|
bool ContainsTx(uint256 hash);
|
|
|
|
bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
|
|
|
|
bool ReadDiskTx(uint256 hash, CTransaction& tx);
|
|
|
|
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
|
|
|
|
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
|
|
|
|
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
|
|
|
|
bool ReadHashBestChain(uint256& hashBestChain);
|
|
|
|
bool WriteHashBestChain(uint256 hashBestChain);
|
|
|
|
bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
|
|
|
|
bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
|
|
|
|
bool LoadBlockIndex();
|
2012-05-23 06:17:07 +02:00
|
|
|
private:
|
|
|
|
bool LoadBlockIndexGuts();
|
2010-08-29 18:58:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-17 04:11:19 +02:00
|
|
|
/** Access to the (IP) address database (peers.dat) */
|
|
|
|
class CAddrDB
|
2010-08-29 18:58:15 +02:00
|
|
|
{
|
|
|
|
private:
|
2012-05-17 04:11:19 +02:00
|
|
|
boost::filesystem::path pathAddr;
|
2010-08-29 18:58:15 +02:00
|
|
|
public:
|
2012-05-17 04:11:19 +02:00
|
|
|
CAddrDB();
|
|
|
|
bool Write(const CAddrMan& addr);
|
|
|
|
bool Read(CAddrMan& addr);
|
2010-08-29 18:58:15 +02:00
|
|
|
};
|
|
|
|
|
2012-04-15 23:39:49 +02:00
|
|
|
#endif // BITCOIN_DB_H
|