wallet.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "txdb.h"
  3. #include "wallet.h"
  4. #include "walletdb.h"
  5. #include "crypter.h"
  6. #include "ui_interface.h"
  7. #include "base58.h"
  8. #include "kernel.h"
  9. #include "coincontrol.h"
  10. #include <boost/algorithm/string/replace.hpp>
  11. using namespace std;
  12. extern int nStakeMaxAge;
  13. //////////////////////////////////////////////////////////////////////////////
  14. //
  15. // mapWallet
  16. //
  17. struct CompareValueOnly
  18. {
  19. bool operator()(const pair<int64, pair<const CWalletTx*, unsigned int> >& t1,
  20. const pair<int64, 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 key;
  30. key.MakeNewKey(fCompressed);
  31. // Compressed public keys were introduced in version 0.6.0
  32. if (fCompressed)
  33. SetMinVersion(FEATURE_COMPRPUBKEY);
  34. CPubKey pubkey = key.GetPubKey();
  35. // Create new metadata
  36. int64 nCreationTime = GetTime();
  37. mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
  38. if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
  39. nTimeFirstKey = nCreationTime;
  40. if (!AddKey(key))
  41. throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
  42. return key.GetPubKey();
  43. }
  44. bool CWallet::AddKey(const CKey& key)
  45. {
  46. CPubKey pubkey = key.GetPubKey();
  47. if (!CCryptoKeyStore::AddKey(key))
  48. return false;
  49. if (!fFileBacked)
  50. return true;
  51. if (!IsCrypted())
  52. return CWalletDB(strWalletFile).WriteKey(pubkey, key.GetPrivKey(), mapKeyMetadata[pubkey.GetID()]);
  53. return true;
  54. }
  55. bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
  56. {
  57. if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
  58. return false;
  59. if (!fFileBacked)
  60. return true;
  61. {
  62. LOCK(cs_wallet);
  63. if (pwalletdbEncryption)
  64. return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
  65. else
  66. return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
  67. }
  68. return false;
  69. }
  70. bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
  71. {
  72. if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
  73. nTimeFirstKey = meta.nCreateTime;
  74. mapKeyMetadata[pubkey.GetID()] = meta;
  75. return true;
  76. }
  77. bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
  78. {
  79. return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
  80. }
  81. bool CWallet::AddCScript(const CScript& redeemScript)
  82. {
  83. if (!CCryptoKeyStore::AddCScript(redeemScript))
  84. return false;
  85. if (!fFileBacked)
  86. return true;
  87. return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
  88. }
  89. // ecoin: optional setting to unlock wallet for block minting only;
  90. // serves to disable the trivial sendmoney when OS account compromised
  91. bool fWalletUnlockStakingOnly = false;
  92. bool CWallet::Unlock(const SecureString& strWalletPassphrase)
  93. {
  94. if (!IsLocked())
  95. return false;
  96. CCrypter crypter;
  97. CKeyingMaterial vMasterKey;
  98. {
  99. LOCK(cs_wallet);
  100. BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
  101. {
  102. if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
  103. return false;
  104. if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
  105. return false;
  106. if (CCryptoKeyStore::Unlock(vMasterKey))
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
  113. {
  114. bool fWasLocked = IsLocked();
  115. {
  116. LOCK(cs_wallet);
  117. Lock();
  118. CCrypter crypter;
  119. CKeyingMaterial vMasterKey;
  120. BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
  121. {
  122. if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
  123. return false;
  124. if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
  125. return false;
  126. if (CCryptoKeyStore::Unlock(vMasterKey))
  127. {
  128. int64 nStartTime = GetTimeMillis();
  129. crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
  130. pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
  131. nStartTime = GetTimeMillis();
  132. crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
  133. pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
  134. if (pMasterKey.second.nDeriveIterations < 25000)
  135. pMasterKey.second.nDeriveIterations = 25000;
  136. printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
  137. if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
  138. return false;
  139. if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
  140. return false;
  141. CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
  142. if (fWasLocked)
  143. Lock();
  144. return true;
  145. }
  146. }
  147. }
  148. return false;
  149. }
  150. void CWallet::SetBestChain(const CBlockLocator& loc)
  151. {
  152. CWalletDB walletdb(strWalletFile);
  153. walletdb.WriteBestBlock(loc);
  154. }
  155. // This class implements an addrIncoming entry that causes pre-0.4
  156. // clients to crash on startup if reading a private-key-encrypted wallet.
  157. class CCorruptAddress
  158. {
  159. public:
  160. IMPLEMENT_SERIALIZE
  161. (
  162. if (nType & SER_DISK)
  163. READWRITE(nVersion);
  164. )
  165. };
  166. bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
  167. {
  168. if (nWalletVersion >= nVersion)
  169. return true;
  170. // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
  171. if (fExplicit && nVersion > nWalletMaxVersion)
  172. nVersion = FEATURE_LATEST;
  173. nWalletVersion = nVersion;
  174. if (nVersion > nWalletMaxVersion)
  175. nWalletMaxVersion = nVersion;
  176. if (fFileBacked)
  177. {
  178. CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
  179. if (nWalletVersion >= 40000)
  180. {
  181. // Versions prior to 0.4.0 did not support the "minversion" record.
  182. // Use a CCorruptAddress to make them crash instead.
  183. CCorruptAddress corruptAddress;
  184. pwalletdb->WriteSetting("addrIncoming", corruptAddress);
  185. }
  186. if (nWalletVersion > 40000)
  187. pwalletdb->WriteMinVersion(nWalletVersion);
  188. if (!pwalletdbIn)
  189. delete pwalletdb;
  190. }
  191. return true;
  192. }
  193. bool CWallet::SetMaxVersion(int nVersion)
  194. {
  195. // cannot downgrade below current version
  196. if (nWalletVersion > nVersion)
  197. return false;
  198. nWalletMaxVersion = nVersion;
  199. return true;
  200. }
  201. bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
  202. {
  203. if (IsCrypted())
  204. return false;
  205. CKeyingMaterial vMasterKey;
  206. RandAddSeedPerfmon();
  207. vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
  208. RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
  209. //CMasterKey kMasterKey(nDerivationMethodIndex);
  210. CMasterKey kMasterKey;
  211. RandAddSeedPerfmon();
  212. kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
  213. RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
  214. CCrypter crypter;
  215. int64 nStartTime = GetTimeMillis();
  216. crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
  217. kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
  218. nStartTime = GetTimeMillis();
  219. crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
  220. kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
  221. if (kMasterKey.nDeriveIterations < 25000)
  222. kMasterKey.nDeriveIterations = 25000;
  223. printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
  224. if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
  225. return false;
  226. if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
  227. return false;
  228. {
  229. LOCK(cs_wallet);
  230. mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
  231. if (fFileBacked)
  232. {
  233. pwalletdbEncryption = new CWalletDB(strWalletFile);
  234. if (!pwalletdbEncryption->TxnBegin())
  235. return false;
  236. pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
  237. }
  238. if (!EncryptKeys(vMasterKey))
  239. {
  240. if (fFileBacked)
  241. pwalletdbEncryption->TxnAbort();
  242. 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.
  243. }
  244. // Encryption was introduced in version 0.4.0
  245. SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
  246. if (fFileBacked)
  247. {
  248. if (!pwalletdbEncryption->TxnCommit())
  249. 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.
  250. delete pwalletdbEncryption;
  251. pwalletdbEncryption = NULL;
  252. }
  253. Lock();
  254. Unlock(strWalletPassphrase);
  255. NewKeyPool();
  256. Lock();
  257. // Need to completely rewrite the wallet file; if we don't, bdb might keep
  258. // bits of the unencrypted private key in slack space in the database file.
  259. CDB::Rewrite(strWalletFile);
  260. }
  261. NotifyStatusChanged(this);
  262. return true;
  263. }
  264. int64 CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
  265. {
  266. int64 nRet = nOrderPosNext++;
  267. if (pwalletdb) {
  268. pwalletdb->WriteOrderPosNext(nOrderPosNext);
  269. } else {
  270. CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
  271. }
  272. return nRet;
  273. }
  274. CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
  275. {
  276. CWalletDB walletdb(strWalletFile);
  277. // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
  278. TxItems txOrdered;
  279. // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
  280. // would make this much faster for applications that do this a lot.
  281. for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  282. {
  283. CWalletTx* wtx = &((*it).second);
  284. txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
  285. }
  286. acentries.clear();
  287. walletdb.ListAccountCreditDebit(strAccount, acentries);
  288. BOOST_FOREACH(CAccountingEntry& entry, acentries)
  289. {
  290. txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
  291. }
  292. return txOrdered;
  293. }
  294. void CWallet::WalletUpdateSpent(const CTransaction &tx, bool fBlock)
  295. {
  296. // Anytime a signature is successfully verified, it's proof the outpoint is spent.
  297. // Update the wallet spent flag if it doesn't know due to wallet.dat being
  298. // restored from backup or the user making copies of wallet.dat.
  299. {
  300. LOCK(cs_wallet);
  301. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  302. {
  303. map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
  304. if (mi != mapWallet.end())
  305. {
  306. CWalletTx& wtx = (*mi).second;
  307. if (txin.prevout.n >= wtx.vout.size())
  308. printf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
  309. else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
  310. {
  311. printf("WalletUpdateSpent found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
  312. wtx.MarkSpent(txin.prevout.n);
  313. wtx.WriteToDisk();
  314. NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
  315. }
  316. }
  317. }
  318. if (fBlock)
  319. {
  320. uint256 hash = tx.GetHash();
  321. map<uint256, CWalletTx>::iterator mi = mapWallet.find(hash);
  322. CWalletTx& wtx = (*mi).second;
  323. BOOST_FOREACH(const CTxOut& txout, tx.vout)
  324. {
  325. if (IsMine(txout))
  326. {
  327. wtx.MarkUnspent(&txout - &tx.vout[0]);
  328. wtx.WriteToDisk();
  329. NotifyTransactionChanged(this, hash, CT_UPDATED);
  330. }
  331. }
  332. }
  333. }
  334. }
  335. void CWallet::MarkDirty()
  336. {
  337. {
  338. LOCK(cs_wallet);
  339. BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
  340. item.second.MarkDirty();
  341. }
  342. }
  343. bool CWallet::AddToWallet(const CWalletTx& wtxIn)
  344. {
  345. uint256 hash = wtxIn.GetHash();
  346. {
  347. LOCK(cs_wallet);
  348. // Inserts only if not already there, returns tx inserted or tx found
  349. pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
  350. CWalletTx& wtx = (*ret.first).second;
  351. wtx.BindWallet(this);
  352. bool fInsertedNew = ret.second;
  353. if (fInsertedNew)
  354. {
  355. wtx.nTimeReceived = GetAdjustedTime();
  356. wtx.nOrderPos = IncOrderPosNext();
  357. wtx.nTimeSmart = wtx.nTimeReceived;
  358. if (wtxIn.hashBlock != 0)
  359. {
  360. if (mapBlockIndex.count(wtxIn.hashBlock))
  361. {
  362. unsigned int latestNow = wtx.nTimeReceived;
  363. unsigned int latestEntry = 0;
  364. {
  365. // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
  366. int64 latestTolerated = latestNow + 300;
  367. std::list<CAccountingEntry> acentries;
  368. TxItems txOrdered = OrderedTxItems(acentries);
  369. for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
  370. {
  371. CWalletTx *const pwtx = (*it).second.first;
  372. if (pwtx == &wtx)
  373. continue;
  374. CAccountingEntry *const pacentry = (*it).second.second;
  375. int64 nSmartTime;
  376. if (pwtx)
  377. {
  378. nSmartTime = pwtx->nTimeSmart;
  379. if (!nSmartTime)
  380. nSmartTime = pwtx->nTimeReceived;
  381. }
  382. else
  383. nSmartTime = pacentry->nTime;
  384. if (nSmartTime <= latestTolerated)
  385. {
  386. latestEntry = nSmartTime;
  387. if (nSmartTime > latestNow)
  388. latestNow = nSmartTime;
  389. break;
  390. }
  391. }
  392. }
  393. unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
  394. wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
  395. }
  396. else
  397. printf("AddToWallet() : found %s in block %s not in index\n",
  398. wtxIn.GetHash().ToString().substr(0,10).c_str(),
  399. wtxIn.hashBlock.ToString().c_str());
  400. }
  401. }
  402. bool fUpdated = false;
  403. if (!fInsertedNew)
  404. {
  405. // Merge
  406. if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
  407. {
  408. wtx.hashBlock = wtxIn.hashBlock;
  409. fUpdated = true;
  410. }
  411. if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
  412. {
  413. wtx.vMerkleBranch = wtxIn.vMerkleBranch;
  414. wtx.nIndex = wtxIn.nIndex;
  415. fUpdated = true;
  416. }
  417. if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
  418. {
  419. wtx.fFromMe = wtxIn.fFromMe;
  420. fUpdated = true;
  421. }
  422. fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
  423. }
  424. //// debug print
  425. printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,10).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
  426. // Write to disk
  427. if (fInsertedNew || fUpdated)
  428. if (!wtx.WriteToDisk())
  429. return false;
  430. #ifndef QT_GUI
  431. // If default receiving address gets used, replace it with a new one
  432. CScript scriptDefaultKey;
  433. scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
  434. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  435. {
  436. if (txout.scriptPubKey == scriptDefaultKey)
  437. {
  438. CPubKey newDefaultKey;
  439. if (GetKeyFromPool(newDefaultKey, false))
  440. {
  441. SetDefaultKey(newDefaultKey);
  442. SetAddressBookName(vchDefaultKey.GetID(), "");
  443. }
  444. }
  445. }
  446. #endif
  447. // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
  448. WalletUpdateSpent(wtx, (wtxIn.hashBlock != 0));
  449. // Notify UI of new or updated transaction
  450. NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
  451. // notify an external script when a wallet transaction comes in or is updated
  452. std::string strCmd = GetArg("-walletnotify", "");
  453. if ( !strCmd.empty())
  454. {
  455. boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
  456. boost::thread t(runCommand, strCmd); // thread runs free
  457. }
  458. }
  459. return true;
  460. }
  461. // Add a transaction to the wallet, or update it.
  462. // pblock is optional, but should be provided if the transaction is known to be in a block.
  463. // If fUpdate is true, existing transactions will be updated.
  464. bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
  465. {
  466. uint256 hash = tx.GetHash();
  467. {
  468. LOCK(cs_wallet);
  469. bool fExisted = mapWallet.count(hash);
  470. if (fExisted && !fUpdate) return false;
  471. if (fExisted || IsMine(tx) || IsFromMe(tx))
  472. {
  473. CWalletTx wtx(this,tx);
  474. // Get merkle branch if transaction was found in a block
  475. if (pblock)
  476. wtx.SetMerkleBranch(pblock);
  477. return AddToWallet(wtx);
  478. }
  479. else
  480. WalletUpdateSpent(tx);
  481. }
  482. return false;
  483. }
  484. bool CWallet::EraseFromWallet(uint256 hash)
  485. {
  486. if (!fFileBacked)
  487. return false;
  488. {
  489. LOCK(cs_wallet);
  490. if (mapWallet.erase(hash))
  491. CWalletDB(strWalletFile).EraseTx(hash);
  492. }
  493. return true;
  494. }
  495. bool CWallet::IsMine(const CTxIn &txin) const
  496. {
  497. {
  498. LOCK(cs_wallet);
  499. map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
  500. if (mi != mapWallet.end())
  501. {
  502. const CWalletTx& prev = (*mi).second;
  503. if (txin.prevout.n < prev.vout.size())
  504. if (IsMine(prev.vout[txin.prevout.n]))
  505. return true;
  506. }
  507. }
  508. return false;
  509. }
  510. int64 CWallet::GetDebit(const CTxIn &txin) const
  511. {
  512. {
  513. LOCK(cs_wallet);
  514. map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
  515. if (mi != mapWallet.end())
  516. {
  517. const CWalletTx& prev = (*mi).second;
  518. if (txin.prevout.n < prev.vout.size())
  519. if (IsMine(prev.vout[txin.prevout.n]))
  520. return prev.vout[txin.prevout.n].nValue;
  521. }
  522. }
  523. return 0;
  524. }
  525. bool CWallet::IsChange(const CTxOut& txout) const
  526. {
  527. CTxDestination address;
  528. // TODO: fix handling of 'change' outputs. The assumption is that any
  529. // payment to a TX_PUBKEYHASH that is mine but isn't in the address book
  530. // is change. That assumption is likely to break when we implement multisignature
  531. // wallets that return change back into a multi-signature-protected address;
  532. // a better way of identifying which outputs are 'the send' and which are
  533. // 'the change' will need to be implemented (maybe extend CWalletTx to remember
  534. // which output, if any, was change).
  535. if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address))
  536. {
  537. LOCK(cs_wallet);
  538. if (!mapAddressBook.count(address))
  539. return true;
  540. }
  541. return false;
  542. }
  543. int64 CWalletTx::GetTxTime() const
  544. {
  545. int64 n = nTimeSmart;
  546. return n ? n : nTimeReceived;
  547. }
  548. int CWalletTx::GetRequestCount() const
  549. {
  550. // Returns -1 if it wasn't being tracked
  551. int nRequests = -1;
  552. {
  553. LOCK(pwallet->cs_wallet);
  554. if (IsCoinBase() || IsCoinStake())
  555. {
  556. // Generated block
  557. if (hashBlock != 0)
  558. {
  559. map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
  560. if (mi != pwallet->mapRequestCount.end())
  561. nRequests = (*mi).second;
  562. }
  563. }
  564. else
  565. {
  566. // Did anyone request this transaction?
  567. map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
  568. if (mi != pwallet->mapRequestCount.end())
  569. {
  570. nRequests = (*mi).second;
  571. // How about the block it's in?
  572. if (nRequests == 0 && hashBlock != 0)
  573. {
  574. map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
  575. if (mi != pwallet->mapRequestCount.end())
  576. nRequests = (*mi).second;
  577. else
  578. nRequests = 1; // If it's in someone else's block it must have got out
  579. }
  580. }
  581. }
  582. }
  583. return nRequests;
  584. }
  585. void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list<pair<CTxDestination, int64> >& listReceived,
  586. list<pair<CTxDestination, int64> >& listSent, int64& nFee, string& strSentAccount) const
  587. {
  588. nGeneratedImmature = nGeneratedMature = nFee = 0;
  589. listReceived.clear();
  590. listSent.clear();
  591. strSentAccount = strFromAccount;
  592. if (IsCoinBase() || IsCoinStake())
  593. {
  594. if (GetBlocksToMaturity() > 0)
  595. nGeneratedImmature = pwallet->GetCredit(*this);
  596. else
  597. nGeneratedMature = GetCredit();
  598. return;
  599. }
  600. // Compute fee:
  601. int64 nDebit = GetDebit();
  602. if (nDebit > 0) // debit>0 means we signed/sent this transaction
  603. {
  604. int64 nValueOut = GetValueOut();
  605. nFee = nDebit - nValueOut;
  606. }
  607. // Sent/received.
  608. BOOST_FOREACH(const CTxOut& txout, vout)
  609. {
  610. bool fIsMine;
  611. // Only need to handle txouts if AT LEAST one of these is true:
  612. // 1) they debit from us (sent)
  613. // 2) the output is to us (received)
  614. if (nDebit > 0)
  615. {
  616. // Don't report 'change' txouts
  617. if (pwallet->IsChange(txout))
  618. continue;
  619. fIsMine = pwallet->IsMine(txout);
  620. }
  621. else if (!(fIsMine = pwallet->IsMine(txout)))
  622. continue;
  623. // In either case, we need to get the destination address
  624. CTxDestination address;
  625. if (!ExtractDestination(txout.scriptPubKey, address))
  626. {
  627. printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
  628. this->GetHash().ToString().c_str());
  629. address = CNoDestination();
  630. }
  631. // If we are debited by the transaction, add the output as a "sent" entry
  632. if (nDebit > 0)
  633. listSent.push_back(make_pair(address, txout.nValue));
  634. // If we are receiving the output, add it as a "received" entry
  635. if (fIsMine)
  636. listReceived.push_back(make_pair(address, txout.nValue));
  637. }
  638. }
  639. void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
  640. int64& nSent, int64& nFee) const
  641. {
  642. nGenerated = nReceived = nSent = nFee = 0;
  643. int64 allGeneratedImmature, allGeneratedMature, allFee;
  644. allGeneratedImmature = allGeneratedMature = allFee = 0;
  645. string strSentAccount;
  646. list<pair<CTxDestination, int64> > listReceived;
  647. list<pair<CTxDestination, int64> > listSent;
  648. GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
  649. if (strAccount == "")
  650. nGenerated = allGeneratedMature;
  651. if (strAccount == strSentAccount)
  652. {
  653. BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& s, listSent)
  654. nSent += s.second;
  655. nFee = allFee;
  656. }
  657. {
  658. LOCK(pwallet->cs_wallet);
  659. BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listReceived)
  660. {
  661. if (pwallet->mapAddressBook.count(r.first))
  662. {
  663. map<CTxDestination, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
  664. if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
  665. nReceived += r.second;
  666. }
  667. else if (strAccount.empty())
  668. {
  669. nReceived += r.second;
  670. }
  671. }
  672. }
  673. }
  674. void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
  675. {
  676. vtxPrev.clear();
  677. const int COPY_DEPTH = 3;
  678. if (SetMerkleBranch() < COPY_DEPTH)
  679. {
  680. vector<uint256> vWorkQueue;
  681. BOOST_FOREACH(const CTxIn& txin, vin)
  682. vWorkQueue.push_back(txin.prevout.hash);
  683. // This critsect is OK because txdb is already open
  684. {
  685. LOCK(pwallet->cs_wallet);
  686. map<uint256, const CMerkleTx*> mapWalletPrev;
  687. set<uint256> setAlreadyDone;
  688. for (unsigned int i = 0; i < vWorkQueue.size(); i++)
  689. {
  690. uint256 hash = vWorkQueue[i];
  691. if (setAlreadyDone.count(hash))
  692. continue;
  693. setAlreadyDone.insert(hash);
  694. CMerkleTx tx;
  695. map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
  696. if (mi != pwallet->mapWallet.end())
  697. {
  698. tx = (*mi).second;
  699. BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
  700. mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
  701. }
  702. else if (mapWalletPrev.count(hash))
  703. {
  704. tx = *mapWalletPrev[hash];
  705. }
  706. else if (!fClient && txdb.ReadDiskTx(hash, tx))
  707. {
  708. ;
  709. }
  710. else
  711. {
  712. printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
  713. continue;
  714. }
  715. int nDepth = tx.SetMerkleBranch();
  716. vtxPrev.push_back(tx);
  717. if (nDepth < COPY_DEPTH)
  718. {
  719. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  720. vWorkQueue.push_back(txin.prevout.hash);
  721. }
  722. }
  723. }
  724. }
  725. reverse(vtxPrev.begin(), vtxPrev.end());
  726. }
  727. bool CWalletTx::WriteToDisk()
  728. {
  729. return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
  730. }
  731. // Scan the block chain (starting in pindexStart) for transactions
  732. // from or to us. If fUpdate is true, found transactions that already
  733. // exist in the wallet will be updated.
  734. int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
  735. {
  736. int ret = 0;
  737. CBlockIndex* pindex = pindexStart;
  738. {
  739. LOCK(cs_wallet);
  740. while (pindex)
  741. {
  742. CBlock block;
  743. block.ReadFromDisk(pindex, true);
  744. BOOST_FOREACH(CTransaction& tx, block.vtx)
  745. {
  746. if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
  747. ret++;
  748. }
  749. pindex = pindex->pnext;
  750. }
  751. }
  752. return ret;
  753. }
  754. int CWallet::ScanForWalletTransaction(const uint256& hashTx)
  755. {
  756. CTransaction tx;
  757. tx.ReadFromDisk(COutPoint(hashTx, 0));
  758. if (AddToWalletIfInvolvingMe(tx, NULL, true, true))
  759. return 1;
  760. return 0;
  761. }
  762. void CWallet::ReacceptWalletTransactions()
  763. {
  764. CTxDB txdb("r");
  765. bool fRepeat = true;
  766. while (fRepeat)
  767. {
  768. LOCK(cs_wallet);
  769. fRepeat = false;
  770. vector<CDiskTxPos> vMissingTx;
  771. BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
  772. {
  773. CWalletTx& wtx = item.second;
  774. if ((wtx.IsCoinBase() && wtx.IsSpent(0)) || (wtx.IsCoinStake() && wtx.IsSpent(1)))
  775. continue;
  776. CTxIndex txindex;
  777. bool fUpdated = false;
  778. if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
  779. {
  780. // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
  781. if (txindex.vSpent.size() != wtx.vout.size())
  782. {
  783. printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %" PRIszu" != wtx.vout.size() %" PRIszu"\n", txindex.vSpent.size(), wtx.vout.size());
  784. continue;
  785. }
  786. for (unsigned int i = 0; i < txindex.vSpent.size(); i++)
  787. {
  788. if (wtx.IsSpent(i))
  789. continue;
  790. if (!txindex.vSpent[i].IsNull() && IsMine(wtx.vout[i]))
  791. {
  792. wtx.MarkSpent(i);
  793. fUpdated = true;
  794. vMissingTx.push_back(txindex.vSpent[i]);
  795. }
  796. }
  797. if (fUpdated)
  798. {
  799. printf("ReacceptWalletTransactions found spent coin %snvc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
  800. wtx.MarkDirty();
  801. wtx.WriteToDisk();
  802. }
  803. }
  804. else
  805. {
  806. // Re-accept any txes of ours that aren't already in a block
  807. if (!(wtx.IsCoinBase() || wtx.IsCoinStake()))
  808. wtx.AcceptWalletTransaction(txdb, false);
  809. }
  810. }
  811. if (!vMissingTx.empty())
  812. {
  813. // TODO: optimize this to scan just part of the block chain?
  814. if (ScanForWalletTransactions(pindexGenesisBlock))
  815. fRepeat = true; // Found missing transactions: re-do re-accept.
  816. }
  817. }
  818. }
  819. void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
  820. {
  821. BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
  822. {
  823. if (!(tx.IsCoinBase() || tx.IsCoinStake()))
  824. {
  825. uint256 hash = tx.GetHash();
  826. if (!txdb.ContainsTx(hash))
  827. RelayTransaction((CTransaction)tx, hash);
  828. }
  829. }
  830. if (!(IsCoinBase() || IsCoinStake()))
  831. {
  832. uint256 hash = GetHash();
  833. if (!txdb.ContainsTx(hash))
  834. {
  835. printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
  836. RelayTransaction((CTransaction)*this, hash);
  837. }
  838. }
  839. }
  840. void CWalletTx::RelayWalletTransaction()
  841. {
  842. CTxDB txdb("r");
  843. RelayWalletTransaction(txdb);
  844. }
  845. void CWallet::ResendWalletTransactions(bool fForce)
  846. {
  847. if (!fForce)
  848. {
  849. // Do this infrequently and randomly to avoid giving away
  850. // that these are our transactions.
  851. static int64_t nNextTime;
  852. if (GetTime() < nNextTime)
  853. return;
  854. bool fFirst = (nNextTime == 0);
  855. nNextTime = GetTime() + GetRand(30 * 60);
  856. if (fFirst)
  857. return;
  858. // Only do it if there's been a new block since last time
  859. static int64_t nLastTime;
  860. if (nTimeBestReceived < nLastTime)
  861. return;
  862. nLastTime = GetTime();
  863. }
  864. // Rebroadcast any of our txes that aren't in a block yet
  865. printf("ResendWalletTransactions()\n");
  866. CTxDB txdb("r");
  867. {
  868. LOCK(cs_wallet);
  869. // Sort them in chronological order
  870. multimap<unsigned int, CWalletTx*> mapSorted;
  871. BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
  872. {
  873. CWalletTx& wtx = item.second;
  874. // Don't rebroadcast until it's had plenty of time that
  875. // it should have gotten in already by now.
  876. if (fForce || nTimeBestReceived - (int64_t)wtx.nTimeReceived > 5 * 60)
  877. mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
  878. }
  879. BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
  880. {
  881. CWalletTx& wtx = *item.second;
  882. if (wtx.CheckTransaction())
  883. wtx.RelayWalletTransaction(txdb);
  884. else
  885. printf("ResendWalletTransactions() : CheckTransaction failed for transaction %s\n", wtx.GetHash().ToString().c_str());
  886. }
  887. }
  888. }
  889. //////////////////////////////////////////////////////////////////////////////
  890. //
  891. // Actions
  892. //
  893. int64 CWallet::GetBalance() const
  894. {
  895. int64 nTotal = 0;
  896. {
  897. LOCK(cs_wallet);
  898. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  899. {
  900. const CWalletTx* pcoin = &(*it).second;
  901. if (pcoin->IsTrusted())
  902. nTotal += pcoin->GetAvailableCredit();
  903. }
  904. }
  905. return nTotal;
  906. }
  907. int64 CWallet::GetUnconfirmedBalance() const
  908. {
  909. int64 nTotal = 0;
  910. {
  911. LOCK(cs_wallet);
  912. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  913. {
  914. const CWalletTx* pcoin = &(*it).second;
  915. if (!pcoin->IsFinal() || !pcoin->IsTrusted())
  916. nTotal += pcoin->GetAvailableCredit();
  917. }
  918. }
  919. return nTotal;
  920. }
  921. int64 CWallet::GetImmatureBalance() const
  922. {
  923. int64 nTotal = 0;
  924. {
  925. LOCK(cs_wallet);
  926. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  927. {
  928. const CWalletTx& pcoin = (*it).second;
  929. if (pcoin.IsCoinBase() && pcoin.GetBlocksToMaturity() > 0 && pcoin.IsInMainChain())
  930. nTotal += GetCredit(pcoin);
  931. }
  932. }
  933. return nTotal;
  934. }
  935. // populate vCoins with vector of spendable COutputs
  936. void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl) const
  937. {
  938. vCoins.clear();
  939. {
  940. LOCK(cs_wallet);
  941. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  942. {
  943. const CWalletTx* pcoin = &(*it).second;
  944. if (!pcoin->IsFinal())
  945. continue;
  946. if (fOnlyConfirmed && !pcoin->IsTrusted())
  947. continue;
  948. if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
  949. continue;
  950. if(pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0)
  951. continue;
  952. for (unsigned int i = 0; i < pcoin->vout.size(); i++)
  953. if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue >= nMinimumInputValue &&
  954. (!coinControl || !coinControl->HasSelected() || coinControl->IsSelected((*it).first, i)))
  955. vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
  956. }
  957. }
  958. }
  959. void CWallet::AvailableCoinsMinConf(vector<COutput>& vCoins, int nConf) const
  960. {
  961. vCoins.clear();
  962. {
  963. LOCK(cs_wallet);
  964. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  965. {
  966. const CWalletTx* pcoin = &(*it).second;
  967. if (!pcoin->IsFinal())
  968. continue;
  969. if(pcoin->GetDepthInMainChain() < nConf)
  970. continue;
  971. for (unsigned int i = 0; i < pcoin->vout.size(); i++)
  972. if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) && pcoin->vout[i].nValue >= nMinimumInputValue)
  973. vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
  974. }
  975. }
  976. }
  977. static void ApproximateBestSubset(vector<pair<int64, pair<const CWalletTx*,unsigned int> > >vValue, int64 nTotalLower, int64 nTargetValue,
  978. vector<char>& vfBest, int64& nBest, int iterations = 1000)
  979. {
  980. vector<char> vfIncluded;
  981. vfBest.assign(vValue.size(), true);
  982. nBest = nTotalLower;
  983. for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
  984. {
  985. vfIncluded.assign(vValue.size(), false);
  986. int64 nTotal = 0;
  987. bool fReachedTarget = false;
  988. for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
  989. {
  990. for (unsigned int i = 0; i < vValue.size(); i++)
  991. {
  992. if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
  993. {
  994. nTotal += vValue[i].first;
  995. vfIncluded[i] = true;
  996. if (nTotal >= nTargetValue)
  997. {
  998. fReachedTarget = true;
  999. if (nTotal < nBest)
  1000. {
  1001. nBest = nTotal;
  1002. vfBest = vfIncluded;
  1003. }
  1004. nTotal -= vValue[i].first;
  1005. vfIncluded[i] = false;
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. // ecoin: total coins staked (non-spendable until maturity)
  1013. int64 CWallet::GetStake() const
  1014. {
  1015. int64 nTotal = 0;
  1016. LOCK(cs_wallet);
  1017. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  1018. {
  1019. const CWalletTx* pcoin = &(*it).second;
  1020. if (pcoin->IsCoinStake() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
  1021. nTotal += CWallet::GetCredit(*pcoin);
  1022. }
  1023. return nTotal;
  1024. }
  1025. int64 CWallet::GetNewMint() const
  1026. {
  1027. int64 nTotal = 0;
  1028. LOCK(cs_wallet);
  1029. for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  1030. {
  1031. const CWalletTx* pcoin = &(*it).second;
  1032. if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0 && pcoin->GetDepthInMainChain() > 0)
  1033. nTotal += CWallet::GetCredit(*pcoin);
  1034. }
  1035. return nTotal;
  1036. }
  1037. bool CWallet::SelectCoinsMinConf(int64 nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, vector<COutput> vCoins, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
  1038. {
  1039. setCoinsRet.clear();
  1040. nValueRet = 0;
  1041. // List of values less than target
  1042. pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
  1043. coinLowestLarger.first = std::numeric_limits<int64>::max();
  1044. coinLowestLarger.second.first = NULL;
  1045. vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
  1046. int64 nTotalLower = 0;
  1047. random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
  1048. BOOST_FOREACH(COutput output, vCoins)
  1049. {
  1050. const CWalletTx *pcoin = output.tx;
  1051. if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
  1052. continue;
  1053. int i = output.i;
  1054. // Follow the timestamp rules
  1055. if (pcoin->nTime > nSpendTime)
  1056. continue;
  1057. int64 n = pcoin->vout[i].nValue;
  1058. pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
  1059. if (n == nTargetValue)
  1060. {
  1061. setCoinsRet.insert(coin.second);
  1062. nValueRet += coin.first;
  1063. return true;
  1064. }
  1065. else if (n < nTargetValue + CENT)
  1066. {
  1067. vValue.push_back(coin);
  1068. nTotalLower += n;
  1069. }
  1070. else if (n < coinLowestLarger.first)
  1071. {
  1072. coinLowestLarger = coin;
  1073. }
  1074. }
  1075. if (nTotalLower == nTargetValue)
  1076. {
  1077. for (unsigned int i = 0; i < vValue.size(); ++i)
  1078. {
  1079. setCoinsRet.insert(vValue[i].second);
  1080. nValueRet += vValue[i].first;
  1081. }
  1082. return true;
  1083. }
  1084. if (nTotalLower < nTargetValue)
  1085. {
  1086. if (coinLowestLarger.second.first == NULL)
  1087. return false;
  1088. setCoinsRet.insert(coinLowestLarger.second);
  1089. nValueRet += coinLowestLarger.first;
  1090. return true;
  1091. }
  1092. // Solve subset sum by stochastic approximation
  1093. sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
  1094. vector<char> vfBest;
  1095. int64 nBest;
  1096. ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
  1097. if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
  1098. ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
  1099. // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
  1100. // or the next bigger coin is closer), return the bigger coin
  1101. if (coinLowestLarger.second.first &&
  1102. ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
  1103. {
  1104. setCoinsRet.insert(coinLowestLarger.second);
  1105. nValueRet += coinLowestLarger.first;
  1106. }
  1107. else {
  1108. for (unsigned int i = 0; i < vValue.size(); i++)
  1109. if (vfBest[i])
  1110. {
  1111. setCoinsRet.insert(vValue[i].second);
  1112. nValueRet += vValue[i].first;
  1113. }
  1114. if (fDebug && GetBoolArg("-printpriority"))
  1115. {
  1116. //// debug print
  1117. printf("SelectCoins() best subset: ");
  1118. for (unsigned int i = 0; i < vValue.size(); i++)
  1119. if (vfBest[i])
  1120. printf("%s ", FormatMoney(vValue[i].first).c_str());
  1121. printf("total %s\n", FormatMoney(nBest).c_str());
  1122. }
  1123. }
  1124. return true;
  1125. }
  1126. bool CWallet::SelectCoins(int64 nTargetValue, unsigned int nSpendTime, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet, const CCoinControl* coinControl) const
  1127. {
  1128. vector<COutput> vCoins;
  1129. AvailableCoins(vCoins, true, coinControl);
  1130. // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
  1131. if (coinControl && coinControl->HasSelected())
  1132. {
  1133. BOOST_FOREACH(const COutput& out, vCoins)
  1134. {
  1135. nValueRet += out.tx->vout[out.i].nValue;
  1136. setCoinsRet.insert(make_pair(out.tx, out.i));
  1137. }
  1138. return (nValueRet >= nTargetValue);
  1139. }
  1140. return (SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 6, vCoins, setCoinsRet, nValueRet) ||
  1141. SelectCoinsMinConf(nTargetValue, nSpendTime, 1, 1, vCoins, setCoinsRet, nValueRet) ||
  1142. SelectCoinsMinConf(nTargetValue, nSpendTime, 0, 1, vCoins, setCoinsRet, nValueRet));
  1143. }
  1144. // Select some coins without random shuffle or best subset approximation
  1145. bool CWallet::SelectCoinsSimple(int64 nTargetValue, unsigned int nSpendTime, int nMinConf, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
  1146. {
  1147. vector<COutput> vCoins;
  1148. AvailableCoinsMinConf(vCoins, nMinConf);
  1149. setCoinsRet.clear();
  1150. nValueRet = 0;
  1151. BOOST_FOREACH(COutput output, vCoins)
  1152. {
  1153. const CWalletTx *pcoin = output.tx;
  1154. int i = output.i;
  1155. // Stop if we've chosen enough inputs
  1156. if (nValueRet >= nTargetValue)
  1157. break;
  1158. // Follow the timestamp rules
  1159. if (pcoin->nTime > nSpendTime)
  1160. continue;
  1161. int64 n = pcoin->vout[i].nValue;
  1162. pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
  1163. if (n >= nTargetValue)
  1164. {
  1165. // If input value is greater or equal to target then simply insert
  1166. // it into the current subset and exit
  1167. setCoinsRet.insert(coin.second);
  1168. nValueRet += coin.first;
  1169. break;
  1170. }
  1171. else if (n < nTargetValue + CENT)
  1172. {
  1173. setCoinsRet.insert(coin.second);
  1174. nValueRet += coin.first;
  1175. }
  1176. }
  1177. return true;
  1178. }
  1179. bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl* coinControl)
  1180. {
  1181. int64 nValue = 0;
  1182. BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
  1183. {
  1184. if (nValue < 0)
  1185. return false;
  1186. nValue += s.second;
  1187. }
  1188. if (vecSend.empty() || nValue < 0)
  1189. return false;
  1190. wtxNew.BindWallet(this);
  1191. {
  1192. LOCK2(cs_main, cs_wallet);
  1193. // txdb must be opened before the mapWallet lock
  1194. CTxDB txdb("r");
  1195. {
  1196. nFeeRet = nTransactionFee;
  1197. while (true)
  1198. {
  1199. wtxNew.vin.clear();
  1200. wtxNew.vout.clear();
  1201. wtxNew.fFromMe = true;
  1202. int64 nTotalValue = nValue + nFeeRet;
  1203. double dPriority = 0;
  1204. // vouts to the payees
  1205. BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
  1206. wtxNew.vout.push_back(CTxOut(s.second, s.first));
  1207. // Choose coins to use
  1208. set<pair<const CWalletTx*,unsigned int> > setCoins;
  1209. int64 nValueIn = 0;
  1210. if (!SelectCoins(nTotalValue, wtxNew.nTime, setCoins, nValueIn, coinControl))
  1211. return false;
  1212. BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
  1213. {
  1214. int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
  1215. dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
  1216. }
  1217. int64 nChange = nValueIn - nValue - nFeeRet;
  1218. // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
  1219. // or until nChange becomes zero
  1220. // NOTE: this depends on the exact behaviour of GetMinFee
  1221. if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
  1222. {
  1223. int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
  1224. nChange -= nMoveToFee;
  1225. nFeeRet += nMoveToFee;
  1226. }
  1227. // ecoin: sub-cent change is moved to fee
  1228. if (nChange > 0 && nChange < MIN_TXOUT_AMOUNT)
  1229. {
  1230. nFeeRet += nChange;
  1231. nChange = 0;
  1232. }
  1233. if (nChange > 0)
  1234. {
  1235. // Fill a vout to ourself
  1236. // TODO: pass in scriptChange instead of reservekey so
  1237. // change transaction isn't always pay-to-ecoin-address
  1238. CScript scriptChange;
  1239. // coin control: send change to custom address
  1240. if (coinControl && !boost::get<CNoDestination>(&coinControl->destChange))
  1241. scriptChange.SetDestination(coinControl->destChange);
  1242. // no coin control: send change to newly generated address
  1243. else
  1244. {
  1245. // Note: We use a new key here to keep it from being obvious which side is the change.
  1246. // The drawback is that by not reusing a previous key, the change may be lost if a
  1247. // backup is restored, if the backup doesn't have the new private key for the change.
  1248. // If we reused the old key, it would be possible to add code to look for and
  1249. // rediscover unknown transactions that were written with keys of ours to recover
  1250. // post-backup change.
  1251. // Reserve a new key pair from key pool
  1252. CPubKey vchPubKey = reservekey.GetReservedKey();
  1253. scriptChange.SetDestination(vchPubKey.GetID());
  1254. }
  1255. // Insert change txn at random position:
  1256. vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
  1257. wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
  1258. }
  1259. else
  1260. reservekey.ReturnKey();
  1261. // Fill vin
  1262. BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
  1263. wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
  1264. // Sign
  1265. int nIn = 0;
  1266. BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
  1267. if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
  1268. return false;
  1269. // Limit size
  1270. unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
  1271. if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
  1272. return false;
  1273. dPriority /= nBytes;
  1274. // Check that enough fee is included
  1275. int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
  1276. int64 nMinFee = wtxNew.GetMinFee(1, false, GMF_SEND, nBytes);
  1277. if (nFeeRet < max(nPayFee, nMinFee))
  1278. {
  1279. nFeeRet = max(nPayFee, nMinFee);
  1280. continue;
  1281. }
  1282. // Fill vtxPrev by copying from previous transactions vtxPrev
  1283. wtxNew.AddSupportingTransactions(txdb);
  1284. wtxNew.fTimeReceivedIsTxTime = true;
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. return true;
  1290. }
  1291. bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl* coinControl)
  1292. {
  1293. vector< pair<CScript, int64> > vecSend;
  1294. vecSend.push_back(make_pair(scriptPubKey, nValue));
  1295. return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, coinControl);
  1296. }
  1297. // Ecoin: get current stake weight
  1298. bool CWallet::GetStakeWeight(const CKeyStore& keystore, uint64_t& nMinWeight, uint64_t& nMaxWeight, uint64_t& nWeight)
  1299. {
  1300. // Choose coins to use
  1301. int64 nBalance = GetBalance();
  1302. int64 nReserveBalance = 0;
  1303. if (mapArgs.count("-reservebalance") && !ParseMoney(mapArgs["-reservebalance"], nReserveBalance))
  1304. {
  1305. error("GetStakeWeight : invalid reserve balance amount");
  1306. return false;
  1307. }
  1308. if (nBalance <= nReserveBalance)
  1309. return false;
  1310. vector<const CWalletTx*> vwtxPrev;
  1311. /*
  1312. * TODO: performance comparison
  1313. static set<pair<const CWalletTx*,unsigned int> > setCoins;
  1314. static uint256 hashPrevBlock;
  1315. static int64 nValueIn = 0;
  1316. // Cache outputs unless best block changed
  1317. if (hashPrevBlock != pindexBest->GetBlockHash())
  1318. {
  1319. if (!SelectCoinsSimple(nBalance - nReserveBalance, GetAdjustedTime(), nCoinbaseMaturity * 10, setCoins, nValueIn))
  1320. return false;
  1321. if (setCoins.empty())
  1322. return false;
  1323. hashPrevBlock == pindexBest->GetBlockHash();
  1324. }
  1325. */
  1326. set<pair<const CWalletTx*,unsigned int> > setCoins;
  1327. int64 nValueIn = 0;
  1328. if (!SelectCoinsSimple(nBalance - nReserveBalance, GetTime(), nCoinbaseMaturity * 10, setCoins, nValueIn))
  1329. return false;
  1330. if (setCoins.empty())
  1331. return false;
  1332. CTxDB txdb("r");
  1333. BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
  1334. {
  1335. CTxIndex txindex;
  1336. {
  1337. LOCK2(cs_main, cs_wallet);
  1338. if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
  1339. continue;
  1340. }
  1341. int64 nTimeWeight = GetWeight((int64)pcoin.first->nTime, (int64)GetTime());
  1342. CBigNum bnCoinDayWeight = CBigNum(pcoin.first->vout[pcoin.second].nValue) * nTimeWeight / COIN / (24 * 60 * 60);
  1343. // Weight is greater than zero
  1344. if (nTimeWeight > 0)
  1345. {
  1346. nWeight += bnCoinDayWeight.getuint64();
  1347. }
  1348. // Weight is greater than zero, but the maximum value isn't reached yet
  1349. if (nTimeWeight > 0 && nTimeWeight < nStakeMaxAge)
  1350. {
  1351. nMinWeight += bnCoinDayWeight.getuint64();
  1352. }
  1353. // Maximum weight was reached
  1354. if (nTimeWeight == nStakeMaxAge)
  1355. {
  1356. nMaxWeight += bnCoinDayWeight.getuint64();
  1357. }
  1358. }
  1359. return true;
  1360. }
  1361. bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64 nSearchInterval, CTransaction& txNew, CKey& key)
  1362. {
  1363. // The following combine threshold is important to security
  1364. // Should not be adjusted if you don't understand the consequences
  1365. int64 nCombineThreshold = GetProofOfWorkReward(GetLastBlockIndex(pindexBest, false)->nHeight, GetLastBlockIndex(pindexBest, false)->GetBlockHash()) / 3;
  1366. //CBlockIndex* pindexPrev = pindexBest;
  1367. CBigNum bnTargetPerCoinDay;
  1368. bnTargetPerCoinDay.SetCompact(nBits);
  1369. txNew.vin.clear();
  1370. txNew.vout.clear();
  1371. // Mark coin stake transaction
  1372. CScript scriptEmpty;
  1373. scriptEmpty.clear();
  1374. txNew.vout.push_back(CTxOut(0, scriptEmpty));
  1375. // Choose coins to use
  1376. int64 nBalance = GetBalance();
  1377. int64 nReserveBalance = 0;
  1378. if (mapArgs.count("-reservebalance") && !ParseMoney(mapArgs["-reservebalance"], nReserveBalance))
  1379. return error("CreateCoinStake : invalid reserve balance amount");
  1380. if (nBalance <= nReserveBalance)
  1381. return false;
  1382. vector<const CWalletTx*> vwtxPrev;
  1383. /*
  1384. * TODO: performance comparison
  1385. static set<pair<const CWalletTx*,unsigned int> > setCoins;
  1386. static uint256 hashPrevBlock;
  1387. static int64 nValueIn = 0;
  1388. // Cache outputs unless best block changed
  1389. if (hashPrevBlock != pindexBest->GetBlockHash())
  1390. {
  1391. if (!SelectCoinsSimple(nBalance - nReserveBalance, txNew.nTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
  1392. return false;
  1393. if (setCoins.empty())
  1394. return false;
  1395. hashPrevBlock == pindexBest->GetBlockHash();
  1396. }
  1397. */
  1398. set<pair<const CWalletTx*,unsigned int> > setCoins;
  1399. int64 nValueIn = 0;
  1400. // Select coins with suitable depth
  1401. if (!SelectCoinsSimple(nBalance - nReserveBalance, txNew.nTime, nCoinbaseMaturity * 10, setCoins, nValueIn))
  1402. return false;
  1403. if (setCoins.empty())
  1404. return false;
  1405. int64 nCredit = 0;
  1406. CScript scriptPubKeyKernel;
  1407. CTxDB txdb("r");
  1408. BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
  1409. {
  1410. CTxIndex txindex;
  1411. {
  1412. LOCK2(cs_main, cs_wallet);
  1413. if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
  1414. continue;
  1415. }
  1416. // Read block header
  1417. CBlock block;
  1418. {
  1419. LOCK2(cs_main, cs_wallet);
  1420. if (!block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
  1421. continue;
  1422. }
  1423. static int nMaxStakeSearchInterval = 60;
  1424. if (block.GetBlockTime() + nStakeMinAge > txNew.nTime - nMaxStakeSearchInterval)
  1425. continue; // only count coins meeting min age requirement
  1426. bool fKernelFound = false;
  1427. for (unsigned int n=0; n<min(nSearchInterval,(int64)nMaxStakeSearchInterval) && !fKernelFound && !fShutdown; n++)
  1428. {
  1429. // Search backward in time from the given txNew timestamp
  1430. // Search nSearchInterval seconds back up to nMaxStakeSearchInterval
  1431. uint256 hashProofOfStake = 0, targetProofOfStake = 0;
  1432. COutPoint prevoutStake = COutPoint(pcoin.first->GetHash(), pcoin.second);
  1433. if (CheckStakeKernelHash(nBits, block, txindex.pos.nTxPos - txindex.pos.nBlockPos, *pcoin.first, prevoutStake, txNew.nTime - n, hashProofOfStake, targetProofOfStake))
  1434. {
  1435. // Found a kernel
  1436. if (fDebug && GetBoolArg("-printcoinstake"))
  1437. printf("CreateCoinStake : kernel found\n");
  1438. vector<valtype> vSolutions;
  1439. txnouttype whichType;
  1440. CScript scriptPubKeyOut;
  1441. scriptPubKeyKernel = pcoin.first->vout[pcoin.second].scriptPubKey;
  1442. if (!Solver(scriptPubKeyKernel, whichType, vSolutions))
  1443. {
  1444. if (fDebug && GetBoolArg("-printcoinstake"))
  1445. printf("CreateCoinStake : failed to parse kernel\n");
  1446. break;
  1447. }
  1448. if (fDebug && GetBoolArg("-printcoinstake"))
  1449. printf("CreateCoinStake : parsed kernel type=%d\n", whichType);
  1450. if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH)
  1451. {
  1452. if (fDebug && GetBoolArg("-printcoinstake"))
  1453. printf("CreateCoinStake : no support for kernel type=%d\n", whichType);
  1454. break; // only support pay to public key and pay to address
  1455. }
  1456. if (whichType == TX_PUBKEYHASH) // pay to address type
  1457. {
  1458. // convert to pay to public key type
  1459. if (!keystore.GetKey(uint160(vSolutions[0]), key))
  1460. {
  1461. if (fDebug && GetBoolArg("-printcoinstake"))
  1462. printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
  1463. break; // unable to find corresponding public key
  1464. }
  1465. scriptPubKeyOut << key.GetPubKey() << OP_CHECKSIG;
  1466. }
  1467. if (whichType == TX_PUBKEY)
  1468. {
  1469. valtype& vchPubKey = vSolutions[0];
  1470. if (!keystore.GetKey(Hash160(vchPubKey), key))
  1471. {
  1472. if (fDebug && GetBoolArg("-printcoinstake"))
  1473. printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
  1474. break; // unable to find corresponding public key
  1475. }
  1476. if (key.GetPubKey() != vchPubKey)
  1477. {
  1478. if (fDebug && GetBoolArg("-printcoinstake"))
  1479. printf("CreateCoinStake : invalid key for kernel type=%d\n", whichType);
  1480. break; // keys mismatch
  1481. }
  1482. scriptPubKeyOut = scriptPubKeyKernel;
  1483. }
  1484. txNew.nTime -= n;
  1485. txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
  1486. nCredit += pcoin.first->vout[pcoin.second].nValue;
  1487. vwtxPrev.push_back(pcoin.first);
  1488. txNew.vout.push_back(CTxOut(0, scriptPubKeyOut));
  1489. if (GetWeight(block.GetBlockTime(), (int64)txNew.nTime) < nStakeMaxAge)
  1490. txNew.vout.push_back(CTxOut(0, scriptPubKeyOut)); //split stake
  1491. if (fDebug && GetBoolArg("-printcoinstake"))
  1492. printf("CreateCoinStake : added kernel type=%d\n", whichType);
  1493. fKernelFound = true;
  1494. break;
  1495. }
  1496. }
  1497. if (fKernelFound || fShutdown)
  1498. break; // if kernel is found stop searching
  1499. }
  1500. if (nCredit == 0 || nCredit > nBalance - nReserveBalance)
  1501. return false;
  1502. BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
  1503. {
  1504. // Attempt to add more inputs
  1505. // Only add coins of the same key/address as kernel
  1506. if (txNew.vout.size() == 2 && ((pcoin.first->vout[pcoin.second].scriptPubKey == scriptPubKeyKernel || pcoin.first->vout[pcoin.second].scriptPubKey == txNew.vout[1].scriptPubKey))
  1507. && pcoin.first->GetHash() != txNew.vin[0].prevout.hash)
  1508. {
  1509. int64 nTimeWeight = GetWeight((int64)pcoin.first->nTime, (int64)txNew.nTime);
  1510. // Stop adding more inputs if already too many inputs
  1511. if (txNew.vin.size() >= 100)
  1512. break;
  1513. // Stop adding more inputs if value is already pretty significant
  1514. if (nCredit > nCombineThreshold)
  1515. break;
  1516. // Stop adding inputs if reached reserve limit
  1517. if (nCredit + pcoin.first->vout[pcoin.second].nValue > nBalance - nReserveBalance)
  1518. break;
  1519. // Do not add additional significant input
  1520. if (pcoin.first->vout[pcoin.second].nValue > nCombineThreshold)
  1521. continue;
  1522. // Do not add input that is still too young
  1523. if (nTimeWeight < nStakeMaxAge)
  1524. continue;
  1525. txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
  1526. nCredit += pcoin.first->vout[pcoin.second].nValue;
  1527. vwtxPrev.push_back(pcoin.first);
  1528. }
  1529. }
  1530. // Calculate coin age reward
  1531. {
  1532. uint64 nCoinAge;
  1533. CTxDB txdb("r");
  1534. if (!txNew.GetCoinAge(txdb, nCoinAge))
  1535. return error("CreateCoinStake : failed to calculate coin age");
  1536. nCredit += GetProofOfStakeReward(nCoinAge, nBits, txNew.nTime);
  1537. }
  1538. int64 nMinFee = 0;
  1539. while (true)
  1540. {
  1541. // Set output amount
  1542. if (txNew.vout.size() == 3)
  1543. {
  1544. txNew.vout[1].nValue = ((nCredit - nMinFee) / 2 );
  1545. txNew.vout[2].nValue = nCredit - nMinFee - txNew.vout[1].nValue;
  1546. }
  1547. else
  1548. txNew.vout[1].nValue = nCredit - nMinFee;
  1549. // Sign
  1550. int nIn = 0;
  1551. BOOST_FOREACH(const CWalletTx* pcoin, vwtxPrev)
  1552. {
  1553. if (!SignSignature(*this, *pcoin, txNew, nIn++))
  1554. return error("CreateCoinStake : failed to sign coinstake");
  1555. }
  1556. // Limit size
  1557. unsigned int nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
  1558. if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
  1559. return error("CreateCoinStake : exceeded coinstake size limit");
  1560. // Check enough fee is paid
  1561. if (nMinFee < txNew.GetMinFee() - MIN_TX_FEE)
  1562. {
  1563. nMinFee = txNew.GetMinFee() - MIN_TX_FEE;
  1564. continue; // try signing again
  1565. }
  1566. else
  1567. {
  1568. if (fDebug && GetBoolArg("-printfee"))
  1569. printf("CreateCoinStake : fee for coinstake %s\n", FormatMoney(nMinFee).c_str());
  1570. break;
  1571. }
  1572. }
  1573. // Successfully generated coinstake
  1574. return true;
  1575. }
  1576. // Call after CreateTransaction unless you want to abort
  1577. bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
  1578. {
  1579. {
  1580. LOCK2(cs_main, cs_wallet);
  1581. printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
  1582. {
  1583. // This is only to keep the database open to defeat the auto-flush for the
  1584. // duration of this scope. This is the only place where this optimization
  1585. // maybe makes sense; please don't do it anywhere else.
  1586. CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
  1587. // Take key pair from key pool so it won't be used again
  1588. reservekey.KeepKey();
  1589. // Add tx to wallet, because if it has change it's also ours,
  1590. // otherwise just for transaction history.
  1591. AddToWallet(wtxNew);
  1592. // Mark old coins as spent
  1593. set<CWalletTx*> setCoins;
  1594. BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
  1595. {
  1596. CWalletTx &coin = mapWallet[txin.prevout.hash];
  1597. coin.BindWallet(this);
  1598. coin.MarkSpent(txin.prevout.n);
  1599. coin.WriteToDisk();
  1600. NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
  1601. }
  1602. if (fFileBacked)
  1603. delete pwalletdb;
  1604. }
  1605. // Track how many getdata requests our transaction gets
  1606. mapRequestCount[wtxNew.GetHash()] = 0;
  1607. // Broadcast
  1608. if (!wtxNew.AcceptToMemoryPool())
  1609. {
  1610. // This must not fail. The transaction has already been signed and recorded.
  1611. printf("CommitTransaction() : Error: Transaction not valid");
  1612. return false;
  1613. }
  1614. wtxNew.RelayWalletTransaction();
  1615. }
  1616. return true;
  1617. }
  1618. string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
  1619. {
  1620. CReserveKey reservekey(this);
  1621. int64 nFeeRequired;
  1622. if (IsLocked())
  1623. {
  1624. string strError = _("Error: Wallet locked, unable to create transaction ");
  1625. printf("SendMoney() : %s", strError.c_str());
  1626. return strError;
  1627. }
  1628. if (fWalletUnlockStakingOnly)
  1629. {
  1630. string strError = _("Error: Wallet unlocked for block minting only, unable to create transaction.");
  1631. printf("SendMoney() : %s", strError.c_str());
  1632. return strError;
  1633. }
  1634. if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
  1635. {
  1636. string strError;
  1637. if (nValue + nFeeRequired > GetBalance())
  1638. 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());
  1639. else
  1640. strError = _("Error: Transaction creation failed ");
  1641. printf("SendMoney() : %s", strError.c_str());
  1642. return strError;
  1643. }
  1644. if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired, _("Sending...")))
  1645. return "ABORTED";
  1646. if (!CommitTransaction(wtxNew, reservekey))
  1647. 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.");
  1648. return "";
  1649. }
  1650. string CWallet::SendMoneyToDestination(const CTxDestination& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
  1651. {
  1652. // Check amount
  1653. if (nValue <= 0)
  1654. return _("Invalid amount");
  1655. if (nValue + nTransactionFee > GetBalance())
  1656. return _("Insufficient funds");
  1657. // Parse Ecoin address
  1658. CScript scriptPubKey;
  1659. scriptPubKey.SetDestination(address);
  1660. return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
  1661. }
  1662. DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
  1663. {
  1664. if (!fFileBacked)
  1665. return DB_LOAD_OK;
  1666. fFirstRunRet = false;
  1667. DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
  1668. if (nLoadWalletRet == DB_NEED_REWRITE)
  1669. {
  1670. if (CDB::Rewrite(strWalletFile, "\x04pool"))
  1671. {
  1672. setKeyPool.clear();
  1673. // Note: can't top-up keypool here, because wallet is locked.
  1674. // User will be prompted to unlock wallet the next operation
  1675. // the requires a new key.
  1676. }
  1677. }
  1678. if (nLoadWalletRet != DB_LOAD_OK)
  1679. return nLoadWalletRet;
  1680. fFirstRunRet = !vchDefaultKey.IsValid();
  1681. NewThread(ThreadFlushWalletDB, &strWalletFile);
  1682. return DB_LOAD_OK;
  1683. }
  1684. bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
  1685. {
  1686. std::map<CTxDestination, std::string>::iterator mi = mapAddressBook.find(address);
  1687. mapAddressBook[address] = strName;
  1688. NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
  1689. if (!fFileBacked)
  1690. return false;
  1691. return CWalletDB(strWalletFile).WriteName(CEcoinAddress(address).ToString(), strName);
  1692. }
  1693. bool CWallet::DelAddressBookName(const CTxDestination& address)
  1694. {
  1695. mapAddressBook.erase(address);
  1696. NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED);
  1697. if (!fFileBacked)
  1698. return false;
  1699. return CWalletDB(strWalletFile).EraseName(CEcoinAddress(address).ToString());
  1700. }
  1701. void CWallet::PrintWallet(const CBlock& block)
  1702. {
  1703. {
  1704. LOCK(cs_wallet);
  1705. if (block.IsProofOfWork() && mapWallet.count(block.vtx[0].GetHash()))
  1706. {
  1707. CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
  1708. printf(" mine: %d %d %" PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
  1709. }
  1710. if (block.IsProofOfStake() && mapWallet.count(block.vtx[1].GetHash()))
  1711. {
  1712. CWalletTx& wtx = mapWallet[block.vtx[1].GetHash()];
  1713. printf(" stake: %d %d %" PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
  1714. }
  1715. }
  1716. printf("\n");
  1717. }
  1718. bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
  1719. {
  1720. {
  1721. LOCK(cs_wallet);
  1722. map<uint256, CWalletTx>::iterator mi = mapWallet.find(hashTx);
  1723. if (mi != mapWallet.end())
  1724. {
  1725. wtx = (*mi).second;
  1726. return true;
  1727. }
  1728. }
  1729. return false;
  1730. }
  1731. bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
  1732. {
  1733. if (fFileBacked)
  1734. {
  1735. if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
  1736. return false;
  1737. }
  1738. vchDefaultKey = vchPubKey;
  1739. return true;
  1740. }
  1741. bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
  1742. {
  1743. if (!pwallet->fFileBacked)
  1744. return false;
  1745. strWalletFileOut = pwallet->strWalletFile;
  1746. return true;
  1747. }
  1748. //
  1749. // Mark old keypool keys as used,
  1750. // and generate all new keys
  1751. //
  1752. bool CWallet::NewKeyPool()
  1753. {
  1754. {
  1755. LOCK(cs_wallet);
  1756. CWalletDB walletdb(strWalletFile);
  1757. BOOST_FOREACH(int64 nIndex, setKeyPool)
  1758. walletdb.ErasePool(nIndex);
  1759. setKeyPool.clear();
  1760. if (IsLocked())
  1761. return false;
  1762. int64 nKeys = max(GetArg("-keypool", 100), (int64)0);
  1763. for (int i = 0; i < nKeys; i++)
  1764. {
  1765. int64 nIndex = i+1;
  1766. walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
  1767. setKeyPool.insert(nIndex);
  1768. }
  1769. printf("CWallet::NewKeyPool wrote %" PRI64d" new keys\n", nKeys);
  1770. }
  1771. return true;
  1772. }
  1773. bool CWallet::TopUpKeyPool(unsigned int nSize)
  1774. {
  1775. {
  1776. LOCK(cs_wallet);
  1777. if (IsLocked())
  1778. return false;
  1779. CWalletDB walletdb(strWalletFile);
  1780. // Top up key pool
  1781. unsigned int nTargetSize;
  1782. if (nSize > 0)
  1783. nTargetSize = nSize;
  1784. else
  1785. nTargetSize = max(GetArg("-keypool", 100), 0LL);
  1786. while (setKeyPool.size() < (nTargetSize + 1))
  1787. {
  1788. int64 nEnd = 1;
  1789. if (!setKeyPool.empty())
  1790. nEnd = *(--setKeyPool.end()) + 1;
  1791. if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
  1792. throw runtime_error("TopUpKeyPool() : writing generated key failed");
  1793. setKeyPool.insert(nEnd);
  1794. printf("keypool added key %" PRI64d", size=%" PRIszu"\n", nEnd, setKeyPool.size());
  1795. }
  1796. }
  1797. return true;
  1798. }
  1799. void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
  1800. {
  1801. nIndex = -1;
  1802. keypool.vchPubKey = CPubKey();
  1803. {
  1804. LOCK(cs_wallet);
  1805. if (!IsLocked())
  1806. TopUpKeyPool();
  1807. // Get the oldest key
  1808. if(setKeyPool.empty())
  1809. return;
  1810. CWalletDB walletdb(strWalletFile);
  1811. nIndex = *(setKeyPool.begin());
  1812. setKeyPool.erase(setKeyPool.begin());
  1813. if (!walletdb.ReadPool(nIndex, keypool))
  1814. throw runtime_error("ReserveKeyFromKeyPool() : read failed");
  1815. if (!HaveKey(keypool.vchPubKey.GetID()))
  1816. throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
  1817. assert(keypool.vchPubKey.IsValid());
  1818. if (fDebug && GetBoolArg("-printkeypool"))
  1819. printf("keypool reserve %" PRI64d"\n", nIndex);
  1820. }
  1821. }
  1822. int64 CWallet::AddReserveKey(const CKeyPool& keypool)
  1823. {
  1824. {
  1825. LOCK2(cs_main, cs_wallet);
  1826. CWalletDB walletdb(strWalletFile);
  1827. int64 nIndex = 1 + *(--setKeyPool.end());
  1828. if (!walletdb.WritePool(nIndex, keypool))
  1829. throw runtime_error("AddReserveKey() : writing added key failed");
  1830. setKeyPool.insert(nIndex);
  1831. return nIndex;
  1832. }
  1833. return -1;
  1834. }
  1835. void CWallet::KeepKey(int64 nIndex)
  1836. {
  1837. // Remove from key pool
  1838. if (fFileBacked)
  1839. {
  1840. CWalletDB walletdb(strWalletFile);
  1841. walletdb.ErasePool(nIndex);
  1842. }
  1843. if(fDebug)
  1844. printf("keypool keep %" PRI64d"\n", nIndex);
  1845. }
  1846. void CWallet::ReturnKey(int64 nIndex)
  1847. {
  1848. // Return to key pool
  1849. {
  1850. LOCK(cs_wallet);
  1851. setKeyPool.insert(nIndex);
  1852. }
  1853. if(fDebug)
  1854. printf("keypool return %" PRI64d"\n", nIndex);
  1855. }
  1856. bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
  1857. {
  1858. int64 nIndex = 0;
  1859. CKeyPool keypool;
  1860. {
  1861. LOCK(cs_wallet);
  1862. ReserveKeyFromKeyPool(nIndex, keypool);
  1863. if (nIndex == -1)
  1864. {
  1865. if (fAllowReuse && vchDefaultKey.IsValid())
  1866. {
  1867. result = vchDefaultKey;
  1868. return true;
  1869. }
  1870. if (IsLocked()) return false;
  1871. result = GenerateNewKey();
  1872. return true;
  1873. }
  1874. KeepKey(nIndex);
  1875. result = keypool.vchPubKey;
  1876. }
  1877. return true;
  1878. }
  1879. int64 CWallet::GetOldestKeyPoolTime()
  1880. {
  1881. int64 nIndex = 0;
  1882. CKeyPool keypool;
  1883. ReserveKeyFromKeyPool(nIndex, keypool);
  1884. if (nIndex == -1)
  1885. return GetTime();
  1886. ReturnKey(nIndex);
  1887. return keypool.nTime;
  1888. }
  1889. std::map<CTxDestination, int64> CWallet::GetAddressBalances()
  1890. {
  1891. map<CTxDestination, int64> balances;
  1892. {
  1893. LOCK(cs_wallet);
  1894. BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
  1895. {
  1896. CWalletTx *pcoin = &walletEntry.second;
  1897. if (!pcoin->IsFinal() || !pcoin->IsTrusted())
  1898. continue;
  1899. if ((pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetBlocksToMaturity() > 0)
  1900. continue;
  1901. int nDepth = pcoin->GetDepthInMainChain();
  1902. if (nDepth < (pcoin->IsFromMe() ? 0 : 1))
  1903. continue;
  1904. for (unsigned int i = 0; i < pcoin->vout.size(); i++)
  1905. {
  1906. CTxDestination addr;
  1907. if (!IsMine(pcoin->vout[i]))
  1908. continue;
  1909. if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
  1910. continue;
  1911. int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
  1912. if (!balances.count(addr))
  1913. balances[addr] = 0;
  1914. balances[addr] += n;
  1915. }
  1916. }
  1917. }
  1918. return balances;
  1919. }
  1920. set< set<CTxDestination> > CWallet::GetAddressGroupings()
  1921. {
  1922. set< set<CTxDestination> > groupings;
  1923. set<CTxDestination> grouping;
  1924. BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
  1925. {
  1926. CWalletTx *pcoin = &walletEntry.second;
  1927. if (pcoin->vin.size() > 0 && IsMine(pcoin->vin[0]))
  1928. {
  1929. // group all input addresses with each other
  1930. BOOST_FOREACH(CTxIn txin, pcoin->vin)
  1931. {
  1932. CTxDestination address;
  1933. if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
  1934. continue;
  1935. grouping.insert(address);
  1936. }
  1937. // group change with input addresses
  1938. BOOST_FOREACH(CTxOut txout, pcoin->vout)
  1939. if (IsChange(txout))
  1940. {
  1941. CWalletTx tx = mapWallet[pcoin->vin[0].prevout.hash];
  1942. CTxDestination txoutAddr;
  1943. if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
  1944. continue;
  1945. grouping.insert(txoutAddr);
  1946. }
  1947. groupings.insert(grouping);
  1948. grouping.clear();
  1949. }
  1950. // group lone addrs by themselves
  1951. for (unsigned int i = 0; i < pcoin->vout.size(); i++)
  1952. if (IsMine(pcoin->vout[i]))
  1953. {
  1954. CTxDestination address;
  1955. if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
  1956. continue;
  1957. grouping.insert(address);
  1958. groupings.insert(grouping);
  1959. grouping.clear();
  1960. }
  1961. }
  1962. set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
  1963. map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it
  1964. BOOST_FOREACH(set<CTxDestination> grouping, groupings)
  1965. {
  1966. // make a set of all the groups hit by this new group
  1967. set< set<CTxDestination>* > hits;
  1968. map< CTxDestination, set<CTxDestination>* >::iterator it;
  1969. BOOST_FOREACH(CTxDestination address, grouping)
  1970. if ((it = setmap.find(address)) != setmap.end())
  1971. hits.insert((*it).second);
  1972. // merge all hit groups into a new single group and delete old groups
  1973. set<CTxDestination>* merged = new set<CTxDestination>(grouping);
  1974. BOOST_FOREACH(set<CTxDestination>* hit, hits)
  1975. {
  1976. merged->insert(hit->begin(), hit->end());
  1977. uniqueGroupings.erase(hit);
  1978. delete hit;
  1979. }
  1980. uniqueGroupings.insert(merged);
  1981. // update setmap
  1982. BOOST_FOREACH(CTxDestination element, *merged)
  1983. setmap[element] = merged;
  1984. }
  1985. set< set<CTxDestination> > ret;
  1986. BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
  1987. {
  1988. ret.insert(*uniqueGrouping);
  1989. delete uniqueGrouping;
  1990. }
  1991. return ret;
  1992. }
  1993. // ecoin: check 'spent' consistency between wallet and txindex
  1994. // ecoin: fix wallet spent state according to txindex
  1995. void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion, bool fCheckOnly)
  1996. {
  1997. nMismatchFound = 0;
  1998. nBalanceInQuestion = 0;
  1999. LOCK(cs_wallet);
  2000. vector<CWalletTx*> vCoins;
  2001. vCoins.reserve(mapWallet.size());
  2002. for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
  2003. vCoins.push_back(&(*it).second);
  2004. CTxDB txdb("r");
  2005. BOOST_FOREACH(CWalletTx* pcoin, vCoins)
  2006. {
  2007. // Find the corresponding transaction index
  2008. CTxIndex txindex;
  2009. if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex))
  2010. continue;
  2011. for (unsigned int n=0; n < pcoin->vout.size(); n++)
  2012. {
  2013. if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull()))
  2014. {
  2015. printf("FixSpentCoins found lost coin %sppc %s[%d], %s\n",
  2016. FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
  2017. nMismatchFound++;
  2018. nBalanceInQuestion += pcoin->vout[n].nValue;
  2019. if (!fCheckOnly)
  2020. {
  2021. pcoin->MarkUnspent(n);
  2022. pcoin->WriteToDisk();
  2023. }
  2024. }
  2025. else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull()))
  2026. {
  2027. printf("FixSpentCoins found spent coin %sppc %s[%d], %s\n",
  2028. FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing");
  2029. nMismatchFound++;
  2030. nBalanceInQuestion += pcoin->vout[n].nValue;
  2031. if (!fCheckOnly)
  2032. {
  2033. pcoin->MarkSpent(n);
  2034. pcoin->WriteToDisk();
  2035. }
  2036. }
  2037. }
  2038. }
  2039. }
  2040. // ecoin: disable transaction (only for coinstake)
  2041. void CWallet::DisableTransaction(const CTransaction &tx)
  2042. {
  2043. if (!tx.IsCoinStake() || !IsFromMe(tx))
  2044. return; // only disconnecting coinstake requires marking input unspent
  2045. LOCK(cs_wallet);
  2046. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  2047. {
  2048. map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
  2049. if (mi != mapWallet.end())
  2050. {
  2051. CWalletTx& prev = (*mi).second;
  2052. if (txin.prevout.n < prev.vout.size() && IsMine(prev.vout[txin.prevout.n]))
  2053. {
  2054. prev.MarkUnspent(txin.prevout.n);
  2055. prev.WriteToDisk();
  2056. }
  2057. }
  2058. }
  2059. }
  2060. CPubKey CReserveKey::GetReservedKey()
  2061. {
  2062. if (nIndex == -1)
  2063. {
  2064. CKeyPool keypool;
  2065. pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
  2066. if (nIndex != -1)
  2067. vchPubKey = keypool.vchPubKey;
  2068. else
  2069. {
  2070. printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
  2071. vchPubKey = pwallet->vchDefaultKey;
  2072. }
  2073. }
  2074. assert(vchPubKey.IsValid());
  2075. return vchPubKey;
  2076. }
  2077. void CReserveKey::KeepKey()
  2078. {
  2079. if (nIndex != -1)
  2080. pwallet->KeepKey(nIndex);
  2081. nIndex = -1;
  2082. vchPubKey = CPubKey();
  2083. }
  2084. void CReserveKey::ReturnKey()
  2085. {
  2086. if (nIndex != -1)
  2087. pwallet->ReturnKey(nIndex);
  2088. nIndex = -1;
  2089. vchPubKey = CPubKey();
  2090. }
  2091. void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
  2092. {
  2093. setAddress.clear();
  2094. CWalletDB walletdb(strWalletFile);
  2095. LOCK2(cs_main, cs_wallet);
  2096. BOOST_FOREACH(const int64& id, setKeyPool)
  2097. {
  2098. CKeyPool keypool;
  2099. if (!walletdb.ReadPool(id, keypool))
  2100. throw runtime_error("GetAllReserveKeyHashes() : read failed");
  2101. assert(keypool.vchPubKey.IsValid());
  2102. CKeyID keyID = keypool.vchPubKey.GetID();
  2103. if (!HaveKey(keyID))
  2104. throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
  2105. setAddress.insert(keyID);
  2106. }
  2107. }
  2108. void CWallet::UpdatedTransaction(const uint256 &hashTx)
  2109. {
  2110. {
  2111. LOCK(cs_wallet);
  2112. // Only notify UI if this transaction is in this wallet
  2113. map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
  2114. if (mi != mapWallet.end())
  2115. NotifyTransactionChanged(this, hashTx, CT_UPDATED);
  2116. }
  2117. }
  2118. void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64> &mapKeyBirth) const {
  2119. mapKeyBirth.clear();
  2120. // get birth times for keys with metadata
  2121. for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
  2122. if (it->second.nCreateTime)
  2123. mapKeyBirth[it->first] = it->second.nCreateTime;
  2124. // map in which we'll infer heights of other keys
  2125. CBlockIndex *pindexMax = FindBlockByHeight(std::max(0, nBestHeight - 144)); // the tip can be reorganised; use a 144-block safety margin
  2126. std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
  2127. std::set<CKeyID> setKeys;
  2128. GetKeys(setKeys);
  2129. BOOST_FOREACH(const CKeyID &keyid, setKeys) {
  2130. if (mapKeyBirth.count(keyid) == 0)
  2131. mapKeyFirstBlock[keyid] = pindexMax;
  2132. }
  2133. setKeys.clear();
  2134. // if there are no such keys, we're done
  2135. if (mapKeyFirstBlock.empty())
  2136. return;
  2137. // find first block that affects those keys, if there are any left
  2138. std::vector<CKeyID> vAffected;
  2139. for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
  2140. // iterate over all wallet transactions...
  2141. const CWalletTx &wtx = (*it).second;
  2142. std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
  2143. if (blit != mapBlockIndex.end() && blit->second->IsInMainChain()) {
  2144. // ... which are already in a block
  2145. int nHeight = blit->second->nHeight;
  2146. BOOST_FOREACH(const CTxOut &txout, wtx.vout) {
  2147. // iterate over all their outputs
  2148. //::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected);
  2149. BOOST_FOREACH(const CKeyID &keyid, vAffected) {
  2150. // ... and all their affected keys
  2151. std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
  2152. if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
  2153. rit->second = blit->second;
  2154. }
  2155. vAffected.clear();
  2156. }
  2157. }
  2158. }
  2159. // Extract block timestamps for those keys
  2160. for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
  2161. mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off
  2162. }