Miner stats didn't work in RPC and UI
[novacoin.git] / src / wallet.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_WALLET_H
6 #define BITCOIN_WALLET_H
7
8 #include <string>
9 #include <vector>
10
11 #include <stdlib.h>
12
13 #include "main.h"
14 #include "key.h"
15 #include "keystore.h"
16 #include "script.h"
17 #include "ui_interface.h"
18 #include "util.h"
19 #include "walletdb.h"
20
21 extern unsigned int nStakeMaxAge;
22 extern bool fWalletUnlockMintOnly;
23 extern bool fConfChange;
24 class CAccountingEntry;
25 class CWalletTx;
26 class CReserveKey;
27 class COutput;
28 class CCoinControl;
29
30 // Set of selected transactions
31 typedef std::set<std::pair<const CWalletTx*,unsigned int> > CoinsSet;
32
33 /** (client) version numbers for particular wallet features */
34 enum WalletFeature
35 {
36     FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
37
38     FEATURE_WALLETCRYPT = 40000, // wallet encryption
39     FEATURE_COMPRPUBKEY = 60000, // compressed public keys
40     FEATURE_LATEST = 60000
41 };
42
43 /** A key pool entry */
44 class CKeyPool
45 {
46 public:
47     int64_t nTime;
48     CPubKey vchPubKey;
49
50     CKeyPool()
51     {
52         nTime = GetTime();
53     }
54
55     CKeyPool(const CPubKey& vchPubKeyIn)
56     {
57         nTime = GetTime();
58         vchPubKey = vchPubKeyIn;
59     }
60
61     IMPLEMENT_SERIALIZE
62     (
63         if (!(nType & SER_GETHASH))
64             READWRITE(nVersion);
65         READWRITE(nTime);
66         READWRITE(vchPubKey);
67     )
68 };
69
70 /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
71  * and provides the ability to create new transactions.
72  */
73 class CWallet : public CCryptoKeyStore
74 {
75 private:
76     bool SelectCoins(int64_t nTargetValue, unsigned int nSpendTime, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl *coinControl=NULL) const;
77
78     CWalletDB *pwalletdbEncryption, *pwalletdbDecryption;
79
80     // the current wallet version: clients below this version are not able to load the wallet
81     int nWalletVersion;
82
83     // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
84     int nWalletMaxVersion;
85
86     // stake mining statistics
87     uint64_t nKernelsTried;
88     uint64_t nCoinDaysTried;
89
90 public:
91     mutable CCriticalSection cs_wallet;
92
93     bool fFileBacked;
94     std::string strWalletFile;
95
96     std::set<int64_t> setKeyPool;
97     std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
98
99     typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
100     MasterKeyMap mapMasterKeys;
101     unsigned int nMasterKeyMaxID;
102
103     CWallet()
104     {
105         nWalletVersion = FEATURE_BASE;
106         nWalletMaxVersion = FEATURE_BASE;
107         fFileBacked = false;
108         nMasterKeyMaxID = 0;
109         pwalletdbEncryption = NULL;
110         pwalletdbDecryption = NULL;
111         nOrderPosNext = 0;
112         nKernelsTried = 0;
113         nCoinDaysTried = 0;
114     }
115     CWallet(std::string strWalletFileIn)
116     {
117         nWalletVersion = FEATURE_BASE;
118         nWalletMaxVersion = FEATURE_BASE;
119         strWalletFile = strWalletFileIn;
120         fFileBacked = true;
121         nMasterKeyMaxID = 0;
122         pwalletdbEncryption = NULL;
123         pwalletdbDecryption = NULL;
124         nOrderPosNext = 0;
125         nKernelsTried = 0;
126         nCoinDaysTried = 0;
127     }
128
129     std::map<uint256, CWalletTx> mapWallet;
130     std::vector<uint256> vMintingWalletUpdated;
131     int64_t nOrderPosNext;
132     std::map<uint256, int> mapRequestCount;
133
134     std::map<CTxDestination, std::string> mapAddressBook;
135
136     CPubKey vchDefaultKey;
137     int64_t nTimeFirstKey;
138
139     // check whether we are allowed to upgrade (or already support) to the named feature
140     bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
141
142     void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const;
143     void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
144     bool SelectCoinsMinConf(int64_t nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const;
145
146     // Simple select (without randomization)
147     bool SelectCoinsSimple(int64_t nTargetValue, int64_t nMinValue, int64_t nMaxValue, unsigned int nSpendTime, int nMinConf, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const;
148
149     // keystore implementation
150     // Generate a new key
151     CPubKey GenerateNewKey();
152     // Adds a key to the store, and saves it to disk.
153     bool AddKey(const CKey& key);
154     // Adds a key to the store, without saving it to disk (used by LoadWallet)
155     bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
156     // Load metadata (used by LoadWallet)
157     bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
158
159     bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
160
161     // Adds an encrypted key to the store, and saves it to disk.
162     bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
163     // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
164     bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
165     bool AddCScript(const CScript& redeemScript);
166     bool LoadCScript(const CScript& redeemScript);
167
168     // Adds a watch-only address to the store, and saves it to disk.
169     bool AddWatchOnly(const CScript &dest);
170     bool RemoveWatchOnly(const CScript &dest);
171     // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
172     bool LoadWatchOnly(const CScript &dest);
173
174     bool Unlock(const SecureString& strWalletPassphrase);
175     bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
176     bool EncryptWallet(const SecureString& strWalletPassphrase);
177     bool DecryptWallet(const SecureString& strWalletPassphrase);
178
179     void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
180
181
182     /** Increment the next transaction order id
183         @return next transaction order id
184      */
185     int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
186
187     typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
188     typedef std::multimap<int64_t, TxPair > TxItems;
189
190     /** Get the wallet's activity log
191         @return multimap of ordered transactions and accounting entries
192         @warning Returned pointers are *only* valid within the scope of passed acentries
193      */
194     TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
195
196     void MarkDirty();
197     bool AddToWallet(const CWalletTx& wtxIn);
198     bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
199     bool EraseFromWallet(uint256 hash);
200     void ClearOrphans();
201     void WalletUpdateSpent(const CTransaction& prevout, bool fBlock = false);
202     int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
203     int ScanForWalletTransaction(const uint256& hashTx);
204     void ReacceptWalletTransactions();
205     void ResendWalletTransactions();
206     int64_t GetBalance() const;
207     int64_t GetWatchOnlyBalance() const;
208     int64_t GetUnconfirmedBalance() const;
209     int64_t GetUnconfirmedWatchOnlyBalance() const;
210     int64_t GetImmatureBalance() const;
211     int64_t GetImmatureWatchOnlyBalance() const;
212     int64_t GetStake() const;
213     int64_t GetNewMint() const;
214     int64_t GetWatchOnlyStake() const;
215     int64_t GetWatchOnlyNewMint() const;
216     bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
217     bool CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
218     bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
219
220     void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
221     bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
222     bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
223
224     std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
225     std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
226
227     bool NewKeyPool(unsigned int nSize = 0);
228     bool TopUpKeyPool(unsigned int nSize = 0);
229     int64_t AddReserveKey(const CKeyPool& keypool);
230     void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
231     void KeepKey(int64_t nIndex);
232     void ReturnKey(int64_t nIndex);
233     bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
234     int64_t GetOldestKeyPoolTime();
235     void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
236
237     std::set< std::set<CTxDestination> > GetAddressGroupings();
238     std::map<CTxDestination, int64_t> GetAddressBalances();
239
240     isminetype IsMine(const CTxIn& txin) const;
241     int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
242     isminetype IsMine(const CTxOut& txout) const
243     {
244         return ::IsMine(*this, txout.scriptPubKey);
245     }
246     int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
247     {
248         if (!MoneyRange(txout.nValue))
249             throw std::runtime_error("CWallet::GetCredit() : value out of range");
250         return (IsMine(txout) & filter ? txout.nValue : 0);
251     }
252     bool IsChange(const CTxOut& txout) const;
253     int64_t GetChange(const CTxOut& txout) const
254     {
255         if (!MoneyRange(txout.nValue))
256             throw std::runtime_error("CWallet::GetChange() : value out of range");
257         return (IsChange(txout) ? txout.nValue : 0);
258     }
259     bool IsMine(const CTransaction& tx) const
260     {
261         BOOST_FOREACH(const CTxOut& txout, tx.vout)
262             if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
263                 return true;
264         return false;
265     }
266     bool IsFromMe(const CTransaction& tx) const
267     {
268         return (GetDebit(tx, MINE_ALL) > 0);
269     }
270     int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
271     {
272         int64_t nDebit = 0;
273         BOOST_FOREACH(const CTxIn& txin, tx.vin)
274         {
275             nDebit += GetDebit(txin, filter);
276             if (!MoneyRange(nDebit))
277                 throw std::runtime_error("CWallet::GetDebit() : value out of range");
278         }
279         return nDebit;
280     }
281     int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
282     {
283         int64_t nCredit = 0;
284         BOOST_FOREACH(const CTxOut& txout, tx.vout)
285         {
286             nCredit += GetCredit(txout, filter);
287             if (!MoneyRange(nCredit))
288                 throw std::runtime_error("CWallet::GetCredit() : value out of range");
289         }
290         return nCredit;
291     }
292     int64_t GetChange(const CTransaction& tx) const
293     {
294         int64_t nChange = 0;
295         BOOST_FOREACH(const CTxOut& txout, tx.vout)
296         {
297             nChange += GetChange(txout);
298             if (!MoneyRange(nChange))
299                 throw std::runtime_error("CWallet::GetChange() : value out of range");
300         }
301         return nChange;
302     }
303     void SetBestChain(const CBlockLocator& loc);
304
305     DBErrors LoadWallet(bool& fFirstRunRet);
306
307     DBErrors ZapWalletTx();
308
309     bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
310
311     bool DelAddressBookName(const CTxDestination& address);
312
313     void UpdatedTransaction(const uint256 &hashTx);
314
315     void PrintWallet(const CBlock& block);
316
317     void Inventory(const uint256 &hash)
318     {
319         {
320             LOCK(cs_wallet);
321             std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
322             if (mi != mapRequestCount.end())
323                 (*mi).second++;
324         }
325     }
326
327     unsigned int GetKeyPoolSize()
328     {
329         return (unsigned int)(setKeyPool.size());
330     }
331
332     bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
333
334     bool SetDefaultKey(const CPubKey &vchPubKey);
335
336     // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
337     bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
338
339     // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
340     bool SetMaxVersion(int nVersion);
341
342     // get the current wallet format (the oldest client version guaranteed to understand this wallet)
343     int GetVersion() { return nWalletVersion; }
344
345     void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
346     void DisableTransaction(const CTransaction &tx);
347
348     /** Address book entry changed.
349      * @note called with lock cs_wallet held.
350      */
351     boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
352
353     /** Wallet transaction added, removed or updated.
354      * @note called with lock cs_wallet held.
355      */
356     boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
357
358     /** Watch-only address added */
359     boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
360 };
361
362 /** A key allocated from the key pool. */
363 class CReserveKey
364 {
365 protected:
366     CWallet* pwallet;
367     int64_t nIndex;
368     CPubKey vchPubKey;
369 public:
370     CReserveKey(CWallet* pwalletIn)
371     {
372         nIndex = -1;
373         pwallet = pwalletIn;
374     }
375
376     ~CReserveKey()
377     {
378         if (!fShutdown)
379             ReturnKey();
380     }
381
382     void ReturnKey();
383     CPubKey GetReservedKey();
384     void KeepKey();
385 };
386
387
388 typedef std::map<std::string, std::string> mapValue_t;
389
390
391 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
392 {
393     if (!mapValue.count("n"))
394     {
395         nOrderPos = -1; // TODO: calculate elsewhere
396         return;
397     }
398     nOrderPos = atoi64(mapValue["n"].c_str());
399 }
400
401
402 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
403 {
404     if (nOrderPos == -1)
405         return;
406     mapValue["n"] = i64tostr(nOrderPos);
407 }
408
409
410 /** A transaction with a bunch of additional info that only the owner cares about.
411  * It includes any unrecorded transactions needed to link it back to the block chain.
412  */
413 class CWalletTx : public CMerkleTx
414 {
415 private:
416     const CWallet* pwallet;
417
418 public:
419     std::vector<CMerkleTx> vtxPrev;
420     mapValue_t mapValue;
421     std::vector<std::pair<std::string, std::string> > vOrderForm;
422     unsigned int fTimeReceivedIsTxTime;
423     unsigned int nTimeReceived;  // time received by this node
424     unsigned int nTimeSmart;
425     char fFromMe;
426     std::string strFromAccount;
427     std::vector<char> vfSpent; // which outputs are already spent
428     int64_t nOrderPos;  // position in ordered transaction list
429
430     // memory only
431     mutable bool fDebitCached;
432     mutable bool fWatchDebitCached;
433     mutable bool fCreditCached;
434     mutable bool fWatchCreditCached;
435     mutable bool fAvailableCreditCached;
436     mutable bool fImmatureCreditCached;
437     mutable bool fImmatureWatchCreditCached;
438     mutable bool fAvailableWatchCreditCached;
439     mutable bool fChangeCached;
440     mutable int64_t nDebitCached;
441     mutable int64_t nWatchDebitCached;
442     mutable int64_t nCreditCached;
443     mutable int64_t nWatchCreditCached;
444     mutable int64_t nAvailableCreditCached;
445     mutable int64_t nImmatureCreditCached;
446     mutable int64_t nImmatureWatchCreditCached;
447     mutable int64_t nAvailableWatchCreditCached;
448     mutable int64_t nChangeCached;
449
450     CWalletTx()
451     {
452         Init(NULL);
453     }
454
455     CWalletTx(const CWallet* pwalletIn)
456     {
457         Init(pwalletIn);
458     }
459
460     CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
461     {
462         Init(pwalletIn);
463     }
464
465     CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
466     {
467         Init(pwalletIn);
468     }
469
470     void Init(const CWallet* pwalletIn)
471     {
472         pwallet = pwalletIn;
473         vtxPrev.clear();
474         mapValue.clear();
475         vOrderForm.clear();
476         fTimeReceivedIsTxTime = false;
477         nTimeReceived = 0;
478         nTimeSmart = 0;
479         fFromMe = false;
480         strFromAccount.clear();
481         vfSpent.clear();
482         fDebitCached = false;
483         fWatchDebitCached = false;
484         fCreditCached = false;
485         fWatchCreditCached = false;
486         fAvailableCreditCached = false;
487         fAvailableWatchCreditCached = false;
488         fImmatureCreditCached = false;
489         fImmatureWatchCreditCached = false;
490         fChangeCached = false;
491         nDebitCached = 0;
492         nWatchDebitCached = 0;
493         nCreditCached = 0;
494         nWatchCreditCached = 0;
495         nAvailableCreditCached = 0;
496         nAvailableWatchCreditCached = 0;
497         nImmatureCreditCached = 0;
498         nImmatureWatchCreditCached = 0;
499         nChangeCached = 0;
500         nOrderPos = -1;
501     }
502
503     IMPLEMENT_SERIALIZE
504     (
505         CWalletTx* pthis = const_cast<CWalletTx*>(this);
506         if (fRead)
507             pthis->Init(NULL);
508         char fSpent = false;
509
510         if (!fRead)
511         {
512             pthis->mapValue["fromaccount"] = pthis->strFromAccount;
513
514             std::string str;
515             BOOST_FOREACH(char f, vfSpent)
516             {
517                 str += (f ? '1' : '0');
518                 if (f)
519                     fSpent = true;
520             }
521             pthis->mapValue["spent"] = str;
522
523             WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
524
525             if (nTimeSmart)
526                 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
527         }
528
529         nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
530         READWRITE(vtxPrev);
531         READWRITE(mapValue);
532         READWRITE(vOrderForm);
533         READWRITE(fTimeReceivedIsTxTime);
534         READWRITE(nTimeReceived);
535         READWRITE(fFromMe);
536         READWRITE(fSpent);
537
538         if (fRead)
539         {
540             pthis->strFromAccount = pthis->mapValue["fromaccount"];
541
542             if (mapValue.count("spent"))
543                 BOOST_FOREACH(char c, pthis->mapValue["spent"])
544                     pthis->vfSpent.push_back(c != '0');
545             else
546                 pthis->vfSpent.assign(vout.size(), fSpent);
547
548             ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
549
550             pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
551         }
552
553         pthis->mapValue.erase("fromaccount");
554         pthis->mapValue.erase("version");
555         pthis->mapValue.erase("spent");
556         pthis->mapValue.erase("n");
557         pthis->mapValue.erase("timesmart");
558     )
559
560     // marks certain txout's as spent
561     // returns true if any update took place
562     bool UpdateSpent(const std::vector<char>& vfNewSpent)
563     {
564         bool fReturn = false;
565         for (unsigned int i = 0; i < vfNewSpent.size(); i++)
566         {
567             if (i == vfSpent.size())
568                 break;
569
570             if (vfNewSpent[i] && !vfSpent[i])
571             {
572                 vfSpent[i] = true;
573                 fReturn = true;
574                 fAvailableCreditCached = fAvailableWatchCreditCached = false;
575             }
576         }
577         return fReturn;
578     }
579
580     // make sure balances are recalculated
581     void MarkDirty()
582     {
583         fCreditCached = false;
584         fAvailableCreditCached = fAvailableWatchCreditCached = false;
585         fDebitCached = fWatchDebitCached = false;
586         fChangeCached = false;
587     }
588
589     void BindWallet(CWallet *pwalletIn)
590     {
591         pwallet = pwalletIn;
592         MarkDirty();
593     }
594
595     void MarkSpent(unsigned int nOut)
596     {
597         if (nOut >= vout.size())
598             throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
599         vfSpent.resize(vout.size());
600         if (!vfSpent[nOut])
601         {
602             vfSpent[nOut] = true;
603             fAvailableCreditCached = fAvailableWatchCreditCached = false;
604         }
605     }
606
607     void MarkUnspent(unsigned int nOut)
608     {
609         if (nOut >= vout.size())
610             throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
611         vfSpent.resize(vout.size());
612         if (vfSpent[nOut])
613         {
614             vfSpent[nOut] = false;
615             fAvailableCreditCached = fAvailableWatchCreditCached = false;
616         }
617     }
618
619     bool IsSpent(unsigned int nOut) const
620     {
621         if (nOut >= vout.size())
622             throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
623         if (nOut >= vfSpent.size())
624             return false;
625         return (!!vfSpent[nOut]);
626     }
627
628     int64_t GetDebit(const isminefilter& filter) const
629     {
630         if (vin.empty())
631             return 0;
632
633         int64_t nDebit = 0;
634         if (filter & MINE_SPENDABLE)
635         {
636             if (fDebitCached)
637                 nDebit += nDebitCached;
638             else
639             {
640                 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
641                 fDebitCached = true;
642                 nDebit += nDebitCached;
643             }
644         }
645         if (filter & MINE_WATCH_ONLY)
646         {
647             if (fWatchDebitCached)
648                 nDebit += nWatchDebitCached;
649             else
650             {
651                 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
652                 fWatchDebitCached = true;
653                 nDebit += nWatchDebitCached;
654             }
655         }
656
657         return nDebit;
658     }
659
660     int64_t GetCredit(const isminefilter& filter) const
661     {
662         // Must wait until coinbase is safely deep enough in the chain before valuing it
663         if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
664             return 0;
665
666         int64_t credit = 0;
667         if (filter & MINE_SPENDABLE)
668         {
669             // GetBalance can assume transactions in mapWallet won't change
670             if (fCreditCached)
671                 credit += nCreditCached;
672             else
673             {
674                 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
675                 fCreditCached = true;
676                 credit += nCreditCached;
677             }
678         }
679         if (filter & MINE_WATCH_ONLY)
680         {
681             if (fWatchCreditCached)
682                 credit += nWatchCreditCached;
683             else
684             {
685                 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
686                 fWatchCreditCached = true;
687                 credit += nWatchCreditCached;
688             }
689         }
690         return credit;
691     }
692
693     int64_t GetImmatureCredit(bool fUseCache=true) const
694     {
695         if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
696         {
697             if (fUseCache && fImmatureCreditCached)
698                 return nImmatureCreditCached;
699             nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
700             fImmatureCreditCached = true;
701             return nImmatureCreditCached;
702         }
703
704         return 0;
705     }
706
707     int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
708     {
709         if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
710         {
711             if (fUseCache && fImmatureWatchCreditCached)
712                 return nImmatureWatchCreditCached;
713             nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
714             fImmatureWatchCreditCached = true;
715             return nImmatureWatchCreditCached;
716         }
717
718         return 0;
719     }
720
721
722     int64_t GetAvailableCredit(bool fUseCache=true) const
723     {
724         // Must wait until coinbase is safely deep enough in the chain before valuing it
725         if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
726             return 0;
727
728         if (fUseCache) {
729             if (fAvailableCreditCached)
730                 return nAvailableCreditCached;
731         }
732
733         int64_t nCredit = 0;
734         for (unsigned int i = 0; i < vout.size(); i++)
735         {
736             if (!IsSpent(i))
737             {
738                 const CTxOut &txout = vout[i];
739                 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
740                 if (!MoneyRange(nCredit))
741                     throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
742             }
743         }
744
745         nAvailableCreditCached = nCredit;
746         fAvailableCreditCached = true;
747
748         return nCredit;
749     }
750
751     int64_t GetAvailableWatchCredit(bool fUseCache=true) const
752     {
753         // Must wait until coinbase is safely deep enough in the chain before valuing it
754         if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
755             return 0;
756
757         if (fUseCache) {
758             if (fAvailableWatchCreditCached)
759                 return nAvailableWatchCreditCached;
760         }
761
762         int64_t nCredit = 0;
763         for (unsigned int i = 0; i < vout.size(); i++)
764         {
765             if (!IsSpent(i))
766             {
767                 const CTxOut &txout = vout[i];
768                 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
769                 if (!MoneyRange(nCredit))
770                     throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
771             }
772         }
773
774         nAvailableWatchCreditCached = nCredit;
775         fAvailableWatchCreditCached = true;
776
777         return nCredit;
778     }
779
780     int64_t GetChange() const
781     {
782         if (fChangeCached)
783             return nChangeCached;
784         nChangeCached = pwallet->GetChange(*this);
785         fChangeCached = true;
786         return nChangeCached;
787     }
788
789     void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CTxDestination, int64_t> >& listReceived,
790                     std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
791
792     void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
793                            int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
794
795     bool IsFromMe(const isminefilter& filter) const
796     {
797         return (GetDebit(filter) > 0);
798     }
799
800     bool IsTrusted() const
801     {
802         // Quick answer in most cases
803         if (!IsFinal())
804             return false;
805         if (GetDepthInMainChain() >= 1)
806             return true;
807         if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
808             return false;
809
810         // If no confirmations but it's from us, we can still
811         // consider it confirmed if all dependencies are confirmed
812         std::map<uint256, const CMerkleTx*> mapPrev;
813         std::vector<const CMerkleTx*> vWorkQueue;
814         vWorkQueue.reserve(vtxPrev.size()+1);
815         vWorkQueue.push_back(this);
816         for (unsigned int i = 0; i < vWorkQueue.size(); i++)
817         {
818             const CMerkleTx* ptx = vWorkQueue[i];
819
820             if (!ptx->IsFinal())
821                 return false;
822             if (ptx->GetDepthInMainChain() >= 1)
823                 continue;
824             if (!pwallet->IsFromMe(*ptx))
825                 return false;
826
827             if (mapPrev.empty())
828             {
829                 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
830                     mapPrev[tx.GetHash()] = &tx;
831             }
832
833             BOOST_FOREACH(const CTxIn& txin, ptx->vin)
834             {
835                 if (!mapPrev.count(txin.prevout.hash))
836                     return false;
837                 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
838             }
839         }
840
841         return true;
842     }
843
844     bool WriteToDisk();
845
846     int64_t GetTxTime() const;
847     int GetRequestCount() const;
848
849     void AddSupportingTransactions(CTxDB& txdb);
850
851     bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
852     bool AcceptWalletTransaction();
853
854     void RelayWalletTransaction(CTxDB& txdb);
855     void RelayWalletTransaction();
856 };
857
858
859
860
861 class COutput
862 {
863 public:
864     const CWalletTx *tx;
865     int i;
866     int nDepth;
867     bool fSpendable;
868
869     COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
870     {
871         tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
872     }
873
874     std::string ToString() const
875     {
876         return strprintf("COutput(%s, %d, %d, %d) [%s]", tx->GetHash().ToString().substr(0,10).c_str(), i, fSpendable, nDepth, FormatMoney(tx->vout[i].nValue).c_str());
877     }
878
879     void print() const
880     {
881         printf("%s\n", ToString().c_str());
882     }
883 };
884
885
886
887
888 /** Private key that includes an expiration date in case it never gets used. */
889 class CWalletKey
890 {
891 public:
892     CPrivKey vchPrivKey;
893     int64_t nTimeCreated;
894     int64_t nTimeExpires;
895     std::string strComment;
896     //// todo: add something to note what created it (user, getnewaddress, change)
897     ////   maybe should have a map<string, string> property map
898
899     CWalletKey(int64_t nExpires=0)
900     {
901         nTimeCreated = (nExpires ? GetTime() : 0);
902         nTimeExpires = nExpires;
903     }
904
905     IMPLEMENT_SERIALIZE
906     (
907         if (!(nType & SER_GETHASH))
908             READWRITE(nVersion);
909         READWRITE(vchPrivKey);
910         READWRITE(nTimeCreated);
911         READWRITE(nTimeExpires);
912         READWRITE(strComment);
913     )
914 };
915
916
917
918
919
920
921 /** Account information.
922  * Stored in wallet with key "acc"+string account name.
923  */
924 class CAccount
925 {
926 public:
927     CPubKey vchPubKey;
928
929     CAccount()
930     {
931         SetNull();
932     }
933
934     void SetNull()
935     {
936         vchPubKey = CPubKey();
937     }
938
939     IMPLEMENT_SERIALIZE
940     (
941         if (!(nType & SER_GETHASH))
942             READWRITE(nVersion);
943         READWRITE(vchPubKey);
944     )
945 };
946
947
948
949 /** Internal transfers.
950  * Database key is acentry<account><counter>.
951  */
952 class CAccountingEntry
953 {
954 public:
955     std::string strAccount;
956     int64_t nCreditDebit;
957     int64_t nTime;
958     std::string strOtherAccount;
959     std::string strComment;
960     mapValue_t mapValue;
961     int64_t nOrderPos;  // position in ordered transaction list
962     uint64_t nEntryNo;
963
964     CAccountingEntry()
965     {
966         SetNull();
967     }
968
969     void SetNull()
970     {
971         nCreditDebit = 0;
972         nTime = 0;
973         strAccount.clear();
974         strOtherAccount.clear();
975         strComment.clear();
976         nOrderPos = -1;
977     }
978
979     IMPLEMENT_SERIALIZE
980     (
981         CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
982         if (!(nType & SER_GETHASH))
983             READWRITE(nVersion);
984         // Note: strAccount is serialized as part of the key, not here.
985         READWRITE(nCreditDebit);
986         READWRITE(nTime);
987         READWRITE(strOtherAccount);
988
989         if (!fRead)
990         {
991             WriteOrderPos(nOrderPos, me.mapValue);
992
993             if (!(mapValue.empty() && _ssExtra.empty()))
994             {
995                 CDataStream ss(nType, nVersion);
996                 ss.insert(ss.begin(), '\0');
997                 ss << mapValue;
998                 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
999                 me.strComment.append(ss.str());
1000             }
1001         }
1002
1003         READWRITE(strComment);
1004
1005         size_t nSepPos = strComment.find("\0", 0, 1);
1006         if (fRead)
1007         {
1008             me.mapValue.clear();
1009             if (std::string::npos != nSepPos)
1010             {
1011                 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1012                 ss >> me.mapValue;
1013                 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1014             }
1015             ReadOrderPos(me.nOrderPos, me.mapValue);
1016         }
1017         if (std::string::npos != nSepPos)
1018             me.strComment.erase(nSepPos);
1019
1020         me.mapValue.erase("n");
1021     )
1022
1023 private:
1024     std::vector<char> _ssExtra;
1025 };
1026
1027 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
1028
1029 #endif