X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fdb.h;h=c8d46d8fe6fddcda0b99f2aca677ecfe3e1b6123;hb=4e87d341f75f13bbd7d108c31c03886fbc4df56f;hp=290981c06afff0f157b66cd6bb6072f77fb0763e;hpb=84c3fb07b0b8199c7f85c5de280e7100bad0786f;p=novacoin.git diff --git a/src/db.h b/src/db.h index 290981c..c8d46d8 100644 --- a/src/db.h +++ b/src/db.h @@ -1,34 +1,36 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // 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 +#define BITCOIN_DB_H + +#include "key.h" + +#include +#include +#include + +#include -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 map mapAddressBook; -extern CCriticalSection cs_mapAddressBook; -extern vector vchDefaultKey; -extern bool fClient; -extern int nBestHeight; - extern unsigned int nWalletDBUpdated; extern DbEnv dbenv; extern void DBFlush(bool fShutdown); -extern vector GetKeyFromKeyPool(); -extern int64 GetOldestKeyPoolTime(); +void ThreadFlushWalletDB(void* parg); +bool BackupWallet(const CWallet& wallet, const std::string& strDest); @@ -37,8 +39,8 @@ class CDB { protected: Db* pdb; - string strFile; - vector vTxn; + std::string strFile; + std::vector vTxn; bool fReadOnly; explicit CDB(const char* pszFile, const char* pszMode="r+"); @@ -247,12 +249,12 @@ public: bool ReadVersion(int& nVersion) { nVersion = 0; - return Read(string("version"), nVersion); + return Read(std::string("version"), nVersion); } bool WriteVersion(int nVersion) { - return Write(string("version"), nVersion); + return Write(std::string("version"), nVersion); } }; @@ -276,7 +278,7 @@ public: bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight); bool EraseTxIndex(const CTransaction& tx); bool ContainsTx(uint256 hash); - bool ReadOwnerTxes(uint160 hash160, int nHeight, vector& vtx); + bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector& vtx); bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex); bool ReadDiskTx(uint256 hash, CTransaction& tx); bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex); @@ -311,21 +313,18 @@ bool LoadAddresses(); - - - class CKeyPool { public: int64 nTime; - vector vchPubKey; + std::vector vchPubKey; CKeyPool() { nTime = GetTime(); } - CKeyPool(const vector& vchPubKeyIn) + CKeyPool(const std::vector& vchPubKeyIn) { nTime = GetTime(); vchPubKey = vchPubKeyIn; @@ -346,169 +345,131 @@ public: 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: CWalletDB(const CWalletDB&); void operator=(const CWalletDB&); public: - bool ReadName(const string& strAddress, string& strName) + bool ReadName(const std::string& strAddress, std::string& strName) { strName = ""; - return Read(make_pair(string("name"), strAddress), strName); + return Read(std::make_pair(std::string("name"), strAddress), strName); } - bool WriteName(const string& strAddress, const string& strName) - { - CRITICAL_BLOCK(cs_mapAddressBook) - mapAddressBook[strAddress] = strName; - nWalletDBUpdated++; - return Write(make_pair(string("name"), strAddress), strName); - } + bool WriteName(const std::string& strAddress, const std::string& strName); - bool EraseName(const 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(make_pair(string("name"), strAddress)); - } + bool EraseName(const std::string& strAddress); bool ReadTx(uint256 hash, CWalletTx& wtx) { - return Read(make_pair(string("tx"), hash), wtx); + return Read(std::make_pair(std::string("tx"), hash), wtx); } bool WriteTx(uint256 hash, const CWalletTx& wtx) { nWalletDBUpdated++; - return Write(make_pair(string("tx"), hash), wtx); + return Write(std::make_pair(std::string("tx"), hash), wtx); } bool EraseTx(uint256 hash) { nWalletDBUpdated++; - return Erase(make_pair(string("tx"), hash)); + return Erase(std::make_pair(std::string("tx"), hash)); } - bool ReadKey(const vector& vchPubKey, CPrivKey& vchPrivKey) + bool ReadKey(const std::vector& vchPubKey, CPrivKey& vchPrivKey) { vchPrivKey.clear(); - return Read(make_pair(string("key"), vchPubKey), vchPrivKey); + return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey); } - bool WriteKey(const vector& vchPubKey, const CPrivKey& vchPrivKey) + bool WriteKey(const std::vector& vchPubKey, const CPrivKey& vchPrivKey) { nWalletDBUpdated++; - return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false); + return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false); } - bool WriteBestBlock(const CBlockLocator& locator) + bool WriteCryptedKey(const std::vector& vchPubKey, const std::vector& vchCryptedSecret, bool fEraseUnencryptedKey = true) { nWalletDBUpdated++; - return Write(string("bestblock"), locator); + 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 ReadBestBlock(CBlockLocator& locator) + bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey) { - return Read(string("bestblock"), locator); + nWalletDBUpdated++; + return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true); } - bool ReadDefaultKey(vector& vchPubKey) + bool WriteBestBlock(const CBlockLocator& locator) { - vchPubKey.clear(); - return Read(string("defaultkey"), vchPubKey); + nWalletDBUpdated++; + return Write(std::string("bestblock"), locator); } - bool WriteDefaultKey(const vector& vchPubKey) + bool ReadBestBlock(CBlockLocator& locator) { - vchDefaultKey = vchPubKey; - nWalletDBUpdated++; - return Write(string("defaultkey"), vchPubKey); + return Read(std::string("bestblock"), locator); } - template - bool ReadSetting(const string& strKey, T& value) + bool ReadDefaultKey(std::vector& vchPubKey) { - return Read(make_pair(string("setting"), strKey), value); + vchPubKey.clear(); + return Read(std::string("defaultkey"), vchPubKey); } - template - bool WriteSetting(const string& strKey, const T& value) + bool WriteDefaultKey(const std::vector& vchPubKey) { nWalletDBUpdated++; - return Write(make_pair(string("setting"), strKey), value); + return Write(std::string("defaultkey"), vchPubKey); } - bool ReadAccount(const string& strAccount, CAccount& account); - bool WriteAccount(const string& strAccount, const CAccount& account); - bool WriteAccountingEntry(const CAccountingEntry& acentry); - int64 GetAccountCreditDebit(const string& strAccount); - void ListAccountCreditDebit(const string& strAccount, list& acentries); - - bool LoadWallet(); -protected: - void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool); - void KeepKey(int64 nIndex); - static void ReturnKey(int64 nIndex); - friend class CReserveKey; - friend vector GetKeyFromKeyPool(); - friend int64 GetOldestKeyPoolTime(); -}; - -bool LoadWallet(bool& fFirstRunRet); -void BackupWallet(const string& strDest); - -inline bool SetAddressBookName(const string& strAddress, const string& strName) -{ - return CWalletDB().WriteName(strAddress, strName); -} - -class CReserveKey -{ -protected: - int64 nIndex; - vector vchPubKey; -public: - CReserveKey() + bool ReadPool(int64 nPool, CKeyPool& keypool) { - nIndex = -1; + return Read(std::make_pair(std::string("pool"), nPool), keypool); } - ~CReserveKey() + bool WritePool(int64 nPool, const CKeyPool& keypool) { - if (!fShutdown) - ReturnKey(); + nWalletDBUpdated++; + return Write(std::make_pair(std::string("pool"), nPool), keypool); } - vector GetReservedKey() + bool ErasePool(int64 nPool) { - if (nIndex == -1) - { - CKeyPool keypool; - CWalletDB().ReserveKeyFromKeyPool(nIndex, keypool); - vchPubKey = keypool.vchPubKey; - } - assert(!vchPubKey.empty()); - return vchPubKey; + nWalletDBUpdated++; + return Erase(std::make_pair(std::string("pool"), nPool)); } - void KeepKey() + template + bool ReadSetting(const std::string& strKey, T& value) { - if (nIndex != -1) - CWalletDB().KeepKey(nIndex); - nIndex = -1; - vchPubKey.clear(); + return Read(std::make_pair(std::string("setting"), strKey), value); } - void ReturnKey() + template + bool WriteSetting(const std::string& strKey, const T& value) { - if (nIndex != -1) - CWalletDB::ReturnKey(nIndex); - nIndex = -1; - vchPubKey.clear(); + nWalletDBUpdated++; + return Write(std::make_pair(std::string("setting"), strKey), value); } + + bool ReadAccount(const std::string& strAccount, CAccount& account); + bool WriteAccount(const std::string& strAccount, const CAccount& account); + bool WriteAccountingEntry(const CAccountingEntry& acentry); + int64 GetAccountCreditDebit(const std::string& strAccount); + void ListAccountCreditDebit(const std::string& strAccount, std::list& acentries); + + bool LoadWallet(CWallet* pwallet); }; + +#endif