forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] DisconnectedBlockTransactions::DynamicMemoryUsage
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) 2023 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
// | ||
#include <test/util/setup_common.h> | ||
#include <kernel/disconnected_transactions.h> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
BOOST_FIXTURE_TEST_SUITE(disconnected_transactions, TestChain100Setup) | ||
|
||
//! Tests that DisconnectedBlockTransactions limits its own memory properly | ||
BOOST_AUTO_TEST_CASE(disconnectpool_memory_limits) | ||
{ | ||
// Use the coinbase transactions from TestChain100Setup. It doesn't matter whether these | ||
// transactions would realistically be in a block together, they just need distinct txids for | ||
// this test to work. | ||
std::vector<CTransactionRef> block_vtx(m_coinbase_txns); | ||
|
||
size_t usage_full{0}; | ||
// DisconnectedBlockTransactions with a comfortable maximum memory usage s.t. nothing is evicted. | ||
{ | ||
DisconnectedBlockTransactions disconnectpool{MAX_DISCONNECTED_TX_POOL_SIZE * 1000}; | ||
auto evicted_txns = disconnectpool.AddTransactionsFromBlock(block_vtx); | ||
BOOST_CHECK_EQUAL(evicted_txns.size(), 0); | ||
BOOST_CHECK(disconnectpool.DynamicMemoryUsage() <= MAX_DISCONNECTED_TX_POOL_SIZE * 1000); | ||
|
||
usage_full = disconnectpool.DynamicMemoryUsage(); | ||
|
||
disconnectpool.clear(); | ||
} | ||
|
||
// DisconnectedBlockTransactions that's just a little too small for all of the transactions. | ||
{ | ||
const size_t MAX_MEMUSAGE_99{usage_full - sizeof(void*)}; | ||
DisconnectedBlockTransactions disconnectpool{MAX_MEMUSAGE_99}; | ||
auto evicted_txns = disconnectpool.AddTransactionsFromBlock(block_vtx); | ||
BOOST_CHECK(disconnectpool.DynamicMemoryUsage() <= MAX_MEMUSAGE_99); | ||
|
||
// Only 1 transaction needed to be evicted | ||
BOOST_CHECK_EQUAL(1, evicted_txns.size()); | ||
|
||
// Transactions are added from back to front and eviction is FIFO. | ||
// The last transaction of block_vtx should be the first to be evicted. | ||
BOOST_CHECK_EQUAL(block_vtx.back(), evicted_txns.front()); | ||
|
||
disconnectpool.clear(); | ||
} | ||
} | ||
|
||
//! Sanity checks for DisconnectedBlockTransactions::DynamicMemoryUsage. | ||
BOOST_AUTO_TEST_CASE(disconnectpool_memusage) | ||
{ | ||
if (sizeof(void*) != 8) { | ||
BOOST_TEST_MESSAGE("Exiting DisconnectedBlockTransactions memusage tests early due to unsupported arch"); | ||
} | ||
|
||
// Use the coinbase transactions from TestChain100Setup as they all have the same size and it | ||
// doesn't matter whether these transactions would realistically be in a block together. They | ||
// just need distinct txids for this test to work. | ||
std::vector<CTransactionRef> block_vtx(m_coinbase_txns); | ||
|
||
// Sanity check the sizes are as expected. If setup_common changes the size of coinbase | ||
// transactions, update COINBASE_TX_USAGE. | ||
const size_t COINBASE_TX_USAGE{640}; | ||
BOOST_CHECK_EQUAL(block_vtx.size(), 100); | ||
for (const auto& tx : block_vtx) { | ||
BOOST_CHECK_EQUAL(RecursiveDynamicUsage(tx), COINBASE_TX_USAGE); | ||
} | ||
|
||
// Test DisconnectedBlockTransactions::DynamicMemoryUsage() for 100 transactions | ||
const size_t EXPECTED_USAGE_100{77648}; | ||
{ | ||
const size_t MAX_MEMUSAGE_100{EXPECTED_USAGE_100}; | ||
DisconnectedBlockTransactions disconnectpool{MAX_MEMUSAGE_100}; | ||
auto evicted_txns = disconnectpool.AddTransactionsFromBlock(block_vtx); | ||
BOOST_CHECK_EQUAL(evicted_txns.size(), 0); | ||
BOOST_CHECK_EQUAL(disconnectpool.DynamicMemoryUsage(), EXPECTED_USAGE_100); | ||
disconnectpool.clear(); | ||
} | ||
|
||
// Test DisconnectedBlockTransactions::DynamicMemoryUsage() for 1 transaction | ||
const size_t EXPECTED_USAGE_1{800}; | ||
{ | ||
const size_t MAX_MEMUSAGE_100{EXPECTED_USAGE_100}; | ||
DisconnectedBlockTransactions disconnectpool{MAX_MEMUSAGE_100}; | ||
auto evicted_txns = disconnectpool.AddTransactionsFromBlock({block_vtx.front()}); | ||
BOOST_CHECK_EQUAL(evicted_txns.size(), 0); | ||
BOOST_CHECK_EQUAL(disconnectpool.DynamicMemoryUsage(), EXPECTED_USAGE_1); | ||
disconnectpool.clear(); | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |