Reject invalid wallet files
[bitcoinplatinum.git] / src / wallet / wallet.cpp
blobef1d764146c7f309da8d78ebbbd81d8de4d4a5ba
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "wallet/wallet.h"
8 #include "base58.h"
9 #include "checkpoints.h"
10 #include "chain.h"
11 #include "wallet/coincontrol.h"
12 #include "consensus/consensus.h"
13 #include "consensus/validation.h"
14 #include "fs.h"
15 #include "key.h"
16 #include "keystore.h"
17 #include "validation.h"
18 #include "net.h"
19 #include "policy/fees.h"
20 #include "policy/policy.h"
21 #include "policy/rbf.h"
22 #include "primitives/block.h"
23 #include "primitives/transaction.h"
24 #include "script/script.h"
25 #include "script/sign.h"
26 #include "scheduler.h"
27 #include "timedata.h"
28 #include "txmempool.h"
29 #include "util.h"
30 #include "ui_interface.h"
31 #include "utilmoneystr.h"
33 #include <assert.h>
35 #include <boost/algorithm/string/replace.hpp>
36 #include <boost/thread.hpp>
38 std::vector<CWalletRef> vpwallets;
39 /** Transaction fee set by the user */
40 CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
41 unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
42 bool bSpendZeroConfChange = DEFAULT_SPEND_ZEROCONF_CHANGE;
43 bool fWalletRbf = DEFAULT_WALLET_RBF;
45 const char * DEFAULT_WALLET_DAT = "wallet.dat";
46 const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
48 /**
49 * Fees smaller than this (in satoshi) are considered zero fee (for transaction creation)
50 * Override with -mintxfee
52 CFeeRate CWallet::minTxFee = CFeeRate(DEFAULT_TRANSACTION_MINFEE);
53 /**
54 * If fee estimation does not have enough data to provide estimates, use this fee instead.
55 * Has no effect if not using fee estimation
56 * Override with -fallbackfee
58 CFeeRate CWallet::fallbackFee = CFeeRate(DEFAULT_FALLBACK_FEE);
60 CFeeRate CWallet::m_discard_rate = CFeeRate(DEFAULT_DISCARD_FEE);
62 const uint256 CMerkleTx::ABANDON_HASH(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
64 /** @defgroup mapWallet
66 * @{
69 struct CompareValueOnly
71 bool operator()(const CInputCoin& t1,
72 const CInputCoin& t2) const
74 return t1.txout.nValue < t2.txout.nValue;
78 std::string COutput::ToString() const
80 return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString(), i, nDepth, FormatMoney(tx->tx->vout[i].nValue));
83 const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
85 LOCK(cs_wallet);
86 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash);
87 if (it == mapWallet.end())
88 return NULL;
89 return &(it->second);
92 CPubKey CWallet::GenerateNewKey(CWalletDB &walletdb, bool internal)
94 AssertLockHeld(cs_wallet); // mapKeyMetadata
95 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
97 CKey secret;
99 // Create new metadata
100 int64_t nCreationTime = GetTime();
101 CKeyMetadata metadata(nCreationTime);
103 // use HD key derivation if HD was enabled during wallet creation
104 if (IsHDEnabled()) {
105 DeriveNewChildKey(walletdb, metadata, secret, (CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
106 } else {
107 secret.MakeNewKey(fCompressed);
110 // Compressed public keys were introduced in version 0.6.0
111 if (fCompressed) {
112 SetMinVersion(FEATURE_COMPRPUBKEY);
115 CPubKey pubkey = secret.GetPubKey();
116 assert(secret.VerifyPubKey(pubkey));
118 mapKeyMetadata[pubkey.GetID()] = metadata;
119 UpdateTimeFirstKey(nCreationTime);
121 if (!AddKeyPubKeyWithDB(walletdb, secret, pubkey)) {
122 throw std::runtime_error(std::string(__func__) + ": AddKey failed");
124 return pubkey;
127 void CWallet::DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKey& secret, bool internal)
129 // for now we use a fixed keypath scheme of m/0'/0'/k
130 CKey key; //master key seed (256bit)
131 CExtKey masterKey; //hd master key
132 CExtKey accountKey; //key at m/0'
133 CExtKey chainChildKey; //key at m/0'/0' (external) or m/0'/1' (internal)
134 CExtKey childKey; //key at m/0'/0'/<n>'
136 // try to get the master key
137 if (!GetKey(hdChain.masterKeyID, key))
138 throw std::runtime_error(std::string(__func__) + ": Master key not found");
140 masterKey.SetMaster(key.begin(), key.size());
142 // derive m/0'
143 // use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
144 masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT);
146 // derive m/0'/0' (external chain) OR m/0'/1' (internal chain)
147 assert(internal ? CanSupportFeature(FEATURE_HD_SPLIT) : true);
148 accountKey.Derive(chainChildKey, BIP32_HARDENED_KEY_LIMIT+(internal ? 1 : 0));
150 // derive child key at next index, skip keys already known to the wallet
151 do {
152 // always derive hardened keys
153 // childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
154 // example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
155 if (internal) {
156 chainChildKey.Derive(childKey, hdChain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
157 metadata.hdKeypath = "m/0'/1'/" + std::to_string(hdChain.nInternalChainCounter) + "'";
158 hdChain.nInternalChainCounter++;
160 else {
161 chainChildKey.Derive(childKey, hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
162 metadata.hdKeypath = "m/0'/0'/" + std::to_string(hdChain.nExternalChainCounter) + "'";
163 hdChain.nExternalChainCounter++;
165 } while (HaveKey(childKey.key.GetPubKey().GetID()));
166 secret = childKey.key;
167 metadata.hdMasterKeyID = hdChain.masterKeyID;
168 // update the chain model in the database
169 if (!walletdb.WriteHDChain(hdChain))
170 throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
173 bool CWallet::AddKeyPubKeyWithDB(CWalletDB &walletdb, const CKey& secret, const CPubKey &pubkey)
175 AssertLockHeld(cs_wallet); // mapKeyMetadata
177 // CCryptoKeyStore has no concept of wallet databases, but calls AddCryptedKey
178 // which is overridden below. To avoid flushes, the database handle is
179 // tunneled through to it.
180 bool needsDB = !pwalletdbEncryption;
181 if (needsDB) {
182 pwalletdbEncryption = &walletdb;
184 if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey)) {
185 if (needsDB) pwalletdbEncryption = NULL;
186 return false;
188 if (needsDB) pwalletdbEncryption = NULL;
190 // check if we need to remove from watch-only
191 CScript script;
192 script = GetScriptForDestination(pubkey.GetID());
193 if (HaveWatchOnly(script)) {
194 RemoveWatchOnly(script);
196 script = GetScriptForRawPubKey(pubkey);
197 if (HaveWatchOnly(script)) {
198 RemoveWatchOnly(script);
201 if (!IsCrypted()) {
202 return walletdb.WriteKey(pubkey,
203 secret.GetPrivKey(),
204 mapKeyMetadata[pubkey.GetID()]);
206 return true;
209 bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
211 CWalletDB walletdb(*dbw);
212 return CWallet::AddKeyPubKeyWithDB(walletdb, secret, pubkey);
215 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
216 const std::vector<unsigned char> &vchCryptedSecret)
218 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
219 return false;
221 LOCK(cs_wallet);
222 if (pwalletdbEncryption)
223 return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
224 vchCryptedSecret,
225 mapKeyMetadata[vchPubKey.GetID()]);
226 else
227 return CWalletDB(*dbw).WriteCryptedKey(vchPubKey,
228 vchCryptedSecret,
229 mapKeyMetadata[vchPubKey.GetID()]);
233 bool CWallet::LoadKeyMetadata(const CTxDestination& keyID, const CKeyMetadata &meta)
235 AssertLockHeld(cs_wallet); // mapKeyMetadata
236 UpdateTimeFirstKey(meta.nCreateTime);
237 mapKeyMetadata[keyID] = meta;
238 return true;
241 bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
243 return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
247 * Update wallet first key creation time. This should be called whenever keys
248 * are added to the wallet, with the oldest key creation time.
250 void CWallet::UpdateTimeFirstKey(int64_t nCreateTime)
252 AssertLockHeld(cs_wallet);
253 if (nCreateTime <= 1) {
254 // Cannot determine birthday information, so set the wallet birthday to
255 // the beginning of time.
256 nTimeFirstKey = 1;
257 } else if (!nTimeFirstKey || nCreateTime < nTimeFirstKey) {
258 nTimeFirstKey = nCreateTime;
262 bool CWallet::AddCScript(const CScript& redeemScript)
264 if (!CCryptoKeyStore::AddCScript(redeemScript))
265 return false;
266 return CWalletDB(*dbw).WriteCScript(Hash160(redeemScript), redeemScript);
269 bool CWallet::LoadCScript(const CScript& redeemScript)
271 /* A sanity check was added in pull #3843 to avoid adding redeemScripts
272 * that never can be redeemed. However, old wallets may still contain
273 * these. Do not add them to the wallet and warn. */
274 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
276 std::string strAddr = CBitcoinAddress(CScriptID(redeemScript)).ToString();
277 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",
278 __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
279 return true;
282 return CCryptoKeyStore::AddCScript(redeemScript);
285 bool CWallet::AddWatchOnly(const CScript& dest)
287 if (!CCryptoKeyStore::AddWatchOnly(dest))
288 return false;
289 const CKeyMetadata& meta = mapKeyMetadata[CScriptID(dest)];
290 UpdateTimeFirstKey(meta.nCreateTime);
291 NotifyWatchonlyChanged(true);
292 return CWalletDB(*dbw).WriteWatchOnly(dest, meta);
295 bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
297 mapKeyMetadata[CScriptID(dest)].nCreateTime = nCreateTime;
298 return AddWatchOnly(dest);
301 bool CWallet::RemoveWatchOnly(const CScript &dest)
303 AssertLockHeld(cs_wallet);
304 if (!CCryptoKeyStore::RemoveWatchOnly(dest))
305 return false;
306 if (!HaveWatchOnly())
307 NotifyWatchonlyChanged(false);
308 if (!CWalletDB(*dbw).EraseWatchOnly(dest))
309 return false;
311 return true;
314 bool CWallet::LoadWatchOnly(const CScript &dest)
316 return CCryptoKeyStore::AddWatchOnly(dest);
319 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
321 CCrypter crypter;
322 CKeyingMaterial _vMasterKey;
325 LOCK(cs_wallet);
326 for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
328 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
329 return false;
330 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
331 continue; // try another master key
332 if (CCryptoKeyStore::Unlock(_vMasterKey))
333 return true;
336 return false;
339 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
341 bool fWasLocked = IsLocked();
344 LOCK(cs_wallet);
345 Lock();
347 CCrypter crypter;
348 CKeyingMaterial _vMasterKey;
349 for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
351 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
352 return false;
353 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
354 return false;
355 if (CCryptoKeyStore::Unlock(_vMasterKey))
357 int64_t nStartTime = GetTimeMillis();
358 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
359 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
361 nStartTime = GetTimeMillis();
362 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
363 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
365 if (pMasterKey.second.nDeriveIterations < 25000)
366 pMasterKey.second.nDeriveIterations = 25000;
368 LogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
370 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
371 return false;
372 if (!crypter.Encrypt(_vMasterKey, pMasterKey.second.vchCryptedKey))
373 return false;
374 CWalletDB(*dbw).WriteMasterKey(pMasterKey.first, pMasterKey.second);
375 if (fWasLocked)
376 Lock();
377 return true;
382 return false;
385 void CWallet::SetBestChain(const CBlockLocator& loc)
387 CWalletDB walletdb(*dbw);
388 walletdb.WriteBestBlock(loc);
391 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
393 LOCK(cs_wallet); // nWalletVersion
394 if (nWalletVersion >= nVersion)
395 return true;
397 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
398 if (fExplicit && nVersion > nWalletMaxVersion)
399 nVersion = FEATURE_LATEST;
401 nWalletVersion = nVersion;
403 if (nVersion > nWalletMaxVersion)
404 nWalletMaxVersion = nVersion;
407 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(*dbw);
408 if (nWalletVersion > 40000)
409 pwalletdb->WriteMinVersion(nWalletVersion);
410 if (!pwalletdbIn)
411 delete pwalletdb;
414 return true;
417 bool CWallet::SetMaxVersion(int nVersion)
419 LOCK(cs_wallet); // nWalletVersion, nWalletMaxVersion
420 // cannot downgrade below current version
421 if (nWalletVersion > nVersion)
422 return false;
424 nWalletMaxVersion = nVersion;
426 return true;
429 std::set<uint256> CWallet::GetConflicts(const uint256& txid) const
431 std::set<uint256> result;
432 AssertLockHeld(cs_wallet);
434 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(txid);
435 if (it == mapWallet.end())
436 return result;
437 const CWalletTx& wtx = it->second;
439 std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
441 for (const CTxIn& txin : wtx.tx->vin)
443 if (mapTxSpends.count(txin.prevout) <= 1)
444 continue; // No conflict if zero or one spends
445 range = mapTxSpends.equal_range(txin.prevout);
446 for (TxSpends::const_iterator _it = range.first; _it != range.second; ++_it)
447 result.insert(_it->second);
449 return result;
452 bool CWallet::HasWalletSpend(const uint256& txid) const
454 AssertLockHeld(cs_wallet);
455 auto iter = mapTxSpends.lower_bound(COutPoint(txid, 0));
456 return (iter != mapTxSpends.end() && iter->first.hash == txid);
459 void CWallet::Flush(bool shutdown)
461 dbw->Flush(shutdown);
464 bool CWallet::Verify()
466 if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET))
467 return true;
469 uiInterface.InitMessage(_("Verifying wallet(s)..."));
471 // Keep track of each wallet absolute path to detect duplicates.
472 std::set<fs::path> wallet_paths;
474 for (const std::string& walletFile : gArgs.GetArgs("-wallet")) {
475 if (boost::filesystem::path(walletFile).filename() != walletFile) {
476 return InitError(_("-wallet parameter must only specify a filename (not a path)"));
479 if (SanitizeString(walletFile, SAFE_CHARS_FILENAME) != walletFile) {
480 return InitError(_("Invalid characters in -wallet filename"));
483 fs::path wallet_path = fs::absolute(walletFile, GetDataDir());
485 if (fs::exists(wallet_path) && (!fs::is_regular_file(wallet_path) || fs::is_symlink(wallet_path))) {
486 return InitError(_("Invalid -wallet file"));
489 if (!wallet_paths.insert(wallet_path).second) {
490 return InitError(_("Duplicate -wallet filename"));
493 std::string strError;
494 if (!CWalletDB::VerifyEnvironment(walletFile, GetDataDir().string(), strError)) {
495 return InitError(strError);
498 if (GetBoolArg("-salvagewallet", false)) {
499 // Recover readable keypairs:
500 CWallet dummyWallet;
501 std::string backup_filename;
502 if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename)) {
503 return false;
507 std::string strWarning;
508 bool dbV = CWalletDB::VerifyDatabaseFile(walletFile, GetDataDir().string(), strWarning, strError);
509 if (!strWarning.empty()) {
510 InitWarning(strWarning);
512 if (!dbV) {
513 InitError(strError);
514 return false;
518 return true;
521 void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)
523 // We want all the wallet transactions in range to have the same metadata as
524 // the oldest (smallest nOrderPos).
525 // So: find smallest nOrderPos:
527 int nMinOrderPos = std::numeric_limits<int>::max();
528 const CWalletTx* copyFrom = NULL;
529 for (TxSpends::iterator it = range.first; it != range.second; ++it)
531 const uint256& hash = it->second;
532 int n = mapWallet[hash].nOrderPos;
533 if (n < nMinOrderPos)
535 nMinOrderPos = n;
536 copyFrom = &mapWallet[hash];
539 // Now copy data from copyFrom to rest:
540 for (TxSpends::iterator it = range.first; it != range.second; ++it)
542 const uint256& hash = it->second;
543 CWalletTx* copyTo = &mapWallet[hash];
544 if (copyFrom == copyTo) continue;
545 if (!copyFrom->IsEquivalentTo(*copyTo)) continue;
546 copyTo->mapValue = copyFrom->mapValue;
547 copyTo->vOrderForm = copyFrom->vOrderForm;
548 // fTimeReceivedIsTxTime not copied on purpose
549 // nTimeReceived not copied on purpose
550 copyTo->nTimeSmart = copyFrom->nTimeSmart;
551 copyTo->fFromMe = copyFrom->fFromMe;
552 copyTo->strFromAccount = copyFrom->strFromAccount;
553 // nOrderPos not copied on purpose
554 // cached members not copied on purpose
559 * Outpoint is spent if any non-conflicted transaction
560 * spends it:
562 bool CWallet::IsSpent(const uint256& hash, unsigned int n) const
564 const COutPoint outpoint(hash, n);
565 std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
566 range = mapTxSpends.equal_range(outpoint);
568 for (TxSpends::const_iterator it = range.first; it != range.second; ++it)
570 const uint256& wtxid = it->second;
571 std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
572 if (mit != mapWallet.end()) {
573 int depth = mit->second.GetDepthInMainChain();
574 if (depth > 0 || (depth == 0 && !mit->second.isAbandoned()))
575 return true; // Spent
578 return false;
581 void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid)
583 mapTxSpends.insert(std::make_pair(outpoint, wtxid));
585 std::pair<TxSpends::iterator, TxSpends::iterator> range;
586 range = mapTxSpends.equal_range(outpoint);
587 SyncMetaData(range);
591 void CWallet::AddToSpends(const uint256& wtxid)
593 assert(mapWallet.count(wtxid));
594 CWalletTx& thisTx = mapWallet[wtxid];
595 if (thisTx.IsCoinBase()) // Coinbases don't spend anything!
596 return;
598 for (const CTxIn& txin : thisTx.tx->vin)
599 AddToSpends(txin.prevout, wtxid);
602 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
604 if (IsCrypted())
605 return false;
607 CKeyingMaterial _vMasterKey;
609 _vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
610 GetStrongRandBytes(&_vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
612 CMasterKey kMasterKey;
614 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
615 GetStrongRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
617 CCrypter crypter;
618 int64_t nStartTime = GetTimeMillis();
619 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
620 kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
622 nStartTime = GetTimeMillis();
623 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
624 kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
626 if (kMasterKey.nDeriveIterations < 25000)
627 kMasterKey.nDeriveIterations = 25000;
629 LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
631 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
632 return false;
633 if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey))
634 return false;
637 LOCK(cs_wallet);
638 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
639 assert(!pwalletdbEncryption);
640 pwalletdbEncryption = new CWalletDB(*dbw);
641 if (!pwalletdbEncryption->TxnBegin()) {
642 delete pwalletdbEncryption;
643 pwalletdbEncryption = NULL;
644 return false;
646 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
648 if (!EncryptKeys(_vMasterKey))
650 pwalletdbEncryption->TxnAbort();
651 delete pwalletdbEncryption;
652 // We now probably have half of our keys encrypted in memory, and half not...
653 // die and let the user reload the unencrypted wallet.
654 assert(false);
657 // Encryption was introduced in version 0.4.0
658 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
660 if (!pwalletdbEncryption->TxnCommit()) {
661 delete pwalletdbEncryption;
662 // We now have keys encrypted in memory, but not on disk...
663 // die to avoid confusion and let the user reload the unencrypted wallet.
664 assert(false);
667 delete pwalletdbEncryption;
668 pwalletdbEncryption = NULL;
670 Lock();
671 Unlock(strWalletPassphrase);
673 // if we are using HD, replace the HD master key (seed) with a new one
674 if (IsHDEnabled()) {
675 if (!SetHDMasterKey(GenerateNewHDMasterKey())) {
676 return false;
680 NewKeyPool();
681 Lock();
683 // Need to completely rewrite the wallet file; if we don't, bdb might keep
684 // bits of the unencrypted private key in slack space in the database file.
685 dbw->Rewrite();
688 NotifyStatusChanged(this);
690 return true;
693 DBErrors CWallet::ReorderTransactions()
695 LOCK(cs_wallet);
696 CWalletDB walletdb(*dbw);
698 // Old wallets didn't have any defined order for transactions
699 // Probably a bad idea to change the output of this
701 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
702 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
703 typedef std::multimap<int64_t, TxPair > TxItems;
704 TxItems txByTime;
706 for (std::map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
708 CWalletTx* wtx = &((*it).second);
709 txByTime.insert(std::make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
711 std::list<CAccountingEntry> acentries;
712 walletdb.ListAccountCreditDebit("", acentries);
713 for (CAccountingEntry& entry : acentries)
715 txByTime.insert(std::make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
718 nOrderPosNext = 0;
719 std::vector<int64_t> nOrderPosOffsets;
720 for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
722 CWalletTx *const pwtx = (*it).second.first;
723 CAccountingEntry *const pacentry = (*it).second.second;
724 int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
726 if (nOrderPos == -1)
728 nOrderPos = nOrderPosNext++;
729 nOrderPosOffsets.push_back(nOrderPos);
731 if (pwtx)
733 if (!walletdb.WriteTx(*pwtx))
734 return DB_LOAD_FAIL;
736 else
737 if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
738 return DB_LOAD_FAIL;
740 else
742 int64_t nOrderPosOff = 0;
743 for (const int64_t& nOffsetStart : nOrderPosOffsets)
745 if (nOrderPos >= nOffsetStart)
746 ++nOrderPosOff;
748 nOrderPos += nOrderPosOff;
749 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
751 if (!nOrderPosOff)
752 continue;
754 // Since we're changing the order, write it back
755 if (pwtx)
757 if (!walletdb.WriteTx(*pwtx))
758 return DB_LOAD_FAIL;
760 else
761 if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
762 return DB_LOAD_FAIL;
765 walletdb.WriteOrderPosNext(nOrderPosNext);
767 return DB_LOAD_OK;
770 int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
772 AssertLockHeld(cs_wallet); // nOrderPosNext
773 int64_t nRet = nOrderPosNext++;
774 if (pwalletdb) {
775 pwalletdb->WriteOrderPosNext(nOrderPosNext);
776 } else {
777 CWalletDB(*dbw).WriteOrderPosNext(nOrderPosNext);
779 return nRet;
782 bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment)
784 CWalletDB walletdb(*dbw);
785 if (!walletdb.TxnBegin())
786 return false;
788 int64_t nNow = GetAdjustedTime();
790 // Debit
791 CAccountingEntry debit;
792 debit.nOrderPos = IncOrderPosNext(&walletdb);
793 debit.strAccount = strFrom;
794 debit.nCreditDebit = -nAmount;
795 debit.nTime = nNow;
796 debit.strOtherAccount = strTo;
797 debit.strComment = strComment;
798 AddAccountingEntry(debit, &walletdb);
800 // Credit
801 CAccountingEntry credit;
802 credit.nOrderPos = IncOrderPosNext(&walletdb);
803 credit.strAccount = strTo;
804 credit.nCreditDebit = nAmount;
805 credit.nTime = nNow;
806 credit.strOtherAccount = strFrom;
807 credit.strComment = strComment;
808 AddAccountingEntry(credit, &walletdb);
810 if (!walletdb.TxnCommit())
811 return false;
813 return true;
816 bool CWallet::GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew)
818 CWalletDB walletdb(*dbw);
820 CAccount account;
821 walletdb.ReadAccount(strAccount, account);
823 if (!bForceNew) {
824 if (!account.vchPubKey.IsValid())
825 bForceNew = true;
826 else {
827 // Check if the current key has been used
828 CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID());
829 for (std::map<uint256, CWalletTx>::iterator it = mapWallet.begin();
830 it != mapWallet.end() && account.vchPubKey.IsValid();
831 ++it)
832 for (const CTxOut& txout : (*it).second.tx->vout)
833 if (txout.scriptPubKey == scriptPubKey) {
834 bForceNew = true;
835 break;
840 // Generate a new key
841 if (bForceNew) {
842 if (!GetKeyFromPool(account.vchPubKey, false))
843 return false;
845 SetAddressBook(account.vchPubKey.GetID(), strAccount, "receive");
846 walletdb.WriteAccount(strAccount, account);
849 pubKey = account.vchPubKey;
851 return true;
854 void CWallet::MarkDirty()
857 LOCK(cs_wallet);
858 for (std::pair<const uint256, CWalletTx>& item : mapWallet)
859 item.second.MarkDirty();
863 bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
865 LOCK(cs_wallet);
867 auto mi = mapWallet.find(originalHash);
869 // There is a bug if MarkReplaced is not called on an existing wallet transaction.
870 assert(mi != mapWallet.end());
872 CWalletTx& wtx = (*mi).second;
874 // Ensure for now that we're not overwriting data
875 assert(wtx.mapValue.count("replaced_by_txid") == 0);
877 wtx.mapValue["replaced_by_txid"] = newHash.ToString();
879 CWalletDB walletdb(*dbw, "r+");
881 bool success = true;
882 if (!walletdb.WriteTx(wtx)) {
883 LogPrintf("%s: Updating walletdb tx %s failed", __func__, wtx.GetHash().ToString());
884 success = false;
887 NotifyTransactionChanged(this, originalHash, CT_UPDATED);
889 return success;
892 bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
894 LOCK(cs_wallet);
896 CWalletDB walletdb(*dbw, "r+", fFlushOnClose);
898 uint256 hash = wtxIn.GetHash();
900 // Inserts only if not already there, returns tx inserted or tx found
901 std::pair<std::map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(std::make_pair(hash, wtxIn));
902 CWalletTx& wtx = (*ret.first).second;
903 wtx.BindWallet(this);
904 bool fInsertedNew = ret.second;
905 if (fInsertedNew)
907 wtx.nTimeReceived = GetAdjustedTime();
908 wtx.nOrderPos = IncOrderPosNext(&walletdb);
909 wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
910 wtx.nTimeSmart = ComputeTimeSmart(wtx);
911 AddToSpends(hash);
914 bool fUpdated = false;
915 if (!fInsertedNew)
917 // Merge
918 if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock)
920 wtx.hashBlock = wtxIn.hashBlock;
921 fUpdated = true;
923 // If no longer abandoned, update
924 if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned())
926 wtx.hashBlock = wtxIn.hashBlock;
927 fUpdated = true;
929 if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex))
931 wtx.nIndex = wtxIn.nIndex;
932 fUpdated = true;
934 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
936 wtx.fFromMe = wtxIn.fFromMe;
937 fUpdated = true;
941 //// debug print
942 LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
944 // Write to disk
945 if (fInsertedNew || fUpdated)
946 if (!walletdb.WriteTx(wtx))
947 return false;
949 // Break debit/credit balance caches:
950 wtx.MarkDirty();
952 // Notify UI of new or updated transaction
953 NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
955 // notify an external script when a wallet transaction comes in or is updated
956 std::string strCmd = GetArg("-walletnotify", "");
958 if ( !strCmd.empty())
960 boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
961 boost::thread t(runCommand, strCmd); // thread runs free
964 return true;
967 bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
969 uint256 hash = wtxIn.GetHash();
971 mapWallet[hash] = wtxIn;
972 CWalletTx& wtx = mapWallet[hash];
973 wtx.BindWallet(this);
974 wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
975 AddToSpends(hash);
976 for (const CTxIn& txin : wtx.tx->vin) {
977 if (mapWallet.count(txin.prevout.hash)) {
978 CWalletTx& prevtx = mapWallet[txin.prevout.hash];
979 if (prevtx.nIndex == -1 && !prevtx.hashUnset()) {
980 MarkConflicted(prevtx.hashBlock, wtx.GetHash());
985 return true;
989 * Add a transaction to the wallet, or update it. pIndex and posInBlock should
990 * be set when the transaction was known to be included in a block. When
991 * pIndex == NULL, then wallet state is not updated in AddToWallet, but
992 * notifications happen and cached balances are marked dirty.
994 * If fUpdate is true, existing transactions will be updated.
995 * TODO: One exception to this is that the abandoned state is cleared under the
996 * assumption that any further notification of a transaction that was considered
997 * abandoned is an indication that it is not safe to be considered abandoned.
998 * Abandoned state should probably be more carefully tracked via different
999 * posInBlock signals or by checking mempool presence when necessary.
1001 bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate)
1003 const CTransaction& tx = *ptx;
1005 AssertLockHeld(cs_wallet);
1007 if (pIndex != NULL) {
1008 for (const CTxIn& txin : tx.vin) {
1009 std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range = mapTxSpends.equal_range(txin.prevout);
1010 while (range.first != range.second) {
1011 if (range.first->second != tx.GetHash()) {
1012 LogPrintf("Transaction %s (in block %s) conflicts with wallet transaction %s (both spend %s:%i)\n", tx.GetHash().ToString(), pIndex->GetBlockHash().ToString(), range.first->second.ToString(), range.first->first.hash.ToString(), range.first->first.n);
1013 MarkConflicted(pIndex->GetBlockHash(), range.first->second);
1015 range.first++;
1020 bool fExisted = mapWallet.count(tx.GetHash()) != 0;
1021 if (fExisted && !fUpdate) return false;
1022 if (fExisted || IsMine(tx) || IsFromMe(tx))
1024 CWalletTx wtx(this, ptx);
1026 // Get merkle branch if transaction was found in a block
1027 if (pIndex != NULL)
1028 wtx.SetMerkleBranch(pIndex, posInBlock);
1030 return AddToWallet(wtx, false);
1033 return false;
1036 bool CWallet::TransactionCanBeAbandoned(const uint256& hashTx) const
1038 LOCK2(cs_main, cs_wallet);
1039 const CWalletTx* wtx = GetWalletTx(hashTx);
1040 return wtx && !wtx->isAbandoned() && wtx->GetDepthInMainChain() <= 0 && !wtx->InMempool();
1043 bool CWallet::AbandonTransaction(const uint256& hashTx)
1045 LOCK2(cs_main, cs_wallet);
1047 CWalletDB walletdb(*dbw, "r+");
1049 std::set<uint256> todo;
1050 std::set<uint256> done;
1052 // Can't mark abandoned if confirmed or in mempool
1053 assert(mapWallet.count(hashTx));
1054 CWalletTx& origtx = mapWallet[hashTx];
1055 if (origtx.GetDepthInMainChain() > 0 || origtx.InMempool()) {
1056 return false;
1059 todo.insert(hashTx);
1061 while (!todo.empty()) {
1062 uint256 now = *todo.begin();
1063 todo.erase(now);
1064 done.insert(now);
1065 assert(mapWallet.count(now));
1066 CWalletTx& wtx = mapWallet[now];
1067 int currentconfirm = wtx.GetDepthInMainChain();
1068 // If the orig tx was not in block, none of its spends can be
1069 assert(currentconfirm <= 0);
1070 // if (currentconfirm < 0) {Tx and spends are already conflicted, no need to abandon}
1071 if (currentconfirm == 0 && !wtx.isAbandoned()) {
1072 // If the orig tx was not in block/mempool, none of its spends can be in mempool
1073 assert(!wtx.InMempool());
1074 wtx.nIndex = -1;
1075 wtx.setAbandoned();
1076 wtx.MarkDirty();
1077 walletdb.WriteTx(wtx);
1078 NotifyTransactionChanged(this, wtx.GetHash(), CT_UPDATED);
1079 // Iterate over all its outputs, and mark transactions in the wallet that spend them abandoned too
1080 TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(hashTx, 0));
1081 while (iter != mapTxSpends.end() && iter->first.hash == now) {
1082 if (!done.count(iter->second)) {
1083 todo.insert(iter->second);
1085 iter++;
1087 // If a transaction changes 'conflicted' state, that changes the balance
1088 // available of the outputs it spends. So force those to be recomputed
1089 for (const CTxIn& txin : wtx.tx->vin)
1091 if (mapWallet.count(txin.prevout.hash))
1092 mapWallet[txin.prevout.hash].MarkDirty();
1097 return true;
1100 void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
1102 LOCK2(cs_main, cs_wallet);
1104 int conflictconfirms = 0;
1105 if (mapBlockIndex.count(hashBlock)) {
1106 CBlockIndex* pindex = mapBlockIndex[hashBlock];
1107 if (chainActive.Contains(pindex)) {
1108 conflictconfirms = -(chainActive.Height() - pindex->nHeight + 1);
1111 // If number of conflict confirms cannot be determined, this means
1112 // that the block is still unknown or not yet part of the main chain,
1113 // for example when loading the wallet during a reindex. Do nothing in that
1114 // case.
1115 if (conflictconfirms >= 0)
1116 return;
1118 // Do not flush the wallet here for performance reasons
1119 CWalletDB walletdb(*dbw, "r+", false);
1121 std::set<uint256> todo;
1122 std::set<uint256> done;
1124 todo.insert(hashTx);
1126 while (!todo.empty()) {
1127 uint256 now = *todo.begin();
1128 todo.erase(now);
1129 done.insert(now);
1130 assert(mapWallet.count(now));
1131 CWalletTx& wtx = mapWallet[now];
1132 int currentconfirm = wtx.GetDepthInMainChain();
1133 if (conflictconfirms < currentconfirm) {
1134 // Block is 'more conflicted' than current confirm; update.
1135 // Mark transaction as conflicted with this block.
1136 wtx.nIndex = -1;
1137 wtx.hashBlock = hashBlock;
1138 wtx.MarkDirty();
1139 walletdb.WriteTx(wtx);
1140 // Iterate over all its outputs, and mark transactions in the wallet that spend them conflicted too
1141 TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(now, 0));
1142 while (iter != mapTxSpends.end() && iter->first.hash == now) {
1143 if (!done.count(iter->second)) {
1144 todo.insert(iter->second);
1146 iter++;
1148 // If a transaction changes 'conflicted' state, that changes the balance
1149 // available of the outputs it spends. So force those to be recomputed
1150 for (const CTxIn& txin : wtx.tx->vin)
1152 if (mapWallet.count(txin.prevout.hash))
1153 mapWallet[txin.prevout.hash].MarkDirty();
1159 void CWallet::SyncTransaction(const CTransactionRef& ptx, const CBlockIndex *pindex, int posInBlock) {
1160 const CTransaction& tx = *ptx;
1162 if (!AddToWalletIfInvolvingMe(ptx, pindex, posInBlock, true))
1163 return; // Not one of ours
1165 // If a transaction changes 'conflicted' state, that changes the balance
1166 // available of the outputs it spends. So force those to be
1167 // recomputed, also:
1168 for (const CTxIn& txin : tx.vin)
1170 if (mapWallet.count(txin.prevout.hash))
1171 mapWallet[txin.prevout.hash].MarkDirty();
1175 void CWallet::TransactionAddedToMempool(const CTransactionRef& ptx) {
1176 LOCK2(cs_main, cs_wallet);
1177 SyncTransaction(ptx);
1180 void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) {
1181 LOCK2(cs_main, cs_wallet);
1182 // TODO: Temporarily ensure that mempool removals are notified before
1183 // connected transactions. This shouldn't matter, but the abandoned
1184 // state of transactions in our wallet is currently cleared when we
1185 // receive another notification and there is a race condition where
1186 // notification of a connected conflict might cause an outside process
1187 // to abandon a transaction and then have it inadvertently cleared by
1188 // the notification that the conflicted transaction was evicted.
1190 for (const CTransactionRef& ptx : vtxConflicted) {
1191 SyncTransaction(ptx);
1193 for (size_t i = 0; i < pblock->vtx.size(); i++) {
1194 SyncTransaction(pblock->vtx[i], pindex, i);
1198 void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) {
1199 LOCK2(cs_main, cs_wallet);
1201 for (const CTransactionRef& ptx : pblock->vtx) {
1202 SyncTransaction(ptx);
1208 isminetype CWallet::IsMine(const CTxIn &txin) const
1211 LOCK(cs_wallet);
1212 std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
1213 if (mi != mapWallet.end())
1215 const CWalletTx& prev = (*mi).second;
1216 if (txin.prevout.n < prev.tx->vout.size())
1217 return IsMine(prev.tx->vout[txin.prevout.n]);
1220 return ISMINE_NO;
1223 // Note that this function doesn't distinguish between a 0-valued input,
1224 // and a not-"is mine" (according to the filter) input.
1225 CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
1228 LOCK(cs_wallet);
1229 std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
1230 if (mi != mapWallet.end())
1232 const CWalletTx& prev = (*mi).second;
1233 if (txin.prevout.n < prev.tx->vout.size())
1234 if (IsMine(prev.tx->vout[txin.prevout.n]) & filter)
1235 return prev.tx->vout[txin.prevout.n].nValue;
1238 return 0;
1241 isminetype CWallet::IsMine(const CTxOut& txout) const
1243 return ::IsMine(*this, txout.scriptPubKey);
1246 CAmount CWallet::GetCredit(const CTxOut& txout, const isminefilter& filter) const
1248 if (!MoneyRange(txout.nValue))
1249 throw std::runtime_error(std::string(__func__) + ": value out of range");
1250 return ((IsMine(txout) & filter) ? txout.nValue : 0);
1253 bool CWallet::IsChange(const CTxOut& txout) const
1255 // TODO: fix handling of 'change' outputs. The assumption is that any
1256 // payment to a script that is ours, but is not in the address book
1257 // is change. That assumption is likely to break when we implement multisignature
1258 // wallets that return change back into a multi-signature-protected address;
1259 // a better way of identifying which outputs are 'the send' and which are
1260 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
1261 // which output, if any, was change).
1262 if (::IsMine(*this, txout.scriptPubKey))
1264 CTxDestination address;
1265 if (!ExtractDestination(txout.scriptPubKey, address))
1266 return true;
1268 LOCK(cs_wallet);
1269 if (!mapAddressBook.count(address))
1270 return true;
1272 return false;
1275 CAmount CWallet::GetChange(const CTxOut& txout) const
1277 if (!MoneyRange(txout.nValue))
1278 throw std::runtime_error(std::string(__func__) + ": value out of range");
1279 return (IsChange(txout) ? txout.nValue : 0);
1282 bool CWallet::IsMine(const CTransaction& tx) const
1284 for (const CTxOut& txout : tx.vout)
1285 if (IsMine(txout))
1286 return true;
1287 return false;
1290 bool CWallet::IsFromMe(const CTransaction& tx) const
1292 return (GetDebit(tx, ISMINE_ALL) > 0);
1295 CAmount CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) const
1297 CAmount nDebit = 0;
1298 for (const CTxIn& txin : tx.vin)
1300 nDebit += GetDebit(txin, filter);
1301 if (!MoneyRange(nDebit))
1302 throw std::runtime_error(std::string(__func__) + ": value out of range");
1304 return nDebit;
1307 bool CWallet::IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const
1309 LOCK(cs_wallet);
1311 for (const CTxIn& txin : tx.vin)
1313 auto mi = mapWallet.find(txin.prevout.hash);
1314 if (mi == mapWallet.end())
1315 return false; // any unknown inputs can't be from us
1317 const CWalletTx& prev = (*mi).second;
1319 if (txin.prevout.n >= prev.tx->vout.size())
1320 return false; // invalid input!
1322 if (!(IsMine(prev.tx->vout[txin.prevout.n]) & filter))
1323 return false;
1325 return true;
1328 CAmount CWallet::GetCredit(const CTransaction& tx, const isminefilter& filter) const
1330 CAmount nCredit = 0;
1331 for (const CTxOut& txout : tx.vout)
1333 nCredit += GetCredit(txout, filter);
1334 if (!MoneyRange(nCredit))
1335 throw std::runtime_error(std::string(__func__) + ": value out of range");
1337 return nCredit;
1340 CAmount CWallet::GetChange(const CTransaction& tx) const
1342 CAmount nChange = 0;
1343 for (const CTxOut& txout : tx.vout)
1345 nChange += GetChange(txout);
1346 if (!MoneyRange(nChange))
1347 throw std::runtime_error(std::string(__func__) + ": value out of range");
1349 return nChange;
1352 CPubKey CWallet::GenerateNewHDMasterKey()
1354 CKey key;
1355 key.MakeNewKey(true);
1357 int64_t nCreationTime = GetTime();
1358 CKeyMetadata metadata(nCreationTime);
1360 // calculate the pubkey
1361 CPubKey pubkey = key.GetPubKey();
1362 assert(key.VerifyPubKey(pubkey));
1364 // set the hd keypath to "m" -> Master, refers the masterkeyid to itself
1365 metadata.hdKeypath = "m";
1366 metadata.hdMasterKeyID = pubkey.GetID();
1369 LOCK(cs_wallet);
1371 // mem store the metadata
1372 mapKeyMetadata[pubkey.GetID()] = metadata;
1374 // write the key&metadata to the database
1375 if (!AddKeyPubKey(key, pubkey))
1376 throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed");
1379 return pubkey;
1382 bool CWallet::SetHDMasterKey(const CPubKey& pubkey)
1384 LOCK(cs_wallet);
1385 // store the keyid (hash160) together with
1386 // the child index counter in the database
1387 // as a hdchain object
1388 CHDChain newHdChain;
1389 newHdChain.nVersion = CanSupportFeature(FEATURE_HD_SPLIT) ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE;
1390 newHdChain.masterKeyID = pubkey.GetID();
1391 SetHDChain(newHdChain, false);
1393 return true;
1396 bool CWallet::SetHDChain(const CHDChain& chain, bool memonly)
1398 LOCK(cs_wallet);
1399 if (!memonly && !CWalletDB(*dbw).WriteHDChain(chain))
1400 throw std::runtime_error(std::string(__func__) + ": writing chain failed");
1402 hdChain = chain;
1403 return true;
1406 bool CWallet::IsHDEnabled() const
1408 return !hdChain.masterKeyID.IsNull();
1411 int64_t CWalletTx::GetTxTime() const
1413 int64_t n = nTimeSmart;
1414 return n ? n : nTimeReceived;
1417 int CWalletTx::GetRequestCount() const
1419 // Returns -1 if it wasn't being tracked
1420 int nRequests = -1;
1422 LOCK(pwallet->cs_wallet);
1423 if (IsCoinBase())
1425 // Generated block
1426 if (!hashUnset())
1428 std::map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
1429 if (mi != pwallet->mapRequestCount.end())
1430 nRequests = (*mi).second;
1433 else
1435 // Did anyone request this transaction?
1436 std::map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
1437 if (mi != pwallet->mapRequestCount.end())
1439 nRequests = (*mi).second;
1441 // How about the block it's in?
1442 if (nRequests == 0 && !hashUnset())
1444 std::map<uint256, int>::const_iterator _mi = pwallet->mapRequestCount.find(hashBlock);
1445 if (_mi != pwallet->mapRequestCount.end())
1446 nRequests = (*_mi).second;
1447 else
1448 nRequests = 1; // If it's in someone else's block it must have got out
1453 return nRequests;
1456 void CWalletTx::GetAmounts(std::list<COutputEntry>& listReceived,
1457 std::list<COutputEntry>& listSent, CAmount& nFee, std::string& strSentAccount, const isminefilter& filter) const
1459 nFee = 0;
1460 listReceived.clear();
1461 listSent.clear();
1462 strSentAccount = strFromAccount;
1464 // Compute fee:
1465 CAmount nDebit = GetDebit(filter);
1466 if (nDebit > 0) // debit>0 means we signed/sent this transaction
1468 CAmount nValueOut = tx->GetValueOut();
1469 nFee = nDebit - nValueOut;
1472 // Sent/received.
1473 for (unsigned int i = 0; i < tx->vout.size(); ++i)
1475 const CTxOut& txout = tx->vout[i];
1476 isminetype fIsMine = pwallet->IsMine(txout);
1477 // Only need to handle txouts if AT LEAST one of these is true:
1478 // 1) they debit from us (sent)
1479 // 2) the output is to us (received)
1480 if (nDebit > 0)
1482 // Don't report 'change' txouts
1483 if (pwallet->IsChange(txout))
1484 continue;
1486 else if (!(fIsMine & filter))
1487 continue;
1489 // In either case, we need to get the destination address
1490 CTxDestination address;
1492 if (!ExtractDestination(txout.scriptPubKey, address) && !txout.scriptPubKey.IsUnspendable())
1494 LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
1495 this->GetHash().ToString());
1496 address = CNoDestination();
1499 COutputEntry output = {address, txout.nValue, (int)i};
1501 // If we are debited by the transaction, add the output as a "sent" entry
1502 if (nDebit > 0)
1503 listSent.push_back(output);
1505 // If we are receiving the output, add it as a "received" entry
1506 if (fIsMine & filter)
1507 listReceived.push_back(output);
1513 * Scan active chain for relevant transactions after importing keys. This should
1514 * be called whenever new keys are added to the wallet, with the oldest key
1515 * creation time.
1517 * @return Earliest timestamp that could be successfully scanned from. Timestamp
1518 * returned will be higher than startTime if relevant blocks could not be read.
1520 int64_t CWallet::RescanFromTime(int64_t startTime, bool update)
1522 AssertLockHeld(cs_main);
1523 AssertLockHeld(cs_wallet);
1525 // Find starting block. May be null if nCreateTime is greater than the
1526 // highest blockchain timestamp, in which case there is nothing that needs
1527 // to be scanned.
1528 CBlockIndex* const startBlock = chainActive.FindEarliestAtLeast(startTime - TIMESTAMP_WINDOW);
1529 LogPrintf("%s: Rescanning last %i blocks\n", __func__, startBlock ? chainActive.Height() - startBlock->nHeight + 1 : 0);
1531 if (startBlock) {
1532 const CBlockIndex* const failedBlock = ScanForWalletTransactions(startBlock, update);
1533 if (failedBlock) {
1534 return failedBlock->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1;
1537 return startTime;
1541 * Scan the block chain (starting in pindexStart) for transactions
1542 * from or to us. If fUpdate is true, found transactions that already
1543 * exist in the wallet will be updated.
1545 * Returns null if scan was successful. Otherwise, if a complete rescan was not
1546 * possible (due to pruning or corruption), returns pointer to the most recent
1547 * block that could not be scanned.
1549 CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
1551 int64_t nNow = GetTime();
1552 const CChainParams& chainParams = Params();
1554 CBlockIndex* pindex = pindexStart;
1555 CBlockIndex* ret = nullptr;
1557 LOCK2(cs_main, cs_wallet);
1558 fAbortRescan = false;
1559 fScanningWallet = true;
1561 ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
1562 double dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
1563 double dProgressTip = GuessVerificationProgress(chainParams.TxData(), chainActive.Tip());
1564 while (pindex && !fAbortRescan)
1566 if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
1567 ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((GuessVerificationProgress(chainParams.TxData(), pindex) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
1568 if (GetTime() >= nNow + 60) {
1569 nNow = GetTime();
1570 LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
1573 CBlock block;
1574 if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
1575 for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
1576 AddToWalletIfInvolvingMe(block.vtx[posInBlock], pindex, posInBlock, fUpdate);
1578 } else {
1579 ret = pindex;
1581 pindex = chainActive.Next(pindex);
1583 if (pindex && fAbortRescan) {
1584 LogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
1586 ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
1588 fScanningWallet = false;
1590 return ret;
1593 void CWallet::ReacceptWalletTransactions()
1595 // If transactions aren't being broadcasted, don't let them into local mempool either
1596 if (!fBroadcastTransactions)
1597 return;
1598 LOCK2(cs_main, cs_wallet);
1599 std::map<int64_t, CWalletTx*> mapSorted;
1601 // Sort pending wallet transactions based on their initial wallet insertion order
1602 for (std::pair<const uint256, CWalletTx>& item : mapWallet)
1604 const uint256& wtxid = item.first;
1605 CWalletTx& wtx = item.second;
1606 assert(wtx.GetHash() == wtxid);
1608 int nDepth = wtx.GetDepthInMainChain();
1610 if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
1611 mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
1615 // Try to add wallet transactions to memory pool
1616 for (std::pair<const int64_t, CWalletTx*>& item : mapSorted)
1618 CWalletTx& wtx = *(item.second);
1620 LOCK(mempool.cs);
1621 CValidationState state;
1622 wtx.AcceptToMemoryPool(maxTxFee, state);
1626 bool CWalletTx::RelayWalletTransaction(CConnman* connman)
1628 assert(pwallet->GetBroadcastTransactions());
1629 if (!IsCoinBase() && !isAbandoned() && GetDepthInMainChain() == 0)
1631 CValidationState state;
1632 /* GetDepthInMainChain already catches known conflicts. */
1633 if (InMempool() || AcceptToMemoryPool(maxTxFee, state)) {
1634 LogPrintf("Relaying wtx %s\n", GetHash().ToString());
1635 if (connman) {
1636 CInv inv(MSG_TX, GetHash());
1637 connman->ForEachNode([&inv](CNode* pnode)
1639 pnode->PushInventory(inv);
1641 return true;
1645 return false;
1648 std::set<uint256> CWalletTx::GetConflicts() const
1650 std::set<uint256> result;
1651 if (pwallet != NULL)
1653 uint256 myHash = GetHash();
1654 result = pwallet->GetConflicts(myHash);
1655 result.erase(myHash);
1657 return result;
1660 CAmount CWalletTx::GetDebit(const isminefilter& filter) const
1662 if (tx->vin.empty())
1663 return 0;
1665 CAmount debit = 0;
1666 if(filter & ISMINE_SPENDABLE)
1668 if (fDebitCached)
1669 debit += nDebitCached;
1670 else
1672 nDebitCached = pwallet->GetDebit(*this, ISMINE_SPENDABLE);
1673 fDebitCached = true;
1674 debit += nDebitCached;
1677 if(filter & ISMINE_WATCH_ONLY)
1679 if(fWatchDebitCached)
1680 debit += nWatchDebitCached;
1681 else
1683 nWatchDebitCached = pwallet->GetDebit(*this, ISMINE_WATCH_ONLY);
1684 fWatchDebitCached = true;
1685 debit += nWatchDebitCached;
1688 return debit;
1691 CAmount CWalletTx::GetCredit(const isminefilter& filter) const
1693 // Must wait until coinbase is safely deep enough in the chain before valuing it
1694 if (IsCoinBase() && GetBlocksToMaturity() > 0)
1695 return 0;
1697 CAmount credit = 0;
1698 if (filter & ISMINE_SPENDABLE)
1700 // GetBalance can assume transactions in mapWallet won't change
1701 if (fCreditCached)
1702 credit += nCreditCached;
1703 else
1705 nCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE);
1706 fCreditCached = true;
1707 credit += nCreditCached;
1710 if (filter & ISMINE_WATCH_ONLY)
1712 if (fWatchCreditCached)
1713 credit += nWatchCreditCached;
1714 else
1716 nWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY);
1717 fWatchCreditCached = true;
1718 credit += nWatchCreditCached;
1721 return credit;
1724 CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
1726 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
1728 if (fUseCache && fImmatureCreditCached)
1729 return nImmatureCreditCached;
1730 nImmatureCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE);
1731 fImmatureCreditCached = true;
1732 return nImmatureCreditCached;
1735 return 0;
1738 CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
1740 if (pwallet == 0)
1741 return 0;
1743 // Must wait until coinbase is safely deep enough in the chain before valuing it
1744 if (IsCoinBase() && GetBlocksToMaturity() > 0)
1745 return 0;
1747 if (fUseCache && fAvailableCreditCached)
1748 return nAvailableCreditCached;
1750 CAmount nCredit = 0;
1751 uint256 hashTx = GetHash();
1752 for (unsigned int i = 0; i < tx->vout.size(); i++)
1754 if (!pwallet->IsSpent(hashTx, i))
1756 const CTxOut &txout = tx->vout[i];
1757 nCredit += pwallet->GetCredit(txout, ISMINE_SPENDABLE);
1758 if (!MoneyRange(nCredit))
1759 throw std::runtime_error(std::string(__func__) + " : value out of range");
1763 nAvailableCreditCached = nCredit;
1764 fAvailableCreditCached = true;
1765 return nCredit;
1768 CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool& fUseCache) const
1770 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
1772 if (fUseCache && fImmatureWatchCreditCached)
1773 return nImmatureWatchCreditCached;
1774 nImmatureWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY);
1775 fImmatureWatchCreditCached = true;
1776 return nImmatureWatchCreditCached;
1779 return 0;
1782 CAmount CWalletTx::GetAvailableWatchOnlyCredit(const bool& fUseCache) const
1784 if (pwallet == 0)
1785 return 0;
1787 // Must wait until coinbase is safely deep enough in the chain before valuing it
1788 if (IsCoinBase() && GetBlocksToMaturity() > 0)
1789 return 0;
1791 if (fUseCache && fAvailableWatchCreditCached)
1792 return nAvailableWatchCreditCached;
1794 CAmount nCredit = 0;
1795 for (unsigned int i = 0; i < tx->vout.size(); i++)
1797 if (!pwallet->IsSpent(GetHash(), i))
1799 const CTxOut &txout = tx->vout[i];
1800 nCredit += pwallet->GetCredit(txout, ISMINE_WATCH_ONLY);
1801 if (!MoneyRange(nCredit))
1802 throw std::runtime_error(std::string(__func__) + ": value out of range");
1806 nAvailableWatchCreditCached = nCredit;
1807 fAvailableWatchCreditCached = true;
1808 return nCredit;
1811 CAmount CWalletTx::GetChange() const
1813 if (fChangeCached)
1814 return nChangeCached;
1815 nChangeCached = pwallet->GetChange(*this);
1816 fChangeCached = true;
1817 return nChangeCached;
1820 bool CWalletTx::InMempool() const
1822 LOCK(mempool.cs);
1823 return mempool.exists(GetHash());
1826 bool CWalletTx::IsTrusted() const
1828 // Quick answer in most cases
1829 if (!CheckFinalTx(*this))
1830 return false;
1831 int nDepth = GetDepthInMainChain();
1832 if (nDepth >= 1)
1833 return true;
1834 if (nDepth < 0)
1835 return false;
1836 if (!bSpendZeroConfChange || !IsFromMe(ISMINE_ALL)) // using wtx's cached debit
1837 return false;
1839 // Don't trust unconfirmed transactions from us unless they are in the mempool.
1840 if (!InMempool())
1841 return false;
1843 // Trusted if all inputs are from us and are in the mempool:
1844 for (const CTxIn& txin : tx->vin)
1846 // Transactions not sent by us: not trusted
1847 const CWalletTx* parent = pwallet->GetWalletTx(txin.prevout.hash);
1848 if (parent == NULL)
1849 return false;
1850 const CTxOut& parentOut = parent->tx->vout[txin.prevout.n];
1851 if (pwallet->IsMine(parentOut) != ISMINE_SPENDABLE)
1852 return false;
1854 return true;
1857 bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
1859 CMutableTransaction tx1 = *this->tx;
1860 CMutableTransaction tx2 = *_tx.tx;
1861 for (auto& txin : tx1.vin) txin.scriptSig = CScript();
1862 for (auto& txin : tx2.vin) txin.scriptSig = CScript();
1863 return CTransaction(tx1) == CTransaction(tx2);
1866 std::vector<uint256> CWallet::ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman)
1868 std::vector<uint256> result;
1870 LOCK(cs_wallet);
1871 // Sort them in chronological order
1872 std::multimap<unsigned int, CWalletTx*> mapSorted;
1873 for (std::pair<const uint256, CWalletTx>& item : mapWallet)
1875 CWalletTx& wtx = item.second;
1876 // Don't rebroadcast if newer than nTime:
1877 if (wtx.nTimeReceived > nTime)
1878 continue;
1879 mapSorted.insert(std::make_pair(wtx.nTimeReceived, &wtx));
1881 for (std::pair<const unsigned int, CWalletTx*>& item : mapSorted)
1883 CWalletTx& wtx = *item.second;
1884 if (wtx.RelayWalletTransaction(connman))
1885 result.push_back(wtx.GetHash());
1887 return result;
1890 void CWallet::ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman)
1892 // Do this infrequently and randomly to avoid giving away
1893 // that these are our transactions.
1894 if (GetTime() < nNextResend || !fBroadcastTransactions)
1895 return;
1896 bool fFirst = (nNextResend == 0);
1897 nNextResend = GetTime() + GetRand(30 * 60);
1898 if (fFirst)
1899 return;
1901 // Only do it if there's been a new block since last time
1902 if (nBestBlockTime < nLastResend)
1903 return;
1904 nLastResend = GetTime();
1906 // Rebroadcast unconfirmed txes older than 5 minutes before the last
1907 // block was found:
1908 std::vector<uint256> relayed = ResendWalletTransactionsBefore(nBestBlockTime-5*60, connman);
1909 if (!relayed.empty())
1910 LogPrintf("%s: rebroadcast %u unconfirmed transactions\n", __func__, relayed.size());
1913 /** @} */ // end of mapWallet
1918 /** @defgroup Actions
1920 * @{
1924 CAmount CWallet::GetBalance() const
1926 CAmount nTotal = 0;
1928 LOCK2(cs_main, cs_wallet);
1929 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1931 const CWalletTx* pcoin = &(*it).second;
1932 if (pcoin->IsTrusted())
1933 nTotal += pcoin->GetAvailableCredit();
1937 return nTotal;
1940 CAmount CWallet::GetUnconfirmedBalance() const
1942 CAmount nTotal = 0;
1944 LOCK2(cs_main, cs_wallet);
1945 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1947 const CWalletTx* pcoin = &(*it).second;
1948 if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
1949 nTotal += pcoin->GetAvailableCredit();
1952 return nTotal;
1955 CAmount CWallet::GetImmatureBalance() const
1957 CAmount nTotal = 0;
1959 LOCK2(cs_main, cs_wallet);
1960 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1962 const CWalletTx* pcoin = &(*it).second;
1963 nTotal += pcoin->GetImmatureCredit();
1966 return nTotal;
1969 CAmount CWallet::GetWatchOnlyBalance() const
1971 CAmount nTotal = 0;
1973 LOCK2(cs_main, cs_wallet);
1974 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1976 const CWalletTx* pcoin = &(*it).second;
1977 if (pcoin->IsTrusted())
1978 nTotal += pcoin->GetAvailableWatchOnlyCredit();
1982 return nTotal;
1985 CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const
1987 CAmount nTotal = 0;
1989 LOCK2(cs_main, cs_wallet);
1990 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1992 const CWalletTx* pcoin = &(*it).second;
1993 if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
1994 nTotal += pcoin->GetAvailableWatchOnlyCredit();
1997 return nTotal;
2000 CAmount CWallet::GetImmatureWatchOnlyBalance() const
2002 CAmount nTotal = 0;
2004 LOCK2(cs_main, cs_wallet);
2005 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2007 const CWalletTx* pcoin = &(*it).second;
2008 nTotal += pcoin->GetImmatureWatchOnlyCredit();
2011 return nTotal;
2014 // Calculate total balance in a different way from GetBalance. The biggest
2015 // difference is that GetBalance sums up all unspent TxOuts paying to the
2016 // wallet, while this sums up both spent and unspent TxOuts paying to the
2017 // wallet, and then subtracts the values of TxIns spending from the wallet. This
2018 // also has fewer restrictions on which unconfirmed transactions are considered
2019 // trusted.
2020 CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth, const std::string* account) const
2022 LOCK2(cs_main, cs_wallet);
2024 CAmount balance = 0;
2025 for (const auto& entry : mapWallet) {
2026 const CWalletTx& wtx = entry.second;
2027 const int depth = wtx.GetDepthInMainChain();
2028 if (depth < 0 || !CheckFinalTx(*wtx.tx) || wtx.GetBlocksToMaturity() > 0) {
2029 continue;
2032 // Loop through tx outputs and add incoming payments. For outgoing txs,
2033 // treat change outputs specially, as part of the amount debited.
2034 CAmount debit = wtx.GetDebit(filter);
2035 const bool outgoing = debit > 0;
2036 for (const CTxOut& out : wtx.tx->vout) {
2037 if (outgoing && IsChange(out)) {
2038 debit -= out.nValue;
2039 } else if (IsMine(out) & filter && depth >= minDepth && (!account || *account == GetAccountName(out.scriptPubKey))) {
2040 balance += out.nValue;
2044 // For outgoing txs, subtract amount debited.
2045 if (outgoing && (!account || *account == wtx.strFromAccount)) {
2046 balance -= debit;
2050 if (account) {
2051 balance += CWalletDB(*dbw).GetAccountCreditDebit(*account);
2054 return balance;
2057 CAmount CWallet::GetAvailableBalance(const CCoinControl* coinControl) const
2059 LOCK2(cs_main, cs_wallet);
2061 CAmount balance = 0;
2062 std::vector<COutput> vCoins;
2063 AvailableCoins(vCoins, true, coinControl);
2064 for (const COutput& out : vCoins) {
2065 if (out.fSpendable) {
2066 balance += out.tx->tx->vout[out.i].nValue;
2069 return balance;
2072 void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const CCoinControl *coinControl, const CAmount &nMinimumAmount, const CAmount &nMaximumAmount, const CAmount &nMinimumSumAmount, const uint64_t &nMaximumCount, const int &nMinDepth, const int &nMaxDepth) const
2074 vCoins.clear();
2077 LOCK2(cs_main, cs_wallet);
2079 CAmount nTotal = 0;
2081 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
2083 const uint256& wtxid = it->first;
2084 const CWalletTx* pcoin = &(*it).second;
2086 if (!CheckFinalTx(*pcoin))
2087 continue;
2089 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
2090 continue;
2092 int nDepth = pcoin->GetDepthInMainChain();
2093 if (nDepth < 0)
2094 continue;
2096 // We should not consider coins which aren't at least in our mempool
2097 // It's possible for these to be conflicted via ancestors which we may never be able to detect
2098 if (nDepth == 0 && !pcoin->InMempool())
2099 continue;
2101 bool safeTx = pcoin->IsTrusted();
2103 // We should not consider coins from transactions that are replacing
2104 // other transactions.
2106 // Example: There is a transaction A which is replaced by bumpfee
2107 // transaction B. In this case, we want to prevent creation of
2108 // a transaction B' which spends an output of B.
2110 // Reason: If transaction A were initially confirmed, transactions B
2111 // and B' would no longer be valid, so the user would have to create
2112 // a new transaction C to replace B'. However, in the case of a
2113 // one-block reorg, transactions B' and C might BOTH be accepted,
2114 // when the user only wanted one of them. Specifically, there could
2115 // be a 1-block reorg away from the chain where transactions A and C
2116 // were accepted to another chain where B, B', and C were all
2117 // accepted.
2118 if (nDepth == 0 && pcoin->mapValue.count("replaces_txid")) {
2119 safeTx = false;
2122 // Similarly, we should not consider coins from transactions that
2123 // have been replaced. In the example above, we would want to prevent
2124 // creation of a transaction A' spending an output of A, because if
2125 // transaction B were initially confirmed, conflicting with A and
2126 // A', we wouldn't want to the user to create a transaction D
2127 // intending to replace A', but potentially resulting in a scenario
2128 // where A, A', and D could all be accepted (instead of just B and
2129 // D, or just A and A' like the user would want).
2130 if (nDepth == 0 && pcoin->mapValue.count("replaced_by_txid")) {
2131 safeTx = false;
2134 if (fOnlySafe && !safeTx) {
2135 continue;
2138 if (nDepth < nMinDepth || nDepth > nMaxDepth)
2139 continue;
2141 for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) {
2142 if (pcoin->tx->vout[i].nValue < nMinimumAmount || pcoin->tx->vout[i].nValue > nMaximumAmount)
2143 continue;
2145 if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(COutPoint((*it).first, i)))
2146 continue;
2148 if (IsLockedCoin((*it).first, i))
2149 continue;
2151 if (IsSpent(wtxid, i))
2152 continue;
2154 isminetype mine = IsMine(pcoin->tx->vout[i]);
2156 if (mine == ISMINE_NO) {
2157 continue;
2160 bool fSpendableIn = ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (coinControl && coinControl->fAllowWatchOnly && (mine & ISMINE_WATCH_SOLVABLE) != ISMINE_NO);
2161 bool fSolvableIn = (mine & (ISMINE_SPENDABLE | ISMINE_WATCH_SOLVABLE)) != ISMINE_NO;
2163 vCoins.push_back(COutput(pcoin, i, nDepth, fSpendableIn, fSolvableIn, safeTx));
2165 // Checks the sum amount of all UTXO's.
2166 if (nMinimumSumAmount != MAX_MONEY) {
2167 nTotal += pcoin->tx->vout[i].nValue;
2169 if (nTotal >= nMinimumSumAmount) {
2170 return;
2174 // Checks the maximum number of UTXO's.
2175 if (nMaximumCount > 0 && vCoins.size() >= nMaximumCount) {
2176 return;
2183 std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const
2185 // TODO: Add AssertLockHeld(cs_wallet) here.
2187 // Because the return value from this function contains pointers to
2188 // CWalletTx objects, callers to this function really should acquire the
2189 // cs_wallet lock before calling it. However, the current caller doesn't
2190 // acquire this lock yet. There was an attempt to add the missing lock in
2191 // https://github.com/bitcoin/bitcoin/pull/10340, but that change has been
2192 // postponed until after https://github.com/bitcoin/bitcoin/pull/10244 to
2193 // avoid adding some extra complexity to the Qt code.
2195 std::map<CTxDestination, std::vector<COutput>> result;
2197 std::vector<COutput> availableCoins;
2198 AvailableCoins(availableCoins);
2200 LOCK2(cs_main, cs_wallet);
2201 for (auto& coin : availableCoins) {
2202 CTxDestination address;
2203 if (coin.fSpendable &&
2204 ExtractDestination(FindNonChangeParentOutput(*coin.tx->tx, coin.i).scriptPubKey, address)) {
2205 result[address].emplace_back(std::move(coin));
2209 std::vector<COutPoint> lockedCoins;
2210 ListLockedCoins(lockedCoins);
2211 for (const auto& output : lockedCoins) {
2212 auto it = mapWallet.find(output.hash);
2213 if (it != mapWallet.end()) {
2214 int depth = it->second.GetDepthInMainChain();
2215 if (depth >= 0 && output.n < it->second.tx->vout.size() &&
2216 IsMine(it->second.tx->vout[output.n]) == ISMINE_SPENDABLE) {
2217 CTxDestination address;
2218 if (ExtractDestination(FindNonChangeParentOutput(*it->second.tx, output.n).scriptPubKey, address)) {
2219 result[address].emplace_back(
2220 &it->second, output.n, depth, true /* spendable */, true /* solvable */, false /* safe */);
2226 return result;
2229 const CTxOut& CWallet::FindNonChangeParentOutput(const CTransaction& tx, int output) const
2231 const CTransaction* ptx = &tx;
2232 int n = output;
2233 while (IsChange(ptx->vout[n]) && ptx->vin.size() > 0) {
2234 const COutPoint& prevout = ptx->vin[0].prevout;
2235 auto it = mapWallet.find(prevout.hash);
2236 if (it == mapWallet.end() || it->second.tx->vout.size() <= prevout.n ||
2237 !IsMine(it->second.tx->vout[prevout.n])) {
2238 break;
2240 ptx = it->second.tx.get();
2241 n = prevout.n;
2243 return ptx->vout[n];
2246 static void ApproximateBestSubset(const std::vector<CInputCoin>& vValue, const CAmount& nTotalLower, const CAmount& nTargetValue,
2247 std::vector<char>& vfBest, CAmount& nBest, int iterations = 1000)
2249 std::vector<char> vfIncluded;
2251 vfBest.assign(vValue.size(), true);
2252 nBest = nTotalLower;
2254 FastRandomContext insecure_rand;
2256 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
2258 vfIncluded.assign(vValue.size(), false);
2259 CAmount nTotal = 0;
2260 bool fReachedTarget = false;
2261 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
2263 for (unsigned int i = 0; i < vValue.size(); i++)
2265 //The solver here uses a randomized algorithm,
2266 //the randomness serves no real security purpose but is just
2267 //needed to prevent degenerate behavior and it is important
2268 //that the rng is fast. We do not use a constant random sequence,
2269 //because there may be some privacy improvement by making
2270 //the selection random.
2271 if (nPass == 0 ? insecure_rand.randbool() : !vfIncluded[i])
2273 nTotal += vValue[i].txout.nValue;
2274 vfIncluded[i] = true;
2275 if (nTotal >= nTargetValue)
2277 fReachedTarget = true;
2278 if (nTotal < nBest)
2280 nBest = nTotal;
2281 vfBest = vfIncluded;
2283 nTotal -= vValue[i].txout.nValue;
2284 vfIncluded[i] = false;
2292 bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMine, const int nConfTheirs, const uint64_t nMaxAncestors, std::vector<COutput> vCoins,
2293 std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet) const
2295 setCoinsRet.clear();
2296 nValueRet = 0;
2298 // List of values less than target
2299 boost::optional<CInputCoin> coinLowestLarger;
2300 std::vector<CInputCoin> vValue;
2301 CAmount nTotalLower = 0;
2303 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
2305 for (const COutput &output : vCoins)
2307 if (!output.fSpendable)
2308 continue;
2310 const CWalletTx *pcoin = output.tx;
2312 if (output.nDepth < (pcoin->IsFromMe(ISMINE_ALL) ? nConfMine : nConfTheirs))
2313 continue;
2315 if (!mempool.TransactionWithinChainLimit(pcoin->GetHash(), nMaxAncestors))
2316 continue;
2318 int i = output.i;
2320 CInputCoin coin = CInputCoin(pcoin, i);
2322 if (coin.txout.nValue == nTargetValue)
2324 setCoinsRet.insert(coin);
2325 nValueRet += coin.txout.nValue;
2326 return true;
2328 else if (coin.txout.nValue < nTargetValue + MIN_CHANGE)
2330 vValue.push_back(coin);
2331 nTotalLower += coin.txout.nValue;
2333 else if (!coinLowestLarger || coin.txout.nValue < coinLowestLarger->txout.nValue)
2335 coinLowestLarger = coin;
2339 if (nTotalLower == nTargetValue)
2341 for (const auto& input : vValue)
2343 setCoinsRet.insert(input);
2344 nValueRet += input.txout.nValue;
2346 return true;
2349 if (nTotalLower < nTargetValue)
2351 if (!coinLowestLarger)
2352 return false;
2353 setCoinsRet.insert(coinLowestLarger.get());
2354 nValueRet += coinLowestLarger->txout.nValue;
2355 return true;
2358 // Solve subset sum by stochastic approximation
2359 std::sort(vValue.begin(), vValue.end(), CompareValueOnly());
2360 std::reverse(vValue.begin(), vValue.end());
2361 std::vector<char> vfBest;
2362 CAmount nBest;
2364 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest);
2365 if (nBest != nTargetValue && nTotalLower >= nTargetValue + MIN_CHANGE)
2366 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + MIN_CHANGE, vfBest, nBest);
2368 // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
2369 // or the next bigger coin is closer), return the bigger coin
2370 if (coinLowestLarger &&
2371 ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger->txout.nValue <= nBest))
2373 setCoinsRet.insert(coinLowestLarger.get());
2374 nValueRet += coinLowestLarger->txout.nValue;
2376 else {
2377 for (unsigned int i = 0; i < vValue.size(); i++)
2378 if (vfBest[i])
2380 setCoinsRet.insert(vValue[i]);
2381 nValueRet += vValue[i].txout.nValue;
2384 if (LogAcceptCategory(BCLog::SELECTCOINS)) {
2385 LogPrint(BCLog::SELECTCOINS, "SelectCoins() best subset: ");
2386 for (unsigned int i = 0; i < vValue.size(); i++) {
2387 if (vfBest[i]) {
2388 LogPrint(BCLog::SELECTCOINS, "%s ", FormatMoney(vValue[i].txout.nValue));
2391 LogPrint(BCLog::SELECTCOINS, "total %s\n", FormatMoney(nBest));
2395 return true;
2398 bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl) const
2400 std::vector<COutput> vCoins(vAvailableCoins);
2402 // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
2403 if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs)
2405 for (const COutput& out : vCoins)
2407 if (!out.fSpendable)
2408 continue;
2409 nValueRet += out.tx->tx->vout[out.i].nValue;
2410 setCoinsRet.insert(CInputCoin(out.tx, out.i));
2412 return (nValueRet >= nTargetValue);
2415 // calculate value from preset inputs and store them
2416 std::set<CInputCoin> setPresetCoins;
2417 CAmount nValueFromPresetInputs = 0;
2419 std::vector<COutPoint> vPresetInputs;
2420 if (coinControl)
2421 coinControl->ListSelected(vPresetInputs);
2422 for (const COutPoint& outpoint : vPresetInputs)
2424 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(outpoint.hash);
2425 if (it != mapWallet.end())
2427 const CWalletTx* pcoin = &it->second;
2428 // Clearly invalid input, fail
2429 if (pcoin->tx->vout.size() <= outpoint.n)
2430 return false;
2431 nValueFromPresetInputs += pcoin->tx->vout[outpoint.n].nValue;
2432 setPresetCoins.insert(CInputCoin(pcoin, outpoint.n));
2433 } else
2434 return false; // TODO: Allow non-wallet inputs
2437 // remove preset inputs from vCoins
2438 for (std::vector<COutput>::iterator it = vCoins.begin(); it != vCoins.end() && coinControl && coinControl->HasSelected();)
2440 if (setPresetCoins.count(CInputCoin(it->tx, it->i)))
2441 it = vCoins.erase(it);
2442 else
2443 ++it;
2446 size_t nMaxChainLength = std::min(GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT), GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT));
2447 bool fRejectLongChains = GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS);
2449 bool res = nTargetValue <= nValueFromPresetInputs ||
2450 SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 1, 6, 0, vCoins, setCoinsRet, nValueRet) ||
2451 SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 1, 1, 0, vCoins, setCoinsRet, nValueRet) ||
2452 (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, 2, vCoins, setCoinsRet, nValueRet)) ||
2453 (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, std::min((size_t)4, nMaxChainLength/3), vCoins, setCoinsRet, nValueRet)) ||
2454 (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, nMaxChainLength/2, vCoins, setCoinsRet, nValueRet)) ||
2455 (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, nMaxChainLength, vCoins, setCoinsRet, nValueRet)) ||
2456 (bSpendZeroConfChange && !fRejectLongChains && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, std::numeric_limits<uint64_t>::max(), vCoins, setCoinsRet, nValueRet));
2458 // because SelectCoinsMinConf clears the setCoinsRet, we now add the possible inputs to the coinset
2459 setCoinsRet.insert(setPresetCoins.begin(), setPresetCoins.end());
2461 // add preset inputs to the total value selected
2462 nValueRet += nValueFromPresetInputs;
2464 return res;
2467 bool CWallet::SignTransaction(CMutableTransaction &tx)
2469 AssertLockHeld(cs_wallet); // mapWallet
2471 // sign the new tx
2472 CTransaction txNewConst(tx);
2473 int nIn = 0;
2474 for (const auto& input : tx.vin) {
2475 std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(input.prevout.hash);
2476 if(mi == mapWallet.end() || input.prevout.n >= mi->second.tx->vout.size()) {
2477 return false;
2479 const CScript& scriptPubKey = mi->second.tx->vout[input.prevout.n].scriptPubKey;
2480 const CAmount& amount = mi->second.tx->vout[input.prevout.n].nValue;
2481 SignatureData sigdata;
2482 if (!ProduceSignature(TransactionSignatureCreator(this, &txNewConst, nIn, amount, SIGHASH_ALL), scriptPubKey, sigdata)) {
2483 return false;
2485 UpdateTransaction(tx, nIn, sigdata);
2486 nIn++;
2488 return true;
2491 bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl coinControl)
2493 std::vector<CRecipient> vecSend;
2495 // Turn the txout set into a CRecipient vector
2496 for (size_t idx = 0; idx < tx.vout.size(); idx++)
2498 const CTxOut& txOut = tx.vout[idx];
2499 CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, setSubtractFeeFromOutputs.count(idx) == 1};
2500 vecSend.push_back(recipient);
2503 coinControl.fAllowOtherInputs = true;
2505 for (const CTxIn& txin : tx.vin)
2506 coinControl.Select(txin.prevout);
2508 CReserveKey reservekey(this);
2509 CWalletTx wtx;
2510 if (!CreateTransaction(vecSend, wtx, reservekey, nFeeRet, nChangePosInOut, strFailReason, coinControl, false)) {
2511 return false;
2514 if (nChangePosInOut != -1) {
2515 tx.vout.insert(tx.vout.begin() + nChangePosInOut, wtx.tx->vout[nChangePosInOut]);
2516 // we dont have the normal Create/Commit cycle, and dont want to risk reusing change,
2517 // so just remove the key from the keypool here.
2518 reservekey.KeepKey();
2521 // Copy output sizes from new transaction; they may have had the fee subtracted from them
2522 for (unsigned int idx = 0; idx < tx.vout.size(); idx++)
2523 tx.vout[idx].nValue = wtx.tx->vout[idx].nValue;
2525 // Add new txins (keeping original txin scriptSig/order)
2526 for (const CTxIn& txin : wtx.tx->vin)
2528 if (!coinControl.IsSelected(txin.prevout))
2530 tx.vin.push_back(txin);
2532 if (lockUnspents)
2534 LOCK2(cs_main, cs_wallet);
2535 LockCoin(txin.prevout);
2541 return true;
2544 static CFeeRate GetDiscardRate(const CBlockPolicyEstimator& estimator)
2546 unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
2547 CFeeRate discard_rate = estimator.estimateSmartFee(highest_target, nullptr /* FeeCalculation */, false /* conservative */);
2548 // Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
2549 discard_rate = (discard_rate == CFeeRate(0)) ? CWallet::m_discard_rate : std::min(discard_rate, CWallet::m_discard_rate);
2550 // Discard rate must be at least dustRelayFee
2551 discard_rate = std::max(discard_rate, ::dustRelayFee);
2552 return discard_rate;
2555 bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet,
2556 int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign)
2558 CAmount nValue = 0;
2559 int nChangePosRequest = nChangePosInOut;
2560 unsigned int nSubtractFeeFromAmount = 0;
2561 for (const auto& recipient : vecSend)
2563 if (nValue < 0 || recipient.nAmount < 0)
2565 strFailReason = _("Transaction amounts must not be negative");
2566 return false;
2568 nValue += recipient.nAmount;
2570 if (recipient.fSubtractFeeFromAmount)
2571 nSubtractFeeFromAmount++;
2573 if (vecSend.empty())
2575 strFailReason = _("Transaction must have at least one recipient");
2576 return false;
2579 wtxNew.fTimeReceivedIsTxTime = true;
2580 wtxNew.BindWallet(this);
2581 CMutableTransaction txNew;
2583 // Discourage fee sniping.
2585 // For a large miner the value of the transactions in the best block and
2586 // the mempool can exceed the cost of deliberately attempting to mine two
2587 // blocks to orphan the current best block. By setting nLockTime such that
2588 // only the next block can include the transaction, we discourage this
2589 // practice as the height restricted and limited blocksize gives miners
2590 // considering fee sniping fewer options for pulling off this attack.
2592 // A simple way to think about this is from the wallet's point of view we
2593 // always want the blockchain to move forward. By setting nLockTime this
2594 // way we're basically making the statement that we only want this
2595 // transaction to appear in the next block; we don't want to potentially
2596 // encourage reorgs by allowing transactions to appear at lower heights
2597 // than the next block in forks of the best chain.
2599 // Of course, the subsidy is high enough, and transaction volume low
2600 // enough, that fee sniping isn't a problem yet, but by implementing a fix
2601 // now we ensure code won't be written that makes assumptions about
2602 // nLockTime that preclude a fix later.
2603 txNew.nLockTime = chainActive.Height();
2605 // Secondly occasionally randomly pick a nLockTime even further back, so
2606 // that transactions that are delayed after signing for whatever reason,
2607 // e.g. high-latency mix networks and some CoinJoin implementations, have
2608 // better privacy.
2609 if (GetRandInt(10) == 0)
2610 txNew.nLockTime = std::max(0, (int)txNew.nLockTime - GetRandInt(100));
2612 assert(txNew.nLockTime <= (unsigned int)chainActive.Height());
2613 assert(txNew.nLockTime < LOCKTIME_THRESHOLD);
2614 FeeCalculation feeCalc;
2615 unsigned int nBytes;
2617 std::set<CInputCoin> setCoins;
2618 LOCK2(cs_main, cs_wallet);
2620 std::vector<COutput> vAvailableCoins;
2621 AvailableCoins(vAvailableCoins, true, &coin_control);
2623 // Create change script that will be used if we need change
2624 // TODO: pass in scriptChange instead of reservekey so
2625 // change transaction isn't always pay-to-bitcoin-address
2626 CScript scriptChange;
2628 // coin control: send change to custom address
2629 if (!boost::get<CNoDestination>(&coin_control.destChange)) {
2630 scriptChange = GetScriptForDestination(coin_control.destChange);
2631 } else { // no coin control: send change to newly generated address
2632 // Note: We use a new key here to keep it from being obvious which side is the change.
2633 // The drawback is that by not reusing a previous key, the change may be lost if a
2634 // backup is restored, if the backup doesn't have the new private key for the change.
2635 // If we reused the old key, it would be possible to add code to look for and
2636 // rediscover unknown transactions that were written with keys of ours to recover
2637 // post-backup change.
2639 // Reserve a new key pair from key pool
2640 CPubKey vchPubKey;
2641 bool ret;
2642 ret = reservekey.GetReservedKey(vchPubKey, true);
2643 if (!ret)
2645 strFailReason = _("Keypool ran out, please call keypoolrefill first");
2646 return false;
2649 scriptChange = GetScriptForDestination(vchPubKey.GetID());
2651 CTxOut change_prototype_txout(0, scriptChange);
2652 size_t change_prototype_size = GetSerializeSize(change_prototype_txout, SER_DISK, 0);
2654 CFeeRate discard_rate = GetDiscardRate(::feeEstimator);
2655 nFeeRet = 0;
2656 bool pick_new_inputs = true;
2657 CAmount nValueIn = 0;
2658 // Start with no fee and loop until there is enough fee
2659 while (true)
2661 nChangePosInOut = nChangePosRequest;
2662 txNew.vin.clear();
2663 txNew.vout.clear();
2664 wtxNew.fFromMe = true;
2665 bool fFirst = true;
2667 CAmount nValueToSelect = nValue;
2668 if (nSubtractFeeFromAmount == 0)
2669 nValueToSelect += nFeeRet;
2670 // vouts to the payees
2671 for (const auto& recipient : vecSend)
2673 CTxOut txout(recipient.nAmount, recipient.scriptPubKey);
2675 if (recipient.fSubtractFeeFromAmount)
2677 txout.nValue -= nFeeRet / nSubtractFeeFromAmount; // Subtract fee equally from each selected recipient
2679 if (fFirst) // first receiver pays the remainder not divisible by output count
2681 fFirst = false;
2682 txout.nValue -= nFeeRet % nSubtractFeeFromAmount;
2686 if (IsDust(txout, ::dustRelayFee))
2688 if (recipient.fSubtractFeeFromAmount && nFeeRet > 0)
2690 if (txout.nValue < 0)
2691 strFailReason = _("The transaction amount is too small to pay the fee");
2692 else
2693 strFailReason = _("The transaction amount is too small to send after the fee has been deducted");
2695 else
2696 strFailReason = _("Transaction amount too small");
2697 return false;
2699 txNew.vout.push_back(txout);
2702 // Choose coins to use
2703 if (pick_new_inputs) {
2704 nValueIn = 0;
2705 setCoins.clear();
2706 if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins, nValueIn, &coin_control))
2708 strFailReason = _("Insufficient funds");
2709 return false;
2713 const CAmount nChange = nValueIn - nValueToSelect;
2715 if (nChange > 0)
2717 // Fill a vout to ourself
2718 CTxOut newTxOut(nChange, scriptChange);
2720 // Never create dust outputs; if we would, just
2721 // add the dust to the fee.
2722 if (IsDust(newTxOut, discard_rate))
2724 nChangePosInOut = -1;
2725 nFeeRet += nChange;
2727 else
2729 if (nChangePosInOut == -1)
2731 // Insert change txn at random position:
2732 nChangePosInOut = GetRandInt(txNew.vout.size()+1);
2734 else if ((unsigned int)nChangePosInOut > txNew.vout.size())
2736 strFailReason = _("Change index out of range");
2737 return false;
2740 std::vector<CTxOut>::iterator position = txNew.vout.begin()+nChangePosInOut;
2741 txNew.vout.insert(position, newTxOut);
2743 } else {
2744 nChangePosInOut = -1;
2747 // Fill vin
2749 // Note how the sequence number is set to non-maxint so that
2750 // the nLockTime set above actually works.
2752 // BIP125 defines opt-in RBF as any nSequence < maxint-1, so
2753 // we use the highest possible value in that range (maxint-2)
2754 // to avoid conflicting with other possible uses of nSequence,
2755 // and in the spirit of "smallest possible change from prior
2756 // behavior."
2757 const uint32_t nSequence = coin_control.signalRbf ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
2758 for (const auto& coin : setCoins)
2759 txNew.vin.push_back(CTxIn(coin.outpoint,CScript(),
2760 nSequence));
2762 // Fill in dummy signatures for fee calculation.
2763 if (!DummySignTx(txNew, setCoins)) {
2764 strFailReason = _("Signing transaction failed");
2765 return false;
2768 nBytes = GetVirtualTransactionSize(txNew);
2770 // Remove scriptSigs to eliminate the fee calculation dummy signatures
2771 for (auto& vin : txNew.vin) {
2772 vin.scriptSig = CScript();
2773 vin.scriptWitness.SetNull();
2776 CAmount nFeeNeeded = GetMinimumFee(nBytes, coin_control, ::mempool, ::feeEstimator, &feeCalc);
2778 // If we made it here and we aren't even able to meet the relay fee on the next pass, give up
2779 // because we must be at the maximum allowed fee.
2780 if (nFeeNeeded < ::minRelayTxFee.GetFee(nBytes))
2782 strFailReason = _("Transaction too large for fee policy");
2783 return false;
2786 if (nFeeRet >= nFeeNeeded) {
2787 // Reduce fee to only the needed amount if possible. This
2788 // prevents potential overpayment in fees if the coins
2789 // selected to meet nFeeNeeded result in a transaction that
2790 // requires less fee than the prior iteration.
2792 // TODO: The case where nSubtractFeeFromAmount > 0 remains
2793 // to be addressed because it requires returning the fee to
2794 // the payees and not the change output.
2796 // If we have no change and a big enough excess fee, then
2797 // try to construct transaction again only without picking
2798 // new inputs. We now know we only need the smaller fee
2799 // (because of reduced tx size) and so we should add a
2800 // change output. Only try this once.
2801 CAmount fee_needed_for_change = GetMinimumFee(change_prototype_size, coin_control, ::mempool, ::feeEstimator, nullptr);
2802 CAmount minimum_value_for_change = GetDustThreshold(change_prototype_txout, discard_rate);
2803 CAmount max_excess_fee = fee_needed_for_change + minimum_value_for_change;
2804 if (nFeeRet > nFeeNeeded + max_excess_fee && nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 && pick_new_inputs) {
2805 pick_new_inputs = false;
2806 nFeeRet = nFeeNeeded + fee_needed_for_change;
2807 continue;
2810 // If we have change output already, just increase it
2811 if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
2812 CAmount extraFeePaid = nFeeRet - nFeeNeeded;
2813 std::vector<CTxOut>::iterator change_position = txNew.vout.begin()+nChangePosInOut;
2814 change_position->nValue += extraFeePaid;
2815 nFeeRet -= extraFeePaid;
2817 break; // Done, enough fee included.
2819 else if (!pick_new_inputs) {
2820 // This shouldn't happen, we should have had enough excess
2821 // fee to pay for the new output and still meet nFeeNeeded
2822 strFailReason = _("Transaction fee and change calculation failed");
2823 return false;
2826 // Try to reduce change to include necessary fee
2827 if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
2828 CAmount additionalFeeNeeded = nFeeNeeded - nFeeRet;
2829 std::vector<CTxOut>::iterator change_position = txNew.vout.begin()+nChangePosInOut;
2830 // Only reduce change if remaining amount is still a large enough output.
2831 if (change_position->nValue >= MIN_FINAL_CHANGE + additionalFeeNeeded) {
2832 change_position->nValue -= additionalFeeNeeded;
2833 nFeeRet += additionalFeeNeeded;
2834 break; // Done, able to increase fee from change
2838 // Include more fee and try again.
2839 nFeeRet = nFeeNeeded;
2840 continue;
2844 if (nChangePosInOut == -1) reservekey.ReturnKey(); // Return any reserved key if we don't have change
2846 if (sign)
2848 CTransaction txNewConst(txNew);
2849 int nIn = 0;
2850 for (const auto& coin : setCoins)
2852 const CScript& scriptPubKey = coin.txout.scriptPubKey;
2853 SignatureData sigdata;
2855 if (!ProduceSignature(TransactionSignatureCreator(this, &txNewConst, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata))
2857 strFailReason = _("Signing transaction failed");
2858 return false;
2859 } else {
2860 UpdateTransaction(txNew, nIn, sigdata);
2863 nIn++;
2867 // Embed the constructed transaction data in wtxNew.
2868 wtxNew.SetTx(MakeTransactionRef(std::move(txNew)));
2870 // Limit size
2871 if (GetTransactionWeight(wtxNew) >= MAX_STANDARD_TX_WEIGHT)
2873 strFailReason = _("Transaction too large");
2874 return false;
2878 if (GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS)) {
2879 // Lastly, ensure this tx will pass the mempool's chain limits
2880 LockPoints lp;
2881 CTxMemPoolEntry entry(wtxNew.tx, 0, 0, 0, false, 0, lp);
2882 CTxMemPool::setEntries setAncestors;
2883 size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
2884 size_t nLimitAncestorSize = GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
2885 size_t nLimitDescendants = GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
2886 size_t nLimitDescendantSize = GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
2887 std::string errString;
2888 if (!mempool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
2889 strFailReason = _("Transaction has too long of a mempool chain");
2890 return false;
2894 LogPrintf("Fee Calculation: Fee:%d Bytes:%u Tgt:%d (requested %d) Reason:\"%s\" Decay %.5f: Estimation: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
2895 nFeeRet, nBytes, feeCalc.returnedTarget, feeCalc.desiredTarget, StringForFeeReason(feeCalc.reason), feeCalc.est.decay,
2896 feeCalc.est.pass.start, feeCalc.est.pass.end,
2897 100 * feeCalc.est.pass.withinTarget / (feeCalc.est.pass.totalConfirmed + feeCalc.est.pass.inMempool + feeCalc.est.pass.leftMempool),
2898 feeCalc.est.pass.withinTarget, feeCalc.est.pass.totalConfirmed, feeCalc.est.pass.inMempool, feeCalc.est.pass.leftMempool,
2899 feeCalc.est.fail.start, feeCalc.est.fail.end,
2900 100 * feeCalc.est.fail.withinTarget / (feeCalc.est.fail.totalConfirmed + feeCalc.est.fail.inMempool + feeCalc.est.fail.leftMempool),
2901 feeCalc.est.fail.withinTarget, feeCalc.est.fail.totalConfirmed, feeCalc.est.fail.inMempool, feeCalc.est.fail.leftMempool);
2902 return true;
2906 * Call after CreateTransaction unless you want to abort
2908 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, CConnman* connman, CValidationState& state)
2911 LOCK2(cs_main, cs_wallet);
2912 LogPrintf("CommitTransaction:\n%s", wtxNew.tx->ToString());
2914 // Take key pair from key pool so it won't be used again
2915 reservekey.KeepKey();
2917 // Add tx to wallet, because if it has change it's also ours,
2918 // otherwise just for transaction history.
2919 AddToWallet(wtxNew);
2921 // Notify that old coins are spent
2922 for (const CTxIn& txin : wtxNew.tx->vin)
2924 CWalletTx &coin = mapWallet[txin.prevout.hash];
2925 coin.BindWallet(this);
2926 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
2930 // Track how many getdata requests our transaction gets
2931 mapRequestCount[wtxNew.GetHash()] = 0;
2933 if (fBroadcastTransactions)
2935 // Broadcast
2936 if (!wtxNew.AcceptToMemoryPool(maxTxFee, state)) {
2937 LogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", state.GetRejectReason());
2938 // TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
2939 } else {
2940 wtxNew.RelayWalletTransaction(connman);
2944 return true;
2947 void CWallet::ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries) {
2948 CWalletDB walletdb(*dbw);
2949 return walletdb.ListAccountCreditDebit(strAccount, entries);
2952 bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry)
2954 CWalletDB walletdb(*dbw);
2956 return AddAccountingEntry(acentry, &walletdb);
2959 bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry, CWalletDB *pwalletdb)
2961 if (!pwalletdb->WriteAccountingEntry(++nAccountingEntryNumber, acentry)) {
2962 return false;
2965 laccentries.push_back(acentry);
2966 CAccountingEntry & entry = laccentries.back();
2967 wtxOrdered.insert(std::make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
2969 return true;
2972 CAmount CWallet::GetRequiredFee(unsigned int nTxBytes)
2974 return std::max(minTxFee.GetFee(nTxBytes), ::minRelayTxFee.GetFee(nTxBytes));
2977 CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
2979 /* User control of how to calculate fee uses the following parameter precedence:
2980 1. coin_control.m_feerate
2981 2. coin_control.m_confirm_target
2982 3. payTxFee (user-set global variable)
2983 4. nTxConfirmTarget (user-set global variable)
2984 The first parameter that is set is used.
2986 CAmount fee_needed;
2987 if (coin_control.m_feerate) { // 1.
2988 fee_needed = coin_control.m_feerate->GetFee(nTxBytes);
2989 if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
2990 // Allow to override automatic min/max check over coin control instance
2991 if (coin_control.fOverrideFeeRate) return fee_needed;
2993 else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee
2994 fee_needed = ::payTxFee.GetFee(nTxBytes);
2995 if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
2997 else { // 2. or 4.
2998 // We will use smart fee estimation
2999 unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : ::nTxConfirmTarget;
3000 // By default estimates are economical iff we are signaling opt-in-RBF
3001 bool conservative_estimate = !coin_control.signalRbf;
3002 // Allow to override the default fee estimate mode over the CoinControl instance
3003 if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
3004 else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
3006 fee_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate).GetFee(nTxBytes);
3007 if (fee_needed == 0) {
3008 // if we don't have enough data for estimateSmartFee, then use fallbackFee
3009 fee_needed = fallbackFee.GetFee(nTxBytes);
3010 if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
3012 // Obey mempool min fee when using smart fee estimation
3013 CAmount min_mempool_fee = pool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nTxBytes);
3014 if (fee_needed < min_mempool_fee) {
3015 fee_needed = min_mempool_fee;
3016 if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
3020 // prevent user from paying a fee below minRelayTxFee or minTxFee
3021 CAmount required_fee = GetRequiredFee(nTxBytes);
3022 if (required_fee > fee_needed) {
3023 fee_needed = required_fee;
3024 if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
3026 // But always obey the maximum
3027 if (fee_needed > maxTxFee) {
3028 fee_needed = maxTxFee;
3029 if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
3031 return fee_needed;
3037 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
3039 fFirstRunRet = false;
3040 DBErrors nLoadWalletRet = CWalletDB(*dbw,"cr+").LoadWallet(this);
3041 if (nLoadWalletRet == DB_NEED_REWRITE)
3043 if (dbw->Rewrite("\x04pool"))
3045 LOCK(cs_wallet);
3046 setInternalKeyPool.clear();
3047 setExternalKeyPool.clear();
3048 // Note: can't top-up keypool here, because wallet is locked.
3049 // User will be prompted to unlock wallet the next operation
3050 // that requires a new key.
3054 if (nLoadWalletRet != DB_LOAD_OK)
3055 return nLoadWalletRet;
3056 fFirstRunRet = !vchDefaultKey.IsValid();
3058 uiInterface.LoadWallet(this);
3060 return DB_LOAD_OK;
3063 DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
3065 AssertLockHeld(cs_wallet); // mapWallet
3066 vchDefaultKey = CPubKey();
3067 DBErrors nZapSelectTxRet = CWalletDB(*dbw,"cr+").ZapSelectTx(vHashIn, vHashOut);
3068 for (uint256 hash : vHashOut)
3069 mapWallet.erase(hash);
3071 if (nZapSelectTxRet == DB_NEED_REWRITE)
3073 if (dbw->Rewrite("\x04pool"))
3075 setInternalKeyPool.clear();
3076 setExternalKeyPool.clear();
3077 // Note: can't top-up keypool here, because wallet is locked.
3078 // User will be prompted to unlock wallet the next operation
3079 // that requires a new key.
3083 if (nZapSelectTxRet != DB_LOAD_OK)
3084 return nZapSelectTxRet;
3086 MarkDirty();
3088 return DB_LOAD_OK;
3092 DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
3094 vchDefaultKey = CPubKey();
3095 DBErrors nZapWalletTxRet = CWalletDB(*dbw,"cr+").ZapWalletTx(vWtx);
3096 if (nZapWalletTxRet == DB_NEED_REWRITE)
3098 if (dbw->Rewrite("\x04pool"))
3100 LOCK(cs_wallet);
3101 setInternalKeyPool.clear();
3102 setExternalKeyPool.clear();
3103 // Note: can't top-up keypool here, because wallet is locked.
3104 // User will be prompted to unlock wallet the next operation
3105 // that requires a new key.
3109 if (nZapWalletTxRet != DB_LOAD_OK)
3110 return nZapWalletTxRet;
3112 return DB_LOAD_OK;
3116 bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& strPurpose)
3118 bool fUpdated = false;
3120 LOCK(cs_wallet); // mapAddressBook
3121 std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
3122 fUpdated = mi != mapAddressBook.end();
3123 mapAddressBook[address].name = strName;
3124 if (!strPurpose.empty()) /* update purpose only if requested */
3125 mapAddressBook[address].purpose = strPurpose;
3127 NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != ISMINE_NO,
3128 strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) );
3129 if (!strPurpose.empty() && !CWalletDB(*dbw).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose))
3130 return false;
3131 return CWalletDB(*dbw).WriteName(CBitcoinAddress(address).ToString(), strName);
3134 bool CWallet::DelAddressBook(const CTxDestination& address)
3137 LOCK(cs_wallet); // mapAddressBook
3139 // Delete destdata tuples associated with address
3140 std::string strAddress = CBitcoinAddress(address).ToString();
3141 for (const std::pair<std::string, std::string> &item : mapAddressBook[address].destdata)
3143 CWalletDB(*dbw).EraseDestData(strAddress, item.first);
3145 mapAddressBook.erase(address);
3148 NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != ISMINE_NO, "", CT_DELETED);
3150 CWalletDB(*dbw).ErasePurpose(CBitcoinAddress(address).ToString());
3151 return CWalletDB(*dbw).EraseName(CBitcoinAddress(address).ToString());
3154 const std::string& CWallet::GetAccountName(const CScript& scriptPubKey) const
3156 CTxDestination address;
3157 if (ExtractDestination(scriptPubKey, address) && !scriptPubKey.IsUnspendable()) {
3158 auto mi = mapAddressBook.find(address);
3159 if (mi != mapAddressBook.end()) {
3160 return mi->second.name;
3163 // A scriptPubKey that doesn't have an entry in the address book is
3164 // associated with the default account ("").
3165 const static std::string DEFAULT_ACCOUNT_NAME;
3166 return DEFAULT_ACCOUNT_NAME;
3169 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
3171 if (!CWalletDB(*dbw).WriteDefaultKey(vchPubKey))
3172 return false;
3173 vchDefaultKey = vchPubKey;
3174 return true;
3178 * Mark old keypool keys as used,
3179 * and generate all new keys
3181 bool CWallet::NewKeyPool()
3184 LOCK(cs_wallet);
3185 CWalletDB walletdb(*dbw);
3187 for (int64_t nIndex : setInternalKeyPool) {
3188 walletdb.ErasePool(nIndex);
3190 setInternalKeyPool.clear();
3192 for (int64_t nIndex : setExternalKeyPool) {
3193 walletdb.ErasePool(nIndex);
3195 setExternalKeyPool.clear();
3197 if (!TopUpKeyPool()) {
3198 return false;
3200 LogPrintf("CWallet::NewKeyPool rewrote keypool\n");
3202 return true;
3205 size_t CWallet::KeypoolCountExternalKeys()
3207 AssertLockHeld(cs_wallet); // setExternalKeyPool
3208 return setExternalKeyPool.size();
3211 bool CWallet::TopUpKeyPool(unsigned int kpSize)
3214 LOCK(cs_wallet);
3216 if (IsLocked())
3217 return false;
3219 // Top up key pool
3220 unsigned int nTargetSize;
3221 if (kpSize > 0)
3222 nTargetSize = kpSize;
3223 else
3224 nTargetSize = std::max(GetArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t) 0);
3226 // count amount of available keys (internal, external)
3227 // make sure the keypool of external and internal keys fits the user selected target (-keypool)
3228 int64_t missingExternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setExternalKeyPool.size(), (int64_t) 0);
3229 int64_t missingInternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setInternalKeyPool.size(), (int64_t) 0);
3231 if (!IsHDEnabled() || !CanSupportFeature(FEATURE_HD_SPLIT))
3233 // don't create extra internal keys
3234 missingInternal = 0;
3236 bool internal = false;
3237 CWalletDB walletdb(*dbw);
3238 for (int64_t i = missingInternal + missingExternal; i--;)
3240 if (i < missingInternal) {
3241 internal = true;
3244 assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
3245 int64_t index = ++m_max_keypool_index;
3247 if (!walletdb.WritePool(index, CKeyPool(GenerateNewKey(walletdb, internal), internal))) {
3248 throw std::runtime_error(std::string(__func__) + ": writing generated key failed");
3251 if (internal) {
3252 setInternalKeyPool.insert(index);
3253 } else {
3254 setExternalKeyPool.insert(index);
3257 if (missingInternal + missingExternal > 0) {
3258 LogPrintf("keypool added %d keys (%d internal), size=%u (%u internal)\n", missingInternal + missingExternal, missingInternal, setInternalKeyPool.size() + setExternalKeyPool.size(), setInternalKeyPool.size());
3261 return true;
3264 void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal)
3266 nIndex = -1;
3267 keypool.vchPubKey = CPubKey();
3269 LOCK(cs_wallet);
3271 if (!IsLocked())
3272 TopUpKeyPool();
3274 bool fReturningInternal = IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT) && fRequestedInternal;
3275 std::set<int64_t>& setKeyPool = fReturningInternal ? setInternalKeyPool : setExternalKeyPool;
3277 // Get the oldest key
3278 if(setKeyPool.empty())
3279 return;
3281 CWalletDB walletdb(*dbw);
3283 auto it = setKeyPool.begin();
3284 nIndex = *it;
3285 setKeyPool.erase(it);
3286 if (!walletdb.ReadPool(nIndex, keypool)) {
3287 throw std::runtime_error(std::string(__func__) + ": read failed");
3289 if (!HaveKey(keypool.vchPubKey.GetID())) {
3290 throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
3292 if (keypool.fInternal != fReturningInternal) {
3293 throw std::runtime_error(std::string(__func__) + ": keypool entry misclassified");
3296 assert(keypool.vchPubKey.IsValid());
3297 LogPrintf("keypool reserve %d\n", nIndex);
3301 void CWallet::KeepKey(int64_t nIndex)
3303 // Remove from key pool
3304 CWalletDB walletdb(*dbw);
3305 walletdb.ErasePool(nIndex);
3306 LogPrintf("keypool keep %d\n", nIndex);
3309 void CWallet::ReturnKey(int64_t nIndex, bool fInternal)
3311 // Return to key pool
3313 LOCK(cs_wallet);
3314 if (fInternal) {
3315 setInternalKeyPool.insert(nIndex);
3316 } else {
3317 setExternalKeyPool.insert(nIndex);
3320 LogPrintf("keypool return %d\n", nIndex);
3323 bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
3325 CKeyPool keypool;
3327 LOCK(cs_wallet);
3328 int64_t nIndex = 0;
3329 ReserveKeyFromKeyPool(nIndex, keypool, internal);
3330 if (nIndex == -1)
3332 if (IsLocked()) return false;
3333 CWalletDB walletdb(*dbw);
3334 result = GenerateNewKey(walletdb, internal);
3335 return true;
3337 KeepKey(nIndex);
3338 result = keypool.vchPubKey;
3340 return true;
3343 static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, CWalletDB& walletdb) {
3344 if (setKeyPool.empty()) {
3345 return GetTime();
3348 CKeyPool keypool;
3349 int64_t nIndex = *(setKeyPool.begin());
3350 if (!walletdb.ReadPool(nIndex, keypool)) {
3351 throw std::runtime_error(std::string(__func__) + ": read oldest key in keypool failed");
3353 assert(keypool.vchPubKey.IsValid());
3354 return keypool.nTime;
3357 int64_t CWallet::GetOldestKeyPoolTime()
3359 LOCK(cs_wallet);
3361 CWalletDB walletdb(*dbw);
3363 // load oldest key from keypool, get time and return
3364 int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, walletdb);
3365 if (IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT)) {
3366 oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, walletdb), oldestKey);
3369 return oldestKey;
3372 std::map<CTxDestination, CAmount> CWallet::GetAddressBalances()
3374 std::map<CTxDestination, CAmount> balances;
3377 LOCK(cs_wallet);
3378 for (const auto& walletEntry : mapWallet)
3380 const CWalletTx *pcoin = &walletEntry.second;
3382 if (!pcoin->IsTrusted())
3383 continue;
3385 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
3386 continue;
3388 int nDepth = pcoin->GetDepthInMainChain();
3389 if (nDepth < (pcoin->IsFromMe(ISMINE_ALL) ? 0 : 1))
3390 continue;
3392 for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++)
3394 CTxDestination addr;
3395 if (!IsMine(pcoin->tx->vout[i]))
3396 continue;
3397 if(!ExtractDestination(pcoin->tx->vout[i].scriptPubKey, addr))
3398 continue;
3400 CAmount n = IsSpent(walletEntry.first, i) ? 0 : pcoin->tx->vout[i].nValue;
3402 if (!balances.count(addr))
3403 balances[addr] = 0;
3404 balances[addr] += n;
3409 return balances;
3412 std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
3414 AssertLockHeld(cs_wallet); // mapWallet
3415 std::set< std::set<CTxDestination> > groupings;
3416 std::set<CTxDestination> grouping;
3418 for (const auto& walletEntry : mapWallet)
3420 const CWalletTx *pcoin = &walletEntry.second;
3422 if (pcoin->tx->vin.size() > 0)
3424 bool any_mine = false;
3425 // group all input addresses with each other
3426 for (CTxIn txin : pcoin->tx->vin)
3428 CTxDestination address;
3429 if(!IsMine(txin)) /* If this input isn't mine, ignore it */
3430 continue;
3431 if(!ExtractDestination(mapWallet[txin.prevout.hash].tx->vout[txin.prevout.n].scriptPubKey, address))
3432 continue;
3433 grouping.insert(address);
3434 any_mine = true;
3437 // group change with input addresses
3438 if (any_mine)
3440 for (CTxOut txout : pcoin->tx->vout)
3441 if (IsChange(txout))
3443 CTxDestination txoutAddr;
3444 if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
3445 continue;
3446 grouping.insert(txoutAddr);
3449 if (grouping.size() > 0)
3451 groupings.insert(grouping);
3452 grouping.clear();
3456 // group lone addrs by themselves
3457 for (const auto& txout : pcoin->tx->vout)
3458 if (IsMine(txout))
3460 CTxDestination address;
3461 if(!ExtractDestination(txout.scriptPubKey, address))
3462 continue;
3463 grouping.insert(address);
3464 groupings.insert(grouping);
3465 grouping.clear();
3469 std::set< std::set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
3470 std::map< CTxDestination, std::set<CTxDestination>* > setmap; // map addresses to the unique group containing it
3471 for (std::set<CTxDestination> _grouping : groupings)
3473 // make a set of all the groups hit by this new group
3474 std::set< std::set<CTxDestination>* > hits;
3475 std::map< CTxDestination, std::set<CTxDestination>* >::iterator it;
3476 for (CTxDestination address : _grouping)
3477 if ((it = setmap.find(address)) != setmap.end())
3478 hits.insert((*it).second);
3480 // merge all hit groups into a new single group and delete old groups
3481 std::set<CTxDestination>* merged = new std::set<CTxDestination>(_grouping);
3482 for (std::set<CTxDestination>* hit : hits)
3484 merged->insert(hit->begin(), hit->end());
3485 uniqueGroupings.erase(hit);
3486 delete hit;
3488 uniqueGroupings.insert(merged);
3490 // update setmap
3491 for (CTxDestination element : *merged)
3492 setmap[element] = merged;
3495 std::set< std::set<CTxDestination> > ret;
3496 for (std::set<CTxDestination>* uniqueGrouping : uniqueGroupings)
3498 ret.insert(*uniqueGrouping);
3499 delete uniqueGrouping;
3502 return ret;
3505 std::set<CTxDestination> CWallet::GetAccountAddresses(const std::string& strAccount) const
3507 LOCK(cs_wallet);
3508 std::set<CTxDestination> result;
3509 for (const std::pair<CTxDestination, CAddressBookData>& item : mapAddressBook)
3511 const CTxDestination& address = item.first;
3512 const std::string& strName = item.second.name;
3513 if (strName == strAccount)
3514 result.insert(address);
3516 return result;
3519 bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
3521 if (nIndex == -1)
3523 CKeyPool keypool;
3524 pwallet->ReserveKeyFromKeyPool(nIndex, keypool, internal);
3525 if (nIndex != -1)
3526 vchPubKey = keypool.vchPubKey;
3527 else {
3528 return false;
3530 fInternal = keypool.fInternal;
3532 assert(vchPubKey.IsValid());
3533 pubkey = vchPubKey;
3534 return true;
3537 void CReserveKey::KeepKey()
3539 if (nIndex != -1)
3540 pwallet->KeepKey(nIndex);
3541 nIndex = -1;
3542 vchPubKey = CPubKey();
3545 void CReserveKey::ReturnKey()
3547 if (nIndex != -1) {
3548 pwallet->ReturnKey(nIndex, fInternal);
3550 nIndex = -1;
3551 vchPubKey = CPubKey();
3554 static void LoadReserveKeysToSet(std::set<CKeyID>& setAddress, const std::set<int64_t>& setKeyPool, CWalletDB& walletdb) {
3555 for (const int64_t& id : setKeyPool)
3557 CKeyPool keypool;
3558 if (!walletdb.ReadPool(id, keypool))
3559 throw std::runtime_error(std::string(__func__) + ": read failed");
3560 assert(keypool.vchPubKey.IsValid());
3561 CKeyID keyID = keypool.vchPubKey.GetID();
3562 setAddress.insert(keyID);
3566 void CWallet::GetAllReserveKeys(std::set<CKeyID>& setAddress) const
3568 setAddress.clear();
3570 CWalletDB walletdb(*dbw);
3572 LOCK2(cs_main, cs_wallet);
3573 LoadReserveKeysToSet(setAddress, setInternalKeyPool, walletdb);
3574 LoadReserveKeysToSet(setAddress, setExternalKeyPool, walletdb);
3576 for (const CKeyID& keyID : setAddress) {
3577 if (!HaveKey(keyID)) {
3578 throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
3583 void CWallet::GetScriptForMining(std::shared_ptr<CReserveScript> &script)
3585 std::shared_ptr<CReserveKey> rKey = std::make_shared<CReserveKey>(this);
3586 CPubKey pubkey;
3587 if (!rKey->GetReservedKey(pubkey))
3588 return;
3590 script = rKey;
3591 script->reserveScript = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
3594 void CWallet::LockCoin(const COutPoint& output)
3596 AssertLockHeld(cs_wallet); // setLockedCoins
3597 setLockedCoins.insert(output);
3600 void CWallet::UnlockCoin(const COutPoint& output)
3602 AssertLockHeld(cs_wallet); // setLockedCoins
3603 setLockedCoins.erase(output);
3606 void CWallet::UnlockAllCoins()
3608 AssertLockHeld(cs_wallet); // setLockedCoins
3609 setLockedCoins.clear();
3612 bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
3614 AssertLockHeld(cs_wallet); // setLockedCoins
3615 COutPoint outpt(hash, n);
3617 return (setLockedCoins.count(outpt) > 0);
3620 void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) const
3622 AssertLockHeld(cs_wallet); // setLockedCoins
3623 for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
3624 it != setLockedCoins.end(); it++) {
3625 COutPoint outpt = (*it);
3626 vOutpts.push_back(outpt);
3630 /** @} */ // end of Actions
3632 class CAffectedKeysVisitor : public boost::static_visitor<void> {
3633 private:
3634 const CKeyStore &keystore;
3635 std::vector<CKeyID> &vKeys;
3637 public:
3638 CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector<CKeyID> &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {}
3640 void Process(const CScript &script) {
3641 txnouttype type;
3642 std::vector<CTxDestination> vDest;
3643 int nRequired;
3644 if (ExtractDestinations(script, type, vDest, nRequired)) {
3645 for (const CTxDestination &dest : vDest)
3646 boost::apply_visitor(*this, dest);
3650 void operator()(const CKeyID &keyId) {
3651 if (keystore.HaveKey(keyId))
3652 vKeys.push_back(keyId);
3655 void operator()(const CScriptID &scriptId) {
3656 CScript script;
3657 if (keystore.GetCScript(scriptId, script))
3658 Process(script);
3661 void operator()(const CNoDestination &none) {}
3664 void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) const {
3665 AssertLockHeld(cs_wallet); // mapKeyMetadata
3666 mapKeyBirth.clear();
3668 // get birth times for keys with metadata
3669 for (const auto& entry : mapKeyMetadata) {
3670 if (entry.second.nCreateTime) {
3671 mapKeyBirth[entry.first] = entry.second.nCreateTime;
3675 // map in which we'll infer heights of other keys
3676 CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganized; use a 144-block safety margin
3677 std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
3678 std::set<CKeyID> setKeys;
3679 GetKeys(setKeys);
3680 for (const CKeyID &keyid : setKeys) {
3681 if (mapKeyBirth.count(keyid) == 0)
3682 mapKeyFirstBlock[keyid] = pindexMax;
3684 setKeys.clear();
3686 // if there are no such keys, we're done
3687 if (mapKeyFirstBlock.empty())
3688 return;
3690 // find first block that affects those keys, if there are any left
3691 std::vector<CKeyID> vAffected;
3692 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
3693 // iterate over all wallet transactions...
3694 const CWalletTx &wtx = (*it).second;
3695 BlockMap::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
3696 if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) {
3697 // ... which are already in a block
3698 int nHeight = blit->second->nHeight;
3699 for (const CTxOut &txout : wtx.tx->vout) {
3700 // iterate over all their outputs
3701 CAffectedKeysVisitor(*this, vAffected).Process(txout.scriptPubKey);
3702 for (const CKeyID &keyid : vAffected) {
3703 // ... and all their affected keys
3704 std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
3705 if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
3706 rit->second = blit->second;
3708 vAffected.clear();
3713 // Extract block timestamps for those keys
3714 for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
3715 mapKeyBirth[it->first] = it->second->GetBlockTime() - TIMESTAMP_WINDOW; // block times can be 2h off
3719 * Compute smart timestamp for a transaction being added to the wallet.
3721 * Logic:
3722 * - If sending a transaction, assign its timestamp to the current time.
3723 * - If receiving a transaction outside a block, assign its timestamp to the
3724 * current time.
3725 * - If receiving a block with a future timestamp, assign all its (not already
3726 * known) transactions' timestamps to the current time.
3727 * - If receiving a block with a past timestamp, before the most recent known
3728 * transaction (that we care about), assign all its (not already known)
3729 * transactions' timestamps to the same timestamp as that most-recent-known
3730 * transaction.
3731 * - If receiving a block with a past timestamp, but after the most recent known
3732 * transaction, assign all its (not already known) transactions' timestamps to
3733 * the block time.
3735 * For more information see CWalletTx::nTimeSmart,
3736 * https://bitcointalk.org/?topic=54527, or
3737 * https://github.com/bitcoin/bitcoin/pull/1393.
3739 unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
3741 unsigned int nTimeSmart = wtx.nTimeReceived;
3742 if (!wtx.hashUnset()) {
3743 if (mapBlockIndex.count(wtx.hashBlock)) {
3744 int64_t latestNow = wtx.nTimeReceived;
3745 int64_t latestEntry = 0;
3747 // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
3748 int64_t latestTolerated = latestNow + 300;
3749 const TxItems& txOrdered = wtxOrdered;
3750 for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
3751 CWalletTx* const pwtx = it->second.first;
3752 if (pwtx == &wtx) {
3753 continue;
3755 CAccountingEntry* const pacentry = it->second.second;
3756 int64_t nSmartTime;
3757 if (pwtx) {
3758 nSmartTime = pwtx->nTimeSmart;
3759 if (!nSmartTime) {
3760 nSmartTime = pwtx->nTimeReceived;
3762 } else {
3763 nSmartTime = pacentry->nTime;
3765 if (nSmartTime <= latestTolerated) {
3766 latestEntry = nSmartTime;
3767 if (nSmartTime > latestNow) {
3768 latestNow = nSmartTime;
3770 break;
3774 int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
3775 nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
3776 } else {
3777 LogPrintf("%s: found %s in block %s not in index\n", __func__, wtx.GetHash().ToString(), wtx.hashBlock.ToString());
3780 return nTimeSmart;
3783 bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
3785 if (boost::get<CNoDestination>(&dest))
3786 return false;
3788 mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
3789 return CWalletDB(*dbw).WriteDestData(CBitcoinAddress(dest).ToString(), key, value);
3792 bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
3794 if (!mapAddressBook[dest].destdata.erase(key))
3795 return false;
3796 return CWalletDB(*dbw).EraseDestData(CBitcoinAddress(dest).ToString(), key);
3799 bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
3801 mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
3802 return true;
3805 bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
3807 std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest);
3808 if(i != mapAddressBook.end())
3810 CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
3811 if(j != i->second.destdata.end())
3813 if(value)
3814 *value = j->second;
3815 return true;
3818 return false;
3821 std::vector<std::string> CWallet::GetDestValues(const std::string& prefix) const
3823 LOCK(cs_wallet);
3824 std::vector<std::string> values;
3825 for (const auto& address : mapAddressBook) {
3826 for (const auto& data : address.second.destdata) {
3827 if (!data.first.compare(0, prefix.size(), prefix)) {
3828 values.emplace_back(data.second);
3832 return values;
3835 std::string CWallet::GetWalletHelpString(bool showDebug)
3837 std::string strUsage = HelpMessageGroup(_("Wallet options:"));
3838 strUsage += HelpMessageOpt("-disablewallet", _("Do not load the wallet and disable wallet RPC calls"));
3839 strUsage += HelpMessageOpt("-keypool=<n>", strprintf(_("Set key pool size to <n> (default: %u)"), DEFAULT_KEYPOOL_SIZE));
3840 strUsage += HelpMessageOpt("-fallbackfee=<amt>", strprintf(_("A fee rate (in %s/kB) that will be used when fee estimation has insufficient data (default: %s)"),
3841 CURRENCY_UNIT, FormatMoney(DEFAULT_FALLBACK_FEE)));
3842 strUsage += HelpMessageOpt("-discardfee=<amt>", strprintf(_("The fee rate (in %s/kB) used to discard change (to fee) if it would be dust at this fee rate (default: %s) "
3843 "Note: We will always discard up to the dust relay fee and a discard fee above that is limited by the longest target fee estimate"),
3844 CURRENCY_UNIT, FormatMoney(DEFAULT_DISCARD_FEE)));
3845 strUsage += HelpMessageOpt("-mintxfee=<amt>", strprintf(_("Fees (in %s/kB) smaller than this are considered zero fee for transaction creation (default: %s)"),
3846 CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MINFEE)));
3847 strUsage += HelpMessageOpt("-paytxfee=<amt>", strprintf(_("Fee (in %s/kB) to add to transactions you send (default: %s)"),
3848 CURRENCY_UNIT, FormatMoney(payTxFee.GetFeePerK())));
3849 strUsage += HelpMessageOpt("-rescan", _("Rescan the block chain for missing wallet transactions on startup"));
3850 strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet on startup"));
3851 strUsage += HelpMessageOpt("-spendzeroconfchange", strprintf(_("Spend unconfirmed change when sending transactions (default: %u)"), DEFAULT_SPEND_ZEROCONF_CHANGE));
3852 strUsage += HelpMessageOpt("-txconfirmtarget=<n>", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), DEFAULT_TX_CONFIRM_TARGET));
3853 strUsage += HelpMessageOpt("-usehd", _("Use hierarchical deterministic key generation (HD) after BIP32. Only has effect during wallet creation/first start") + " " + strprintf(_("(default: %u)"), DEFAULT_USE_HD_WALLET));
3854 strUsage += HelpMessageOpt("-walletrbf", strprintf(_("Send transactions with full-RBF opt-in enabled (default: %u)"), DEFAULT_WALLET_RBF));
3855 strUsage += HelpMessageOpt("-upgradewallet", _("Upgrade wallet to latest format on startup"));
3856 strUsage += HelpMessageOpt("-wallet=<file>", _("Specify wallet file (within data directory)") + " " + strprintf(_("(default: %s)"), DEFAULT_WALLET_DAT));
3857 strUsage += HelpMessageOpt("-walletbroadcast", _("Make the wallet broadcast transactions") + " " + strprintf(_("(default: %u)"), DEFAULT_WALLETBROADCAST));
3858 strUsage += HelpMessageOpt("-walletnotify=<cmd>", _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)"));
3859 strUsage += HelpMessageOpt("-zapwallettxes=<mode>", _("Delete all wallet transactions and only recover those parts of the blockchain through -rescan on startup") +
3860 " " + _("(1 = keep tx meta data e.g. account owner and payment request information, 2 = drop tx meta data)"));
3862 if (showDebug)
3864 strUsage += HelpMessageGroup(_("Wallet debugging/testing options:"));
3866 strUsage += HelpMessageOpt("-dblogsize=<n>", strprintf("Flush wallet database activity from memory to disk log every <n> megabytes (default: %u)", DEFAULT_WALLET_DBLOGSIZE));
3867 strUsage += HelpMessageOpt("-flushwallet", strprintf("Run a thread to flush wallet periodically (default: %u)", DEFAULT_FLUSHWALLET));
3868 strUsage += HelpMessageOpt("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", DEFAULT_WALLET_PRIVDB));
3869 strUsage += HelpMessageOpt("-walletrejectlongchains", strprintf(_("Wallet will not create transactions that violate mempool chain limits (default: %u)"), DEFAULT_WALLET_REJECT_LONG_CHAINS));
3872 return strUsage;
3875 CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
3877 // needed to restore wallet transaction meta data after -zapwallettxes
3878 std::vector<CWalletTx> vWtx;
3880 if (GetBoolArg("-zapwallettxes", false)) {
3881 uiInterface.InitMessage(_("Zapping all transactions from wallet..."));
3883 std::unique_ptr<CWalletDBWrapper> dbw(new CWalletDBWrapper(&bitdb, walletFile));
3884 CWallet *tempWallet = new CWallet(std::move(dbw));
3885 DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
3886 if (nZapWalletRet != DB_LOAD_OK) {
3887 InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3888 return NULL;
3891 delete tempWallet;
3892 tempWallet = NULL;
3895 uiInterface.InitMessage(_("Loading wallet..."));
3897 int64_t nStart = GetTimeMillis();
3898 bool fFirstRun = true;
3899 std::unique_ptr<CWalletDBWrapper> dbw(new CWalletDBWrapper(&bitdb, walletFile));
3900 CWallet *walletInstance = new CWallet(std::move(dbw));
3901 DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
3902 if (nLoadWalletRet != DB_LOAD_OK)
3904 if (nLoadWalletRet == DB_CORRUPT) {
3905 InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3906 return NULL;
3908 else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
3910 InitWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
3911 " or address book entries might be missing or incorrect."),
3912 walletFile));
3914 else if (nLoadWalletRet == DB_TOO_NEW) {
3915 InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), walletFile, _(PACKAGE_NAME)));
3916 return NULL;
3918 else if (nLoadWalletRet == DB_NEED_REWRITE)
3920 InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
3921 return NULL;
3923 else {
3924 InitError(strprintf(_("Error loading %s"), walletFile));
3925 return NULL;
3929 if (GetBoolArg("-upgradewallet", fFirstRun))
3931 int nMaxVersion = GetArg("-upgradewallet", 0);
3932 if (nMaxVersion == 0) // the -upgradewallet without argument case
3934 LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
3935 nMaxVersion = CLIENT_VERSION;
3936 walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
3938 else
3939 LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
3940 if (nMaxVersion < walletInstance->GetVersion())
3942 InitError(_("Cannot downgrade wallet"));
3943 return NULL;
3945 walletInstance->SetMaxVersion(nMaxVersion);
3948 if (fFirstRun)
3950 // Create new keyUser and set as default key
3951 if (GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET) && !walletInstance->IsHDEnabled()) {
3953 // ensure this wallet.dat can only be opened by clients supporting HD with chain split
3954 walletInstance->SetMinVersion(FEATURE_HD_SPLIT);
3956 // generate a new master key
3957 CPubKey masterPubKey = walletInstance->GenerateNewHDMasterKey();
3958 if (!walletInstance->SetHDMasterKey(masterPubKey))
3959 throw std::runtime_error(std::string(__func__) + ": Storing master key failed");
3961 CPubKey newDefaultKey;
3962 if (walletInstance->GetKeyFromPool(newDefaultKey, false)) {
3963 walletInstance->SetDefaultKey(newDefaultKey);
3964 if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive")) {
3965 InitError(_("Cannot write default address") += "\n");
3966 return NULL;
3970 walletInstance->SetBestChain(chainActive.GetLocator());
3972 else if (IsArgSet("-usehd")) {
3973 bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET);
3974 if (walletInstance->IsHDEnabled() && !useHD) {
3975 InitError(strprintf(_("Error loading %s: You can't disable HD on an already existing HD wallet"), walletFile));
3976 return NULL;
3978 if (!walletInstance->IsHDEnabled() && useHD) {
3979 InitError(strprintf(_("Error loading %s: You can't enable HD on an already existing non-HD wallet"), walletFile));
3980 return NULL;
3984 LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart);
3986 RegisterValidationInterface(walletInstance);
3988 CBlockIndex *pindexRescan = chainActive.Genesis();
3989 if (!GetBoolArg("-rescan", false))
3991 CWalletDB walletdb(*walletInstance->dbw);
3992 CBlockLocator locator;
3993 if (walletdb.ReadBestBlock(locator))
3994 pindexRescan = FindForkInGlobalIndex(chainActive, locator);
3996 if (chainActive.Tip() && chainActive.Tip() != pindexRescan)
3998 //We can't rescan beyond non-pruned blocks, stop and throw an error
3999 //this might happen if a user uses an old wallet within a pruned node
4000 // or if he ran -disablewallet for a longer time, then decided to re-enable
4001 if (fPruneMode)
4003 CBlockIndex *block = chainActive.Tip();
4004 while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA) && block->pprev->nTx > 0 && pindexRescan != block)
4005 block = block->pprev;
4007 if (pindexRescan != block) {
4008 InitError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
4009 return NULL;
4013 uiInterface.InitMessage(_("Rescanning..."));
4014 LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight);
4016 // No need to read and scan block if block was created before
4017 // our wallet birthday (as adjusted for block time variability)
4018 while (pindexRescan && walletInstance->nTimeFirstKey && (pindexRescan->GetBlockTime() < (walletInstance->nTimeFirstKey - TIMESTAMP_WINDOW))) {
4019 pindexRescan = chainActive.Next(pindexRescan);
4022 nStart = GetTimeMillis();
4023 walletInstance->ScanForWalletTransactions(pindexRescan, true);
4024 LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
4025 walletInstance->SetBestChain(chainActive.GetLocator());
4026 walletInstance->dbw->IncrementUpdateCounter();
4028 // Restore wallet transaction metadata after -zapwallettxes=1
4029 if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")
4031 CWalletDB walletdb(*walletInstance->dbw);
4033 for (const CWalletTx& wtxOld : vWtx)
4035 uint256 hash = wtxOld.GetHash();
4036 std::map<uint256, CWalletTx>::iterator mi = walletInstance->mapWallet.find(hash);
4037 if (mi != walletInstance->mapWallet.end())
4039 const CWalletTx* copyFrom = &wtxOld;
4040 CWalletTx* copyTo = &mi->second;
4041 copyTo->mapValue = copyFrom->mapValue;
4042 copyTo->vOrderForm = copyFrom->vOrderForm;
4043 copyTo->nTimeReceived = copyFrom->nTimeReceived;
4044 copyTo->nTimeSmart = copyFrom->nTimeSmart;
4045 copyTo->fFromMe = copyFrom->fFromMe;
4046 copyTo->strFromAccount = copyFrom->strFromAccount;
4047 copyTo->nOrderPos = copyFrom->nOrderPos;
4048 walletdb.WriteTx(*copyTo);
4053 walletInstance->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
4056 LOCK(walletInstance->cs_wallet);
4057 LogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());
4058 LogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size());
4059 LogPrintf("mapAddressBook.size() = %u\n", walletInstance->mapAddressBook.size());
4062 return walletInstance;
4065 bool CWallet::InitLoadWallet()
4067 if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
4068 LogPrintf("Wallet disabled!\n");
4069 return true;
4072 for (const std::string& walletFile : gArgs.GetArgs("-wallet")) {
4073 CWallet * const pwallet = CreateWalletFromFile(walletFile);
4074 if (!pwallet) {
4075 return false;
4077 vpwallets.push_back(pwallet);
4080 return true;
4083 std::atomic<bool> CWallet::fFlushScheduled(false);
4085 void CWallet::postInitProcess(CScheduler& scheduler)
4087 // Add wallet transactions that aren't already in a block to mempool
4088 // Do this here as mempool requires genesis block to be loaded
4089 ReacceptWalletTransactions();
4091 // Run a thread to flush wallet periodically
4092 if (!CWallet::fFlushScheduled.exchange(true)) {
4093 scheduler.scheduleEvery(MaybeCompactWalletDB, 500);
4097 bool CWallet::ParameterInteraction()
4099 SoftSetArg("-wallet", DEFAULT_WALLET_DAT);
4100 const bool is_multiwallet = gArgs.GetArgs("-wallet").size() > 1;
4102 if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET))
4103 return true;
4105 if (GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY) && SoftSetBoolArg("-walletbroadcast", false)) {
4106 LogPrintf("%s: parameter interaction: -blocksonly=1 -> setting -walletbroadcast=0\n", __func__);
4109 if (GetBoolArg("-salvagewallet", false)) {
4110 if (is_multiwallet) {
4111 return InitError(strprintf("%s is only allowed with a single wallet file", "-salvagewallet"));
4113 // Rewrite just private keys: rescan to find transactions
4114 if (SoftSetBoolArg("-rescan", true)) {
4115 LogPrintf("%s: parameter interaction: -salvagewallet=1 -> setting -rescan=1\n", __func__);
4119 int zapwallettxes = GetArg("-zapwallettxes", 0);
4120 // -zapwallettxes implies dropping the mempool on startup
4121 if (zapwallettxes != 0 && SoftSetBoolArg("-persistmempool", false)) {
4122 LogPrintf("%s: parameter interaction: -zapwallettxes=%s -> setting -persistmempool=0\n", __func__, zapwallettxes);
4125 // -zapwallettxes implies a rescan
4126 if (zapwallettxes != 0) {
4127 if (is_multiwallet) {
4128 return InitError(strprintf("%s is only allowed with a single wallet file", "-zapwallettxes"));
4130 if (SoftSetBoolArg("-rescan", true)) {
4131 LogPrintf("%s: parameter interaction: -zapwallettxes=%s -> setting -rescan=1\n", __func__, zapwallettxes);
4135 if (is_multiwallet) {
4136 if (GetBoolArg("-upgradewallet", false)) {
4137 return InitError(strprintf("%s is only allowed with a single wallet file", "-upgradewallet"));
4141 if (GetBoolArg("-sysperms", false))
4142 return InitError("-sysperms is not allowed in combination with enabled wallet functionality");
4143 if (GetArg("-prune", 0) && GetBoolArg("-rescan", false))
4144 return InitError(_("Rescans are not possible in pruned mode. You will need to use -reindex which will download the whole blockchain again."));
4146 if (::minRelayTxFee.GetFeePerK() > HIGH_TX_FEE_PER_KB)
4147 InitWarning(AmountHighWarn("-minrelaytxfee") + " " +
4148 _("The wallet will avoid paying less than the minimum relay fee."));
4150 if (IsArgSet("-mintxfee"))
4152 CAmount n = 0;
4153 if (!ParseMoney(GetArg("-mintxfee", ""), n) || 0 == n)
4154 return InitError(AmountErrMsg("mintxfee", GetArg("-mintxfee", "")));
4155 if (n > HIGH_TX_FEE_PER_KB)
4156 InitWarning(AmountHighWarn("-mintxfee") + " " +
4157 _("This is the minimum transaction fee you pay on every transaction."));
4158 CWallet::minTxFee = CFeeRate(n);
4160 if (IsArgSet("-fallbackfee"))
4162 CAmount nFeePerK = 0;
4163 if (!ParseMoney(GetArg("-fallbackfee", ""), nFeePerK))
4164 return InitError(strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), GetArg("-fallbackfee", "")));
4165 if (nFeePerK > HIGH_TX_FEE_PER_KB)
4166 InitWarning(AmountHighWarn("-fallbackfee") + " " +
4167 _("This is the transaction fee you may pay when fee estimates are not available."));
4168 CWallet::fallbackFee = CFeeRate(nFeePerK);
4170 if (IsArgSet("-discardfee"))
4172 CAmount nFeePerK = 0;
4173 if (!ParseMoney(GetArg("-discardfee", ""), nFeePerK))
4174 return InitError(strprintf(_("Invalid amount for -discardfee=<amount>: '%s'"), GetArg("-discardfee", "")));
4175 if (nFeePerK > HIGH_TX_FEE_PER_KB)
4176 InitWarning(AmountHighWarn("-discardfee") + " " +
4177 _("This is the transaction fee you may discard if change is smaller than dust at this level"));
4178 CWallet::m_discard_rate = CFeeRate(nFeePerK);
4180 if (IsArgSet("-paytxfee"))
4182 CAmount nFeePerK = 0;
4183 if (!ParseMoney(GetArg("-paytxfee", ""), nFeePerK))
4184 return InitError(AmountErrMsg("paytxfee", GetArg("-paytxfee", "")));
4185 if (nFeePerK > HIGH_TX_FEE_PER_KB)
4186 InitWarning(AmountHighWarn("-paytxfee") + " " +
4187 _("This is the transaction fee you will pay if you send a transaction."));
4189 payTxFee = CFeeRate(nFeePerK, 1000);
4190 if (payTxFee < ::minRelayTxFee)
4192 return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
4193 GetArg("-paytxfee", ""), ::minRelayTxFee.ToString()));
4196 if (IsArgSet("-maxtxfee"))
4198 CAmount nMaxFee = 0;
4199 if (!ParseMoney(GetArg("-maxtxfee", ""), nMaxFee))
4200 return InitError(AmountErrMsg("maxtxfee", GetArg("-maxtxfee", "")));
4201 if (nMaxFee > HIGH_MAX_TX_FEE)
4202 InitWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
4203 maxTxFee = nMaxFee;
4204 if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee)
4206 return InitError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
4207 GetArg("-maxtxfee", ""), ::minRelayTxFee.ToString()));
4210 nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
4211 bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
4212 fWalletRbf = GetBoolArg("-walletrbf", DEFAULT_WALLET_RBF);
4214 return true;
4217 bool CWallet::BackupWallet(const std::string& strDest)
4219 return dbw->Backup(strDest);
4222 CKeyPool::CKeyPool()
4224 nTime = GetTime();
4225 fInternal = false;
4228 CKeyPool::CKeyPool(const CPubKey& vchPubKeyIn, bool internalIn)
4230 nTime = GetTime();
4231 vchPubKey = vchPubKeyIn;
4232 fInternal = internalIn;
4235 CWalletKey::CWalletKey(int64_t nExpires)
4237 nTimeCreated = (nExpires ? GetTime() : 0);
4238 nTimeExpires = nExpires;
4241 void CMerkleTx::SetMerkleBranch(const CBlockIndex* pindex, int posInBlock)
4243 // Update the tx's hashBlock
4244 hashBlock = pindex->GetBlockHash();
4246 // set the position of the transaction in the block
4247 nIndex = posInBlock;
4250 int CMerkleTx::GetDepthInMainChain(const CBlockIndex* &pindexRet) const
4252 if (hashUnset())
4253 return 0;
4255 AssertLockHeld(cs_main);
4257 // Find the block it claims to be in
4258 BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
4259 if (mi == mapBlockIndex.end())
4260 return 0;
4261 CBlockIndex* pindex = (*mi).second;
4262 if (!pindex || !chainActive.Contains(pindex))
4263 return 0;
4265 pindexRet = pindex;
4266 return ((nIndex == -1) ? (-1) : 1) * (chainActive.Height() - pindex->nHeight + 1);
4269 int CMerkleTx::GetBlocksToMaturity() const
4271 if (!IsCoinBase())
4272 return 0;
4273 return std::max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain());
4277 bool CMerkleTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state)
4279 return ::AcceptToMemoryPool(mempool, state, tx, true, NULL, NULL, false, nAbsurdFee);