Rep crawler special handling of unit tests

This commit is contained in:
Piotr Wójcik 2024-11-15 18:59:32 +01:00
commit 97868ed2ae
4 changed files with 9 additions and 19 deletions

View file

@ -269,13 +269,13 @@ std::vector<std::shared_ptr<nano::transport::channel>> nano::rep_crawler::prepar
return { random_peers.begin (), random_peers.end () }; 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; constexpr int max_attempts = 4;
auto transaction = node.ledger.tx_begin_read (); 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 // Randomly select a block from ledger to request votes for
for (auto i = 0; i < max_attempts && !hash_root; ++i) for (auto i = 0; i < max_attempts && !hash_root; ++i)
@ -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; hash_root = std::make_pair (node.network_params.ledger.genesis->hash (), node.network_params.ledger.genesis->root ());
}
// 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);
}
} }
return hash_root; return hash_root;

View file

@ -107,7 +107,7 @@ private:
/** Returns a list of endpoints to crawl. The total weight is passed in to avoid computing it twice. */ /** 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::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); bool track_rep_request (hash_root_t hash_root, std::shared_ptr<nano::transport::channel> const & channel);
private: private:

View file

@ -936,7 +936,7 @@ std::string nano::ledger::block_text (nano::block_hash const & hash_a)
return result; 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::block_hash hash (0);
nano::root root (0); nano::root root (0);
@ -962,7 +962,7 @@ std::pair<nano::block_hash, nano::block_hash> nano::ledger::hash_root_random (se
root = block->root (); root = block->root ();
} }
} }
return std::make_pair (hash, root.as_block_hash ()); return std::make_pair (hash, root);
} }
// Vote weight of an account // Vote weight of an account

View file

@ -58,7 +58,7 @@ public:
nano::block_hash representative_calculated (secure::transaction const &, nano::block_hash const &); nano::block_hash representative_calculated (secure::transaction const &, nano::block_hash const &);
std::string block_text (char const *); std::string block_text (char const *);
std::string block_text (nano::block_hash 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::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); 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); nano::block_status process (secure::write_transaction const &, std::shared_ptr<nano::block> block);