Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Periodic rep crawler weights update #2319

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions nano/node/repcrawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void nano::rep_crawler::ongoing_crawl ()
auto now (std::chrono::steady_clock::now ());
auto total_weight_l (total_weight ());
cleanup_reps ();
update_weights ();
query (get_crawl_targets (total_weight_l));
auto sufficient_weight (total_weight_l > node.config.online_weight_minimum.number ());
// If online weight drops below minimum, reach out to preconfigured peers
Expand Down Expand Up @@ -117,28 +118,30 @@ void nano::rep_crawler::query (std::shared_ptr<nano::transport::channel> channel

bool nano::rep_crawler::response (std::shared_ptr<nano::transport::channel> channel_a, nano::account const & rep_account_a, nano::amount const & weight_a)
{
auto updated (false);
auto updated_or_inserted (false);
nano::lock_guard<std::mutex> lock (probable_reps_mutex);
auto existing (probable_reps.find (rep_account_a));
if (existing != probable_reps.end ())
{
probable_reps.modify (existing, [weight_a, &updated, rep_account_a, channel_a](nano::representative & info) {
probable_reps.modify (existing, [weight_a, &updated_or_inserted, rep_account_a, channel_a](nano::representative & info) {
info.last_response = std::chrono::steady_clock::now ();

if (info.weight < weight_a)
// Update if representative channel was changed
if (info.channel->get_endpoint () != channel_a->get_endpoint ())
{
updated = true;
assert (info.account == rep_account_a);
updated_or_inserted = true;
info.weight = weight_a;
info.channel = channel_a;
info.account = rep_account_a;
}
});
}
else
{
probable_reps.insert (nano::representative (rep_account_a, weight_a, channel_a));
updated_or_inserted = true;
}
return updated;
return updated_or_inserted;
}

nano::uint128_t nano::rep_crawler::total_weight () const
Expand Down Expand Up @@ -166,15 +169,8 @@ std::vector<nano::representative> nano::rep_crawler::representatives_by_weight (
nano::lock_guard<std::mutex> lock (probable_reps_mutex);
for (auto i (probable_reps.get<tag_weight> ().begin ()), n (probable_reps.get<tag_weight> ().end ()); i != n; ++i)
{
auto weight (i->weight.number ());
if (weight > 0)
{
result.push_back (*i);
}
else
{
break;
}
assert (i->weight.number () > 0);
result.push_back (*i);
}
return result;
}
Expand Down Expand Up @@ -234,6 +230,30 @@ void nano::rep_crawler::cleanup_reps ()
}
}

void nano::rep_crawler::update_weights ()
{
nano::lock_guard<std::mutex> lock (probable_reps_mutex);
for (auto i (probable_reps.get<tag_last_request> ().begin ()), n (probable_reps.get<tag_last_request> ().end ()); i != n;)
{
auto weight (node.ledger.weight (i->account));
if (weight > 0)
{
if (i->weight.number () != weight)
{
probable_reps.get<tag_last_request> ().modify (i, [weight](nano::representative & info) {
info.weight = weight;
});
}
++i;
}
else
{
// Erase non representatives
i = probable_reps.get<tag_last_request> ().erase (i);
}
}
}

std::vector<nano::representative> nano::rep_crawler::representatives (size_t count_a)
{
std::vector<representative> result;
Expand Down
3 changes: 3 additions & 0 deletions nano/node/repcrawler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class rep_crawler
/** Clean representatives with inactive channels */
void cleanup_reps ();

/** Update representatives weights from ledger */
void update_weights ();

/** Protects the probable_reps container */
mutable std::mutex probable_reps_mutex;

Expand Down