forked from nanocurrency/nano-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e815ef
commit ee1bf0b
Showing
13 changed files
with
260 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include <nano/lib/logging.hpp> | ||
#include <nano/lib/thread_roles.hpp> | ||
#include <nano/node/online_reps.hpp> | ||
#include <nano/node/rep_tiers.hpp> | ||
#include <nano/secure/common.hpp> | ||
#include <nano/secure/ledger.hpp> | ||
|
||
using namespace std::chrono_literals; | ||
|
||
nano::rep_tiers::rep_tiers (nano::ledger & ledger_a, nano::network_params & network_params_a, nano::online_reps & online_reps_a, nano::stats & stats_a, nano::logger & logger_a) : | ||
ledger{ ledger_a }, | ||
network_params{ network_params_a }, | ||
online_reps{ online_reps_a }, | ||
stats{ stats_a }, | ||
logger{ logger_a } | ||
{ | ||
} | ||
|
||
nano::rep_tiers::~rep_tiers () | ||
{ | ||
// Thread must be stopped before destruction | ||
debug_assert (!thread.joinable ()); | ||
} | ||
|
||
void nano::rep_tiers::start () | ||
{ | ||
debug_assert (!thread.joinable ()); | ||
|
||
thread = std::thread{ [this] () { | ||
nano::thread_role::set (nano::thread_role::name::rep_tiers); | ||
run (); | ||
} }; | ||
} | ||
|
||
void nano::rep_tiers::stop () | ||
{ | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
stopped = true; | ||
} | ||
condition.notify_all (); | ||
if (thread.joinable ()) | ||
{ | ||
thread.join (); | ||
} | ||
} | ||
|
||
nano::rep_tier nano::rep_tiers::tier (const nano::account & representative) const | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
if (representatives_3.find (representative) != representatives_3.end ()) | ||
{ | ||
return nano::rep_tier::tier_3; | ||
} | ||
if (representatives_2.find (representative) != representatives_2.end ()) | ||
{ | ||
return nano::rep_tier::tier_2; | ||
} | ||
if (representatives_1.find (representative) != representatives_1.end ()) | ||
{ | ||
return nano::rep_tier::tier_1; | ||
} | ||
return nano::rep_tier::none; | ||
} | ||
|
||
void nano::rep_tiers::run () | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ mutex }; | ||
while (!stopped) | ||
{ | ||
stats.inc (nano::stat::type::rep_tiers, nano::stat::detail::loop); | ||
|
||
lock.unlock (); | ||
|
||
calculate_tiers (); | ||
|
||
lock.lock (); | ||
|
||
std::chrono::milliseconds interval = network_params.network.is_dev_network () ? 500ms : 10min; | ||
condition.wait_for (lock, interval); | ||
} | ||
} | ||
|
||
void nano::rep_tiers::calculate_tiers () | ||
{ | ||
auto online = online_reps.online (); | ||
auto rep_amounts = ledger.cache.rep_weights.get_rep_amounts (); | ||
|
||
decltype (representatives_1) representatives_1_l; | ||
decltype (representatives_2) representatives_2_l; | ||
decltype (representatives_3) representatives_3_l; | ||
|
||
int ignored = 0; | ||
for (auto const & rep_amount : rep_amounts) | ||
{ | ||
nano::account const & representative = rep_amount.first; | ||
|
||
// Using ledger weight here because it takes preconfigured bootstrap weights into account | ||
auto weight = ledger.weight (representative); | ||
if (weight > online / 1000) // 0.1% or above (level 1) | ||
{ | ||
representatives_1_l.insert (representative); | ||
if (weight > online / 100) // 1% or above (level 2) | ||
{ | ||
representatives_2_l.insert (representative); | ||
if (weight > online / 20) // 5% or above (level 3) | ||
{ | ||
representatives_3_l.insert (representative); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
++ignored; | ||
} | ||
} | ||
|
||
stats.add (nano::stat::type::rep_tiers, nano::stat::detail::processed, nano::stat::dir::in, rep_amounts.size ()); | ||
stats.add (nano::stat::type::rep_tiers, nano::stat::detail::ignored, nano::stat::dir::in, ignored); | ||
logger.debug (nano::log::type::rep_tiers, "Representative tiers updated, tier 1: {}, tier 2: {}, tier 3: {} ({} ignored)", | ||
representatives_1_l.size (), | ||
representatives_2_l.size (), | ||
representatives_3_l.size (), | ||
ignored); | ||
|
||
{ | ||
nano::lock_guard<nano::mutex> guard{ mutex }; | ||
representatives_1 = std::move (representatives_1_l); | ||
representatives_2 = std::move (representatives_2_l); | ||
representatives_3 = std::move (representatives_3_l); | ||
} | ||
|
||
stats.inc (nano::stat::type::rep_tiers, nano::stat::detail::updated); | ||
} | ||
|
||
std::unique_ptr<nano::container_info_component> nano::rep_tiers::collect_container_info (const std::string & name) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ mutex }; | ||
auto composite = std::make_unique<container_info_composite> (name); | ||
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "representatives_1", representatives_1.size (), sizeof (decltype (representatives_1)::value_type) })); | ||
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "representatives_2", representatives_2.size (), sizeof (decltype (representatives_2)::value_type) })); | ||
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "representatives_3", representatives_3.size (), sizeof (decltype (representatives_3)::value_type) })); | ||
return composite; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/numbers.hpp> | ||
#include <nano/lib/utility.hpp> | ||
#include <nano/secure/common.hpp> | ||
|
||
#include <memory> | ||
#include <thread> | ||
#include <unordered_set> | ||
|
||
namespace nano | ||
{ | ||
class ledger; | ||
class network_params; | ||
class stats; | ||
class logger; | ||
class container_info_component; | ||
class online_reps; | ||
|
||
// Higher number means higher priority | ||
enum class rep_tier | ||
{ | ||
none, // Not a principal representatives | ||
tier_1, // (0.1-1%) of online stake | ||
tier_2, // (1-5%) of online stake | ||
tier_3, // (> 5%) of online stake | ||
}; | ||
|
||
class rep_tiers final | ||
{ | ||
public: | ||
rep_tiers (nano::ledger &, nano::network_params &, nano::online_reps &, nano::stats &, nano::logger &); | ||
~rep_tiers (); | ||
|
||
void start (); | ||
void stop (); | ||
|
||
/** Returns the representative tier for the account */ | ||
nano::rep_tier tier (nano::account const & representative) const; | ||
|
||
std::unique_ptr<container_info_component> collect_container_info (std::string const & name); | ||
|
||
private: // Dependencies | ||
nano::ledger & ledger; | ||
nano::network_params & network_params; | ||
nano::online_reps & online_reps; | ||
nano::stats & stats; | ||
nano::logger & logger; | ||
|
||
private: | ||
void run (); | ||
void calculate_tiers (); | ||
|
||
private: | ||
/** Representatives levels for early prioritization */ | ||
std::unordered_set<nano::account> representatives_1; | ||
std::unordered_set<nano::account> representatives_2; | ||
std::unordered_set<nano::account> representatives_3; | ||
|
||
std::atomic<bool> stopped{ false }; | ||
nano::condition_variable condition; | ||
mutable nano::mutex mutex; | ||
std::thread thread; | ||
}; | ||
} |
Oops, something went wrong.