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.

coins.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (c) 2012-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "coins.h"
  5. #include "random.h"
  6. #include <assert.h>
  7. // calculate number of bytes for the bitmask, and its number of non-zero bytes
  8. // each bit in the bitmask represents the availability of one output, but the
  9. // availabilities of the first two outputs are encoded separately
  10. void CCoins::CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const {
  11. unsigned int nLastUsedByte = 0;
  12. for (unsigned int b = 0; 2+b*8 < vout.size(); b++) {
  13. bool fZero = true;
  14. for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) {
  15. if (!vout[2+b*8+i].IsNull()) {
  16. fZero = false;
  17. continue;
  18. }
  19. }
  20. if (!fZero) {
  21. nLastUsedByte = b + 1;
  22. nNonzeroBytes++;
  23. }
  24. }
  25. nBytes += nLastUsedByte;
  26. }
  27. bool CCoins::Spend(const COutPoint &out, CTxInUndo &undo) {
  28. if (out.n >= vout.size())
  29. return false;
  30. if (vout[out.n].IsNull())
  31. return false;
  32. undo = CTxInUndo(vout[out.n]);
  33. vout[out.n].SetNull();
  34. Cleanup();
  35. if (vout.size() == 0) {
  36. undo.nHeight = nHeight;
  37. undo.fCoinBase = fCoinBase;
  38. undo.nVersion = this->nVersion;
  39. }
  40. return true;
  41. }
  42. bool CCoins::Spend(int nPos) {
  43. CTxInUndo undo;
  44. COutPoint out(0, nPos);
  45. return Spend(out, undo);
  46. }
  47. bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
  48. bool CCoinsView::SetCoins(const uint256 &txid, const CCoins &coins) { return false; }
  49. bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
  50. uint256 CCoinsView::GetBestBlock() const { return uint256(0); }
  51. bool CCoinsView::SetBestBlock(const uint256 &hashBlock) { return false; }
  52. bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
  53. bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }
  54. CCoinsViewBacked::CCoinsViewBacked(CCoinsView &viewIn) : base(&viewIn) { }
  55. bool CCoinsViewBacked::GetCoins(const uint256 &txid, CCoins &coins) const { return base->GetCoins(txid, coins); }
  56. bool CCoinsViewBacked::SetCoins(const uint256 &txid, const CCoins &coins) { return base->SetCoins(txid, coins); }
  57. bool CCoinsViewBacked::HaveCoins(const uint256 &txid) const { return base->HaveCoins(txid); }
  58. uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
  59. bool CCoinsViewBacked::SetBestBlock(const uint256 &hashBlock) { return base->SetBestBlock(hashBlock); }
  60. void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
  61. bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
  62. bool CCoinsViewBacked::GetStats(CCoinsStats &stats) const { return base->GetStats(stats); }
  63. CCoinsKeyHasher::CCoinsKeyHasher() : salt(GetRandHash()) {}
  64. CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), hashBlock(0) { }
  65. bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) const {
  66. if (cacheCoins.count(txid)) {
  67. coins = cacheCoins[txid];
  68. return true;
  69. }
  70. if (base->GetCoins(txid, coins)) {
  71. cacheCoins[txid] = coins;
  72. return true;
  73. }
  74. return false;
  75. }
  76. CCoinsMap::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
  77. CCoinsMap::iterator it = cacheCoins.find(txid);
  78. if (it != cacheCoins.end())
  79. return it;
  80. CCoins tmp;
  81. if (!base->GetCoins(txid,tmp))
  82. return cacheCoins.end();
  83. CCoinsMap::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
  84. tmp.swap(ret->second);
  85. return ret;
  86. }
  87. CCoinsMap::const_iterator CCoinsViewCache::FetchCoins(const uint256 &txid) const {
  88. /* Avoid redundant implementation with the const-cast. */
  89. return const_cast<CCoinsViewCache*>(this)->FetchCoins(txid);
  90. }
  91. CCoins &CCoinsViewCache::GetCoins(const uint256 &txid) {
  92. CCoinsMap::iterator it = FetchCoins(txid);
  93. assert(it != cacheCoins.end());
  94. return it->second;
  95. }
  96. const CCoins &CCoinsViewCache::GetCoins(const uint256 &txid) const {
  97. /* Avoid redundant implementation with the const-cast. */
  98. return const_cast<CCoinsViewCache*>(this)->GetCoins(txid);
  99. }
  100. bool CCoinsViewCache::SetCoins(const uint256 &txid, const CCoins &coins) {
  101. cacheCoins[txid] = coins;
  102. return true;
  103. }
  104. bool CCoinsViewCache::HaveCoins(const uint256 &txid) const {
  105. CCoinsMap::const_iterator it = FetchCoins(txid);
  106. // We're using vtx.empty() instead of IsPruned here for performance reasons,
  107. // as we only care about the case where an transaction was replaced entirely
  108. // in a reorganization (which wipes vout entirely, as opposed to spending
  109. // which just cleans individual outputs).
  110. return (it != cacheCoins.end() && !it->second.vout.empty());
  111. }
  112. uint256 CCoinsViewCache::GetBestBlock() const {
  113. if (hashBlock == uint256(0))
  114. hashBlock = base->GetBestBlock();
  115. return hashBlock;
  116. }
  117. bool CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
  118. hashBlock = hashBlockIn;
  119. return true;
  120. }
  121. bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
  122. for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
  123. cacheCoins[it->first].swap(it->second);
  124. CCoinsMap::iterator itOld = it++;
  125. mapCoins.erase(itOld);
  126. }
  127. hashBlock = hashBlockIn;
  128. return true;
  129. }
  130. bool CCoinsViewCache::Flush() {
  131. bool fOk = base->BatchWrite(cacheCoins, hashBlock);
  132. cacheCoins.clear();
  133. return fOk;
  134. }
  135. unsigned int CCoinsViewCache::GetCacheSize() const {
  136. return cacheCoins.size();
  137. }
  138. const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
  139. {
  140. const CCoins &coins = GetCoins(input.prevout.hash);
  141. assert(coins.IsAvailable(input.prevout.n));
  142. return coins.vout[input.prevout.n];
  143. }
  144. int64_t CCoinsViewCache::GetValueIn(const CTransaction& tx) const
  145. {
  146. if (tx.IsCoinBase())
  147. return 0;
  148. int64_t nResult = 0;
  149. for (unsigned int i = 0; i < tx.vin.size(); i++)
  150. nResult += GetOutputFor(tx.vin[i]).nValue;
  151. return nResult;
  152. }
  153. bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
  154. {
  155. if (!tx.IsCoinBase()) {
  156. // first check whether information about the prevout hash is available
  157. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  158. const COutPoint &prevout = tx.vin[i].prevout;
  159. if (!HaveCoins(prevout.hash))
  160. return false;
  161. }
  162. // then check whether the actual outputs are available
  163. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  164. const COutPoint &prevout = tx.vin[i].prevout;
  165. const CCoins &coins = GetCoins(prevout.hash);
  166. if (!coins.IsAvailable(prevout.n))
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. double CCoinsViewCache::GetPriority(const CTransaction &tx, int nHeight) const
  173. {
  174. if (tx.IsCoinBase())
  175. return 0.0;
  176. double dResult = 0.0;
  177. BOOST_FOREACH(const CTxIn& txin, tx.vin)
  178. {
  179. const CCoins &coins = GetCoins(txin.prevout.hash);
  180. if (!coins.IsAvailable(txin.prevout.n)) continue;
  181. if (coins.nHeight < nHeight) {
  182. dResult += coins.vout[txin.prevout.n].nValue * (nHeight-coins.nHeight);
  183. }
  184. }
  185. return tx.ComputePriority(dResult);
  186. }