Skip to content

Commit

Permalink
Add ledger::head_block query which returns the head block for a given…
Browse files Browse the repository at this point in the history
… account. (#4121)
  • Loading branch information
clemahieu committed Feb 14, 2023
1 parent 1b99696 commit 004ffe5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5673,3 +5673,12 @@ TEST (ledger, is_send_legacy)
ASSERT_TRUE (ledger.is_send (tx, *ctx.blocks ()[0]));
ASSERT_FALSE (ledger.is_send (tx, *ctx.blocks ()[1]));
}

TEST (ledger, head_block)
{
auto ctx = nano::test::context::ledger_empty ();
auto & ledger = ctx.ledger ();
auto & store = ctx.store ();
auto tx = store.tx_begin_read ();
ASSERT_EQ (*nano::dev::genesis, *ledger.head_block (tx, nano::dev::genesis->account ()));
}
10 changes: 10 additions & 0 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,16 @@ std::shared_ptr<nano::block> nano::ledger::forked_block (nano::transaction const
return result;
}

std::shared_ptr<nano::block> nano::ledger::head_block (nano::transaction const & transaction, nano::account const & account)
{
auto info = store.account.get (transaction, account);
if (info)
{
return store.block.get (transaction, info->head);
}
return nullptr;
}

bool nano::ledger::block_confirmed (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const
{
if (store.pruned.exists (transaction_a, hash_a))
Expand Down
1 change: 1 addition & 0 deletions nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ledger final
nano::uint128_t weight (nano::account const &);
std::shared_ptr<nano::block> successor (nano::transaction const &, nano::qualified_root const &);
std::shared_ptr<nano::block> forked_block (nano::transaction const &, nano::block const &);
std::shared_ptr<nano::block> head_block (nano::transaction const &, nano::account const &);
bool block_confirmed (nano::transaction const &, nano::block_hash const &) const;
nano::block_hash latest (nano::transaction const &, nano::account const &);
nano::root latest_root (nano::transaction const &, nano::account const &);
Expand Down

0 comments on commit 004ffe5

Please sign in to comment.