Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 707 lines (593 sloc) 21.052 kb
b2120e2 @TheBlueMatt Unify copyright notices.
TheBlueMatt authored
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
8821641 @gavinandresen Update all copyrights to 2012
gavinandresen authored
2 // Copyright (c) 2009-2012 The Bitcoin developers
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
3 // Distributed under the MIT/X11 software license, see the accompanying
3a25a2b @fanquake Update License in File Headers
fanquake authored
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
5 #ifndef BITCOIN_WALLET_H
6 #define BITCOIN_WALLET_H
7
ed6d0b5 @sipa Remove headers.h
sipa authored
8 #include "main.h"
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
9 #include "key.h"
2a45a49 @gavinandresen Use block times for 'hard' OP_EVAL switchover, and refactored EvalScript
gavinandresen authored
10 #include "keystore.h"
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
11 #include "script.h"
ab1b288 @laanwj Convert UI interface to boost::signals2.
laanwj authored
12 #include "ui_interface.h"
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
13
14 class CWalletTx;
15 class CReserveKey;
16 class CWalletDB;
17
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
18 /** (client) version numbers for particular wallet features */
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
19 enum WalletFeature
20 {
21 FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
22
23 FEATURE_WALLETCRYPT = 40000, // wallet encryption
24 FEATURE_COMPRPUBKEY = 60000, // compressed public keys
25
26 FEATURE_LATEST = 60000
27 };
28
ed6d0b5 @sipa Remove headers.h
sipa authored
29
30 /** A key pool entry */
31 class CKeyPool
32 {
33 public:
34 int64 nTime;
35 std::vector<unsigned char> vchPubKey;
36
37 CKeyPool()
38 {
39 nTime = GetTime();
40 }
41
42 CKeyPool(const std::vector<unsigned char>& vchPubKeyIn)
43 {
44 nTime = GetTime();
45 vchPubKey = vchPubKeyIn;
46 }
47
48 IMPLEMENT_SERIALIZE
49 (
50 if (!(nType & SER_GETHASH))
51 READWRITE(nVersion);
52 READWRITE(nTime);
53 READWRITE(vchPubKey);
54 )
55 };
56
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
57 /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
58 * and provides the ability to create new transactions.
59 */
acd6501 @sipa Prepare codebase for Encrypted Keys.
sipa authored
60 class CWallet : public CCryptoKeyStore
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
61 {
62 private:
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
63 bool SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
64 bool SelectCoins(int64 nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
65
96f34cd @TheBlueMatt Use DB Transactions when encrypting wallet.
TheBlueMatt authored
66 CWalletDB *pwalletdbEncryption;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
67
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
68 // the current wallet version: clients below this version are not able to load the wallet
0b807a4 @sipa Add SetMinVersion to CWallet
sipa authored
69 int nWalletVersion;
70
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
71 // the maxmimum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
72 int nWalletMaxVersion;
73
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
74 public:
6cc4a62 @gavinandresen Fix rpc-hanging deadlocks
gavinandresen authored
75 mutable CCriticalSection cs_wallet;
76
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
77 bool fFileBacked;
78 std::string strWalletFile;
79
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
80 std::set<int64> setKeyPool;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
81
0b807a4 @sipa Add SetMinVersion to CWallet
sipa authored
82
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
83 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
84 MasterKeyMap mapMasterKeys;
85 unsigned int nMasterKeyMaxID;
86
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
87 CWallet()
88 {
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
89 nWalletVersion = FEATURE_BASE;
90 nWalletMaxVersion = FEATURE_BASE;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
91 fFileBacked = false;
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
92 nMasterKeyMaxID = 0;
96f34cd @TheBlueMatt Use DB Transactions when encrypting wallet.
TheBlueMatt authored
93 pwalletdbEncryption = NULL;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
94 }
95 CWallet(std::string strWalletFileIn)
96 {
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
97 nWalletVersion = FEATURE_BASE;
98 nWalletMaxVersion = FEATURE_BASE;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
99 strWalletFile = strWalletFileIn;
100 fFileBacked = true;
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
101 nMasterKeyMaxID = 0;
96f34cd @TheBlueMatt Use DB Transactions when encrypting wallet.
TheBlueMatt authored
102 pwalletdbEncryption = NULL;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
103 }
104
105 std::map<uint256, CWalletTx> mapWallet;
106 std::map<uint256, int> mapRequestCount;
107
2ffba73 @sipa Use CBitcoinAddress instead of string/uint160
sipa authored
108 std::map<CBitcoinAddress, std::string> mapAddressBook;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
109
110 std::vector<unsigned char> vchDefaultKey;
111
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
112 // check whether we are allowed to upgrade (or already support) to the named feature
113 bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
114
acd6501 @sipa Prepare codebase for Encrypted Keys.
sipa authored
115 // keystore implementation
9976cf0 @sipa Move GenerateNewKey back to CWallet
sipa authored
116 // Generate a new key
117 std::vector<unsigned char> GenerateNewKey();
d825e6a @sipa Some extra comments
sipa authored
118 // Adds a key to the store, and saves it to disk.
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
119 bool AddKey(const CKey& key);
d825e6a @sipa Some extra comments
sipa authored
120 // Adds a key to the store, without saving it to disk (used by LoadWallet)
acd6501 @sipa Prepare codebase for Encrypted Keys.
sipa authored
121 bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
d825e6a @sipa Some extra comments
sipa authored
122
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
123 bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
0b807a4 @sipa Add SetMinVersion to CWallet
sipa authored
124
d825e6a @sipa Some extra comments
sipa authored
125 // Adds an encrypted key to the store, and saves it to disk.
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
126 bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
d825e6a @sipa Some extra comments
sipa authored
127 // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
128 bool LoadCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
922e8e2 @gavinandresen Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
gavinandresen authored
129 bool AddCScript(const CScript& redeemScript);
130 bool LoadCScript(const CScript& redeemScript) { return CCryptoKeyStore::AddCScript(redeemScript); }
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
131
94f778b @nobled Implement an mlock()'d string class for storing passphrases
nobled authored
132 bool Unlock(const SecureString& strWalletPassphrase);
133 bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
134 bool EncryptWallet(const SecureString& strWalletPassphrase);
acd6501 @sipa Prepare codebase for Encrypted Keys.
sipa authored
135
95d888a @sipa Key import and export
sipa authored
136 void MarkDirty();
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
137 bool AddToWallet(const CWalletTx& wtxIn);
30ab2c9 @sipa Preparations for key import/export
sipa authored
138 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
139 bool EraseFromWallet(uint256 hash);
140 void WalletUpdateSpent(const CTransaction& prevout);
141 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
30ab2c9 @sipa Preparations for key import/export
sipa authored
142 int ScanForWalletTransaction(const uint256& hashTx);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
143 void ReacceptWalletTransactions();
144 void ResendWalletTransactions();
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
145 int64 GetBalance() const;
146 int64 GetUnconfirmedBalance() const;
8fdb7e1 @sje397 Added 'immature balance' for miners. Only displayed if the balance is…
sje397 authored
147 int64 GetImmatureBalance() const;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
148 bool CreateTransaction(const std::vector<std::pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
149 bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
150 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
151 std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
152 std::string SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
153
37971fc @gavinandresen Create new keypool for newly encrypted wallets.
gavinandresen authored
154 bool NewKeyPool();
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
155 bool TopUpKeyPool();
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
156 int64 AddReserveKey(const CKeyPool& keypool);
157 void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);
158 void KeepKey(int64 nIndex);
159 void ReturnKey(int64 nIndex);
7db3b75 @gavinandresen Logic running with -keypool=0 was wrong (empty keys were being return…
gavinandresen authored
160 bool GetKeyFromPool(std::vector<unsigned char> &key, bool fAllowReuse=true);
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
161 int64 GetOldestKeyPoolTime();
30ab2c9 @sipa Preparations for key import/export
sipa authored
162 void GetAllReserveAddresses(std::set<CBitcoinAddress>& setAddress);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
163
164 bool IsMine(const CTxIn& txin) const;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
165 int64 GetDebit(const CTxIn& txin) const;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
166 bool IsMine(const CTxOut& txout) const
167 {
168 return ::IsMine(*this, txout.scriptPubKey);
169 }
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
170 int64 GetCredit(const CTxOut& txout) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
171 {
172 if (!MoneyRange(txout.nValue))
173 throw std::runtime_error("CWallet::GetCredit() : value out of range");
174 return (IsMine(txout) ? txout.nValue : 0);
175 }
e679ec9 @gavinandresen OP_EVAL implementation
gavinandresen authored
176 bool IsChange(const CTxOut& txout) const;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
177 int64 GetChange(const CTxOut& txout) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
178 {
179 if (!MoneyRange(txout.nValue))
180 throw std::runtime_error("CWallet::GetChange() : value out of range");
181 return (IsChange(txout) ? txout.nValue : 0);
182 }
183 bool IsMine(const CTransaction& tx) const
184 {
185 BOOST_FOREACH(const CTxOut& txout, tx.vout)
186 if (IsMine(txout))
187 return true;
188 return false;
189 }
190 bool IsFromMe(const CTransaction& tx) const
191 {
192 return (GetDebit(tx) > 0);
193 }
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
194 int64 GetDebit(const CTransaction& tx) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
195 {
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
196 int64 nDebit = 0;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
197 BOOST_FOREACH(const CTxIn& txin, tx.vin)
198 {
199 nDebit += GetDebit(txin);
200 if (!MoneyRange(nDebit))
201 throw std::runtime_error("CWallet::GetDebit() : value out of range");
202 }
203 return nDebit;
204 }
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
205 int64 GetCredit(const CTransaction& tx) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
206 {
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
207 int64 nCredit = 0;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
208 BOOST_FOREACH(const CTxOut& txout, tx.vout)
209 {
210 nCredit += GetCredit(txout);
211 if (!MoneyRange(nCredit))
212 throw std::runtime_error("CWallet::GetCredit() : value out of range");
213 }
214 return nCredit;
215 }
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
216 int64 GetChange(const CTransaction& tx) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
217 {
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
218 int64 nChange = 0;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
219 BOOST_FOREACH(const CTxOut& txout, tx.vout)
220 {
221 nChange += GetChange(txout);
222 if (!MoneyRange(nChange))
223 throw std::runtime_error("CWallet::GetChange() : value out of range");
224 }
225 return nChange;
226 }
ed6d0b5 @sipa Remove headers.h
sipa authored
227 void SetBestChain(const CBlockLocator& loc);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
228
116df55 Update CWallet::LoadWallet for proper return type.
Jeff Garzik authored
229 int LoadWallet(bool& fFirstRunRet);
ae3d0ab @laanwj Sync to bitcoin git e94010b2395694d56dd6
laanwj authored
230
2ffba73 @sipa Use CBitcoinAddress instead of string/uint160
sipa authored
231 bool SetAddressBookName(const CBitcoinAddress& address, const std::string& strName);
ae3d0ab @laanwj Sync to bitcoin git e94010b2395694d56dd6
laanwj authored
232
2ffba73 @sipa Use CBitcoinAddress instead of string/uint160
sipa authored
233 bool DelAddressBookName(const CBitcoinAddress& address);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
234
fe4a655 @laanwj Fine-grained UI updates
laanwj authored
235 void UpdatedTransaction(const uint256 &hashTx);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
236
237 void PrintWallet(const CBlock& block);
238
239 void Inventory(const uint256 &hash)
240 {
241 {
f8dcd5c @sipa Use scoped locks instead of CRITICAL_BLOCK
sipa authored
242 LOCK(cs_wallet);
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
243 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
244 if (mi != mapRequestCount.end())
245 (*mi).second++;
246 }
247 }
248
4e87d34 @TheBlueMatt Add wallet privkey encryption.
TheBlueMatt authored
249 int GetKeyPoolSize()
250 {
251 return setKeyPool.size();
252 }
253
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
254 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
255
ae3d0ab @laanwj Sync to bitcoin git e94010b2395694d56dd6
laanwj authored
256 bool SetDefaultKey(const std::vector<unsigned char> &vchPubKey);
0b807a4 @sipa Add SetMinVersion to CWallet
sipa authored
257
439e149 @sipa Introduce explicit -walletupgrade option
sipa authored
258 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
259 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
260
261 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
262 bool SetMaxVersion(int nVersion);
263
264 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
265 int GetVersion() { return nWalletVersion; }
ab1b288 @laanwj Convert UI interface to boost::signals2.
laanwj authored
266
267 /** Address book entry changed.
268 * @note called with lock cs_wallet held.
269 */
0832c0d @laanwj Process address book updates incrementally
laanwj authored
270 boost::signals2::signal<void (CWallet *wallet, const std::string &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
ab1b288 @laanwj Convert UI interface to boost::signals2.
laanwj authored
271
272 /** Wallet transaction added, removed or updated.
273 * @note called with lock cs_wallet held.
274 */
275 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
276 };
277
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
278 /** A key allocated from the key pool. */
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
279 class CReserveKey
280 {
281 protected:
282 CWallet* pwallet;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
283 int64 nIndex;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
284 std::vector<unsigned char> vchPubKey;
285 public:
286 CReserveKey(CWallet* pwalletIn)
287 {
288 nIndex = -1;
289 pwallet = pwalletIn;
290 }
291
292 ~CReserveKey()
293 {
294 if (!fShutdown)
295 ReturnKey();
296 }
297
298 void ReturnKey();
299 std::vector<unsigned char> GetReservedKey();
300 void KeepKey();
301 };
302
303
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
304 /** A transaction with a bunch of additional info that only the owner cares about.
305 * It includes any unrecorded transactions needed to link it back to the block chain.
306 */
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
307 class CWalletTx : public CMerkleTx
308 {
4c6e229 @sipa Make CWalletTx::pwallet private
sipa authored
309 private:
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
310 const CWallet* pwallet;
311
4c6e229 @sipa Make CWalletTx::pwallet private
sipa authored
312 public:
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
313 std::vector<CMerkleTx> vtxPrev;
314 std::map<std::string, std::string> mapValue;
315 std::vector<std::pair<std::string, std::string> > vOrderForm;
316 unsigned int fTimeReceivedIsTxTime;
317 unsigned int nTimeReceived; // time received by this node
318 char fFromMe;
319 std::string strFromAccount;
d825e6a @sipa Some extra comments
sipa authored
320 std::vector<char> vfSpent; // which outputs are already spent
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
321
322 // memory only
cdcc319 @laanwj change type of various bare chars to bool that are only used as bool …
laanwj authored
323 mutable bool fDebitCached;
324 mutable bool fCreditCached;
325 mutable bool fAvailableCreditCached;
326 mutable bool fChangeCached;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
327 mutable int64 nDebitCached;
328 mutable int64 nCreditCached;
329 mutable int64 nAvailableCreditCached;
330 mutable int64 nChangeCached;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
331
332 CWalletTx()
333 {
334 Init(NULL);
335 }
336
337 CWalletTx(const CWallet* pwalletIn)
338 {
339 Init(pwalletIn);
340 }
341
342 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
343 {
344 Init(pwalletIn);
345 }
346
347 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
348 {
349 Init(pwalletIn);
350 }
351
352 void Init(const CWallet* pwalletIn)
353 {
354 pwallet = pwalletIn;
355 vtxPrev.clear();
356 mapValue.clear();
357 vOrderForm.clear();
358 fTimeReceivedIsTxTime = false;
359 nTimeReceived = 0;
360 fFromMe = false;
361 strFromAccount.clear();
362 vfSpent.clear();
363 fDebitCached = false;
364 fCreditCached = false;
365 fAvailableCreditCached = false;
366 fChangeCached = false;
367 nDebitCached = 0;
368 nCreditCached = 0;
369 nAvailableCreditCached = 0;
370 nChangeCached = 0;
371 }
372
373 IMPLEMENT_SERIALIZE
374 (
375 CWalletTx* pthis = const_cast<CWalletTx*>(this);
376 if (fRead)
377 pthis->Init(NULL);
378 char fSpent = false;
379
380 if (!fRead)
381 {
382 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
383
384 std::string str;
385 BOOST_FOREACH(char f, vfSpent)
386 {
387 str += (f ? '1' : '0');
388 if (f)
389 fSpent = true;
390 }
391 pthis->mapValue["spent"] = str;
392 }
393
394 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
395 READWRITE(vtxPrev);
396 READWRITE(mapValue);
397 READWRITE(vOrderForm);
398 READWRITE(fTimeReceivedIsTxTime);
399 READWRITE(nTimeReceived);
400 READWRITE(fFromMe);
401 READWRITE(fSpent);
402
403 if (fRead)
404 {
405 pthis->strFromAccount = pthis->mapValue["fromaccount"];
406
407 if (mapValue.count("spent"))
408 BOOST_FOREACH(char c, pthis->mapValue["spent"])
409 pthis->vfSpent.push_back(c != '0');
410 else
411 pthis->vfSpent.assign(vout.size(), fSpent);
412 }
413
414 pthis->mapValue.erase("fromaccount");
415 pthis->mapValue.erase("version");
416 pthis->mapValue.erase("spent");
417 )
418
419 // marks certain txout's as spent
420 // returns true if any update took place
421 bool UpdateSpent(const std::vector<char>& vfNewSpent)
422 {
423 bool fReturn = false;
c376ac3 Fix loop index var types, fixing many minor sign comparison warnings
Jeff Garzik authored
424 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
425 {
426 if (i == vfSpent.size())
427 break;
428
429 if (vfNewSpent[i] && !vfSpent[i])
430 {
431 vfSpent[i] = true;
432 fReturn = true;
433 fAvailableCreditCached = false;
434 }
435 }
436 return fReturn;
437 }
438
d825e6a @sipa Some extra comments
sipa authored
439 // make sure balances are recalculated
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
440 void MarkDirty()
441 {
442 fCreditCached = false;
443 fAvailableCreditCached = false;
444 fDebitCached = false;
445 fChangeCached = false;
446 }
447
4c6e229 @sipa Make CWalletTx::pwallet private
sipa authored
448 void BindWallet(CWallet *pwalletIn)
449 {
450 pwallet = pwalletIn;
451 MarkDirty();
452 }
453
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
454 void MarkSpent(unsigned int nOut)
455 {
456 if (nOut >= vout.size())
457 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
458 vfSpent.resize(vout.size());
459 if (!vfSpent[nOut])
460 {
461 vfSpent[nOut] = true;
462 fAvailableCreditCached = false;
463 }
464 }
465
466 bool IsSpent(unsigned int nOut) const
467 {
468 if (nOut >= vout.size())
469 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
470 if (nOut >= vfSpent.size())
471 return false;
472 return (!!vfSpent[nOut]);
473 }
474
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
475 int64 GetDebit() const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
476 {
477 if (vin.empty())
478 return 0;
479 if (fDebitCached)
480 return nDebitCached;
481 nDebitCached = pwallet->GetDebit(*this);
482 fDebitCached = true;
483 return nDebitCached;
484 }
485
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
486 int64 GetCredit(bool fUseCache=true) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
487 {
488 // Must wait until coinbase is safely deep enough in the chain before valuing it
489 if (IsCoinBase() && GetBlocksToMaturity() > 0)
490 return 0;
491
492 // GetBalance can assume transactions in mapWallet won't change
493 if (fUseCache && fCreditCached)
494 return nCreditCached;
495 nCreditCached = pwallet->GetCredit(*this);
496 fCreditCached = true;
497 return nCreditCached;
498 }
499
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
500 int64 GetAvailableCredit(bool fUseCache=true) const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
501 {
502 // Must wait until coinbase is safely deep enough in the chain before valuing it
503 if (IsCoinBase() && GetBlocksToMaturity() > 0)
504 return 0;
505
506 if (fUseCache && fAvailableCreditCached)
507 return nAvailableCreditCached;
508
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
509 int64 nCredit = 0;
c376ac3 Fix loop index var types, fixing many minor sign comparison warnings
Jeff Garzik authored
510 for (unsigned int i = 0; i < vout.size(); i++)
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
511 {
512 if (!IsSpent(i))
513 {
514 const CTxOut &txout = vout[i];
515 nCredit += pwallet->GetCredit(txout);
516 if (!MoneyRange(nCredit))
517 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
518 }
519 }
520
521 nAvailableCreditCached = nCredit;
522 fAvailableCreditCached = true;
523 return nCredit;
524 }
525
526
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
527 int64 GetChange() const
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
528 {
529 if (fChangeCached)
530 return nChangeCached;
531 nChangeCached = pwallet->GetChange(*this);
532 fChangeCached = true;
533 return nChangeCached;
534 }
535
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
536 void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list<std::pair<CBitcoinAddress, int64> >& listReceived,
537 std::list<std::pair<CBitcoinAddress, int64> >& listSent, int64& nFee, std::string& strSentAccount) const;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
538
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
539 void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived,
540 int64& nSent, int64& nFee) const;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
541
542 bool IsFromMe() const
543 {
544 return (GetDebit() > 0);
545 }
546
547 bool IsConfirmed() const
548 {
549 // Quick answer in most cases
550 if (!IsFinal())
551 return false;
552 if (GetDepthInMainChain() >= 1)
553 return true;
554 if (!IsFromMe()) // using wtx's cached debit
555 return false;
556
557 // If no confirmations but it's from us, we can still
558 // consider it confirmed if all dependencies are confirmed
559 std::map<uint256, const CMerkleTx*> mapPrev;
560 std::vector<const CMerkleTx*> vWorkQueue;
561 vWorkQueue.reserve(vtxPrev.size()+1);
562 vWorkQueue.push_back(this);
c376ac3 Fix loop index var types, fixing many minor sign comparison warnings
Jeff Garzik authored
563 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
564 {
565 const CMerkleTx* ptx = vWorkQueue[i];
566
567 if (!ptx->IsFinal())
568 return false;
569 if (ptx->GetDepthInMainChain() >= 1)
570 continue;
571 if (!pwallet->IsFromMe(*ptx))
572 return false;
573
574 if (mapPrev.empty())
da7bbd9 @laanwj fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wpa…
laanwj authored
575 {
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
576 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
577 mapPrev[tx.GetHash()] = &tx;
da7bbd9 @laanwj fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wpa…
laanwj authored
578 }
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
579
580 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
581 {
582 if (!mapPrev.count(txin.prevout.hash))
583 return false;
584 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
585 }
586 }
587 return true;
588 }
589
590 bool WriteToDisk();
591
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
592 int64 GetTxTime() const;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
593 int GetRequestCount() const;
594
595 void AddSupportingTransactions(CTxDB& txdb);
596
597 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
598 bool AcceptWalletTransaction();
599
600 void RelayWalletTransaction(CTxDB& txdb);
601 void RelayWalletTransaction();
602 };
603
604
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
605 /** Private key that includes an expiration date in case it never gets used. */
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
606 class CWalletKey
607 {
608 public:
609 CPrivKey vchPrivKey;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
610 int64 nTimeCreated;
611 int64 nTimeExpires;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
612 std::string strComment;
613 //// todo: add something to note what created it (user, getnewaddress, change)
614 //// maybe should have a map<string, string> property map
615
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
616 CWalletKey(int64 nExpires=0)
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
617 {
618 nTimeCreated = (nExpires ? GetTime() : 0);
619 nTimeExpires = nExpires;
620 }
621
622 IMPLEMENT_SERIALIZE
623 (
624 if (!(nType & SER_GETHASH))
625 READWRITE(nVersion);
626 READWRITE(vchPrivKey);
627 READWRITE(nTimeCreated);
628 READWRITE(nTimeExpires);
629 READWRITE(strComment);
630 )
631 };
632
633
634
635
636
637
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
638 /** Account information.
639 * Stored in wallet with key "acc"+string account name.
640 */
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
641 class CAccount
642 {
643 public:
644 std::vector<unsigned char> vchPubKey;
645
646 CAccount()
647 {
648 SetNull();
649 }
650
651 void SetNull()
652 {
653 vchPubKey.clear();
654 }
655
656 IMPLEMENT_SERIALIZE
657 (
658 if (!(nType & SER_GETHASH))
659 READWRITE(nVersion);
660 READWRITE(vchPubKey);
661 )
662 };
663
664
665
6b8de05 @sipa Begin doxygen-compatible comments
sipa authored
666 /** Internal transfers.
667 * Database key is acentry<account><counter>.
668 */
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
669 class CAccountingEntry
670 {
671 public:
672 std::string strAccount;
bde280b @laanwj Revert "Use standard C99 (and Qt) types for 64-bit integers"
laanwj authored
673 int64 nCreditDebit;
674 int64 nTime;
e8ef3da @laanwj update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
laanwj authored
675 std::string strOtherAccount;
676 std::string strComment;
677
678 CAccountingEntry()
679 {
680 SetNull();
681 }
682
683 void SetNull()
684 {
685 nCreditDebit = 0;
686 nTime = 0;
687 strAccount.clear();
688 strOtherAccount.clear();
689 strComment.clear();
690 }
691
692 IMPLEMENT_SERIALIZE
693 (
694 if (!(nType & SER_GETHASH))
695 READWRITE(nVersion);
696 // Note: strAccount is serialized as part of the key, not here.
697 READWRITE(nCreditDebit);
698 READWRITE(nTime);
699 READWRITE(strOtherAccount);
700 READWRITE(strComment);
701 )
702 };
703
704 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
705
706 #endif
Something went wrong with that request. Please try again.