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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (c) 2012-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "coins.h"
  5. #include "consensus/consensus.h"
  6. #include "memusage.h"
  7. #include "random.h"
  8. #include <assert.h>
  9. bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
  10. uint256 CCoinsView::GetBestBlock() const { return uint256(); }
  11. std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
  12. bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
  13. CCoinsViewCursor *CCoinsView::Cursor() const { return 0; }
  14. bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
  15. {
  16. Coin coin;
  17. return GetCoin(outpoint, coin);
  18. }
  19. CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
  20. bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { return base->GetCoin(outpoint, coin); }
  21. bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
  22. uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
  23. std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
  24. void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
  25. bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
  26. CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
  27. size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
  28. SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
  29. CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {}
  30. size_t CCoinsViewCache::DynamicMemoryUsage() const {
  31. return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
  32. }
  33. CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
  34. CCoinsMap::iterator it = cacheCoins.find(outpoint);
  35. if (it != cacheCoins.end())
  36. return it;
  37. Coin tmp;
  38. if (!base->GetCoin(outpoint, tmp))
  39. return cacheCoins.end();
  40. CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
  41. if (ret->second.coin.IsSpent()) {
  42. // The parent only has an empty entry for this outpoint; we can consider our
  43. // version as fresh.
  44. ret->second.flags = CCoinsCacheEntry::FRESH;
  45. }
  46. cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
  47. return ret;
  48. }
  49. bool CCoinsViewCache::GetCoin(const COutPoint &outpoint, Coin &coin) const {
  50. CCoinsMap::const_iterator it = FetchCoin(outpoint);
  51. if (it != cacheCoins.end()) {
  52. coin = it->second.coin;
  53. return !coin.IsSpent();
  54. }
  55. return false;
  56. }
  57. void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
  58. assert(!coin.IsSpent());
  59. if (coin.out.scriptPubKey.IsUnspendable()) return;
  60. CCoinsMap::iterator it;
  61. bool inserted;
  62. std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
  63. bool fresh = false;
  64. if (!inserted) {
  65. cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
  66. }
  67. if (!possible_overwrite) {
  68. if (!it->second.coin.IsSpent()) {
  69. throw std::logic_error("Adding new coin that replaces non-pruned entry");
  70. }
  71. fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY);
  72. }
  73. it->second.coin = std::move(coin);
  74. it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
  75. cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
  76. }
  77. void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check) {
  78. bool fCoinbase = tx.IsCoinBase();
  79. const uint256& txid = tx.GetHash();
  80. for (size_t i = 0; i < tx.vout.size(); ++i) {
  81. bool overwrite = check ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
  82. // Always set the possible_overwrite flag to AddCoin for coinbase txn, in order to correctly
  83. // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
  84. cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
  85. }
  86. }
  87. bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
  88. CCoinsMap::iterator it = FetchCoin(outpoint);
  89. if (it == cacheCoins.end()) return false;
  90. cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
  91. if (moveout) {
  92. *moveout = std::move(it->second.coin);
  93. }
  94. if (it->second.flags & CCoinsCacheEntry::FRESH) {
  95. cacheCoins.erase(it);
  96. } else {
  97. it->second.flags |= CCoinsCacheEntry::DIRTY;
  98. it->second.coin.Clear();
  99. }
  100. return true;
  101. }
  102. static const Coin coinEmpty;
  103. const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
  104. CCoinsMap::const_iterator it = FetchCoin(outpoint);
  105. if (it == cacheCoins.end()) {
  106. return coinEmpty;
  107. } else {
  108. return it->second.coin;
  109. }
  110. }
  111. bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
  112. CCoinsMap::const_iterator it = FetchCoin(outpoint);
  113. return (it != cacheCoins.end() && !it->second.coin.IsSpent());
  114. }
  115. bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
  116. CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
  117. return (it != cacheCoins.end() && !it->second.coin.IsSpent());
  118. }
  119. uint256 CCoinsViewCache::GetBestBlock() const {
  120. if (hashBlock.IsNull())
  121. hashBlock = base->GetBestBlock();
  122. return hashBlock;
  123. }
  124. void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
  125. hashBlock = hashBlockIn;
  126. }
  127. bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
  128. for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
  129. if (it->second.flags & CCoinsCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
  130. CCoinsMap::iterator itUs = cacheCoins.find(it->first);
  131. if (itUs == cacheCoins.end()) {
  132. // The parent cache does not have an entry, while the child does
  133. // We can ignore it if it's both FRESH and pruned in the child
  134. if (!(it->second.flags & CCoinsCacheEntry::FRESH && it->second.coin.IsSpent())) {
  135. // Otherwise we will need to create it in the parent
  136. // and move the data up and mark it as dirty
  137. CCoinsCacheEntry& entry = cacheCoins[it->first];
  138. entry.coin = std::move(it->second.coin);
  139. cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
  140. entry.flags = CCoinsCacheEntry::DIRTY;
  141. // We can mark it FRESH in the parent if it was FRESH in the child
  142. // Otherwise it might have just been flushed from the parent's cache
  143. // and already exist in the grandparent
  144. if (it->second.flags & CCoinsCacheEntry::FRESH)
  145. entry.flags |= CCoinsCacheEntry::FRESH;
  146. }
  147. } else {
  148. // Assert that the child cache entry was not marked FRESH if the
  149. // parent cache entry has unspent outputs. If this ever happens,
  150. // it means the FRESH flag was misapplied and there is a logic
  151. // error in the calling code.
  152. if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coin.IsSpent())
  153. throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
  154. // Found the entry in the parent cache
  155. if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coin.IsSpent()) {
  156. // The grandparent does not have an entry, and the child is
  157. // modified and being pruned. This means we can just delete
  158. // it from the parent.
  159. cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
  160. cacheCoins.erase(itUs);
  161. } else {
  162. // A normal modification.
  163. cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
  164. itUs->second.coin = std::move(it->second.coin);
  165. cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
  166. itUs->second.flags |= CCoinsCacheEntry::DIRTY;
  167. // NOTE: It is possible the child has a FRESH flag here in
  168. // the event the entry we found in the parent is pruned. But
  169. // we must not copy that FRESH flag to the parent as that
  170. // pruned state likely still needs to be communicated to the
  171. // grandparent.
  172. }
  173. }
  174. }
  175. CCoinsMap::iterator itOld = it++;
  176. mapCoins.erase(itOld);
  177. }
  178. hashBlock = hashBlockIn;
  179. return true;
  180. }
  181. bool CCoinsViewCache::Flush() {
  182. bool fOk = base->BatchWrite(cacheCoins, hashBlock);
  183. cacheCoins.clear();
  184. cachedCoinsUsage = 0;
  185. return fOk;
  186. }
  187. void CCoinsViewCache::Uncache(const COutPoint& hash)
  188. {
  189. CCoinsMap::iterator it = cacheCoins.find(hash);
  190. if (it != cacheCoins.end() && it->second.flags == 0) {
  191. cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
  192. cacheCoins.erase(it);
  193. }
  194. }
  195. unsigned int CCoinsViewCache::GetCacheSize() const {
  196. return cacheCoins.size();
  197. }
  198. CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
  199. {
  200. if (tx.IsCoinBase())
  201. return 0;
  202. CAmount nResult = 0;
  203. for (unsigned int i = 0; i < tx.vin.size(); i++)
  204. nResult += AccessCoin(tx.vin[i].prevout).out.nValue;
  205. return nResult;
  206. }
  207. bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
  208. {
  209. if (!tx.IsCoinBase()) {
  210. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  211. if (!HaveCoin(tx.vin[i].prevout)) {
  212. return false;
  213. }
  214. }
  215. }
  216. return true;
  217. }
  218. static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut(), SER_NETWORK, PROTOCOL_VERSION);
  219. static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT;
  220. const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
  221. {
  222. COutPoint iter(txid, 0);
  223. while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
  224. const Coin& alternate = view.AccessCoin(iter);
  225. if (!alternate.IsSpent()) return alternate;
  226. ++iter.n;
  227. }
  228. return coinEmpty;
  229. }