Skip to content

Commit

Permalink
Fix pruning tests that did not confirm blocks before pruning. Strongl…
Browse files Browse the repository at this point in the history
…y ensure blocks are confirmed while pruning.
  • Loading branch information
clemahieu committed Apr 4, 2024
1 parent 96d7e41 commit b874d8d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 47 deletions.
1 change: 1 addition & 0 deletions nano/core_test/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ TEST (bootstrap_processor, push_diamond_pruning)

{
auto transaction (node1->store.tx_begin_write ());
node1->ledger.confirm (transaction, open->hash ());
ASSERT_EQ (1, node1->ledger.pruning_action (transaction, send1->hash (), 2));
ASSERT_EQ (1, node1->ledger.pruning_action (transaction, open->hash (), 1));
ASSERT_TRUE (node1->ledger.any.exists (transaction, nano::dev::genesis->hash ()));
Expand Down
55 changes: 8 additions & 47 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4911,6 +4911,7 @@ TEST (ledger, pruning_action)
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send2));
ASSERT_TRUE (store->block.exists (transaction, send2->hash ()));
// Pruning action
ledger.confirm (transaction, send1->hash ());
ASSERT_EQ (1, ledger.pruning_action (transaction, send1->hash (), 1));
ASSERT_EQ (0, ledger.pruning_action (transaction, nano::dev::genesis->hash (), 1));
ASSERT_TRUE (ledger.any.get (transaction, nano::pending_key{ nano::dev::genesis_key.pub, send1->hash () }));
Expand Down Expand Up @@ -4946,6 +4947,7 @@ TEST (ledger, pruning_action)
ASSERT_FALSE (receive1_stored->sideband ().details.is_epoch);
// Middle block pruning
ASSERT_TRUE (store->block.exists (transaction, send2->hash ()));
ledger.confirm (transaction, send2->hash ());
ASSERT_EQ (1, ledger.pruning_action (transaction, send2->hash (), 1));
ASSERT_TRUE (store->pruned.exists (transaction, send2->hash ()));
ASSERT_FALSE (store->block.exists (transaction, send2->hash ()));
Expand Down Expand Up @@ -4998,6 +5000,7 @@ TEST (ledger, pruning_large_chain)
}
ASSERT_EQ (0, store->pruned.count (transaction));
ASSERT_EQ (send_receive_pairs * 2 + 1, store->block.count (transaction));
ledger.confirm (transaction, last_hash);
// Pruning action
ASSERT_EQ (send_receive_pairs * 2, ledger.pruning_action (transaction, last_hash, 5));
ASSERT_TRUE (store->pruned.exists (transaction, last_hash));
Expand Down Expand Up @@ -5056,6 +5059,7 @@ TEST (ledger, pruning_source_rollback)
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send2));
ASSERT_TRUE (store->block.exists (transaction, send2->hash ()));
ledger.confirm (transaction, send1->hash ());
// Pruning action
ASSERT_EQ (2, ledger.pruning_action (transaction, send1->hash (), 1));
ASSERT_FALSE (store->block.exists (transaction, send1->hash ()));
Expand Down Expand Up @@ -5142,6 +5146,7 @@ TEST (ledger, pruning_source_rollback_legacy)
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send3));
ASSERT_TRUE (store->block.exists (transaction, send3->hash ()));
ASSERT_TRUE (ledger.any.get (transaction, nano::pending_key{ nano::dev::genesis_key.pub, send3->hash () }));
ledger.confirm (transaction, send2->hash ());
// Pruning action
ASSERT_EQ (2, ledger.pruning_action (transaction, send2->hash (), 1));
ASSERT_FALSE (store->block.exists (transaction, send2->hash ()));
Expand Down Expand Up @@ -5210,53 +5215,6 @@ TEST (ledger, pruning_source_rollback_legacy)
ASSERT_EQ (6, ledger.block_count ());
}

TEST (ledger, pruning_process_error)
{
nano::logger logger;
auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants);
ASSERT_TRUE (!store->init_error ());
nano::stats stats;
nano::ledger ledger (*store, stats, nano::dev::constants);
ledger.pruning = true;
auto transaction (store->tx_begin_write ());
store->initialize (transaction, ledger.cache, ledger.constants);
nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits<unsigned>::max () };
nano::block_builder builder;
auto send1 = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (nano::dev::genesis->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount - nano::Gxrb_ratio)
.link (nano::dev::genesis_key.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*pool.generate (nano::dev::genesis->hash ()))
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send1));
ASSERT_EQ (0, ledger.pruned_count ());
ASSERT_EQ (2, ledger.block_count ());
// 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 ()));
// Attempt to process pruned block again
ASSERT_EQ (nano::block_status::old, ledger.process (transaction, send1));
// Attept to process new block after pruned
auto send2 = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (send1->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount - nano::Gxrb_ratio * 2)
.link (nano::dev::genesis_key.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*pool.generate (send1->hash ()))
.build ();
ASSERT_EQ (nano::block_status::gap_previous, ledger.process (transaction, send2));
ASSERT_EQ (1, ledger.pruned_count ());
ASSERT_EQ (2, ledger.block_count ());
}

TEST (ledger, pruning_legacy_blocks)
{
nano::logger logger;
Expand Down Expand Up @@ -5323,6 +5281,7 @@ TEST (ledger, pruning_legacy_blocks)
.work (*pool.generate (open1->hash ()))
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send3));
ledger.confirm (transaction, open1->hash ());
// Pruning action
ASSERT_EQ (3, ledger.pruning_action (transaction, change1->hash (), 2));
ASSERT_EQ (1, ledger.pruning_action (transaction, open1->hash (), 1));
Expand Down Expand Up @@ -5379,6 +5338,7 @@ TEST (ledger, pruning_safe_functions)
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send2));
ASSERT_TRUE (store->block.exists (transaction, send2->hash ()));
ledger.confirm (transaction, send1->hash ());
// Pruning action
ASSERT_EQ (1, ledger.pruning_action (transaction, send1->hash (), 1));
ASSERT_FALSE (store->block.exists (transaction, send1->hash ()));
Expand Down Expand Up @@ -5430,6 +5390,7 @@ TEST (ledger, hash_root_random)
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send2));
ASSERT_TRUE (store->block.exists (transaction, send2->hash ()));
ledger.confirm (transaction, send1->hash ());
// Pruning action
ASSERT_EQ (1, ledger.pruning_action (transaction, send1->hash (), 1));
ASSERT_FALSE (store->block.exists (transaction, send1->hash ()));
Expand Down
1 change: 1 addition & 0 deletions nano/core_test/ledger_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ TEST (ledger_confirm, pruned_source)
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send2));
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send3));
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, open2));
ledger.confirm (transaction, send2->hash ());
ASSERT_EQ (2, ledger.pruning_action (transaction, send2->hash (), 2));
ASSERT_FALSE (ledger.any.exists (transaction, send2->hash ()));
ASSERT_FALSE (ledger.confirmed.exists (transaction, open2->hash ()));
Expand Down
4 changes: 4 additions & 0 deletions nano/qt_test/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ TEST (history, pruned_source)
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, receive));
auto open = std::make_shared<nano::open_block> (send2->hash (), key.pub, key.pub, key.prv, key.pub, *system.work.generate (key.pub));
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, open));
ledger.confirm (transaction, send1->hash ());
ASSERT_EQ (1, ledger.pruning_action (transaction, send1->hash (), 2));
next_pruning = send2->hash ();
}
Expand All @@ -594,6 +595,7 @@ TEST (history, pruned_source)
// Additional legacy test
{
auto transaction (store->tx_begin_write ());
ledger.confirm (transaction, next_pruning);
ASSERT_EQ (1, ledger.pruning_action (transaction, next_pruning, 2));
}
history1.refresh ();
Expand All @@ -609,7 +611,9 @@ TEST (history, pruned_source)
auto latest_key (ledger.any.head (transaction, key.pub));
auto receive = std::make_shared<nano::state_block> (key.pub, latest_key, key.pub, 200, send->hash (), key.prv, key.pub, *system.work.generate (latest_key));
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, receive));
ledger.confirm (transaction, latest);
ASSERT_EQ (1, ledger.pruning_action (transaction, latest, 2));
ledger.confirm (transaction, latest_key);
ASSERT_EQ (1, ledger.pruning_action (transaction, latest_key, 2));
}
history1.refresh ();
Expand Down
1 change: 1 addition & 0 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ uint64_t nano::ledger::pruning_action (store::write_transaction & transaction_a,
auto block_l = any.get (transaction_a, hash);
if (block_l != nullptr)
{
release_assert (confirmed.exists_or_pruned (transaction_a, hash));
store.block.del (transaction_a, hash);
store.pruned.put (transaction_a, hash);
hash = block_l->previous ();
Expand Down

0 comments on commit b874d8d

Please sign in to comment.