Skip to content

Commit

Permalink
Add block count cache (#2290)
Browse files Browse the repository at this point in the history
* Add block count cache
Estimation used for comparision with bootstrap_weight_max_blocks
* Add block_count_cache to store initialization
  • Loading branch information
SergiySW authored Sep 9, 2019
1 parent 1913ce7 commit ce00d62
Show file tree
Hide file tree
Showing 26 changed files with 298 additions and 302 deletions.
58 changes: 34 additions & 24 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@ TEST (block_store, genesis)
auto hash (genesis.hash ());
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
std::atomic<uint64_t> block_count_cache{ 0 };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis, rep_weights, cemented_count);
store->initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
nano::account_info info;
ASSERT_FALSE (store->account_get (transaction, nano::genesis_account, info));
ASSERT_EQ (hash, info.head);
Expand Down Expand Up @@ -749,7 +750,8 @@ TEST (block_store, cemented_count_cache)
nano::genesis genesis;
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store->initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store->initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
ASSERT_EQ (1, cemented_count);
}

Expand Down Expand Up @@ -797,19 +799,19 @@ TEST (mdb_block_store, upgrade_v2_v3)
auto hash (genesis.hash ());
nano::stat stats;
nano::ledger ledger (store, stats);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
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 +827,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 Expand Up @@ -877,7 +879,7 @@ TEST (mdb_block_store, upgrade_v4_v5)
nano::genesis genesis;
nano::stat stats;
nano::ledger ledger (store, stats);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
store.version_put (transaction, 4);
nano::account_info info;
ASSERT_FALSE (store.account_get (transaction, nano::test_genesis_key.pub, info));
Expand Down Expand Up @@ -908,8 +910,9 @@ TEST (block_store, block_random)
{
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
std::atomic<uint64_t> block_count_cache{ 0 };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis, rep_weights, cemented_count);
store->initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
}
auto transaction (store->tx_begin_read ());
auto block (store->block_random (transaction));
Expand All @@ -928,7 +931,8 @@ TEST (mdb_block_store, upgrade_v5_v6)
nano::genesis genesis;
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
store.version_put (transaction, 5);
modify_genesis_account_info_to_v5 (store, transaction);
}
Expand All @@ -952,7 +956,8 @@ TEST (mdb_block_store, upgrade_v6_v7)
nano::genesis genesis;
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
store.version_put (transaction, 6);
modify_account_info_to_v13 (store, transaction, nano::genesis_account);
auto send1 (std::make_shared<nano::send_block> (0, 0, 0, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0));
Expand Down Expand Up @@ -1115,7 +1120,8 @@ TEST (block_store, state_block)
auto transaction (store->tx_begin_write ());
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store->initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store->initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
ASSERT_EQ (nano::block_type::state, block1.type ());
nano::block_sideband sideband1 (nano::block_type::state, 0, 0, 0, 0, 0);
store->block_put (transaction, block1.hash (), block1, sideband1);
Expand Down Expand Up @@ -1171,7 +1177,8 @@ TEST (mdb_block_store, upgrade_sideband_genesis)
store.version_put (transaction, 11);
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
modify_account_info_to_v13 (store, transaction, nano::genesis_account);
nano::block_sideband sideband;
auto genesis_block (store.block_get (transaction, genesis.hash (), &sideband));
Expand Down Expand Up @@ -1206,7 +1213,7 @@ TEST (mdb_block_store, upgrade_sideband_two_blocks)
nano::ledger ledger (store, stat);
auto transaction (store.tx_begin_write ());
store.version_put (transaction, 11);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::state_block block (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, nano::test_genesis_key.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
hash2 = block.hash ();
Expand Down Expand Up @@ -1244,7 +1251,7 @@ TEST (mdb_block_store, upgrade_sideband_two_accounts)
nano::ledger ledger (store, stat);
auto transaction (store.tx_begin_write ());
store.version_put (transaction, 11);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::state_block block1 (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, key.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
hash2 = block1.hash ();
Expand Down Expand Up @@ -1287,7 +1294,7 @@ TEST (mdb_block_store, insert_after_legacy)
nano::ledger ledger (store, stat);
auto transaction (store.tx_begin_write ());
store.version_put (transaction, 11);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
write_legacy_sideband (store, transaction, *genesis.open, 0, store.open_blocks);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::state_block block (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, nano::test_genesis_key.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
Expand All @@ -1304,7 +1311,7 @@ TEST (mdb_block_store, legacy_account_computed)
nano::ledger ledger (store, stats);
nano::genesis genesis;
auto transaction (store.tx_begin_write ());
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
store.version_put (transaction, 11);
write_legacy_sideband (store, transaction, *genesis.open, 0, store.open_blocks);
ASSERT_EQ (nano::genesis_account, ledger.account (transaction, genesis.hash ()));
Expand All @@ -1325,7 +1332,7 @@ TEST (mdb_block_store, upgrade_sideband_epoch)
nano::ledger ledger (store, stat);
auto transaction (store.tx_begin_write ());
store.version_put (transaction, 11);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
nano::state_block block1 (nano::test_genesis_key.pub, genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount, ledger.link (nano::epoch::epoch_1), nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
hash2 = block1.hash ();
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, block1).code);
Expand Down Expand Up @@ -1362,7 +1369,7 @@ TEST (mdb_block_store, sideband_height)
nano::stat stat;
nano::ledger ledger (store, stat);
auto transaction (store.tx_begin_write ());
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count);
store.initialize (transaction, genesis, ledger.rep_weights, ledger.cemented_count, ledger.block_count_cache);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::send_block send (genesis.hash (), nano::test_genesis_key.pub, nano::genesis_amount - nano::Gxrb_ratio, nano::test_genesis_key.prv, nano::test_genesis_key.pub, pool.generate (genesis.hash ()));
ASSERT_EQ (nano::process_result::progress, ledger.process (transaction, send).code);
Expand Down Expand Up @@ -1560,7 +1567,8 @@ TEST (mdb_block_store, upgrade_v13_v14)
auto transaction (store.tx_begin_write ());
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
nano::account_info account_info;
ASSERT_FALSE (store.account_get (transaction, nano::genesis_account, account_info));
uint64_t confirmation_height;
Expand Down Expand Up @@ -1613,7 +1621,8 @@ TEST (mdb_block_store, upgrade_v14_v15)
auto transaction (store.tx_begin_write ());
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
nano::account_info account_info;
ASSERT_FALSE (store.account_get (transaction, nano::genesis_account, account_info));
uint64_t confirmation_height;
Expand Down Expand Up @@ -1754,7 +1763,8 @@ TEST (block_store, upgrade_confirmation_height_many)
store.version_put (transaction, 13);
nano::rep_weights rep_weights;
std::atomic<uint64_t> cemented_count{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count);
std::atomic<uint64_t> block_count_cache{ 0 };
store.initialize (transaction, genesis, rep_weights, cemented_count, block_count_cache);
modify_account_info_to_v13 (store, transaction, nano::genesis_account);

// Add many accounts
Expand Down
Loading

0 comments on commit ce00d62

Please sign in to comment.