9#include <boost/test/unit_test.hpp>
14BOOST_AUTO_TEST_SUITE(txgraph_tests)
28 static constexpr int MAX_CLUSTER_COUNT = 50;
30 static constexpr int NUM_BOTTOM_TX = 49;
34 static constexpr int NUM_TOP_TX = 50;
36 static constexpr int NUM_TOTAL_TX = NUM_BOTTOM_TX + NUM_TOP_TX;
37 static_assert(NUM_TOTAL_TX > MAX_CLUSTER_COUNT);
39 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
45 std::vector<TxGraph::Ref> refs;
46 refs.reserve(NUM_TOTAL_TX);
48 for (
unsigned int i = 0; i < NUM_BOTTOM_TX; ++i) {
49 refs.push_back(graph->AddTransaction(
FeePerWeight{200 - i, 100}));
52 for (
unsigned int i = 0; i < NUM_TOP_TX; ++i) {
53 refs.push_back(graph->AddTransaction(
FeePerWeight{100 - i, 100}));
59 for (
unsigned int i = 0; i < NUM_BOTTOM_TX; ++i) {
60 graph->AddDependency(refs[NUM_BOTTOM_TX + i], refs[i]);
61 graph->AddDependency(refs[NUM_BOTTOM_TX + i + 1], refs[i]);
71 auto removed_refs = graph->Trim();
78 for (
unsigned int i = 0; i < refs.size(); ++i) {
95 static constexpr int MAX_CLUSTER_COUNT = 50;
99 static constexpr int NUM_TOP_TX = MAX_CLUSTER_COUNT * 2;
101 static constexpr int NUM_TOTAL_TX = NUM_TOP_TX + 1;
103 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
108 std::vector<TxGraph::Ref> refs;
109 refs.reserve(NUM_TOTAL_TX);
112 refs.push_back(graph->AddTransaction({1, 100}));
113 for (
unsigned int i = 0; i < NUM_TOP_TX; ++i) {
114 refs.push_back(graph->AddTransaction(
FeePerWeight{500 + i, 100}));
116 graph->SanityCheck();
119 for (
unsigned int i = 1; i < NUM_TOTAL_TX; ++i) {
120 graph->AddDependency(refs[i], refs[0]);
122 graph->SanityCheck();
129 auto removed_refs = graph->Trim();
130 graph->SanityCheck();
137 for (
unsigned int i = 1; i < refs.size(); ++i) {
167 static constexpr int MAX_CLUSTER_COUNT = 64;
169 static constexpr int NUM_TOP_CHAINS = 1000;
171 static constexpr int NUM_TX_PER_TOP_CHAIN = MAX_CLUSTER_COUNT;
173 static constexpr int NUM_DEPS_PER_BOTTOM_TX = 100;
175 static constexpr int NUM_BOTTOM_TX = (NUM_TOP_CHAINS - 1 + (NUM_DEPS_PER_BOTTOM_TX - 2)) / (NUM_DEPS_PER_BOTTOM_TX - 1);
177 static constexpr int NUM_TOTAL_TX = NUM_TOP_CHAINS * NUM_TX_PER_TOP_CHAIN + NUM_BOTTOM_TX;
179 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
182 std::vector<TxGraph::Ref> top_refs;
184 std::vector<TxGraph::Ref> bottom_refs;
188 std::vector<size_t> top_components;
194 for (
int chain = 0; chain < NUM_TOP_CHAINS; ++chain) {
195 for (
int chaintx = 0; chaintx < NUM_TX_PER_TOP_CHAIN; ++chaintx) {
199 top_refs.push_back(graph->AddTransaction(feerate));
202 graph->AddDependency(*(top_refs.rbegin()), *(top_refs.rbegin() + 1));
206 top_components.push_back(top_refs.size() - 1);
208 graph->SanityCheck();
214 while (top_components.size() > 1) {
218 auto bottom_tx = graph->AddTransaction(feerate);
220 int deps = std::min<int>(NUM_DEPS_PER_BOTTOM_TX, top_components.size());
221 for (
int dep = 0; dep < deps; ++dep) {
223 auto idx = rng.
randrange(top_components.size());
225 graph->AddDependency(top_refs[top_components[idx]], bottom_tx);
228 if (dep < deps - 1) {
230 if (idx != top_components.size() - 1) std::swap(top_components.back(), top_components[idx]);
232 top_components.pop_back();
235 bottom_refs.push_back(std::move(bottom_tx));
237 graph->SanityCheck();
241 const auto total_tx_count = graph->GetTransactionCount();
242 BOOST_CHECK(total_tx_count == top_refs.size() + bottom_refs.size());
246 auto removed_refs = graph->Trim();
248 BOOST_CHECK(removed_refs.size() == total_tx_count - graph->GetTransactionCount());
249 graph->SanityCheck();
252 BOOST_CHECK(graph->GetTransactionCount() >= (NUM_TOP_CHAINS * NUM_TX_PER_TOP_CHAIN * 99) / 100);
258 static constexpr int MAX_CLUSTER_COUNT = 64;
259 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000;
260 static constexpr int NUM_TOTAL_TX = 100;
266 std::vector<TxGraph::Ref> refs;
267 refs.reserve(NUM_TOTAL_TX);
270 for (
unsigned int i = 0; i < NUM_TOTAL_TX; ++i) {
273 const FeePerWeight feerate{500 + i, (i == 88 || i % 20 == 0) ? MAX_CLUSTER_SIZE + 1 : 100};
274 refs.push_back(graph->AddTransaction(feerate));
276 graph->SanityCheck();
283 auto removed_refs = graph->Trim();
284 graph->SanityCheck();
289 for (
unsigned int i = 0; i < refs.size(); ++i) {
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
Tagged wrapper around FeeFrac to avoid unit confusion.
std::unique_ptr< TxGraph > MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept
Construct a new TxGraph with the specified limit on the number of transactions within a cluster,...
BOOST_AUTO_TEST_CASE(txgraph_trim_zigzag)
static constexpr uint64_t NUM_ACCEPTABLE_ITERS
The number used as acceptable_iters argument in these tests.