New stake miner
[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 GetStakeStats(float &nKernelsRate, float &nCoinDaysRate);
221     void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
222     bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
223     bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
224
225     std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
226     std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
227
228     bool NewKeyPool(unsigned int nSize = 0);
229     bool TopUpKeyPool(unsigned int nSize = 0);
230     int64_t AddReserveKey(const CKeyPool& keypool);
231     void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
232     void KeepKey(int64_t nIndex);
233     void ReturnKey(int64_t nIndex);
234     bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
235     int64_t GetOldestKeyPoolTime();
236     void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
237
238     std::set< std::set<CTxDestination> > GetAddressGroupings();
239     std::map<CTxDestination, int64_t> GetAddressBalances();
240
241     isminetype IsMine(const CTxIn& txin) const;
242     int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
243     isminetype IsMine(const CTxOut& txout) const
244     {
245         return ::IsMine(*this, txout.scriptPubKey);
246     }
247     int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
248     {
249         if (!MoneyRange(txout.nValue))
250             throw std::runtime_error("CWallet::GetCredit() : value out of range");
251         return (IsMine(txout) & filter ? txout.nValue : 0);
252     }
253     bool IsChange(const CTxOut& txout) const;
254     int64_t GetChange(const CTxOut& txout) const
255     {
256         if (!MoneyRange(txout.nValue))
257             throw std::runtime_error("CWallet::GetChange() : value out of range");
258         return (IsChange(txout) ? txout.nValue : 0);
259     }
260     bool IsMine(const CTransaction& tx) const
261     {
262         BOOST_FOREACH(const CTxOut& txout, tx.vout)
263             if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
264                 return true;
265         return false;
266     }
267     bool IsFromMe(const CTransaction& tx) const
268     {
269         return (GetDebit(tx, MINE_ALL) > 0);
270     }
271     int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
272     {
273         int64_t nDebit = 0;
274         BOOST_FOREACH(const CTxIn& txin, tx.vin)
275         {
276             nDebit += GetDebit(txin, filter);
277             if (!MoneyRange(nDebit))
278                 throw std::runtime_error("CWallet::GetDebit() : value out of range");
279         }
280         return nDebit;
281     }
282     int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
283     {
284         int64_t nCredit = 0;
285         BOOST_FOREACH(const CTxOut& txout, tx.vout)
286         {
287             nCredit += GetCredit(txout, filter);
288             if (!MoneyRange(nCredit))
289                 throw std::runtime_error("CWallet::GetCredit() : value out of range");
290         }
291         return nCredit;
292     }
293     int64_t GetChange(const CTransaction& tx) const
294     {
295         int64_t nChange = 0;
296         BOOST_FOREACH(const CTxOut& txout, tx.vout)
297         {
298             nChange += GetChange(txout);
299             if (!MoneyRange(nChange))
300                 throw std::runtime_error("CWallet::GetChange() : value out of range");
301         }
302         return nChange;
303     }
304     void SetBestChain(const CBlockLocator& loc);
305
306     DBErrors LoadWallet(bool& fFirstRunRet);
307
308     DBErrors ZapWalletTx();
309
310     bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
311
312     bool DelAddressBookName(const CTxDestination& address);
313
314     void UpdatedTransaction(const uint256 &hashTx);
315
316     void PrintWallet(const CBlock& block);
317
318     void Inventory(const uint256 &hash)
319     {
320         {
321             LOCK(cs_wallet);
322             std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
323             if (mi != mapRequestCount.end())
324                 (*mi).second++;
325         }
326     }
327
328     unsigned int GetKeyPoolSize()
329     {
330         return (unsigned int)(setKeyPool.size());
331     }
332
333     bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
334
335     bool SetDefaultKey(const CPubKey &vchPubKey);
336
337     // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
338     bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
339
340     // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
341     bool SetMaxVersion(int nVersion);
342
343     // get the current wallet format (the oldest client version guaranteed to understand this wallet)
344     int GetVersion() { return nWalletVersion; }
345
346     void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
347     void DisableTransaction(const CTransaction &tx);
348
349     /** Address book entry changed.
350      * @note called with lock cs_wallet held.
351      */
352     boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
353
354     /** Wallet transaction added, removed or updated.
355      * @note called with lock cs_wallet held.
356      */
357     boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
358
359     /** Watch-only address added */
360     boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
361 };
362
363 /** A key allocated from the key pool. */
364 class CReserveKey
365 {
366 protected:
367     CWallet* pwallet;
368     int64_t nIndex;
369     CPubKey vchPubKey;
370 public:
371     CReserveKey(CWallet* pwalletIn)
372     {
373         nIndex = -1;
374         pwallet = pwalletIn;
375     }
376
377     ~CReserveKey()
378     {
379         if (!fShutdown)
380             ReturnKey();
381     }
382
383     void ReturnKey();
384     CPubKey GetReservedKey();
385     void KeepKey();
386 };
387
388
389 typedef std::map<std::string, std::string> mapValue_t;
390
391
392 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
393 {
394     if (!mapValue.count("n"))
395     {
396         nOrderPos = -1; // TODO: calculate elsewhere
397         return;
398     }
399     nOrderPos = atoi64(mapValue["n"].c_str());
400 }
401
402
403 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
404 {
405     if (nOrderPos == -1)
406         return;
407     mapValue["n"] = i64tostr(nOrderPos);
408 }
409
410
411 /** A transaction with a bunch of additional info that only the owner cares about.
412  * It includes any unrecorded transactions needed to link it back to the block chain.
413  */
414 class CWalletTx : public CMerkleTx
415 {
416 private:
417     const CWallet* pwallet;
418
419 public:
420     std::vector<CMerkleTx> vtxPrev;
421     mapValue_t mapValue;
422     std::vector<std::pair<std::string, std::string> > vOrderForm;
423     unsigned int fTimeReceivedIsTxTime;
424     unsigned int nTimeReceived;  // time received by this node
425     unsigned int nTimeSmart;
426     char fFromMe;
427     std::string strFromAccount;
428     std::vector<char> vfSpent; // which outputs are already spent
429     int64_t nOrderPos;  // position in ordered transaction list
430
431     // memory only
432     mutable bool fDebitCached;
433     mutable bool fWatchDebitCached;
434     mutable bool fCreditCached;
435     mutable bool fWatchCreditCached;
436     mutable bool fAvailableCreditCached;
437     mutable bool fImmatureCreditCached;
438     mutable bool fImmatureWatchCreditCached;
439     mutable bool fAvailableWatchCreditCached;
440     mutable bool fChangeCached;
441     mutable int64_t nDebitCached;
442     mutable int64_t nWatchDebitCached;
443     mutable int64_t nCreditCached;
444     mutable int64_t nWatchCreditCached;
445     mutable int64_t nAvailableCreditCached;
446     mutable int64_t nImmatureCreditCached;
447     mutable int64_t nImmatureWatchCreditCached;
448     mutable int64_t nAvailableWatchCreditCached;
449     mutable int64_t nChangeCached;
450
451     CWalletTx()
452     {
453         Init(NULL);
454     }
455
456     CWalletTx(const CWallet* pwalletIn)
457     {
458         Init(pwalletIn);
459     }
460
461     CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
462     {
463         Init(pwalletIn);
464     }
465
466     CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
467     {
468         Init(pwalletIn);
469     }
470
471     void Init(const CWallet* pwalletIn)
472     {
473         pwallet = pwalletIn;
474         vtxPrev.clear();
475         mapValue.clear();
476         vOrderForm.clear();
477         fTimeReceivedIsTxTime = false;
478         nTimeReceived = 0;
479         nTimeSmart = 0;
480         fFromMe = false;
481         strFromAccount.clear();
482         vfSpent.clear();
483         fDebitCached = false;
484         fWatchDebitCached = false;
485         fCreditCached = false;
486         fWatchCreditCached = false;
487         fAvailableCreditCached = false;
488         fAvailableWatchCreditCached = false;
489         fImmatureCreditCached = false;
490         fImmatureWatchCreditCached = false;
491         fChangeCached = false;
492         nDebitCached = 0;
493         nWatchDebitCached = 0;
494         nCreditCached = 0;
495         nWatchCreditCached = 0;
496         nAvailableCreditCached = 0;
497         nAvailableWatchCreditCached = 0;
498         nImmatureCreditCached = 0;
499         nImmatureWatchCreditCached = 0;
500         nChangeCached = 0;
501         nOrderPos = -1;
502     }
503
504     IMPLEMENT_SERIALIZE
505     (
506         CWalletTx* pthis = const_cast<CWalletTx*>(this);
507         if (fRead)
508             pthis->Init(NULL);
509         char fSpent = false;
510
511         if (!fRead)
512         {
513             pthis->mapValue["fromaccount"] = pthis->strFromAccount;
514
515             std::string str;
516             BOOST_FOREACH(char f, vfSpent)
517             {
518                 str += (f ? '1' : '0');
519                 if (f)
520                     fSpent = true;
521             }
522             pthis->mapValue["spent"] = str;
523
524             WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
525
526             if (nTimeSmart)
527                 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
528         }
529
530         nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
531         READWRITE(vtxPrev);
532         READWRITE(mapValue);
533         READWRITE(vOrderForm);
534         READWRITE(fTimeReceivedIsTxTime);
535         READWRITE(nTimeReceived);
536         READWRITE(fFromMe);
537         READWRITE(fSpent);
538
539         if (fRead)
540         {
541             pthis->strFromAccount = pthis->mapValue["fromaccount"];
542
543             if (mapValue.count("spent"))
544                 BOOST_FOREACH(char c, pthis->mapValue["spent"])
545                     pthis->vfSpent.push_back(c != '0');
546             else
547                 pthis->vfSpent.assign(vout.size(), fSpent);
548
549             ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
550
551             pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
552         }
553
554         pthis->mapValue.erase("fromaccount");
555         pthis->mapValue.erase("version");
556         pthis->mapValue.erase("spent");
557         pthis->mapValue.erase("n");
558         pthis->mapValue.erase("timesmart");
559     )
560
561     // marks certain txout's as spent
562     // returns true if any update took place
563     bool UpdateSpent(const std::vector<char>& vfNewSpent)
564     {
565         bool fReturn = false;
566         for (unsigned int i = 0; i < vfNewSpent.size(); i++)
567         {
568             if (i == vfSpent.size())
569                 break;
570
571             if (vfNewSpent[i] && !vfSpent[i])
572             {
573                 vfSpent[i] = true;
574                 fReturn = true;
575                 fAvailableCreditCached = fAvailableWatchCreditCached = false;
576             }
577         }
578         return fReturn;
579     }
580
581     // make sure balances are recalculated
582     void MarkDirty()
583     {
584         fCreditCached = false;
585         fAvailableCreditCached = fAvailableWatchCreditCached = false;
586         fDebitCached = fWatchDebitCached = false;
587         fChangeCached = false;
588     }
589
590     void BindWallet(CWallet *pwalletIn)
591     {
592         pwallet = pwalletIn;
593         MarkDirty();
594     }
595
596     void MarkSpent(unsigned int nOut)
597     {
598         if (nOut >= vout.size())
599             throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
600         vfSpent.resize(vout.size());
601         if (!vfSpent[nOut])
602         {
603             vfSpent[nOut] = true;
604             fAvailableCreditCached = fAvailableWatchCreditCached = false;
605         }
606     }
607
608     void MarkUnspent(unsigned int nOut)
609     {
610         if (nOut >= vout.size())
611             throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
612         vfSpent.resize(vout.size());
613         if (vfSpent[nOut])
614         {
615             vfSpent[nOut] = false;
616             fAvailableCreditCached = fAvailableWatchCreditCached = false;
617         }
618     }
619
620     bool IsSpent(unsigned int nOut) const
621     {
622         if (nOut >= vout.size())
623             throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
624         if (nOut >= vfSpent.size())
625             return false;
626         return (!!vfSpent[nOut]);
627     }
628
629     int64_t GetDebit(const isminefilter& filter) const
630     {
631         if (vin.empty())
632             return 0;
633
634         int64_t nDebit = 0;
635         if (filter & MINE_SPENDABLE)
636         {
637             if (fDebitCached)
638                 nDebit += nDebitCached;
639             else
640             {
641                 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
642                 fDebitCached = true;
643                 nDebit += nDebitCached;
644             }
645         }
646         if (filter & MINE_WATCH_ONLY)
647         {
648             if (fWatchDebitCached)
649                 nDebit += nWatchDebitCached;
650             else
651             {
652                 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
653                 fWatchDebitCached = true;
654                 nDebit += nWatchDebitCached;
655             }
656         }
657
658         return nDebit;
659     }
660
661     int64_t GetCredit(const isminefilter& filter) const
662     {
663         // Must wait until coinbase is safely deep enough in the chain before valuing it
664         if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
665             return 0;
666
667         int64_t credit = 0;
668         if (filter & MINE_SPENDABLE)
669         {
670             // GetBalance can assume transactions in mapWallet won't change
671             if (fCreditCached)
672                 credit += nCreditCached;
673             else
674             {
675                 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
676                 fCreditCached = true;
677                 credit += nCreditCached;
678             }
679         }
680         if (filter & MINE_WATCH_ONLY)
681         {
682             if (fWatchCreditCached)
683                 credit += nWatchCreditCached;
684             else
685             {
686                 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
687                 fWatchCreditCached = true;
688                 credit += nWatchCreditCached;
689             }
690         }
691         return credit;
692     }
693
694     int64_t GetImmatureCredit(bool fUseCache=true) const
695     {
696         if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
697         {
698             if (fUseCache && fImmatureCreditCached)
699                 return nImmatureCreditCached;
700             nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
701             fImmatureCreditCached = true;
702             return nImmatureCreditCached;
703         }
704
705         return 0;
706     }
707
708     int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
709     {
710         if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
711         {
712             if (fUseCache && fImmatureWatchCreditCached)
713                 return nImmatureWatchCreditCached;
714             nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
715             fImmatureWatchCreditCached = true;
716             return nImmatureWatchCreditCached;
717         }
718
719         return 0;
720     }
721
722
723     int64_t GetAvailableCredit(bool fUseCache=true) const
724     {
725         // Must wait until coinbase is safely deep enough in the chain before valuing it
726         if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
727             return 0;
728
729         if (fUseCache) {
730             if (fAvailableCreditCached)
731                 return nAvailableCreditCached;
732         }
733
734         int64_t nCredit = 0;
735         for (unsigned int i = 0; i < vout.size(); i++)
736         {
737             if (!IsSpent(i))
738             {
739                 const CTxOut &txout = vout[i];
740                 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
741                 if (!MoneyRange(nCredit))
742                     throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
743             }
744         }
745
746         nAvailableCreditCached = nCredit;
747         fAvailableCreditCached = true;
748
749         return nCredit;
750     }
751
752     int64_t GetAvailableWatchCredit(bool fUseCache=true) const
753     {
754         // Must wait until coinbase is safely deep enough in the chain before valuing it
755         if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
756             return 0;
757
758         if (fUseCache) {
759             if (fAvailableWatchCreditCached)
760                 return nAvailableWatchCreditCached;
761         }
762
763         int64_t nCredit = 0;
764         for (unsigned int i = 0; i < vout.size(); i++)
765         {
766             if (!IsSpent(i))
767             {
768                 const CTxOut &txout = vout[i];
769                 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
770                 if (!MoneyRange(nCredit))
771                     throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
772             }
773         }
774
775         nAvailableWatchCreditCached = nCredit;
776         fAvailableWatchCreditCached = true;
777
778         return nCredit;
779     }
780
781     int64_t GetChange() const
782     {
783         if (fChangeCached)
784             return nChangeCached;
785         nChangeCached = pwallet->GetChange(*this);
786         fChangeCached = true;
787         return nChangeCached;
788     }
789
790     void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CTxDestination, int64_t> >& listReceived,
791                     std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
792
793     void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
794                            int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
795
796     bool IsFromMe(const isminefilter& filter) const
797     {
798         return (GetDebit(filter) > 0);
799     }
800
801     bool IsTrusted() const
802     {
803         // Quick answer in most cases
804         if (!IsFinal())
805             return false;
806         if (GetDepthInMainChain() >= 1)
807             return true;
808         if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
809             return false;
810
811         // If no confirmations but it's from us, we can still
812         // consider it confirmed if all dependencies are confirmed
813         std::map<uint256, const CMerkleTx*> mapPrev;
814         std::vector<const CMerkleTx*> vWorkQueue;
815         vWorkQueue.reserve(vtxPrev.size()+1);
816         vWorkQueue.push_back(this);
817         for (unsigned int i = 0; i < vWorkQueue.size(); i++)
818         {
819             const CMerkleTx* ptx = vWorkQueue[i];
820
821             if (!ptx->IsFinal())
822                 return false;
823             if (ptx->GetDepthInMainChain() >= 1)
824                 continue;
825             if (!pwallet->IsFromMe(*ptx))
826                 return false;
827
828             if (mapPrev.empty())
829             {
830                 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
831                     mapPrev[tx.GetHash()] = &tx;
832             }
833
834             BOOST_FOREACH(const CTxIn& txin, ptx->vin)
835             {
836                 if (!mapPrev.count(txin.prevout.hash))
837                     return false;
838                 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
839             }
840         }
841
842         return true;
843     }
844
845     bool WriteToDisk();
846
847     int64_t GetTxTime() const;
848     int GetRequestCount() const;
849
850     void AddSupportingTransactions(CTxDB& txdb);
851
852     bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
853     bool AcceptWalletTransaction();
854
855     void RelayWalletTransaction(CTxDB& txdb);
856     void RelayWalletTransaction();
857 };
858
859
860
861
862 class COutput
863 {
864 public:
865     const CWalletTx *tx;
866     int i;
867     int nDepth;
868     bool fSpendable;
869
870     COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
871     {
872         tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
873     }
874
875     std::string ToString() const
876     {
877         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());
878     }
879
880     void print() const
881     {
882         printf("%s\n", ToString().c_str());
883     }
884 };
885
886
887
888
889 /** Private key that includes an expiration date in case it never gets used. */
890 class CWalletKey
891 {
892 public:
893     CPrivKey vchPrivKey;
894     int64_t nTimeCreated;
895     int64_t nTimeExpires;
896     std::string strComment;
897     //// todo: add something to note what created it (user, getnewaddress, change)
898     ////   maybe should have a map<string, string> property map
899
900     CWalletKey(int64_t nExpires=0)
901     {
902         nTimeCreated = (nExpires ? GetTime() : 0);
903         nTimeExpires = nExpires;
904     }
905
906     IMPLEMENT_SERIALIZE
907     (
908         if (!(nType & SER_GETHASH))
909             READWRITE(nVersion);
910         READWRITE(vchPrivKey);
911         READWRITE(nTimeCreated);
912         READWRITE(nTimeExpires);
913         READWRITE(strComment);
914     )
915 };
916
917
918
919
920
921
922 /** Account information.
923  * Stored in wallet with key "acc"+string account name.
924  */
925 class CAccount
926 {
927 public:
928     CPubKey vchPubKey;
929
930     CAccount()
931     {
932         SetNull();
933     }
934
935     void SetNull()
936     {
937         vchPubKey = CPubKey();
938     }
939
940     IMPLEMENT_SERIALIZE
941     (
942         if (!(nType & SER_GETHASH))
943             READWRITE(nVersion);
944         READWRITE(vchPubKey);
945     )
946 };
947
948
949
950 /** Internal transfers.
951  * Database key is acentry<account><counter>.
952  */
953 class CAccountingEntry
954 {
955 public:
956     std::string strAccount;
957     int64_t nCreditDebit;
958     int64_t nTime;
959     std::string strOtherAccount;
960     std::string strComment;
961     mapValue_t mapValue;
962     int64_t nOrderPos;  // position in ordered transaction list
963     uint64_t nEntryNo;
964
965     CAccountingEntry()
966     {
967         SetNull();
968     }
969
970     void SetNull()
971     {
972         nCreditDebit = 0;
973         nTime = 0;
974         strAccount.clear();
975         strOtherAccount.clear();
976         strComment.clear();
977         nOrderPos = -1;
978     }
979
980     IMPLEMENT_SERIALIZE
981     (
982         CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
983         if (!(nType & SER_GETHASH))
984             READWRITE(nVersion);
985         // Note: strAccount is serialized as part of the key, not here.
986         READWRITE(nCreditDebit);
987         READWRITE(nTime);
988         READWRITE(strOtherAccount);
989
990         if (!fRead)
991         {
992             WriteOrderPos(nOrderPos, me.mapValue);
993
994             if (!(mapValue.empty() && _ssExtra.empty()))
995             {
996                 CDataStream ss(nType, nVersion);
997                 ss.insert(ss.begin(), '\0');
998                 ss << mapValue;
999                 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
1000                 me.strComment.append(ss.str());
1001             }
1002         }
1003
1004         READWRITE(strComment);
1005
1006         size_t nSepPos = strComment.find("\0", 0, 1);
1007         if (fRead)
1008         {
1009             me.mapValue.clear();
1010             if (std::string::npos != nSepPos)
1011             {
1012                 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1013                 ss >> me.mapValue;
1014                 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1015             }
1016             ReadOrderPos(me.nOrderPos, me.mapValue);
1017         }
1018         if (std::string::npos != nSepPos)
1019             me.strComment.erase(nSepPos);
1020
1021         me.mapValue.erase("n");
1022     )
1023
1024 private:
1025     std::vector<char> _ssExtra;
1026 };
1027
1028 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
1029
1030 #endif