Changeset View
Changeset View
Standalone View
Standalone View
src/test/mempool_tests.cpp
// Copyright (c) 2011-2016 The Bitcoin Core developers | // Copyright (c) 2011-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "policy/policy.h" | #include "policy/policy.h" | ||||
#include "txmempool.h" | #include "txmempool.h" | ||||
#include "util.h" | #include "util.h" | ||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include "reverse_iterator.h" | |||||
#include <algorithm> | #include <algorithm> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <list> | #include <list> | ||||
#include <vector> | #include <vector> | ||||
BOOST_FIXTURE_TEST_SUITE(mempool_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(mempool_tests, TestingSetup) | ||||
BOOST_AUTO_TEST_CASE(TestPackageAccounting) { | BOOST_AUTO_TEST_CASE(TestPackageAccounting) { | ||||
▲ Show 20 Lines • Show All 769 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest) { | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL( | ||||
pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), | pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), | ||||
(maxFeeRateRemoved.GetFeePerK() + feeIncrement) / 8 + SATOSHI); | (maxFeeRateRemoved.GetFeePerK() + feeIncrement) / 8 + SATOSHI); | ||||
// ... with a 1/4 halflife when mempool is < 1/4 its target size | // ... with a 1/4 halflife when mempool is < 1/4 its target size | ||||
SetMockTime(0); | SetMockTime(0); | ||||
} | } | ||||
void CheckDisconnectPoolSort(DisconnectedBlockTransactions &disconnectPool, | |||||
std::vector<TxId> ids) { | |||||
jasonbcox: ids -> expectedOrder to make it clear that this is being checked against | |||||
unsigned int i = 0; | |||||
// Txns in queuedTx's insertion_order index are sorted from children to | |||||
// parent txn | |||||
for (const CTransactionRef &tx : | |||||
reverse_iterate(disconnectPool.GetQueuedTx().get<insertion_order>())) { | |||||
BOOST_CHECK(tx->GetId() == ids[i]); | |||||
i++; | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_CASE(TestImportMempool) { | |||||
CTxMemPool testPool; | |||||
DisconnectedBlockTransactions disconnectPool; | |||||
TestMemPoolEntryHelper entry; | |||||
CMutableTransaction chainedTxn[5]; | |||||
std::vector<TxId> ids; | |||||
COutPoint lastOutpoint; | |||||
// Construct a chain of 5 transactions | |||||
for (unsigned int i = 0; i < 5; i++) { | |||||
chainedTxn[i].vin.emplace_back(lastOutpoint); | |||||
chainedTxn[i].vout.emplace_back(10 * SATOSHI, CScript() << OP_TRUE); | |||||
ids.push_back(chainedTxn[i].GetId()); | |||||
lastOutpoint = COutPoint(ids[i], 0); | |||||
} | |||||
// The first 3 transactions go in mixed order into disconnectPool via | |||||
// addForBlock. They simulate transactions that were once confirmed in a | |||||
// block in non-topological order | |||||
std::vector<CTransactionRef> vtx = {MakeTransactionRef(chainedTxn[1]), | |||||
MakeTransactionRef(chainedTxn[2]), | |||||
MakeTransactionRef(chainedTxn[0])}; | |||||
disconnectPool.addForBlock(vtx); | |||||
BOOST_CHECK_EQUAL(disconnectPool.GetQueuedTx().size(), 3); | |||||
CheckDisconnectPoolSort(disconnectPool, ids); | |||||
jasonbcoxUnsubmitted Done Inline ActionsThis block should loop over some number of orderings that include already-in-order and out-of-order txs for vtx. jasonbcox: This block should loop over some number of orderings that include already-in-order and out-of… | |||||
// If the mempool is empty, importMempool doesn't change disconnectPool | |||||
disconnectPool.importMempool(testPool); | |||||
BOOST_CHECK_EQUAL(disconnectPool.GetQueuedTx().size(), 3); | |||||
CheckDisconnectPoolSort(disconnectPool, ids); | |||||
// The last 2 transactions go in inverted order into testPool, simulating a | |||||
// chain of unconfirmed txn | |||||
testPool.addUnchecked(ids[4], entry.FromTx(chainedTxn[4])); | |||||
testPool.addUnchecked(ids[3], entry.FromTx(chainedTxn[3])); | |||||
// Now we test importMempool with a 2 items mempool | |||||
disconnectPool.importMempool(testPool); | |||||
BOOST_CHECK_EQUAL(testPool.size(), 0); | |||||
BOOST_CHECK_EQUAL(disconnectPool.GetQueuedTx().size(), 5); | |||||
CheckDisconnectPoolSort(disconnectPool, ids); | |||||
disconnectPool.clear(); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |
ids -> expectedOrder to make it clear that this is being checked against