Skip to content

Commit

Permalink
Limit max lazy bootstrap blocks size if legacy bootstrap is enabled
Browse files Browse the repository at this point in the history
1M lazy blocks limit
  • Loading branch information
SergiySW committed Nov 7, 2019
1 parent ca6e4f1 commit 52dee5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 22 additions & 4 deletions nano/node/bootstrap/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ constexpr std::chrono::seconds nano::bootstrap_limits::lazy_flush_delay_sec;
constexpr unsigned nano::bootstrap_limits::lazy_destinations_request_limit;
constexpr uint64_t nano::bootstrap_limits::lazy_batch_pull_count_resize_blocks_limit;
constexpr double nano::bootstrap_limits::lazy_batch_pull_count_resize_ratio;
constexpr size_t nano::bootstrap_limits::lazy_blocks_restart_limit;
constexpr std::chrono::hours nano::bootstrap_excluded_peers::exclude_time_hours;
constexpr std::chrono::hours nano::bootstrap_excluded_peers::exclude_remove_hours;

Expand Down Expand Up @@ -890,10 +891,27 @@ bool nano::bootstrap_attempt::lazy_finished ()
return result;
}

bool nano::bootstrap_attempt::lazy_has_expired () const
{
bool result (false);
// Max 30 minutes run with enabled legacy bootstrap
static std::chrono::minutes const max_lazy_time (node->flags.disable_legacy_bootstrap ? 7 * 24 * 60 : 30);
if (std::chrono::steady_clock::now () - lazy_start_time >= max_lazy_time)
{
result = true;
}
else if (!node->flags.disable_legacy_bootstrap && lazy_blocks_count > nano::bootstrap_limits::lazy_blocks_restart_limit)
{
result = true;
}
return result;
}

void nano::bootstrap_attempt::lazy_clear ()
{
assert (!lazy_mutex.try_lock ());
lazy_blocks.clear ();
lazy_blocks_count = 0;
lazy_keys.clear ();
lazy_pulls.clear ();
lazy_state_backlog.clear ();
Expand All @@ -905,13 +923,12 @@ void nano::bootstrap_attempt::lazy_run ()
{
assert (!node->flags.disable_lazy_bootstrap);
start_populate_connections ();
auto start_time (std::chrono::steady_clock::now ());
auto max_time (std::chrono::minutes (node->flags.disable_legacy_bootstrap ? 7 * 24 * 60 : 30));
lazy_start_time = std::chrono::steady_clock::now ();
nano::unique_lock<std::mutex> lock (mutex);
while ((still_pulling () || !lazy_finished ()) && std::chrono::steady_clock::now () - start_time < max_time)
while ((still_pulling () || !lazy_finished ()) && !lazy_has_expired ())
{
unsigned iterations (0);
while (still_pulling () && std::chrono::steady_clock::now () - start_time < max_time)
while (still_pulling () && !lazy_has_expired ())
{
if (!pulls.empty ())
{
Expand Down Expand Up @@ -1030,6 +1047,7 @@ bool nano::bootstrap_attempt::process_block_lazy (std::shared_ptr<nano::block> b
}
}
lazy_blocks.insert (hash);
++lazy_blocks_count;
// Adding lazy balances for first processed block in pull
if (pull_blocks == 0 && (block_a->type () == nano::block_type::state || block_a->type () == nano::block_type::send))
{
Expand Down
4 changes: 4 additions & 0 deletions nano/node/bootstrap/bootstrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class bootstrap_attempt final : public std::enable_shared_from_this<bootstrap_at
void lazy_add (nano::hash_or_account const &, unsigned = std::numeric_limits<unsigned>::max ());
void lazy_requeue (nano::block_hash const &, nano::block_hash const &, bool);
bool lazy_finished ();
bool lazy_has_expired () const;
void lazy_pull_flush ();
void lazy_clear ();
bool process_block_lazy (std::shared_ptr<nano::block>, nano::account const &, uint64_t, nano::bulk_pull::count_t, unsigned);
Expand Down Expand Up @@ -139,6 +140,7 @@ class bootstrap_attempt final : public std::enable_shared_from_this<bootstrap_at
std::unordered_map<nano::block_hash, nano::uint128_t> lazy_balances;
std::unordered_set<nano::block_hash> lazy_keys;
std::deque<std::pair<nano::hash_or_account, unsigned>> lazy_pulls;
std::chrono::steady_clock::time_point lazy_start_time;
std::chrono::steady_clock::time_point last_lazy_flush{ std::chrono::steady_clock::now () };
class account_tag
{
Expand All @@ -152,6 +154,7 @@ class bootstrap_attempt final : public std::enable_shared_from_this<bootstrap_at
boost::multi_index::ordered_non_unique<boost::multi_index::tag<count_tag>, boost::multi_index::member<lazy_destinations_item, uint64_t, &lazy_destinations_item::count>, std::greater<uint64_t>>,
boost::multi_index::hashed_unique<boost::multi_index::tag<account_tag>, boost::multi_index::member<lazy_destinations_item, nano::account, &lazy_destinations_item::account>>>>
lazy_destinations;
std::atomic<size_t> lazy_blocks_count{ 0 };
std::atomic<bool> lazy_destinations_flushed{ false };
std::mutex lazy_mutex;
// Wallet lazy bootstrap
Expand Down Expand Up @@ -284,5 +287,6 @@ class bootstrap_limits final
static constexpr unsigned lazy_destinations_request_limit = 256 * 1024;
static constexpr uint64_t lazy_batch_pull_count_resize_blocks_limit = 4 * 1024 * 1024;
static constexpr double lazy_batch_pull_count_resize_ratio = 2.0;
static constexpr size_t lazy_blocks_restart_limit = 1024 * 1024;
};
}

0 comments on commit 52dee5e

Please sign in to comment.