You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wallet.cpp 66KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "wallet.h"
  6. #include "base58.h"
  7. #include "coincontrol.h"
  8. #include "net.h"
  9. #include <inttypes.h>
  10. #include <boost/algorithm/string/replace.hpp>
  11. #include <openssl/rand.h>
  12. using namespace std;
  13. //////////////////////////////////////////////////////////////////////////////
  14. //
  15. // mapWallet
  16. //
  17. struct CompareValueOnly
  18. {
  19. bool operator()(const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t1,
  20. const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t2) const
  21. {
  22. return t1.first < t2.first;
  23. }
  24. };
  25. CPubKey CWallet::GenerateNewKey()
  26. {
  27. bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
  28. RandAddSeedPerfmon();
  29. CKey secret;
  30. secret.MakeNewKey(fCompressed);
  31. // Compressed public keys were introduced in version 0.6.0
  32. if (fCompressed)
  33. SetMinVersion(FEATURE_COMPRPUBKEY);
  34. CPubKey pubkey = secret.GetPubKey();
  35. // Create new metadata
  36. int64_t nCreationTime = GetTime();
  37. mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
  38. if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
  39. nTimeFirstKey = nCreationTime;
  40. if (!AddKeyPubKey(secret, pubkey))
  41. throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
  42. return pubkey;
  43. }
  44. bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
  45. {
  46. if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey))
  47. return false;
  48. if (!fFileBacked)
  49. return true;
  50. if (!IsCrypted()) {
  51. return CWalletDB(strWalletFile).WriteKey(pubkey,
  52. secret.GetPrivKey(),
  53. mapKeyMetadata[pubkey.GetID()]);
  54. }
  55. return true;
  56. }
  57. bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
  58. const vector<unsigned char> &vchCryptedSecret)
  59. {
  60. if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
  61. return false;
  62. if (!fFileBacked)
  63. return true;
  64. {
  65. LOCK(cs_wallet);
  66. if (pwalletdbEncryption)
  67. return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
  68. vchCryptedSecret,
  69. mapKeyMetadata[vchPubKey.GetID()]);
  70. else
  71. return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey,
  72. vchCryptedSecret,
  73. mapKeyMetadata[vchPubKey.GetID()]);
  74. }
  75. return false;
  76. }
  77. bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
  78. {
  79. if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
  80. nTimeFirstKey = meta.nCreateTime;
  81. mapKeyMetadata[pubkey.GetID()] = meta;
  82. return true;
  83. }
  84. bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
  85. {
  86. return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
  87. }
  88. bool CWallet::AddCScript(const CScript& redeemScript)
  89. {
  90. if (!CCryptoKeyStore::AddCScript(redeemScript))
  91. return false;
  92. if (!fFileBacked)
  93. return true;
  94. return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
  95. }
  96. bool CWallet::Unlock(const SecureString& strWalletPassphrase)
  97. {
  98. CCrypter crypter;
  99. CKeyingMaterial vMasterKey;
  100. {
  101. LOCK(cs_wallet);
  102. BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
  103. {
  104. if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
  105. return false;
  106. if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
  107. continue; // try another master key
  108. if (CCryptoKeyStore::Unlock(vMasterKey))
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
  115. {
  116. bool fWasLocked = IsLocked();
  117. {
  118. LOCK(cs_wallet);
  119. Lock();
  120. CCrypter crypter;
  121. CKeyingMaterial vMasterKey;
  122. BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
  123. {
  124. if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
  125. return false;
  126. if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
  127. return false;
  128. if (CCryptoKeyStore::Unlock(vMasterKey))
  129. {
  130. int64_t nStartTime = GetTimeMillis();
  131. crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
  132. pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
  133. nStartTime = GetTimeMillis();
  134. crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
  135. pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
  136. if (pMasterKey.second.nDeriveIterations < 25000)
  137. pMasterKey.second.nDeriveIterations = 25000;
  138. LogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
  139. if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
  140. return false;
  141. if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
  142. return false;
  143. CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
  144. if (fWasLocked)
  145. Lock();
  146. return true;
  147. }
  148. }
  149. }
  150. return false;
  151. }
  152. void CWallet::SetBestChain(const CBlockLocator& loc)
  153. {
  154. CWalletDB walletdb(strWalletFile);
  155. walletdb.WriteBestBlock(loc);
  156. }
  157. // This class implements an addrIncoming entry that causes pre-0.4
  158. // clients to crash on startup if reading a private-key-encrypted wallet.
  159. class CCorruptAddress
  160. {
  161. public:
  162. IMPLEMENT_SERIALIZE
  163. (
  164. if (nType & SER_DISK)
  165. READWRITE(nVersion);
  166. )
  167. };
  168. bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
  169. {
  170. if (nWalletVersion >= nVersion)
  171. return true;
  172. // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
  173. if (fExplicit && nVersion > nWalletMaxVersion)
  174. nVersion = FEATURE_LATEST;
  175. nWalletVersion = nVersion;
  176. if (nVersion > nWalletMaxVersion)
  177. nWalletMaxVersion = nVersion;
  178. if (fFileBacked)
  179. {
  180. CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
  181. if (nWalletVersion >= 40000)
  182. {
  183. // Versions prior to 0.4.0 did not support the "minversion" record.
  184. // Use a CCorruptAddress to make them crash instead.
  185. CCorruptAddress corruptAddress;
  186. pwalletdb->WriteSetting("addrIncoming", corruptAddress);
  187. }
  188. if (nWalletVersion > 40000)
  189. pwalletdb->WriteMinVersion(nWalletVersion);
  190. if (!pwalletdbIn)
  191. delete pwalletdb;
  192. }
  193. return true;
  194. }
  195. bool CWallet::SetMaxVersion(int nVersion)
  196. {
  197. // cannot downgrade below current version
  198. if (nWalletVersion > nVersion)
  199. return false;
  200. nWalletMaxVersion = nVersion;
  201. return true;
  202. }
  203. bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
  204. {
  205. if (IsCrypted())
  206. return false;
  207. CKeyingMaterial vMasterKey;
  208. RandAddSeedPerfmon();
  209. vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
  210. RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
  211. CMasterKey kMasterKey;
  212. RandAddSeedPerfmon();
  213. kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
  214. RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
  215. CCrypter crypter;
  216. int64_t nStartTime = GetTimeMillis();
  217. crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
  218. kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
  219. nStartTime = GetTimeMillis();
  220. crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
  221. kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
  222. if (kMasterKey.nDeriveIterations < 25000)
  223. kMasterKey.nDeriveIterations = 25000;
  224. LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
  225. if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
  226. return false;
  227. if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
  228. return false;
  229. {
  230. LOCK(cs_wallet);
  231. mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
  232. if (fFileBacked)
  233. {
  234. pwalletdbEncryption = new CWalletDB(strWalletFile);
  235. if (!pwalletdbEncryption->TxnBegin())
  236. return false;
  237. pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
  238. }
  239. if (!EncryptKeys(vMasterKey))
  240. {
  241. if (fFileBacked)
  242. pwalletdbEncryption->TxnAbort();
  243. exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
  244. }
  245. // Encryption was introduced in version 0.4.0
  246. SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
  247. if (fFileBacked)
  248. {
  249. if (!pwalletdbEncryption->TxnCommit())
  250. exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.
  251. delete pwalletdbEncryption;
  252. pwalletdbEncryption = NULL;
  253. }
  254. Lock();
  255. Unlock(strWalletPassphrase);
  256. NewKeyPool();
  257. Lock();
  258. // Need to completely rewrite the wallet file; if we don't, bdb might keep
  259. // bits of the unencrypted private key in slack space in the database file.
  260. CDB::Rewrite(strWalletFile);
  261. }
  262. NotifyStatusChanged(this);
  263. return true;
  264. }
  265. int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
  266. {
  267. int64_t nRet = nOrderPosNext++;
  268. if (pwalletdb) {
  269. pwalletdb->WriteOrderPosNext(nOrderPosNext);
  270. } else {
  271. CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
  272. }
  273. return nRet;
  274. }
  275. CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
  276. {
  277. CWalletDB walletdb(strWalletFile);
  278. // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
  279. TxItems txOrdered;
  280. // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
  281. // would make this much faster for applications that do this a lot.
  282. for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  283. {
  284. CWalletTx* wtx = &((*it).second);
  285. txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
  286. }
  287. acentries.clear();
  288. walletdb.ListAccountCreditDebit(strAccount, acentries);
  289. BOOST_FOREACH(CAccountingEntry& entry, acentries)
  290. {
  291. txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
  292. }
  293. return txOrdered;
  294. }
  295. void CWallet::WalletUpdateSpent(const CTransaction &tx)
  296. {
  297. // Anytime a signature is successfully verified, it's proof the outpoint is spent.
  298. // Update the wallet spent flag if it doesn't know due to wallet.dat being
  299. // restored from backup or the user making copies of wallet.dat.
  300. {
  301. LOCK(cs_wallet);
  302. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  303. {
  304. map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
  305. if (mi != mapWallet.end())
  306. {
  307. CWalletTx& wtx = (*mi).second;
  308. if (txin.prevout.n >= wtx.vout.size())
  309. LogPrintf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
  310. else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
  311. {
  312. LogPrintf("WalletUpdateSpent found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
  313. wtx.MarkSpent(txin.prevout.n);
  314. wtx.WriteToDisk();
  315. NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
  316. }
  317. }
  318. }
  319. }
  320. }
  321. void CWallet::MarkDirty()
  322. {
  323. {
  324. LOCK(cs_wallet);
  325. BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
  326. item.second.MarkDirty();
  327. }
  328. }
  329. bool CWallet::AddToWallet(const CWalletTx& wtxIn)
  330. {
  331. uint256 hash = wtxIn.GetHash();
  332. {
  333. LOCK(cs_wallet);
  334. // Inserts only if not already there, returns tx inserted or tx found
  335. pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
  336. CWalletTx& wtx = (*ret.first).second;
  337. wtx.BindWallet(this);
  338. bool fInsertedNew = ret.second;
  339. if (fInsertedNew)
  340. {
  341. wtx.nTimeReceived = GetAdjustedTime();
  342. wtx.nOrderPos = IncOrderPosNext();
  343. wtx.nTimeSmart = wtx.nTimeReceived;
  344. if (wtxIn.hashBlock != 0)
  345. {
  346. if (mapBlockIndex.count(wtxIn.hashBlock))
  347. {
  348. unsigned int latestNow = wtx.nTimeReceived;
  349. unsigned int latestEntry = 0;
  350. {
  351. // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
  352. int64_t latestTolerated = latestNow + 300;
  353. std::list<CAccountingEntry> acentries;
  354. TxItems txOrdered = OrderedTxItems(acentries);
  355. for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
  356. {
  357. CWalletTx *const pwtx = (*it).second.first;
  358. if (pwtx == &wtx)
  359. continue;
  360. CAccountingEntry *const pacentry = (*it).second.second;
  361. int64_t nSmartTime;
  362. if (pwtx)
  363. {
  364. nSmartTime = pwtx->nTimeSmart;
  365. if (!nSmartTime)
  366. nSmartTime = pwtx->nTimeReceived;
  367. }
  368. else
  369. nSmartTime = pacentry->nTime;
  370. if (nSmartTime <= latestTolerated)
  371. {
  372. latestEntry = nSmartTime;
  373. if (nSmartTime > latestNow)
  374. latestNow = nSmartTime;
  375. break;
  376. }
  377. }
  378. }
  379. unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
  380. wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
  381. }
  382. else
  383. LogPrintf("AddToWallet() : found %s in block %s not in index\n",
  384. wtxIn.GetHash().ToString().c_str(),
  385. wtxIn.hashBlock.ToString().c_str());
  386. }
  387. }
  388. bool fUpdated = false;
  389. if (!fInsertedNew)
  390. {
  391. // Merge
  392. if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
  393. {
  394. wtx.hashBlock = wtxIn.hashBlock;
  395. fUpdated = true;
  396. }
  397. if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
  398. {
  399. wtx.vMerkleBranch = wtxIn.vMerkleBranch;
  400. wtx.nIndex = wtxIn.nIndex;
  401. fUpdated = true;
  402. }
  403. if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
  404. {
  405. wtx.fFromMe = wtxIn.fFromMe;
  406. fUpdated = true;
  407. }
  408. fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
  409. }
  410. //// debug print
  411. LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
  412. // Write to disk
  413. if (fInsertedNew || fUpdated)
  414. if (!wtx.WriteToDisk())
  415. return false;
  416. // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
  417. WalletUpdateSpent(wtx);
  418. // Notify UI of new or updated transaction
  419. NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
  420. // notify an external script when a wallet transaction comes in or is updated
  421. std::string strCmd = GetArg("-walletnotify", "");
  422. if ( !strCmd.empty())
  423. {
  424. boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
  425. boost::thread t(runCommand, strCmd); // thread runs free
  426. }
  427. }
  428. return true;
  429. }
  430. // Add a transaction to the wallet, or update it.
  431. // pblock is optional, but should be provided if the transaction is known to be in a block.
  432. // If fUpdate is true, existing transactions will be updated.
  433. bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate)
  434. {
  435. {
  436. LOCK(cs_wallet);
  437. bool fExisted = mapWallet.count(hash);
  438. if (fExisted && !fUpdate) return false;
  439. if (fExisted || IsMine(tx) || IsFromMe(tx))
  440. {
  441. CWalletTx wtx(this,tx);
  442. // Get merkle branch if transaction was found in a block
  443. if (pblock)
  444. wtx.SetMerkleBranch(pblock);
  445. return AddToWallet(wtx);
  446. }
  447. else
  448. WalletUpdateSpent(tx);
  449. }
  450. return false;
  451. }
  452. void CWallet::SyncTransaction(const uint256 &hash, const CTransaction& tx, const CBlock* pblock) {
  453. AddToWalletIfInvolvingMe(hash, tx, pblock, true);
  454. }
  455. void CWallet::EraseFromWallet(const uint256 &hash)
  456. {
  457. if (!fFileBacked)
  458. return;
  459. {
  460. LOCK(cs_wallet);
  461. if (mapWallet.erase(hash))
  462. CWalletDB(strWalletFile).EraseTx(hash);
  463. }
  464. return;
  465. }
  466. bool CWallet::IsMine(const CTxIn &txin) const
  467. {
  468. {
  469. LOCK(cs_wallet);
  470. map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
  471. if (mi != mapWallet.end())
  472. {
  473. const CWalletTx& prev = (*mi).second;
  474. if (txin.prevout.n < prev.vout.size())
  475. if (IsMine(prev.vout[txin.prevout.n]))
  476. return true;
  477. }
  478. }
  479. return false;
  480. }
  481. int64_t CWallet::GetDebit(const CTxIn &txin) const
  482. {
  483. {
  484. LOCK(cs_wallet);
  485. map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
  486. if (mi != mapWallet.end())
  487. {
  488. const CWalletTx& prev = (*mi).second;
  489. if (txin.prevout.n < prev.vout.size())
  490. if (IsMine(prev.vout[txin.prevout.n]))
  491. return prev.vout[txin.prevout.n].nValue;
  492. }
  493. }
  494. return 0;
  495. }
  496. bool CWallet::IsChange(const CTxOut& txout) const
  497. {
  498. CTxDestination address;
  499. // TODO: fix handling of 'change' outputs. The assumption is that any
  500. // payment to a TX_PUBKEYHASH that is mine but isn't in the address book
  501. // is change. That assumption is likely to break when we implement multisignature
  502. // wallets that return change back into a multi-signature-protected address;
  503. // a better way of identifying which outputs are 'the send' and which are
  504. // 'the change' will need to be implemented (maybe extend CWalletTx to remember
  505. // which output, if any, was change).
  506. if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address))
  507. {
  508. LOCK(cs_wallet);
  509. if (!mapAddressBook.count(address))
  510. return true;
  511. }
  512. return false;
  513. }
  514. int64_t CWalletTx::GetTxTime() const
  515. {
  516. int64_t n = nTimeSmart;
  517. return n ? n : nTimeReceived;
  518. }
  519. int CWalletTx::GetRequestCount() const
  520. {
  521. // Returns -1 if it wasn't being tracked
  522. int nRequests = -1;
  523. {
  524. LOCK(pwallet->cs_wallet);
  525. if (IsCoinBase())
  526. {
  527. // Generated block
  528. if (hashBlock != 0)
  529. {
  530. map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
  531. if (mi != pwallet->mapRequestCount.end())
  532. nRequests = (*mi).second;
  533. }
  534. }
  535. else
  536. {
  537. // Did anyone request this transaction?
  538. map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
  539. if (mi != pwallet->mapRequestCount.end())
  540. {
  541. nRequests = (*mi).second;
  542. // How about the block it's in?
  543. if (nRequests == 0 && hashBlock != 0)
  544. {
  545. map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
  546. if (mi != pwallet->mapRequestCount.end())
  547. nRequests = (*mi).second;
  548. else
  549. nRequests = 1; // If it's in someone else's block it must have got out
  550. }
  551. }
  552. }
  553. }
  554. return nRequests;
  555. }
  556. void CWalletTx::GetAmounts(list<pair<CTxDestination, int64_t> >& listReceived,
  557. list<pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, string& strSentAccount) const
  558. {
  559. nFee = 0;
  560. listReceived.clear();
  561. listSent.clear();
  562. strSentAccount = strFromAccount;
  563. // Compute fee:
  564. int64_t nDebit = GetDebit();
  565. if (nDebit > 0) // debit>0 means we signed/sent this transaction
  566. {
  567. int64_t nValueOut = GetValueOut();
  568. nFee = nDebit - nValueOut;
  569. }
  570. // Sent/received.
  571. BOOST_FOREACH(const CTxOut& txout, vout)
  572. {
  573. bool fIsMine;
  574. // Only need to handle txouts if AT LEAST one of these is true:
  575. // 1) they debit from us (sent)
  576. // 2) the output is to us (received)
  577. if (nDebit > 0)
  578. {
  579. // Don't report 'change' txouts
  580. if (pwallet->IsChange(txout))
  581. continue;
  582. fIsMine = pwallet->IsMine(txout);
  583. }
  584. else if (!(fIsMine = pwallet->IsMine(txout)))
  585. continue;
  586. // In either case, we need to get the destination address
  587. CTxDestination address;
  588. if (!ExtractDestination(txout.scriptPubKey, address))
  589. {
  590. LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
  591. this->GetHash().ToString().c_str());
  592. address = CNoDestination();
  593. }
  594. // If we are debited by the transaction, add the output as a "sent" entry
  595. if (nDebit > 0)
  596. listSent.push_back(make_pair(address, txout.nValue));
  597. // If we are receiving the output, add it as a "received" entry
  598. if (fIsMine)
  599. listReceived.push_back(make_pair(address, txout.nValue));
  600. }
  601. }
  602. void CWalletTx::GetAccountAmounts(const string& strAccount, int64_t& nReceived,
  603. int64_t& nSent, int64_t& nFee) const
  604. {
  605. nReceived = nSent = nFee = 0;
  606. int64_t allFee;
  607. string strSentAccount;
  608. list<pair<CTxDestination, int64_t> > listReceived;
  609. list<pair<CTxDestination, int64_t> > listSent;
  610. GetAmounts(listReceived, listSent, allFee, strSentAccount);
  611. if (strAccount == strSentAccount)
  612. {
  613. BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& s, listSent)
  614. nSent += s.second;
  615. nFee = allFee;
  616. }
  617. {
  618. LOCK(pwallet->cs_wallet);
  619. BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& r, listReceived)
  620. {
  621. if (pwallet->mapAddressBook.count(r.first))
  622. {
  623. map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
  624. if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount)
  625. nReceived += r.second;
  626. }
  627. else if (strAccount.empty())
  628. {
  629. nReceived += r.second;
  630. }
  631. }
  632. }
  633. }
  634. void CWalletTx::AddSupportingTransactions()
  635. {
  636. vtxPrev.clear();
  637. const int COPY_DEPTH = 3;
  638. if (SetMerkleBranch() < COPY_DEPTH)
  639. {
  640. vector<uint256> vWorkQueue;
  641. BOOST_FOREACH(const CTxIn& txin, vin)
  642. vWorkQueue.push_back(txin.prevout.hash);
  643. {
  644. LOCK(pwallet->cs_wallet);
  645. map<uint256, const CMerkleTx*> mapWalletPrev;
  646. set<uint256> setAlreadyDone;
  647. for (unsigned int i = 0; i < vWorkQueue.size(); i++)
  648. {
  649. uint256 hash = vWorkQueue[i];
  650. if (setAlreadyDone.count(hash))
  651. continue;
  652. setAlreadyDone.insert(hash);
  653. CMerkleTx tx;
  654. map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
  655. if (mi != pwallet->mapWallet.end())
  656. {
  657. tx = (*mi).second;
  658. BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
  659. mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
  660. }
  661. else if (mapWalletPrev.count(hash))
  662. {
  663. tx = *mapWalletPrev[hash];
  664. }
  665. int nDepth = tx.SetMerkleBranch();
  666. vtxPrev.push_back(tx);
  667. if (nDepth < COPY_DEPTH)
  668. {
  669. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  670. vWorkQueue.push_back(txin.prevout.hash);
  671. }
  672. }
  673. }
  674. }
  675. reverse(vtxPrev.begin(), vtxPrev.end());
  676. }
  677. bool CWalletTx::AcceptWalletTransaction()
  678. {
  679. {
  680. LOCK(mempool.cs);
  681. // Add previous supporting transactions first
  682. BOOST_FOREACH(CMerkleTx& tx, vtxPrev)
  683. {
  684. if (!tx.IsCoinBase())
  685. {
  686. uint256 hash = tx.GetHash();
  687. if (!mempool.exists(hash) && pcoinsTip->HaveCoins(hash))
  688. tx.AcceptToMemoryPool(false);
  689. }
  690. }
  691. return AcceptToMemoryPool(false);
  692. }
  693. return false;
  694. }
  695. bool CWalletTx::WriteToDisk()
  696. {
  697. return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
  698. }
  699. // Scan the block chain (starting in pindexStart) for transactions
  700. // from or to us. If fUpdate is true, found transactions that already
  701. // exist in the wallet will be updated.
  702. int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
  703. {
  704. int ret = 0;
  705. CBlockIndex* pindex = pindexStart;
  706. {
  707. LOCK(cs_wallet);
  708. while (pindex)
  709. {
  710. // no need to read and scan block, if block was created before
  711. // our wallet birthday (as adjusted for block time variability)
  712. if (nTimeFirstKey && (pindex->nTime < (nTimeFirstKey - 7200))) {
  713. pindex = chainActive.Next(pindex);
  714. continue;
  715. }
  716. CBlock block;
  717. ReadBlockFromDisk(block, pindex);
  718. BOOST_FOREACH(CTransaction& tx, block.vtx)
  719. {
  720. if (AddToWalletIfInvolvingMe(tx.GetHash(), tx, &block, fUpdate))
  721. ret++;
  722. }
  723. pindex = chainActive.Next(pindex);
  724. }
  725. }
  726. return ret;
  727. }
  728. void CWallet::ReacceptWalletTransactions()
  729. {
  730. bool fRepeat = true;
  731. while (fRepeat)
  732. {
  733. LOCK(cs_wallet);
  734. fRepeat = false;
  735. bool fMissing = false;
  736. BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
  737. {
  738. CWalletTx& wtx = item.second;
  739. if (wtx.IsCoinBase() && wtx.IsSpent(0))
  740. continue;
  741. CCoins coins;
  742. bool fUpdated = false;
  743. bool fFound = pcoinsTip->GetCoins(wtx.GetHash(), coins);
  744. if (fFound || wtx.GetDepthInMainChain() > 0)
  745. {
  746. // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
  747. for (unsigned int i = 0; i < wtx.vout.size(); i++)
  748. {
  749. if (wtx.IsSpent(i))
  750. continue;
  751. if ((i >= coins.vout.size() || coins.vout[i].IsNull()) && IsMine(wtx.vout[i]))
  752. {
  753. wtx.MarkSpent(i);
  754. fUpdated = true;
  755. fMissing = true;
  756. }
  757. }
  758. if (fUpdated)
  759. {
  760. LogPrintf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
  761. wtx.MarkDirty();
  762. wtx.WriteToDisk();
  763. }
  764. }
  765. else
  766. {
  767. // Re-accept any txes of ours that aren't already in a block
  768. if (!wtx.IsCoinBase())
  769. wtx.AcceptWalletTransaction();
  770. }
  771. }
  772. if (fMissing)
  773. {
  774. // TODO: optimize this to scan just part of the block chain?
  775. if (ScanForWalletTransactions(chainActive.Genesis()))
  776. fRepeat = true; // Found missing transactions: re-do re-accept.
  777. }
  778. }
  779. }
  780. void CWalletTx::RelayWalletTransaction()
  781. {
  782. BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
  783. {
  784. if (!tx.IsCoinBase())
  785. if (tx.GetDepthInMainChain() == 0)
  786. RelayTransaction((CTransaction)tx, tx.GetHash());
  787. }
  788. if (!IsCoinBase())
  789. {
  790. if (GetDepthInMainChain() == 0) {
  791. uint256 hash = GetHash();
  792. LogPrintf("Relaying wtx %s\n", hash.ToString().c_str());
  793. RelayTransaction((CTransaction)*this, hash);
  794. }
  795. }
  796. }
  797. void CWallet::ResendWalletTransactions()
  798. {
  799. // Do this infrequently and randomly to avoid giving away
  800. // that these are our transactions.
  801. if (GetTime() < nNextResend)
  802. return;
  803. bool fFirst = (nNextResend == 0);
  804. nNextResend = GetTime() + GetRand(30 * 60);
  805. if (fFirst)
  806. return;
  807. // Only do it if there's been a new block since last time
  808. if (nTimeBestReceived < nLastResend)
  809. return;
  810. nLastResend = GetTime();
  811. // Rebroadcast any of our txes that aren't in a block yet
  812. LogPrintf("ResendWalletTransactions()\n");
  813. {
  814. LOCK(cs_wallet);
  815. // Sort them in chronological order
  816. multimap<unsigned int, CWalletTx*> mapSorted;
  817. BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
  818. {
  819. CWalletTx& wtx = item.second;
  820. // Don't rebroadcast until it's had plenty of time that
  821. // it should have gotten in already by now.
  822. if (nTimeBestReceived - (int64_t)wtx.nTimeReceived > 5 * 60)
  823. mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
  824. }
  825. BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
  826. {
  827. CWalletTx& wtx = *item.second;
  828. wtx.RelayWalletTransaction();
  829. }
  830. }
  831. }
  832. //////////////////////////////////////////////////////////////////////////////
  833. //
  834. // Actions
  835. //
  836. int64_t CWallet::GetBalance() const
  837. {
  838. int64_t nTotal = 0;
  839. {
  840. LOCK(cs_wallet);
  841. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  842. {
  843. const CWalletTx* pcoin = &(*it).second;
  844. if (pcoin->IsConfirmed())
  845. nTotal += pcoin->GetAvailableCredit();
  846. }
  847. }
  848. return nTotal;
  849. }
  850. int64_t CWallet::GetUnconfirmedBalance() const
  851. {
  852. int64_t nTotal = 0;
  853. {
  854. LOCK(cs_wallet);
  855. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  856. {
  857. const CWalletTx* pcoin = &(*it).second;
  858. if (!IsFinalTx(*pcoin) || !pcoin->IsConfirmed())
  859. nTotal += pcoin->GetAvailableCredit();
  860. }
  861. }
  862. return nTotal;
  863. }
  864. int64_t CWallet::GetImmatureBalance() const
  865. {
  866. int64_t nTotal = 0;
  867. {
  868. LOCK(cs_wallet);
  869. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  870. {
  871. const CWalletTx* pcoin = &(*it).second;
  872. nTotal += pcoin->GetImmatureCredit();
  873. }
  874. }
  875. return nTotal;
  876. }
  877. // populate vCoins with vector of spendable COutputs
  878. void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const
  879. {
  880. vCoins.clear();
  881. {
  882. LOCK(cs_wallet);
  883. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  884. {
  885. const CWalletTx* pcoin = &(*it).second;
  886. if (!IsFinalTx(*pcoin))
  887. continue;
  888. if (fOnlyConfirmed && !pcoin->IsConfirmed())
  889. continue;
  890. if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
  891. continue;
  892. for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
  893. if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) &&
  894. !IsLockedCoin((*it).first, i) && pcoin->vout[i].nValue > 0 &&
  895. (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
  896. vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
  897. }
  898. }
  899. }
  900. }
  901. static void ApproximateBestSubset(vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > >vValue, int64_t nTotalLower, int64_t nTargetValue,
  902. vector<char>& vfBest, int64_t& nBest, int iterations = 1000)
  903. {
  904. vector<char> vfIncluded;
  905. vfBest.assign(vValue.size(), true);
  906. nBest = nTotalLower;
  907. seed_insecure_rand();
  908. for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
  909. {
  910. vfIncluded.assign(vValue.size(), false);
  911. int64_t nTotal = 0;
  912. bool fReachedTarget = false;
  913. for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
  914. {
  915. for (unsigned int i = 0; i < vValue.size(); i++)
  916. {
  917. //The solver here uses a randomized algorithm,
  918. //the randomness serves no real security purpose but is just
  919. //needed to prevent degenerate behavior and it is important
  920. //that the rng fast. We do not use a constant random sequence,
  921. //because there may be some privacy improvement by making
  922. //the selection random.
  923. if (nPass == 0 ? insecure_rand()&1 : !vfIncluded[i])
  924. {
  925. nTotal += vValue[i].first;
  926. vfIncluded[i] = true;
  927. if (nTotal >= nTargetValue)
  928. {
  929. fReachedTarget = true;
  930. if (nTotal < nBest)
  931. {
  932. nBest = nTotal;
  933. vfBest = vfIncluded;
  934. }
  935. nTotal -= vValue[i].first;
  936. vfIncluded[i] = false;
  937. }
  938. }
  939. }
  940. }
  941. }
  942. }
  943. bool CWallet::SelectCoinsMinConf(int64_t nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
  944. set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const
  945. {
  946. setCoinsRet.clear();
  947. nValueRet = 0;
  948. // List of values less than target
  949. pair<int64_t, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
  950. coinLowestLarger.first = std::numeric_limits<int64_t>::max();
  951. coinLowestLarger.second.first = NULL;
  952. vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > > vValue;
  953. int64_t nTotalLower = 0;
  954. random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
  955. BOOST_FOREACH(COutput output, vCoins)
  956. {
  957. const CWalletTx *pcoin = output.tx;
  958. if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
  959. continue;
  960. int i = output.i;
  961. int64_t n = pcoin->vout[i].nValue;
  962. pair<int64_t,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
  963. if (n == nTargetValue)
  964. {
  965. setCoinsRet.insert(coin.second);
  966. nValueRet += coin.first;
  967. return true;
  968. }
  969. else if (n < nTargetValue + CENT)
  970. {
  971. vValue.push_back(coin);
  972. nTotalLower += n;
  973. }
  974. else if (n < coinLowestLarger.first)
  975. {
  976. coinLowestLarger = coin;
  977. }
  978. }
  979. if (nTotalLower == nTargetValue)
  980. {
  981. for (unsigned int i = 0; i < vValue.size(); ++i)
  982. {
  983. setCoinsRet.insert(vValue[i].second);
  984. nValueRet += vValue[i].first;
  985. }
  986. return true;
  987. }
  988. if (nTotalLower < nTargetValue)
  989. {
  990. if (coinLowestLarger.second.first == NULL)
  991. return false;
  992. setCoinsRet.insert(coinLowestLarger.second);
  993. nValueRet += coinLowestLarger.first;
  994. return true;
  995. }
  996. // Solve subset sum by stochastic approximation
  997. sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
  998. vector<char> vfBest;
  999. int64_t nBest;
  1000. ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
  1001. if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
  1002. ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
  1003. // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
  1004. // or the next bigger coin is closer), return the bigger coin
  1005. if (coinLowestLarger.second.first &&
  1006. ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
  1007. {
  1008. setCoinsRet.insert(coinLowestLarger.second);
  1009. nValueRet += coinLowestLarger.first;
  1010. }
  1011. else {
  1012. for (unsigned int i = 0; i < vValue.size(); i++)
  1013. if (vfBest[i])
  1014. {
  1015. setCoinsRet.insert(vValue[i].second);
  1016. nValueRet += vValue[i].first;
  1017. }
  1018. LogPrint("selectcoins", "SelectCoins() best subset: ");
  1019. for (unsigned int i = 0; i < vValue.size(); i++)
  1020. if (vfBest[i])
  1021. LogPrint("selectcoins", "%s ", FormatMoney(vValue[i].first).c_str());
  1022. LogPrint("selectcoins", "total %s\n", FormatMoney(nBest).c_str());
  1023. }
  1024. return true;
  1025. }
  1026. bool CWallet::SelectCoins(int64_t nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl* coinControl) const
  1027. {
  1028. vector<COutput> vCoins;
  1029. AvailableCoins(vCoins, true, coinControl);
  1030. // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
  1031. if (coinControl && coinControl->HasSelected())
  1032. {
  1033. BOOST_FOREACH(const COutput& out, vCoins)
  1034. {
  1035. nValueRet += out.tx->vout[out.i].nValue;
  1036. setCoinsRet.insert(make_pair(out.tx, out.i));
  1037. }
  1038. return (nValueRet >= nTargetValue);
  1039. }
  1040. return (SelectCoinsMinConf(nTargetValue, 1, 6, vCoins, setCoinsRet, nValueRet) ||
  1041. SelectCoinsMinConf(nTargetValue, 1, 1, vCoins, setCoinsRet, nValueRet) ||
  1042. SelectCoinsMinConf(nTargetValue, 0, 1, vCoins, setCoinsRet, nValueRet));
  1043. }
  1044. bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
  1045. CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl)
  1046. {
  1047. int64_t nValue = 0;
  1048. BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend)
  1049. {
  1050. if (nValue < 0)
  1051. {
  1052. strFailReason = _("Transaction amounts must be positive");
  1053. return false;
  1054. }
  1055. nValue += s.second;
  1056. }
  1057. if (vecSend.empty() || nValue < 0)
  1058. {
  1059. strFailReason = _("Transaction amounts must be positive");
  1060. return false;
  1061. }
  1062. wtxNew.BindWallet(this);
  1063. {
  1064. LOCK2(cs_main, cs_wallet);
  1065. {
  1066. nFeeRet = nTransactionFee;
  1067. while (true)
  1068. {
  1069. wtxNew.vin.clear();
  1070. wtxNew.vout.clear();
  1071. wtxNew.fFromMe = true;
  1072. int64_t nTotalValue = nValue + nFeeRet;
  1073. double dPriority = 0;
  1074. // vouts to the payees
  1075. BOOST_FOREACH (const PAIRTYPE(CScript, int64_t)& s, vecSend)
  1076. {
  1077. CTxOut txout(s.second, s.first);
  1078. if (txout.IsDust(CTransaction::nMinRelayTxFee))
  1079. {
  1080. strFailReason = _("Transaction amount too small");
  1081. return false;
  1082. }
  1083. wtxNew.vout.push_back(txout);
  1084. }
  1085. // Choose coins to use
  1086. set<pair<const CWalletTx*,unsigned int> > setCoins;
  1087. int64_t nValueIn = 0;
  1088. if (!SelectCoins(nTotalValue, setCoins, nValueIn, coinControl))
  1089. {
  1090. strFailReason = _("Insufficient funds");
  1091. return false;
  1092. }
  1093. BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
  1094. {
  1095. int64_t nCredit = pcoin.first->vout[pcoin.second].nValue;
  1096. //The priority after the next block (depth+1) is used instead of the current,
  1097. //reflecting an assumption the user would accept a bit more delay for
  1098. //a chance at a free transaction.
  1099. dPriority += (double)nCredit * (pcoin.first->GetDepthInMainChain()+1);
  1100. }
  1101. int64_t nChange = nValueIn - nValue - nFeeRet;
  1102. // The following if statement should be removed once enough miners
  1103. // have upgraded to the 0.9 GetMinFee() rules. Until then, this avoids
  1104. // creating free transactions that have change outputs less than
  1105. // CENT bitcoins.
  1106. if (nFeeRet < CTransaction::nMinTxFee && nChange > 0 && nChange < CENT)
  1107. {
  1108. int64_t nMoveToFee = min(nChange, CTransaction::nMinTxFee - nFeeRet);
  1109. nChange -= nMoveToFee;
  1110. nFeeRet += nMoveToFee;
  1111. }
  1112. if (nChange > 0)
  1113. {
  1114. // Fill a vout to ourself
  1115. // TODO: pass in scriptChange instead of reservekey so
  1116. // change transaction isn't always pay-to-bitcoin-address
  1117. CScript scriptChange;
  1118. // coin control: send change to custom address
  1119. if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange))
  1120. scriptChange.SetDestination(coinControl->destChange);
  1121. // no coin control: send change to newly generated address
  1122. else
  1123. {
  1124. // Note: We use a new key here to keep it from being obvious which side is the change.
  1125. // The drawback is that by not reusing a previous key, the change may be lost if a
  1126. // backup is restored, if the backup doesn't have the new private key for the change.
  1127. // If we reused the old key, it would be possible to add code to look for and
  1128. // rediscover unknown transactions that were written with keys of ours to recover
  1129. // post-backup change.
  1130. // Reserve a new key pair from key pool
  1131. CPubKey vchPubKey;
  1132. bool ret;
  1133. ret = reservekey.GetReservedKey(vchPubKey);
  1134. assert(ret); // should never fail, as we just unlocked
  1135. scriptChange.SetDestination(vchPubKey.GetID());
  1136. }
  1137. CTxOut newTxOut(nChange, scriptChange);
  1138. // Never create dust outputs; if we would, just
  1139. // add the dust to the fee.
  1140. if (newTxOut.IsDust(CTransaction::nMinRelayTxFee))
  1141. {
  1142. nFeeRet += nChange;
  1143. reservekey.ReturnKey();
  1144. }
  1145. else
  1146. {
  1147. // Insert change txn at random position:
  1148. vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size()+1);
  1149. wtxNew.vout.insert(position, newTxOut);
  1150. }
  1151. }
  1152. else
  1153. reservekey.ReturnKey();
  1154. // Fill vin
  1155. BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
  1156. wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
  1157. // Sign
  1158. int nIn = 0;
  1159. BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
  1160. if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
  1161. {
  1162. strFailReason = _("Signing transaction failed");
  1163. return false;
  1164. }
  1165. // Limit size
  1166. unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
  1167. if (nBytes >= MAX_STANDARD_TX_SIZE)
  1168. {
  1169. strFailReason = _("Transaction too large");
  1170. return false;
  1171. }
  1172. dPriority = wtxNew.ComputePriority(dPriority, nBytes);
  1173. // Check that enough fee is included
  1174. int64_t nPayFee = nTransactionFee * (1 + (int64_t)nBytes / 1000);
  1175. bool fAllowFree = AllowFree(dPriority);
  1176. int64_t nMinFee = GetMinFee(wtxNew, nBytes, fAllowFree, GMF_SEND);
  1177. if (nFeeRet < max(nPayFee, nMinFee))
  1178. {
  1179. nFeeRet = max(nPayFee, nMinFee);
  1180. continue;
  1181. }
  1182. // Fill vtxPrev by copying from previous transactions vtxPrev
  1183. wtxNew.AddSupportingTransactions();
  1184. wtxNew.fTimeReceivedIsTxTime = true;
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. return true;
  1190. }
  1191. bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue,
  1192. CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl* coinControl)
  1193. {
  1194. vector< pair<CScript, int64_t> > vecSend;
  1195. vecSend.push_back(make_pair(scriptPubKey, nValue));
  1196. return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, strFailReason, coinControl);
  1197. }
  1198. // Call after CreateTransaction unless you want to abort
  1199. bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
  1200. {
  1201. {
  1202. LOCK2(cs_main, cs_wallet);
  1203. LogPrintf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
  1204. {
  1205. // This is only to keep the database open to defeat the auto-flush for the
  1206. // duration of this scope. This is the only place where this optimization
  1207. // maybe makes sense; please don't do it anywhere else.
  1208. CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
  1209. // Take key pair from key pool so it won't be used again
  1210. reservekey.KeepKey();
  1211. // Add tx to wallet, because if it has change it's also ours,
  1212. // otherwise just for transaction history.
  1213. AddToWallet(wtxNew);
  1214. // Mark old coins as spent
  1215. set<CWalletTx*> setCoins;
  1216. BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
  1217. {
  1218. CWalletTx &coin = mapWallet[txin.prevout.hash];
  1219. coin.BindWallet(this);
  1220. coin.MarkSpent(txin.prevout.n);
  1221. coin.WriteToDisk();
  1222. NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
  1223. }
  1224. if (fFileBacked)
  1225. delete pwalletdb;
  1226. }
  1227. // Track how many getdata requests our transaction gets
  1228. mapRequestCount[wtxNew.GetHash()] = 0;
  1229. // Broadcast
  1230. if (!wtxNew.AcceptToMemoryPool(false))
  1231. {
  1232. // This must not fail. The transaction has already been signed and recorded.
  1233. LogPrintf("CommitTransaction() : Error: Transaction not valid");
  1234. return false;
  1235. }
  1236. wtxNew.RelayWalletTransaction();
  1237. }
  1238. return true;
  1239. }
  1240. string CWallet::SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee)
  1241. {
  1242. CReserveKey reservekey(this);
  1243. int64_t nFeeRequired;
  1244. if (IsLocked())
  1245. {
  1246. string strError = _("Error: Wallet locked, unable to create transaction!");
  1247. LogPrintf("SendMoney() : %s", strError.c_str());
  1248. return strError;
  1249. }
  1250. string strError;
  1251. if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
  1252. {
  1253. if (nValue + nFeeRequired > GetBalance())
  1254. strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!"), FormatMoney(nFeeRequired).c_str());
  1255. LogPrintf("SendMoney() : %s\n", strError.c_str());
  1256. return strError;
  1257. }
  1258. if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired))
  1259. return "ABORTED";
  1260. if (!CommitTransaction(wtxNew, reservekey))
  1261. return _("Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
  1262. return "";
  1263. }
  1264. string CWallet::SendMoneyToDestination(const CTxDestination& address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee)
  1265. {
  1266. // Check amount
  1267. if (nValue <= 0)
  1268. return _("Invalid amount");
  1269. if (nValue + nTransactionFee > GetBalance())
  1270. return _("Insufficient funds");
  1271. // Parse Bitcoin address
  1272. CScript scriptPubKey;
  1273. scriptPubKey.SetDestination(address);
  1274. return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
  1275. }
  1276. DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
  1277. {
  1278. if (!fFileBacked)
  1279. return DB_LOAD_OK;
  1280. fFirstRunRet = false;
  1281. DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
  1282. if (nLoadWalletRet == DB_NEED_REWRITE)
  1283. {
  1284. if (CDB::Rewrite(strWalletFile, "\x04pool"))
  1285. {
  1286. setKeyPool.clear();
  1287. // Note: can't top-up keypool here, because wallet is locked.
  1288. // User will be prompted to unlock wallet the next operation
  1289. // the requires a new key.
  1290. }
  1291. }
  1292. if (nLoadWalletRet != DB_LOAD_OK)
  1293. return nLoadWalletRet;
  1294. fFirstRunRet = !vchDefaultKey.IsValid();
  1295. return DB_LOAD_OK;
  1296. }
  1297. bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose)
  1298. {
  1299. std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
  1300. mapAddressBook[address].name = strName;
  1301. if (!strPurpose.empty()) /* update purpose only if requested */
  1302. mapAddressBook[address].purpose = strPurpose;
  1303. NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address),
  1304. mapAddressBook[address].purpose,
  1305. (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
  1306. if (!fFileBacked)
  1307. return false;
  1308. if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose))
  1309. return false;
  1310. return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName);
  1311. }
  1312. bool CWallet::DelAddressBook(const CTxDestination& address)
  1313. {
  1314. mapAddressBook.erase(address);
  1315. NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), "", CT_DELETED);
  1316. if (!fFileBacked)
  1317. return false;
  1318. CWalletDB(strWalletFile).ErasePurpose(CBitcoinAddress(address).ToString());
  1319. return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString());
  1320. }
  1321. bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
  1322. {
  1323. if (fFileBacked)
  1324. {
  1325. if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
  1326. return false;
  1327. }
  1328. vchDefaultKey = vchPubKey;
  1329. return true;
  1330. }
  1331. //
  1332. // Mark old keypool keys as used,
  1333. // and generate all new keys
  1334. //
  1335. bool CWallet::NewKeyPool()
  1336. {
  1337. {
  1338. LOCK(cs_wallet);
  1339. CWalletDB walletdb(strWalletFile);
  1340. BOOST_FOREACH(int64_t nIndex, setKeyPool)
  1341. walletdb.ErasePool(nIndex);
  1342. setKeyPool.clear();
  1343. if (IsLocked())
  1344. return false;
  1345. int64_t nKeys = max(GetArg("-keypool", 100), (int64_t)0);
  1346. for (int i = 0; i < nKeys; i++)
  1347. {
  1348. int64_t nIndex = i+1;
  1349. walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
  1350. setKeyPool.insert(nIndex);
  1351. }
  1352. LogPrintf("CWallet::NewKeyPool wrote %"PRId64" new keys\n", nKeys);
  1353. }
  1354. return true;
  1355. }
  1356. bool CWallet::TopUpKeyPool(unsigned int kpSize)
  1357. {
  1358. {
  1359. LOCK(cs_wallet);
  1360. if (IsLocked())
  1361. return false;
  1362. CWalletDB walletdb(strWalletFile);
  1363. // Top up key pool
  1364. unsigned int nTargetSize;
  1365. if (kpSize > 0)
  1366. nTargetSize = kpSize;
  1367. else
  1368. nTargetSize = max(GetArg("-keypool", 100), (int64_t) 0);
  1369. while (setKeyPool.size() < (nTargetSize + 1))
  1370. {
  1371. int64_t nEnd = 1;
  1372. if (!setKeyPool.empty())
  1373. nEnd = *(--setKeyPool.end()) + 1;
  1374. if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
  1375. throw runtime_error("TopUpKeyPool() : writing generated key failed");
  1376. setKeyPool.insert(nEnd);
  1377. LogPrintf("keypool added key %"PRId64", size=%"PRIszu"\n", nEnd, setKeyPool.size());
  1378. }
  1379. }
  1380. return true;
  1381. }
  1382. void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
  1383. {
  1384. nIndex = -1;
  1385. keypool.vchPubKey = CPubKey();
  1386. {
  1387. LOCK(cs_wallet);
  1388. if (!IsLocked())
  1389. TopUpKeyPool();
  1390. // Get the oldest key
  1391. if(setKeyPool.empty())
  1392. return;
  1393. CWalletDB walletdb(strWalletFile);
  1394. nIndex = *(setKeyPool.begin());
  1395. setKeyPool.erase(setKeyPool.begin());
  1396. if (!walletdb.ReadPool(nIndex, keypool))
  1397. throw runtime_error("ReserveKeyFromKeyPool() : read failed");
  1398. if (!HaveKey(keypool.vchPubKey.GetID()))
  1399. throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
  1400. assert(keypool.vchPubKey.IsValid());
  1401. LogPrintf("keypool reserve %"PRId64"\n", nIndex);
  1402. }
  1403. }
  1404. int64_t CWallet::AddReserveKey(const CKeyPool& keypool)
  1405. {
  1406. {
  1407. LOCK2(cs_main, cs_wallet);
  1408. CWalletDB walletdb(strWalletFile);
  1409. int64_t nIndex = 1 + *(--setKeyPool.end());
  1410. if (!walletdb.WritePool(nIndex, keypool))
  1411. throw runtime_error("AddReserveKey() : writing added key failed");
  1412. setKeyPool.insert(nIndex);
  1413. return nIndex;
  1414. }
  1415. return -1;
  1416. }
  1417. void CWallet::KeepKey(int64_t nIndex)
  1418. {
  1419. // Remove from key pool
  1420. if (fFileBacked)
  1421. {
  1422. CWalletDB walletdb(strWalletFile);
  1423. walletdb.ErasePool(nIndex);
  1424. }
  1425. LogPrintf("keypool keep %"PRId64"\n", nIndex);
  1426. }
  1427. void CWallet::ReturnKey(int64_t nIndex)
  1428. {
  1429. // Return to key pool
  1430. {
  1431. LOCK(cs_wallet);
  1432. setKeyPool.insert(nIndex);
  1433. }
  1434. LogPrintf("keypool return %"PRId64"\n", nIndex);
  1435. }
  1436. bool CWallet::GetKeyFromPool(CPubKey& result)
  1437. {
  1438. int64_t nIndex = 0;
  1439. CKeyPool keypool;
  1440. {
  1441. LOCK(cs_wallet);
  1442. ReserveKeyFromKeyPool(nIndex, keypool);
  1443. if (nIndex == -1)
  1444. {
  1445. if (IsLocked()) return false;
  1446. result = GenerateNewKey();
  1447. return true;
  1448. }
  1449. KeepKey(nIndex);
  1450. result = keypool.vchPubKey;
  1451. }
  1452. return true;
  1453. }
  1454. int64_t CWallet::GetOldestKeyPoolTime()
  1455. {
  1456. int64_t nIndex = 0;
  1457. CKeyPool keypool;
  1458. ReserveKeyFromKeyPool(nIndex, keypool);
  1459. if (nIndex == -1)
  1460. return GetTime();
  1461. ReturnKey(nIndex);
  1462. return keypool.nTime;
  1463. }
  1464. std::map<CTxDestination, int64_t> CWallet::GetAddressBalances()
  1465. {
  1466. map<CTxDestination, int64_t> balances;
  1467. {
  1468. LOCK(cs_wallet);
  1469. BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
  1470. {
  1471. CWalletTx *pcoin = &walletEntry.second;
  1472. if (!IsFinalTx(*pcoin) || !pcoin->IsConfirmed())
  1473. continue;
  1474. if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
  1475. continue;
  1476. int nDepth = pcoin->GetDepthInMainChain();
  1477. if (nDepth < (pcoin->IsFromMe() ? 0 : 1))
  1478. continue;
  1479. for (unsigned int i = 0; i < pcoin->vout.size(); i++)
  1480. {
  1481. CTxDestination addr;
  1482. if (!IsMine(pcoin->vout[i]))
  1483. continue;
  1484. if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
  1485. continue;
  1486. int64_t n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
  1487. if (!balances.count(addr))
  1488. balances[addr] = 0;
  1489. balances[addr] += n;
  1490. }
  1491. }
  1492. }
  1493. return balances;
  1494. }
  1495. set< set<CTxDestination> > CWallet::GetAddressGroupings()
  1496. {
  1497. set< set<CTxDestination> > groupings;
  1498. set<CTxDestination> grouping;
  1499. BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
  1500. {
  1501. CWalletTx *pcoin = &walletEntry.second;
  1502. if (pcoin->vin.size() > 0)
  1503. {
  1504. bool any_mine = false;
  1505. // group all input addresses with each other
  1506. BOOST_FOREACH(CTxIn txin, pcoin->vin)
  1507. {
  1508. CTxDestination address;
  1509. if(!IsMine(txin)) /* If this input isn't mine, ignore it */
  1510. continue;
  1511. if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
  1512. continue;
  1513. grouping.insert(address);
  1514. any_mine = true;
  1515. }
  1516. // group change with input addresses
  1517. if (any_mine)
  1518. {
  1519. BOOST_FOREACH(CTxOut txout, pcoin->vout)
  1520. if (IsChange(txout))
  1521. {
  1522. CTxDestination txoutAddr;
  1523. if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
  1524. continue;
  1525. grouping.insert(txoutAddr);
  1526. }
  1527. }
  1528. if (grouping.size() > 0)
  1529. {
  1530. groupings.insert(grouping);
  1531. grouping.clear();
  1532. }
  1533. }
  1534. // group lone addrs by themselves
  1535. for (unsigned int i = 0; i < pcoin->vout.size(); i++)
  1536. if (IsMine(pcoin->vout[i]))
  1537. {
  1538. CTxDestination address;
  1539. if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
  1540. continue;
  1541. grouping.insert(address);
  1542. groupings.insert(grouping);
  1543. grouping.clear();
  1544. }
  1545. }
  1546. set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
  1547. map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it
  1548. BOOST_FOREACH(set<CTxDestination> grouping, groupings)
  1549. {
  1550. // make a set of all the groups hit by this new group
  1551. set< set<CTxDestination>* > hits;
  1552. map< CTxDestination, set<CTxDestination>* >::iterator it;
  1553. BOOST_FOREACH(CTxDestination address, grouping)
  1554. if ((it = setmap.find(address)) != setmap.end())
  1555. hits.insert((*it).second);
  1556. // merge all hit groups into a new single group and delete old groups
  1557. set<CTxDestination>* merged = new set<CTxDestination>(grouping);
  1558. BOOST_FOREACH(set<CTxDestination>* hit, hits)
  1559. {
  1560. merged->insert(hit->begin(), hit->end());
  1561. uniqueGroupings.erase(hit);
  1562. delete hit;
  1563. }
  1564. uniqueGroupings.insert(merged);
  1565. // update setmap
  1566. BOOST_FOREACH(CTxDestination element, *merged)
  1567. setmap[element] = merged;
  1568. }
  1569. set< set<CTxDestination> > ret;
  1570. BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
  1571. {
  1572. ret.insert(*uniqueGrouping);
  1573. delete uniqueGrouping;
  1574. }
  1575. return ret;
  1576. }
  1577. set<CTxDestination> CWallet::GetAccountAddresses(string strAccount) const
  1578. {
  1579. set<CTxDestination> result;
  1580. BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, mapAddressBook)
  1581. {
  1582. const CTxDestination& address = item.first;
  1583. const string& strName = item.second.name;
  1584. if (strName == strAccount)
  1585. result.insert(address);
  1586. }
  1587. return result;
  1588. }
  1589. bool CReserveKey::GetReservedKey(CPubKey& pubkey)
  1590. {
  1591. if (nIndex == -1)
  1592. {
  1593. CKeyPool keypool;
  1594. pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
  1595. if (nIndex != -1)
  1596. vchPubKey = keypool.vchPubKey;
  1597. else {
  1598. if (pwallet->vchDefaultKey.IsValid()) {
  1599. LogPrintf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
  1600. vchPubKey = pwallet->vchDefaultKey;
  1601. } else
  1602. return false;
  1603. }
  1604. }
  1605. assert(vchPubKey.IsValid());
  1606. pubkey = vchPubKey;
  1607. return true;
  1608. }
  1609. void CReserveKey::KeepKey()
  1610. {
  1611. if (nIndex != -1)
  1612. pwallet->KeepKey(nIndex);
  1613. nIndex = -1;
  1614. vchPubKey = CPubKey();
  1615. }
  1616. void CReserveKey::ReturnKey()
  1617. {
  1618. if (nIndex != -1)
  1619. pwallet->ReturnKey(nIndex);
  1620. nIndex = -1;
  1621. vchPubKey = CPubKey();
  1622. }
  1623. void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
  1624. {
  1625. setAddress.clear();
  1626. CWalletDB walletdb(strWalletFile);
  1627. LOCK2(cs_main, cs_wallet);
  1628. BOOST_FOREACH(const int64_t& id, setKeyPool)
  1629. {
  1630. CKeyPool keypool;
  1631. if (!walletdb.ReadPool(id, keypool))
  1632. throw runtime_error("GetAllReserveKeyHashes() : read failed");
  1633. assert(keypool.vchPubKey.IsValid());
  1634. CKeyID keyID = keypool.vchPubKey.GetID();
  1635. if (!HaveKey(keyID))
  1636. throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
  1637. setAddress.insert(keyID);
  1638. }
  1639. }
  1640. void CWallet::UpdatedTransaction(const uint256 &hashTx)
  1641. {
  1642. {
  1643. LOCK(cs_wallet);
  1644. // Only notify UI if this transaction is in this wallet
  1645. map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
  1646. if (mi != mapWallet.end())
  1647. NotifyTransactionChanged(this, hashTx, CT_UPDATED);
  1648. }
  1649. }
  1650. void CWallet::LockCoin(COutPoint& output)
  1651. {
  1652. setLockedCoins.insert(output);
  1653. }
  1654. void CWallet::UnlockCoin(COutPoint& output)
  1655. {
  1656. setLockedCoins.erase(output);
  1657. }
  1658. void CWallet::UnlockAllCoins()
  1659. {
  1660. setLockedCoins.clear();
  1661. }
  1662. bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
  1663. {
  1664. COutPoint outpt(hash, n);
  1665. return (setLockedCoins.count(outpt) > 0);
  1666. }
  1667. void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts)
  1668. {
  1669. for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
  1670. it != setLockedCoins.end(); it++) {
  1671. COutPoint outpt = (*it);
  1672. vOutpts.push_back(outpt);
  1673. }
  1674. }
  1675. void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
  1676. mapKeyBirth.clear();
  1677. // get birth times for keys with metadata
  1678. for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
  1679. if (it->second.nCreateTime)
  1680. mapKeyBirth[it->first] = it->second.nCreateTime;
  1681. // map in which we'll infer heights of other keys
  1682. CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganised; use a 144-block safety margin
  1683. std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
  1684. std::set<CKeyID> setKeys;
  1685. GetKeys(setKeys);
  1686. BOOST_FOREACH(const CKeyID &keyid, setKeys) {
  1687. if (mapKeyBirth.count(keyid) == 0)
  1688. mapKeyFirstBlock[keyid] = pindexMax;
  1689. }
  1690. setKeys.clear();
  1691. // if there are no such keys, we're done
  1692. if (mapKeyFirstBlock.empty())
  1693. return;
  1694. // find first block that affects those keys, if there are any left
  1695. std::vector<CKeyID> vAffected;
  1696. for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
  1697. // iterate over all wallet transactions...
  1698. const CWalletTx &wtx = (*it).second;
  1699. std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
  1700. if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) {
  1701. // ... which are already in a block
  1702. int nHeight = blit->second->nHeight;
  1703. BOOST_FOREACH(const CTxOut &txout, wtx.vout) {
  1704. // iterate over all their outputs
  1705. ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected);
  1706. BOOST_FOREACH(const CKeyID &keyid, vAffected) {
  1707. // ... and all their affected keys
  1708. std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
  1709. if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
  1710. rit->second = blit->second;
  1711. }
  1712. vAffected.clear();
  1713. }
  1714. }
  1715. }
  1716. // Extract block timestamps for those keys
  1717. for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
  1718. mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off
  1719. }