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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "main.h"
  5. #include "txmempool.h"
  6. #include "util.h"
  7. #include "test/test_bitcoin.h"
  8. #include <boost/test/unit_test.hpp>
  9. #include <list>
  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. BOOST_AUTO_TEST_SUITE_END()