Namespace indentation (#4817)

This commit is contained in:
Piotr Wójcik 2025-01-05 19:00:18 +01:00 committed by GitHub
commit e6a4c07bc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 207 additions and 212 deletions

View file

@ -17,145 +17,143 @@
namespace mi = boost::multi_index; 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 */ public: // Constants
class account_sets 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 nano::account account;
static double constexpr priority_initial = 2.0; double priority;
static double constexpr priority_increase = 2.0; unsigned fails;
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
class tag_sequenced {};
class tag_account {};
class tag_id {};
class tag_dependency {};
class tag_dependency_account {};
class tag_priority {};
// Tracks the ongoing account priorities
using ordered_priorities = boost::multi_index_container<priority_entry,
mi::indexed_by<
mi::sequenced<mi::tag<tag_sequenced>>,
mi::ordered_unique<mi::tag<tag_account>,
mi::member<priority_entry, nano::account, &priority_entry::account>>,
mi::ordered_non_unique<mi::tag<tag_priority>,
mi::member<priority_entry, double, &priority_entry::priority>, std::greater<>>, // Descending
mi::ordered_unique<mi::tag<tag_id>,
mi::member<priority_entry, id_t, &priority_entry::id>>
>>;
// A blocked account is an account that has failed to insert a new block because the source block is not currently present in the ledger
// An account is unblocked once it has a block successfully inserted
using ordered_blocking = boost::multi_index_container<blocking_entry,
mi::indexed_by<
mi::sequenced<mi::tag<tag_sequenced>>,
mi::ordered_unique<mi::tag<tag_account>,
mi::member<blocking_entry, nano::account, &blocking_entry::account>>,
mi::ordered_non_unique<mi::tag<tag_dependency>,
mi::member<blocking_entry, nano::block_hash, &blocking_entry::dependency>>,
mi::ordered_non_unique<mi::tag<tag_dependency_account>,
mi::member<blocking_entry, nano::account, &blocking_entry::dependency_account>>,
mi::ordered_unique<mi::tag<tag_id>,
mi::member<blocking_entry, id_t, &blocking_entry::id>>
>>;
// clang-format on
ordered_priorities priorities;
ordered_blocking blocking;
public:
using info_t = std::tuple<decltype (blocking), decltype (priorities)>; // <blocking, priorities>
info_t info () const;
}; };
}
/**
* 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 {};
class tag_dependency {};
class tag_dependency_account {};
class tag_priority {};
// Tracks the ongoing account priorities
using ordered_priorities = boost::multi_index_container<priority_entry,
mi::indexed_by<
mi::sequenced<mi::tag<tag_sequenced>>,
mi::ordered_unique<mi::tag<tag_account>,
mi::member<priority_entry, nano::account, &priority_entry::account>>,
mi::ordered_non_unique<mi::tag<tag_priority>,
mi::member<priority_entry, double, &priority_entry::priority>, std::greater<>>, // Descending
mi::ordered_unique<mi::tag<tag_id>,
mi::member<priority_entry, id_t, &priority_entry::id>>
>>;
// A blocked account is an account that has failed to insert a new block because the source block is not currently present in the ledger
// An account is unblocked once it has a block successfully inserted
using ordered_blocking = boost::multi_index_container<blocking_entry,
mi::indexed_by<
mi::sequenced<mi::tag<tag_sequenced>>,
mi::ordered_unique<mi::tag<tag_account>,
mi::member<blocking_entry, nano::account, &blocking_entry::account>>,
mi::ordered_non_unique<mi::tag<tag_dependency>,
mi::member<blocking_entry, nano::block_hash, &blocking_entry::dependency>>,
mi::ordered_non_unique<mi::tag<tag_dependency_account>,
mi::member<blocking_entry, nano::account, &blocking_entry::dependency_account>>,
mi::ordered_unique<mi::tag<tag_id>,
mi::member<blocking_entry, id_t, &blocking_entry::id>>
>>;
// clang-format on
ordered_priorities priorities;
ordered_blocking blocking;
public:
using info_t = std::tuple<decltype (blocking), decltype (priorities)>; // <blocking, priorities>
info_t info () const;
};
} }

View file

@ -13,87 +13,84 @@
namespace mi = boost::multi_index; 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 public:
class peer_scoring static nano::transport::traffic_type constexpr traffic_type = nano::transport::traffic_type::bootstrap_requests;
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);
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);
// 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;
nano::container_info container_info () const;
private:
bootstrap_config const & config;
nano::network_constants const & network_constants;
private:
class peer_score
{ {
public: public:
static nano::transport::traffic_type constexpr traffic_type = nano::transport::traffic_type::bootstrap_requests; explicit peer_score (std::shared_ptr<nano::transport::channel> const &, uint64_t, uint64_t, uint64_t);
std::weak_ptr<nano::transport::channel> channel;
public: // std::weak_ptr does not provide ordering so the naked pointer is also tracked and used for ordering channels
peer_scoring (bootstrap_config const &, nano::network_constants const &); // This pointer may be invalid if the channel has been destroyed
nano::transport::channel * channel_ptr;
// Returns true if channel limit has been exceeded // Acquire reference to the shared channel object if it is still valid
bool limit_exceeded (std::shared_ptr<nano::transport::channel> const & channel) const; [[nodiscard]] std::shared_ptr<nano::transport::channel> shared () 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 ();
// 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 ();
std::size_t size () const;
std::size_t available () const;
nano::container_info container_info () const;
private:
bootstrap_config const & config;
nano::network_constants const & network_constants;
private:
class peer_score
{ {
public: auto result = channel.lock ();
explicit peer_score (std::shared_ptr<nano::transport::channel> const &, uint64_t, uint64_t, uint64_t); if (result)
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 (); debug_assert (result.get () == channel_ptr);
if (result)
{
debug_assert (result.get () == channel_ptr);
}
return result;
} }
void decay () return result;
{ }
outstanding = outstanding > 0 ? outstanding - 1 : 0; void decay ()
} {
// Number of outstanding requests to a peer outstanding = outstanding > 0 ? outstanding - 1 : 0;
uint64_t outstanding{ 0 }; }
uint64_t request_count_total{ 0 }; // Number of outstanding requests to a peer
uint64_t response_count_total{ 0 }; uint64_t outstanding{ 0 };
}; uint64_t request_count_total{ 0 };
uint64_t response_count_total{ 0 };
// clang-format off
// Indexes scores by their shared channel pointer
class tag_channel {};
// Indexes scores by the number of outstanding requests in ascending order
class tag_outstanding {};
using ordered_scoring = boost::multi_index_container<peer_score,
mi::indexed_by<
mi::hashed_unique<mi::tag<tag_channel>,
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;
std::deque<std::shared_ptr<nano::transport::channel>> channels;
}; };
}
// clang-format off
// Indexes scores by their shared channel pointer
class tag_channel {};
// Indexes scores by the number of outstanding requests in ascending order
class tag_outstanding {};
using ordered_scoring = boost::multi_index_container<peer_score,
mi::indexed_by<
mi::hashed_unique<mi::tag<tag_channel>,
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;
std::deque<std::shared_ptr<nano::transport::channel>> channels;
};
} }