Skip to content

Commit

Permalink
Increase rep_crawl count if observed peers weight < online_weight_min…
Browse files Browse the repository at this point in the history
…imum (#1001)

* Increase rep_crawl speed if observed peers weight < online_weight_minimum

* Use uint16_t

* peers.online_weight_minimum

* Improve network::broadcast_confirm_req

* Calculate if representative isn't recorded for several IP addresses
  • Loading branch information
SergiySW authored and argakiig committed Aug 9, 2018
1 parent c7605b1 commit 9d69385
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 24 additions & 4 deletions rai/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void rai::network::republish_vote (std::shared_ptr<rai::vote> vote_a)
void rai::network::broadcast_confirm_req (std::shared_ptr<rai::block> block_a)
{
auto list (std::make_shared<std::vector<rai::peer_information>> (node.peers.representatives (std::numeric_limits<size_t>::max ())));
if (list->empty () || node.online_reps.online_stake () == node.config.online_weight_minimum.number ())
if (list->empty () || node.peers.total_weight () < node.config.online_weight_minimum.number ())
{
// broadcast request to all peers
list = std::make_shared<std::vector<rai::peer_information>> (node.peers.list_vector ());
Expand Down Expand Up @@ -1830,6 +1830,7 @@ stats (config.stat_config)
node_id = rai::keypair (store.get_node_id (transaction));
BOOST_LOG (log) << "Node ID: " << node_id.pub.to_account ();
}
peers.online_weight_minimum = config.online_weight_minimum.number ();
if (rai::rai_network == rai::rai_networks::rai_live_network)
{
extern const char rai_bootstrap_weights[];
Expand Down Expand Up @@ -3093,10 +3094,12 @@ std::vector<rai::peer_information> rai::peer_container::purge_list (std::chrono:
std::vector<rai::endpoint> rai::peer_container::rep_crawl ()
{
std::vector<rai::endpoint> result;
result.reserve (10);
// If there is enough observed peers weight, crawl 10 peers. Otherwise - 40
uint16_t max_count = (total_weight () > online_weight_minimum) ? 10 : 40;
result.reserve (max_count);
std::lock_guard<std::mutex> lock (mutex);
auto count (0);
for (auto i (peers.get<5> ().begin ()), n (peers.get<5> ().end ()); i != n && count < 10; ++i, ++count)
uint16_t count (0);
for (auto i (peers.get<5> ().begin ()), n (peers.get<5> ().end ()); i != n && count < max_count; ++i, ++count)
{
result.push_back (i->endpoint);
};
Expand All @@ -3115,6 +3118,23 @@ size_t rai::peer_container::size_sqrt ()
return result;
}

rai::uint128_t rai::peer_container::total_weight ()
{
rai::uint128_t result (0);
std::unordered_set<rai::account> probable_reps;
std::lock_guard<std::mutex> lock (mutex);
for (auto i (peers.get<6> ().begin ()), n (peers.get<6> ().end ()); i != n; ++i)
{
// Calculate if representative isn't recorded for several IP addresses
if (probable_reps.find (i->probable_rep_account) == probable_reps.end ())
{
result = result + i->rep_weight.number ();
probable_reps.insert (i->probable_rep_account);
}
}
return result;
}

bool rai::peer_container::empty ()
{
return size () == 0;
Expand Down
2 changes: 2 additions & 0 deletions rai/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ class peer_container
bool validate_syn_cookie (rai::endpoint const &, rai::account, rai::signature);
size_t size ();
size_t size_sqrt ();
rai::uint128_t total_weight ();
rai::uint128_t online_weight_minimum;
bool empty ();
std::mutex mutex;
rai::endpoint self;
Expand Down

0 comments on commit 9d69385

Please sign in to comment.