Skip to content

Commit

Permalink
Moves out pruned methods from block store class (#3317)
Browse files Browse the repository at this point in the history
  • Loading branch information
thsfs authored Jun 2, 2021
1 parent db8b4ca commit cb4906c
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 150 deletions.
40 changes: 20 additions & 20 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,10 @@ TEST (block_store, pruned_random)
nano::ledger_cache ledger_cache;
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis, ledger_cache);
store->pruned_put (transaction, hash1);
store->pruned.put (transaction, hash1);
}
auto transaction (store->tx_begin_read ());
auto random_hash (store->pruned_random (transaction));
auto random_hash (store->pruned.random (transaction));
ASSERT_EQ (hash1, random_hash);
}

Expand Down Expand Up @@ -1212,54 +1212,54 @@ TEST (block_store, pruned_blocks)
auto transaction (store->tx_begin_write ());

// Confirm that the store is empty
ASSERT_FALSE (store->pruned_exists (transaction, hash1));
ASSERT_EQ (store->pruned_count (transaction), 0);
ASSERT_FALSE (store->pruned.exists (transaction, hash1));
ASSERT_EQ (store->pruned.count (transaction), 0);

// Add one
store->pruned_put (transaction, hash1);
ASSERT_TRUE (store->pruned_exists (transaction, hash1));
store->pruned.put (transaction, hash1);
ASSERT_TRUE (store->pruned.exists (transaction, hash1));
}

// Confirm that it can be found
ASSERT_EQ (store->pruned_count (store->tx_begin_read ()), 1);
ASSERT_EQ (store->pruned.count (store->tx_begin_read ()), 1);

// Add another one and check that it (and the existing one) can be found
nano::open_block block2 (1, 2, key1.pub, key1.prv, key1.pub, 0);
block2.sideband_set ({});
auto hash2 (block2.hash ());
{
auto transaction (store->tx_begin_write ());
store->pruned_put (transaction, hash2);
ASSERT_TRUE (store->pruned_exists (transaction, hash2)); // Check new pruned hash is here
store->pruned.put (transaction, hash2);
ASSERT_TRUE (store->pruned.exists (transaction, hash2)); // Check new pruned hash is here
ASSERT_FALSE (store->block_exists (transaction, hash2));
ASSERT_TRUE (store->pruned_exists (transaction, hash1)); // Check first pruned hash is still here
ASSERT_TRUE (store->pruned.exists (transaction, hash1)); // Check first pruned hash is still here
ASSERT_FALSE (store->block_exists (transaction, hash1));
}

ASSERT_EQ (store->pruned_count (store->tx_begin_read ()), 2);
ASSERT_EQ (store->pruned.count (store->tx_begin_read ()), 2);

// Delete the first one
{
auto transaction (store->tx_begin_write ());
store->pruned_del (transaction, hash2);
ASSERT_FALSE (store->pruned_exists (transaction, hash2)); // Confirm it no longer exists
store->pruned.del (transaction, hash2);
ASSERT_FALSE (store->pruned.exists (transaction, hash2)); // Confirm it no longer exists
ASSERT_FALSE (store->block_exists (transaction, hash2)); // true for block_exists
store->block_put (transaction, hash2, block2); // Add corresponding block
ASSERT_TRUE (store->block_exists (transaction, hash2));
ASSERT_TRUE (store->pruned_exists (transaction, hash1)); // Check first pruned hash is still here
ASSERT_TRUE (store->pruned.exists (transaction, hash1)); // Check first pruned hash is still here
ASSERT_FALSE (store->block_exists (transaction, hash1));
}

ASSERT_EQ (store->pruned_count (store->tx_begin_read ()), 1);
ASSERT_EQ (store->pruned.count (store->tx_begin_read ()), 1);

// Delete original one
{
auto transaction (store->tx_begin_write ());
store->pruned_del (transaction, hash1);
ASSERT_FALSE (store->pruned_exists (transaction, hash1));
store->pruned.del (transaction, hash1);
ASSERT_FALSE (store->pruned.exists (transaction, hash1));
}

ASSERT_EQ (store->pruned_count (store->tx_begin_read ()), 0);
ASSERT_EQ (store->pruned.count (store->tx_begin_read ()), 0);
}

TEST (mdb_block_store, upgrade_v14_v15)
Expand Down Expand Up @@ -1799,13 +1799,13 @@ TEST (mdb_block_store, upgrade_v19_v20)
auto transaction (store.tx_begin_write ());
store.initialize (transaction, genesis, ledger.cache);
// Delete pruned table
ASSERT_FALSE (mdb_drop (store.env.tx (transaction), store.pruned, 1));
ASSERT_FALSE (mdb_drop (store.env.tx (transaction), store.pruned_handle, 1));
store.version_put (transaction, 19);
}
// Upgrading should create the table
nano::mdb_store store (logger, path);
ASSERT_FALSE (store.init_error ());
ASSERT_NE (store.pruned, 0);
ASSERT_NE (store.pruned_handle, 0);

// Version should be correct
auto transaction (store.tx_begin_read ());
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ TEST (bootstrap_processor, push_diamond_pruning)
ASSERT_EQ (1, node1->ledger.pruning_action (transaction, open->hash (), 1));
ASSERT_TRUE (node1->store.block_exists (transaction, latest));
ASSERT_FALSE (node1->store.block_exists (transaction, send1->hash ()));
ASSERT_TRUE (node1->store.pruned_exists (transaction, send1->hash ()));
ASSERT_TRUE (node1->store.pruned.exists (transaction, send1->hash ()));
ASSERT_FALSE (node1->store.block_exists (transaction, open->hash ()));
ASSERT_TRUE (node1->store.pruned_exists (transaction, open->hash ()));
ASSERT_TRUE (node1->store.pruned.exists (transaction, open->hash ()));
ASSERT_TRUE (node1->store.block_exists (transaction, send2->hash ()));
ASSERT_TRUE (node1->store.block_exists (transaction, receive->hash ()));
ASSERT_EQ (2, node1->ledger.cache.pruned_count);
Expand Down
42 changes: 21 additions & 21 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,7 @@ TEST (ledger, cache)

{
auto transaction (store->tx_begin_write ());
ledger.store.pruned_put (transaction, open->hash ());
ledger.store.pruned.put (transaction, open->hash ());
++ledger.cache.pruned_count;
}
++pruned_count;
Expand Down Expand Up @@ -3474,7 +3474,7 @@ TEST (ledger, pruning_action)
ledger.pruning = false;
ASSERT_TRUE (ledger.block_or_pruned_exists (transaction, send1.hash ()));
ledger.pruning = true;
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
ASSERT_TRUE (store->block_exists (transaction, send2.hash ()));
// Receiving pruned block
Expand All @@ -3492,10 +3492,10 @@ TEST (ledger, pruning_action)
// Middle block pruning
ASSERT_TRUE (store->block_exists (transaction, send2.hash ()));
ASSERT_EQ (1, ledger.pruning_action (transaction, send2.hash (), 1));
ASSERT_TRUE (store->pruned_exists (transaction, send2.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send2.hash ()));
ASSERT_FALSE (store->block_exists (transaction, send2.hash ()));
ASSERT_EQ (store->account.count (transaction), ledger.cache.account_count);
ASSERT_EQ (store->pruned_count (transaction), ledger.cache.pruned_count);
ASSERT_EQ (store->pruned.count (transaction), ledger.cache.pruned_count);
ASSERT_EQ (store->block_count (transaction), ledger.cache.block_count - ledger.cache.pruned_count);
}

Expand Down Expand Up @@ -3523,16 +3523,16 @@ TEST (ledger, pruning_large_chain)
ASSERT_TRUE (store->block_exists (transaction, receive.hash ()));
last_hash = receive.hash ();
}
ASSERT_EQ (0, store->pruned_count (transaction));
ASSERT_EQ (0, store->pruned.count (transaction));
ASSERT_EQ (send_receive_pairs * 2 + 1, store->block_count (transaction));
// Pruning action
ASSERT_EQ (send_receive_pairs * 2, ledger.pruning_action (transaction, last_hash, 5));
ASSERT_TRUE (store->pruned_exists (transaction, last_hash));
ASSERT_TRUE (store->pruned.exists (transaction, last_hash));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
ASSERT_FALSE (store->block_exists (transaction, last_hash));
ASSERT_EQ (store->pruned_count (transaction), ledger.cache.pruned_count);
ASSERT_EQ (store->pruned.count (transaction), ledger.cache.pruned_count);
ASSERT_EQ (store->block_count (transaction), ledger.cache.block_count - ledger.cache.pruned_count);
ASSERT_EQ (send_receive_pairs * 2, store->pruned_count (transaction));
ASSERT_EQ (send_receive_pairs * 2, store->pruned.count (transaction));
ASSERT_EQ (1, store->block_count (transaction)); // Genesis
}

Expand All @@ -3559,9 +3559,9 @@ TEST (ledger, pruning_source_rollback)
// Pruning action
ASSERT_EQ (2, ledger.pruning_action (transaction, send1.hash (), 1));
ASSERT_FALSE (store->block_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
ASSERT_FALSE (store->block_exists (transaction, epoch1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, epoch1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, epoch1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
nano::pending_info info;
ASSERT_FALSE (store->pending.get (transaction, nano::pending_key (nano::genesis_account, send1.hash ()), info));
Expand Down Expand Up @@ -3615,9 +3615,9 @@ TEST (ledger, pruning_source_rollback_legacy)
// Pruning action
ASSERT_EQ (2, ledger.pruning_action (transaction, send2.hash (), 1));
ASSERT_FALSE (store->block_exists (transaction, send2.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, send2.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send2.hash ()));
ASSERT_FALSE (store->block_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
nano::pending_info info1;
ASSERT_FALSE (store->pending.get (transaction, nano::pending_key (nano::genesis_account, send1.hash ()), info1));
Expand Down Expand Up @@ -3686,7 +3686,7 @@ TEST (ledger, pruning_process_error)
// Pruning action for latest block (not valid action)
ASSERT_EQ (1, ledger.pruning_action (transaction, send1.hash (), 1));
ASSERT_FALSE (store->block_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
// Attempt to process pruned block again
ASSERT_EQ (nano::process_result::old, ledger.process (transaction, send1).code);
// Attept to process new block after pruned
Expand Down Expand Up @@ -3727,18 +3727,18 @@ TEST (ledger, pruning_legacy_blocks)
ASSERT_EQ (1, ledger.pruning_action (transaction, open1.hash (), 1));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
ASSERT_FALSE (store->block_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
ASSERT_FALSE (store->block_exists (transaction, receive1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, receive1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, receive1.hash ()));
ASSERT_FALSE (store->block_exists (transaction, change1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, change1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, change1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, send2.hash ()));
ASSERT_FALSE (store->block_exists (transaction, open1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, open1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, open1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, send3.hash ()));
ASSERT_EQ (4, ledger.cache.pruned_count);
ASSERT_EQ (7, ledger.cache.block_count);
ASSERT_EQ (store->pruned_count (transaction), ledger.cache.pruned_count);
ASSERT_EQ (store->pruned.count (transaction), ledger.cache.pruned_count);
ASSERT_EQ (store->block_count (transaction), ledger.cache.block_count - ledger.cache.pruned_count);
}

Expand All @@ -3764,7 +3764,7 @@ TEST (ledger, pruning_safe_functions)
ASSERT_EQ (1, ledger.pruning_action (transaction, send1.hash (), 1));
ASSERT_FALSE (store->block_exists (transaction, send1.hash ()));
ASSERT_TRUE (ledger.block_or_pruned_exists (transaction, send1.hash ())); // true for pruned
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
ASSERT_TRUE (store->block_exists (transaction, send2.hash ()));
// Safe ledger actions
Expand Down Expand Up @@ -3806,7 +3806,7 @@ TEST (ledger, hash_root_random)
// Pruning action
ASSERT_EQ (1, ledger.pruning_action (transaction, send1.hash (), 1));
ASSERT_FALSE (store->block_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned_exists (transaction, send1.hash ()));
ASSERT_TRUE (store->pruned.exists (transaction, send1.hash ()));
ASSERT_TRUE (store->block_exists (transaction, genesis.hash ()));
ASSERT_TRUE (store->block_exists (transaction, send2.hash ()));
// Test random block including pruned
Expand Down Expand Up @@ -3867,7 +3867,7 @@ TEST (ledger, migrate_lmdb_to_rocksdb)
store.peer_put (transaction, endpoint_key);

store.pending.put (transaction, nano::pending_key (nano::genesis_account, send->hash ()), nano::pending_info (nano::genesis_account, 100, nano::epoch::epoch_0));
store.pruned_put (transaction, send->hash ());
store.pruned.put (transaction, send->hash ());
store.unchecked_put (transaction, nano::genesis_hash, send);
store.version_put (transaction, version);
send->sideband_set ({});
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4742,7 +4742,7 @@ TEST (node, pruning_automatic)
ASSERT_TIMELY (2s, node1.active.empty () && node1.block_confirmed (send2->hash ()));
// Check pruning result
ASSERT_TIMELY (3s, node1.ledger.cache.pruned_count == 1);
ASSERT_TIMELY (2s, node1.store.pruned_count (node1.store.tx_begin_read ()) == 1); // Transaction commit
ASSERT_TIMELY (2s, node1.store.pruned.count (node1.store.tx_begin_read ()) == 1); // Transaction commit
ASSERT_EQ (1, node1.ledger.cache.pruned_count);
ASSERT_EQ (3, node1.ledger.cache.block_count);
ASSERT_TRUE (node1.ledger.block_or_pruned_exists (genesis.hash ()));
Expand Down
10 changes: 5 additions & 5 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ int main (int argc, char * const * argv)
else
{
pruned_block = true;
if (!node->store.pruned_exists (transaction, block->previous ()))
if (!node->store.pruned.exists (transaction, block->previous ()))
{
print_error_message (boost::str (boost::format ("Pruned previous block does not exist %1%\n") % block->previous ().to_string ()));
}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ int main (int argc, char * const * argv)
}
}
}
else if (!node->store.pruned_exists (transaction, block->previous ()))
else if (!node->store.pruned.exists (transaction, block->previous ()))
{
print_error_message (boost::str (boost::format ("Previous pruned block does not exist %1%\n") % block->previous ().to_string ()));
}
Expand All @@ -1564,7 +1564,7 @@ int main (int argc, char * const * argv)
print_error_message (boost::str (boost::format ("Incorrect sideband block details for block %1%\n") % hash.to_string ()));
}
// Check link epoch version
if (sideband.details.is_receive && (!node->ledger.pruning || !node->store.pruned_exists (transaction, block->link ().as_block_hash ())))
if (sideband.details.is_receive && (!node->ledger.pruning || !node->store.pruned.exists (transaction, block->link ().as_block_hash ())))
{
if (sideband.source_epoch != node->store.block_version (transaction, block->link ().as_block_hash ()))
{
Expand Down Expand Up @@ -1687,7 +1687,7 @@ int main (int argc, char * const * argv)
bool pruned (false);
if (block == nullptr)
{
pruned = node->ledger.pruning && node->store.pruned_exists (transaction, key.hash);
pruned = node->ledger.pruning && node->store.pruned.exists (transaction, key.hash);
if (!pruned)
{
print_error_message (boost::str (boost::format ("Pending block does not exist %1%\n") % key.hash.to_string ()));
Expand All @@ -1697,7 +1697,7 @@ int main (int argc, char * const * argv)
{
// Check if pending destination is correct
nano::account destination (0);
bool previous_pruned = node->ledger.pruning && node->store.pruned_exists (transaction, block->previous ());
bool previous_pruned = node->ledger.pruning && node->store.pruned.exists (transaction, block->previous ());
if (previous_pruned)
{
block = node->store.block_get (transaction, key.hash);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ nano::inactive_cache_status nano::active_transactions::inactive_votes_bootstrap_
lock_a.lock ();
insert_impl (lock_a, block);
}
else if (!block && status.bootstrap_started && !previously_a.bootstrap_started && (!node.ledger.pruning || !node.store.pruned_exists (transaction, hash_a)))
else if (!block && status.bootstrap_started && !previously_a.bootstrap_started && (!node.ledger.pruning || !node.store.pruned.exists (transaction, hash_a)))
{
node.gap_cache.bootstrap_start (hash_a);
}
Expand Down
2 changes: 1 addition & 1 deletion nano/node/confirmation_height_bounded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void nano::confirmation_height_bounded::process (std::shared_ptr<nano::block> or

if (!block)
{
if (ledger.pruning && ledger.store.pruned_exists (transaction, current))
if (ledger.pruning && ledger.store.pruned.exists (transaction, current))
{
if (!receive_source_pairs.empty ())
{
Expand Down
2 changes: 1 addition & 1 deletion nano/node/confirmation_height_unbounded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ void nano::confirmation_height_unbounded::cement_blocks (nano::write_guard & sco

if (!block)
{
if (ledger.pruning && ledger.store.pruned_exists (transaction, pending.hash))
if (ledger.pruning && ledger.store.pruned.exists (transaction, pending.hash))
{
pending_writes.erase (pending_writes.begin ());
--pending_writes_size;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ void nano::json_handler::pruned_exists ()
auto transaction (node.store.tx_begin_read ());
if (node.ledger.pruning)
{
auto exists (node.store.pruned_exists (transaction, hash));
auto exists (node.store.pruned.exists (transaction, hash));
response_l.put ("exists", exists ? "1" : "0");
}
else
Expand Down
8 changes: 4 additions & 4 deletions nano/node/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void nano::mdb_store::open_databases (bool & error_a, nano::transaction const &
error_a |= mdb_dbi_open (env.tx (transaction_a), "online_weight", flags, &online_weight_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "meta", flags, &meta) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "peers", flags, &peers) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "pruned", flags, &pruned) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "pruned", flags, &pruned_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "confirmation_height", flags, &confirmation_height_handle) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "accounts", flags, &accounts_v0) != 0;
accounts = accounts_v0;
Expand Down Expand Up @@ -744,7 +744,7 @@ void nano::mdb_store::upgrade_v18_to_v19 (nano::write_transaction const & transa
void nano::mdb_store::upgrade_v19_to_v20 (nano::write_transaction const & transaction_a)
{
logger.always_log ("Preparing v19 to v20 database upgrade...");
mdb_dbi_open (env.tx (transaction_a), "pruned", MDB_CREATE, &pruned);
mdb_dbi_open (env.tx (transaction_a), "pruned", MDB_CREATE, &pruned_handle);
version_put (transaction_a, 20);
logger.always_log ("Finished creating new pruned table");
}
Expand Down Expand Up @@ -874,7 +874,7 @@ MDB_dbi nano::mdb_store::table_to_dbi (tables table_a) const
case tables::peers:
return peers;
case tables::pruned:
return pruned;
return pruned_handle;
case tables::confirmation_height:
return confirmation_height_handle;
case tables::final_votes:
Expand Down Expand Up @@ -913,7 +913,7 @@ bool nano::mdb_store::copy_db (boost::filesystem::path const & destination_file)
void nano::mdb_store::rebuild_db (nano::write_transaction const & transaction_a)
{
// Tables with uint256_union key
std::vector<MDB_dbi> tables = { accounts, blocks, pruned, confirmation_height_handle };
std::vector<MDB_dbi> tables = { accounts, blocks, pruned_handle, confirmation_height_handle };
for (auto const & table : tables)
{
MDB_dbi temp;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/lmdb/lmdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class mdb_store : public block_store_partial<MDB_val, mdb_store>
* Pruned blocks hashes
* nano::block_hash -> none
*/
MDB_dbi pruned{ 0 };
MDB_dbi pruned_handle{ 0 };

/*
* Endpoints for peers
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, boost::filesystem::path co
}
}

ledger.pruning = flags.enable_pruning || store.pruned_count (store.tx_begin_read ()) > 0;
ledger.pruning = flags.enable_pruning || store.pruned.count (store.tx_begin_read ()) > 0;

if (ledger.pruning)
{
Expand Down
Loading

0 comments on commit cb4906c

Please sign in to comment.