Skip to content

Commit

Permalink
Encapsulate ledger_cache counts and return them from functions on nan…
Browse files Browse the repository at this point in the history
…o::ledger
  • Loading branch information
clemahieu committed Mar 30, 2024
1 parent 8d7f7d2 commit 324be97
Show file tree
Hide file tree
Showing 26 changed files with 256 additions and 227 deletions.
22 changes: 11 additions & 11 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ TEST (active_transactions, confirm_frontier)
std::shared_ptr<nano::election> election2;
ASSERT_TIMELY (5s, election2 = node2.active.election (send->qualified_root ()));
ASSERT_TIMELY (5s, nano::test::confirmed (node2, { send }));
ASSERT_TIMELY_EQ (5s, node2.ledger.cache.cemented_count, 2);
ASSERT_TIMELY_EQ (5s, node2.ledger.cemented_count (), 2);
ASSERT_TIMELY (5s, node2.active.empty ());
ASSERT_GT (election2->confirmation_request_count, 0u);
}
Expand Down Expand Up @@ -486,9 +486,9 @@ TEST (inactive_votes_cache, election_start)
ASSERT_EQ (nano::block_status::progress, node.process (send2));
ASSERT_EQ (nano::block_status::progress, node.process (open1));
ASSERT_EQ (nano::block_status::progress, node.process (open2));
ASSERT_TIMELY_EQ (5s, 5, node.ledger.cache.block_count);
ASSERT_TIMELY_EQ (5s, 5, node.ledger.block_count ());
ASSERT_TRUE (node.active.empty ());
ASSERT_EQ (1, node.ledger.cache.cemented_count);
ASSERT_EQ (1, node.ledger.cemented_count ());
// These blocks will be processed later
auto send3 = send_block_builder.make_block ()
.previous (send2->hash ())
Expand All @@ -510,7 +510,7 @@ TEST (inactive_votes_cache, election_start)
node.vote_processor.vote (vote1, std::make_shared<nano::transport::inproc::channel> (node, node));
ASSERT_TIMELY_EQ (5s, node.vote_cache.size (), 3);
ASSERT_TRUE (node.active.empty ());
ASSERT_EQ (1, node.ledger.cache.cemented_count);
ASSERT_EQ (1, node.ledger.cemented_count ());

// 2 votes are required to start election (dev network)
auto vote2 = nano::test::make_vote (key2, { open1, open2, send4 });
Expand All @@ -523,7 +523,7 @@ TEST (inactive_votes_cache, election_start)
auto vote0 = nano::test::make_final_vote (nano::dev::genesis_key, { open1, open2, send4 });
node.vote_processor.vote (vote0, std::make_shared<nano::transport::inproc::channel> (node, node));
ASSERT_TIMELY_EQ (5s, 0, node.active.size ());
ASSERT_TIMELY_EQ (5s, 5, node.ledger.cache.cemented_count);
ASSERT_TIMELY_EQ (5s, 5, node.ledger.cemented_count ());
ASSERT_TRUE (nano::test::confirmed (node, { send1, send2, open1, open2 }));

// A late block arrival also checks the inactive votes cache
Expand All @@ -536,7 +536,7 @@ TEST (inactive_votes_cache, election_start)
// send7 cannot be voted on but an election should be started from inactive votes
ASSERT_FALSE (node.ledger.dependents_confirmed (node.store.tx_begin_read (), *send4));
node.process_active (send4);
ASSERT_TIMELY_EQ (5s, 7, node.ledger.cache.cemented_count);
ASSERT_TIMELY_EQ (5s, 7, node.ledger.cemented_count ());
}

namespace nano
Expand Down Expand Up @@ -829,8 +829,8 @@ TEST (active_transactions, fork_filter_cleanup)
// how about node1 picking up "send1" from node2? we know it does because we assert at
// the end that it is within node1's AEC, but why node1.block_count doesn't increase?
//
ASSERT_TIMELY_EQ (5s, node2.ledger.cache.block_count, 2);
ASSERT_TIMELY_EQ (5s, node1.ledger.cache.block_count, 2);
ASSERT_TIMELY_EQ (5s, node2.ledger.block_count (), 2);
ASSERT_TIMELY_EQ (5s, node1.ledger.block_count (), 2);

// Block is erased from the duplicate filter
ASSERT_TIMELY (5s, node1.network.publish_filter.apply (send_block_bytes.data (), send_block_bytes.size ()));
Expand Down Expand Up @@ -890,7 +890,7 @@ TEST (active_transactions, fork_replacement_tally)
auto vote = nano::test::make_final_vote (nano::dev::genesis_key, { send, open });
node1.vote_processor.vote (vote, std::make_shared<nano::transport::inproc::channel> (node1, node1));
}
ASSERT_TIMELY_EQ (5s, node1.ledger.cache.cemented_count, 1 + 2 * reps_count);
ASSERT_TIMELY_EQ (5s, node1.ledger.cemented_count (), 1 + 2 * reps_count);

nano::keypair key;
auto send_last = builder.make_block ()
Expand Down Expand Up @@ -1050,8 +1050,8 @@ TEST (active_transactions, confirm_new)
// Let node2 know about the block
ASSERT_TIMELY (5s, node2.block (send->hash ()));
// Wait confirmation
ASSERT_TIMELY (5s, node1.ledger.cache.cemented_count == 2);
ASSERT_TIMELY (5s, node2.ledger.cache.cemented_count == 2);
ASSERT_TIMELY (5s, node1.ledger.cemented_count () == 2);
ASSERT_TIMELY (5s, node2.ledger.cemented_count () == 2);
}

// Ensures votes are tallied on election::publish even if no vote is inserted through inactive_votes_cache
Expand Down
7 changes: 4 additions & 3 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,10 @@ TEST (block_store, cemented_count_cache)
auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants);
ASSERT_TRUE (!store->init_error ());
auto transaction (store->tx_begin_write ());
nano::ledger_cache ledger_cache{ store->rep_weight };
store->initialize (transaction, ledger_cache, nano::dev::constants);
ASSERT_EQ (1, ledger_cache.cemented_count);
nano::stats stats;
nano::ledger ledger (*store, stats, nano::dev::constants);
store->initialize (transaction, ledger.cache, nano::dev::constants);
ASSERT_EQ (1, ledger.cemented_count ());
}

TEST (block_store, block_random)
Expand Down
44 changes: 22 additions & 22 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,8 @@ TEST (bootstrap_processor, push_diamond_pruning)
ASSERT_TRUE (node1->store.pruned.exists (transaction, open->hash ()));
ASSERT_TRUE (node1->ledger.block_exists (transaction, send2->hash ()));
ASSERT_TRUE (node1->ledger.block_exists (transaction, receive->hash ()));
ASSERT_EQ (2, node1->ledger.cache.pruned_count);
ASSERT_EQ (5, node1->ledger.cache.block_count);
ASSERT_EQ (2, node1->ledger.pruned_count ());
ASSERT_EQ (5, node1->ledger.block_count ());
}

// 2nd bootstrap
Expand Down Expand Up @@ -985,22 +985,22 @@ TEST (bootstrap_processor, lazy_hash_pruning)
ASSERT_TIMELY (5s, node1->block_confirmed (change1->hash ()));
ASSERT_TIMELY (5s, node1->block_confirmed (change2->hash ()));
ASSERT_TIMELY (5s, node1->active.empty ());
ASSERT_EQ (5, node1->ledger.cache.block_count);
ASSERT_EQ (5, node1->ledger.cache.cemented_count);
ASSERT_EQ (5, node1->ledger.block_count ());
ASSERT_EQ (5, node1->ledger.cemented_count ());

// Pruning action
node1->ledger_pruning (2, false);
ASSERT_EQ (9, node0->ledger.cache.block_count);
ASSERT_EQ (0, node0->ledger.cache.pruned_count);
ASSERT_EQ (5, node1->ledger.cache.block_count);
ASSERT_EQ (3, node1->ledger.cache.pruned_count);
ASSERT_EQ (9, node0->ledger.block_count ());
ASSERT_EQ (0, node0->ledger.pruned_count ());
ASSERT_EQ (5, node1->ledger.block_count ());
ASSERT_EQ (3, node1->ledger.pruned_count ());

// Start lazy bootstrap with last block in chain known
nano::test::establish_tcp (system, *node1, node0->network.endpoint ());
node1->bootstrap_initiator.bootstrap_lazy (receive3->hash (), true);

// Check processed blocks
ASSERT_TIMELY_EQ (5s, node1->ledger.cache.block_count, 9);
ASSERT_TIMELY_EQ (5s, node1->ledger.block_count (), 9);
ASSERT_TIMELY (5s, node1->balance (key2.pub) != 0);
ASSERT_TIMELY (5s, !node1->bootstrap_initiator.in_progress ());
node1->stop ();
Expand Down Expand Up @@ -1386,14 +1386,14 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)
// Confirm last block to prune previous
ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1, send2, open, state_open }, true));
ASSERT_TIMELY (5s, nano::test::confirmed (*node1, { send2, open, state_open }));
ASSERT_EQ (5, node1->ledger.cache.block_count);
ASSERT_EQ (5, node1->ledger.cache.cemented_count);
ASSERT_EQ (5, node1->ledger.block_count ());
ASSERT_EQ (5, node1->ledger.cemented_count ());

// Pruning action, send1 should get pruned
ASSERT_EQ (0, node1->ledger.cache.pruned_count);
ASSERT_EQ (0, node1->ledger.pruned_count ());
node1->ledger_pruning (2, false);
ASSERT_EQ (1, node1->ledger.cache.pruned_count);
ASSERT_EQ (5, node1->ledger.cache.block_count);
ASSERT_EQ (1, node1->ledger.pruned_count ());
ASSERT_EQ (5, node1->ledger.block_count ());
ASSERT_TRUE (node1->ledger.store.pruned.exists (node1->ledger.store.tx_begin_read (), send1->hash ()));
ASSERT_TRUE (nano::test::exists (*node1, { send2, open, state_open }));

Expand All @@ -1409,7 +1409,7 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)
ASSERT_TIMELY (5s, lazy_attempt->stopped || lazy_attempt->requeued_pulls >= 4);

// Some blocks cannot be retrieved from pruned node
ASSERT_EQ (1, node2->ledger.cache.block_count);
ASSERT_EQ (1, node2->ledger.block_count ());
ASSERT_TRUE (nano::test::block_or_pruned_none_exists (*node2, { send1, send2, open, state_open }));
{
auto transaction (node2->store.tx_begin_read ());
Expand All @@ -1418,7 +1418,7 @@ TEST (bootstrap_processor, lazy_pruning_missing_block)

// Insert missing block
node2->process_active (send1);
ASSERT_TIMELY_EQ (5s, 3, node2->ledger.cache.block_count);
ASSERT_TIMELY_EQ (5s, 3, node2->ledger.block_count ());
ASSERT_TIMELY (5s, nano::test::exists (*node2, { send1, send2 }));
ASSERT_TRUE (nano::test::block_or_pruned_none_exists (*node2, { open, state_open }));
node2->stop ();
Expand Down Expand Up @@ -2073,18 +2073,18 @@ TEST (bulk, genesis_pruning)

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

node1->ledger_pruning (2, false);
ASSERT_EQ (2, node1->ledger.cache.pruned_count);
ASSERT_EQ (4, node1->ledger.cache.block_count);
ASSERT_EQ (2, node1->ledger.pruned_count ());
ASSERT_EQ (4, node1->ledger.block_count ());
ASSERT_TRUE (node1->ledger.store.pruned.exists (node1->ledger.store.tx_begin_read (), send1->hash ()));
ASSERT_FALSE (nano::test::exists (*node1, { send1 }));
ASSERT_TRUE (node1->ledger.store.pruned.exists (node1->ledger.store.tx_begin_read (), send2->hash ()));
Expand All @@ -2100,7 +2100,7 @@ TEST (bulk, genesis_pruning)
ASSERT_TIMELY (5s, !node2->bootstrap_initiator.in_progress ());

// node2 still missing blocks
ASSERT_EQ (1, node2->ledger.cache.block_count);
ASSERT_EQ (1, node2->ledger.block_count ());
{
auto transaction (node2->store.tx_begin_write ());
node2->unchecked.clear ();
Expand All @@ -2109,7 +2109,7 @@ TEST (bulk, genesis_pruning)
// Insert pruned blocks
node2->process_active (send1);
node2->process_active (send2);
ASSERT_TIMELY_EQ (5s, 3, node2->ledger.cache.block_count);
ASSERT_TIMELY_EQ (5s, 3, node2->ledger.block_count ());

// New bootstrap to sync up everything
ASSERT_TIMELY_EQ (5s, node2->bootstrap_initiator.connections->connections_count, 0);
Expand Down
10 changes: 5 additions & 5 deletions nano/core_test/confirming_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST (confirming_set, process_one)
std::unique_lock lock{ mutex };
ASSERT_TRUE (condition.wait_for (lock, 5s, [&] () { return count == 1; }));
ASSERT_EQ (1, ctx.stats ().count (nano::stat::type::confirmation_height, nano::stat::detail::blocks_confirmed, nano::stat::dir::in));
ASSERT_EQ (2, ctx.ledger ().cache.cemented_count);
ASSERT_EQ (2, ctx.ledger ().cemented_count ());
}

TEST (confirming_set, process_multiple)
Expand All @@ -64,7 +64,7 @@ TEST (confirming_set, process_multiple)
std::unique_lock lock{ mutex };
ASSERT_TRUE (condition.wait_for (lock, 5s, [&] () { return count == 2; }));
ASSERT_EQ (2, ctx.stats ().count (nano::stat::type::confirmation_height, nano::stat::detail::blocks_confirmed, nano::stat::dir::in));
ASSERT_EQ (3, ctx.ledger ().cache.cemented_count);
ASSERT_EQ (3, ctx.ledger ().cemented_count ());
}

TEST (confirmation_callback, observer_callbacks)
Expand Down Expand Up @@ -109,7 +109,7 @@ TEST (confirmation_callback, observer_callbacks)
ASSERT_TIMELY_EQ (5s, 2, node->ledger.stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::all, nano::stat::dir::out));

ASSERT_EQ (2, node->stats.count (nano::stat::type::confirmation_height, nano::stat::detail::blocks_confirmed, nano::stat::dir::in));
ASSERT_EQ (3, node->ledger.cache.cemented_count);
ASSERT_EQ (3, node->ledger.cemented_count ());
ASSERT_EQ (0, node->active.election_winner_details_size ());
}

Expand Down Expand Up @@ -188,7 +188,7 @@ TEST (confirmation_callback, confirmed_history)
ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_quorum, nano::stat::dir::out));
ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::inactive_conf_height, nano::stat::dir::out));
ASSERT_EQ (2, node->stats.count (nano::stat::type::confirmation_height, nano::stat::detail::blocks_confirmed, nano::stat::dir::in));
ASSERT_EQ (3, node->ledger.cache.cemented_count);
ASSERT_EQ (3, node->ledger.cemented_count ());
ASSERT_EQ (0, node->active.election_winner_details_size ());
}

Expand Down Expand Up @@ -251,7 +251,7 @@ TEST (confirmation_callback, dependent_election)
ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_quorum, nano::stat::dir::out));
ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::active_conf_height, nano::stat::dir::out));
ASSERT_EQ (1, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::inactive_conf_height, nano::stat::dir::out));
ASSERT_EQ (4, node->ledger.cache.cemented_count);
ASSERT_EQ (4, node->ledger.cemented_count ());

ASSERT_EQ (0, node->active.election_winner_details_size ());
}
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/conflicts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TEST (conflicts, add_two)
// create 2 new accounts, that receive 1 raw each, all blocks are force confirmed
auto [send1, open1] = nano::test::setup_new_account (system, *node, 1, gk, key1, gk.pub, true);
auto [send2, open2] = nano::test::setup_new_account (system, *node, 1, gk, key2, gk.pub, true);
ASSERT_EQ (5, node->ledger.cache.cemented_count);
ASSERT_EQ (5, node->ledger.cemented_count ());

// send 1 raw to account key3 from key1
auto send_a = nano::state_block_builder ()
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ TEST (election, quorum_minimum_update_weight_before_quorum_checks)
.work (*system.work.generate (open1->hash ()))
.build ();
ASSERT_EQ (nano::block_status::progress, node1.process (send2));
ASSERT_TIMELY_EQ (5s, node1.ledger.cache.block_count, 4);
ASSERT_TIMELY_EQ (5s, node1.ledger.block_count (), 4);

node_config.peering_port = system.get_available_port ();
auto & node2 = *system.add_node (node_config);

system.wallet (1)->insert_adhoc (key1.prv);
ASSERT_TIMELY_EQ (10s, node2.ledger.cache.block_count, 4);
ASSERT_TIMELY_EQ (10s, node2.ledger.block_count (), 4);

std::shared_ptr<nano::election> election;
ASSERT_TIMELY (5s, (election = node1.active.election (send1->qualified_root ())) != nullptr);
Expand Down
Loading

0 comments on commit 324be97

Please sign in to comment.