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" | |||||
deadalnix: Put that with userland includes. This is not a system library. | |||||
#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); | ||||
} | } | ||||
// expectedSize can be smaller than correctlyOrderedIds.size(), since we | |||||
// might be testing intermediary states. Just avoiding some slice operations, | |||||
void CheckDisconnectPoolOrder(DisconnectedBlockTransactions &disconnectPool, | |||||
std::vector<TxId> correctlyOrderedIds, | |||||
unsigned int expectedSize) { | |||||
unsigned int i = 0; | |||||
BOOST_CHECK_EQUAL(disconnectPool.GetQueuedTx().size(), expectedSize); | |||||
assert(correctlyOrderedIds.size() >= expectedSize); | |||||
deadalnixUnsubmitted Done Inline ActionsI'm not sure why you need this. You just checked it before. If you want to make sure the program stops, you can use BOOST_REQUIRE_EQUAL deadalnix: I'm not sure why you need this. You just checked it before. If you want to make sure the… | |||||
// 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() == correctlyOrderedIds[i]); | |||||
i++; | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_CASE(TestImportMempool) { | |||||
CMutableTransaction chainedTxn[5]; | |||||
std::vector<TxId> correctlyOrderedIds; | |||||
COutPoint lastOutpoint; | |||||
// Construct a chain of 5 transactions | |||||
for (int i = 0; i < 5; i++) { | |||||
chainedTxn[i].vin.emplace_back(lastOutpoint); | |||||
chainedTxn[i].vout.emplace_back(10 * SATOSHI, CScript() << OP_TRUE); | |||||
correctlyOrderedIds.push_back(chainedTxn[i].GetId()); | |||||
lastOutpoint = COutPoint(correctlyOrderedIds[i], 0); | |||||
} | |||||
// Tests the chained transactions in the following different orders. The | |||||
// first 3 txns simulate once confirmed transactions. The last 2 simulate a | |||||
// chain of unconfirmed ones. | |||||
const int tests_len = 6; | |||||
deadalnixUnsubmitted Done Inline Actionsconstant are CAPITAL_SNAKE_CASE . I'm also pretty sure this is not about how long the test is. deadalnix: constant are CAPITAL_SNAKE_CASE . I'm also pretty sure this is not about how long the test is. | |||||
const int tests[tests_len][5] = {{1, 2, 0, 4, 3}, {1, 2, 0, 3, 4}, | |||||
{0, 1, 2, 4, 3}, {0, 1, 2, 3, 4}, | |||||
{2, 1, 0, 4, 3}, {2, 1, 0, 3, 4}}; | |||||
deadalnixUnsubmitted Done Inline ActionsThere is essentially no way for me to look at these test cases and ensure they are correct in any reliable way. Test cases usually comes in the form of inputs => expect results. This is just a bunch of numbers. deadalnix: There is essentially no way for me to look at these test cases and ensure they are correct in… | |||||
for (int i = 0; i < tests_len; i++) { | |||||
// index holds the ordering information for this test | |||||
const int *indexes = tests[i]; | |||||
DisconnectedBlockTransactions disconnectPool; | |||||
// The first 3 transactions go in different orders to addForBlock, which | |||||
// sorts them into disconnectPool. They simulate transactions that were | |||||
// once confirmed in a block in not necessarily topological order | |||||
disconnectPool.addForBlock(std::vector<CTransactionRef>( | |||||
{MakeTransactionRef(chainedTxn[indexes[0]]), | |||||
MakeTransactionRef(chainedTxn[indexes[1]]), | |||||
MakeTransactionRef(chainedTxn[indexes[2]])})); | |||||
deadalnixUnsubmitted Done Inline ActionsI don't think you need to call the constructor explicitly. deadalnix: I don't think you need to call the constructor explicitly. | |||||
CheckDisconnectPoolOrder(disconnectPool, correctlyOrderedIds, 3); | |||||
// If the mempool is empty, importMempool doesn't change disconnectPool | |||||
CTxMemPool testPool; | |||||
disconnectPool.importMempool(testPool); | |||||
CheckDisconnectPoolOrder(disconnectPool, correctlyOrderedIds, 3); | |||||
// The last 2 transactions go in different orders into testPool, | |||||
// simulating a chain of unconfirmed txn | |||||
TestMemPoolEntryHelper entry; | |||||
testPool.addUnchecked(chainedTxn[indexes[3]].GetId(), | |||||
entry.FromTx(chainedTxn[indexes[3]])); | |||||
testPool.addUnchecked(chainedTxn[indexes[4]].GetId(), | |||||
entry.FromTx(chainedTxn[indexes[4]])); | |||||
// Now we test importMempool with a 2 items mempool | |||||
disconnectPool.importMempool(testPool); | |||||
BOOST_CHECK_EQUAL(testPool.size(), 0); | |||||
deadalnixUnsubmitted Done Inline ActionsRemove deadalnix: Remove | |||||
CheckDisconnectPoolOrder(disconnectPool, correctlyOrderedIds, 5); | |||||
disconnectPool.clear(); | |||||
deadalnixUnsubmitted Not Done Inline ActionsRemove deadalnix: Remove | |||||
florianAuthorUnsubmitted Done Inline ActionsdisconnectPool has an assert in its destructor. I do need to clear() it. florian: disconnectPool has an assert in its destructor. I do need to clear() it. | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |
Put that with userland includes. This is not a system library.