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.

mempool_tests.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (c) 2011-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 "txmempool.h"
  5. #include "util.h"
  6. #include "test/test_bitcoin.h"
  7. #include <boost/test/unit_test.hpp>
  8. #include <list>
  9. #include <vector>
  10. BOOST_FIXTURE_TEST_SUITE(mempool_tests, TestingSetup)
  11. BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
  12. {
  13. // Test CTxMemPool::remove functionality
  14. // Parent transaction with three children,
  15. // and three grand-children:
  16. CMutableTransaction txParent;
  17. txParent.vin.resize(1);
  18. txParent.vin[0].scriptSig = CScript() << OP_11;
  19. txParent.vout.resize(3);
  20. for (int i = 0; i < 3; i++)
  21. {
  22. txParent.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  23. txParent.vout[i].nValue = 33000LL;
  24. }
  25. CMutableTransaction txChild[3];
  26. for (int i = 0; i < 3; i++)
  27. {
  28. txChild[i].vin.resize(1);
  29. txChild[i].vin[0].scriptSig = CScript() << OP_11;
  30. txChild[i].vin[0].prevout.hash = txParent.GetHash();
  31. txChild[i].vin[0].prevout.n = i;
  32. txChild[i].vout.resize(1);
  33. txChild[i].vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  34. txChild[i].vout[0].nValue = 11000LL;
  35. }
  36. CMutableTransaction txGrandChild[3];
  37. for (int i = 0; i < 3; i++)
  38. {
  39. txGrandChild[i].vin.resize(1);
  40. txGrandChild[i].vin[0].scriptSig = CScript() << OP_11;
  41. txGrandChild[i].vin[0].prevout.hash = txChild[i].GetHash();
  42. txGrandChild[i].vin[0].prevout.n = 0;
  43. txGrandChild[i].vout.resize(1);
  44. txGrandChild[i].vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  45. txGrandChild[i].vout[0].nValue = 11000LL;
  46. }
  47. CTxMemPool testPool(CFeeRate(0));
  48. std::list<CTransaction> removed;
  49. // Nothing in pool, remove should do nothing:
  50. testPool.remove(txParent, removed, true);
  51. BOOST_CHECK_EQUAL(removed.size(), 0);
  52. // Just the parent:
  53. testPool.addUnchecked(txParent.GetHash(), CTxMemPoolEntry(txParent, 0, 0, 0.0, 1));
  54. testPool.remove(txParent, removed, true);
  55. BOOST_CHECK_EQUAL(removed.size(), 1);
  56. removed.clear();
  57. // Parent, children, grandchildren:
  58. testPool.addUnchecked(txParent.GetHash(), CTxMemPoolEntry(txParent, 0, 0, 0.0, 1));
  59. for (int i = 0; i < 3; i++)
  60. {
  61. testPool.addUnchecked(txChild[i].GetHash(), CTxMemPoolEntry(txChild[i], 0, 0, 0.0, 1));
  62. testPool.addUnchecked(txGrandChild[i].GetHash(), CTxMemPoolEntry(txGrandChild[i], 0, 0, 0.0, 1));
  63. }
  64. // Remove Child[0], GrandChild[0] should be removed:
  65. testPool.remove(txChild[0], removed, true);
  66. BOOST_CHECK_EQUAL(removed.size(), 2);
  67. removed.clear();
  68. // ... make sure grandchild and child are gone:
  69. testPool.remove(txGrandChild[0], removed, true);
  70. BOOST_CHECK_EQUAL(removed.size(), 0);
  71. testPool.remove(txChild[0], removed, true);
  72. BOOST_CHECK_EQUAL(removed.size(), 0);
  73. // Remove parent, all children/grandchildren should go:
  74. testPool.remove(txParent, removed, true);
  75. BOOST_CHECK_EQUAL(removed.size(), 5);
  76. BOOST_CHECK_EQUAL(testPool.size(), 0);
  77. removed.clear();
  78. // Add children and grandchildren, but NOT the parent (simulate the parent being in a block)
  79. for (int i = 0; i < 3; i++)
  80. {
  81. testPool.addUnchecked(txChild[i].GetHash(), CTxMemPoolEntry(txChild[i], 0, 0, 0.0, 1));
  82. testPool.addUnchecked(txGrandChild[i].GetHash(), CTxMemPoolEntry(txGrandChild[i], 0, 0, 0.0, 1));
  83. }
  84. // Now remove the parent, as might happen if a block-re-org occurs but the parent cannot be
  85. // put into the mempool (maybe because it is non-standard):
  86. testPool.remove(txParent, removed, true);
  87. BOOST_CHECK_EQUAL(removed.size(), 6);
  88. BOOST_CHECK_EQUAL(testPool.size(), 0);
  89. removed.clear();
  90. }
  91. void CheckSort(CTxMemPool &pool, std::vector<std::string> &sortedOrder)
  92. {
  93. BOOST_CHECK_EQUAL(pool.size(), sortedOrder.size());
  94. CTxMemPool::indexed_transaction_set::nth_index<1>::type::iterator it = pool.mapTx.get<1>().begin();
  95. int count=0;
  96. for (; it != pool.mapTx.get<1>().end(); ++it, ++count) {
  97. BOOST_CHECK_EQUAL(it->GetTx().GetHash().ToString(), sortedOrder[count]);
  98. }
  99. }
  100. BOOST_AUTO_TEST_CASE(MempoolIndexingTest)
  101. {
  102. CTxMemPool pool(CFeeRate(0));
  103. /* 3rd highest fee */
  104. CMutableTransaction tx1 = CMutableTransaction();
  105. tx1.vout.resize(1);
  106. tx1.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  107. tx1.vout[0].nValue = 10 * COIN;
  108. pool.addUnchecked(tx1.GetHash(), CTxMemPoolEntry(tx1, 10000LL, 0, 10.0, 1, true));
  109. /* highest fee */
  110. CMutableTransaction tx2 = CMutableTransaction();
  111. tx2.vout.resize(1);
  112. tx2.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  113. tx2.vout[0].nValue = 2 * COIN;
  114. pool.addUnchecked(tx2.GetHash(), CTxMemPoolEntry(tx2, 20000LL, 0, 9.0, 1, true));
  115. /* lowest fee */
  116. CMutableTransaction tx3 = CMutableTransaction();
  117. tx3.vout.resize(1);
  118. tx3.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  119. tx3.vout[0].nValue = 5 * COIN;
  120. pool.addUnchecked(tx3.GetHash(), CTxMemPoolEntry(tx3, 0LL, 0, 100.0, 1, true));
  121. /* 2nd highest fee */
  122. CMutableTransaction tx4 = CMutableTransaction();
  123. tx4.vout.resize(1);
  124. tx4.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  125. tx4.vout[0].nValue = 6 * COIN;
  126. pool.addUnchecked(tx4.GetHash(), CTxMemPoolEntry(tx4, 15000LL, 0, 1.0, 1, true));
  127. /* equal fee rate to tx1, but newer */
  128. CMutableTransaction tx5 = CMutableTransaction();
  129. tx5.vout.resize(1);
  130. tx5.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  131. tx5.vout[0].nValue = 11 * COIN;
  132. pool.addUnchecked(tx5.GetHash(), CTxMemPoolEntry(tx5, 10000LL, 1, 10.0, 1, true));
  133. BOOST_CHECK_EQUAL(pool.size(), 5);
  134. std::vector<std::string> sortedOrder;
  135. sortedOrder.resize(5);
  136. sortedOrder[0] = tx2.GetHash().ToString(); // 20000
  137. sortedOrder[1] = tx4.GetHash().ToString(); // 15000
  138. sortedOrder[2] = tx1.GetHash().ToString(); // 10000
  139. sortedOrder[3] = tx5.GetHash().ToString(); // 10000
  140. sortedOrder[4] = tx3.GetHash().ToString(); // 0
  141. CheckSort(pool, sortedOrder);
  142. /* low fee but with high fee child */
  143. /* tx6 -> tx7 -> tx8, tx9 -> tx10 */
  144. CMutableTransaction tx6 = CMutableTransaction();
  145. tx6.vout.resize(1);
  146. tx6.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  147. tx6.vout[0].nValue = 20 * COIN;
  148. pool.addUnchecked(tx6.GetHash(), CTxMemPoolEntry(tx6, 0LL, 1, 10.0, 1, true));
  149. BOOST_CHECK_EQUAL(pool.size(), 6);
  150. // Check that at this point, tx6 is sorted low
  151. sortedOrder.push_back(tx6.GetHash().ToString());
  152. CheckSort(pool, sortedOrder);
  153. CTxMemPool::setEntries setAncestors;
  154. setAncestors.insert(pool.mapTx.find(tx6.GetHash()));
  155. CMutableTransaction tx7 = CMutableTransaction();
  156. tx7.vin.resize(1);
  157. tx7.vin[0].prevout = COutPoint(tx6.GetHash(), 0);
  158. tx7.vin[0].scriptSig = CScript() << OP_11;
  159. tx7.vout.resize(2);
  160. tx7.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  161. tx7.vout[0].nValue = 10 * COIN;
  162. tx7.vout[1].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  163. tx7.vout[1].nValue = 1 * COIN;
  164. CTxMemPool::setEntries setAncestorsCalculated;
  165. std::string dummy;
  166. CTxMemPoolEntry entry7(tx7, 2000000LL, 1, 10.0, 1, true);
  167. BOOST_CHECK_EQUAL(pool.CalculateMemPoolAncestors(entry7, setAncestorsCalculated, 100, 1000000, 1000, 1000000, dummy), true);
  168. BOOST_CHECK(setAncestorsCalculated == setAncestors);
  169. pool.addUnchecked(tx7.GetHash(), CTxMemPoolEntry(tx7, 2000000LL, 1, 10.0, 1, true), setAncestors);
  170. BOOST_CHECK_EQUAL(pool.size(), 7);
  171. // Now tx6 should be sorted higher (high fee child): tx7, tx6, tx2, ...
  172. sortedOrder.erase(sortedOrder.end()-1);
  173. sortedOrder.insert(sortedOrder.begin(), tx6.GetHash().ToString());
  174. sortedOrder.insert(sortedOrder.begin(), tx7.GetHash().ToString());
  175. CheckSort(pool, sortedOrder);
  176. /* low fee child of tx7 */
  177. CMutableTransaction tx8 = CMutableTransaction();
  178. tx8.vin.resize(1);
  179. tx8.vin[0].prevout = COutPoint(tx7.GetHash(), 0);
  180. tx8.vin[0].scriptSig = CScript() << OP_11;
  181. tx8.vout.resize(1);
  182. tx8.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  183. tx8.vout[0].nValue = 10 * COIN;
  184. setAncestors.insert(pool.mapTx.find(tx7.GetHash()));
  185. pool.addUnchecked(tx8.GetHash(), CTxMemPoolEntry(tx8, 0LL, 2, 10.0, 1, true), setAncestors);
  186. // Now tx8 should be sorted low, but tx6/tx both high
  187. sortedOrder.push_back(tx8.GetHash().ToString());
  188. CheckSort(pool, sortedOrder);
  189. /* low fee child of tx7 */
  190. CMutableTransaction tx9 = CMutableTransaction();
  191. tx9.vin.resize(1);
  192. tx9.vin[0].prevout = COutPoint(tx7.GetHash(), 1);
  193. tx9.vin[0].scriptSig = CScript() << OP_11;
  194. tx9.vout.resize(1);
  195. tx9.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  196. tx9.vout[0].nValue = 1 * COIN;
  197. pool.addUnchecked(tx9.GetHash(), CTxMemPoolEntry(tx9, 0LL, 3, 10.0, 1, true), setAncestors);
  198. // tx9 should be sorted low
  199. BOOST_CHECK_EQUAL(pool.size(), 9);
  200. sortedOrder.push_back(tx9.GetHash().ToString());
  201. CheckSort(pool, sortedOrder);
  202. std::vector<std::string> snapshotOrder = sortedOrder;
  203. setAncestors.insert(pool.mapTx.find(tx8.GetHash()));
  204. setAncestors.insert(pool.mapTx.find(tx9.GetHash()));
  205. /* tx10 depends on tx8 and tx9 and has a high fee*/
  206. CMutableTransaction tx10 = CMutableTransaction();
  207. tx10.vin.resize(2);
  208. tx10.vin[0].prevout = COutPoint(tx8.GetHash(), 0);
  209. tx10.vin[0].scriptSig = CScript() << OP_11;
  210. tx10.vin[1].prevout = COutPoint(tx9.GetHash(), 0);
  211. tx10.vin[1].scriptSig = CScript() << OP_11;
  212. tx10.vout.resize(1);
  213. tx10.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
  214. tx10.vout[0].nValue = 10 * COIN;
  215. setAncestorsCalculated.clear();
  216. CTxMemPoolEntry entry10(tx10, 200000LL, 4, 10.0, 1, true);
  217. BOOST_CHECK_EQUAL(pool.CalculateMemPoolAncestors(entry10, setAncestorsCalculated, 100, 1000000, 1000, 1000000, dummy), true);
  218. BOOST_CHECK(setAncestorsCalculated == setAncestors);
  219. pool.addUnchecked(tx10.GetHash(), CTxMemPoolEntry(tx10, 200000LL, 4, 10.0, 1, true), setAncestors);
  220. /**
  221. * tx8 and tx9 should both now be sorted higher
  222. * Final order after tx10 is added:
  223. *
  224. * tx7 = 2.2M (4 txs)
  225. * tx6 = 2.2M (5 txs)
  226. * tx10 = 200k (1 tx)
  227. * tx8 = 200k (2 txs)
  228. * tx9 = 200k (2 txs)
  229. * tx2 = 20000 (1)
  230. * tx4 = 15000 (1)
  231. * tx1 = 10000 (1)
  232. * tx5 = 10000 (1)
  233. * tx3 = 0 (1)
  234. */
  235. sortedOrder.erase(sortedOrder.end()-2, sortedOrder.end()); // take out tx8, tx9 from the end
  236. sortedOrder.insert(sortedOrder.begin()+2, tx10.GetHash().ToString()); // tx10 is after tx6
  237. sortedOrder.insert(sortedOrder.begin()+3, tx9.GetHash().ToString());
  238. sortedOrder.insert(sortedOrder.begin()+3, tx8.GetHash().ToString());
  239. CheckSort(pool, sortedOrder);
  240. // there should be 10 transactions in the mempool
  241. BOOST_CHECK_EQUAL(pool.size(), 10);
  242. // Now try removing tx10 and verify the sort order returns to normal
  243. std::list<CTransaction> removed;
  244. pool.remove(pool.mapTx.find(tx10.GetHash())->GetTx(), removed, true);
  245. CheckSort(pool, snapshotOrder);
  246. }
  247. BOOST_AUTO_TEST_SUITE_END()