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_tests.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2014 The Bitcoin Core 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 "random.h"
  6. #include "uint256.h"
  7. #include "test/test_bitcoin.h"
  8. #include <vector>
  9. #include <map>
  10. #include <boost/test/unit_test.hpp>
  11. namespace
  12. {
  13. class CCoinsViewTest : public CCoinsView
  14. {
  15. uint256 hashBestBlock_;
  16. std::map<uint256, CCoins> map_;
  17. public:
  18. bool GetCoins(const uint256& txid, CCoins& coins) const
  19. {
  20. std::map<uint256, CCoins>::const_iterator it = map_.find(txid);
  21. if (it == map_.end()) {
  22. return false;
  23. }
  24. coins = it->second;
  25. if (coins.IsPruned() && insecure_rand() % 2 == 0) {
  26. // Randomly return false in case of an empty entry.
  27. return false;
  28. }
  29. return true;
  30. }
  31. bool HaveCoins(const uint256& txid) const
  32. {
  33. CCoins coins;
  34. return GetCoins(txid, coins);
  35. }
  36. uint256 GetBestBlock() const { return hashBestBlock_; }
  37. bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock)
  38. {
  39. for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
  40. map_[it->first] = it->second.coins;
  41. if (it->second.coins.IsPruned() && insecure_rand() % 3 == 0) {
  42. // Randomly delete empty entries on write.
  43. map_.erase(it->first);
  44. }
  45. mapCoins.erase(it++);
  46. }
  47. mapCoins.clear();
  48. hashBestBlock_ = hashBlock;
  49. return true;
  50. }
  51. bool GetStats(CCoinsStats& stats) const { return false; }
  52. };
  53. class CCoinsViewCacheTest : public CCoinsViewCache
  54. {
  55. public:
  56. CCoinsViewCacheTest(CCoinsView* base) : CCoinsViewCache(base) {}
  57. void SelfTest() const
  58. {
  59. // Manually recompute the dynamic usage of the whole data, and compare it.
  60. size_t ret = memusage::DynamicUsage(cacheCoins);
  61. for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) {
  62. ret += memusage::DynamicUsage(it->second.coins);
  63. }
  64. BOOST_CHECK_EQUAL(memusage::DynamicUsage(*this), ret);
  65. }
  66. };
  67. }
  68. BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
  69. static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
  70. // This is a large randomized insert/remove simulation test on a variable-size
  71. // stack of caches on top of CCoinsViewTest.
  72. //
  73. // It will randomly create/update/delete CCoins entries to a tip of caches, with
  74. // txids picked from a limited list of random 256-bit hashes. Occasionally, a
  75. // new tip is added to the stack of caches, or the tip is flushed and removed.
  76. //
  77. // During the process, booleans are kept to make sure that the randomized
  78. // operation hits all branches.
  79. BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
  80. {
  81. // Various coverage trackers.
  82. bool removed_all_caches = false;
  83. bool reached_4_caches = false;
  84. bool added_an_entry = false;
  85. bool removed_an_entry = false;
  86. bool updated_an_entry = false;
  87. bool found_an_entry = false;
  88. bool missed_an_entry = false;
  89. // A simple map to track what we expect the cache stack to represent.
  90. std::map<uint256, CCoins> result;
  91. // The cache stack.
  92. CCoinsViewTest base; // A CCoinsViewTest at the bottom.
  93. std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
  94. stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache.
  95. // Use a limited set of random transaction ids, so we do test overwriting entries.
  96. std::vector<uint256> txids;
  97. txids.resize(NUM_SIMULATION_ITERATIONS / 8);
  98. for (unsigned int i = 0; i < txids.size(); i++) {
  99. txids[i] = GetRandHash();
  100. }
  101. for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
  102. // Do a random modification.
  103. {
  104. uint256 txid = txids[insecure_rand() % txids.size()]; // txid we're going to modify in this iteration.
  105. CCoins& coins = result[txid];
  106. CCoinsModifier entry = stack.back()->ModifyCoins(txid);
  107. BOOST_CHECK(coins == *entry);
  108. if (insecure_rand() % 5 == 0 || coins.IsPruned()) {
  109. if (coins.IsPruned()) {
  110. added_an_entry = true;
  111. } else {
  112. updated_an_entry = true;
  113. }
  114. coins.nVersion = insecure_rand();
  115. coins.vout.resize(1);
  116. coins.vout[0].nValue = insecure_rand();
  117. *entry = coins;
  118. } else {
  119. coins.Clear();
  120. entry->Clear();
  121. removed_an_entry = true;
  122. }
  123. }
  124. // Once every 1000 iterations and at the end, verify the full cache.
  125. if (insecure_rand() % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
  126. for (std::map<uint256, CCoins>::iterator it = result.begin(); it != result.end(); it++) {
  127. const CCoins* coins = stack.back()->AccessCoins(it->first);
  128. if (coins) {
  129. BOOST_CHECK(*coins == it->second);
  130. found_an_entry = true;
  131. } else {
  132. BOOST_CHECK(it->second.IsPruned());
  133. missed_an_entry = true;
  134. }
  135. }
  136. BOOST_FOREACH(const CCoinsViewCacheTest *test, stack) {
  137. test->SelfTest();
  138. }
  139. }
  140. if (insecure_rand() % 100 == 0) {
  141. // Every 100 iterations, change the cache stack.
  142. if (stack.size() > 0 && insecure_rand() % 2 == 0) {
  143. stack.back()->Flush();
  144. delete stack.back();
  145. stack.pop_back();
  146. }
  147. if (stack.size() == 0 || (stack.size() < 4 && insecure_rand() % 2)) {
  148. CCoinsView* tip = &base;
  149. if (stack.size() > 0) {
  150. tip = stack.back();
  151. } else {
  152. removed_all_caches = true;
  153. }
  154. stack.push_back(new CCoinsViewCacheTest(tip));
  155. if (stack.size() == 4) {
  156. reached_4_caches = true;
  157. }
  158. }
  159. }
  160. }
  161. // Clean up the stack.
  162. while (stack.size() > 0) {
  163. delete stack.back();
  164. stack.pop_back();
  165. }
  166. // Verify coverage.
  167. BOOST_CHECK(removed_all_caches);
  168. BOOST_CHECK(reached_4_caches);
  169. BOOST_CHECK(added_an_entry);
  170. BOOST_CHECK(removed_an_entry);
  171. BOOST_CHECK(updated_an_entry);
  172. BOOST_CHECK(found_an_entry);
  173. BOOST_CHECK(missed_an_entry);
  174. }
  175. BOOST_AUTO_TEST_SUITE_END()