From 3f540253844bb8e7159e5bb02c7af3fd752175be Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Sat, 21 Jun 2025 13:16:56 +0200 Subject: [PATCH] CI: added more block template tests --- src/block_template.cpp | 2 +- src/mempool.h | 6 ++++ src/p2pool.cpp | 2 +- tests/src/block_template_tests.cpp | 50 ++++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index 115efec..3cf9304 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -741,7 +741,7 @@ void BlockTemplate::fill_optimal_knapsack(const MinerData& data, uint64_t base_r constexpr uint64_t FEE_COEFF = 1000; const uint64_t n = m_mempoolTxs.size(); - const uint64_t max_weight = data.median_weight + (data.median_weight / 32) - miner_tx_weight; + const uint64_t max_weight = data.median_weight + (data.median_weight / 8) - miner_tx_weight; m_knapsack.resize((n + 1) * max_weight); memset(m_knapsack.data(), 0, max_weight * sizeof(uint32_t)); diff --git a/src/mempool.h b/src/mempool.h index c71ea01..d2b59ae 100644 --- a/src/mempool.h +++ b/src/mempool.h @@ -50,6 +50,12 @@ public: } } + void clear() + { + WriteLock lock(m_lock); + m_transactions.clear(); + } + private: mutable uv_rwlock_t m_lock; unordered_map m_transactions; diff --git a/src/p2pool.cpp b/src/p2pool.cpp index d2dfa5a..f8f2b0f 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -390,7 +390,7 @@ void p2pool::handle_tx(TxMempoolData& tx) void p2pool::handle_miner_data(MinerData& data) { #if TEST_MEMPOOL_PICKING_ALGORITHM - if (m_mempool->m_transactions.size() < data.tx_backlog.size()) { + if (m_mempool->size() < data.tx_backlog.size()) { m_mempool->swap(data.tx_backlog); } #else diff --git a/tests/src/block_template_tests.cpp b/tests/src/block_template_tests.cpp index 6e33234..ab8a40c 100644 --- a/tests/src/block_template_tests.cpp +++ b/tests/src/block_template_tests.cpp @@ -58,6 +58,7 @@ TEST(block_template, update) // Test 1: empty template tpl.update(data, mempool, &wallet); + ASSERT_EQ(tpl.get_reward(), 600000000000ULL); const PoolBlock* b = tpl.pool_block_template(); ASSERT_EQ(b->m_sidechainId, H("2c90c1926a75c81afd49bf4a4b63e9de7b4c153866411c47e7af864b547c23ec")); @@ -68,7 +69,7 @@ TEST(block_template, update) hash seed_hash; size_t nonce_offset; uint32_t template_id; - tpl.get_hashing_blobs(0, 10000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); + tpl.get_hashing_blobs(0, 1000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); ASSERT_EQ(height, 2762973); ASSERT_EQ(diff, 300346053753ULL); @@ -79,7 +80,7 @@ TEST(block_template, update) hash blobs_hash; keccak(blobs.data(), static_cast(blobs.size()), blobs_hash.h); - ASSERT_EQ(blobs_hash, H("c043b754d043b4f6fe3f70273a937c77ff75a5afba9a6ca92776e79da28a1165")); + ASSERT_EQ(blobs_hash, H("da11e1ee86779a559df63a55e0b238ce5a67b977e0f68a0b347a39d37096a4bc")); // Test 2: mempool with high fee and low fee transactions, it must choose high fee transactions for (uint64_t i = 0; i < 512; ++i) { @@ -91,6 +92,7 @@ TEST(block_template, update) } tpl.update(data, mempool, &wallet); + ASSERT_EQ(tpl.get_reward(), 612054770773ULL); ASSERT_EQ(b->m_sidechainId, H("c9df4853003ab436416b9fc9a5a072d16b4dede849e697a8be2ebb9c88c8ec72")); ASSERT_EQ(b->m_transactions.size(), 203); @@ -99,7 +101,7 @@ TEST(block_template, update) ASSERT_GE(*reinterpret_cast(b->m_transactions[i].h), 256); } - tpl.get_hashing_blobs(0, 10000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); + tpl.get_hashing_blobs(0, 1000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); ASSERT_EQ(height, 2762973); ASSERT_EQ(diff, 300346053753ULL); @@ -109,7 +111,7 @@ TEST(block_template, update) ASSERT_EQ(template_id, 2U); keccak(blobs.data(), static_cast(blobs.size()), blobs_hash.h); - ASSERT_EQ(blobs_hash, H("f00e196216d160a4fcbf468f748205039d276d62edfa6c6fd4c81dbd1f62d9b7")); + ASSERT_EQ(blobs_hash, H("20aa6a98ca92bc4564bcdc367c078425d4b44b156c2bc7bb703ef055e4fd2c1b")); // Test 3: small but not empty mempool, and aux chains @@ -127,11 +129,12 @@ TEST(block_template, update) data.aux_chains.emplace_back(H("01f0cf665bd4cd31cbb2b2470236389c483522b350335e10a4a5dca34cb85990"), H("d9de1cfba7cdbd47f12f77addcb39b24c1ae7a16c35372bf28d6aee5d7579ee6"), difficulty_type(1000000)); tpl.update(data, mempool, &wallet); + ASSERT_EQ(tpl.get_reward(), 600300000000ULL); ASSERT_EQ(b->m_sidechainId, H("c32abac2cad40e263a94f5f43f90e0a7d7d4b151305b79951dbc8c88c3180613")); ASSERT_EQ(b->m_transactions.size(), 11); - tpl.get_hashing_blobs(0, 10000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); + tpl.get_hashing_blobs(0, 1000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); ASSERT_EQ(height, 2762973); ASSERT_EQ(diff, 300346053753ULL); @@ -141,7 +144,42 @@ TEST(block_template, update) ASSERT_EQ(template_id, 3U); keccak(blobs.data(), static_cast(blobs.size()), blobs_hash.h); - ASSERT_EQ(blobs_hash, H("17cd4d517a1a52ea919924c84261815e32a29decd0b194ad43ee814adb78eb9a")); + ASSERT_EQ(blobs_hash, H("536c0ee8013718b174b63613939379939cee2267e803f77cdabb05fcb47e846f")); + + // Test 4: mempool with a lot of transactions with various fees, all parts of transaction picking algorithm should be tested + + mempool.clear(); + + std::mt19937_64 rng; + + for (uint64_t i = 0; i < 10000; ++i) { + TxMempoolData tx; + + *reinterpret_cast(tx.id.h) = i; + + tx.weight = 1500 + (rng() % 10007); + tx.fee = 30000000 + (rng() % 100000007); + + mempool.add(tx); + } + + tpl.update(data, mempool, &wallet); + ASSERT_EQ(tpl.get_reward(), 619742028747ULL); + + ASSERT_EQ(b->m_sidechainId, H("69e7dd43dd99ac6be3f57ca333cc0d814189e83aee1773c99a341aca085c0d46")); + ASSERT_EQ(b->m_transactions.size(), 174); + + tpl.get_hashing_blobs(0, 1000, blobs, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset, template_id); + + ASSERT_EQ(height, 2762973); + ASSERT_EQ(diff, 300346053753ULL); + ASSERT_EQ(sidechain_diff, sidechain.difficulty()); + ASSERT_EQ(seed_hash, data.seed_hash); + ASSERT_EQ(nonce_offset, 39U); + ASSERT_EQ(template_id, 4U); + + keccak(blobs.data(), static_cast(blobs.size()), blobs_hash.h); + ASSERT_EQ(blobs_hash, H("4f62562aa84400eb085f58447d8daa45257369f1ec046b2150212329c9e86ae4")); destroy_crypto_cache(); }