X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fdb.h;h=38daad3e919c0fbbe52b761435de1a0af86c81e3;hb=d11488abd05cb39a9f481e7c4c35f780197a3d28;hp=9826194ed0d1f0805ecc5e1c059def6924cb9cf2;hpb=ba4081c1fcaddf361abd61b2721994eff5475bb3;p=novacoin.git diff --git a/src/db.h b/src/db.h index 9826194..38daad3 100644 --- a/src/db.h +++ b/src/db.h @@ -1,6 +1,7 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_DB_H #define BITCOIN_DB_H @@ -12,37 +13,28 @@ #include -class CTransaction; -class CTxIndex; +class CAccount; +class CAccountingEntry; +class CAddress; +class CAddrMan; +class CBlockLocator; class CDiskBlockIndex; class CDiskTxPos; +class CMasterKey; class COutPoint; -class CUser; -class CReview; -class CAddress; +class CTxIndex; +class CWallet; class CWalletTx; -class CAccount; -class CAccountingEntry; -class CBlockLocator; - -extern std::map mapAddressBook; -extern CCriticalSection cs_mapAddressBook; -extern std::vector vchDefaultKey; -extern bool fClient; -extern int nBestHeight; - extern unsigned int nWalletDBUpdated; extern DbEnv dbenv; - extern void DBFlush(bool fShutdown); -extern std::vector GetKeyFromKeyPool(); -extern int64 GetOldestKeyPoolTime(); - - +void ThreadFlushWalletDB(void* parg); +bool BackupWallet(const CWallet& wallet, const std::string& strDest); +/** RAII class that provides access to a Berkeley database */ class CDB { protected: @@ -96,7 +88,7 @@ protected: 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); @@ -125,7 +117,7 @@ protected: 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); @@ -264,8 +256,9 @@ public: { return Write(std::string("version"), nVersion); } -}; + bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL); +}; @@ -273,6 +266,7 @@ public: +/** Access to the transaction database (blkindex.dat) */ class CTxDB : public CDB { public: @@ -303,7 +297,7 @@ public: - +/** Access to the (IP) address database (addr.dat) */ class CAddrDB : public CDB { public: @@ -312,18 +306,14 @@ private: CAddrDB(const CAddrDB&); void operator=(const CAddrDB&); public: - bool WriteAddress(const CAddress& addr); - bool EraseAddress(const CAddress& addr); + bool WriteAddrman(const CAddrMan& addr); bool LoadAddresses(); }; bool LoadAddresses(); - - - - +/** A key pool entry */ class CKeyPool { public: @@ -352,11 +342,21 @@ public: +/** Error statuses for the wallet database */ +enum DBErrors +{ + DB_LOAD_OK, + DB_CORRUPT, + DB_TOO_NEW, + DB_LOAD_FAIL, + DB_NEED_REWRITE +}; +/** Access to the wallet database (wallet.dat) */ 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: @@ -369,23 +369,9 @@ public: 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) { @@ -416,6 +402,38 @@ public: return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false); } + bool WriteCryptedKey(const std::vector& vchPubKey, const std::vector& 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); + } + + // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013 + bool ReadCScript(const uint160 &hash, CScript& redeemScript) + { + redeemScript.clear(); + return Read(std::make_pair(std::string("cscript"), hash), redeemScript); + } + + bool WriteCScript(const uint160& hash, const CScript& redeemScript) + { + nWalletDBUpdated++; + return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false); + } + bool WriteBestBlock(const CBlockLocator& locator) { nWalletDBUpdated++; @@ -435,92 +453,58 @@ public: bool WriteDefaultKey(const std::vector& vchPubKey) { - vchDefaultKey = vchPubKey; nWalletDBUpdated++; return Write(std::string("defaultkey"), vchPubKey); } - template - bool ReadSetting(const std::string& strKey, T& value) + bool ReadPool(int64 nPool, CKeyPool& keypool) { - return Read(std::make_pair(std::string("setting"), strKey), value); + return Read(std::make_pair(std::string("pool"), nPool), keypool); } - template - bool WriteSetting(const std::string& strKey, const T& value) + bool WritePool(int64 nPool, const CKeyPool& keypool) { nWalletDBUpdated++; - return Write(std::make_pair(std::string("setting"), strKey), value); + return Write(std::make_pair(std::string("pool"), nPool), keypool); } - 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(); -protected: - void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool); - void KeepKey(int64 nIndex); - static void ReturnKey(int64 nIndex); - friend class CReserveKey; - friend std::vector 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 vchPubKey; -public: - CReserveKey() + bool ErasePool(int64 nPool) { - nIndex = -1; + nWalletDBUpdated++; + return Erase(std::make_pair(std::string("pool"), nPool)); } - ~CReserveKey() + // Settings are no longer stored in wallet.dat; these are + // used only for backwards compatibility: + template + bool ReadSetting(const std::string& strKey, T& value) { - if (!fShutdown) - ReturnKey(); + return Read(std::make_pair(std::string("setting"), strKey), value); } - - std::vector GetReservedKey() + template + bool WriteSetting(const std::string& strKey, const T& value) { - if (nIndex == -1) - { - CKeyPool keypool; - CWalletDB().ReserveKeyFromKeyPool(nIndex, keypool); - vchPubKey = keypool.vchPubKey; - } - assert(!vchPubKey.empty()); - return vchPubKey; + nWalletDBUpdated++; + return Write(std::make_pair(std::string("setting"), strKey), value); } - - void KeepKey() + bool EraseSetting(const std::string& strKey) { - if (nIndex != -1) - CWalletDB().KeepKey(nIndex); - nIndex = -1; - vchPubKey.clear(); + nWalletDBUpdated++; + return Erase(std::make_pair(std::string("setting"), strKey)); } - void ReturnKey() + bool WriteMinVersion(int nVersion) { - if (nIndex != -1) - CWalletDB::ReturnKey(nIndex); - nIndex = -1; - vchPubKey.clear(); + return Write(std::string("minversion"), nVersion); } + + 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); + + int LoadWallet(CWallet* pwallet); }; #endif