Fix insanity of CWalletDB::WriteTx and CWalletTx::WriteToDisk
[bitcoinplatinum.git] / src / wallet / walletdb.h
blob5345c0907e9cd00e2159a7b9a451fe69190c1c25
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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_WALLETDB_H
7 #define BITCOIN_WALLET_WALLETDB_H
9 #include "amount.h"
10 #include "wallet/db.h"
11 #include "key.h"
13 #include <list>
14 #include <stdint.h>
15 #include <string>
16 #include <utility>
17 #include <vector>
19 static const bool DEFAULT_FLUSHWALLET = true;
21 class CAccount;
22 class CAccountingEntry;
23 struct CBlockLocator;
24 class CKeyPool;
25 class CMasterKey;
26 class CScript;
27 class CWallet;
28 class CWalletTx;
29 class uint160;
30 class uint256;
32 /** Error statuses for the wallet database */
33 enum DBErrors
35 DB_LOAD_OK,
36 DB_CORRUPT,
37 DB_NONCRITICAL_ERROR,
38 DB_TOO_NEW,
39 DB_LOAD_FAIL,
40 DB_NEED_REWRITE
43 class CKeyMetadata
45 public:
46 static const int CURRENT_VERSION=1;
47 int nVersion;
48 int64_t nCreateTime; // 0 means unknown
50 CKeyMetadata()
52 SetNull();
54 CKeyMetadata(int64_t nCreateTime_)
56 nVersion = CKeyMetadata::CURRENT_VERSION;
57 nCreateTime = nCreateTime_;
60 ADD_SERIALIZE_METHODS;
62 template <typename Stream, typename Operation>
63 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
64 READWRITE(this->nVersion);
65 nVersion = this->nVersion;
66 READWRITE(nCreateTime);
69 void SetNull()
71 nVersion = CKeyMetadata::CURRENT_VERSION;
72 nCreateTime = 0;
76 /** Access to the wallet database */
77 class CWalletDB : public CDB
79 public:
80 CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
84 bool WriteName(const std::string& strAddress, const std::string& strName);
85 bool EraseName(const std::string& strAddress);
87 bool WritePurpose(const std::string& strAddress, const std::string& purpose);
88 bool ErasePurpose(const std::string& strAddress);
90 bool WriteTx(const CWalletTx& wtx);
91 bool EraseTx(uint256 hash);
93 bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
94 bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
95 bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
97 bool WriteCScript(const uint160& hash, const CScript& redeemScript);
99 bool WriteWatchOnly(const CScript &script);
100 bool EraseWatchOnly(const CScript &script);
102 bool WriteBestBlock(const CBlockLocator& locator);
103 bool ReadBestBlock(CBlockLocator& locator);
105 bool WriteOrderPosNext(int64_t nOrderPosNext);
107 bool WriteDefaultKey(const CPubKey& vchPubKey);
109 bool ReadPool(int64_t nPool, CKeyPool& keypool);
110 bool WritePool(int64_t nPool, const CKeyPool& keypool);
111 bool ErasePool(int64_t nPool);
113 bool WriteMinVersion(int nVersion);
115 /// This writes directly to the database, and will not update the CWallet's cached accounting entries!
116 /// Use wallet.AddAccountingEntry instead, to write *and* update its caches.
117 bool WriteAccountingEntry_Backend(const CAccountingEntry& acentry);
118 bool ReadAccount(const std::string& strAccount, CAccount& account);
119 bool WriteAccount(const std::string& strAccount, const CAccount& account);
121 /// Write destination data key,value tuple to database
122 bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
123 /// Erase destination data tuple from wallet database
124 bool EraseDestData(const std::string &address, const std::string &key);
126 CAmount GetAccountCreditDebit(const std::string& strAccount);
127 void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
129 DBErrors ReorderTransactions(CWallet* pwallet);
130 DBErrors LoadWallet(CWallet* pwallet);
131 DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
132 DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
133 DBErrors ZapSelectTx(CWallet* pwallet, std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
134 static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
135 static bool Recover(CDBEnv& dbenv, const std::string& filename);
137 private:
138 CWalletDB(const CWalletDB&);
139 void operator=(const CWalletDB&);
141 bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
144 bool BackupWallet(const CWallet& wallet, const std::string& strDest);
145 void ThreadFlushWalletDB(const std::string& strFile);
147 #endif // BITCOIN_WALLET_WALLETDB_H