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

Add block count cache #2290

Merged
merged 7 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 6 additions & 6 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,14 @@ TEST (mdb_block_store, upgrade_v2_v3)
nano::change_block change (hash, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (hash));
change_hash = change.hash ();
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, change).code);
ASSERT_EQ (0, ledger.weight (transaction, nano::test_genesis_key.pub));
ASSERT_EQ (nano::genesis_amount, ledger.weight (transaction, key1.pub));
ASSERT_EQ (0, ledger.weight (nano::test_genesis_key.pub));
ASSERT_EQ (nano::genesis_amount, ledger.weight (key1.pub));
store.version_put (transaction, 2);
ledger.rep_weights.representation_put (key1.pub, 7);
ASSERT_EQ (7, ledger.weight (transaction, key1.pub));
ASSERT_EQ (7, ledger.weight (key1.pub));
ASSERT_EQ (2, store.version_get (transaction));
ledger.rep_weights.representation_put (key2.pub, 6);
ASSERT_EQ (6, ledger.weight (transaction, key2.pub));
ASSERT_EQ (6, ledger.weight (key2.pub));
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction, nano::test_genesis_key.pub, info));
auto rep_block = ledger.representative (transaction, ledger.latest (transaction, nano::test_genesis_key.pub));
Expand All @@ -825,8 +825,8 @@ TEST (mdb_block_store, upgrade_v2_v3)
auto transaction (store.tx_begin_write ());
ASSERT_TRUE (!store.init_error ());
ASSERT_LT (2, store.version_get (transaction));
ASSERT_EQ (nano::genesis_amount, ledger.weight (transaction, key1.pub));
ASSERT_EQ (0, ledger.weight (transaction, key2.pub));
ASSERT_EQ (nano::genesis_amount, ledger.weight (key1.pub));
ASSERT_EQ (0, ledger.weight (key2.pub));
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction, nano::test_genesis_key.pub, info));
ASSERT_EQ (change_hash, ledger.representative (transaction, ledger.latest (transaction, nano::test_genesis_key.pub)));
Expand Down
203 changes: 102 additions & 101 deletions nano/core_test/ledger.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ TEST (confirmation_height, conflict_rollback_cemented)
auto transaction1 (system.nodes[0]->store.tx_begin_read ());
auto transaction2 (system.nodes[1]->store.tx_begin_read ());
lock.lock ();
auto winner (*votes1->tally (transaction2).begin ());
auto winner (*votes1->tally ().begin ());
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (nano::genesis_amount - 100, winner.first);
ASSERT_TRUE (node1.store.block_exists (transaction1, publish1.block->hash ()));
Expand Down
14 changes: 7 additions & 7 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ TEST (node, auto_bootstrap)
while (node1->bootstrap_initiator.in_progress ())
{
ASSERT_NO_ERROR (system.poll ());
};
}
system.deadline_set (5s);
while (node1->stats.count (nano::stat::type::observer, nano::stat::detail::observer_confirmation_active_quorum, nano::stat::dir::out) + node1->stats.count (nano::stat::type::observer, nano::stat::detail::observer_confirmation_active_conf_height, nano::stat::dir::out) < 2)
{
Expand Down Expand Up @@ -1146,7 +1146,7 @@ TEST (node, fork_publish)
ASSERT_NE (election->last_votes.end (), existing1);
ASSERT_EQ (send1->hash (), existing1->second.hash);
auto transaction (node1.store.tx_begin_read ());
auto winner (*election->tally (transaction).begin ());
auto winner (*election->tally ().begin ());
ASSERT_EQ (*send1, *winner.second);
ASSERT_EQ (nano::genesis_amount - 100, winner.first);
}
Expand Down Expand Up @@ -1199,7 +1199,7 @@ TEST (node, fork_keep)
auto transaction1 (system.nodes[1]->store.tx_begin_read ());
// The vote should be in agreement with what we already have.
lock.lock ();
auto winner (*votes1->tally (transaction1).begin ());
auto winner (*votes1->tally ().begin ());
ASSERT_EQ (*send1, *winner.second);
ASSERT_EQ (nano::genesis_amount - 100, winner.first);
ASSERT_TRUE (system.nodes[0]->store.block_exists (transaction0, send1->hash ()));
Expand Down Expand Up @@ -1257,7 +1257,7 @@ TEST (node, fork_flip)
auto transaction1 (system.nodes[0]->store.tx_begin_read ());
auto transaction2 (system.nodes[1]->store.tx_begin_read ());
lock.lock ();
auto winner (*votes1->tally (transaction2).begin ());
auto winner (*votes1->tally ().begin ());
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (nano::genesis_amount - 100, winner.first);
ASSERT_TRUE (node1.store.block_exists (transaction1, publish1.block->hash ()));
Expand Down Expand Up @@ -1319,7 +1319,7 @@ TEST (node, fork_multi_flip)
auto transaction1 (system.nodes[0]->store.tx_begin_read ());
auto transaction2 (system.nodes[1]->store.tx_begin_read ());
lock.lock ();
auto winner (*votes1->tally (transaction2).begin ());
auto winner (*votes1->tally ().begin ());
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (nano::genesis_amount - 100, winner.first);
ASSERT_TRUE (node1.store.block_exists (transaction1, publish1.block->hash ()));
Expand Down Expand Up @@ -1445,7 +1445,7 @@ TEST (node, fork_open_flip)
auto transaction1 (system.nodes[0]->store.tx_begin_read ());
auto transaction2 (system.nodes[1]->store.tx_begin_read ());
lock.lock ();
auto winner (*votes1->tally (transaction2).begin ());
auto winner (*votes1->tally ().begin ());
ASSERT_EQ (*open1, *winner.second);
ASSERT_EQ (nano::genesis_amount - 1, winner.first);
ASSERT_TRUE (node1.store.block_exists (transaction1, open1->hash ()));
Expand Down Expand Up @@ -2475,7 +2475,7 @@ TEST (node, vote_by_hash_bundle)
system.wallet (0)->insert_adhoc (key1.prv);

std::atomic<size_t> max_hashes{ 0 };
system.nodes[0]->observers.vote.add ([&max_hashes](nano::transaction const & transaction, std::shared_ptr<nano::vote> vote_a, std::shared_ptr<nano::transport::channel> channel_a) {
system.nodes[0]->observers.vote.add ([&max_hashes](std::shared_ptr<nano::vote> vote_a, std::shared_ptr<nano::transport::channel> channel_a) {
if (vote_a->blocks.size () > max_hashes)
{
max_hashes = vote_a->blocks.size ();
Expand Down
13 changes: 5 additions & 8 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
/* Confirm frontiers when there aren't many confirmations already pending and node finished initial bootstrap
In auto mode start confirm only if node contains almost principal representative (half of required for principal weight) */
lock_a.unlock ();
if (node.config.frontiers_confirmation != nano::frontiers_confirmation_mode::disabled && node.pending_confirmation_height.size () < confirmed_frontiers_max_pending_cut_off && node.store.block_count (transaction).sum () >= node.ledger.bootstrap_weight_max_blocks)
if (node.config.frontiers_confirmation != nano::frontiers_confirmation_mode::disabled && node.pending_confirmation_height.size () < confirmed_frontiers_max_pending_cut_off && node.ledger.block_count_cache >= node.ledger.bootstrap_weight_max_blocks)
{
confirm_frontiers (transaction);
}
Expand All @@ -147,7 +147,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
// Log votes for very long unconfirmed elections
if (election_l->confirmation_request_count % 50 == 1)
{
auto tally_l (election_l->tally (transaction));
auto tally_l (election_l->tally ());
election_l->log_votes (tally_l);
}
/* Escalation for long unconfirmed elections
Expand Down Expand Up @@ -931,10 +931,8 @@ size_t nano::active_transactions::inactive_votes_cache_size ()

void nano::active_transactions::add_inactive_votes_cache (nano::block_hash const & hash_a, nano::account const & representative_a)
{
/* Check principal representative status
nano::ledger::weight (...) call is too expensive due to read transaction requirements
Replace with nano::ledger::weight (...) after proper changes */
if (node.ledger.rep_weights.representation_get (representative_a) > node.minimum_principal_weight ())
// Check principal representative status
if (node.ledger.weight (representative_a) > node.minimum_principal_weight ())
{
auto existing (inactive_votes_cache.get<1> ().find (hash_a));
if (existing != inactive_votes_cache.get<1> ().end ())
Expand All @@ -952,8 +950,7 @@ void nano::active_transactions::add_inactive_votes_cache (nano::block_hash const

if (is_new)
{
auto transaction (node.store.tx_begin_read ());
node.gap_cache.bootstrap_check (transaction, existing->voters, hash_a);
node.gap_cache.bootstrap_check (existing->voters, hash_a);
}
}
else
Expand Down
5 changes: 1 addition & 4 deletions nano/node/bootstrap/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ void nano::bootstrap_attempt::run ()
lazy_run ();
lock.lock ();
}
if (!node->flags.disable_unchecked_cleanup)
{
node->unchecked_cleanup ();
}
node->unchecked_cleanup ();
}
stopped = true;
condition.notify_all ();
Expand Down
20 changes: 9 additions & 11 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void nano::election::compute_rep_votes (nano::transaction const & transaction_a)
{
if (node.config.enable_voting)
{
node.wallets.foreach_representative (transaction_a, [this, &transaction_a](nano::public_key const & pub_a, nano::raw_key const & prv_a) {
node.wallets.foreach_representative ([this, &transaction_a](nano::public_key const & pub_a, nano::raw_key const & prv_a) {
auto vote (this->node.store.vote_generate (transaction_a, pub_a, prv_a, status.winner));
this->node.vote_processor.vote (vote, std::make_shared<nano::transport::channel_udp> (this->node.network.udp_channels, this->node.network.endpoint (), this->node.network_params.protocol.protocol_version));
});
Expand Down Expand Up @@ -83,12 +83,12 @@ bool nano::election::have_quorum (nano::tally_t const & tally_a, nano::uint128_t
return result;
}

nano::tally_t nano::election::tally (nano::transaction const & transaction_a)
nano::tally_t nano::election::tally ()
{
std::unordered_map<nano::block_hash, nano::uint128_t> block_weights;
for (auto vote_info : last_votes)
{
block_weights[vote_info.second.hash] += node.ledger.weight (transaction_a, vote_info.first);
block_weights[vote_info.second.hash] += node.ledger.weight (vote_info.first);
}
last_tally = block_weights;
nano::tally_t result;
Expand All @@ -103,9 +103,9 @@ nano::tally_t nano::election::tally (nano::transaction const & transaction_a)
return result;
}

void nano::election::confirm_if_quorum (nano::transaction const & transaction_a)
void nano::election::confirm_if_quorum ()
{
auto tally_l (tally (transaction_a));
auto tally_l (tally ());
assert (!tally_l.empty ());
auto winner (tally_l.begin ());
auto block_l (winner->second);
Expand Down Expand Up @@ -152,10 +152,9 @@ void nano::election::log_votes (nano::tally_t const & tally_a) const
nano::election_vote_result nano::election::vote (nano::account rep, uint64_t sequence, nano::block_hash block_hash)
{
// see republish_vote documentation for an explanation of these rules
auto transaction (node.store.tx_begin_read ());
auto replay (false);
auto online_stake (node.online_reps.online_stake ());
auto weight (node.ledger.weight (transaction, rep));
auto weight (node.ledger.weight (rep));
auto should_process (false);
if (node.network_params.network.is_test_network () || weight > node.minimum_principal_weight (online_stake))
{
Expand Down Expand Up @@ -198,7 +197,7 @@ nano::election_vote_result nano::election::vote (nano::account rep, uint64_t seq
last_votes[rep] = { std::chrono::steady_clock::now (), sequence, block_hash };
if (!confirmed)
{
confirm_if_quorum (transaction);
confirm_if_quorum ();
}
}
}
Expand All @@ -224,7 +223,7 @@ bool nano::election::publish (std::shared_ptr<nano::block> block_a)
if (blocks.find (block_a->hash ()) == blocks.end ())
{
blocks.insert (std::make_pair (block_a->hash (), block_a));
confirm_if_quorum (transaction);
confirm_if_quorum ();
node.network.flood_block (block_a, false);
}
else
Expand Down Expand Up @@ -321,7 +320,6 @@ void nano::election::insert_inactive_votes_cache ()
node.stats.inc (nano::stat::type::election, nano::stat::detail::late_block);
node.stats.add (nano::stat::type::election, nano::stat::detail::late_block_seconds, nano::stat::dir::in, delay.count (), true);
}
auto transaction (node.store.tx_begin_read ());
confirm_if_quorum (transaction);
confirm_if_quorum ();
}
}
4 changes: 2 additions & 2 deletions nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class election final : public std::enable_shared_from_this<nano::election>
public:
election (nano::node &, std::shared_ptr<nano::block>, std::function<void(std::shared_ptr<nano::block>)> const &);
nano::election_vote_result vote (nano::account, uint64_t, nano::block_hash);
nano::tally_t tally (nano::transaction const &);
nano::tally_t tally ();
// Check if we have vote quorum
bool have_quorum (nano::tally_t const &, nano::uint128_t) const;
// Change our winner to agree with the network
void compute_rep_votes (nano::transaction const &);
void confirm_once (nano::election_status_type = nano::election_status_type::active_confirmed_quorum);
// Confirm this block if quorum is met
void confirm_if_quorum (nano::transaction const &);
void confirm_if_quorum ();
void log_votes (nano::tally_t const &) const;
bool publish (std::shared_ptr<nano::block> block_a);
size_t last_votes_size ();
Expand Down
11 changes: 5 additions & 6 deletions nano/node/gap_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void nano::gap_cache::erase (nano::block_hash const & hash_a)
void nano::gap_cache::vote (std::shared_ptr<nano::vote> vote_a)
{
nano::lock_guard<std::mutex> lock (mutex);
auto transaction (node.store.tx_begin_read ());
for (auto hash : *vote_a)
{
auto existing (blocks.get<1> ().find (hash));
Expand All @@ -54,18 +53,18 @@ void nano::gap_cache::vote (std::shared_ptr<nano::vote> vote_a)

if (is_new)
{
bootstrap_check (transaction, existing->voters, hash);
bootstrap_check (existing->voters, hash);
}
}
}
}

void nano::gap_cache::bootstrap_check (nano::transaction const & transaction_a, std::vector<nano::account> const & voters_a, nano::block_hash const & hash_a)
void nano::gap_cache::bootstrap_check (std::vector<nano::account> const & voters_a, nano::block_hash const & hash_a)
{
uint128_t tally;
for (auto & voter : voters_a)
{
tally += node.ledger.weight (transaction_a, voter);
tally += node.ledger.weight (voter);
}
bool start_bootstrap (false);
if (!node.flags.disable_lazy_bootstrap)
Expand All @@ -75,7 +74,7 @@ void nano::gap_cache::bootstrap_check (nano::transaction const & transaction_a,
start_bootstrap = true;
}
}
else if (!node.flags.disable_legacy_bootstrap && tally > bootstrap_threshold (transaction_a))
else if (!node.flags.disable_legacy_bootstrap && tally > bootstrap_threshold ())
{
start_bootstrap = true;
}
Expand Down Expand Up @@ -104,7 +103,7 @@ void nano::gap_cache::bootstrap_check (nano::transaction const & transaction_a,
}
}

nano::uint128_t nano::gap_cache::bootstrap_threshold (nano::transaction const & transaction_a)
nano::uint128_t nano::gap_cache::bootstrap_threshold ()
{
auto result ((node.online_reps.online_stake () / 256) * node.config.bootstrap_fraction_numerator);
return result;
Expand Down
4 changes: 2 additions & 2 deletions nano/node/gap_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class gap_cache final
void add (nano::block_hash const &, std::chrono::steady_clock::time_point = std::chrono::steady_clock::now ());
void erase (nano::block_hash const & hash_a);
void vote (std::shared_ptr<nano::vote>);
void bootstrap_check (nano::transaction const &, std::vector<nano::account> const &, nano::block_hash const &);
nano::uint128_t bootstrap_threshold (nano::transaction const &);
void bootstrap_check (std::vector<nano::account> const &, nano::block_hash const &);
nano::uint128_t bootstrap_threshold ();
size_t size ();
boost::multi_index_container<
nano::gap_information,
Expand Down
14 changes: 6 additions & 8 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ void nano::json_handler::account_info ()
}
if (weight)
{
auto account_weight (node.ledger.weight (transaction, account));
auto account_weight (node.ledger.weight (account));
response_l.put ("weight", account_weight.convert_to<std::string> ());
}
if (pending)
Expand Down Expand Up @@ -1812,8 +1812,7 @@ void nano::json_handler::confirmation_info ()
auto election (conflict_info->election);
nano::uint128_t total (0);
response_l.put ("last_winner", election->status.winner->hash ().to_string ());
auto transaction (node.store.tx_begin_read ());
auto tally_l (election->tally (transaction));
auto tally_l (election->tally ());
boost::property_tree::ptree blocks;
for (auto i (tally_l.begin ()), n (tally_l.end ()); i != n; ++i)
{
Expand Down Expand Up @@ -2433,7 +2432,7 @@ void nano::json_handler::ledger ()
}
if (weight)
{
auto account_weight (node.ledger.weight (transaction, account));
auto account_weight (node.ledger.weight (account));
response_a.put ("weight", account_weight.convert_to<std::string> ());
}
accounts.push_back (std::make_pair (account.to_account (), response_a));
Expand Down Expand Up @@ -2485,7 +2484,7 @@ void nano::json_handler::ledger ()
}
if (weight)
{
auto account_weight (node.ledger.weight (transaction, account));
auto account_weight (node.ledger.weight (account));
response_a.put ("weight", account_weight.convert_to<std::string> ());
}
accounts.push_back (std::make_pair (account.to_account (), response_a));
Expand Down Expand Up @@ -3229,7 +3228,6 @@ void nano::json_handler::representatives_online ()
if (!ec)
{
boost::property_tree::ptree representatives;
auto transaction (node.store.tx_begin_read ());
auto reps (node.online_reps.list ());
for (auto & i : reps)
{
Expand All @@ -3252,7 +3250,7 @@ void nano::json_handler::representatives_online ()
if (weight)
{
boost::property_tree::ptree weight_node;
auto account_weight (node.ledger.weight (transaction, i));
auto account_weight (node.ledger.weight (i));
weight_node.put ("weight", account_weight.convert_to<std::string> ());
representatives.add_child (i.to_account (), weight_node);
}
Expand Down Expand Up @@ -4236,7 +4234,7 @@ void nano::json_handler::wallet_ledger ()
}
if (weight)
{
auto account_weight (node.ledger.weight (block_transaction, account));
auto account_weight (node.ledger.weight (account));
entry.put ("weight", account_weight.convert_to<std::string> ());
}
if (pending)
Expand Down
4 changes: 2 additions & 2 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ bool confirm_block (nano::transaction const & transaction_a, nano::node & node_a
if (votes.empty ())
{
// Generate new vote
node_a.wallets.foreach_representative (transaction_a, [&result, &list_a, &node_a, &transaction_a, &hash](nano::public_key const & pub_a, nano::raw_key const & prv_a) {
node_a.wallets.foreach_representative ([&result, &list_a, &node_a, &transaction_a, &hash](nano::public_key const & pub_a, nano::raw_key const & prv_a) {
result = true;
auto vote (node_a.store.vote_generate (transaction_a, pub_a, prv_a, std::vector<nano::block_hash> (1, hash)));
nano::confirm_ack confirm (vote);
Expand Down Expand Up @@ -186,7 +186,7 @@ void nano::network::confirm_hashes (nano::transaction const & transaction_a, std
{
if (node.config.enable_voting)
{
node.wallets.foreach_representative (transaction_a, [this, &blocks_bundle_a, &channel_a, &transaction_a](nano::public_key const & pub_a, nano::raw_key const & prv_a) {
node.wallets.foreach_representative ([this, &blocks_bundle_a, &channel_a, &transaction_a](nano::public_key const & pub_a, nano::raw_key const & prv_a) {
auto vote (this->node.store.vote_generate (transaction_a, pub_a, prv_a, blocks_bundle_a));
nano::confirm_ack confirm (vote);
std::shared_ptr<std::vector<uint8_t>> bytes (new std::vector<uint8_t>);
Expand Down
Loading