From 864dbf17ca0067caea564a273a42f218047bba35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 5 Sep 2024 19:41:41 +0200 Subject: [PATCH] Fix compilation --- nano/core_test/ledger.cpp | 159 ++++++++++++++------------ nano/core_test/request_aggregator.cpp | 7 +- nano/secure/ledger.cpp | 4 +- nano/secure/ledger.hpp | 2 +- nano/test_common/testutil.cpp | 3 +- 5 files changed, 98 insertions(+), 77 deletions(-) diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index 1156ad11a8..2eb73adc48 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -5529,15 +5529,18 @@ TEST (ledger, cement_unbounded) auto ctx = nano::test::ledger_diamond (5); auto & ledger = ctx.ledger (); auto bottom = ctx.blocks ().back (); - auto confirmed = ledger.confirm (ledger.tx_begin_write (), bottom->hash ()); - ASSERT_TRUE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - // Check that all blocks got confirmed in a single call - ASSERT_TRUE (std::all_of (ctx.blocks ().begin (), ctx.blocks ().end (), [&] (auto const & block) { - return std::find_if (confirmed.begin (), confirmed.end (), [&] (auto const & block2) { - return block2->hash () == block->hash (); - }) - != confirmed.end (); - })); + { + auto tx = ledger.tx_begin_write (); + auto confirmed = ledger.confirm (tx, bottom->hash ()); + ASSERT_TRUE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + // Check that all blocks got confirmed in a single call + ASSERT_TRUE (std::all_of (ctx.blocks ().begin (), ctx.blocks ().end (), [&] (auto const & block) { + return std::find_if (confirmed.begin (), confirmed.end (), [&] (auto const & block2) { + return block2->hash () == block->hash (); + }) + != confirmed.end (); + })); + } } // Tests that bounded cementing works when recursion stack is large @@ -5546,33 +5549,39 @@ TEST (ledger, cement_bounded) auto ctx = nano::test::ledger_single_chain (64); auto & ledger = ctx.ledger (); auto bottom = ctx.blocks ().back (); - - // This should only cement some of the dependencies - auto confirmed1 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 3); - ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_EQ (confirmed1.size (), 3); - // Only topmost dependencies should get cemented during this call - ASSERT_TRUE (std::all_of (confirmed1.begin (), confirmed1.end (), [&] (auto const & block) { - return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); - })); - - // This should cement a few more dependencies - auto confirmed2 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 16); - ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_EQ (confirmed2.size (), 16); - // Only topmost dependencies should get cemented during this call - ASSERT_TRUE (std::all_of (confirmed2.begin (), confirmed2.end (), [&] (auto const & block) { - return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); - })); - - // This should cement the remaining dependencies - auto confirmed3 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 64); - ASSERT_TRUE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_LE (confirmed3.size (), 64); - // Every block in the ledger should be cemented - ASSERT_TRUE (std::all_of (ctx.blocks ().begin (), ctx.blocks ().end (), [&] (auto const & block) { - return ledger.confirmed.block_exists (ledger.tx_begin_read (), block->hash ()); - })); + { + // This should only cement some of the dependencies + auto tx = ledger.tx_begin_write (); + auto confirmed1 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 3); + ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_EQ (confirmed1.size (), 3); + // Only topmost dependencies should get cemented during this call + ASSERT_TRUE (std::all_of (confirmed1.begin (), confirmed1.end (), [&] (auto const & block) { + return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); + })); + } + { + // This should cement a few more dependencies + auto tx = ledger.tx_begin_write (); + auto confirmed2 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 16); + ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_EQ (confirmed2.size (), 16); + // Only topmost dependencies should get cemented during this call + ASSERT_TRUE (std::all_of (confirmed2.begin (), confirmed2.end (), [&] (auto const & block) { + return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); + })); + } + { + // This should cement the remaining dependencies + auto tx = ledger.tx_begin_write (); + auto confirmed3 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 64); + ASSERT_TRUE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_LE (confirmed3.size (), 64); + // Every block in the ledger should be cemented + ASSERT_TRUE (std::all_of (ctx.blocks ().begin (), ctx.blocks ().end (), [&] (auto const & block) { + return ledger.confirmed.block_exists (ledger.tx_begin_read (), block->hash ()); + })); + } } // Tests that bounded cementing works when mumber of blocks is large but tree height is small (recursion stack is small) @@ -5581,41 +5590,49 @@ TEST (ledger, cement_bounded_diamond) auto ctx = nano::test::ledger_diamond (4); auto & ledger = ctx.ledger (); auto bottom = ctx.blocks ().back (); - - // This should only cement some of the dependencies - auto confirmed1 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 3); - ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_EQ (confirmed1.size (), 3); - // Only topmost dependencies should get cemented during this call - ASSERT_TRUE (std::all_of (confirmed1.begin (), confirmed1.end (), [&] (auto const & block) { - return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); - })); - - // This should cement a few more dependencies - auto confirmed2 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 16); - ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_EQ (confirmed2.size (), 16); - // Only topmost dependencies should get cemented during this call - ASSERT_TRUE (std::all_of (confirmed2.begin (), confirmed2.end (), [&] (auto const & block) { - return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); - })); - - // A few more bounded calls should confirm the whole tree - auto confirmed3 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 64); - ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_EQ (confirmed3.size (), 64); - // Only topmost dependencies should get cemented during this call - ASSERT_TRUE (std::all_of (confirmed2.begin (), confirmed2.end (), [&] (auto const & block) { - return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); - })); - - auto confirmed4 = ledger.confirm (ledger.tx_begin_write (), bottom->hash (), /* max cementing batch size */ 64); - ASSERT_TRUE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); - ASSERT_LT (confirmed4.size (), 64); - // Every block in the ledger should be cemented - ASSERT_TRUE (std::all_of (ctx.blocks ().begin (), ctx.blocks ().end (), [&] (auto const & block) { - return ledger.confirmed.block_exists (ledger.tx_begin_read (), block->hash ()); - })); + { + // This should only cement some of the dependencies + auto tx = ledger.tx_begin_write (); + auto confirmed1 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 3); + ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_EQ (confirmed1.size (), 3); + // Only topmost dependencies should get cemented during this call + ASSERT_TRUE (std::all_of (confirmed1.begin (), confirmed1.end (), [&] (auto const & block) { + return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); + })); + } + { + // This should cement a few more dependencies + auto tx = ledger.tx_begin_write (); + auto confirmed2 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 16); + ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_EQ (confirmed2.size (), 16); + // Only topmost dependencies should get cemented during this call + ASSERT_TRUE (std::all_of (confirmed2.begin (), confirmed2.end (), [&] (auto const & block) { + return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); + })); + } + { + // A few more bounded calls should confirm the whole tree + auto tx = ledger.tx_begin_write (); + auto confirmed3 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 64); + ASSERT_FALSE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_EQ (confirmed3.size (), 64); + // Only topmost dependencies should get cemented during this call + ASSERT_TRUE (std::all_of (confirmed3.begin (), confirmed3.end (), [&] (auto const & block) { + return ledger.dependents_confirmed (ledger.tx_begin_read (), *block); + })); + } + { + auto tx = ledger.tx_begin_write (); + auto confirmed4 = ledger.confirm (tx, bottom->hash (), /* max cementing batch size */ 64); + ASSERT_TRUE (ledger.confirmed.block_exists (ledger.tx_begin_read (), bottom->hash ())); + ASSERT_LT (confirmed4.size (), 64); + // Every block in the ledger should be cemented + ASSERT_TRUE (std::all_of (ctx.blocks ().begin (), ctx.blocks ().end (), [&] (auto const & block) { + return ledger.confirmed.block_exists (ledger.tx_begin_read (), block->hash ()); + })); + } } // Test that nullopt can be returned when there are no receivable entries diff --git a/nano/core_test/request_aggregator.cpp b/nano/core_test/request_aggregator.cpp index 5cc40202bb..fcb580ef9a 100644 --- a/nano/core_test/request_aggregator.cpp +++ b/nano/core_test/request_aggregator.cpp @@ -290,8 +290,11 @@ TEST (request_aggregator, split) ASSERT_EQ (nano::block_status::progress, node.ledger.process (node.ledger.tx_begin_write (), block)); request.emplace_back (block->hash (), block->root ()); } - // Confirm all blocks - node.ledger.confirm (node.ledger.tx_begin_write (), blocks.back ()->hash ()); + { + // Confirm all blocks + auto tx = node.ledger.tx_begin_write (); + node.ledger.confirm (tx, blocks.back ()->hash ()); + } ASSERT_TIMELY_EQ (5s, max_vbh + 2, node.ledger.cemented_count ()); ASSERT_EQ (max_vbh + 1, request.size ()); auto client = std::make_shared (node); diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index 31a161c8ac..666b2759d5 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -851,7 +851,7 @@ std::deque> nano::ledger::confirm (secure::write_tr { // We must only confirm blocks that have their dependencies confirmed debug_assert (dependents_confirmed (transaction, *block)); - confirm (transaction, *block); + confirm_one (transaction, *block); result.push_back (block); } } @@ -878,7 +878,7 @@ std::deque> nano::ledger::confirm (secure::write_tr return result; } -void nano::ledger::confirm (secure::write_transaction const & transaction, nano::block const & block) +void nano::ledger::confirm_one (secure::write_transaction & transaction, nano::block const & block) { debug_assert ((!store.confirmation_height.get (transaction, block.account ()) && block.sideband ().height == 1) || store.confirmation_height.get (transaction, block.account ()).value ().height + 1 == block.sideband ().height); confirmation_height_info info{ block.sideband ().height, block.hash () }; diff --git a/nano/secure/ledger.hpp b/nano/secure/ledger.hpp index 284a9cfc54..3419731e11 100644 --- a/nano/secure/ledger.hpp +++ b/nano/secure/ledger.hpp @@ -100,7 +100,7 @@ class ledger final private: void initialize (nano::generate_cache_flags const &); - void confirm (secure::write_transaction const & transaction, nano::block const & block); + void confirm_one (secure::write_transaction &, nano::block const & block); std::unique_ptr any_impl; std::unique_ptr confirmed_impl; diff --git a/nano/test_common/testutil.cpp b/nano/test_common/testutil.cpp index d77eefc47b..59554846fa 100644 --- a/nano/test_common/testutil.cpp +++ b/nano/test_common/testutil.cpp @@ -138,7 +138,8 @@ void nano::test::confirm (nano::ledger & ledger, std::shared_ptr co void nano::test::confirm (nano::ledger & ledger, nano::block_hash const & hash) { - ledger.confirm (ledger.tx_begin_write (), hash); + auto transaction = ledger.tx_begin_write (); + ledger.confirm (transaction, hash); } bool nano::test::block_or_pruned_all_exists (nano::node & node, std::vector hashes)