Skip to content

Commit

Permalink
Creating block::is_receive function to determine if a block is a rece…
Browse files Browse the repository at this point in the history
…ive/open/state block that's a receive
  • Loading branch information
clemahieu committed Mar 8, 2024
1 parent 6770910 commit ec4706a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
28 changes: 14 additions & 14 deletions nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ TEST (ledger, state_send_receive)
ASSERT_TRUE (store.pending.exists (transaction, nano::pending_key (nano::dev::genesis_key.pub, send1->hash ())));
ASSERT_EQ (2, send2->sideband ().height);
ASSERT_TRUE (send2->is_send ());
ASSERT_FALSE (send2->sideband ().details.is_receive);
ASSERT_FALSE (send2->is_receive ());
ASSERT_FALSE (send2->sideband ().details.is_epoch);
auto receive1 = builder
.state ()
Expand All @@ -2417,7 +2417,7 @@ TEST (ledger, state_send_receive)
ASSERT_EQ (store.account.count (transaction), ledger.cache.account_count);
ASSERT_EQ (3, receive2->sideband ().height);
ASSERT_FALSE (receive2->is_send ());
ASSERT_TRUE (receive2->sideband ().details.is_receive);
ASSERT_TRUE (receive2->is_receive ());
ASSERT_FALSE (receive2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -2465,7 +2465,7 @@ TEST (ledger, state_receive)
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (nano::dev::genesis_key.pub));
ASSERT_EQ (3, receive2->sideband ().height);
ASSERT_FALSE (receive2->is_send ());
ASSERT_TRUE (receive2->sideband ().details.is_receive);
ASSERT_TRUE (receive2->is_receive ());
ASSERT_FALSE (receive2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -2499,7 +2499,7 @@ TEST (ledger, state_rep_change)
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (rep.pub));
ASSERT_EQ (2, change2->sideband ().height);
ASSERT_FALSE (change2->is_send ());
ASSERT_FALSE (change2->sideband ().details.is_receive);
ASSERT_FALSE (change2->is_receive ());
ASSERT_FALSE (change2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -2553,7 +2553,7 @@ TEST (ledger, state_open)
ASSERT_EQ (ledger.cache.account_count, store.account.count (transaction));
ASSERT_EQ (1, open2->sideband ().height);
ASSERT_FALSE (open2->is_send ());
ASSERT_TRUE (open2->sideband ().details.is_receive);
ASSERT_TRUE (open2->is_receive ());
ASSERT_FALSE (open2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -2979,7 +2979,7 @@ TEST (ledger, state_send_change)
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (rep.pub));
ASSERT_EQ (2, send2->sideband ().height);
ASSERT_TRUE (send2->is_send ());
ASSERT_FALSE (send2->sideband ().details.is_receive);
ASSERT_FALSE (send2->is_receive ());
ASSERT_FALSE (send2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -3031,7 +3031,7 @@ TEST (ledger, state_receive_change)
ASSERT_EQ (nano::dev::constants.genesis_amount, ledger.weight (rep.pub));
ASSERT_EQ (3, receive2->sideband ().height);
ASSERT_FALSE (receive2->is_send ());
ASSERT_TRUE (receive2->sideband ().details.is_receive);
ASSERT_TRUE (receive2->is_receive ());
ASSERT_FALSE (receive2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -3402,7 +3402,7 @@ TEST (ledger, epoch_blocks_v1_general)
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, epoch1));
ASSERT_FALSE (epoch1->is_send ());
ASSERT_FALSE (epoch1->sideband ().details.is_receive);
ASSERT_FALSE (epoch1->is_receive ());
ASSERT_TRUE (epoch1->sideband ().details.is_epoch);
ASSERT_EQ (nano::epoch::epoch_1, epoch1->sideband ().details.epoch);
ASSERT_EQ (nano::epoch::epoch_0, epoch1->sideband ().source_epoch); // Not used for epoch blocks
Expand All @@ -3429,7 +3429,7 @@ TEST (ledger, epoch_blocks_v1_general)
ASSERT_TRUE (genesis_info);
ASSERT_EQ (genesis_info->epoch (), nano::epoch::epoch_1);
ASSERT_FALSE (epoch1->is_send ());
ASSERT_FALSE (epoch1->sideband ().details.is_receive);
ASSERT_FALSE (epoch1->is_receive ());
ASSERT_TRUE (epoch1->sideband ().details.is_epoch);
ASSERT_EQ (nano::epoch::epoch_1, epoch1->sideband ().details.epoch);
ASSERT_EQ (nano::epoch::epoch_0, epoch1->sideband ().source_epoch); // Not used for epoch blocks
Expand All @@ -3453,7 +3453,7 @@ TEST (ledger, epoch_blocks_v1_general)
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send1));
ASSERT_TRUE (send1->is_send ());
ASSERT_FALSE (send1->sideband ().details.is_receive);
ASSERT_FALSE (send1->is_receive ());
ASSERT_FALSE (send1->sideband ().details.is_epoch);
ASSERT_EQ (nano::epoch::epoch_1, send1->sideband ().details.epoch);
ASSERT_EQ (nano::epoch::epoch_0, send1->sideband ().source_epoch); // Not used for send blocks
Expand Down Expand Up @@ -3489,7 +3489,7 @@ TEST (ledger, epoch_blocks_v1_general)
.build ();
ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, epoch4));
ASSERT_FALSE (epoch4->is_send ());
ASSERT_FALSE (epoch4->sideband ().details.is_receive);
ASSERT_FALSE (epoch4->is_receive ());
ASSERT_TRUE (epoch4->sideband ().details.is_epoch);
ASSERT_EQ (nano::epoch::epoch_1, epoch4->sideband ().details.epoch);
ASSERT_EQ (nano::epoch::epoch_0, epoch4->sideband ().source_epoch); // Not used for epoch blocks
Expand Down Expand Up @@ -3520,7 +3520,7 @@ TEST (ledger, epoch_blocks_v1_general)
ASSERT_EQ (nano::dev::constants.genesis_amount - nano::Gxrb_ratio, ledger.weight (nano::dev::genesis_key.pub));
ASSERT_EQ (nano::Gxrb_ratio, ledger.weight (destination.pub));
ASSERT_FALSE (receive2->is_send ());
ASSERT_TRUE (receive2->sideband ().details.is_receive);
ASSERT_TRUE (receive2->is_receive ());
ASSERT_FALSE (receive2->sideband ().details.is_epoch);
}

Expand Down Expand Up @@ -4297,7 +4297,7 @@ TEST (ledger, unchecked_epoch_invalid)
ASSERT_EQ (nano::epoch::epoch_0, epoch2_store->sideband ().details.epoch);
ASSERT_TRUE (epoch2_store->is_send ());
ASSERT_FALSE (epoch2_store->sideband ().details.is_epoch);
ASSERT_FALSE (epoch2_store->sideband ().details.is_receive);
ASSERT_FALSE (epoch2_store->is_receive ());
}
}

Expand Down Expand Up @@ -4917,7 +4917,7 @@ TEST (ledger, pruning_action)
ASSERT_FALSE (store->pending.exists (transaction, nano::pending_key (nano::dev::genesis_key.pub, send1->hash ())));
ASSERT_EQ (4, receive1_stored->sideband ().height);
ASSERT_FALSE (receive1_stored->is_send ());
ASSERT_TRUE (receive1_stored->sideband ().details.is_receive);
ASSERT_TRUE (receive1_stored->is_receive ());
ASSERT_FALSE (receive1_stored->sideband ().details.is_epoch);
// Middle block pruning
ASSERT_TRUE (store->block.exists (transaction, send2->hash ()));
Expand Down
15 changes: 15 additions & 0 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ bool nano::block::is_send () const
}
}

bool nano::block::is_receive () const
{
release_assert (has_sideband ());
switch (type ())
{
case nano::block_type::receive:
case nano::block_type::open:
return true;
case nano::block_type::state:
return sideband ().details.is_receive;
default:
return false;
}
}

nano::block_hash const & nano::block::hash () const
{
if (!cached_hash.is_zero ())
Expand Down
1 change: 1 addition & 0 deletions nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class block
// If there are any changes to the hashables, call this to update the cached hash
void refresh ();
bool is_send () const;
bool is_receive () const;

public: // Direct access to the block fields or nullopt if the block type does not have the specified field
// Returns account field or account from sideband
Expand Down
2 changes: 1 addition & 1 deletion nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ void nano::json_handler::blocks_info ()
}
if (source)
{
if (!block->sideband ().details.is_receive || !node.ledger.block_exists (transaction, block->source ()))
if (!block->is_receive () || !node.ledger.block_exists (transaction, block->source ()))
{
entry.put ("source_account", "0");
}
Expand Down
6 changes: 3 additions & 3 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4769,7 +4769,7 @@ TEST (rpc, block_create_state_open)
auto process_result (node->process (state_block));
ASSERT_EQ (nano::block_status::progress, process_result);
ASSERT_EQ (state_block->sideband ().details.epoch, nano::epoch::epoch_0);
ASSERT_TRUE (state_block->sideband ().details.is_receive);
ASSERT_TRUE (state_block->is_receive ());
ASSERT_FALSE (node->latest (key.pub).is_zero ());
}

Expand Down Expand Up @@ -4848,7 +4848,7 @@ TEST (rpc, block_create_open_epoch_v2)
auto process_result (node->process (state_block));
ASSERT_EQ (nano::block_status::progress, process_result);
ASSERT_EQ (state_block->sideband ().details.epoch, nano::epoch::epoch_2);
ASSERT_TRUE (state_block->sideband ().details.is_receive);
ASSERT_TRUE (state_block->is_receive ());
ASSERT_FALSE (node->latest (key.pub).is_zero ());
}

Expand Down Expand Up @@ -4900,7 +4900,7 @@ TEST (rpc, block_create_receive_epoch_v2)
auto process_result (node->process (state_block));
ASSERT_EQ (nano::block_status::progress, process_result);
ASSERT_EQ (state_block->sideband ().details.epoch, nano::epoch::epoch_2);
ASSERT_TRUE (state_block->sideband ().details.is_receive);
ASSERT_TRUE (state_block->is_receive ());
ASSERT_FALSE (node->latest (key.pub).is_zero ());
}

Expand Down
2 changes: 1 addition & 1 deletion nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ std::shared_ptr<nano::block> nano::ledger::find_receive_block_by_send_hash (stor
// walk down the chain until the source field of a receive block matches the send block hash
while (possible_receive_block != nullptr)
{
if (possible_receive_block->sideband ().details.is_receive && send_block_hash == possible_receive_block->source ())
if (possible_receive_block->is_receive () && send_block_hash == possible_receive_block->source ())
{
// we have a match
result = possible_receive_block;
Expand Down

0 comments on commit ec4706a

Please sign in to comment.