1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
9 #include "checkpoints.h"
10 #include "coincontrol.h"
14 #include <boost/algorithm/string/replace.hpp>
19 CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
20 unsigned int nTxConfirmTarget = 1;
21 bool bSpendZeroConfChange = true;
23 /** Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) */
24 CFeeRate CWallet::minTxFee = CFeeRate(10000); // Override with -mintxfee
26 //////////////////////////////////////////////////////////////////////////////
31 struct CompareValueOnly
33 bool operator()(const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t1,
34 const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t2) const
36 return t1.first < t2.first;
40 const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
43 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash);
44 if (it == mapWallet.end())
49 CPubKey CWallet::GenerateNewKey()
51 AssertLockHeld(cs_wallet); // mapKeyMetadata
52 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
56 secret.MakeNewKey(fCompressed);
58 // Compressed public keys were introduced in version 0.6.0
60 SetMinVersion(FEATURE_COMPRPUBKEY);
62 CPubKey pubkey = secret.GetPubKey();
64 // Create new metadata
65 int64_t nCreationTime = GetTime();
66 mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
67 if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
68 nTimeFirstKey = nCreationTime;
70 if (!AddKeyPubKey(secret, pubkey))
71 throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
75 bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
77 AssertLockHeld(cs_wallet); // mapKeyMetadata
78 if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey))
83 return CWalletDB(strWalletFile).WriteKey(pubkey,
85 mapKeyMetadata[pubkey.GetID()]);
90 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
91 const vector<unsigned char> &vchCryptedSecret)
93 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
99 if (pwalletdbEncryption)
100 return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
102 mapKeyMetadata[vchPubKey.GetID()]);
104 return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey,
106 mapKeyMetadata[vchPubKey.GetID()]);
111 bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
113 AssertLockHeld(cs_wallet); // mapKeyMetadata
114 if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
115 nTimeFirstKey = meta.nCreateTime;
117 mapKeyMetadata[pubkey.GetID()] = meta;
121 bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
123 return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
126 bool CWallet::AddCScript(const CScript& redeemScript)
128 if (!CCryptoKeyStore::AddCScript(redeemScript))
132 return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
135 bool CWallet::LoadCScript(const CScript& redeemScript)
137 /* A sanity check was added in pull #3843 to avoid adding redeemScripts
138 * that never can be redeemed. However, old wallets may still contain
139 * these. Do not add them to the wallet and warn. */
140 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
142 std::string strAddr = CBitcoinAddress(redeemScript.GetID()).ToString();
143 LogPrintf("%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n",
144 __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
148 return CCryptoKeyStore::AddCScript(redeemScript);
151 bool CWallet::AddWatchOnly(const CScript &dest)
153 if (!CCryptoKeyStore::AddWatchOnly(dest))
155 nTimeFirstKey = 1; // No birthday information for watch-only keys.
158 return CWalletDB(strWalletFile).WriteWatchOnly(dest);
161 bool CWallet::LoadWatchOnly(const CScript &dest)
163 return CCryptoKeyStore::AddWatchOnly(dest);
166 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
169 CKeyingMaterial vMasterKey;
173 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
175 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
177 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
178 continue; // try another master key
179 if (CCryptoKeyStore::Unlock(vMasterKey))
186 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
188 bool fWasLocked = IsLocked();
195 CKeyingMaterial vMasterKey;
196 BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
198 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
200 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
202 if (CCryptoKeyStore::Unlock(vMasterKey))
204 int64_t nStartTime = GetTimeMillis();
205 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
206 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
208 nStartTime = GetTimeMillis();
209 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
210 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
212 if (pMasterKey.second.nDeriveIterations < 25000)
213 pMasterKey.second.nDeriveIterations = 25000;
215 LogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
217 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
219 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
221 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
232 void CWallet::SetBestChain(const CBlockLocator& loc)
234 CWalletDB walletdb(strWalletFile);
235 walletdb.WriteBestBlock(loc);
238 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
240 LOCK(cs_wallet); // nWalletVersion
241 if (nWalletVersion >= nVersion)
244 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
245 if (fExplicit && nVersion > nWalletMaxVersion)
246 nVersion = FEATURE_LATEST;
248 nWalletVersion = nVersion;
250 if (nVersion > nWalletMaxVersion)
251 nWalletMaxVersion = nVersion;
255 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
256 if (nWalletVersion > 40000)
257 pwalletdb->WriteMinVersion(nWalletVersion);
265 bool CWallet::SetMaxVersion(int nVersion)
267 LOCK(cs_wallet); // nWalletVersion, nWalletMaxVersion
268 // cannot downgrade below current version
269 if (nWalletVersion > nVersion)
272 nWalletMaxVersion = nVersion;
277 set<uint256> CWallet::GetConflicts(const uint256& txid) const
280 AssertLockHeld(cs_wallet);
282 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(txid);
283 if (it == mapWallet.end())
285 const CWalletTx& wtx = it->second;
287 std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
289 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
291 if (mapTxSpends.count(txin.prevout) <= 1)
292 continue; // No conflict if zero or one spends
293 range = mapTxSpends.equal_range(txin.prevout);
294 for (TxSpends::const_iterator it = range.first; it != range.second; ++it)
295 result.insert(it->second);
300 void CWallet::SyncMetaData(pair<TxSpends::iterator, TxSpends::iterator> range)
302 // We want all the wallet transactions in range to have the same metadata as
303 // the oldest (smallest nOrderPos).
304 // So: find smallest nOrderPos:
306 int nMinOrderPos = std::numeric_limits<int>::max();
307 const CWalletTx* copyFrom = NULL;
308 for (TxSpends::iterator it = range.first; it != range.second; ++it)
310 const uint256& hash = it->second;
311 int n = mapWallet[hash].nOrderPos;
312 if (n < nMinOrderPos)
315 copyFrom = &mapWallet[hash];
318 // Now copy data from copyFrom to rest:
319 for (TxSpends::iterator it = range.first; it != range.second; ++it)
321 const uint256& hash = it->second;
322 CWalletTx* copyTo = &mapWallet[hash];
323 if (copyFrom == copyTo) continue;
324 copyTo->mapValue = copyFrom->mapValue;
325 copyTo->vOrderForm = copyFrom->vOrderForm;
326 // fTimeReceivedIsTxTime not copied on purpose
327 // nTimeReceived not copied on purpose
328 copyTo->nTimeSmart = copyFrom->nTimeSmart;
329 copyTo->fFromMe = copyFrom->fFromMe;
330 copyTo->strFromAccount = copyFrom->strFromAccount;
331 // nOrderPos not copied on purpose
332 // cached members not copied on purpose
336 // Outpoint is spent if any non-conflicted transaction
338 bool CWallet::IsSpent(const uint256& hash, unsigned int n) const
340 const COutPoint outpoint(hash, n);
341 pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
342 range = mapTxSpends.equal_range(outpoint);
344 for (TxSpends::const_iterator it = range.first; it != range.second; ++it)
346 const uint256& wtxid = it->second;
347 std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
348 if (mit != mapWallet.end() && mit->second.GetDepthInMainChain() >= 0)
349 return true; // Spent
354 void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid)
356 mapTxSpends.insert(make_pair(outpoint, wtxid));
358 pair<TxSpends::iterator, TxSpends::iterator> range;
359 range = mapTxSpends.equal_range(outpoint);
364 void CWallet::AddToSpends(const uint256& wtxid)
366 assert(mapWallet.count(wtxid));
367 CWalletTx& thisTx = mapWallet[wtxid];
368 if (thisTx.IsCoinBase()) // Coinbases don't spend anything!
371 BOOST_FOREACH(const CTxIn& txin, thisTx.vin)
372 AddToSpends(txin.prevout, wtxid);
375 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
380 CKeyingMaterial vMasterKey;
381 RandAddSeedPerfmon();
383 vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
384 if (!GetRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE))
387 CMasterKey kMasterKey;
388 RandAddSeedPerfmon();
390 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
391 if (!GetRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE))
395 int64_t nStartTime = GetTimeMillis();
396 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
397 kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
399 nStartTime = GetTimeMillis();
400 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
401 kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
403 if (kMasterKey.nDeriveIterations < 25000)
404 kMasterKey.nDeriveIterations = 25000;
406 LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
408 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
410 if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
415 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
418 pwalletdbEncryption = new CWalletDB(strWalletFile);
419 if (!pwalletdbEncryption->TxnBegin())
421 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
424 if (!EncryptKeys(vMasterKey))
427 pwalletdbEncryption->TxnAbort();
428 exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
431 // Encryption was introduced in version 0.4.0
432 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
436 if (!pwalletdbEncryption->TxnCommit())
437 exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.
439 delete pwalletdbEncryption;
440 pwalletdbEncryption = NULL;
444 Unlock(strWalletPassphrase);
448 // Need to completely rewrite the wallet file; if we don't, bdb might keep
449 // bits of the unencrypted private key in slack space in the database file.
450 CDB::Rewrite(strWalletFile);
453 NotifyStatusChanged(this);
458 int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
460 AssertLockHeld(cs_wallet); // nOrderPosNext
461 int64_t nRet = nOrderPosNext++;
463 pwalletdb->WriteOrderPosNext(nOrderPosNext);
465 CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
470 CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
472 AssertLockHeld(cs_wallet); // mapWallet
473 CWalletDB walletdb(strWalletFile);
475 // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
478 // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
479 // would make this much faster for applications that do this a lot.
480 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
482 CWalletTx* wtx = &((*it).second);
483 txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
486 walletdb.ListAccountCreditDebit(strAccount, acentries);
487 BOOST_FOREACH(CAccountingEntry& entry, acentries)
489 txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
495 void CWallet::MarkDirty()
499 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
500 item.second.MarkDirty();
504 bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet)
506 uint256 hash = wtxIn.GetHash();
510 mapWallet[hash] = wtxIn;
511 mapWallet[hash].BindWallet(this);
517 // Inserts only if not already there, returns tx inserted or tx found
518 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
519 CWalletTx& wtx = (*ret.first).second;
520 wtx.BindWallet(this);
521 bool fInsertedNew = ret.second;
524 wtx.nTimeReceived = GetAdjustedTime();
525 wtx.nOrderPos = IncOrderPosNext();
527 wtx.nTimeSmart = wtx.nTimeReceived;
528 if (wtxIn.hashBlock != 0)
530 if (mapBlockIndex.count(wtxIn.hashBlock))
532 int64_t latestNow = wtx.nTimeReceived;
533 int64_t latestEntry = 0;
535 // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
536 int64_t latestTolerated = latestNow + 300;
537 std::list<CAccountingEntry> acentries;
538 TxItems txOrdered = OrderedTxItems(acentries);
539 for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
541 CWalletTx *const pwtx = (*it).second.first;
544 CAccountingEntry *const pacentry = (*it).second.second;
548 nSmartTime = pwtx->nTimeSmart;
550 nSmartTime = pwtx->nTimeReceived;
553 nSmartTime = pacentry->nTime;
554 if (nSmartTime <= latestTolerated)
556 latestEntry = nSmartTime;
557 if (nSmartTime > latestNow)
558 latestNow = nSmartTime;
564 int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime();
565 wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
568 LogPrintf("AddToWallet() : found %s in block %s not in index\n",
569 wtxIn.GetHash().ToString(),
570 wtxIn.hashBlock.ToString());
575 bool fUpdated = false;
579 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
581 wtx.hashBlock = wtxIn.hashBlock;
584 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
586 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
587 wtx.nIndex = wtxIn.nIndex;
590 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
592 wtx.fFromMe = wtxIn.fFromMe;
598 LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
601 if (fInsertedNew || fUpdated)
602 if (!wtx.WriteToDisk())
605 // Break debit/credit balance caches:
608 // Notify UI of new or updated transaction
609 NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
611 // notify an external script when a wallet transaction comes in or is updated
612 std::string strCmd = GetArg("-walletnotify", "");
614 if ( !strCmd.empty())
616 boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
617 boost::thread t(runCommand, strCmd); // thread runs free
624 // Add a transaction to the wallet, or update it.
625 // pblock is optional, but should be provided if the transaction is known to be in a block.
626 // If fUpdate is true, existing transactions will be updated.
627 bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate)
630 AssertLockHeld(cs_wallet);
631 bool fExisted = mapWallet.count(tx.GetHash());
632 if (fExisted && !fUpdate) return false;
633 if (fExisted || IsMine(tx) || IsFromMe(tx))
635 CWalletTx wtx(this,tx);
636 // Get merkle branch if transaction was found in a block
638 wtx.SetMerkleBranch(pblock);
639 return AddToWallet(wtx);
645 void CWallet::SyncTransaction(const CTransaction& tx, const CBlock* pblock)
647 LOCK2(cs_main, cs_wallet);
648 if (!AddToWalletIfInvolvingMe(tx, pblock, true))
649 return; // Not one of ours
651 // If a transaction changes 'conflicted' state, that changes the balance
652 // available of the outputs it spends. So force those to be
654 BOOST_FOREACH(const CTxIn& txin, tx.vin)
656 if (mapWallet.count(txin.prevout.hash))
657 mapWallet[txin.prevout.hash].MarkDirty();
661 void CWallet::EraseFromWallet(const uint256 &hash)
667 if (mapWallet.erase(hash))
668 CWalletDB(strWalletFile).EraseTx(hash);
674 isminetype CWallet::IsMine(const CTxIn &txin) const
678 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
679 if (mi != mapWallet.end())
681 const CWalletTx& prev = (*mi).second;
682 if (txin.prevout.n < prev.vout.size())
683 return IsMine(prev.vout[txin.prevout.n]);
689 int64_t CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
693 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
694 if (mi != mapWallet.end())
696 const CWalletTx& prev = (*mi).second;
697 if (txin.prevout.n < prev.vout.size())
698 if (IsMine(prev.vout[txin.prevout.n]) & filter)
699 return prev.vout[txin.prevout.n].nValue;
705 bool CWallet::IsChange(const CTxOut& txout) const
707 // TODO: fix handling of 'change' outputs. The assumption is that any
708 // payment to a script that is ours, but is not in the address book
709 // is change. That assumption is likely to break when we implement multisignature
710 // wallets that return change back into a multi-signature-protected address;
711 // a better way of identifying which outputs are 'the send' and which are
712 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
713 // which output, if any, was change).
714 if (::IsMine(*this, txout.scriptPubKey))
716 CTxDestination address;
717 if (!ExtractDestination(txout.scriptPubKey, address))
721 if (!mapAddressBook.count(address))
727 int64_t CWalletTx::GetTxTime() const
729 int64_t n = nTimeSmart;
730 return n ? n : nTimeReceived;
733 int CWalletTx::GetRequestCount() const
735 // Returns -1 if it wasn't being tracked
738 LOCK(pwallet->cs_wallet);
744 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
745 if (mi != pwallet->mapRequestCount.end())
746 nRequests = (*mi).second;
751 // Did anyone request this transaction?
752 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
753 if (mi != pwallet->mapRequestCount.end())
755 nRequests = (*mi).second;
757 // How about the block it's in?
758 if (nRequests == 0 && hashBlock != 0)
760 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
761 if (mi != pwallet->mapRequestCount.end())
762 nRequests = (*mi).second;
764 nRequests = 1; // If it's in someone else's block it must have got out
772 void CWalletTx::GetAmounts(list<COutputEntry>& listReceived,
773 list<COutputEntry>& listSent, int64_t& nFee, string& strSentAccount, const isminefilter& filter) const
776 listReceived.clear();
778 strSentAccount = strFromAccount;
781 int64_t nDebit = GetDebit(filter);
782 if (nDebit > 0) // debit>0 means we signed/sent this transaction
784 int64_t nValueOut = GetValueOut();
785 nFee = nDebit - nValueOut;
789 for (unsigned int i = 0; i < vout.size(); ++i)
791 const CTxOut& txout = vout[i];
792 isminetype fIsMine = pwallet->IsMine(txout);
793 // Only need to handle txouts if AT LEAST one of these is true:
794 // 1) they debit from us (sent)
795 // 2) the output is to us (received)
798 // Don't report 'change' txouts
799 if (pwallet->IsChange(txout))
802 else if (!(fIsMine & filter))
805 // In either case, we need to get the destination address
806 CTxDestination address;
807 if (!ExtractDestination(txout.scriptPubKey, address))
809 LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
810 this->GetHash().ToString());
811 address = CNoDestination();
814 COutputEntry output = {address, txout.nValue, (int)i};
816 // If we are debited by the transaction, add the output as a "sent" entry
818 listSent.push_back(output);
820 // If we are receiving the output, add it as a "received" entry
821 if (fIsMine & filter)
822 listReceived.push_back(output);
827 void CWalletTx::GetAccountAmounts(const string& strAccount, int64_t& nReceived,
828 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const
830 nReceived = nSent = nFee = 0;
833 string strSentAccount;
834 list<COutputEntry> listReceived;
835 list<COutputEntry> listSent;
836 GetAmounts(listReceived, listSent, allFee, strSentAccount, filter);
838 if (strAccount == strSentAccount)
840 BOOST_FOREACH(const COutputEntry& s, listSent)
845 LOCK(pwallet->cs_wallet);
846 BOOST_FOREACH(const COutputEntry& r, listReceived)
848 if (pwallet->mapAddressBook.count(r.destination))
850 map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook.find(r.destination);
851 if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount)
852 nReceived += r.amount;
854 else if (strAccount.empty())
856 nReceived += r.amount;
863 bool CWalletTx::WriteToDisk()
865 return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
868 // Scan the block chain (starting in pindexStart) for transactions
869 // from or to us. If fUpdate is true, found transactions that already
870 // exist in the wallet will be updated.
871 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
874 int64_t nNow = GetTime();
876 CBlockIndex* pindex = pindexStart;
878 LOCK2(cs_main, cs_wallet);
880 // no need to read and scan block, if block was created before
881 // our wallet birthday (as adjusted for block time variability)
882 while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200)))
883 pindex = chainActive.Next(pindex);
885 ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
886 double dProgressStart = Checkpoints::GuessVerificationProgress(pindex, false);
887 double dProgressTip = Checkpoints::GuessVerificationProgress(chainActive.Tip(), false);
890 if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
891 ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
894 ReadBlockFromDisk(block, pindex);
895 BOOST_FOREACH(CTransaction& tx, block.vtx)
897 if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
900 pindex = chainActive.Next(pindex);
901 if (GetTime() >= nNow + 60) {
903 LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(pindex));
906 ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
911 void CWallet::ReacceptWalletTransactions()
913 LOCK2(cs_main, cs_wallet);
914 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
916 const uint256& wtxid = item.first;
917 CWalletTx& wtx = item.second;
918 assert(wtx.GetHash() == wtxid);
920 int nDepth = wtx.GetDepthInMainChain();
922 if (!wtx.IsCoinBase() && nDepth < 0)
924 // Try to add to memory pool
926 wtx.AcceptToMemoryPool(false);
931 void CWalletTx::RelayWalletTransaction()
935 if (GetDepthInMainChain() == 0) {
936 LogPrintf("Relaying wtx %s\n", GetHash().ToString());
937 RelayTransaction((CTransaction)*this);
942 set<uint256> CWalletTx::GetConflicts() const
947 uint256 myHash = GetHash();
948 result = pwallet->GetConflicts(myHash);
949 result.erase(myHash);
954 void CWallet::ResendWalletTransactions()
956 // Do this infrequently and randomly to avoid giving away
957 // that these are our transactions.
958 if (GetTime() < nNextResend)
960 bool fFirst = (nNextResend == 0);
961 nNextResend = GetTime() + GetRand(30 * 60);
965 // Only do it if there's been a new block since last time
966 if (nTimeBestReceived < nLastResend)
968 nLastResend = GetTime();
970 // Rebroadcast any of our txes that aren't in a block yet
971 LogPrintf("ResendWalletTransactions()\n");
974 // Sort them in chronological order
975 multimap<unsigned int, CWalletTx*> mapSorted;
976 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
978 CWalletTx& wtx = item.second;
979 // Don't rebroadcast until it's had plenty of time that
980 // it should have gotten in already by now.
981 if (nTimeBestReceived - (int64_t)wtx.nTimeReceived > 5 * 60)
982 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
984 BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
986 CWalletTx& wtx = *item.second;
987 wtx.RelayWalletTransaction();
997 //////////////////////////////////////////////////////////////////////////////
1003 int64_t CWallet::GetBalance() const
1007 LOCK2(cs_main, cs_wallet);
1008 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1010 const CWalletTx* pcoin = &(*it).second;
1011 if (pcoin->IsTrusted())
1012 nTotal += pcoin->GetAvailableCredit();
1019 int64_t CWallet::GetUnconfirmedBalance() const
1023 LOCK2(cs_main, cs_wallet);
1024 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1026 const CWalletTx* pcoin = &(*it).second;
1027 if (!IsFinalTx(*pcoin) || (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0))
1028 nTotal += pcoin->GetAvailableCredit();
1034 int64_t CWallet::GetImmatureBalance() const
1038 LOCK2(cs_main, cs_wallet);
1039 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1041 const CWalletTx* pcoin = &(*it).second;
1042 nTotal += pcoin->GetImmatureCredit();
1048 int64_t CWallet::GetWatchOnlyBalance() const
1052 LOCK2(cs_main, cs_wallet);
1053 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1055 const CWalletTx* pcoin = &(*it).second;
1056 if (pcoin->IsTrusted())
1057 nTotal += pcoin->GetAvailableWatchOnlyCredit();
1064 int64_t CWallet::GetUnconfirmedWatchOnlyBalance() const
1068 LOCK2(cs_main, cs_wallet);
1069 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1071 const CWalletTx* pcoin = &(*it).second;
1072 if (!IsFinalTx(*pcoin) || (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0))
1073 nTotal += pcoin->GetAvailableWatchOnlyCredit();
1079 int64_t CWallet::GetImmatureWatchOnlyBalance() const
1083 LOCK2(cs_main, cs_wallet);
1084 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1086 const CWalletTx* pcoin = &(*it).second;
1087 nTotal += pcoin->GetImmatureWatchOnlyCredit();
1093 // populate vCoins with vector of available COutputs.
1094 void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const
1099 LOCK2(cs_main, cs_wallet);
1100 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1102 const uint256& wtxid = it->first;
1103 const CWalletTx* pcoin = &(*it).second;
1105 if (!IsFinalTx(*pcoin))
1108 if (fOnlyConfirmed && !pcoin->IsTrusted())
1111 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1114 int nDepth = pcoin->GetDepthInMainChain();
1118 for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1119 isminetype mine = IsMine(pcoin->vout[i]);
1120 if (!(IsSpent(wtxid, i)) && mine != ISMINE_NO &&
1121 !IsLockedCoin((*it).first, i) && pcoin->vout[i].nValue > 0 &&
1122 (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
1123 vCoins.push_back(COutput(pcoin, i, nDepth, mine & ISMINE_SPENDABLE));
1129 static void ApproximateBestSubset(vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > >vValue, int64_t nTotalLower, int64_t nTargetValue,
1130 vector<char>& vfBest, int64_t& nBest, int iterations = 1000)
1132 vector<char> vfIncluded;
1134 vfBest.assign(vValue.size(), true);
1135 nBest = nTotalLower;
1137 seed_insecure_rand();
1139 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
1141 vfIncluded.assign(vValue.size(), false);
1143 bool fReachedTarget = false;
1144 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
1146 for (unsigned int i = 0; i < vValue.size(); i++)
1148 //The solver here uses a randomized algorithm,
1149 //the randomness serves no real security purpose but is just
1150 //needed to prevent degenerate behavior and it is important
1151 //that the rng fast. We do not use a constant random sequence,
1152 //because there may be some privacy improvement by making
1153 //the selection random.
1154 if (nPass == 0 ? insecure_rand()&1 : !vfIncluded[i])
1156 nTotal += vValue[i].first;
1157 vfIncluded[i] = true;
1158 if (nTotal >= nTargetValue)
1160 fReachedTarget = true;
1164 vfBest = vfIncluded;
1166 nTotal -= vValue[i].first;
1167 vfIncluded[i] = false;
1175 bool CWallet::SelectCoinsMinConf(int64_t nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
1176 set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const
1178 setCoinsRet.clear();
1181 // List of values less than target
1182 pair<int64_t, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1183 coinLowestLarger.first = std::numeric_limits<int64_t>::max();
1184 coinLowestLarger.second.first = NULL;
1185 vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > > vValue;
1186 int64_t nTotalLower = 0;
1188 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
1190 BOOST_FOREACH(const COutput &output, vCoins)
1192 if (!output.fSpendable)
1195 const CWalletTx *pcoin = output.tx;
1197 if (output.nDepth < (pcoin->IsFromMe(ISMINE_ALL) ? nConfMine : nConfTheirs))
1201 int64_t n = pcoin->vout[i].nValue;
1203 pair<int64_t,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1205 if (n == nTargetValue)
1207 setCoinsRet.insert(coin.second);
1208 nValueRet += coin.first;
1211 else if (n < nTargetValue + CENT)
1213 vValue.push_back(coin);
1216 else if (n < coinLowestLarger.first)
1218 coinLowestLarger = coin;
1222 if (nTotalLower == nTargetValue)
1224 for (unsigned int i = 0; i < vValue.size(); ++i)
1226 setCoinsRet.insert(vValue[i].second);
1227 nValueRet += vValue[i].first;
1232 if (nTotalLower < nTargetValue)
1234 if (coinLowestLarger.second.first == NULL)
1236 setCoinsRet.insert(coinLowestLarger.second);
1237 nValueRet += coinLowestLarger.first;
1241 // Solve subset sum by stochastic approximation
1242 sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
1243 vector<char> vfBest;
1246 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1247 if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1248 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1250 // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
1251 // or the next bigger coin is closer), return the bigger coin
1252 if (coinLowestLarger.second.first &&
1253 ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1255 setCoinsRet.insert(coinLowestLarger.second);
1256 nValueRet += coinLowestLarger.first;
1259 for (unsigned int i = 0; i < vValue.size(); i++)
1262 setCoinsRet.insert(vValue[i].second);
1263 nValueRet += vValue[i].first;
1266 LogPrint("selectcoins", "SelectCoins() best subset: ");
1267 for (unsigned int i = 0; i < vValue.size(); i++)
1269 LogPrint("selectcoins", "%s ", FormatMoney(vValue[i].first));
1270 LogPrint("selectcoins", "total %s\n", FormatMoney(nBest));
1276 bool CWallet::SelectCoins(int64_t nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl* coinControl) const
1278 vector<COutput> vCoins;
1279 AvailableCoins(vCoins, true, coinControl);
1281 // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
1282 if (coinControl && coinControl->HasSelected())
1284 BOOST_FOREACH(const COutput& out, vCoins)
1288 nValueRet += out.tx->vout[out.i].nValue;
1289 setCoinsRet.insert(make_pair(out.tx, out.i));
1291 return (nValueRet >= nTargetValue);
1294 return (SelectCoinsMinConf(nTargetValue, 1, 6, vCoins, setCoinsRet, nValueRet) ||
1295 SelectCoinsMinConf(nTargetValue, 1, 1, vCoins, setCoinsRet, nValueRet) ||
1296 (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue, 0, 1, vCoins, setCoinsRet, nValueRet)));
1302 bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
1303 CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl)
1306 BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend)
1310 strFailReason = _("Transaction amounts must be positive");
1315 if (vecSend.empty() || nValue < 0)
1317 strFailReason = _("Transaction amounts must be positive");
1321 wtxNew.fTimeReceivedIsTxTime = true;
1322 wtxNew.BindWallet(this);
1323 CMutableTransaction txNew;
1326 LOCK2(cs_main, cs_wallet);
1328 nFeeRet = payTxFee.GetFeePerK();
1333 wtxNew.fFromMe = true;
1335 int64_t nTotalValue = nValue + nFeeRet;
1336 double dPriority = 0;
1337 // vouts to the payees
1338 BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend)
1340 CTxOut txout(s.second, s.first);
1341 if (txout.IsDust(::minRelayTxFee))
1343 strFailReason = _("Transaction amount too small");
1346 txNew.vout.push_back(txout);
1349 // Choose coins to use
1350 set<pair<const CWalletTx*,unsigned int> > setCoins;
1351 int64_t nValueIn = 0;
1352 if (!SelectCoins(nTotalValue, setCoins, nValueIn, coinControl))
1354 strFailReason = _("Insufficient funds");
1357 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1359 int64_t nCredit = pcoin.first->vout[pcoin.second].nValue;
1360 //The priority after the next block (depth+1) is used instead of the current,
1361 //reflecting an assumption the user would accept a bit more delay for
1362 //a chance at a free transaction.
1363 dPriority += (double)nCredit * (pcoin.first->GetDepthInMainChain()+1);
1366 int64_t nChange = nValueIn - nValue - nFeeRet;
1370 // Fill a vout to ourself
1371 // TODO: pass in scriptChange instead of reservekey so
1372 // change transaction isn't always pay-to-bitcoin-address
1373 CScript scriptChange;
1375 // coin control: send change to custom address
1376 if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange))
1377 scriptChange.SetDestination(coinControl->destChange);
1379 // no coin control: send change to newly generated address
1382 // Note: We use a new key here to keep it from being obvious which side is the change.
1383 // The drawback is that by not reusing a previous key, the change may be lost if a
1384 // backup is restored, if the backup doesn't have the new private key for the change.
1385 // If we reused the old key, it would be possible to add code to look for and
1386 // rediscover unknown transactions that were written with keys of ours to recover
1387 // post-backup change.
1389 // Reserve a new key pair from key pool
1392 ret = reservekey.GetReservedKey(vchPubKey);
1393 assert(ret); // should never fail, as we just unlocked
1395 scriptChange.SetDestination(vchPubKey.GetID());
1398 CTxOut newTxOut(nChange, scriptChange);
1400 // Never create dust outputs; if we would, just
1401 // add the dust to the fee.
1402 if (newTxOut.IsDust(::minRelayTxFee))
1405 reservekey.ReturnKey();
1409 // Insert change txn at random position:
1410 vector<CTxOut>::iterator position = txNew.vout.begin()+GetRandInt(txNew.vout.size()+1);
1411 txNew.vout.insert(position, newTxOut);
1415 reservekey.ReturnKey();
1418 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1419 txNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
1423 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1424 if (!SignSignature(*this, *coin.first, txNew, nIn++))
1426 strFailReason = _("Signing transaction failed");
1430 // Embed the constructed transaction data in wtxNew.
1431 *static_cast<CTransaction*>(&wtxNew) = CTransaction(txNew);
1434 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
1435 if (nBytes >= MAX_STANDARD_TX_SIZE)
1437 strFailReason = _("Transaction too large");
1440 dPriority = wtxNew.ComputePriority(dPriority, nBytes);
1442 int64_t nFeeNeeded = GetMinimumFee(nBytes, nTxConfirmTarget, mempool);
1444 if (nFeeRet >= nFeeNeeded)
1445 break; // Done, enough fee included.
1447 // Too big to send for free? Include more fee and try again:
1448 if (nBytes > MAX_FREE_TRANSACTION_CREATE_SIZE)
1450 nFeeRet = nFeeNeeded;
1454 // Not enough fee: enough priority?
1455 double dPriorityNeeded = mempool.estimatePriority(nTxConfirmTarget);
1456 // Not enough mempool history to estimate: use hard-coded AllowFree.
1457 if (dPriorityNeeded <= 0 && AllowFree(dPriority))
1460 // Small enough, and priority high enough, to send for free
1461 if (dPriorityNeeded > 0 && dPriority >= dPriorityNeeded)
1464 // Include more fee and try again.
1465 nFeeRet = nFeeNeeded;
1473 bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue,
1474 CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl)
1476 vector< pair<CScript, int64_t> > vecSend;
1477 vecSend.push_back(make_pair(scriptPubKey, nValue));
1478 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, strFailReason, coinControl);
1481 // Call after CreateTransaction unless you want to abort
1482 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
1485 LOCK2(cs_main, cs_wallet);
1486 LogPrintf("CommitTransaction:\n%s", wtxNew.ToString());
1488 // This is only to keep the database open to defeat the auto-flush for the
1489 // duration of this scope. This is the only place where this optimization
1490 // maybe makes sense; please don't do it anywhere else.
1491 CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1493 // Take key pair from key pool so it won't be used again
1494 reservekey.KeepKey();
1496 // Add tx to wallet, because if it has change it's also ours,
1497 // otherwise just for transaction history.
1498 AddToWallet(wtxNew);
1500 // Notify that old coins are spent
1501 set<CWalletTx*> setCoins;
1502 BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
1504 CWalletTx &coin = mapWallet[txin.prevout.hash];
1505 coin.BindWallet(this);
1506 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
1513 // Track how many getdata requests our transaction gets
1514 mapRequestCount[wtxNew.GetHash()] = 0;
1517 if (!wtxNew.AcceptToMemoryPool(false))
1519 // This must not fail. The transaction has already been signed and recorded.
1520 LogPrintf("CommitTransaction() : Error: Transaction not valid");
1523 wtxNew.RelayWalletTransaction();
1531 string CWallet::SendMoney(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew)
1535 return _("Invalid amount");
1536 if (nValue > GetBalance())
1537 return _("Insufficient funds");
1542 strError = _("Error: Wallet locked, unable to create transaction!");
1543 LogPrintf("SendMoney() : %s", strError);
1547 // Parse Bitcoin address
1548 CScript scriptPubKey;
1549 scriptPubKey.SetDestination(address);
1551 // Create and send the transaction
1552 CReserveKey reservekey(this);
1553 int64_t nFeeRequired;
1554 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
1556 if (nValue + nFeeRequired > GetBalance())
1557 strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!"), FormatMoney(nFeeRequired));
1558 LogPrintf("SendMoney() : %s\n", strError);
1561 if (!CommitTransaction(wtxNew, reservekey))
1562 return _("Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
1569 int64_t CWallet::GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool)
1571 // payTxFee is user-set "I want to pay this much"
1572 int64_t nFeeNeeded = payTxFee.GetFee(nTxBytes);
1573 // User didn't set: use -txconfirmtarget to estimate...
1574 if (nFeeNeeded == 0)
1575 nFeeNeeded = pool.estimateFee(nConfirmTarget).GetFee(nTxBytes);
1576 // ... unless we don't have enough mempool data, in which case fall
1577 // back to a hard-coded fee
1578 if (nFeeNeeded == 0)
1579 nFeeNeeded = minTxFee.GetFee(nTxBytes);
1586 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
1590 fFirstRunRet = false;
1591 DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
1592 if (nLoadWalletRet == DB_NEED_REWRITE)
1594 if (CDB::Rewrite(strWalletFile, "\x04pool"))
1598 // Note: can't top-up keypool here, because wallet is locked.
1599 // User will be prompted to unlock wallet the next operation
1600 // the requires a new key.
1604 if (nLoadWalletRet != DB_LOAD_OK)
1605 return nLoadWalletRet;
1606 fFirstRunRet = !vchDefaultKey.IsValid();
1608 uiInterface.LoadWallet(this);
1614 DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
1618 DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this, vWtx);
1619 if (nZapWalletTxRet == DB_NEED_REWRITE)
1621 if (CDB::Rewrite(strWalletFile, "\x04pool"))
1625 // Note: can't top-up keypool here, because wallet is locked.
1626 // User will be prompted to unlock wallet the next operation
1627 // the requires a new key.
1631 if (nZapWalletTxRet != DB_LOAD_OK)
1632 return nZapWalletTxRet;
1638 bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose)
1640 bool fUpdated = false;
1642 LOCK(cs_wallet); // mapAddressBook
1643 std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
1644 fUpdated = mi != mapAddressBook.end();
1645 mapAddressBook[address].name = strName;
1646 if (!strPurpose.empty()) /* update purpose only if requested */
1647 mapAddressBook[address].purpose = strPurpose;
1649 NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address),
1650 strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) );
1653 if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose))
1655 return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName);
1658 bool CWallet::DelAddressBook(const CTxDestination& address)
1661 LOCK(cs_wallet); // mapAddressBook
1665 // Delete destdata tuples associated with address
1666 std::string strAddress = CBitcoinAddress(address).ToString();
1667 BOOST_FOREACH(const PAIRTYPE(string, string) &item, mapAddressBook[address].destdata)
1669 CWalletDB(strWalletFile).EraseDestData(strAddress, item.first);
1672 mapAddressBook.erase(address);
1675 NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED);
1679 CWalletDB(strWalletFile).ErasePurpose(CBitcoinAddress(address).ToString());
1680 return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString());
1683 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
1687 if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
1690 vchDefaultKey = vchPubKey;
1695 // Mark old keypool keys as used,
1696 // and generate all new keys
1698 bool CWallet::NewKeyPool()
1702 CWalletDB walletdb(strWalletFile);
1703 BOOST_FOREACH(int64_t nIndex, setKeyPool)
1704 walletdb.ErasePool(nIndex);
1710 int64_t nKeys = max(GetArg("-keypool", 100), (int64_t)0);
1711 for (int i = 0; i < nKeys; i++)
1713 int64_t nIndex = i+1;
1714 walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
1715 setKeyPool.insert(nIndex);
1717 LogPrintf("CWallet::NewKeyPool wrote %d new keys\n", nKeys);
1722 bool CWallet::TopUpKeyPool(unsigned int kpSize)
1730 CWalletDB walletdb(strWalletFile);
1733 unsigned int nTargetSize;
1735 nTargetSize = kpSize;
1737 nTargetSize = max(GetArg("-keypool", 100), (int64_t) 0);
1739 while (setKeyPool.size() < (nTargetSize + 1))
1742 if (!setKeyPool.empty())
1743 nEnd = *(--setKeyPool.end()) + 1;
1744 if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
1745 throw runtime_error("TopUpKeyPool() : writing generated key failed");
1746 setKeyPool.insert(nEnd);
1747 LogPrintf("keypool added key %d, size=%u\n", nEnd, setKeyPool.size());
1753 void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
1756 keypool.vchPubKey = CPubKey();
1763 // Get the oldest key
1764 if(setKeyPool.empty())
1767 CWalletDB walletdb(strWalletFile);
1769 nIndex = *(setKeyPool.begin());
1770 setKeyPool.erase(setKeyPool.begin());
1771 if (!walletdb.ReadPool(nIndex, keypool))
1772 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
1773 if (!HaveKey(keypool.vchPubKey.GetID()))
1774 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
1775 assert(keypool.vchPubKey.IsValid());
1776 LogPrintf("keypool reserve %d\n", nIndex);
1780 void CWallet::KeepKey(int64_t nIndex)
1782 // Remove from key pool
1785 CWalletDB walletdb(strWalletFile);
1786 walletdb.ErasePool(nIndex);
1788 LogPrintf("keypool keep %d\n", nIndex);
1791 void CWallet::ReturnKey(int64_t nIndex)
1793 // Return to key pool
1796 setKeyPool.insert(nIndex);
1798 LogPrintf("keypool return %d\n", nIndex);
1801 bool CWallet::GetKeyFromPool(CPubKey& result)
1807 ReserveKeyFromKeyPool(nIndex, keypool);
1810 if (IsLocked()) return false;
1811 result = GenerateNewKey();
1815 result = keypool.vchPubKey;
1820 int64_t CWallet::GetOldestKeyPoolTime()
1824 ReserveKeyFromKeyPool(nIndex, keypool);
1828 return keypool.nTime;
1831 std::map<CTxDestination, int64_t> CWallet::GetAddressBalances()
1833 map<CTxDestination, int64_t> balances;
1837 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
1839 CWalletTx *pcoin = &walletEntry.second;
1841 if (!IsFinalTx(*pcoin) || !pcoin->IsTrusted())
1844 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1847 int nDepth = pcoin->GetDepthInMainChain();
1848 if (nDepth < (pcoin->IsFromMe(ISMINE_ALL) ? 0 : 1))
1851 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1853 CTxDestination addr;
1854 if (!IsMine(pcoin->vout[i]))
1856 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
1859 int64_t n = IsSpent(walletEntry.first, i) ? 0 : pcoin->vout[i].nValue;
1861 if (!balances.count(addr))
1863 balances[addr] += n;
1871 set< set<CTxDestination> > CWallet::GetAddressGroupings()
1873 AssertLockHeld(cs_wallet); // mapWallet
1874 set< set<CTxDestination> > groupings;
1875 set<CTxDestination> grouping;
1877 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
1879 CWalletTx *pcoin = &walletEntry.second;
1881 if (pcoin->vin.size() > 0)
1883 bool any_mine = false;
1884 // group all input addresses with each other
1885 BOOST_FOREACH(CTxIn txin, pcoin->vin)
1887 CTxDestination address;
1888 if(!IsMine(txin)) /* If this input isn't mine, ignore it */
1890 if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
1892 grouping.insert(address);
1896 // group change with input addresses
1899 BOOST_FOREACH(CTxOut txout, pcoin->vout)
1900 if (IsChange(txout))
1902 CTxDestination txoutAddr;
1903 if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
1905 grouping.insert(txoutAddr);
1908 if (grouping.size() > 0)
1910 groupings.insert(grouping);
1915 // group lone addrs by themselves
1916 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1917 if (IsMine(pcoin->vout[i]))
1919 CTxDestination address;
1920 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
1922 grouping.insert(address);
1923 groupings.insert(grouping);
1928 set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
1929 map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it
1930 BOOST_FOREACH(set<CTxDestination> grouping, groupings)
1932 // make a set of all the groups hit by this new group
1933 set< set<CTxDestination>* > hits;
1934 map< CTxDestination, set<CTxDestination>* >::iterator it;
1935 BOOST_FOREACH(CTxDestination address, grouping)
1936 if ((it = setmap.find(address)) != setmap.end())
1937 hits.insert((*it).second);
1939 // merge all hit groups into a new single group and delete old groups
1940 set<CTxDestination>* merged = new set<CTxDestination>(grouping);
1941 BOOST_FOREACH(set<CTxDestination>* hit, hits)
1943 merged->insert(hit->begin(), hit->end());
1944 uniqueGroupings.erase(hit);
1947 uniqueGroupings.insert(merged);
1950 BOOST_FOREACH(CTxDestination element, *merged)
1951 setmap[element] = merged;
1954 set< set<CTxDestination> > ret;
1955 BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
1957 ret.insert(*uniqueGrouping);
1958 delete uniqueGrouping;
1964 set<CTxDestination> CWallet::GetAccountAddresses(string strAccount) const
1966 AssertLockHeld(cs_wallet); // mapWallet
1967 set<CTxDestination> result;
1968 BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, mapAddressBook)
1970 const CTxDestination& address = item.first;
1971 const string& strName = item.second.name;
1972 if (strName == strAccount)
1973 result.insert(address);
1978 bool CReserveKey::GetReservedKey(CPubKey& pubkey)
1983 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
1985 vchPubKey = keypool.vchPubKey;
1990 assert(vchPubKey.IsValid());
1995 void CReserveKey::KeepKey()
1998 pwallet->KeepKey(nIndex);
2000 vchPubKey = CPubKey();
2003 void CReserveKey::ReturnKey()
2006 pwallet->ReturnKey(nIndex);
2008 vchPubKey = CPubKey();
2011 void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
2015 CWalletDB walletdb(strWalletFile);
2017 LOCK2(cs_main, cs_wallet);
2018 BOOST_FOREACH(const int64_t& id, setKeyPool)
2021 if (!walletdb.ReadPool(id, keypool))
2022 throw runtime_error("GetAllReserveKeyHashes() : read failed");
2023 assert(keypool.vchPubKey.IsValid());
2024 CKeyID keyID = keypool.vchPubKey.GetID();
2025 if (!HaveKey(keyID))
2026 throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
2027 setAddress.insert(keyID);
2031 void CWallet::UpdatedTransaction(const uint256 &hashTx)
2035 // Only notify UI if this transaction is in this wallet
2036 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
2037 if (mi != mapWallet.end())
2038 NotifyTransactionChanged(this, hashTx, CT_UPDATED);
2042 void CWallet::LockCoin(COutPoint& output)
2044 AssertLockHeld(cs_wallet); // setLockedCoins
2045 setLockedCoins.insert(output);
2048 void CWallet::UnlockCoin(COutPoint& output)
2050 AssertLockHeld(cs_wallet); // setLockedCoins
2051 setLockedCoins.erase(output);
2054 void CWallet::UnlockAllCoins()
2056 AssertLockHeld(cs_wallet); // setLockedCoins
2057 setLockedCoins.clear();
2060 bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
2062 AssertLockHeld(cs_wallet); // setLockedCoins
2063 COutPoint outpt(hash, n);
2065 return (setLockedCoins.count(outpt) > 0);
2068 void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts)
2070 AssertLockHeld(cs_wallet); // setLockedCoins
2071 for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
2072 it != setLockedCoins.end(); it++) {
2073 COutPoint outpt = (*it);
2074 vOutpts.push_back(outpt);
2078 void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
2079 AssertLockHeld(cs_wallet); // mapKeyMetadata
2080 mapKeyBirth.clear();
2082 // get birth times for keys with metadata
2083 for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
2084 if (it->second.nCreateTime)
2085 mapKeyBirth[it->first] = it->second.nCreateTime;
2087 // map in which we'll infer heights of other keys
2088 CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganised; use a 144-block safety margin
2089 std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
2090 std::set<CKeyID> setKeys;
2092 BOOST_FOREACH(const CKeyID &keyid, setKeys) {
2093 if (mapKeyBirth.count(keyid) == 0)
2094 mapKeyFirstBlock[keyid] = pindexMax;
2098 // if there are no such keys, we're done
2099 if (mapKeyFirstBlock.empty())
2102 // find first block that affects those keys, if there are any left
2103 std::vector<CKeyID> vAffected;
2104 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
2105 // iterate over all wallet transactions...
2106 const CWalletTx &wtx = (*it).second;
2107 std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
2108 if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) {
2109 // ... which are already in a block
2110 int nHeight = blit->second->nHeight;
2111 BOOST_FOREACH(const CTxOut &txout, wtx.vout) {
2112 // iterate over all their outputs
2113 ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected);
2114 BOOST_FOREACH(const CKeyID &keyid, vAffected) {
2115 // ... and all their affected keys
2116 std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
2117 if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
2118 rit->second = blit->second;
2125 // Extract block timestamps for those keys
2126 for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
2127 mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
2130 bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
2132 if (boost::get<CNoDestination>(&dest))
2135 mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
2138 return CWalletDB(strWalletFile).WriteDestData(CBitcoinAddress(dest).ToString(), key, value);
2141 bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
2143 if (!mapAddressBook[dest].destdata.erase(key))
2147 return CWalletDB(strWalletFile).EraseDestData(CBitcoinAddress(dest).ToString(), key);
2150 bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
2152 mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
2156 bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
2158 std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest);
2159 if(i != mapAddressBook.end())
2161 CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
2162 if(j != i->second.destdata.end())