Skip to content

Commit

Permalink
Rep crawler special handling of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 25, 2024
1 parent b00064b commit 97868ed
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
20 changes: 5 additions & 15 deletions nano/node/repcrawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ std::vector<std::shared_ptr<nano::transport::channel>> nano::rep_crawler::prepar
return { random_peers.begin (), random_peers.end () };
}

auto nano::rep_crawler::prepare_query_target () -> std::optional<hash_root_t>
auto nano::rep_crawler::prepare_query_target () const -> std::optional<hash_root_t>
{
constexpr int max_attempts = 4;

auto transaction = node.ledger.tx_begin_read ();

std::optional<std::pair<nano::block_hash, nano::block_hash>> hash_root;
std::optional<std::pair<nano::block_hash, nano::root>> hash_root;

// Randomly select a block from ledger to request votes for
for (auto i = 0; i < max_attempts && !hash_root; ++i)
Expand All @@ -289,20 +289,10 @@ auto nano::rep_crawler::prepare_query_target () -> std::optional<hash_root_t>
}
}

if (!hash_root)
// Special case for dev network where number of blocks might be very low: if we can't find a block to query, just pick genesis
if (node.network_params.network.is_dev_network () && !hash_root)
{
return std::nullopt;
}

// Don't send same block multiple times in tests
if (node.network_params.network.is_dev_network ())
{
nano::lock_guard<nano::mutex> lock{ mutex };

for (auto i = 0; queries.get<tag_hash> ().count (hash_root->first) != 0 && i < max_attempts; ++i)
{
hash_root = node.ledger.hash_root_random (transaction);
}
hash_root = std::make_pair (node.network_params.ledger.genesis->hash (), node.network_params.ledger.genesis->root ());
}

return hash_root;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/repcrawler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class rep_crawler final

/** Returns a list of endpoints to crawl. The total weight is passed in to avoid computing it twice. */
std::vector<std::shared_ptr<nano::transport::channel>> prepare_crawl_targets (bool sufficient_weight) const;
std::optional<hash_root_t> prepare_query_target ();
std::optional<hash_root_t> prepare_query_target () const;
bool track_rep_request (hash_root_t hash_root, std::shared_ptr<nano::transport::channel> const & channel);

private:
Expand Down
4 changes: 2 additions & 2 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ std::string nano::ledger::block_text (nano::block_hash const & hash_a)
return result;
}

std::pair<nano::block_hash, nano::block_hash> nano::ledger::hash_root_random (secure::transaction const & transaction_a) const
std::pair<nano::block_hash, nano::root> nano::ledger::hash_root_random (secure::transaction const & transaction_a) const
{
nano::block_hash hash (0);
nano::root root (0);
Expand All @@ -962,7 +962,7 @@ std::pair<nano::block_hash, nano::block_hash> nano::ledger::hash_root_random (se
root = block->root ();
}
}
return std::make_pair (hash, root.as_block_hash ());
return std::make_pair (hash, root);
}

// Vote weight of an account
Expand Down
2 changes: 1 addition & 1 deletion nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ledger final
nano::block_hash representative_calculated (secure::transaction const &, nano::block_hash const &);
std::string block_text (char const *);
std::string block_text (nano::block_hash const &);
std::pair<nano::block_hash, nano::block_hash> hash_root_random (secure::transaction const &) const;
std::pair<nano::block_hash, nano::root> hash_root_random (secure::transaction const &) const;
std::optional<nano::pending_info> pending_info (secure::transaction const &, nano::pending_key const & key) const;
std::deque<std::shared_ptr<nano::block>> confirm (secure::write_transaction &, nano::block_hash const & hash, size_t max_blocks = 1024 * 128);
nano::block_status process (secure::write_transaction const &, std::shared_ptr<nano::block> block);
Expand Down

0 comments on commit 97868ed

Please sign in to comment.