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.

policyestimator_tests.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (c) 2011-2015 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 "policy/fees.h"
  5. #include "txmempool.h"
  6. #include "uint256.h"
  7. #include "util.h"
  8. #include "test/test_bitcoin.h"
  9. #include <boost/test/unit_test.hpp>
  10. BOOST_FIXTURE_TEST_SUITE(policyestimator_tests, BasicTestingSetup)
  11. BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
  12. {
  13. CTxMemPool mpool(CFeeRate(1000));
  14. CAmount basefee(2000);
  15. double basepri = 1e6;
  16. CAmount deltaFee(100);
  17. double deltaPri=5e5;
  18. std::vector<CAmount> feeV[2];
  19. std::vector<double> priV[2];
  20. // Populate vectors of increasing fees or priorities
  21. for (int j = 0; j < 10; j++) {
  22. //V[0] is for fee transactions
  23. feeV[0].push_back(basefee * (j+1));
  24. priV[0].push_back(0);
  25. //V[1] is for priority transactions
  26. feeV[1].push_back(CAmount(0));
  27. priV[1].push_back(basepri * pow(10, j+1));
  28. }
  29. // Store the hashes of transactions that have been
  30. // added to the mempool by their associate fee/pri
  31. // txHashes[j] is populated with transactions either of
  32. // fee = basefee * (j+1) OR pri = 10^6 * 10^(j+1)
  33. std::vector<uint256> txHashes[10];
  34. // Create a transaction template
  35. CScript garbage;
  36. for (unsigned int i = 0; i < 128; i++)
  37. garbage.push_back('X');
  38. CMutableTransaction tx;
  39. std::list<CTransaction> dummyConflicted;
  40. tx.vin.resize(1);
  41. tx.vin[0].scriptSig = garbage;
  42. tx.vout.resize(1);
  43. tx.vout[0].nValue=0LL;
  44. CFeeRate baseRate(basefee, ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
  45. // Create a fake block
  46. std::vector<CTransaction> block;
  47. int blocknum = 0;
  48. // Loop through 200 blocks
  49. // At a decay .998 and 4 fee transactions per block
  50. // This makes the tx count about 1.33 per bucket, above the 1 threshold
  51. while (blocknum < 200) {
  52. for (int j = 0; j < 10; j++) { // For each fee/pri multiple
  53. for (int k = 0; k < 5; k++) { // add 4 fee txs for every priority tx
  54. tx.vin[0].prevout.n = 10000*blocknum+100*j+k; // make transaction unique
  55. uint256 hash = tx.GetHash();
  56. mpool.addUnchecked(hash, CTxMemPoolEntry(tx, feeV[k/4][j], GetTime(), priV[k/4][j], blocknum, mpool.HasNoInputsOf(tx)));
  57. txHashes[j].push_back(hash);
  58. }
  59. }
  60. //Create blocks where higher fee/pri txs are included more often
  61. for (int h = 0; h <= blocknum%10; h++) {
  62. // 10/10 blocks add highest fee/pri transactions
  63. // 9/10 blocks add 2nd highest and so on until ...
  64. // 1/10 blocks add lowest fee/pri transactions
  65. while (txHashes[9-h].size()) {
  66. CTransaction btx;
  67. if (mpool.lookup(txHashes[9-h].back(), btx))
  68. block.push_back(btx);
  69. txHashes[9-h].pop_back();
  70. }
  71. }
  72. mpool.removeForBlock(block, ++blocknum, dummyConflicted);
  73. block.clear();
  74. if (blocknum == 30) {
  75. // At this point we should need to combine 5 buckets to get enough data points
  76. // So estimateFee(1) should fail and estimateFee(2) should return somewhere around
  77. // 8*baserate
  78. BOOST_CHECK(mpool.estimateFee(1) == CFeeRate(0));
  79. BOOST_CHECK(mpool.estimateFee(2).GetFeePerK() < 8*baseRate.GetFeePerK() + deltaFee);
  80. BOOST_CHECK(mpool.estimateFee(2).GetFeePerK() > 8*baseRate.GetFeePerK() - deltaFee);
  81. }
  82. }
  83. std::vector<CAmount> origFeeEst;
  84. std::vector<double> origPriEst;
  85. // Highest feerate is 10*baseRate and gets in all blocks,
  86. // second highest feerate is 9*baseRate and gets in 9/10 blocks = 90%,
  87. // third highest feerate is 8*base rate, and gets in 8/10 blocks = 80%,
  88. // so estimateFee(1) should return 9*baseRate.
  89. // Third highest feerate has 90% chance of being included by 2 blocks,
  90. // so estimateFee(2) should return 8*baseRate etc...
  91. for (int i = 1; i < 10;i++) {
  92. origFeeEst.push_back(mpool.estimateFee(i).GetFeePerK());
  93. origPriEst.push_back(mpool.estimatePriority(i));
  94. if (i > 1) { // Fee estimates should be monotonically decreasing
  95. BOOST_CHECK(origFeeEst[i-1] <= origFeeEst[i-2]);
  96. BOOST_CHECK(origPriEst[i-1] <= origPriEst[i-2]);
  97. }
  98. BOOST_CHECK(origFeeEst[i-1] < (10-i)*baseRate.GetFeePerK() + deltaFee);
  99. BOOST_CHECK(origFeeEst[i-1] > (10-i)*baseRate.GetFeePerK() - deltaFee);
  100. BOOST_CHECK(origPriEst[i-1] < pow(10,10-i) * basepri + deltaPri);
  101. BOOST_CHECK(origPriEst[i-1] > pow(10,10-i) * basepri - deltaPri);
  102. }
  103. // Mine 50 more blocks with no transactions happening, estimates shouldn't change
  104. // We haven't decayed the moving average enough so we still have enough data points in every bucket
  105. while (blocknum < 250)
  106. mpool.removeForBlock(block, ++blocknum, dummyConflicted);
  107. for (int i = 1; i < 10;i++) {
  108. BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] + deltaFee);
  109. BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
  110. BOOST_CHECK(mpool.estimatePriority(i) < origPriEst[i-1] + deltaPri);
  111. BOOST_CHECK(mpool.estimatePriority(i) > origPriEst[i-1] - deltaPri);
  112. }
  113. // Mine 15 more blocks with lots of transactions happening and not getting mined
  114. // Estimates should go up
  115. while (blocknum < 265) {
  116. for (int j = 0; j < 10; j++) { // For each fee/pri multiple
  117. for (int k = 0; k < 5; k++) { // add 4 fee txs for every priority tx
  118. tx.vin[0].prevout.n = 10000*blocknum+100*j+k;
  119. uint256 hash = tx.GetHash();
  120. mpool.addUnchecked(hash, CTxMemPoolEntry(tx, feeV[k/4][j], GetTime(), priV[k/4][j], blocknum, mpool.HasNoInputsOf(tx)));
  121. txHashes[j].push_back(hash);
  122. }
  123. }
  124. mpool.removeForBlock(block, ++blocknum, dummyConflicted);
  125. }
  126. for (int i = 1; i < 10;i++) {
  127. BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
  128. BOOST_CHECK(mpool.estimatePriority(i) > origPriEst[i-1] - deltaPri);
  129. }
  130. // Mine all those transactions
  131. // Estimates should still not be below original
  132. for (int j = 0; j < 10; j++) {
  133. while(txHashes[j].size()) {
  134. CTransaction btx;
  135. if (mpool.lookup(txHashes[j].back(), btx))
  136. block.push_back(btx);
  137. txHashes[j].pop_back();
  138. }
  139. }
  140. mpool.removeForBlock(block, 265, dummyConflicted);
  141. block.clear();
  142. for (int i = 1; i < 10;i++) {
  143. BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() > origFeeEst[i-1] - deltaFee);
  144. BOOST_CHECK(mpool.estimatePriority(i) > origPriEst[i-1] - deltaPri);
  145. }
  146. // Mine 100 more blocks where everything is mined every block
  147. // Estimates should be below original estimates (not possible for last estimate)
  148. while (blocknum < 365) {
  149. for (int j = 0; j < 10; j++) { // For each fee/pri multiple
  150. for (int k = 0; k < 5; k++) { // add 4 fee txs for every priority tx
  151. tx.vin[0].prevout.n = 10000*blocknum+100*j+k;
  152. uint256 hash = tx.GetHash();
  153. mpool.addUnchecked(hash, CTxMemPoolEntry(tx, feeV[k/4][j], GetTime(), priV[k/4][j], blocknum, mpool.HasNoInputsOf(tx)));
  154. CTransaction btx;
  155. if (mpool.lookup(hash, btx))
  156. block.push_back(btx);
  157. }
  158. }
  159. mpool.removeForBlock(block, ++blocknum, dummyConflicted);
  160. block.clear();
  161. }
  162. for (int i = 1; i < 9; i++) {
  163. BOOST_CHECK(mpool.estimateFee(i).GetFeePerK() < origFeeEst[i-1] - deltaFee);
  164. BOOST_CHECK(mpool.estimatePriority(i) < origPriEst[i-1] - deltaPri);
  165. }
  166. }
  167. BOOST_AUTO_TEST_SUITE_END()