Skip to content

Commit

Permalink
Modify direct access to store.block.successor to use ledger::successor
Browse files Browse the repository at this point in the history
Adds ledger::successor convenience functions taking nano::block_hash and expands them to nano::qualified_root so both block successor and account open blocks are searched.
  • Loading branch information
clemahieu committed Mar 13, 2024
1 parent 6bf9f90 commit 73384d8
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ TEST (ledger, process_receive)
ASSERT_EQ (nano::dev::constants.genesis_amount - 25, ledger.account_balance (transaction, key2.pub));
ASSERT_EQ (nano::dev::constants.genesis_amount - 25, ledger.weight (key3.pub));
ASSERT_FALSE (ledger.rollback (transaction, hash4));
ASSERT_TRUE (store.block.successor (transaction, hash2).is_zero ());
ASSERT_FALSE (ledger.successor (transaction, hash2));
ASSERT_EQ (key2.pub, store.frontier.get (transaction, hash2));
ASSERT_TRUE (store.frontier.get (transaction, hash4).is_zero ());
ASSERT_EQ (25, ledger.account_balance (transaction, nano::dev::genesis_key.pub));
Expand Down Expand Up @@ -3156,7 +3156,7 @@ TEST (ledger, state_rollback_send)
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.account_balance (transaction, nano::dev::genesis_key.pub));
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis_key.pub));
ASSERT_FALSE (store.pending.exists (transaction, nano::pending_key (nano::dev::genesis_key.pub, send1->hash ())));
ASSERT_TRUE (store.block.successor (transaction, nano::dev::genesis->hash ()).is_zero ());
ASSERT_FALSE (ledger.successor (transaction, nano::dev::genesis->hash ()));
ASSERT_EQ (store.account.count (transaction), ledger.cache.account_count);
}

Expand Down
2 changes: 1 addition & 1 deletion nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ int main (int argc, char * const * argv)
calculated_representative = block->representative_field ().value ();
}
// Retrieving successor block hash
hash = node->store.block.successor (transaction, hash);
hash = node->ledger.successor (transaction, hash).value_or (0);
// Retrieving block data
if (!hash.is_zero ())
{
Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap/bootstrap_bulk_pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void nano::bulk_pull_server::set_current_end ()
{
node->logger.debug (nano::log::type::bulk_pull_server, "Bulk pull request for block hash: {}", request->start.to_string ());

current = ascending () ? node->store.block.successor (transaction, request->start.as_block_hash ()) : request->start.as_block_hash ();
current = ascending () ? node->ledger.successor (transaction, request->start.as_block_hash ()).value_or (0) : request->start.as_block_hash ();
include_start = true;
}
else
Expand Down
6 changes: 3 additions & 3 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,7 @@ void nano::json_handler::chain (bool successors)
entry.put ("", hash.to_string ());
blocks.push_back (std::make_pair ("", entry));
}
hash = successors ? node.store.block.successor (transaction, hash) : block_l->previous ();
hash = successors ? node.ledger.successor (transaction, hash).value_or (0) : block_l->previous ();
}
else
{
Expand Down Expand Up @@ -2665,7 +2665,7 @@ void nano::json_handler::account_history ()
--count;
}
}
hash = reverse ? node.store.block.successor (transaction, hash) : block->previous ();
hash = reverse ? node.ledger.successor (transaction, hash).value_or (0) : block->previous ();
block = node.ledger.block (transaction, hash);
}
response_l.add_child ("history", history);
Expand Down Expand Up @@ -3698,7 +3698,7 @@ void nano::json_handler::republish ()
}
}
}
hash = node.store.block.successor (transaction, hash);
hash = node.ledger.successor (transaction, hash).value_or (0);
}
node.network.flood_block_many (std::move (republish_bundle), nullptr, 25);
response_l.put ("success", ""); // obsolete
Expand Down
18 changes: 4 additions & 14 deletions nano/node/request_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,14 @@ std::pair<std::vector<std::shared_ptr<nano::block>>, std::vector<std::shared_ptr
if (block == nullptr && !root.is_zero ())
{
// Search for block root
auto successor (ledger.store.block.successor (transaction, root.as_block_hash ()));

// Search for account root
if (successor.is_zero ())
{
auto info = ledger.account_info (transaction, root.as_account ());
if (info)
{
successor = info->open_block;
}
}
if (!successor.is_zero ())
auto successor = ledger.successor (transaction, root.as_block_hash ());
if (successor)
{
auto successor_block = ledger.block (transaction, successor);
auto successor_block = ledger.block (transaction, successor.value ());
debug_assert (successor_block != nullptr);
block = std::move (successor_block);
// 5. Votes in cache for successor
auto find_successor_votes (local_votes.votes (root, successor));
auto find_successor_votes (local_votes.votes (root, successor.value ()));
if (!find_successor_votes.empty ())
{
cached_votes.insert (cached_votes.end (), find_successor_votes.begin (), find_successor_votes.end ());
Expand Down
2 changes: 1 addition & 1 deletion nano/node/scheduler/priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool nano::scheduler::priority::activate (nano::account const & account_a, store
if (conf_info.height < info->block_count)
{
debug_assert (conf_info.frontier != info->head);
auto hash = conf_info.height == 0 ? info->open_block : node.store.block.successor (transaction, conf_info.frontier);
auto hash = conf_info.height == 0 ? info->open_block : node.ledger.successor (transaction, conf_info.frontier).value_or (0);
auto block = node.ledger.block (transaction, hash);
debug_assert (block != nullptr);
if (node.ledger.dependents_confirmed (transaction, *block))
Expand Down
8 changes: 4 additions & 4 deletions nano/qt/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ nano_qt::block_viewer::block_viewer (nano_qt::wallet & wallet_a) :
std::string contents;
block_l->serialize_json (contents);
block->setPlainText (contents.c_str ());
auto successor_l (this->wallet.node.store.block.successor (transaction, hash_l));
auto successor_l = this->wallet.node.ledger.successor (transaction, hash_l).value_or (0);
successor->setText (successor_l.to_string ().c_str ());
}
else
Expand Down Expand Up @@ -737,13 +737,13 @@ void nano_qt::block_viewer::rebroadcast_action (nano::block_hash const & hash_a)
if (block != nullptr)
{
wallet.node.network.flood_block (block);
auto successor (wallet.node.store.block.successor (transaction, hash_a));
if (!successor.is_zero ())
auto successor = wallet.node.ledger.successor (transaction, hash_a);
if (successor)
{
done = false;
wallet.node.workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::seconds (1), [this, successor] () {
this->wallet.application.postEvent (&this->wallet.processor, new eventloop_event ([this, successor] () {
rebroadcast_action (successor);
rebroadcast_action (successor.value ());
}));
});
}
Expand Down
12 changes: 11 additions & 1 deletion nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,12 +1274,22 @@ std::optional<nano::block_hash> nano::ledger::successor (store::transaction cons
}
}

std::optional<nano::block_hash> nano::ledger::successor (store::transaction const & transaction, nano::block_hash const & hash) noexcept
{
return successor (transaction, { hash, hash });
}

std::shared_ptr<nano::block> nano::ledger::forked_block (store::transaction const & transaction_a, nano::block const & block_a)
{
debug_assert (!block_exists (transaction_a, block_a.hash ()));
auto root (block_a.root ());
debug_assert (block_exists (transaction_a, root.as_block_hash ()) || store.account.exists (transaction_a, root.as_account ()));
auto result = block (transaction_a, store.block.successor (transaction_a, root.as_block_hash ()));
std::shared_ptr<nano::block> result;
auto successor_l = successor (transaction_a, root.as_block_hash ());
if (successor_l)
{
result = block (transaction_a, successor_l.value ());
}
if (result == nullptr)
{
auto info = account_info (transaction_a, root.as_account ());
Expand Down
1 change: 1 addition & 0 deletions nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ledger final
nano::uint128_t account_receivable (store::transaction const &, nano::account const &, bool = false);
nano::uint128_t weight (nano::account const &);
std::optional<nano::block_hash> successor (store::transaction const &, nano::qualified_root const &) noexcept;
std::optional<nano::block_hash> successor (store::transaction const & transaction, nano::block_hash const & hash) noexcept;
std::shared_ptr<nano::block> forked_block (store::transaction const &, nano::block const &);
std::shared_ptr<nano::block> head_block (store::transaction const &, nano::account const &);
bool block_confirmed (store::transaction const &, nano::block_hash const &) const;
Expand Down

0 comments on commit 73384d8

Please sign in to comment.