// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2011 The Bitcoin developers
+// Copyright (c) 2011-2012 The PPCoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_DB_H
#include <db_cxx.h>
-class CTransaction;
class CTxIndex;
class CDiskBlockIndex;
class CDiskTxPos;
class COutPoint;
-class CUser;
-class CReview;
class CAddress;
class CWalletTx;
+class CWallet;
class CAccount;
class CAccountingEntry;
class CBlockLocator;
-extern std::vector<unsigned char> vchDefaultKey;
-extern bool fClient;
-extern int nBestHeight;
-
extern unsigned int nWalletDBUpdated;
extern DbEnv dbenv;
-
extern void DBFlush(bool fShutdown);
-extern std::vector<unsigned char> GetKeyFromKeyPool();
-extern int64 GetOldestKeyPoolTime();
-extern void ThreadFlushWalletDB(void* parg);
-
-
+void ThreadFlushWalletDB(void* parg);
+bool BackupWallet(const CWallet& wallet, const std::string& strDest);
if (!pdb)
return false;
if (fReadOnly)
- assert(("Write called on database in read-only mode", false));
+ assert(!"Write called on database in read-only mode");
// Key
CDataStream ssKey(SER_DISK);
if (!pdb)
return false;
if (fReadOnly)
- assert(("Erase called on database in read-only mode", false));
+ assert(!"Erase called on database in read-only mode");
// Key
CDataStream ssKey(SER_DISK);
{
return Write(std::string("version"), nVersion);
}
+
+ bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
};
bool EraseBlockIndex(uint256 hash);
bool ReadHashBestChain(uint256& hashBestChain);
bool WriteHashBestChain(uint256 hashBestChain);
- bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
- bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
+ bool ReadBestInvalidTrust(uint64& nBestInvalidTrust);
+ bool WriteBestInvalidTrust(uint64 nBestInvalidTrust);
+ bool ReadAutoCheckpoint(int& nAutoCheckpoint);
+ bool WriteAutoCheckpoint(int nCheckpoint, bool fReset=false);
bool LoadBlockIndex();
};
-
-
-
class CKeyPool
{
public:
+enum DBErrors
+{
+ DB_LOAD_OK,
+ DB_CORRUPT,
+ DB_TOO_NEW,
+ DB_LOAD_FAIL,
+ DB_NEED_REWRITE
+};
+
class CWalletDB : public CDB
{
public:
- CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode)
+ CWalletDB(std::string strFilename, const char* pszMode="r+") : CDB(strFilename.c_str(), pszMode)
{
}
private:
return Read(std::make_pair(std::string("name"), strAddress), strName);
}
- bool WriteName(const std::string& strAddress, const std::string& strName)
- {
- CRITICAL_BLOCK(cs_mapAddressBook)
- mapAddressBook[strAddress] = strName;
- nWalletDBUpdated++;
- return Write(std::make_pair(std::string("name"), strAddress), strName);
- }
+ bool WriteName(const std::string& strAddress, const std::string& strName);
- bool EraseName(const std::string& strAddress)
- {
- // This should only be used for sending addresses, never for receiving addresses,
- // receiving addresses must always have an address book entry if they're not change return.
- CRITICAL_BLOCK(cs_mapAddressBook)
- mapAddressBook.erase(strAddress);
- nWalletDBUpdated++;
- return Erase(std::make_pair(std::string("name"), strAddress));
- }
+ bool EraseName(const std::string& strAddress);
bool ReadTx(uint256 hash, CWalletTx& wtx)
{
return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
}
+ bool WriteCryptedKey(const std::vector<unsigned char>& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, bool fEraseUnencryptedKey = true)
+ {
+ nWalletDBUpdated++;
+ if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
+ return false;
+ if (fEraseUnencryptedKey)
+ {
+ Erase(std::make_pair(std::string("key"), vchPubKey));
+ Erase(std::make_pair(std::string("wkey"), vchPubKey));
+ }
+ return true;
+ }
+
+ bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
+ {
+ nWalletDBUpdated++;
+ return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
+ }
+
bool WriteBestBlock(const CBlockLocator& locator)
{
nWalletDBUpdated++;
bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
{
- vchDefaultKey = vchPubKey;
nWalletDBUpdated++;
return Write(std::string("defaultkey"), vchPubKey);
}
+ bool ReadPool(int64 nPool, CKeyPool& keypool)
+ {
+ return Read(std::make_pair(std::string("pool"), nPool), keypool);
+ }
+
+ bool WritePool(int64 nPool, const CKeyPool& keypool)
+ {
+ nWalletDBUpdated++;
+ return Write(std::make_pair(std::string("pool"), nPool), keypool);
+ }
+
+ bool ErasePool(int64 nPool)
+ {
+ nWalletDBUpdated++;
+ return Erase(std::make_pair(std::string("pool"), nPool));
+ }
+
template<typename T>
bool ReadSetting(const std::string& strKey, T& value)
{
int64 GetAccountCreditDebit(const std::string& strAccount);
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
- bool LoadWallet();
-protected:
- void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);
- void KeepKey(int64 nIndex);
- static void ReturnKey(int64 nIndex);
- friend class CReserveKey;
- friend std::vector<unsigned char> GetKeyFromKeyPool();
- friend int64 GetOldestKeyPoolTime();
-};
-
-bool LoadWallet(bool& fFirstRunRet);
-void BackupWallet(const std::string& strDest);
-
-inline bool SetAddressBookName(const std::string& strAddress, const std::string& strName)
-{
- return CWalletDB().WriteName(strAddress, strName);
-}
-
-class CReserveKey
-{
-protected:
- int64 nIndex;
- std::vector<unsigned char> vchPubKey;
-public:
- CReserveKey()
- {
- nIndex = -1;
- }
-
- ~CReserveKey()
- {
- if (!fShutdown)
- ReturnKey();
- }
-
- std::vector<unsigned char> GetReservedKey();
- void KeepKey();
- void ReturnKey();
+ int LoadWallet(CWallet* pwallet);
};
#endif