Skip to content

Commit

Permalink
Namespace indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 5, 2025
1 parent e65e07a commit c3b59f2
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 159 deletions.
203 changes: 100 additions & 103 deletions nano/node/bootstrap/account_sets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,104 +17,102 @@

namespace mi = boost::multi_index;

namespace nano
namespace nano::bootstrap
{
namespace bootstrap
/** This class tracks accounts various account sets which are shared among the multiple bootstrap threads */
class account_sets
{
/** This class tracks accounts various account sets which are shared among the multiple bootstrap threads */
class account_sets
public: // Constants
static double constexpr priority_initial = 2.0;
static double constexpr priority_increase = 2.0;
static double constexpr priority_divide = 2.0;
static double constexpr priority_max = 128.0;
static double constexpr priority_cutoff = 0.15;
static unsigned constexpr max_fails = 3;

public:
account_sets (account_sets_config const &, nano::stats &);

/**
* If an account is not blocked, increase its priority.
* If the account does not exist in priority set and is not blocked, inserts a new entry.
* Current implementation increases priority by 1.0f each increment
*/
void priority_up (nano::account const & account);
/**
* Decreases account priority
* Current implementation divides priority by 2.0f and saturates down to 1.0f.
*/
void priority_down (nano::account const & account);
void priority_set (nano::account const & account, double priority = priority_initial);

void block (nano::account const & account, nano::block_hash const & dependency);
void unblock (nano::account const & account, std::optional<nano::block_hash> const & hash = std::nullopt);

void timestamp_set (nano::account const & account);
void timestamp_reset (nano::account const & account);

/**
* Sets information about the account chain that contains the block hash
*/
void dependency_update (nano::block_hash const & hash, nano::account const & dependency_account);
/**
* Should be called periodically to reinsert missing dependencies into the priority set
*/
void sync_dependencies ();

struct priority_result
{
public: // Constants
static double constexpr priority_initial = 2.0;
static double constexpr priority_increase = 2.0;
static double constexpr priority_divide = 2.0;
static double constexpr priority_max = 128.0;
static double constexpr priority_cutoff = 0.15;
static unsigned constexpr max_fails = 3;

public:
account_sets (account_sets_config const &, nano::stats &);

/**
* If an account is not blocked, increase its priority.
* If the account does not exist in priority set and is not blocked, inserts a new entry.
* Current implementation increases priority by 1.0f each increment
*/
void priority_up (nano::account const & account);
/**
* Decreases account priority
* Current implementation divides priority by 2.0f and saturates down to 1.0f.
*/
void priority_down (nano::account const & account);
void priority_set (nano::account const & account, double priority = priority_initial);

void block (nano::account const & account, nano::block_hash const & dependency);
void unblock (nano::account const & account, std::optional<nano::block_hash> const & hash = std::nullopt);

void timestamp_set (nano::account const & account);
void timestamp_reset (nano::account const & account);

/**
* Sets information about the account chain that contains the block hash
*/
void dependency_update (nano::block_hash const & hash, nano::account const & dependency_account);
/**
* Should be called periodically to reinsert missing dependencies into the priority set
*/
void sync_dependencies ();

struct priority_result
{
nano::account account;
double priority;
unsigned fails;
};

/**
* Sampling
*/
priority_result next_priority (std::function<bool (nano::account const &)> const & filter);
nano::block_hash next_blocking (std::function<bool (nano::block_hash const &)> const & filter);

bool blocked (nano::account const & account) const;
bool prioritized (nano::account const & account) const;
// Accounts in the ledger but not in priority list are assumed priority 1.0f
// Blocked accounts are assumed priority 0.0f
double priority (nano::account const & account) const;

std::size_t priority_size () const;
std::size_t blocked_size () const;
bool priority_half_full () const;
bool blocked_half_full () const;

nano::container_info container_info () const;

private: // Dependencies
account_sets_config const & config;
nano::stats & stats;

private:
void trim_overflow ();

private:
struct priority_entry
{
nano::account account;
double priority;
unsigned fails{ 0 };
std::chrono::steady_clock::time_point timestamp{};
id_t id{ generate_id () }; // Uniformly distributed, used for random querying
};

struct blocking_entry
{
nano::account account;
nano::block_hash dependency;
nano::account dependency_account{ 0 };
id_t id{ generate_id () }; // Uniformly distributed, used for random querying
};

// clang-format off
nano::account account;
double priority;
unsigned fails;
};

/**
* Sampling
*/
priority_result next_priority (std::function<bool (nano::account const &)> const & filter);
nano::block_hash next_blocking (std::function<bool (nano::block_hash const &)> const & filter);

bool blocked (nano::account const & account) const;
bool prioritized (nano::account const & account) const;
// Accounts in the ledger but not in priority list are assumed priority 1.0f
// Blocked accounts are assumed priority 0.0f
double priority (nano::account const & account) const;

std::size_t priority_size () const;
std::size_t blocked_size () const;
bool priority_half_full () const;
bool blocked_half_full () const;

nano::container_info container_info () const;

private: // Dependencies
account_sets_config const & config;
nano::stats & stats;

private:
void trim_overflow ();

private:
struct priority_entry
{
nano::account account;
double priority;
unsigned fails{ 0 };
std::chrono::steady_clock::time_point timestamp{};
id_t id{ generate_id () }; // Uniformly distributed, used for random querying
};

struct blocking_entry
{
nano::account account;
nano::block_hash dependency;
nano::account dependency_account{ 0 };
id_t id{ generate_id () }; // Uniformly distributed, used for random querying
};

// clang-format off
class tag_sequenced {};
class tag_account {};
class tag_id {};
Expand Down Expand Up @@ -148,14 +146,13 @@ namespace bootstrap
mi::ordered_unique<mi::tag<tag_id>,
mi::member<blocking_entry, id_t, &blocking_entry::id>>
>>;
// clang-format on
// clang-format on

ordered_priorities priorities;
ordered_blocking blocking;
ordered_priorities priorities;
ordered_blocking blocking;

public:
using info_t = std::tuple<decltype (blocking), decltype (priorities)>; // <blocking, priorities>
info_t info () const;
};
}
public:
using info_t = std::tuple<decltype (blocking), decltype (priorities)>; // <blocking, priorities>
info_t info () const;
};
}
109 changes: 53 additions & 56 deletions nano/node/bootstrap/peer_scoring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,70 @@

namespace mi = boost::multi_index;

namespace nano
namespace nano::bootstrap
{
namespace bootstrap
// Container for tracking and scoring peers with respect to bootstrapping
class peer_scoring
{
// Container for tracking and scoring peers with respect to bootstrapping
class peer_scoring
{
public:
static nano::transport::traffic_type constexpr traffic_type = nano::transport::traffic_type::bootstrap_requests;
public:
static nano::transport::traffic_type constexpr traffic_type = nano::transport::traffic_type::bootstrap_requests;

public:
peer_scoring (bootstrap_config const &, nano::network_constants const &);
public:
peer_scoring (bootstrap_config const &, nano::network_constants const &);

// Returns true if channel limit has been exceeded
bool limit_exceeded (std::shared_ptr<nano::transport::channel> const & channel) const;
bool try_send_message (std::shared_ptr<nano::transport::channel> const & channel);
void received_message (std::shared_ptr<nano::transport::channel> const & channel);
// Returns true if channel limit has been exceeded
bool limit_exceeded (std::shared_ptr<nano::transport::channel> const & channel) const;
bool try_send_message (std::shared_ptr<nano::transport::channel> const & channel);
void received_message (std::shared_ptr<nano::transport::channel> const & channel);

std::shared_ptr<nano::transport::channel> channel ();
std::shared_ptr<nano::transport::channel> channel ();

// Synchronize channels with the network, passed channels should be shuffled
void sync (std::deque<std::shared_ptr<nano::transport::channel>> const & list);
// Synchronize channels with the network, passed channels should be shuffled
void sync (std::deque<std::shared_ptr<nano::transport::channel>> const & list);

// Cleans up scores for closed channels
// Decays scores which become inaccurate over time due to message drops
void timeout ();
// Cleans up scores for closed channels
// Decays scores which become inaccurate over time due to message drops
void timeout ();

std::size_t size () const;
std::size_t available () const;
std::size_t size () const;
std::size_t available () const;

nano::container_info container_info () const;
nano::container_info container_info () const;

private:
bootstrap_config const & config;
nano::network_constants const & network_constants;
private:
bootstrap_config const & config;
nano::network_constants const & network_constants;

private:
class peer_score
private:
class peer_score
{
public:
explicit peer_score (std::shared_ptr<nano::transport::channel> const &, uint64_t, uint64_t, uint64_t);
std::weak_ptr<nano::transport::channel> channel;
// std::weak_ptr does not provide ordering so the naked pointer is also tracked and used for ordering channels
// This pointer may be invalid if the channel has been destroyed
nano::transport::channel * channel_ptr;
// Acquire reference to the shared channel object if it is still valid
[[nodiscard]] std::shared_ptr<nano::transport::channel> shared () const
{
public:
explicit peer_score (std::shared_ptr<nano::transport::channel> const &, uint64_t, uint64_t, uint64_t);
std::weak_ptr<nano::transport::channel> channel;
// std::weak_ptr does not provide ordering so the naked pointer is also tracked and used for ordering channels
// This pointer may be invalid if the channel has been destroyed
nano::transport::channel * channel_ptr;
// Acquire reference to the shared channel object if it is still valid
[[nodiscard]] std::shared_ptr<nano::transport::channel> shared () const
auto result = channel.lock ();
if (result)
{
auto result = channel.lock ();
if (result)
{
debug_assert (result.get () == channel_ptr);
}
return result;
debug_assert (result.get () == channel_ptr);
}
void decay ()
{
outstanding = outstanding > 0 ? outstanding - 1 : 0;
}
// Number of outstanding requests to a peer
uint64_t outstanding{ 0 };
uint64_t request_count_total{ 0 };
uint64_t response_count_total{ 0 };
};
return result;
}
void decay ()
{
outstanding = outstanding > 0 ? outstanding - 1 : 0;
}
// Number of outstanding requests to a peer
uint64_t outstanding{ 0 };
uint64_t request_count_total{ 0 };
uint64_t response_count_total{ 0 };
};

// clang-format off
// clang-format off
// Indexes scores by their shared channel pointer
class tag_channel {};
// Indexes scores by the number of outstanding requests in ascending order
Expand All @@ -90,10 +88,9 @@ namespace bootstrap
mi::member<peer_score, nano::transport::channel *, &peer_score::channel_ptr>>,
mi::ordered_non_unique<mi::tag<tag_outstanding>,
mi::member<peer_score, uint64_t, &peer_score::outstanding>>>>;
// clang-format on
ordered_scoring scoring;
// clang-format on
ordered_scoring scoring;

std::deque<std::shared_ptr<nano::transport::channel>> channels;
};
}
std::deque<std::shared_ptr<nano::transport::channel>> channels;
};
}

0 comments on commit c3b59f2

Please sign in to comment.