|
| 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 | + |
| 5 | +#include "main.h" |
| 6 | +#include "txmempool.h" |
| 7 | +#include "util.h" |
| 8 | + |
| 9 | +#include <boost/test/unit_test.hpp> |
| 10 | +#include <list> |
| 11 | + |
| 12 | +BOOST_AUTO_TEST_SUITE(mempool_tests) |
| 13 | + |
| 14 | +BOOST_AUTO_TEST_CASE(MempoolRemoveTest) |
| 15 | +{ |
| 16 | + // Test CTxMemPool::remove functionality |
| 17 | + |
| 18 | + // Parent transaction with three children, |
| 19 | + // and three grand-children: |
| 20 | + CMutableTransaction txParent; |
| 21 | + txParent.vin.resize(1); |
| 22 | + txParent.vin[0].scriptSig = CScript() << OP_11; |
| 23 | + txParent.vout.resize(3); |
| 24 | + for (int i = 0; i < 3; i++) |
| 25 | + { |
| 26 | + txParent.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL; |
| 27 | + txParent.vout[i].nValue = 33000LL; |
| 28 | + } |
| 29 | + CMutableTransaction txChild[3]; |
| 30 | + for (int i = 0; i < 3; i++) |
| 31 | + { |
| 32 | + txChild[i].vin.resize(1); |
| 33 | + txChild[i].vin[0].scriptSig = CScript() << OP_11; |
| 34 | + txChild[i].vin[0].prevout.hash = txParent.GetHash(); |
| 35 | + txChild[i].vin[0].prevout.n = i; |
| 36 | + txChild[i].vout.resize(1); |
| 37 | + txChild[i].vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL; |
| 38 | + txChild[i].vout[0].nValue = 11000LL; |
| 39 | + } |
| 40 | + CMutableTransaction txGrandChild[3]; |
| 41 | + for (int i = 0; i < 3; i++) |
| 42 | + { |
| 43 | + txGrandChild[i].vin.resize(1); |
| 44 | + txGrandChild[i].vin[0].scriptSig = CScript() << OP_11; |
| 45 | + txGrandChild[i].vin[0].prevout.hash = txChild[i].GetHash(); |
| 46 | + txGrandChild[i].vin[0].prevout.n = 0; |
| 47 | + txGrandChild[i].vout.resize(1); |
| 48 | + txGrandChild[i].vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL; |
| 49 | + txGrandChild[i].vout[0].nValue = 11000LL; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + CTxMemPool testPool(CFeeRate(0)); |
| 54 | + std::list<CTransaction> removed; |
| 55 | + |
| 56 | + // Nothing in pool, remove should do nothing: |
| 57 | + testPool.remove(txParent, removed, true); |
| 58 | + BOOST_CHECK_EQUAL(removed.size(), 0); |
| 59 | + |
| 60 | + // Just the parent: |
| 61 | + testPool.addUnchecked(txParent.GetHash(), CTxMemPoolEntry(txParent, 0, 0, 0.0, 1)); |
| 62 | + testPool.remove(txParent, removed, true); |
| 63 | + BOOST_CHECK_EQUAL(removed.size(), 1); |
| 64 | + removed.clear(); |
| 65 | + |
| 66 | + // Parent, children, grandchildren: |
| 67 | + testPool.addUnchecked(txParent.GetHash(), CTxMemPoolEntry(txParent, 0, 0, 0.0, 1)); |
| 68 | + for (int i = 0; i < 3; i++) |
| 69 | + { |
| 70 | + testPool.addUnchecked(txChild[i].GetHash(), CTxMemPoolEntry(txChild[i], 0, 0, 0.0, 1)); |
| 71 | + testPool.addUnchecked(txGrandChild[i].GetHash(), CTxMemPoolEntry(txGrandChild[i], 0, 0, 0.0, 1)); |
| 72 | + } |
| 73 | + // Remove Child[0], GrandChild[0] should be removed: |
| 74 | + testPool.remove(txChild[0], removed, true); |
| 75 | + BOOST_CHECK_EQUAL(removed.size(), 2); |
| 76 | + removed.clear(); |
| 77 | + // ... make sure grandchild and child are gone: |
| 78 | + testPool.remove(txGrandChild[0], removed, true); |
| 79 | + BOOST_CHECK_EQUAL(removed.size(), 0); |
| 80 | + testPool.remove(txChild[0], removed, true); |
| 81 | + BOOST_CHECK_EQUAL(removed.size(), 0); |
| 82 | + // Remove parent, all children/grandchildren should go: |
| 83 | + testPool.remove(txParent, removed, true); |
| 84 | + BOOST_CHECK_EQUAL(removed.size(), 5); |
| 85 | + BOOST_CHECK_EQUAL(testPool.size(), 0); |
| 86 | + removed.clear(); |
| 87 | + |
| 88 | + // Add children and grandchildren, but NOT the parent (simulate the parent being in a block) |
| 89 | + for (int i = 0; i < 3; i++) |
| 90 | + { |
| 91 | + testPool.addUnchecked(txChild[i].GetHash(), CTxMemPoolEntry(txChild[i], 0, 0, 0.0, 1)); |
| 92 | + testPool.addUnchecked(txGrandChild[i].GetHash(), CTxMemPoolEntry(txGrandChild[i], 0, 0, 0.0, 1)); |
| 93 | + } |
| 94 | + // Now remove the parent, as might happen if a block-re-org occurs but the parent cannot be |
| 95 | + // put into the mempool (maybe because it is non-standard): |
| 96 | + testPool.remove(txParent, removed, true); |
| 97 | + BOOST_CHECK_EQUAL(removed.size(), 6); |
| 98 | + BOOST_CHECK_EQUAL(testPool.size(), 0); |
| 99 | + removed.clear(); |
| 100 | +} |
| 101 | + |
| 102 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments