Skip to content

Commit

Permalink
Merge pull request #4605 from clemahieu/testing_direct_confirmation
Browse files Browse the repository at this point in the history
Directly confirm blocks within unit tests rather than starting/forcing elections
  • Loading branch information
clemahieu committed May 9, 2024
2 parents 9cd662c + 4c813b2 commit 21e48cd
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 104 deletions.
12 changes: 2 additions & 10 deletions nano/core_test/active_elections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ TEST (active_elections, confirm_frontier)
// we cannot use the same block instance on 2 different nodes, so make a copy
auto send_copy = builder.make_block ().from (*send).build ();
ASSERT_TRUE (nano::test::process (node1, { send_copy }));
ASSERT_TRUE (nano::test::start_elections (system, node1, { send_copy }));
ASSERT_TIMELY (5s, nano::test::confirmed (node1, { send_copy }));
nano::test::confirm (node1.ledger, send_copy);
}

// The rep crawler would otherwise request confirmations in order to find representatives
Expand Down Expand Up @@ -193,14 +192,7 @@ TEST (active_elections, keep_local)
ASSERT_NE (nullptr, send6);

// force-confirm blocks
for (auto const & block : { send1, send2, send3, send4, send5, send6 })
{
std::shared_ptr<nano::election> election{};
ASSERT_TIMELY (5s, (election = node.active.election (block->qualified_root ())) != nullptr);
node.process_confirmed (nano::election_status{ block });
election->force_confirm ();
ASSERT_TIMELY (5s, node.block_confirmed (block->hash ()));
}
nano::test::confirm (node.ledger, send6);

nano::state_block_builder builder{};
const auto receive1 = builder.make_block ()
Expand Down
52 changes: 11 additions & 41 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <nano/node/bootstrap/bootstrap_lazy.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/secure/ledger_set_any.hpp>
#include <nano/test_common/chains.hpp>
#include <nano/test_common/network.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>
Expand Down Expand Up @@ -955,8 +956,7 @@ TEST (bootstrap_processor, lazy_hash_pruning)

std::vector<std::shared_ptr<nano::block>> blocks = { send1, receive1, change1, change2, send2, receive2, send3, receive3 };
ASSERT_TRUE (nano::test::process (*node0, blocks));
ASSERT_TRUE (nano::test::start_elections (system, *node0, blocks, true));
ASSERT_TIMELY (5s, nano::test::confirmed (*node0, blocks));
nano::test::confirm (node0->ledger, blocks);

config.peering_port = system.get_available_port ();
auto node1 = system.make_disconnected_node (config, node_flags);
Expand All @@ -969,12 +969,7 @@ TEST (bootstrap_processor, lazy_hash_pruning)
ASSERT_TIMELY (5s, nano::test::exists (*node1, { send1, receive1, change1, change2 }));

// Confirm last block to prune previous
ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, receive1, change1, change2 }, true));
ASSERT_TIMELY (5s, node1->block_confirmed (send1->hash ()));
ASSERT_TIMELY (5s, node1->block_confirmed (receive1->hash ()));
ASSERT_TIMELY (5s, node1->block_confirmed (change1->hash ()));
ASSERT_TIMELY (5s, node1->block_confirmed (change2->hash ()));
ASSERT_TIMELY (5s, node1->active.empty ());
nano::test::confirm (node1->ledger, { send1, receive1, change1, change2 });
ASSERT_EQ (5, node1->ledger.block_count ());
ASSERT_EQ (5, node1->ledger.cemented_count ());

Expand Down Expand Up @@ -1368,7 +1363,7 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)
std::vector<std::shared_ptr<nano::block>> const blocks{ send1, send2, open, state_open };
ASSERT_TRUE (nano::test::process (*node1, blocks));
ASSERT_TIMELY (5s, nano::test::exists (*node1, blocks));
nano::test::force_confirm (node1->ledger, blocks);
nano::test::confirm (node1->ledger, blocks);
ASSERT_TIMELY (5s, nano::test::confirmed (*node1, blocks));
ASSERT_EQ (5, node1->ledger.block_count ());
ASSERT_EQ (5, node1->ledger.cemented_count ());
Expand Down Expand Up @@ -1922,8 +1917,7 @@ TEST (frontier_req, confirmed_frontier)
ASSERT_EQ (receive2->hash (), request5->frontier);

// Confirm account before genesis (confirmed only)
ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, receive1 }, true));
ASSERT_TIMELY (5s, node1->block_confirmed (send1->hash ()) && node1->block_confirmed (receive1->hash ()));
nano::test::confirm (node1->ledger, receive1);
auto connection6 (std::make_shared<nano::transport::tcp_server> (std::make_shared<nano::transport::socket> (*node1, nano::transport::socket_endpoint::server), node1));
auto req6 = std::make_unique<nano::frontier_req> (nano::dev::network_params.network);
req6->start = key_before_genesis.pub;
Expand All @@ -1937,8 +1931,7 @@ TEST (frontier_req, confirmed_frontier)
ASSERT_EQ (receive1->hash (), request6->frontier);

// Confirm account after genesis (confirmed only)
ASSERT_TRUE (nano::test::start_elections (system, *node1, { send2, receive2 }, true));
ASSERT_TIMELY (5s, node1->block_confirmed (send2->hash ()) && node1->block_confirmed (receive2->hash ()));
nano::test::confirm (node1->ledger, receive2);
auto connection7 (std::make_shared<nano::transport::tcp_server> (std::make_shared<nano::transport::socket> (*node1, nano::transport::socket_endpoint::server), node1));
auto req7 = std::make_unique<nano::frontier_req> (nano::dev::network_params.network);
req7->start = key_after_genesis.pub;
Expand Down Expand Up @@ -2031,35 +2024,12 @@ TEST (bulk, genesis_pruning)
node_flags.enable_pruning = true;

auto node1 = system.add_node (config, node_flags);
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);

// do 3 sends from genesis to key2
nano::keypair key2;
auto send1 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, 100));
ASSERT_NE (nullptr, send1);
auto send2 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, 100));
ASSERT_NE (nullptr, send2);
auto send3 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key2.pub, 100));
ASSERT_NE (nullptr, send3);

{
auto transaction (node1->wallets.tx_begin_write ());
system.wallet (0)->store.erase (transaction, nano::dev::genesis_key.pub);
}

ASSERT_TIMELY_EQ (5s, send3->hash (), node1->latest (nano::dev::genesis_key.pub));

ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1 }, true));
ASSERT_TIMELY (5s, node1->active.active (send2->qualified_root ()));
ASSERT_EQ (0, node1->ledger.pruned_count ());

ASSERT_TRUE (nano::test::start_elections (system, *node1, { send2 }, true));
ASSERT_TIMELY (5s, node1->active.active (send3->qualified_root ()));
ASSERT_EQ (0, node1->ledger.pruned_count ());

ASSERT_TRUE (nano::test::start_elections (system, *node1, { send3 }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (*node1, { send3 }));
auto blocks = nano::test::setup_chain (system, *node1, 3);
auto send1 = blocks[0];
auto send2 = blocks[1];
auto send3 = blocks[2];

ASSERT_EQ (4, node1->ledger.block_count ());
node1->ledger_pruning (2, false);
ASSERT_EQ (2, node1->ledger.pruned_count ());
ASSERT_EQ (4, node1->ledger.block_count ());
Expand Down
3 changes: 1 addition & 2 deletions nano/core_test/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ TEST (election, continuous_voting)
.build ();

ASSERT_TRUE (nano::test::process (node1, { send1 }));
ASSERT_TRUE (nano::test::start_elections (system, node1, { send1 }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (node1, { send1 }));
nano::test::confirm (node1.ledger, send1);

node1.stats.clear ();

Expand Down
9 changes: 3 additions & 6 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,8 +2164,7 @@ TEST (node, vote_by_hash_bundle)
}

// Confirming last block will confirm whole chain and allow us to generate votes for those blocks later
ASSERT_TRUE (nano::test::start_elections (system, node, { blocks.back () }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (node, { blocks.back () }));
nano::test::confirm (node.ledger, blocks.back ());

system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
nano::keypair key1;
Expand Down Expand Up @@ -2343,8 +2342,7 @@ TEST (node, epoch_conflict_confirm)
ASSERT_TRUE (nano::test::process (node1, { send, send2, open }));

// Confirm open block in node1 to allow generating votes
ASSERT_TRUE (nano::test::start_elections (system, node1, { open }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (node1, { open }));
nano::test::confirm (node1.ledger, open);

// Process initial blocks on node0
ASSERT_TRUE (nano::test::process (node0, { send, send2, open }));
Expand Down Expand Up @@ -3029,8 +3027,7 @@ TEST (node, rollback_vote_self)

// Process and mark the first 2 blocks as confirmed to allow voting
ASSERT_TRUE (nano::test::process (node, { send1, open }));
ASSERT_TRUE (nano::test::start_elections (system, node, { send1, open }, true));
ASSERT_TIMELY_EQ (5s, node.ledger.cemented_count (), 3);
nano::test::confirm (node.ledger, open);

// wait until the rep weights have caught up with the weight transfer
ASSERT_TIMELY_EQ (5s, nano::dev::constants.genesis_amount / 2, node.weight (key.pub));
Expand Down
6 changes: 2 additions & 4 deletions nano/core_test/optimistic_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ TEST (optimistic_scheduler, activate_one)
auto & [account, blocks] = chains.front ();

// Confirm block towards at the beginning the chain, so gap between confirmation and account frontier is larger than `gap_threshold`
ASSERT_TRUE (nano::test::start_elections (system, node, { blocks.at (11) }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (node, { blocks.at (11) }));
nano::test::confirm (node.ledger, blocks.at (11));

// Ensure unconfirmed account head block gets activated
auto const & block = blocks.back ();
Expand Down Expand Up @@ -96,8 +95,7 @@ TEST (optimistic_scheduler, under_gap_threshold)
auto & [account, blocks] = chains.front ();

// Confirm block towards the end of the chain, so gap between confirmation and account frontier is less than `gap_threshold`
ASSERT_TRUE (nano::test::start_elections (system, node, { blocks.at (55) }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (node, { blocks.at (55) }));
nano::test::confirm (node.ledger, blocks.at (55));

// Manually trigger backlog scan
node.backlog.trigger ();
Expand Down
6 changes: 2 additions & 4 deletions nano/rpc_test/receivable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ TEST (rpc, receivable_unconfirmed)
ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0));
request.put ("include_only_confirmed", "false");
ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1));
ASSERT_TRUE (nano::test::start_elections (system, *node, { block1->hash () }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (*node, { block1 }));
nano::test::confirm (node->ledger, block1);
request.put ("include_only_confirmed", "true");
ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1));
}
Expand Down Expand Up @@ -550,8 +549,7 @@ TEST (rpc, accounts_receivable_confirmed)
ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 0));
request.put ("include_only_confirmed", "false");
ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1));
ASSERT_TRUE (nano::test::start_elections (system, *node, { block1->hash () }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (*node, { block1 }));
nano::test::confirm (node->ledger, block1);
request.put ("include_only_confirmed", "true");
ASSERT_TRUE (check_block_response_count (system, rpc_ctx, request, 1));
}
13 changes: 3 additions & 10 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,11 +1259,7 @@ TEST (rpc, history_pruning)
ASSERT_TIMELY (5s, nano::test::exists (*node0, blocks));
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);

ASSERT_TRUE (nano::test::start_elections (system, *node0, blocks, true));
ASSERT_TIMELY (5s, node0->block_confirmed (uchange->hash ()));
nano::confirmation_height_info confirmation_height_info;
node0->store.confirmation_height.get (node0->store.tx_begin_read (), nano::dev::genesis_key.pub, confirmation_height_info);
ASSERT_EQ (7, confirmation_height_info.height);
nano::test::confirm (node0->ledger, blocks);

// Prune block "change"
{
Expand Down Expand Up @@ -5889,11 +5885,8 @@ TEST (rpc, block_confirmed)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (latest))
.build ();
node->process_active (send);
ASSERT_TRUE (nano::test::start_elections (system, *node, { send }, true));

// Wait until the confirmation height has been set
ASSERT_TIMELY (5s, node->ledger.confirmed.block_exists_or_pruned (node->ledger.tx_begin_read (), send->hash ()) && !node->confirming_set.exists (send->hash ()));
ASSERT_EQ (nano::block_status::progress, node->ledger.process (node->ledger.tx_begin_write (), send));
nano::test::confirm (node->ledger, send);

// Requesting confirmation for this should now succeed
request.put ("hash", send->hash ().to_string ());
Expand Down
6 changes: 0 additions & 6 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,9 +1401,3 @@ std::unique_ptr<nano::container_info_component> nano::ledger::collect_container_
composite->add_component (cache.rep_weights.collect_container_info ("rep_weights"));
return composite;
}

void nano::ledger::force_confirm (secure::write_transaction const & transaction, nano::block const & block)
{
release_assert (*constants.genesis == *constants.nano_dev_genesis);
confirm (transaction, block);
}
3 changes: 0 additions & 3 deletions nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,5 @@ class ledger final
public:
ledger_set_any & any;
ledger_set_confirmed & confirmed;

public: // Only used in tests
void force_confirm (secure::write_transaction const & transaction, nano::block const & block);
};
}
4 changes: 2 additions & 2 deletions nano/slow_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,9 +1957,9 @@ TEST (node, aggressive_flooding)
ASSERT_EQ (node1.latest (nano::dev::genesis_key.pub), node_wallet.first->latest (nano::dev::genesis_key.pub));
ASSERT_EQ (genesis_blocks.back ()->hash (), node_wallet.first->latest (nano::dev::genesis_key.pub));
// Confirm blocks for rep crawler & receiving
ASSERT_TRUE (nano::test::start_elections (system, *node_wallet.first, { genesis_blocks.back () }, true));
nano::test::confirm (node_wallet.first->ledger, genesis_blocks.back ());
}
ASSERT_TRUE (nano::test::start_elections (system, node1, { genesis_blocks.back () }, true));
nano::test::confirm (node1.ledger, genesis_blocks.back ());

// Wait until all genesis blocks are received
auto all_received = [&nodes_wallets] () {
Expand Down
6 changes: 2 additions & 4 deletions nano/slow_test/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ nano::keypair setup_rep (nano::test::system & system, nano::node & node, nano::u
.build ();

EXPECT_TRUE (nano::test::process (node, { send, open }));
EXPECT_TRUE (nano::test::start_elections (system, node, { send, open }, true));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open }));
nano::test::confirm (node.ledger, open->hash ());

return key;
}
Expand Down Expand Up @@ -107,8 +106,7 @@ std::vector<std::shared_ptr<nano::block>> setup_blocks (nano::test::system & sys
EXPECT_TRUE (nano::test::process (node, receives));

// Confirm whole genesis chain at once
EXPECT_TRUE (nano::test::start_elections (system, node, { sends.back () }, true));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { sends }));
nano::test::confirm (node.ledger, sends.back ()->hash ());

std::cout << "setup_blocks done" << std::endl;

Expand Down
15 changes: 7 additions & 8 deletions nano/test_common/chains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ nano::block_list_t nano::test::setup_chain (nano::test::system & system, nano::n
if (confirm)
{
// Confirm whole chain at once
EXPECT_TRUE (nano::test::start_elections (system, node, { blocks.back () }, true));
EXPECT_TIMELY (5s, nano::test::confirmed (node, blocks));
nano::test::confirm (node.ledger, blocks);
}

return blocks;
Expand Down Expand Up @@ -84,8 +83,7 @@ std::vector<std::pair<nano::account, nano::block_list_t>> nano::test::setup_chai
if (confirm)
{
// Ensure blocks are in the ledger and confirmed
EXPECT_TRUE (nano::test::start_elections (system, node, { send, open }, true));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open }));
nano::test::confirm (node.ledger, open);
}

auto added_blocks = nano::test::setup_chain (system, node, block_count, key, confirm);
Expand Down Expand Up @@ -143,8 +141,7 @@ nano::block_list_t nano::test::setup_independent_blocks (nano::test::system & sy
}

// Confirm whole genesis chain at once
EXPECT_TRUE (nano::test::start_elections (system, node, { latest }, true));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { latest }));
nano::test::confirm (node.ledger, latest);

return blocks;
}
Expand Down Expand Up @@ -177,8 +174,10 @@ std::pair<std::shared_ptr<nano::block>, std::shared_ptr<nano::block>> nano::test
.build ();

EXPECT_TRUE (nano::test::process (node, { send, open }));
EXPECT_TRUE (nano::test::start_elections (system, node, { send, open }, force_confirm));
EXPECT_TIMELY (5s, nano::test::confirmed (node, { send, open }));
if (force_confirm)
{
nano::test::confirm (node.ledger, open);
}
return std::make_pair (send, open);
}

Expand Down
14 changes: 12 additions & 2 deletions nano/test_common/testutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,24 @@ bool nano::test::exists (nano::node & node, std::vector<std::shared_ptr<nano::bl
return exists (node, blocks_to_hashes (blocks));
}

void nano::test::force_confirm (nano::ledger & ledger, std::vector<std::shared_ptr<nano::block>> const blocks)
void nano::test::confirm (nano::ledger & ledger, std::vector<std::shared_ptr<nano::block>> const blocks)
{
for (auto const block : blocks)
{
ledger.force_confirm (ledger.tx_begin_write (), *block);
confirm (ledger, block);
}
}

void nano::test::confirm (nano::ledger & ledger, std::shared_ptr<nano::block> const block)
{
confirm (ledger, block->hash ());
}

void nano::test::confirm (nano::ledger & ledger, nano::block_hash const & hash)
{
ledger.confirm (ledger.tx_begin_write (), hash);
}

bool nano::test::block_or_pruned_all_exists (nano::node & node, std::vector<nano::block_hash> hashes)
{
auto transaction = node.ledger.tx_begin_read ();
Expand Down
6 changes: 4 additions & 2 deletions nano/test_common/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ namespace test
* height of the account to be the height of the block.
* The blocks are confirmed in the order that they are given.
*/
void force_confirm (nano::ledger & ledger, std::vector<std::shared_ptr<nano::block>> const blocks);
void confirm (nano::ledger & ledger, std::vector<std::shared_ptr<nano::block>> const blocks);
void confirm (nano::ledger & ledger, std::shared_ptr<nano::block> const block);
void confirm (nano::ledger & ledger, nano::block_hash const & hash);
/*
* 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
Expand Down Expand Up @@ -439,4 +441,4 @@ namespace test
*/
std::vector<std::shared_ptr<nano::block>> all_blocks (nano::node &);
}
}
}

0 comments on commit 21e48cd

Please sign in to comment.