Skip to content

Commit

Permalink
Upgrade confirmation height table to include cemented frontier (nanoc…
Browse files Browse the repository at this point in the history
…urrency#2481)

* Upgrade confirmation height table to include cemented frontier

* Stein review comments

* Add //clang-format block around lambda

* Update comment (thanks Gui)

* Upgrade to v17 instead

* Update comments
  • Loading branch information
wezrule committed Jan 22, 2020
1 parent 02a83ec commit 28c3d15
Show file tree
Hide file tree
Showing 19 changed files with 573 additions and 300 deletions.
200 changes: 146 additions & 54 deletions nano/core_test/block_store.cpp

Large diffs are not rendered by default.

285 changes: 149 additions & 136 deletions nano/core_test/confirmation_height.cpp

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ TEST (ledger, genesis_balance)
ASSERT_GE (nano::seconds_since_epoch (), info.modified);
ASSERT_LT (nano::seconds_since_epoch () - info.modified, 10);
// Genesis block should be confirmed by default
uint64_t confirmation_height;
ASSERT_FALSE (store->confirmation_height_get (transaction, nano::genesis_account, confirmation_height));
ASSERT_EQ (confirmation_height, 1);
nano::confirmation_height_info confirmation_height_info;
ASSERT_FALSE (store->confirmation_height_get (transaction, nano::genesis_account, confirmation_height_info));
ASSERT_EQ (confirmation_height_info.height, 1);
ASSERT_EQ (confirmation_height_info.frontier, genesis.hash ());
}

// All nodes in the system should agree on the genesis balance
Expand Down Expand Up @@ -2848,16 +2849,19 @@ TEST (ledger, confirmation_height_not_updated)
ASSERT_FALSE (store->account_get (transaction, nano::test_genesis_key.pub, account_info));
nano::keypair key;
nano::send_block send1 (account_info.head, key.pub, 50, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *pool.generate (account_info.head));
uint64_t confirmation_height;
ASSERT_FALSE (store->confirmation_height_get (transaction, nano::genesis_account, confirmation_height));
ASSERT_EQ (1, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
ASSERT_FALSE (store->confirmation_height_get (transaction, nano::genesis_account, confirmation_height_info));
ASSERT_EQ (1, confirmation_height_info.height);
ASSERT_EQ (genesis.hash (), confirmation_height_info.frontier);
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, send1).code);
ASSERT_FALSE (store->confirmation_height_get (transaction, nano::genesis_account, confirmation_height));
ASSERT_EQ (1, confirmation_height);
ASSERT_FALSE (store->confirmation_height_get (transaction, nano::genesis_account, confirmation_height_info));
ASSERT_EQ (1, confirmation_height_info.height);
ASSERT_EQ (genesis.hash (), confirmation_height_info.frontier);
nano::open_block open1 (send1.hash (), nano::genesis_account, key.pub, key.prv, key.pub, *pool.generate (key.pub));
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, open1).code);
ASSERT_FALSE (store->confirmation_height_get (transaction, key.pub, confirmation_height));
ASSERT_EQ (0, confirmation_height);
ASSERT_FALSE (store->confirmation_height_get (transaction, key.pub, confirmation_height_info));
ASSERT_EQ (0, confirmation_height_info.height);
ASSERT_EQ (nano::block_hash (0), confirmation_height_info.frontier);
}

TEST (ledger, zero_rep)
Expand Down
18 changes: 9 additions & 9 deletions nano/core_test/versioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ TEST (versioning, account_info_v1)
ASSERT_EQ (v1.modified, v_latest.modified);
ASSERT_EQ (v1.rep_block, open.hash ());
ASSERT_EQ (1, v_latest.block_count);
uint64_t confirmation_height;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height));
ASSERT_EQ (0, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height_info));
ASSERT_EQ (0, confirmation_height_info.height);
ASSERT_EQ (nano::epoch::epoch_0, v_latest.epoch ());
}

Expand Down Expand Up @@ -72,9 +72,9 @@ TEST (versioning, account_info_v5)
ASSERT_EQ (v5.modified, v_latest.modified);
ASSERT_EQ (v5.rep_block, open.hash ());
ASSERT_EQ (1, v_latest.block_count);
uint64_t confirmation_height;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height));
ASSERT_EQ (0, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height_info));
ASSERT_EQ (0, confirmation_height_info.height);
ASSERT_EQ (nano::epoch::epoch_0, v_latest.epoch ());
}

Expand Down Expand Up @@ -108,8 +108,8 @@ TEST (versioning, account_info_v13)
ASSERT_EQ (v13.modified, v_latest.modified);
ASSERT_EQ (v13.rep_block, open.hash ());
ASSERT_EQ (v13.block_count, v_latest.block_count);
uint64_t confirmation_height;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height));
ASSERT_EQ (0, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
ASSERT_FALSE (store.confirmation_height_get (transaction, account, confirmation_height_info));
ASSERT_EQ (0, confirmation_height_info.height);
ASSERT_EQ (v13.epoch, v_latest.epoch ());
}
1 change: 1 addition & 0 deletions nano/core_test/wallets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TEST (wallets, upgrade)
auto status (mdb_put (mdb_store.env.tx (transaction_destination), info.epoch () == nano::epoch::epoch_0 ? mdb_store.accounts_v0 : mdb_store.accounts_v1, nano::mdb_val (nano::test_genesis_key.pub), nano::mdb_val (account_info_v13), 0));
(void)status;
assert (status == 0);
mdb_store.confirmation_height_del (transaction_destination, nano::genesis_account);
}
auto node1 (std::make_shared<nano::node> (system.io_ctx, path, system.alarm, node_config1, system.work));
ASSERT_EQ (1, node1->wallets.items.size ());
Expand Down
8 changes: 4 additions & 4 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,12 +976,12 @@ int main (int argc, char * const * argv)
}
nano::account_info const & info (i->second);
nano::account const & account (i->first);
uint64_t confirmation_height;
node.node->store.confirmation_height_get (transaction, account, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
node.node->store.confirmation_height_get (transaction, account, confirmation_height_info);

if (confirmation_height > info.block_count)
if (confirmation_height_info.height > info.block_count)
{
std::cerr << "Confirmation height " << confirmation_height << " greater than block count " << info.block_count << " for account: " << account.to_account () << std::endl;
std::cerr << "Confirmation height " << confirmation_height_info.height << " greater than block count " << info.block_count << " for account: " << account.to_account () << std::endl;
}

auto hash (info.open_block);
Expand Down
24 changes: 12 additions & 12 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ void nano::active_transactions::search_frontiers (nano::transaction const & tran
auto error = node.store.account_get (transaction_a, cementable_account.account, info);
if (!error)
{
uint64_t confirmation_height;
error = node.store.confirmation_height_get (transaction_a, cementable_account.account, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
error = node.store.confirmation_height_get (transaction_a, cementable_account.account, confirmation_height_info);
release_assert (!error);

if (info.block_count > confirmation_height && !this->node.pending_confirmation_height.is_processing_block (info.head))
if (info.block_count > confirmation_height_info.height && !this->node.pending_confirmation_height.is_processing_block (info.head))
{
auto block (this->node.store.block_get (transaction_a, info.head));
if (!this->start (block, true))
Expand Down Expand Up @@ -429,11 +429,11 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra

auto i (wallet->store.begin (wallet_transaction, next_wallet_frontier_account));
auto n (wallet->store.end ());
uint64_t confirmation_height = 0;
nano::confirmation_height_info confirmation_height_info;
for (; i != n; ++i)
{
auto const & account (i->first);
if (!node.store.account_get (transaction_a, account, info) && !node.store.confirmation_height_get (transaction_a, account, confirmation_height))
if (!node.store.account_get (transaction_a, account, info) && !node.store.confirmation_height_get (transaction_a, account, confirmation_height_info))
{
// If it exists in normal priority collection delete from there.
auto it = priority_cementable_frontiers.find (account);
Expand All @@ -444,7 +444,7 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra
priority_cementable_frontiers_size = priority_cementable_frontiers.size ();
}

prioritize_account_for_confirmation (priority_wallet_cementable_frontiers, priority_wallet_cementable_frontiers_size, account, info, confirmation_height);
prioritize_account_for_confirmation (priority_wallet_cementable_frontiers, priority_wallet_cementable_frontiers_size, account, info, confirmation_height_info.height);

if (wallet_account_timer.since_start () >= wallet_account_time_a)
{
Expand Down Expand Up @@ -475,16 +475,16 @@ void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::tra

auto i (node.store.latest_begin (transaction_a, next_frontier_account));
auto n (node.store.latest_end ());
uint64_t confirmation_height = 0;
nano::confirmation_height_info confirmation_height_info;
for (; i != n && !stopped; ++i)
{
auto const & account (i->first);
auto const & info (i->second);
if (priority_wallet_cementable_frontiers.find (account) == priority_wallet_cementable_frontiers.end ())
{
if (!node.store.confirmation_height_get (transaction_a, account, confirmation_height))
if (!node.store.confirmation_height_get (transaction_a, account, confirmation_height_info))
{
prioritize_account_for_confirmation (priority_cementable_frontiers, priority_cementable_frontiers_size, account, info, confirmation_height);
prioritize_account_for_confirmation (priority_cementable_frontiers, priority_cementable_frontiers_size, account, info, confirmation_height_info.height);
}
}
next_frontier_account = account.number () + 1;
Expand Down Expand Up @@ -653,9 +653,9 @@ void nano::active_transactions::update_difficulty (std::shared_ptr<nano::block>
auto hash (block_a->hash ());
auto existing_block (node.store.block_get (*opt_transaction_a, hash, &existing_sideband));
release_assert (existing_block != nullptr);
uint64_t confirmation_height;
release_assert (!node.store.confirmation_height_get (*opt_transaction_a, node.store.block_account (*opt_transaction_a, hash), confirmation_height));
bool confirmed = (confirmation_height >= existing_sideband.height);
nano::confirmation_height_info confirmation_height_info;
release_assert (!node.store.confirmation_height_get (*opt_transaction_a, node.store.block_account (*opt_transaction_a, hash), confirmation_height_info));
bool confirmed = (confirmation_height_info.height >= existing_sideband.height);
if (!confirmed && existing_block->block_work () != block_a->block_work ())
{
uint64_t existing_difficulty;
Expand Down
10 changes: 5 additions & 5 deletions nano/node/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,20 +483,20 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
nano::account account;
if (!account.decode_account (account_str))
{
uint64_t confirmation_height;
nano::confirmation_height_info confirmation_height_info;
auto transaction (node.node->store.tx_begin_read ());
if (!node.node->store.confirmation_height_get (transaction, account, confirmation_height))
if (!node.node->store.confirmation_height_get (transaction, account, confirmation_height_info))
{
auto transaction (node.node->store.tx_begin_write ());
auto conf_height_reset_num = 0;
if (account == node.node->network_params.ledger.genesis_account)
{
conf_height_reset_num = 1;
node.node->store.confirmation_height_put (transaction, account, confirmation_height);
node.node->store.confirmation_height_put (transaction, account, { confirmation_height_info.height, node.node->network_params.ledger.genesis_block });
}
else
{
node.node->store.confirmation_height_clear (transaction, account, confirmation_height);
node.node->store.confirmation_height_clear (transaction, account, confirmation_height_info.height);
}

std::cout << "Confirmation height of account " << account_str << " is set to " << conf_height_reset_num << std::endl;
Expand Down Expand Up @@ -1170,7 +1170,7 @@ void reset_confirmation_heights (nano::block_store & store)

// Then make sure the confirmation height of the genesis account open block is 1
nano::network_params network_params;
store.confirmation_height_put (transaction, network_params.ledger.genesis_account, 1);
store.confirmation_height_put (transaction, network_params.ledger.genesis_account, { 1, network_params.ledger.genesis_block });
}

bool is_using_rocksdb (boost::filesystem::path const & data_path, std::error_code & ec)
Expand Down
12 changes: 7 additions & 5 deletions nano/node/confirmation_height_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ void nano::confirmation_height_processor::add_confirmation_height (nano::block_h

auto block_height (ledger.store.block_account_height (read_transaction, current));
nano::account account (ledger.store.block_account (read_transaction, current));
uint64_t confirmation_height;
release_assert (!ledger.store.confirmation_height_get (read_transaction, account, confirmation_height));
nano::confirmation_height_info confirmation_height_info;
release_assert (!ledger.store.confirmation_height_get (read_transaction, account, confirmation_height_info));
auto confirmation_height = confirmation_height_info.height;
auto iterated_height = confirmation_height;
auto account_it = confirmed_iterated_pairs.find (account);
if (account_it != confirmed_iterated_pairs.cend ())
Expand Down Expand Up @@ -304,9 +305,10 @@ bool nano::confirmation_height_processor::write_pending (std::deque<conf_height_
while (!all_pending_a.empty ())
{
const auto & pending = all_pending_a.front ();
uint64_t confirmation_height;
auto error = ledger.store.confirmation_height_get (transaction, pending.account, confirmation_height);
nano::confirmation_height_info confirmation_height_info;
auto error = ledger.store.confirmation_height_get (transaction, pending.account, confirmation_height_info);
release_assert (!error);
auto confirmation_height = confirmation_height_info.height;
if (pending.height > confirmation_height)
{
#ifndef NDEBUG
Expand Down Expand Up @@ -339,7 +341,7 @@ bool nano::confirmation_height_processor::write_pending (std::deque<conf_height_
assert (pending.num_blocks_confirmed == pending.height - confirmation_height);
confirmation_height = pending.height;
ledger.cache.cemented_count += pending.num_blocks_confirmed;
ledger.store.confirmation_height_put (transaction, pending.account, confirmation_height);
ledger.store.confirmation_height_put (transaction, pending.account, { confirmation_height, pending.hash });
}
total_pending_write_block_count -= pending.num_blocks_confirmed;
++num_accounts_processed;
Expand Down
9 changes: 5 additions & 4 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ void nano::json_handler::account_info ()
const bool pending = request.get<bool> ("pending", false);
auto transaction (node.store.tx_begin_read ());
auto info (account_info_impl (transaction, account));
uint64_t confirmation_height;
if (node.store.confirmation_height_get (transaction, account, confirmation_height))
nano::confirmation_height_info confirmation_height_info;
if (node.store.confirmation_height_get (transaction, account, confirmation_height_info))
{
ec = nano::error_common::account_not_found;
}
Expand All @@ -550,7 +550,8 @@ void nano::json_handler::account_info ()
response_l.put ("modified_timestamp", std::to_string (info.modified));
response_l.put ("block_count", std::to_string (info.block_count));
response_l.put ("account_version", epoch_as_string (info.epoch ()));
response_l.put ("confirmation_height", std::to_string (confirmation_height));
response_l.put ("confirmation_height", std::to_string (confirmation_height_info.height));
response_l.put ("confirmation_height_frontier", confirmation_height_info.frontier.to_string ());
if (representative)
{
response_l.put ("representative", info.representative.to_account ());
Expand Down Expand Up @@ -1766,7 +1767,7 @@ void nano::json_handler::confirmation_height_currently_processing ()
auto hash = node.pending_confirmation_height.current ();
if (!hash.is_zero ())
{
response_l.put ("hash", node.pending_confirmation_height.current ().to_string ());
response_l.put ("hash", hash.to_string ());
}
else
{
Expand Down
Loading

0 comments on commit 28c3d15

Please sign in to comment.