Add receivable_any function to nano::ledger which returns whether there is any receivable entry for an account.

This commit is contained in:
Colin LeMahieu 2024-03-16 16:24:58 +00:00
commit 994bef05d4
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
3 changed files with 35 additions and 1 deletions

View file

@ -5654,3 +5654,29 @@ TEST (ledger_receivable, key_two)
ASSERT_EQ (ledger.receivable_end (), ++next1);
ASSERT_EQ (ledger.receivable_end (), ++next2);
}
TEST (ledger_receivable, any_none)
{
auto ctx = nano::test::context::ledger_empty ();
ASSERT_FALSE (ctx.ledger ().receivable_any (ctx.store ().tx_begin_read (), nano::dev::genesis_key.pub));
}
TEST (ledger_receivable, any_one)
{
auto ctx = nano::test::context::ledger_empty ();
nano::block_builder builder;
nano::keypair key;
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 (*ctx.pool ().generate (nano::dev::genesis->hash ()))
.build ();
ASSERT_EQ (nano::block_status::progress, ctx.ledger ().process (ctx.store ().tx_begin_write (), send1));
ASSERT_TRUE (ctx.ledger ().receivable_any (ctx.store ().tx_begin_read (), nano::dev::genesis_key.pub));
ASSERT_FALSE (ctx.ledger ().receivable_any (ctx.store ().tx_begin_read (), key.pub));
}

View file

@ -413,7 +413,7 @@ void ledger_processor::epoch_block_impl (nano::state_block & block_a)
// Non-exisitng account should have pending entries
if (result == nano::block_status::progress)
{
bool pending_exists = ledger.store.pending.any (transaction, block_a.hashables.account);
bool pending_exists = ledger.receivable_any (transaction, block_a.hashables.account);
result = pending_exists ? nano::block_status::progress : nano::block_status::gap_epoch_open_pending;
}
}
@ -1545,6 +1545,12 @@ uint64_t nano::ledger::height (store::transaction const & transaction, nano::blo
return block_l->sideband ().height;
}
bool nano::ledger::receivable_any (store::transaction const & tx, nano::account const & account) const
{
auto next = receivable_upper_bound (tx, account, 0);
return next != receivable_end ();
}
std::optional<std::pair<nano::pending_key, nano::pending_info>> nano::ledger::receivable_lower_bound (store::transaction const & tx, nano::account const & account, nano::block_hash const & hash) const
{
auto result = store.pending.begin (tx, { account, hash });

View file

@ -87,6 +87,8 @@ public:
static nano::epoch version (nano::block const & block);
nano::epoch version (store::transaction const & transaction, nano::block_hash const & hash) const;
uint64_t height (store::transaction const & transaction, nano::block_hash const & hash) const;
// Returns whether there are any receivable entries for 'account'
bool receivable_any (store::transaction const & tx, nano::account const & account) const;
nano::receivable_iterator receivable_end () const;
// Returns the next receivable entry for an account greater than 'account'
nano::receivable_iterator receivable_upper_bound (store::transaction const & tx, nano::account const & account) const;