Expanding ability for ledger::is_send to process non-state blocks.

Two new ledger contexts generate a send/receive pair on the genesis chain with state and legacy blocks.
This commit is contained in:
clemahieu 2022-07-09 14:52:20 +01:00
commit be4a72c48b
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
3 changed files with 38 additions and 5 deletions

View file

@ -5646,3 +5646,32 @@ TEST (ledger, unconfirmed_frontiers)
ASSERT_EQ (uncemented_info1.cemented_frontier, uncemented_info2.cemented_frontier);
ASSERT_EQ (uncemented_info1.frontier, uncemented_info2.frontier);
}
TEST (ledger, is_send_genesis)
{
auto ctx = nano::test::context::ledger_empty ();
auto & ledger = ctx.ledger ();
auto & store = ctx.store ();
auto tx = store.tx_begin_read ();
ASSERT_FALSE (ledger.is_send (tx, *nano::dev::genesis));
}
TEST (ledger, is_send_state)
{
auto ctx = nano::test::context::ledger_send_receive ();
auto & ledger = ctx.ledger ();
auto & store = ctx.store ();
auto tx = store.tx_begin_read ();
ASSERT_TRUE (ledger.is_send (tx, *ctx.blocks ()[0]));
ASSERT_FALSE (ledger.is_send (tx, *ctx.blocks ()[1]));
}
TEST (ledger, is_send_legacy)
{
auto ctx = nano::test::context::ledger_send_receive_legacy ();
auto & ledger = ctx.ledger ();
auto & store = ctx.store ();
auto tx = store.tx_begin_read ();
ASSERT_TRUE (ledger.is_send (tx, *ctx.blocks ()[0]));
ASSERT_FALSE (ledger.is_send (tx, *ctx.blocks ()[1]));
}

View file

@ -923,14 +923,19 @@ std::string nano::ledger::block_text (nano::block_hash const & hash_a)
return result;
}
bool nano::ledger::is_send (nano::transaction const & transaction_a, nano::state_block const & block_a) const
bool nano::ledger::is_send (nano::transaction const & transaction_a, nano::block const & block_a) const
{
if (block_a.type () != nano::block_type::state)
{
return block_a.type () == nano::block_type::send;
}
nano::block_hash previous = block_a.previous ();
/*
* if block_a does not have a sideband, then is_send()
* requires that the previous block exists in the database.
* This is because it must retrieve the balance of the previous block.
*/
debug_assert (block_a.has_sideband () || block_a.hashables.previous.is_zero () || store.block.exists (transaction_a, block_a.hashables.previous));
debug_assert (block_a.has_sideband () || previous.is_zero () || store.block.exists (transaction_a, previous));
bool result (false);
if (block_a.has_sideband ())
@ -939,10 +944,9 @@ bool nano::ledger::is_send (nano::transaction const & transaction_a, nano::state
}
else
{
nano::block_hash previous (block_a.hashables.previous);
if (!previous.is_zero ())
{
if (block_a.hashables.balance < balance (transaction_a, previous))
if (block_a.balance () < balance (transaction_a, previous))
{
result = true;
}

View file

@ -51,7 +51,7 @@ public:
bool root_exists (nano::transaction const &, nano::root const &);
std::string block_text (char const *);
std::string block_text (nano::block_hash const &);
bool is_send (nano::transaction const &, nano::state_block const &) const;
bool is_send (nano::transaction const &, nano::block const &) const;
nano::account const & block_destination (nano::transaction const &, nano::block const &);
nano::block_hash block_source (nano::transaction const &, nano::block const &);
std::pair<nano::block_hash, nano::block_hash> hash_root_random (nano::transaction const &) const;