Skip to content

Commit

Permalink
Moving find_receive_block_by_send_hash from node to the ledger class
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Silva committed Mar 28, 2022
1 parent 8e3ca10 commit 434045f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ void nano::json_handler::blocks_info ()
}
if (receive_hash)
{
std::shared_ptr<nano::block> receive_block = node.find_receive_block_by_send_hash (transaction, destination, hash);
std::shared_ptr<nano::block> receive_block = node.ledger.find_receive_block_by_send_hash (transaction, destination, hash);
std::string receive_hash = receive_block ? receive_block->hash ().to_string () : nano::block_hash (0).to_string ();
entry.put ("receive_hash", receive_hash);
}
Expand Down
48 changes: 0 additions & 48 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1774,54 +1774,6 @@ void nano::node::populate_backlog ()
}
}

/** Given the block hash of a send block, find the associated receive block that receives that send.
* The send block hash is not checked in any way, it is assumed to be correct.
* @return Return the receive block on success and null on failure
*/
std::shared_ptr<nano::block> nano::node::find_receive_block_by_send_hash (nano::transaction const & transaction, nano::account const & destination, nano::block_hash const & send_block_hash)
{
std::shared_ptr<nano::block> result;
debug_assert (send_block_hash != 0);

// get the cemented frontier
nano::confirmation_height_info info;
if (store.confirmation_height.get (transaction, destination, info))
{
return nullptr;
}
auto possible_receive_block = store.block.get (transaction, info.frontier);

// walk down the chain until the source field of a receive block matches the send block hash
while (possible_receive_block != nullptr)
{
// if source is non-zero then it is a legacy receive or open block
nano::block_hash source = possible_receive_block->source ();

// if source is zero then it could be a state block, which needs a different kind of access
auto state_block = dynamic_cast<nano::state_block const *> (possible_receive_block.get ());
if (state_block != nullptr)
{
// we read the block from the database, so we expect it to have sideband
debug_assert (state_block->has_sideband ());
if (state_block->sideband ().details.is_receive)
{
source = state_block->hashables.link.as_block_hash ();
}
}

if (send_block_hash == source)
{
// we have a match
result = possible_receive_block;
break;
}

possible_receive_block = store.block.get (transaction, possible_receive_block->previous ());
}

return result;
}

nano::node_wrapper::node_wrapper (boost::filesystem::path const & path_a, boost::filesystem::path const & config_path_a, nano::node_flags const & node_flags_a) :
network_params{ nano::network_constants::active_network },
io_context (std::make_shared<boost::asio::io_context> ()),
Expand Down
1 change: 0 additions & 1 deletion nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class node final : public std::enable_shared_from_this<nano::node>
void set_bandwidth_params (std::size_t limit, double ratio);
std::pair<uint64_t, decltype (nano::ledger::bootstrap_weights)> get_bootstrap_weights () const;
void populate_backlog ();
std::shared_ptr<nano::block> find_receive_block_by_send_hash (nano::transaction const & transaction, nano::account const & destination, nano::block_hash const & send_block_hash);
nano::write_database_queue write_database_queue;
boost::asio::io_context & io_ctx;
boost::latch node_initialized_latch;
Expand Down
48 changes: 48 additions & 0 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,54 @@ std::array<nano::block_hash, 2> nano::ledger::dependent_blocks (nano::transactio
return visitor.result;
}

/** Given the block hash of a send block, find the associated receive block that receives that send.
* The send block hash is not checked in any way, it is assumed to be correct.
* @return Return the receive block on success and null on failure
*/
std::shared_ptr<nano::block> nano::ledger::find_receive_block_by_send_hash (nano::transaction const & transaction, nano::account const & destination, nano::block_hash const & send_block_hash)
{
std::shared_ptr<nano::block> result;
debug_assert (send_block_hash != 0);

// get the cemented frontier
nano::confirmation_height_info info;
if (store.confirmation_height.get (transaction, destination, info))
{
return nullptr;
}
auto possible_receive_block = store.block.get (transaction, info.frontier);

// walk down the chain until the source field of a receive block matches the send block hash
while (possible_receive_block != nullptr)
{
// if source is non-zero then it is a legacy receive or open block
nano::block_hash source = possible_receive_block->source ();

// if source is zero then it could be a state block, which needs a different kind of access
auto state_block = dynamic_cast<nano::state_block const *> (possible_receive_block.get ());
if (state_block != nullptr)
{
// we read the block from the database, so we expect it to have sideband
debug_assert (state_block->has_sideband ());
if (state_block->sideband ().details.is_receive)
{
source = state_block->hashables.link.as_block_hash ();
}
}

if (send_block_hash == source)
{
// we have a match
result = possible_receive_block;
break;
}

possible_receive_block = store.block.get (transaction, possible_receive_block->previous ());
}

return result;
}

nano::account const & nano::ledger::epoch_signer (nano::link const & link_a) const
{
return constants.epochs.signer (constants.epochs.epoch (link_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 @@ -63,6 +63,7 @@ class ledger final
bool dependents_confirmed (nano::transaction const &, nano::block const &) const;
bool is_epoch_link (nano::link const &) const;
std::array<nano::block_hash, 2> dependent_blocks (nano::transaction const &, nano::block const &) const;
std::shared_ptr<nano::block> find_receive_block_by_send_hash (nano::transaction const & transaction, nano::account const & destination, nano::block_hash const & send_block_hash);
nano::account const & epoch_signer (nano::link const &) const;
nano::link const & epoch_link (nano::epoch) const;
std::multimap<uint64_t, uncemented_info, std::greater<>> unconfirmed_frontiers () const;
Expand Down

0 comments on commit 434045f

Please sign in to comment.