Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience function for block_or_pruned_exists #4393

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,10 +1126,7 @@ TEST (bootstrap_processor, DISABLED_lazy_unclear_state_link)
node2->bootstrap_initiator.bootstrap_lazy (receive->hash ());
// Check processed blocks
ASSERT_TIMELY (10s, !node2->bootstrap_initiator.in_progress ());
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (receive->hash ()));
ASSERT_TIMELY (5s, nano::test::block_or_pruned_all_exists (*node2, { send1, send2, open, receive }));
ASSERT_EQ (0, node2->stats.count (nano::stat::type::bootstrap, nano::stat::detail::bulk_pull_failed_account, nano::stat::dir::in));
}

Expand Down Expand Up @@ -1185,9 +1182,7 @@ TEST (bootstrap_processor, lazy_unclear_state_link_not_existing)
node2->bootstrap_initiator.bootstrap_lazy (send2->hash ());
// Check processed blocks
ASSERT_TIMELY (15s, !node2->bootstrap_initiator.in_progress ());
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_TIMELY (5s, node2->ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TIMELY (15s, nano::test::block_or_pruned_all_exists (*node2, { send1, open, send2 }));
ASSERT_EQ (1, node2->stats.count (nano::stat::type::bootstrap, nano::stat::detail::bulk_pull_failed_account, nano::stat::dir::in));
}

Expand Down Expand Up @@ -1254,10 +1249,7 @@ TEST (bootstrap_processor, DISABLED_lazy_destinations)
node2->bootstrap_initiator.bootstrap_lazy (send2->hash ());
// Check processed blocks
ASSERT_TIMELY (10s, !node2->bootstrap_initiator.in_progress ());
ASSERT_TRUE (node2->ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TRUE (node2->ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TRUE (node2->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_TRUE (node2->ledger.block_or_pruned_exists (state_open->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (*node2, { send1, send2, open, state_open }));
}

TEST (bootstrap_processor, lazy_pruning_missing_block)
Expand Down Expand Up @@ -1329,10 +1321,7 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)
node1->ledger_pruning (2, false, false);
ASSERT_EQ (5, node1->ledger.cache.block_count);
ASSERT_EQ (1, node1->ledger.cache.pruned_count);
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (send1->hash ())); // true for pruned
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (state_open->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (*node1, { send1, send2, open, state_open }));
// Start lazy bootstrap with last block in sender chain
config.peering_port = system.get_available_port ();
auto node2 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), config, system.work, node_flags, 1));
Expand All @@ -1345,10 +1334,7 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)
// Some blocks cannot be retrieved from pruned node
node2->block_processor.flush ();
ASSERT_EQ (1, node2->ledger.cache.block_count);
ASSERT_FALSE (node2->ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_FALSE (node2->ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_FALSE (node2->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_FALSE (node2->ledger.block_or_pruned_exists (state_open->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_none_exists (*node2, { send1, send2, open, state_open }));
{
auto transaction (node2->store.tx_begin_read ());
ASSERT_TRUE (node2->unchecked.exists (nano::unchecked_key (send2->root ().as_block_hash (), send2->hash ())));
Expand All @@ -1359,10 +1345,8 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)
ASSERT_TIMELY (5s, !node2->bootstrap_initiator.in_progress ());
node2->block_processor.flush ();
ASSERT_EQ (3, node2->ledger.cache.block_count);
ASSERT_TRUE (node2->ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TRUE (node2->ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_FALSE (node2->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_FALSE (node2->ledger.block_or_pruned_exists (state_open->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (*node2, { send1, send2 }));
ASSERT_TRUE (nano::test::block_or_pruned_none_exists (*node2, { open, state_open }));
node2->stop ();
}

Expand Down Expand Up @@ -2034,9 +2018,7 @@ TEST (bulk, DISABLED_genesis_pruning)
node1->ledger_pruning (2, false, false);
ASSERT_EQ (2, node1->ledger.cache.pruned_count);
ASSERT_EQ (4, node1->ledger.cache.block_count);
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (send1->hash ())); // true for pruned
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (send2->hash ())); // true for pruned
ASSERT_TRUE (node1->ledger.block_or_pruned_exists (send3->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (*node2, { send1, send2, send3 }));
// Bootstrap with missing blocks for node2
node2->bootstrap_initiator.bootstrap (node1->network.endpoint (), false);
node2->network.merge_peer (node1->network.endpoint ());
Expand Down
18 changes: 5 additions & 13 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,14 @@ TEST (node, fork_multi_flip)
ASSERT_NE (nullptr, election1);
ASSERT_EQ (1, election1->votes ().size ());
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (publish1.block->hash ()));
ASSERT_TRUE (node2.ledger.block_or_pruned_exists (publish2.block->hash ()));
ASSERT_TRUE (node2.ledger.block_or_pruned_exists (publish3.block->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (node2, { publish2.block, publish3.block }));
ASSERT_TIMELY (10s, node2.ledger.block_or_pruned_exists (publish1.block->hash ()));
auto winner (*election1->tally ().begin ());
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (nano::dev::constants.genesis_amount - 100, winner.first);
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (publish1.block->hash ()));
ASSERT_TRUE (node2.ledger.block_or_pruned_exists (publish1.block->hash ()));
ASSERT_FALSE (node2.ledger.block_or_pruned_exists (publish2.block->hash ()));
ASSERT_FALSE (node2.ledger.block_or_pruned_exists (publish3.block->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_none_exists (node2, { publish2.block, publish3.block }));
}

// Blocks that are no longer actively being voted on should be able to be evicted through bootstrapping.
Expand Down Expand Up @@ -4184,9 +4182,7 @@ TEST (node, pruning_automatic)
ASSERT_EQ (1, node1.ledger.cache.pruned_count);
ASSERT_EQ (3, node1.ledger.cache.block_count);

ASSERT_TRUE (node1.ledger.block_or_pruned_exists (nano::dev::genesis->hash ()));
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (node1, { nano::dev::genesis, send1, send2 }));
}

TEST (node, pruning_age)
Expand Down Expand Up @@ -4245,9 +4241,7 @@ TEST (node, pruning_age)
ASSERT_EQ (1, node1.ledger.cache.pruned_count);
ASSERT_EQ (3, node1.ledger.cache.block_count);

ASSERT_TRUE (node1.ledger.block_or_pruned_exists (nano::dev::genesis->hash ()));
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (node1, { nano::dev::genesis, send1, send2 }));
}

// Test that a node configured with `enable_pruning` will
Expand Down Expand Up @@ -4308,9 +4302,7 @@ TEST (node, pruning_depth)
ASSERT_EQ (1, node1.ledger.cache.pruned_count);
ASSERT_EQ (3, node1.ledger.cache.block_count);

ASSERT_TRUE (node1.ledger.block_or_pruned_exists (nano::dev::genesis->hash ()));
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (send2->hash ()));
ASSERT_TRUE (nano::test::block_or_pruned_all_exists (node1, { nano::dev::genesis, send1, send2 }));
}

TEST (node_config, node_id_private_key_persistence)
Expand Down
3 changes: 1 addition & 2 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6503,8 +6503,7 @@ TEST (rpc, account_lazy_start)

// needs timed assert because the writing (put) operation is done by a different
// thread, it might not get done before DB get operation.
ASSERT_TIMELY (10s, node2->ledger.block_or_pruned_exists (send1->hash ()));
ASSERT_TIMELY (10s, node2->ledger.block_or_pruned_exists (open->hash ()));
ASSERT_TIMELY (15s, nano::test::block_or_pruned_all_exists (*node2, { send1, open }));
}

TEST (rpc, receive)
Expand Down
28 changes: 28 additions & 0 deletions nano/test_common/testutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ bool nano::test::exists (nano::node & node, std::vector<std::shared_ptr<nano::bl
return exists (node, blocks_to_hashes (blocks));
}

bool nano::test::block_or_pruned_all_exists (nano::node & node, std::vector<nano::block_hash> hashes)
{
auto transaction = node.store.tx_begin_read ();
return std::all_of (hashes.begin (), hashes.end (),
[&] (const auto & hash) {
return node.ledger.block_or_pruned_exists (transaction, hash);
});
}

bool nano::test::block_or_pruned_all_exists (nano::node & node, std::vector<std::shared_ptr<nano::block>> blocks)
{
return block_or_pruned_all_exists (node, blocks_to_hashes (blocks));
}

bool nano::test::block_or_pruned_none_exists (nano::node & node, std::vector<nano::block_hash> hashes)
{
auto transaction = node.store.tx_begin_read ();
return std::none_of (hashes.begin (), hashes.end (),
[&] (const auto & hash) {
return node.ledger.block_or_pruned_exists (transaction, hash);
});
}

bool nano::test::block_or_pruned_none_exists (nano::node & node, std::vector<std::shared_ptr<nano::block>> blocks)
{
return block_or_pruned_none_exists (node, blocks_to_hashes (blocks));
}

bool nano::test::activate (nano::node & node, std::vector<nano::block_hash> hashes)
{
for (auto & hash : hashes)
Expand Down
20 changes: 20 additions & 0 deletions nano/test_common/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ namespace test
* @return true if all blocks are fully processed and inserted in the ledger, false otherwise
*/
bool exists (nano::node & node, std::vector<std::shared_ptr<nano::block>> blocks);
/*
* Convenience function to check whether *all* of the hashes exists in node ledger or in the pruned table.
* @return true if all blocks are fully processed and inserted in the ledger, false otherwise
*/
bool block_or_pruned_all_exists (nano::node & node, std::vector<nano::block_hash> hashes);
/*
* Convenience function to check whether *all* of the blocks exists in node ledger or their hash exists in the pruned table.
* @return true if all blocks are fully processed and inserted in the ledger, false otherwise
*/
bool block_or_pruned_all_exists (nano::node & node, std::vector<std::shared_ptr<nano::block>> blocks);
/*
* Convenience function to check whether *none* of the hashes exists in node ledger or in the pruned table.
* @return true if none of the blocks are processed and inserted in the ledger, false otherwise
*/
bool block_or_pruned_none_exists (nano::node & node, std::vector<nano::block_hash> hashes);
/*
* Convenience function to check whether *none* of the blocks exists in node ledger or their hash exists in the pruned table.
* @return true if none of the blocks are processed and inserted in the ledger, false otherwise
*/
bool block_or_pruned_none_exists (nano::node & node, std::vector<std::shared_ptr<nano::block>> blocks);
/*
* Convenience function to start elections for a list of hashes. Blocks are loaded from ledger.
* @return true if all blocks exist and were queued to election scheduler
Expand Down
Loading