Add ledger::head_block query which returns the head block for a given account. (#4121)

This commit is contained in:
clemahieu 2023-02-14 10:41:47 +00:00 committed by GitHub
commit 004ffe5d6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View file

@ -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 ()));
}

View file

@ -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))

View file

@ -53,6 +53,7 @@ public:
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 &);