Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2013 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.
5 :
6 : #ifndef BITCOIN_WALLET_WALLETDB_H
7 : #define BITCOIN_WALLET_WALLETDB_H
8 :
9 : #include "amount.h"
10 : #include "wallet/db.h"
11 : #include "key.h"
12 :
13 : #include <list>
14 : #include <stdint.h>
15 : #include <string>
16 : #include <utility>
17 : #include <vector>
18 :
19 : class CAccount;
20 : class CAccountingEntry;
21 : struct CBlockLocator;
22 : class CKeyPool;
23 : class CMasterKey;
24 : class CScript;
25 : class CWallet;
26 : class CWalletTx;
27 : class uint160;
28 : class uint256;
29 :
30 : /** Error statuses for the wallet database */
31 : enum DBErrors
32 : {
33 : DB_LOAD_OK,
34 : DB_CORRUPT,
35 : DB_NONCRITICAL_ERROR,
36 : DB_TOO_NEW,
37 : DB_LOAD_FAIL,
38 : DB_NEED_REWRITE
39 : };
40 :
41 : class CKeyMetadata
42 : {
43 : public:
44 : static const int CURRENT_VERSION=1;
45 : int nVersion;
46 : int64_t nCreateTime; // 0 means unknown
47 :
48 0 : CKeyMetadata()
49 : {
50 : SetNull();
51 0 : }
52 0 : CKeyMetadata(int64_t nCreateTime_)
53 : {
54 0 : nVersion = CKeyMetadata::CURRENT_VERSION;
55 0 : nCreateTime = nCreateTime_;
56 0 : }
57 :
58 4034 : ADD_SERIALIZE_METHODS;
59 :
60 : template <typename Stream, typename Operation>
61 4034 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
62 4034 : READWRITE(this->nVersion);
63 4034 : nVersion = this->nVersion;
64 4034 : READWRITE(nCreateTime);
65 4034 : }
66 :
67 : void SetNull()
68 : {
69 2620 : nVersion = CKeyMetadata::CURRENT_VERSION;
70 2620 : nCreateTime = 0;
71 : }
72 : };
73 :
74 : /** Access to the wallet database (wallet.dat) */
75 9885 : class CWalletDB : public CDB
76 : {
77 : public:
78 9885 : CWalletDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
79 : {
80 0 : }
81 :
82 : bool WriteName(const std::string& strAddress, const std::string& strName);
83 : bool EraseName(const std::string& strAddress);
84 :
85 : bool WritePurpose(const std::string& strAddress, const std::string& purpose);
86 : bool ErasePurpose(const std::string& strAddress);
87 :
88 : bool WriteTx(uint256 hash, const CWalletTx& wtx);
89 : bool EraseTx(uint256 hash);
90 :
91 : bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
92 : bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
93 : bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
94 :
95 : bool WriteCScript(const uint160& hash, const CScript& redeemScript);
96 :
97 : bool WriteWatchOnly(const CScript &script);
98 : bool EraseWatchOnly(const CScript &script);
99 :
100 : bool WriteBestBlock(const CBlockLocator& locator);
101 : bool ReadBestBlock(CBlockLocator& locator);
102 :
103 : bool WriteOrderPosNext(int64_t nOrderPosNext);
104 :
105 : bool WriteDefaultKey(const CPubKey& vchPubKey);
106 :
107 : bool ReadPool(int64_t nPool, CKeyPool& keypool);
108 : bool WritePool(int64_t nPool, const CKeyPool& keypool);
109 : bool ErasePool(int64_t nPool);
110 :
111 : bool WriteMinVersion(int nVersion);
112 :
113 : bool ReadAccount(const std::string& strAccount, CAccount& account);
114 : bool WriteAccount(const std::string& strAccount, const CAccount& account);
115 :
116 : /// Write destination data key,value tuple to database
117 : bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
118 : /// Erase destination data tuple from wallet database
119 : bool EraseDestData(const std::string &address, const std::string &key);
120 :
121 : bool WriteAccountingEntry(const CAccountingEntry& acentry);
122 : CAmount GetAccountCreditDebit(const std::string& strAccount);
123 : void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
124 :
125 : DBErrors ReorderTransactions(CWallet* pwallet);
126 : DBErrors LoadWallet(CWallet* pwallet);
127 : DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
128 : DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
129 : static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys);
130 : static bool Recover(CDBEnv& dbenv, const std::string& filename);
131 :
132 : private:
133 : CWalletDB(const CWalletDB&);
134 : void operator=(const CWalletDB&);
135 :
136 : bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
137 : };
138 :
139 : bool BackupWallet(const CWallet& wallet, const std::string& strDest);
140 : void ThreadFlushWalletDB(const std::string& strFile);
141 :
142 : #endif // BITCOIN_WALLET_WALLETDB_H
|