9#include <boost/test/unit_test.hpp>
14BOOST_AUTO_TEST_SUITE(txgraph_tests)
20constexpr uint64_t NUM_ACCEPTABLE_ITERS = 100'000'000;
37 static constexpr int MAX_CLUSTER_COUNT = 50;
39 static constexpr int NUM_BOTTOM_TX = 49;
43 static constexpr int NUM_TOP_TX = 50;
45 static constexpr int NUM_TOTAL_TX = NUM_BOTTOM_TX + NUM_TOP_TX;
46 static_assert(NUM_TOTAL_TX > MAX_CLUSTER_COUNT);
48 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
51 auto graph =
MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS, PointerComparator);
54 std::vector<TxGraph::Ref> refs;
55 refs.reserve(NUM_TOTAL_TX);
57 for (
unsigned int i = 0; i < NUM_BOTTOM_TX; ++i) {
58 graph->AddTransaction(refs.emplace_back(),
FeePerWeight{200 - i, 100});
61 for (
unsigned int i = 0; i < NUM_TOP_TX; ++i) {
62 graph->AddTransaction(refs.emplace_back(),
FeePerWeight{100 - i, 100});
68 for (
unsigned int i = 0; i < NUM_BOTTOM_TX; ++i) {
69 graph->AddDependency(refs[NUM_BOTTOM_TX + i], refs[i]);
70 graph->AddDependency(refs[NUM_BOTTOM_TX + i + 1], refs[i]);
80 auto removed_refs = graph->Trim();
87 for (
unsigned int i = 0; i < refs.size(); ++i) {
104 static constexpr int MAX_CLUSTER_COUNT = 50;
108 static constexpr int NUM_TOP_TX = MAX_CLUSTER_COUNT * 2;
110 static constexpr int NUM_TOTAL_TX = NUM_TOP_TX + 1;
112 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
114 auto graph =
MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS, PointerComparator);
117 std::vector<TxGraph::Ref> refs;
118 refs.reserve(NUM_TOTAL_TX);
121 graph->AddTransaction(refs.emplace_back(), {1, 100});
122 for (
unsigned int i = 0; i < NUM_TOP_TX; ++i) {
123 graph->AddTransaction(refs.emplace_back(),
FeePerWeight{500 + i, 100});
125 graph->SanityCheck();
128 for (
unsigned int i = 1; i < NUM_TOTAL_TX; ++i) {
129 graph->AddDependency(refs[i], refs[0]);
131 graph->SanityCheck();
138 auto removed_refs = graph->Trim();
139 graph->SanityCheck();
146 for (
unsigned int i = 1; i < refs.size(); ++i) {
176 static constexpr int MAX_CLUSTER_COUNT = 64;
178 static constexpr int NUM_TOP_CHAINS = 1000;
180 static constexpr int NUM_TX_PER_TOP_CHAIN = MAX_CLUSTER_COUNT;
182 static constexpr int NUM_DEPS_PER_BOTTOM_TX = 100;
184 static constexpr int NUM_BOTTOM_TX = (NUM_TOP_CHAINS - 1 + (NUM_DEPS_PER_BOTTOM_TX - 2)) / (NUM_DEPS_PER_BOTTOM_TX - 1);
186 static constexpr int NUM_TOTAL_TX = NUM_TOP_CHAINS * NUM_TX_PER_TOP_CHAIN + NUM_BOTTOM_TX;
188 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
191 std::vector<TxGraph::Ref> top_refs;
193 std::vector<TxGraph::Ref> bottom_refs;
197 std::vector<size_t> top_components;
200 auto graph =
MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS, PointerComparator);
203 for (
int chain = 0; chain < NUM_TOP_CHAINS; ++chain) {
204 for (
int chaintx = 0; chaintx < NUM_TX_PER_TOP_CHAIN; ++chaintx) {
208 graph->AddTransaction(top_refs.emplace_back(), feerate);
211 graph->AddDependency(*(top_refs.rbegin()), *(top_refs.rbegin() + 1));
215 top_components.push_back(top_refs.size() - 1);
217 graph->SanityCheck();
223 while (top_components.size() > 1) {
228 graph->AddTransaction(bottom_tx, feerate);
230 int deps = std::min<int>(NUM_DEPS_PER_BOTTOM_TX, top_components.size());
231 for (
int dep = 0; dep < deps; ++dep) {
233 auto idx = rng.
randrange(top_components.size());
235 graph->AddDependency(top_refs[top_components[idx]], bottom_tx);
238 if (dep < deps - 1) {
240 if (idx != top_components.size() - 1) std::swap(top_components.back(), top_components[idx]);
242 top_components.pop_back();
245 bottom_refs.push_back(std::move(bottom_tx));
247 graph->SanityCheck();
252 BOOST_CHECK(total_tx_count == top_refs.size() + bottom_refs.size());
256 auto removed_refs = graph->Trim();
259 graph->SanityCheck();
268 static constexpr int MAX_CLUSTER_COUNT = 64;
269 static constexpr int32_t MAX_CLUSTER_SIZE = 100'000;
270 static constexpr int NUM_TOTAL_TX = 100;
273 auto graph =
MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS, PointerComparator);
276 std::vector<TxGraph::Ref> refs;
277 refs.reserve(NUM_TOTAL_TX);
280 for (
unsigned int i = 0; i < NUM_TOTAL_TX; ++i) {
283 const FeePerWeight feerate{500 + i, (i == 88 || i % 20 == 0) ? MAX_CLUSTER_SIZE + 1 : 100};
284 graph->AddTransaction(refs.emplace_back(), feerate);
286 graph->SanityCheck();
293 auto removed_refs = graph->Trim();
294 graph->SanityCheck();
299 for (
unsigned int i = 0; i < refs.size(); ++i) {
307 auto graph =
MakeTxGraph(50, 1000, NUM_ACCEPTABLE_ITERS, PointerComparator);
309 auto block_builder_checker = [&graph](std::vector<std::vector<TxGraph::Ref*>> expected_chunks) {
310 std::vector<std::vector<TxGraph::Ref*>> chunks;
311 auto builder = graph->GetBlockBuilder();
313 while (
auto chunk = builder->GetCurrentChunk()) {
318 BOOST_CHECK(graph->GetMainChunkFeerate(*ref) == chunk->second);
320 sum += graph->GetIndividualFeerate(*ref);
323 chunks.push_back(std::move(chunk->first));
324 last_chunk_feerate = chunk->second;
329 auto& last_chunk = chunks.back();
331 std::reverse(last_chunk.begin(), last_chunk.end());
332 auto [worst_chunk, worst_chunk_feerate] = graph->GetWorstMainChunk();
334 BOOST_CHECK(last_chunk_feerate == worst_chunk_feerate);
337 std::vector<TxGraph::Ref> refs;
347 graph->AddTransaction(refs.emplace_back(), feerateA);
349 block_builder_checker({{&refs[0]}});
351 graph->AddTransaction(refs.emplace_back(), feerateB);
352 graph->AddDependency(refs[0], refs[1]);
354 block_builder_checker({{&refs[0]}, {&refs[1]}});
357 graph->AddTransaction(refs.emplace_back(), feerateC);
358 graph->AddDependency(refs[1], refs[2]);
360 block_builder_checker({{&refs[0]}, {&refs[1], &refs[2]}});
363 graph->AddTransaction(refs.emplace_back(), feerateD);
364 graph->AddDependency(refs[2], refs[3]);
366 block_builder_checker({{&refs[0], &refs[1], &refs[2], &refs[3]}});
368 graph->SanityCheck();
371 graph->RemoveTransaction(refs[1]);
375 graph->RemoveTransaction(refs[2]);
376 graph->RemoveTransaction(refs[3]);
378 block_builder_checker({{&refs[0]}});
386 auto graph =
MakeTxGraph(10, 1000, NUM_ACCEPTABLE_ITERS, PointerComparator);
388 std::vector<TxGraph::Ref> refs;
396 graph->AddTransaction(refs.emplace_back(), feerateA);
400 graph->StartStaging();
405 graph->AddTransaction(refs.emplace_back(), feerateB);
411 graph->AddDependency(refs[0], refs[1]);
415 graph->CommitStaging();
420 graph->StartStaging();
423 graph->RemoveTransaction(refs[1]);
427 graph->CommitStaging();
431 graph->SanityCheck();
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.
@ MAIN
Always refers to the main graph, whether staging is present or not.
@ TOP
Refers to staging if it exists, main otherwise.
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, const std::function< std::strong_ordering(const TxGraph::Ref &, const TxGraph::Ref &)> &fallback_order) noexcept
Construct a new TxGraph with the specified limit on the number of transactions within a cluster,...
BOOST_AUTO_TEST_CASE(txgraph_trim_zigzag)