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.
10 #include "ui_interface.h"
13 #include "coincontrol.h"
14 #include <boost/algorithm/string/replace.hpp>
15 #include <openssl/bio.h>
20 extern int64_t nReserveBalance;
22 //////////////////////////////////////////////////////////////////////////////
32 CWallet::CWallet(std::string strWalletFileIn)
36 strWalletFile = strWalletFileIn;
40 void CWallet::SetNull()
42 nWalletVersion = FEATURE_BASE;
43 nWalletMaxVersion = FEATURE_BASE;
46 pwalletdbEncryption = NULL;
47 pwalletdbDecryption = NULL;
56 struct CompareValueOnly
58 bool operator()(const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t1,
59 const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t2) const
61 return t1.first < t2.first;
65 const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
68 auto it = mapWallet.find(hash);
69 if (it == mapWallet.end())
74 CPubKey CWallet::GenerateNewKey()
76 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
80 key.MakeNewKey(fCompressed);
82 // Compressed public keys were introduced in version 0.6.0
84 SetMinVersion(FEATURE_COMPRPUBKEY);
86 auto pubkey = key.GetPubKey();
88 // Create new metadata
89 auto nCreationTime = GetTime();
90 mapKeyMetadata[CBitcoinAddress(pubkey.GetID())] = CKeyMetadata(nCreationTime);
91 if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
92 nTimeFirstKey = nCreationTime;
95 throw runtime_error("CWallet::GenerateNewKey() : AddKey failed");
96 return key.GetPubKey();
99 CMalleableKeyView CWallet::GenerateNewMalleableKey()
101 RandAddSeedPerfmon();
103 // Compressed public keys were introduced in version 0.6.0
104 SetMinVersion(FEATURE_MALLKEY);
108 const CMalleableKeyView &keyView(mKey);
110 // Create new metadata
111 auto nCreationTime = GetTime();
112 mapKeyMetadata[CBitcoinAddress(keyView.GetMalleablePubKey())] = CKeyMetadata(nCreationTime);
113 if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
114 nTimeFirstKey = nCreationTime;
117 throw runtime_error("CWallet::GenerateNewMalleableKey() : AddKey failed");
118 return CMalleableKeyView(mKey);
121 bool CWallet::AddKey(const CKey& key)
123 auto pubkey = key.GetPubKey();
124 if (!CCryptoKeyStore::AddKey(key))
129 return CWalletDB(strWalletFile).WriteKey(pubkey, key.GetPrivKey(), mapKeyMetadata[CBitcoinAddress(pubkey.GetID())]);
133 bool CWallet::AddKey(const CMalleableKey& mKey)
135 auto keyView = CMalleableKeyView(mKey);
136 auto vchSecretH = mKey.GetSecretH();
137 if (!CCryptoKeyStore::AddMalleableKey(keyView, vchSecretH))
142 return CWalletDB(strWalletFile).WriteMalleableKey(keyView, vchSecretH, mapKeyMetadata[CBitcoinAddress(keyView.GetMalleablePubKey())]);
146 bool CWallet::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const vector<unsigned char> &vchCryptedSecretH)
148 if (!CCryptoKeyStore::AddCryptedMalleableKey(keyView, vchCryptedSecretH))
156 CBitcoinAddress addr(keyView.GetMalleablePubKey());
157 if (pwalletdbEncryption)
158 return pwalletdbEncryption->WriteCryptedMalleableKey(keyView, vchCryptedSecretH, mapKeyMetadata[addr]);
160 return CWalletDB(strWalletFile).WriteCryptedMalleableKey(keyView, vchCryptedSecretH, mapKeyMetadata[addr]);
166 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
168 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
171 // check if we need to remove from watch-only
173 script.SetDestination(vchPubKey.GetID());
174 if (HaveWatchOnly(script))
175 RemoveWatchOnly(script);
181 CBitcoinAddress addr(vchPubKey.GetID());
182 if (pwalletdbEncryption)
183 return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[addr]);
185 return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[addr]);
190 bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
192 if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
193 nTimeFirstKey = meta.nCreateTime;
195 mapKeyMetadata[CBitcoinAddress(pubkey.GetID())] = meta;
199 bool CWallet::LoadKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata)
201 if (metadata.nCreateTime && (!nTimeFirstKey || metadata.nCreateTime < nTimeFirstKey))
202 nTimeFirstKey = metadata.nCreateTime;
204 mapKeyMetadata[CBitcoinAddress(keyView.GetMalleablePubKey())] = metadata;
208 bool CWallet::LoadMinVersion(int nVersion)
210 nWalletVersion = nVersion;
211 nWalletMaxVersion = max(nWalletMaxVersion, nVersion);
215 bool CWallet::AddCScript(const CScript& redeemScript)
217 if (!CCryptoKeyStore::AddCScript(redeemScript))
221 return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
224 bool CWallet::LoadCScript(const CScript& redeemScript)
226 /* A sanity check was added in commit 5ed0a2b to avoid adding redeemScripts
227 * that never can be redeemed. However, old wallets may still contain
228 * these. Do not add them to the wallet and warn. */
229 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
231 auto strAddr = CBitcoinAddress(redeemScript.GetID()).ToString();
232 printf("LoadCScript() : Warning: This wallet contains a redeemScript of size %" PRIszu " which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n",
233 redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr.c_str());
237 return CCryptoKeyStore::AddCScript(redeemScript);
241 bool CWallet::AddWatchOnly(const CScript &dest)
243 if (!CCryptoKeyStore::AddWatchOnly(dest))
245 nTimeFirstKey = 1; // No birthday information for watch-only keys.
246 NotifyWatchonlyChanged(true);
249 return CWalletDB(strWalletFile).WriteWatchOnly(dest);
252 bool CWallet::RemoveWatchOnly(const CScript &dest)
255 if (!CCryptoKeyStore::RemoveWatchOnly(dest))
257 if (!HaveWatchOnly())
258 NotifyWatchonlyChanged(false);
260 if (!CWalletDB(strWalletFile).EraseWatchOnly(dest))
266 bool CWallet::LoadWatchOnly(const CScript &dest)
268 return CCryptoKeyStore::AddWatchOnly(dest);
271 // ppcoin: optional setting to unlock wallet for block minting only;
272 // serves to disable the trivial sendmoney when OS account compromised
273 bool fWalletUnlockMintOnly = false;
275 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
281 CKeyingMaterial vMasterKey;
285 for(const auto& pMasterKey : mapMasterKeys)
287 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
289 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
291 if (CCryptoKeyStore::Unlock(vMasterKey))
298 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
300 bool fWasLocked = IsLocked();
307 CKeyingMaterial vMasterKey;
308 for(auto& pMasterKey : mapMasterKeys)
310 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
312 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
314 if (CCryptoKeyStore::Unlock(vMasterKey))
316 auto nStartTime = GetTimeMillis();
317 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
318 double nFirstMultiplier = 1e2 / (GetTimeMillis() - nStartTime);
319 pMasterKey.second.nDeriveIterations = (uint32_t)(pMasterKey.second.nDeriveIterations *nFirstMultiplier);
321 nStartTime = GetTimeMillis();
322 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
323 double nSecondMultiplier = 1e2 / (GetTimeMillis() - nStartTime);
324 pMasterKey.second.nDeriveIterations = (uint32_t)((pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * nSecondMultiplier) / 2);
326 if (pMasterKey.second.nDeriveIterations < 25000)
327 pMasterKey.second.nDeriveIterations = 25000;
329 printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
331 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
333 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
335 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
346 void CWallet::SetBestChain(const CBlockLocator& loc)
348 CWalletDB walletdb(strWalletFile);
349 walletdb.WriteBestBlock(loc);
352 // This class implements an addrIncoming entry that causes pre-0.4
353 // clients to crash on startup if reading a private-key-encrypted wallet.
354 class CCorruptAddress
359 if (nType & SER_DISK)
364 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
366 if (nWalletVersion >= nVersion)
369 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
370 if (fExplicit && nVersion > nWalletMaxVersion)
371 nVersion = FEATURE_LATEST;
373 nWalletVersion = nVersion;
375 if (nVersion > nWalletMaxVersion)
376 nWalletMaxVersion = nVersion;
380 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
381 if (nWalletVersion > 40000)
382 pwalletdb->WriteMinVersion(nWalletVersion);
390 bool CWallet::SetMaxVersion(int nVersion)
392 // cannot downgrade below current version
393 if (nWalletVersion > nVersion)
396 nWalletMaxVersion = nVersion;
401 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
406 CKeyingMaterial vMasterKey;
407 RandAddSeedPerfmon();
409 vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
410 RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
412 CMasterKey kMasterKey;
414 RandAddSeedPerfmon();
415 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
416 RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
419 auto nStartTime = GetTimeMillis();
420 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
421 auto nDivider = GetTimeMillis() - nStartTime;
422 kMasterKey.nDeriveIterations = (uint32_t)(25e5 / (double)(nDivider));
424 nStartTime = GetTimeMillis();
425 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
426 double nMultiplier = 1e2 / (GetTimeMillis() - nStartTime);
427 kMasterKey.nDeriveIterations = (uint32_t)((kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * nMultiplier) / 2);
429 if (kMasterKey.nDeriveIterations < 25000)
430 kMasterKey.nDeriveIterations = 25000;
432 printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
434 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
436 if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
441 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
444 pwalletdbEncryption = new CWalletDB(strWalletFile);
445 if (!pwalletdbEncryption->TxnBegin())
447 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
450 if (!EncryptKeys(vMasterKey))
453 pwalletdbEncryption->TxnAbort();
454 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.
457 // Encryption was introduced in version 0.4.0
458 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
462 if (!pwalletdbEncryption->TxnCommit())
463 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.
465 delete pwalletdbEncryption;
466 pwalletdbEncryption = NULL;
470 Unlock(strWalletPassphrase);
474 // Need to completely rewrite the wallet file; if we don't, bdb might keep
475 // bits of the unencrypted private key in slack space in the database file.
476 CDB::Rewrite(strWalletFile);
479 NotifyStatusChanged(this);
484 bool CWallet::DecryptWallet(const SecureString& strWalletPassphrase)
490 CKeyingMaterial vMasterKey;
494 for(const auto& pMasterKey : mapMasterKeys)
496 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
498 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
500 if (!CCryptoKeyStore::Unlock(vMasterKey))
506 pwalletdbDecryption = new CWalletDB(strWalletFile);
507 if (!pwalletdbDecryption->TxnBegin())
511 if (!DecryptKeys(vMasterKey))
514 pwalletdbDecryption->TxnAbort();
515 exit(1); //We now probably have half of our keys decrypted in memory, and half not...die and let the user reload their encrypted wallet.
520 // Overwrite crypted keys
521 auto mi = mapKeys.begin();
522 while (mi != mapKeys.end())
525 key.SetSecret((*mi).second.first, (*mi).second.second);
526 pwalletdbDecryption->EraseCryptedKey(key.GetPubKey());
527 pwalletdbDecryption->WriteKey(key.GetPubKey(), key.GetPrivKey(), mapKeyMetadata[CBitcoinAddress(mi->first)]);
531 auto mi2 = mapMalleableKeys.begin();
532 while (mi2 != mapMalleableKeys.end())
534 const auto &vchSecretH = mi2->second;
535 const auto &keyView = mi2->first;
536 pwalletdbDecryption->EraseCryptedMalleableKey(keyView);
537 pwalletdbDecryption->WriteMalleableKey(keyView, vchSecretH, mapKeyMetadata[CBitcoinAddress(keyView.GetMalleablePubKey())]);
542 auto mk = mapMasterKeys.begin();
543 while (mk != mapMasterKeys.end())
545 pwalletdbDecryption->EraseMasterKey((*mk).first);
549 if (!pwalletdbDecryption->TxnCommit())
550 exit(1); //We now have keys decrypted in memory, but no on disk...die to avoid confusion and let the user reload their encrypted wallet.
552 delete pwalletdbDecryption;
553 pwalletdbDecryption = NULL;
556 // Need to completely rewrite the wallet file; if we don't, bdb might keep
557 // encrypted private keys in the database file which can be a reason of consistency issues.
558 CDB::Rewrite(strWalletFile);
560 NotifyStatusChanged(this);
565 bool CWallet::GetPEM(const CKeyID &keyID, const string &fileName, const SecureString &strPassKey) const
567 BIO *pemOut = BIO_new_file(fileName.c_str(), "w");
569 return error("GetPEM() : failed to create file %s\n", fileName.c_str());
571 if (!GetKey(keyID, key))
572 return error("GetPEM() : failed to get key for address=%s\n", CBitcoinAddress(keyID).ToString().c_str());
573 bool result = key.WritePEM(pemOut, strPassKey);
578 int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
580 int64_t nRet = nOrderPosNext++;
582 pwalletdb->WriteOrderPosNext(nOrderPosNext);
584 CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
589 CWallet::TxItems CWallet::OrderedTxItems(list<CAccountingEntry>& acentries, string strAccount)
591 CWalletDB walletdb(strWalletFile);
593 // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
596 // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
597 // would make this much faster for applications that do this a lot.
598 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
600 CWalletTx* wtx = &((*it).second);
601 txOrdered.insert({ wtx->nOrderPos, { wtx, (CAccountingEntry*)0 } });
604 walletdb.ListAccountCreditDebit(strAccount, acentries);
605 for(auto& entry : acentries)
607 txOrdered.insert({ entry.nOrderPos, { (CWalletTx*)0, &entry } });
613 void CWallet::WalletUpdateSpent(const CTransaction &tx, bool fBlock)
615 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
616 // Update the wallet spent flag if it doesn't know due to wallet.dat being
617 // restored from backup or the user making copies of wallet.dat.
620 for(const auto& txin : tx.vin)
622 auto mi = mapWallet.find(txin.prevout.hash);
623 if (mi != mapWallet.end())
625 auto& wtx = (*mi).second;
626 if (txin.prevout.n >= wtx.vout.size())
627 printf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
628 else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
630 printf("WalletUpdateSpent found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit(MINE_ALL)).c_str(), wtx.GetHash().ToString().c_str());
631 wtx.MarkSpent(txin.prevout.n);
633 NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
634 vMintingWalletUpdated.push_back(txin.prevout.hash);
641 auto hash = tx.GetHash();
642 auto mi = mapWallet.find(hash);
643 auto& wtx = (*mi).second;
645 for(const CTxOut& txout : tx.vout)
649 wtx.MarkUnspent(&txout - &tx.vout[0]);
651 NotifyTransactionChanged(this, hash, CT_UPDATED);
652 vMintingWalletUpdated.push_back(hash);
660 void CWallet::MarkDirty()
664 for(auto& item : mapWallet)
665 item.second.MarkDirty();
669 bool CWallet::AddToWallet(const CWalletTx& wtxIn)
671 auto hash = wtxIn.GetHash();
674 // Inserts only if not already there, returns tx inserted or tx found
675 auto ret = mapWallet.insert({ hash, wtxIn });
676 auto& wtx = (*ret.first).second;
677 wtx.BindWallet(this);
678 bool fInsertedNew = ret.second;
681 wtx.nTimeReceived = GetAdjustedTime();
682 wtx.nOrderPos = IncOrderPosNext();
684 wtx.nTimeSmart = wtx.nTimeReceived;
685 if (wtxIn.hashBlock != 0)
687 if (mapBlockIndex.count(wtxIn.hashBlock))
689 unsigned int latestNow = wtx.nTimeReceived;
690 unsigned int latestEntry = 0;
692 // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
693 int64_t latestTolerated = latestNow + 300;
694 list<CAccountingEntry> acentries;
695 auto txOrdered = OrderedTxItems(acentries);
696 for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
698 CWalletTx *const pwtx = (*it).second.first;
701 CAccountingEntry *const pacentry = (*it).second.second;
705 nSmartTime = pwtx->nTimeSmart;
707 nSmartTime = pwtx->nTimeReceived;
710 nSmartTime = pacentry->nTime;
711 if (nSmartTime <= latestTolerated)
713 latestEntry = nSmartTime;
714 if (nSmartTime > latestNow)
715 latestNow = nSmartTime;
721 unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
722 wtx.nTimeSmart = max(latestEntry, min(blocktime, latestNow));
725 printf("AddToWallet() : found %s in block %s not in index\n",
726 wtxIn.GetHash().ToString().substr(0,10).c_str(),
727 wtxIn.hashBlock.ToString().c_str());
731 bool fUpdated = false;
735 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
737 wtx.hashBlock = wtxIn.hashBlock;
740 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
742 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
743 wtx.nIndex = wtxIn.nIndex;
746 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
748 wtx.fFromMe = wtxIn.fFromMe;
751 fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
755 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,10).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
758 if (fInsertedNew || fUpdated)
759 if (!wtx.WriteToDisk())
762 // If default receiving address gets used, replace it with a new one
763 CScript scriptDefaultKey;
764 scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
765 for(const CTxOut& txout : wtx.vout)
767 if (txout.scriptPubKey == scriptDefaultKey)
769 CPubKey newDefaultKey;
770 if (GetKeyFromPool(newDefaultKey, false))
772 SetDefaultKey(newDefaultKey);
773 SetAddressBookName(vchDefaultKey.GetID(), "");
778 // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
779 WalletUpdateSpent(wtx, (wtxIn.hashBlock != 0));
781 // Notify UI of new or updated transaction
782 NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
783 vMintingWalletUpdated.push_back(hash);
784 // notify an external script when a wallet transaction comes in or is updated
785 auto strCmd = GetArg("-walletnotify", "");
787 if ( !strCmd.empty())
789 boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
790 boost::thread t(runCommand, strCmd); // thread runs free
797 // Add a transaction to the wallet, or update it.
798 // pblock is optional, but should be provided if the transaction is known to be in a block.
799 // If fUpdate is true, existing transactions will be updated.
800 bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate)
802 auto hash = tx.GetHash();
805 bool fExisted = mapWallet.count(hash) != 0;
806 if (fExisted && !fUpdate) return false;
807 if (fExisted || IsMine(tx) || IsFromMe(tx))
809 CWalletTx wtx(this,tx);
810 // Get merkle branch if transaction was found in a block
812 wtx.SetMerkleBranch(pblock);
813 return AddToWallet(wtx);
816 WalletUpdateSpent(tx);
821 bool CWallet::EraseFromWallet(uint256 hash)
827 if (mapWallet.erase(hash))
828 CWalletDB(strWalletFile).EraseTx(hash);
834 isminetype CWallet::IsMine(const CTxIn &txin) const
838 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
839 if (mi != mapWallet.end())
841 const CWalletTx& prev = (*mi).second;
842 if (txin.prevout.n < prev.vout.size())
843 return IsMine(prev.vout[txin.prevout.n]);
850 CWalletTx::CWalletTx()
855 CWalletTx::CWalletTx(const CWallet* pwalletIn)
860 CWalletTx::CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
865 CWalletTx::CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
870 void CWalletTx::Init(const CWallet* pwalletIn)
876 fTimeReceivedIsTxTime = false;
880 strFromAccount.clear();
882 fDebitCached = false;
883 fWatchDebitCached = false;
884 fCreditCached = false;
885 fWatchCreditCached = false;
886 fAvailableCreditCached = false;
887 fAvailableWatchCreditCached = false;
888 fImmatureCreditCached = false;
889 fImmatureWatchCreditCached = false;
890 fChangeCached = false;
892 nWatchDebitCached = 0;
894 nWatchCreditCached = 0;
895 nAvailableCreditCached = 0;
896 nAvailableWatchCreditCached = 0;
897 nImmatureCreditCached = 0;
898 nImmatureWatchCreditCached = 0;
903 // marks certain txout's as spent
904 // returns true if any update took place
905 bool CWalletTx::UpdateSpent(const vector<char>& vfNewSpent)
907 bool fReturn = false;
908 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
910 if (i == vfSpent.size())
913 if (vfNewSpent[i] && !vfSpent[i])
917 fAvailableCreditCached = fAvailableWatchCreditCached = false;
923 // make sure balances are recalculated
924 void CWalletTx::MarkDirty()
926 fCreditCached = false;
927 fAvailableCreditCached = fAvailableWatchCreditCached = false;
928 fDebitCached = fWatchDebitCached = false;
929 fChangeCached = false;
932 void CWalletTx::BindWallet(CWallet *pwalletIn)
938 void CWalletTx::MarkSpent(unsigned int nOut)
940 if (nOut >= vout.size())
941 throw runtime_error("CWalletTx::MarkSpent() : nOut out of range");
942 vfSpent.resize(vout.size());
945 vfSpent[nOut] = true;
946 fAvailableCreditCached = fAvailableWatchCreditCached = false;
950 void CWalletTx::MarkUnspent(unsigned int nOut)
952 if (nOut >= vout.size())
953 throw runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
954 vfSpent.resize(vout.size());
957 vfSpent[nOut] = false;
958 fAvailableCreditCached = fAvailableWatchCreditCached = false;
962 bool CWalletTx::IsSpent(unsigned int nOut) const
964 if (nOut >= vout.size())
965 throw runtime_error("CWalletTx::IsSpent() : nOut out of range");
966 if (nOut >= vfSpent.size())
968 return (!!vfSpent[nOut]);
971 int64_t CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
975 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
976 if (mi != mapWallet.end())
978 const CWalletTx& prev = (*mi).second;
979 if (txin.prevout.n < prev.vout.size())
980 if (IsMine(prev.vout[txin.prevout.n]) & filter)
981 return prev.vout[txin.prevout.n].nValue;
987 isminetype CWallet::IsMine(const CTxOut& txout) const
989 return ::IsMine(*this, txout.scriptPubKey);
992 int64_t CWallet::GetCredit(const CTxOut& txout, const isminefilter& filter) const
994 if (!MoneyRange(txout.nValue))
995 throw runtime_error("CWallet::GetCredit() : value out of range");
996 return (IsMine(txout) & filter ? txout.nValue : 0);
999 bool CWallet::IsChange(const CTxOut& txout) const
1001 // TODO: fix handling of 'change' outputs. The assumption is that any
1002 // payment to a script that is ours, but isn't in the address book
1003 // is change. That assumption is likely to break when we implement multisignature
1004 // wallets that return change back into a multi-signature-protected address;
1005 // a better way of identifying which outputs are 'the send' and which are
1006 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
1007 // which output, if any, was change).
1008 if (::IsMine(*this, txout.scriptPubKey))
1010 CTxDestination address;
1011 if (!ExtractDestination(txout.scriptPubKey, address))
1015 if (!mapAddressBook.count(address))
1021 int64_t CWallet::GetChange(const CTxOut& txout) const
1023 if (!MoneyRange(txout.nValue))
1024 throw runtime_error("CWallet::GetChange() : value out of range");
1025 return (IsChange(txout) ? txout.nValue : 0);
1028 bool CWallet::IsMine(const CTransaction& tx) const
1030 for(const CTxOut& txout : tx.vout)
1031 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
1036 bool CWallet::IsFromMe(const CTransaction& tx) const
1038 return (GetDebit(tx, MINE_ALL) > 0);
1041 int64_t CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) const
1044 for(const CTxIn& txin : tx.vin)
1046 auto nValue = GetDebit(txin, filter);
1048 if (!MoneyRange(nValue) || !MoneyRange(nDebit))
1049 throw runtime_error("CWallet::GetDebit() : value out of range");
1051 return nDebit.getint64();
1054 int64_t CWallet::GetCredit(const CTransaction& tx, const isminefilter& filter) const
1056 CBigNum nCredit = 0;
1057 for(const CTxOut& txout : tx.vout)
1059 auto nValue = GetCredit(txout, filter);
1061 if (!MoneyRange(nValue) || !MoneyRange(nCredit))
1062 throw runtime_error("CWallet::GetCredit() : value out of range");
1064 return nCredit.getint64();
1067 int64_t CWallet::GetChange(const CTransaction& tx) const
1069 CBigNum nChange = 0;
1070 for(const CTxOut& txout : tx.vout)
1072 int64_t nValue = GetChange(txout);
1074 if (!MoneyRange(nValue) || !MoneyRange(nChange))
1075 throw runtime_error("CWallet::GetChange() : value out of range");
1077 return nChange.getint64();
1080 int64_t CWalletTx::GetTxTime() const
1085 int CWalletTx::GetRequestCount() const
1087 // Returns -1 if it wasn't being tracked
1090 LOCK(pwallet->cs_wallet);
1091 if (IsCoinBase() || IsCoinStake())
1096 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
1097 if (mi != pwallet->mapRequestCount.end())
1098 nRequests = (*mi).second;
1103 // Did anyone request this transaction?
1104 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
1105 if (mi != pwallet->mapRequestCount.end())
1107 nRequests = (*mi).second;
1109 // How about the block it's in?
1110 if (nRequests == 0 && hashBlock != 0)
1112 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
1113 if (mi != pwallet->mapRequestCount.end())
1114 nRequests = (*mi).second;
1116 nRequests = 1; // If it's in someone else's block it must have got out
1124 bool CWalletTx::InMempool() const
1127 if (mempool.exists(GetHash())) {
1133 bool CWalletTx::IsTrusted() const
1135 // Quick answer in most cases
1138 int nDepth = GetDepthInMainChain();
1143 if (!fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
1146 // Don't trust unconfirmed transactions from us unless they are in the mempool.
1150 // Trusted if all inputs are from us and are in the mempool:
1151 for(const CTxIn& txin : vin)
1153 // Transactions not sent by us: not trusted
1154 const CWalletTx* parent = pwallet->GetWalletTx(txin.prevout.hash);
1157 const CTxOut& parentOut = parent->vout[txin.prevout.n];
1158 if (pwallet->IsMine(parentOut) != MINE_SPENDABLE)
1164 int64_t CWalletTx::GetDebit(const isminefilter& filter) const
1170 if (filter & MINE_SPENDABLE)
1173 nDebit += nDebitCached;
1176 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
1177 fDebitCached = true;
1178 nDebit += nDebitCached;
1181 if (filter & MINE_WATCH_ONLY)
1183 if (fWatchDebitCached)
1184 nDebit += nWatchDebitCached;
1187 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
1188 fWatchDebitCached = true;
1189 nDebit += nWatchDebitCached;
1195 int64_t CWalletTx::GetCredit(const isminefilter& filter) const
1197 // Must wait until coinbase is safely deep enough in the chain before valuing it
1198 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
1202 if (filter & MINE_SPENDABLE)
1204 // GetBalance can assume transactions in mapWallet won't change
1206 credit += nCreditCached;
1209 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
1210 fCreditCached = true;
1211 credit += nCreditCached;
1214 if (filter & MINE_WATCH_ONLY)
1216 if (fWatchCreditCached)
1217 credit += nWatchCreditCached;
1220 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
1221 fWatchCreditCached = true;
1222 credit += nWatchCreditCached;
1228 int64_t CWalletTx::GetImmatureCredit(bool fUseCache) const
1230 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
1232 if (fUseCache && fImmatureCreditCached)
1233 return nImmatureCreditCached;
1234 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
1235 fImmatureCreditCached = true;
1236 return nImmatureCreditCached;
1242 int64_t CWalletTx::GetImmatureWatchOnlyCredit(bool fUseCache) const
1244 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
1246 if (fUseCache && fImmatureWatchCreditCached)
1247 return nImmatureWatchCreditCached;
1248 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
1249 fImmatureWatchCreditCached = true;
1250 return nImmatureWatchCreditCached;
1257 int64_t CWalletTx::GetAvailableCredit(bool fUseCache) const
1259 // Must wait until coinbase is safely deep enough in the chain before valuing it
1260 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
1264 if (fAvailableCreditCached)
1265 return nAvailableCreditCached;
1268 CBigNum nCredit = 0;
1269 for (uint32_t i = 0; i < vout.size(); i++)
1273 const CTxOut &txout = vout[i];
1274 int64_t nValue = pwallet->GetCredit(txout, MINE_SPENDABLE);
1276 if (!MoneyRange(nValue) || !MoneyRange(nCredit))
1277 throw runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
1281 nAvailableCreditCached = nCredit.getint64();
1282 fAvailableCreditCached = true;
1284 return nCredit.getint64();
1287 int64_t CWalletTx::GetAvailableWatchCredit(bool fUseCache) const
1289 // Must wait until coinbase is safely deep enough in the chain before valuing it
1290 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
1294 if (fAvailableWatchCreditCached)
1295 return nAvailableWatchCreditCached;
1298 CBigNum nCredit = 0;
1299 for (unsigned int i = 0; i < vout.size(); i++)
1303 const CTxOut &txout = vout[i];
1304 int64_t nValue = pwallet->GetCredit(txout, MINE_WATCH_ONLY);
1306 if (!MoneyRange(nValue) || !MoneyRange(nCredit))
1307 throw runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
1311 nAvailableWatchCreditCached = nCredit.getint64();
1312 fAvailableWatchCreditCached = true;
1314 return nCredit.getint64();
1317 int64_t CWalletTx::GetChange() const
1320 return nChangeCached;
1321 nChangeCached = pwallet->GetChange(*this);
1322 fChangeCached = true;
1323 return nChangeCached;
1326 void CWalletTx::GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, list<pair<CBitcoinAddress, int64_t> >& listReceived,
1327 list<pair<CBitcoinAddress, int64_t> >& listSent, int64_t& nFee, string& strSentAccount, const isminefilter& filter) const
1329 nGeneratedImmature = nGeneratedMature = nFee = 0;
1330 listReceived.clear();
1332 strSentAccount = strFromAccount;
1334 if (IsCoinBase() || IsCoinStake())
1336 if (GetBlocksToMaturity() > 0)
1337 nGeneratedImmature = pwallet->GetCredit(*this, filter);
1339 nGeneratedMature = GetCredit(filter);
1344 auto nDebit = GetDebit(filter);
1345 if (nDebit > 0) // debit>0 means we signed/sent this transaction
1347 auto nValueOut = GetValueOut();
1348 nFee = nDebit - nValueOut;
1352 for(const CTxOut& txout : vout)
1354 auto fIsMine = pwallet->IsMine(txout);
1355 // Only need to handle txouts if AT LEAST one of these is true:
1356 // 1) they debit from us (sent)
1357 // 2) the output is to us (received)
1360 // Don't report 'change' txouts
1361 if (pwallet->IsChange(txout))
1364 else if (!(fIsMine & filter))
1367 // In either case, we need to get the destination address
1368 CBitcoinAddress address;
1369 if (!ExtractAddress(*pwallet, txout.scriptPubKey, address))
1371 printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
1372 this->GetHash().ToString().c_str());
1373 address = CBitcoinAddress();
1376 // If we are debited by the transaction, add the output as a "sent" entry
1378 listSent.push_back({ address, txout.nValue });
1380 // If we are receiving the output, add it as a "received" entry
1381 if (fIsMine & filter)
1382 listReceived.push_back({ address, txout.nValue });
1387 void CWalletTx::GetAccountAmounts(const string& strAccount, int64_t& nGenerated, int64_t& nReceived,
1388 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const
1390 nGenerated = nReceived = nSent = nFee = 0;
1392 int64_t allGeneratedImmature, allGeneratedMature, allFee;
1393 allGeneratedImmature = allGeneratedMature = allFee = 0;
1394 string strSentAccount;
1395 list<pair<CBitcoinAddress, int64_t> > listReceived;
1396 list<pair<CBitcoinAddress, int64_t> > listSent;
1397 GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount, filter);
1399 if (strAccount.empty())
1400 nGenerated = allGeneratedMature;
1401 if (strAccount == strSentAccount)
1403 for(const auto& s : listSent)
1408 LOCK(pwallet->cs_wallet);
1409 for(const auto& r : listReceived)
1411 if (pwallet->mapAddressBook.count(r.first))
1413 map<CBitcoinAddress, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
1414 if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
1415 nReceived += r.second;
1417 else if (strAccount.empty())
1419 nReceived += r.second;
1425 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
1429 const int COPY_DEPTH = 3;
1430 if (SetMerkleBranch() < COPY_DEPTH)
1432 vector<uint256> vWorkQueue;
1433 for(const CTxIn& txin : vin)
1434 vWorkQueue.push_back(txin.prevout.hash);
1436 // This critsect is OK because txdb is already open
1438 LOCK(pwallet->cs_wallet);
1439 map<uint256, const CMerkleTx*> mapWalletPrev;
1440 set<uint256> setAlreadyDone;
1441 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
1443 auto hash = vWorkQueue[i];
1444 if (setAlreadyDone.count(hash))
1446 setAlreadyDone.insert(hash);
1449 map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
1450 if (mi != pwallet->mapWallet.end())
1453 for(const CMerkleTx& txWalletPrev : (*mi).second.vtxPrev)
1454 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
1456 else if (mapWalletPrev.count(hash))
1458 tx = *mapWalletPrev[hash];
1460 else if (!fClient && txdb.ReadDiskTx(hash, tx))
1466 printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
1470 int nDepth = tx.SetMerkleBranch();
1471 vtxPrev.push_back(tx);
1473 if (nDepth < COPY_DEPTH)
1475 for(const CTxIn& txin : tx.vin)
1476 vWorkQueue.push_back(txin.prevout.hash);
1482 reverse(vtxPrev.begin(), vtxPrev.end());
1485 bool CWalletTx::WriteToDisk()
1487 return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
1490 // Scan the block chain (starting in pindexStart) for transactions
1491 // from or to us. If fUpdate is true, found transactions that already
1492 // exist in the wallet will be updated.
1493 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
1497 CBlockIndex* pindex = pindexStart;
1503 block.ReadFromDisk(pindex, true);
1504 for(CTransaction& tx : block.vtx)
1506 if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
1509 pindex = pindex->pnext;
1515 int CWallet::ScanForWalletTransaction(const uint256& hashTx)
1518 tx.ReadFromDisk(COutPoint(hashTx, 0));
1519 if (AddToWalletIfInvolvingMe(tx, NULL, true))
1524 void CWallet::ReacceptWalletTransactions()
1527 bool fRepeat = true;
1532 vector<CDiskTxPos> vMissingTx;
1533 for(auto& item : mapWallet)
1535 auto& wtx = item.second;
1536 if ((wtx.IsCoinBase() && wtx.IsSpent(0)) || (wtx.IsCoinStake() && wtx.IsSpent(1)))
1540 bool fUpdated = false;
1541 if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
1543 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
1544 if (txindex.vSpent.size() != wtx.vout.size())
1546 printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %" PRIszu " != wtx.vout.size() %" PRIszu "\n", txindex.vSpent.size(), wtx.vout.size());
1549 for (unsigned int i = 0; i < txindex.vSpent.size(); i++)
1553 if (!txindex.vSpent[i].IsNull() && IsMine(wtx.vout[i]))
1557 vMissingTx.push_back(txindex.vSpent[i]);
1562 printf("ReacceptWalletTransactions found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit(MINE_ALL)).c_str(), wtx.GetHash().ToString().c_str());
1569 // Re-accept any txes of ours that aren't already in a block
1570 if (!(wtx.IsCoinBase() || wtx.IsCoinStake()))
1571 wtx.AcceptWalletTransaction(txdb, false);
1574 if (!vMissingTx.empty())
1576 // TODO: optimize this to scan just part of the block chain?
1577 if (ScanForWalletTransactions(pindexGenesisBlock))
1578 fRepeat = true; // Found missing transactions: re-do re-accept.
1583 bool CWalletTx::RelayWalletTransaction(CTxDB& txdb)
1585 auto hash = GetHash();
1586 if (IsCoinBase() || IsCoinStake() || txdb.ContainsTx(hash) || !InMempool())
1589 for(auto it = vtxPrev.begin(); it != vtxPrev.end(); it++)
1591 const CMerkleTx& tx = *it;
1592 auto hash = tx.GetHash();
1594 if (tx.IsCoinBase() || tx.IsCoinStake())
1597 if (!txdb.ContainsTx(hash))
1598 RelayTransaction((CTransaction)tx, hash);
1601 printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
1602 RelayTransaction((CTransaction)*this, hash);
1606 bool CWalletTx::RelayWalletTransaction()
1609 return RelayWalletTransaction(txdb);
1612 vector<uint256> CWallet::ResendWalletTransactionsBefore(int64_t nTime)
1614 vector<uint256> result;
1617 // Sort them in chronological order
1618 map<unsigned int, CWalletTx*> mapSorted;
1619 for(auto& item : mapWallet)
1621 auto& wtx = item.second;
1622 // Don't rebroadcast if newer than nTime:
1623 if (wtx.nTimeReceived > nTime)
1625 mapSorted.insert({ wtx.nTimeReceived, &wtx });
1627 for(auto& item : mapSorted)
1629 auto& wtx = *item.second;
1630 if (wtx.RelayWalletTransaction())
1631 result.push_back(wtx.GetHash());
1636 void CWallet::ResendWalletTransactions(int64_t nBestBlockTime)
1638 auto nNow = GetTime();
1640 // Do this infrequently and randomly to avoid giving away
1641 // that these are our transactions.
1642 if (nNow < nNextResend)
1644 bool fFirst = (nNextResend == 0);
1645 nNextResend = PoissonNextSend(nNow, 5*60);
1649 // Only do it if there's been a new block since last time
1650 if (nBestBlockTime < nLastResend)
1654 // Rebroadcast unconfirmed txes older than 5 minutes before the last
1656 auto relayed = ResendWalletTransactionsBefore(nBestBlockTime - 5*60);
1657 if (!relayed.empty())
1658 printf("CWallet::ResendWalletTransactions: rebroadcast %" PRIszu " unconfirmed transactions\n", relayed.size());
1662 //////////////////////////////////////////////////////////////////////////////
1668 int64_t CWallet::GetBalance() const
1673 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1675 const CWalletTx* pcoin = &(*it).second;
1676 if (pcoin->IsTrusted())
1677 nTotal += pcoin->GetAvailableCredit();
1684 int64_t CWallet::GetWatchOnlyBalance() const
1689 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1691 const CWalletTx* pcoin = &(*it).second;
1692 if (pcoin->IsTrusted())
1693 nTotal += pcoin->GetAvailableWatchCredit();
1700 int64_t CWallet::GetUnconfirmedBalance() const
1705 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1707 const CWalletTx* pcoin = &(*it).second;
1708 if (!pcoin->IsFinal() || !pcoin->IsTrusted())
1709 nTotal += pcoin->GetAvailableCredit();
1715 int64_t CWallet::GetUnconfirmedWatchOnlyBalance() const
1720 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1722 const CWalletTx* pcoin = &(*it).second;
1723 if (!pcoin->IsFinal() || !pcoin->IsTrusted())
1724 nTotal += pcoin->GetAvailableWatchCredit();
1730 int64_t CWallet::GetImmatureBalance() const
1735 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1737 const CWalletTx* pcoin = &(*it).second;
1738 nTotal += pcoin->GetImmatureCredit();
1744 int64_t CWallet::GetImmatureWatchOnlyBalance() const
1749 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1751 const CWalletTx* pcoin = &(*it).second;
1752 nTotal += pcoin->GetImmatureWatchOnlyCredit();
1758 // populate vCoins with vector of spendable COutputs
1759 void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const
1765 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1767 const CWalletTx* pcoin = &(*it).second;
1769 if (!pcoin->IsFinal())
1772 if (fOnlyConfirmed && !pcoin->IsTrusted())
1775 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1778 if(pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0)
1781 for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1782 auto mine = IsMine(pcoin->vout[i]);
1783 if (!(pcoin->IsSpent(i)) && mine != MINE_NO &&
1784 pcoin->vout[i].nValue >= nMinimumInputValue &&
1785 (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
1787 vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain(), mine == MINE_SPENDABLE));
1794 void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const
1800 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1802 const CWalletTx* pcoin = &(*it).second;
1804 if (!pcoin->IsFinal())
1807 if(pcoin->GetDepthInMainChain() < nConf)
1810 for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1811 auto mine = IsMine(pcoin->vout[i]);
1813 // ignore coin if it was already spent or we don't own it
1814 if (pcoin->IsSpent(i) || mine == MINE_NO)
1817 // if coin value is between required limits then add new item to vector
1818 if (pcoin->vout[i].nValue >= nMinValue && pcoin->vout[i].nValue < nMaxValue)
1819 vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain(), mine == MINE_SPENDABLE));
1825 static void ApproximateBestSubset(vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > >vValue, int64_t nTotalLower, int64_t nTargetValue,
1826 vector<char>& vfBest, int64_t& nBest, int iterations = 1000)
1828 vector<char> vfIncluded;
1830 vfBest.assign(vValue.size(), true);
1831 nBest = nTotalLower;
1833 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
1835 vfIncluded.assign(vValue.size(), false);
1837 bool fReachedTarget = false;
1838 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
1840 for (unsigned int i = 0; i < vValue.size(); i++)
1842 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
1844 nTotal += vValue[i].first;
1845 vfIncluded[i] = true;
1846 if (nTotal >= nTargetValue)
1848 fReachedTarget = true;
1852 vfBest = vfIncluded;
1854 nTotal -= vValue[i].first;
1855 vfIncluded[i] = false;
1863 int64_t CWallet::GetStake() const
1867 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1869 const CWalletTx* pcoin = &(*it).second;
1870 if (pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1871 nTotal += CWallet::GetCredit(*pcoin, MINE_ALL);
1876 int64_t CWallet::GetWatchOnlyStake() const
1880 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1882 const CWalletTx* pcoin = &(*it).second;
1883 if (pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1884 nTotal += CWallet::GetCredit(*pcoin, MINE_WATCH_ONLY);
1889 int64_t CWallet::GetNewMint() const
1893 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1895 const CWalletTx* pcoin = &(*it).second;
1896 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1897 nTotal += CWallet::GetCredit(*pcoin, MINE_ALL);
1902 int64_t CWallet::GetWatchOnlyNewMint() const
1906 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
1908 const CWalletTx* pcoin = &(*it).second;
1909 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
1910 nTotal += CWallet::GetCredit(*pcoin, MINE_WATCH_ONLY);
1915 bool CWallet::SelectCoinsMinConf(int64_t nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, vector<COutput> vCoins, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const
1917 setCoinsRet.clear();
1920 // List of values less than target
1921 pair<int64_t, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1922 coinLowestLarger.first = numeric_limits<int64_t>::max();
1923 coinLowestLarger.second.first = NULL;
1924 vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > > vValue;
1925 int64_t nTotalLower = 0;
1927 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
1929 for(const COutput &output : vCoins)
1931 if (!output.fSpendable)
1934 const CWalletTx *pcoin = output.tx;
1936 if (output.nDepth < (pcoin->IsFromMe(MINE_ALL) ? nConfMine : nConfTheirs))
1941 // Follow the timestamp rules
1942 if (pcoin->nTime > nSpendTime)
1945 auto n = pcoin->vout[i].nValue;
1946 auto coin = make_pair(n, make_pair(pcoin, i));
1948 if (n == nTargetValue)
1950 setCoinsRet.insert(coin.second);
1951 nValueRet += coin.first;
1954 else if (n < nTargetValue + CENT)
1956 vValue.push_back(coin);
1959 else if (n < coinLowestLarger.first)
1961 coinLowestLarger = coin;
1965 if (nTotalLower == nTargetValue)
1967 for (unsigned int i = 0; i < vValue.size(); ++i)
1969 setCoinsRet.insert(vValue[i].second);
1970 nValueRet += vValue[i].first;
1975 if (nTotalLower < nTargetValue)
1977 if (coinLowestLarger.second.first == NULL)
1979 setCoinsRet.insert(coinLowestLarger.second);
1980 nValueRet += coinLowestLarger.first;
1984 // Solve subset sum by stochastic approximation
1985 sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
1986 vector<char> vfBest;
1989 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1990 if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1991 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1993 // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
1994 // or the next bigger coin is closer), return the bigger coin
1995 if (coinLowestLarger.second.first &&
1996 ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1998 setCoinsRet.insert(coinLowestLarger.second);
1999 nValueRet += coinLowestLarger.first;
2002 for (unsigned int i = 0; i < vValue.size(); i++)
2005 setCoinsRet.insert(vValue[i].second);
2006 nValueRet += vValue[i].first;
2009 if (fDebug && GetBoolArg("-printpriority"))
2012 printf("SelectCoins() best subset: ");
2013 for (unsigned int i = 0; i < vValue.size(); i++)
2015 printf("%s ", FormatMoney(vValue[i].first).c_str());
2016 printf("total %s\n", FormatMoney(nBest).c_str());
2023 bool CWallet::SelectCoins(int64_t nTargetValue, unsigned int nSpendTime, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl* coinControl) const
2025 vector<COutput> vCoins;
2026 AvailableCoins(vCoins, true, coinControl);
2028 // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
2029 if (coinControl && coinControl->HasSelected())
2031 for(const COutput& out : vCoins)
2035 nValueRet += out.tx->vout[out.i].nValue;
2036 setCoinsRet.insert({ out.tx, out.i });
2038 return (nValueRet >= nTargetValue);
2041 return (SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 6, vCoins, setCoinsRet, nValueRet) ||
2042 SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 1, vCoins, setCoinsRet, nValueRet) ||
2043 SelectCoinsMinConf(nTargetValue, nSpendTime, 0, 1, vCoins, setCoinsRet, nValueRet));
2046 // Select some coins without random shuffle or best subset approximation
2047 bool CWallet::SelectCoinsSimple(int64_t nTargetValue, int64_t nMinValue, int64_t nMaxValue, unsigned int nSpendTime, int nMinConf, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const
2049 vector<COutput> vCoins;
2050 AvailableCoinsMinConf(vCoins, nMinConf, nMinValue, nMaxValue);
2052 setCoinsRet.clear();
2055 for(COutput output : vCoins)
2057 if(!output.fSpendable)
2059 const CWalletTx *pcoin = output.tx;
2062 // Ignore immature coins
2063 if (pcoin->GetBlocksToMaturity() > 0)
2066 // Stop if we've chosen enough inputs
2067 if (nValueRet >= nTargetValue)
2070 // Follow the timestamp rules
2071 if (pcoin->nTime > nSpendTime)
2074 auto n = pcoin->vout[i].nValue;
2075 auto coin = make_pair(n, make_pair(pcoin, i));
2077 if (n >= nTargetValue)
2079 // If input value is greater or equal to target then simply insert
2080 // it into the current subset and exit
2081 setCoinsRet.insert(coin.second);
2082 nValueRet += coin.first;
2085 else if (n < nTargetValue + CENT)
2087 setCoinsRet.insert(coin.second);
2088 nValueRet += coin.first;
2095 bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl* coinControl)
2098 for(auto& s : vecSend)
2104 if (vecSend.empty() || nValue < 0)
2107 wtxNew.BindWallet(this);
2110 LOCK2(cs_main, cs_wallet);
2111 // txdb must be opened before the mapWallet lock
2114 nFeeRet = nTransactionFee;
2118 wtxNew.vout.clear();
2119 wtxNew.fFromMe = true;
2121 auto nTotalValue = nValue + nFeeRet;
2122 double dPriority = 0;
2123 // vouts to the payees
2124 for(const auto& s : vecSend)
2125 wtxNew.vout.push_back(CTxOut(s.second, s.first));
2127 // Choose coins to use
2128 set<pair<const CWalletTx*,unsigned int> > setCoins;
2129 int64_t nValueIn = 0;
2130 if (!SelectCoins(nTotalValue, wtxNew.nTime, setCoins, nValueIn, coinControl))
2132 for(auto& pcoin : setCoins)
2134 auto nCredit = pcoin.first->vout[pcoin.second].nValue;
2135 dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
2138 auto nChange = nValueIn - nValue - nFeeRet;
2141 // Fill a vout to ourself
2142 // TODO: pass in scriptChange instead of reservekey so
2143 // change transaction isn't always pay-to-bitcoin-address
2144 CScript scriptChange;
2146 // coin control: send change to custom address
2147 if (coinControl && coinControl->destChange.IsValid())
2148 scriptChange.SetAddress(coinControl->destChange);
2150 // no coin control: send change to newly generated address
2153 // Note: We use a new key here to keep it from being obvious which side is the change.
2154 // The drawback is that by not reusing a previous key, the change may be lost if a
2155 // backup is restored, if the backup doesn't have the new private key for the change.
2156 // If we reused the old key, it would be possible to add code to look for and
2157 // rediscover unknown transactions that were written with keys of ours to recover
2158 // post-backup change.
2160 // Reserve a new key pair from key pool
2161 auto vchPubKey = reservekey.GetReservedKey();
2163 scriptChange.SetDestination(vchPubKey.GetID());
2166 // Insert change txn at random position:
2167 auto position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
2168 wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
2171 reservekey.ReturnKey();
2174 for(const auto& coin : setCoins)
2175 wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
2179 for(const auto& coin : setCoins)
2180 if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
2184 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
2185 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
2187 dPriority /= nBytes;
2189 // Check that enough fee is included
2190 bool fAllowFree = CTransaction::AllowFree(dPriority);
2191 auto nPayFee = nTransactionFee * (1 + (int64_t)nBytes / 1000);
2192 auto nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
2194 if (nFeeRet < max(nPayFee, nMinFee))
2196 nFeeRet = max(nPayFee, nMinFee);
2200 // Fill vtxPrev by copying from previous transactions vtxPrev
2201 wtxNew.AddSupportingTransactions(txdb);
2202 wtxNew.fTimeReceivedIsTxTime = true;
2211 bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl* coinControl)
2213 vector< pair<CScript, int64_t> > vecSend;
2214 vecSend.push_back({ scriptPubKey, nValue });
2215 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, coinControl);
2218 void CWallet::GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight)
2220 auto nTimeWeight = GetWeight(nTime, GetTime());
2222 // If time weight is lower or equal to zero then weight is zero.
2223 if (nTimeWeight <= 0)
2229 auto bnCoinDayWeight = CBigNum(nValue) * nTimeWeight / COIN / nOneDay;
2230 nWeight = bnCoinDayWeight.getuint64();
2233 bool CWallet::MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nOutputValue, list<uint256>& listMerged)
2235 auto nBalance = GetBalance();
2237 if (nAmount > nBalance)
2241 int64_t nValueIn = 0;
2242 set<pair<const CWalletTx*,unsigned int> > setCoins;
2244 // Simple coins selection - no randomization
2245 if (!SelectCoinsSimple(nAmount, nMinValue, nOutputValue, GetTime(), 1, setCoins, nValueIn))
2248 if (setCoins.empty())
2252 vector<const CWalletTx*> vwtxPrev;
2254 // Reserve a new key pair from key pool
2255 CReserveKey reservekey(this);
2256 auto vchPubKey = reservekey.GetReservedKey();
2259 CScript scriptOutput;
2260 scriptOutput.SetDestination(vchPubKey.GetID());
2263 wtxNew.vout.push_back(CTxOut(0, scriptOutput));
2266 for(auto& pcoin : setCoins)
2268 auto nCredit = pcoin.first->vout[pcoin.second].nValue;
2270 // Add current coin to inputs list and add its credit to transaction output
2271 wtxNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
2272 wtxNew.vout[0].nValue += nCredit;
2273 vwtxPrev.push_back(pcoin.first);
2276 // Replaced with estimation for performance purposes
2278 for (unsigned int i = 0; i < wtxNew.vin.size(); i++) {
2279 const CWalletTx *txin = vwtxPrev[i];
2281 // Sign scripts to get actual transaction size for fee calculation
2282 if (!SignSignature(*this, *txin, wtxNew, i))
2287 // Assuming that average scriptsig size is 110 bytes
2288 auto nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION) + wtxNew.vin.size() * 110;
2289 dWeight += (double)nCredit * pcoin.first->GetDepthInMainChain();
2291 double dFinalPriority = dWeight /= nBytes;
2292 bool fAllowFree = CTransaction::AllowFree(dFinalPriority);
2294 // Get actual transaction fee according to its estimated size and priority
2295 auto nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
2297 // Prepare transaction for commit if sum is enough ot its size is too big
2298 if (nBytes >= MAX_BLOCK_SIZE_GEN/6 || wtxNew.vout[0].nValue >= nOutputValue)
2300 wtxNew.vout[0].nValue -= nMinFee; // Set actual fee
2302 for (unsigned int i = 0; i < wtxNew.vin.size(); i++) {
2303 const CWalletTx *txin = vwtxPrev[i];
2306 if (!SignSignature(*this, *txin, wtxNew, i))
2310 // Try to commit, return false on failure
2311 if (!CommitTransaction(wtxNew, reservekey))
2314 listMerged.push_back(wtxNew.GetHash()); // Add to hashes list
2316 dWeight = 0; // Reset all temporary values
2319 wtxNew.vout.push_back(CTxOut(0, scriptOutput));
2323 // Create transactions if there are some unhandled coins left
2324 if (wtxNew.vout[0].nValue > 0) {
2325 int64_t nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION) + wtxNew.vin.size() * 110;
2327 double dFinalPriority = dWeight /= nBytes;
2328 bool fAllowFree = CTransaction::AllowFree(dFinalPriority);
2330 // Get actual transaction fee according to its size and priority
2331 auto nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND, nBytes);
2333 wtxNew.vout[0].nValue -= nMinFee; // Set actual fee
2335 if (wtxNew.vout[0].nValue <= 0)
2338 for (uint32_t i = 0; i < wtxNew.vin.size(); i++) {
2339 const CWalletTx *txin = vwtxPrev[i];
2341 // Sign all scripts again
2342 if (!SignSignature(*this, *txin, wtxNew, i))
2346 // Try to commit, return false on failure
2347 if (!CommitTransaction(wtxNew, reservekey))
2350 listMerged.push_back(wtxNew.GetHash()); // Add to hashes list
2356 bool CWallet::CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nGenerationTime, uint32_t nBits, CTransaction &txNew, CKey& key)
2359 if (!GetTransaction(hashTx, wtx))
2360 return error("Transaction %s is not found\n", hashTx.GetHex().c_str());
2362 vector<valtype> vSolutions;
2363 txnouttype whichType;
2364 CScript scriptPubKeyOut;
2365 auto scriptPubKeyKernel = wtx.vout[nOut].scriptPubKey;
2366 if (!Solver(scriptPubKeyKernel, whichType, vSolutions))
2367 return error("CreateCoinStake : failed to parse kernel\n");
2369 if (fDebug && GetBoolArg("-printcoinstake"))
2370 printf("CreateCoinStake : parsed kernel type=%d\n", whichType);
2372 if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH)
2373 return error("CreateCoinStake : no support for kernel type=%d\n", whichType);
2375 if (whichType == TX_PUBKEYHASH) // pay to address type
2377 // convert to pay to public key type
2378 if (!GetKey(uint160(vSolutions[0]), key))
2379 return error("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
2381 scriptPubKeyOut << key.GetPubKey() << OP_CHECKSIG;
2383 if (whichType == TX_PUBKEY)
2385 auto& vchPubKey = vSolutions[0];
2386 if (!GetKey(Hash160(vchPubKey), key))
2387 return error("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
2388 if (key.GetPubKey() != vchPubKey)
2389 return error("CreateCoinStake : invalid key for kernel type=%d\n", whichType); // keys mismatch
2390 scriptPubKeyOut = scriptPubKeyKernel;
2393 // The following combine threshold is important to security
2394 // Should not be adjusted if you don't understand the consequences
2395 auto nCombineThreshold = GetProofOfWorkReward(GetLastBlockIndex(pindexBest, false)->nBits) / 3;
2397 auto nBalance = GetBalance();
2398 auto nCredit = wtx.vout[nOut].nValue;
2403 // List of constake dependencies
2404 vector<const CWalletTx*> vwtxPrev;
2405 vwtxPrev.push_back(&wtx);
2407 // Set generation time, and kernel input
2408 txNew.nTime = nGenerationTime;
2409 txNew.vin.push_back(CTxIn(hashTx, nOut));
2411 // Mark coin stake transaction with empty vout[0]
2412 CScript scriptEmpty;
2413 scriptEmpty.clear();
2414 txNew.vout.push_back(CTxOut(0, scriptEmpty));
2416 if (fDebug && GetBoolArg("-printcoinstake"))
2417 printf("CreateCoinStake : added kernel type=%d\n", whichType);
2419 int64_t nValueIn = 0;
2421 if (!SelectCoinsSimple(nBalance - nReserveBalance, MIN_TX_FEE, MAX_MONEY, nGenerationTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
2424 if (setCoins.empty())
2427 bool fDontSplitCoins = false;
2428 if (GetWeight((int64_t)wtx.nTime, (int64_t)nGenerationTime) == nStakeMaxAge)
2430 // Only one output for old kernel inputs
2431 txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
2433 // Iterate through set of (wtx*, nout) in order to find some additional inputs for our new coinstake transaction.
2435 // * Value is higher than 0.01 NVC;
2436 // * Only add inputs of the same key/address as kernel;
2437 // * Input hash and kernel parent hash should be different.
2438 for(auto pcoin = setCoins.begin(); pcoin != setCoins.end(); pcoin++)
2440 // Stop adding more inputs if already too many inputs
2441 if (txNew.vin.size() >= 100)
2443 // Stop adding more inputs if value is already pretty significant
2444 if (nCredit > nCombineThreshold)
2446 // Stop adding inputs if reached reserve limit
2447 if (nCredit + pcoin->first->vout[pcoin->second].nValue > nBalance - nReserveBalance)
2450 auto nTimeWeight = GetWeight((int64_t)pcoin->first->nTime, (int64_t)nGenerationTime);
2452 // Do not add input that is still too young
2453 if (nTimeWeight < nStakeMaxAge)
2455 // Do not add input if key/address is not the same as kernel
2456 if (pcoin->first->vout[pcoin->second].scriptPubKey != scriptPubKeyKernel && pcoin->first->vout[pcoin->second].scriptPubKey != txNew.vout[1].scriptPubKey)
2458 // Do not add input if parents are the same
2459 if (pcoin->first->GetHash() != txNew.vin[0].prevout.hash)
2461 // Do not add additional significant input
2462 if (pcoin->first->vout[pcoin->second].nValue > nCombineThreshold)
2465 txNew.vin.push_back(CTxIn(pcoin->first->GetHash(), pcoin->second));
2466 nCredit += pcoin->first->vout[pcoin->second].nValue;
2467 vwtxPrev.push_back(pcoin->first);
2470 fDontSplitCoins = true;
2474 auto nSplitThreshold = GetArg("-splitthreshold", nCombineThreshold);
2476 if (fDebug && GetBoolArg("-printcoinstake"))
2477 printf("CreateCoinStake : nSplitThreshold=%" PRId64 "\n", nSplitThreshold);
2479 if (nCredit > nSplitThreshold)
2481 // Split stake input if credit is lower than combine threshold and maximum weight isn't reached yet
2482 txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
2483 txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
2485 if (fDebug && GetBoolArg("-printcoinstake"))
2486 printf("CreateCoinStake : splitting coinstake\n");
2490 txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
2491 fDontSplitCoins = true;
2495 // Calculate coin age reward
2498 if (!txNew.GetCoinAge(txdb, nCoinAge))
2499 return error("CreateCoinStake : failed to calculate coin age\n");
2500 nCredit += GetProofOfStakeReward(nCoinAge, nBits, nGenerationTime);
2502 int64_t nMinFee = 0;
2505 // Set output amount
2506 if (fDontSplitCoins)
2507 txNew.vout[1].nValue = nCredit - nMinFee;
2510 txNew.vout[1].nValue = ((nCredit - nMinFee) / 2 / CENT) * CENT;
2511 txNew.vout[2].nValue = nCredit - nMinFee - txNew.vout[1].nValue;
2516 for(const CWalletTx* pcoin : vwtxPrev)
2518 if (!SignSignature(*this, *pcoin, txNew, nIn++))
2519 return error("CreateCoinStake : failed to sign coinstake\n");
2523 auto nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
2524 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
2525 return error("CreateCoinStake : exceeded coinstake size limit\n");
2527 // Check enough fee is paid
2528 if (nMinFee < txNew.GetMinFee(1, false, GMF_BLOCK, nBytes) - CENT)
2530 nMinFee = txNew.GetMinFee(1, false, GMF_BLOCK, nBytes) - CENT;
2531 continue; // try signing again
2535 if (fDebug && GetBoolArg("-printfee"))
2536 printf("CreateCoinStake : fee for coinstake %s\n", FormatMoney(nMinFee).c_str());
2541 // Successfully created coinstake
2545 // Call after CreateTransaction unless you want to abort
2546 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
2549 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
2551 // Track how many getdata requests our transaction gets
2552 mapRequestCount[wtxNew.GetHash()] = 0;
2554 // Try to broadcast before saving
2555 if (!wtxNew.AcceptToMemoryPool())
2557 // This must not fail. The transaction has already been signed.
2558 printf("CommitTransaction() : Error: Transaction not valid");
2562 wtxNew.RelayWalletTransaction();
2565 LOCK2(cs_main, cs_wallet);
2567 // This is only to keep the database open to defeat the auto-flush for the
2568 // duration of this scope. This is the only place where this optimization
2569 // maybe makes sense; please don't do it anywhere else.
2570 CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
2572 // Take key pair from key pool so it won't be used again
2573 reservekey.KeepKey();
2575 // Add tx to wallet, because if it has change it's also ours,
2576 // otherwise just for transaction history.
2577 AddToWallet(wtxNew);
2579 // Mark old coins as spent
2580 for(const CTxIn& txin : wtxNew.vin)
2582 CWalletTx &coin = mapWallet[txin.prevout.hash];
2583 coin.BindWallet(this);
2584 coin.MarkSpent(txin.prevout.n);
2586 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
2587 vMintingWalletUpdated.push_back(coin.GetHash());
2600 string CWallet::SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee)
2604 return _("Invalid amount");
2605 if (nValue + nTransactionFee > GetBalance())
2606 return _("Insufficient funds");
2608 CReserveKey reservekey(this);
2609 int64_t nFeeRequired;
2613 string strError = _("Error: Wallet locked, unable to create transaction ");
2614 printf("SendMoney() : %s", strError.c_str());
2617 if (fWalletUnlockMintOnly)
2619 string strError = _("Error: Wallet unlocked for block minting only, unable to create transaction.");
2620 printf("SendMoney() : %s", strError.c_str());
2623 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
2626 if (nValue + nFeeRequired > GetBalance())
2627 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).c_str());
2629 strError = _("Error: Transaction creation failed ");
2630 printf("SendMoney() : %s", strError.c_str());
2634 if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired, _("Sending...")))
2637 if (!CommitTransaction(wtxNew, reservekey))
2638 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.");
2643 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
2647 fFirstRunRet = false;
2648 DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
2649 if (nLoadWalletRet == DB_NEED_REWRITE)
2651 if (CDB::Rewrite(strWalletFile, "\x04pool"))
2654 // Note: can't top-up keypool here, because wallet is locked.
2655 // User will be prompted to unlock wallet the next operation
2656 // the requires a new key.
2660 if (nLoadWalletRet != DB_LOAD_OK)
2661 return nLoadWalletRet;
2662 fFirstRunRet = !vchDefaultKey.IsValid();
2664 NewThread(ThreadFlushWalletDB, &strWalletFile);
2668 DBErrors CWallet::ZapWalletTx()
2672 DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this);
2673 if (nZapWalletTxRet == DB_NEED_REWRITE)
2675 if (CDB::Rewrite(strWalletFile, "\x04pool"))
2679 // Note: can't top-up keypool here, because wallet is locked.
2680 // User will be prompted to unlock wallet the next operation
2681 // the requires a new key.
2685 if (nZapWalletTxRet != DB_LOAD_OK)
2686 return nZapWalletTxRet;
2691 bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
2693 return SetAddressBookName(CBitcoinAddress(address), strName);
2696 bool CWallet::SetAddressBookName(const CBitcoinAddress& address, const string& strName)
2698 auto mi = mapAddressBook.find(address);
2699 mapAddressBook[address] = strName;
2700 NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != MINE_NO, (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
2703 return CWalletDB(strWalletFile).WriteName(address.ToString(), strName);
2706 bool CWallet::DelAddressBookName(const CBitcoinAddress& address)
2708 mapAddressBook.erase(address);
2709 NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != MINE_NO, CT_DELETED);
2712 return CWalletDB(strWalletFile).EraseName(address.ToString());
2716 void CWallet::PrintWallet(const CBlock& block)
2720 if (block.IsProofOfStake() && mapWallet.count(block.vtx[1].GetHash()))
2722 auto& wtx = mapWallet[block.vtx[1].GetHash()];
2723 printf(" PoS: %d %d %" PRId64 "", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit(MINE_ALL));
2725 else if (mapWallet.count(block.vtx[0].GetHash()))
2727 auto& wtx = mapWallet[block.vtx[0].GetHash()];
2728 printf(" PoW: %d %d %" PRId64 "", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit(MINE_ALL));
2734 void CWallet::Inventory(const uint256 &hash)
2738 auto mi = mapRequestCount.find(hash);
2739 if (mi != mapRequestCount.end())
2744 bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
2748 auto mi = mapWallet.find(hashTx);
2749 if (mi != mapWallet.end())
2758 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
2762 if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
2765 vchDefaultKey = vchPubKey;
2769 bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
2771 if (!pwallet->fFileBacked)
2773 strWalletFileOut = pwallet->strWalletFile;
2778 // Mark old keypool keys as used,
2779 // and generate all new keys
2781 bool CWallet::NewKeyPool(unsigned int nSize)
2785 CWalletDB walletdb(strWalletFile);
2786 for(int64_t nIndex : setKeyPool)
2787 walletdb.ErasePool(nIndex);
2797 nKeys = max<uint64_t>(GetArg("-keypool", 100), 0);
2799 for (uint64_t i = 0; i < nKeys; i++)
2802 walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
2803 setKeyPool.insert(nIndex);
2805 printf("CWallet::NewKeyPool wrote %" PRIu64 " new keys\n", nKeys);
2810 bool CWallet::TopUpKeyPool(unsigned int nSize)
2818 CWalletDB walletdb(strWalletFile);
2821 uint64_t nTargetSize;
2823 nTargetSize = nSize;
2825 nTargetSize = max<uint64_t>(GetArg("-keypool", 100), 0);
2827 while (setKeyPool.size() < (nTargetSize + 1))
2830 if (!setKeyPool.empty())
2831 nEnd = *(--setKeyPool.end()) + 1;
2832 if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
2833 throw runtime_error("TopUpKeyPool() : writing generated key failed");
2834 setKeyPool.insert(nEnd);
2835 printf("keypool added key %" PRIu64 ", size=%" PRIszu "\n", nEnd, setKeyPool.size());
2841 void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
2844 keypool.vchPubKey = CPubKey();
2851 // Get the oldest key
2852 if(setKeyPool.empty())
2855 CWalletDB walletdb(strWalletFile);
2857 nIndex = *(setKeyPool.begin());
2858 setKeyPool.erase(setKeyPool.begin());
2859 if (!walletdb.ReadPool(nIndex, keypool))
2860 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
2861 if (!HaveKey(keypool.vchPubKey.GetID()))
2862 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
2863 assert(keypool.vchPubKey.IsValid());
2864 if (fDebug && GetBoolArg("-printkeypool"))
2865 printf("keypool reserve %" PRId64 "\n", nIndex);
2869 int64_t CWallet::AddReserveKey(const CKeyPool& keypool)
2872 LOCK2(cs_main, cs_wallet);
2873 CWalletDB walletdb(strWalletFile);
2875 int64_t nIndex = 1 + *(--setKeyPool.end());
2876 if (!walletdb.WritePool(nIndex, keypool))
2877 throw runtime_error("AddReserveKey() : writing added key failed");
2878 setKeyPool.insert(nIndex);
2884 void CWallet::KeepKey(int64_t nIndex)
2886 // Remove from key pool
2889 CWalletDB walletdb(strWalletFile);
2890 walletdb.ErasePool(nIndex);
2893 printf("keypool keep %" PRId64 "\n", nIndex);
2896 void CWallet::ReturnKey(int64_t nIndex)
2898 // Return to key pool
2901 setKeyPool.insert(nIndex);
2904 printf("keypool return %" PRId64 "\n", nIndex);
2907 bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
2913 ReserveKeyFromKeyPool(nIndex, keypool);
2916 if (fAllowReuse && vchDefaultKey.IsValid())
2918 result = vchDefaultKey;
2921 if (IsLocked()) return false;
2922 result = GenerateNewKey();
2926 result = keypool.vchPubKey;
2931 int64_t CWallet::GetOldestKeyPoolTime()
2935 ReserveKeyFromKeyPool(nIndex, keypool);
2939 return keypool.nTime;
2942 map<CBitcoinAddress, int64_t> CWallet::GetAddressBalances()
2944 map<CBitcoinAddress, int64_t> balances;
2948 for(auto& walletEntry : mapWallet)
2950 CWalletTx *pcoin = &walletEntry.second;
2952 if (!pcoin->IsFinal() || !pcoin->IsTrusted())
2955 if ((pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetBlocksToMaturity() > 0)
2958 int nDepth = pcoin->GetDepthInMainChain();
2959 if (nDepth < (pcoin->IsFromMe(MINE_ALL) ? 0 : 1))
2962 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
2964 CBitcoinAddress addr;
2965 if (!IsMine(pcoin->vout[i]))
2967 if(!ExtractAddress(*this, pcoin->vout[i].scriptPubKey, addr))
2970 auto n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
2972 if (!balances.count(addr))
2974 balances[addr] += n;
2982 set< set<CBitcoinAddress> > CWallet::GetAddressGroupings()
2984 set< set<CBitcoinAddress> > groupings;
2985 set<CBitcoinAddress> grouping;
2987 for(auto& walletEntry : mapWallet)
2989 CWalletTx *pcoin = &walletEntry.second;
2991 if (pcoin->vin.size() > 0)
2993 bool any_mine = false;
2994 // group all input addresses with each other
2995 for(CTxIn txin : pcoin->vin)
2997 CBitcoinAddress address;
2998 if(!IsMine(txin)) // If this input isn't mine, ignore it
3000 if(!ExtractAddress(*this, mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
3002 grouping.insert(address);
3006 // group change with input addresses
3009 for(CTxOut txout : pcoin->vout)
3010 if (IsChange(txout))
3012 CBitcoinAddress txoutAddr;
3013 if(!ExtractAddress(*this, txout.scriptPubKey, txoutAddr))
3015 grouping.insert(txoutAddr);
3018 if (!grouping.empty())
3020 groupings.insert(grouping);
3025 // group lone addrs by themselves
3026 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
3027 if (IsMine(pcoin->vout[i]))
3029 CBitcoinAddress address;
3030 if(!ExtractAddress(*this, pcoin->vout[i].scriptPubKey, address))
3032 grouping.insert(address);
3033 groupings.insert(grouping);
3038 set< set<CBitcoinAddress>* > uniqueGroupings; // a set of pointers to groups of addresses
3039 map< CBitcoinAddress, set<CBitcoinAddress>* > setmap; // map addresses to the unique group containing it
3040 for(auto& grouping : groupings)
3042 // make a set of all the groups hit by this new group
3043 set< set<CBitcoinAddress>* > hits;
3044 map< CBitcoinAddress, set<CBitcoinAddress>* >::iterator it;
3045 for(auto& address : grouping)
3046 if ((it = setmap.find(address)) != setmap.end())
3047 hits.insert((*it).second);
3049 // merge all hit groups into a new single group and delete old groups
3050 set<CBitcoinAddress>* merged = new set<CBitcoinAddress>(grouping);
3051 for(auto hit : hits)
3053 merged->insert(hit->begin(), hit->end());
3054 uniqueGroupings.erase(hit);
3057 uniqueGroupings.insert(merged);
3060 for(CBitcoinAddress element : *merged)
3061 setmap[element] = merged;
3064 set< set<CBitcoinAddress> > ret;
3065 for(auto uniqueGrouping : uniqueGroupings)
3067 ret.insert(*uniqueGrouping);
3068 delete uniqueGrouping;
3074 // ppcoin: check 'spent' consistency between wallet and txindex
3075 // ppcoin: fix wallet spent state according to txindex
3076 void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion, bool fCheckOnly)
3079 nBalanceInQuestion = 0;
3082 vector<CWalletTx*> vCoins;
3083 vCoins.reserve(mapWallet.size());
3084 for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
3085 vCoins.push_back(&(*it).second);
3088 for(auto pcoin : vCoins)
3090 // Find the corresponding transaction index
3092 if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex))
3094 for (unsigned int n=0; n < pcoin->vout.size(); n++)
3096 if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull()))
3098 printf("FixSpentCoins found lost coin %sppc %s[%u], %s\n",
3099 FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
3101 nBalanceInQuestion += pcoin->vout[n].nValue;
3104 pcoin->MarkUnspent(n);
3105 pcoin->WriteToDisk();
3108 else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull()))
3110 printf("FixSpentCoins found spent coin %sppc %s[%u], %s\n",
3111 FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
3113 nBalanceInQuestion += pcoin->vout[n].nValue;
3116 pcoin->MarkSpent(n);
3117 pcoin->WriteToDisk();
3123 if(IsMine((CTransaction)*pcoin) && (pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetDepthInMainChain() == 0)
3125 printf("FixSpentCoins %s tx %s\n", fCheckOnly ? "found" : "removed", pcoin->GetHash().ToString().c_str());
3128 EraseFromWallet(pcoin->GetHash());
3134 // ppcoin: disable transaction (only for coinstake)
3135 void CWallet::DisableTransaction(const CTransaction &tx)
3137 if (!tx.IsCoinStake() || !IsFromMe(tx))
3138 return; // only disconnecting coinstake requires marking input unspent
3141 for(const auto& txin : tx.vin)
3143 auto mi = mapWallet.find(txin.prevout.hash);
3144 if (mi != mapWallet.end())
3146 auto& prev = (*mi).second;
3147 if (txin.prevout.n < prev.vout.size() && IsMine(prev.vout[txin.prevout.n]))
3149 prev.MarkUnspent(txin.prevout.n);
3156 CPubKey CReserveKey::GetReservedKey()
3161 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
3163 vchPubKey = keypool.vchPubKey;
3166 printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
3167 vchPubKey = pwallet->vchDefaultKey;
3170 assert(vchPubKey.IsValid());
3174 void CReserveKey::KeepKey()
3177 pwallet->KeepKey(nIndex);
3179 vchPubKey = CPubKey();
3182 void CReserveKey::ReturnKey()
3185 pwallet->ReturnKey(nIndex);
3187 vchPubKey = CPubKey();
3190 void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
3194 CWalletDB walletdb(strWalletFile);
3196 LOCK2(cs_main, cs_wallet);
3197 for(const int64_t& id : setKeyPool)
3200 if (!walletdb.ReadPool(id, keypool))
3201 throw runtime_error("GetAllReserveKeyHashes() : read failed");
3202 assert(keypool.vchPubKey.IsValid());
3203 auto keyID = keypool.vchPubKey.GetID();
3204 if (!HaveKey(keyID))
3205 throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
3206 setAddress.insert(keyID);
3210 void CWallet::UpdatedTransaction(const uint256 &hashTx)
3214 // Only notify UI if this transaction is in this wallet
3215 auto mi = mapWallet.find(hashTx);
3216 if (mi != mapWallet.end())
3218 NotifyTransactionChanged(this, hashTx, CT_UPDATED);
3219 vMintingWalletUpdated.push_back(hashTx);
3224 void CWallet::GetAddresses(map<CBitcoinAddress, int64_t> &mapAddresses) const {
3225 mapAddresses.clear();
3227 // get birth times for keys with metadata
3228 for (auto it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++) {
3229 mapAddresses[it->first] = it->second.nCreateTime ? it->second.nCreateTime : 0;
3232 for (auto it = mapWallet.begin(); it != mapWallet.end(); it++) {
3233 // iterate over all wallet transactions...
3234 const auto &wtx = (*it).second;
3235 if (wtx.hashBlock == 0)
3236 continue; // skip unconfirmed transactions
3238 for(auto it2 = wtx.vout.begin(); it2 != wtx.vout.end(); it2++) {
3239 const CTxOut &out = (*it2);
3240 // iterate over all their outputs
3241 CBitcoinAddress addressRet;
3242 if (ExtractAddress(*this, out.scriptPubKey, addressRet)) {
3243 if (mapAddresses.find(addressRet) != mapAddresses.end() && (mapAddresses[addressRet] == 0 || mapAddresses[addressRet] > wtx.nTime))
3244 mapAddresses[addressRet] = wtx.nTime;
3247 // multisig output affects more than one key
3248 vector<CKeyID> vAffected;
3249 ::ExtractAffectedKeys(*this, out.scriptPubKey, vAffected);
3251 for(auto it3 = vAffected.begin(); it3 != vAffected.end(); it3++) {
3252 CBitcoinAddress addrAffected(*it3);
3253 if (mapAddresses.find(addrAffected) != mapAddresses.end() && (mapAddresses[addrAffected] == 0 || mapAddresses[addrAffected] > wtx.nTime))
3254 mapAddresses[addrAffected] = wtx.nTime;
3262 void CWallet::ClearOrphans()
3264 list<uint256> orphans;
3267 for(auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
3269 const CWalletTx *wtx = &(*it).second;
3270 if((wtx->IsCoinBase() || wtx->IsCoinStake()) && !wtx->IsInMainChain())
3272 orphans.push_back(wtx->GetHash());
3276 for(auto it = orphans.begin(); it != orphans.end(); ++it)
3277 EraseFromWallet(*it);