Skip to content

Commit

Permalink
cmd: fix BitTorrent command-line options (#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Oct 18, 2023
1 parent f207523 commit 77272b5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 55 deletions.
39 changes: 22 additions & 17 deletions cmd/common/snapshot_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,36 @@
namespace silkworm::cmd::common {

void add_snapshot_options(CLI::App& cli, snapshot::SnapshotSettings& snapshot_settings) {
cli.add_flag("--snapshots.enabled", snapshot_settings.enabled,
"Flag indicating if usage of snapshots should be enabled or disable")
cli.add_flag("--snapshots.enabled", snapshot_settings.enabled)
->description("Flag indicating if usage of snapshots should be enabled or disable")
->capture_default_str();
cli.add_flag("--snapshots.no_downloader", snapshot_settings.no_downloader,
"If set, the snapshot downloader is disabled and just already present local snapshots are used")
cli.add_flag("--snapshots.no_downloader", snapshot_settings.no_downloader)
->description("If set, the snapshot downloader is disabled and just already present local snapshots are used")
->capture_default_str();
cli.add_flag("--snapshots.repository.path", snapshot_settings.repository_dir,
"Filesystem path where snapshots will be stored")
cli.add_option("--snapshots.repository.path", snapshot_settings.repository_dir)
->description("Filesystem path where snapshots will be stored")
->capture_default_str();

// TODO(canepat) add options for the other snapshot settings and for all bittorrent settings
cli.add_flag("--torrent.verify_on_startup", snapshot_settings.bittorrent_settings.verify_on_startup,
"If set, the snapshot downloader will verify snapshots on startup."
" It will not report founded problems but just re-download broken pieces")
cli.add_option("--torrent.verify_on_startup", snapshot_settings.bittorrent_settings.verify_on_startup)
->description(
"If set, the snapshot downloader will verify snapshots on startup."
" It will not report founded problems but just re-download broken pieces")
->capture_default_str();
cli.add_flag("--torrent.download.rate", snapshot_settings.bittorrent_settings.download_rate_limit,
"Download rate limit for BitTorrent client in bytes per seconds")
cli.add_option("--torrent.download.rate", snapshot_settings.bittorrent_settings.download_rate_limit)
->description("Download rate limit for BitTorrent client in megabytes per seconds")
->capture_default_str();
cli.add_flag("--torrent.upload.rate", snapshot_settings.bittorrent_settings.upload_rate_limit,
"Upload rate limit for BitTorrent client in bytes per seconds")
cli.add_option("--torrent.upload.rate", snapshot_settings.bittorrent_settings.upload_rate_limit)
->description("Upload rate limit for BitTorrent client in megabytes per seconds")
->capture_default_str();
cli.add_flag("--torrent.download.slots", snapshot_settings.bittorrent_settings.active_downloads,
"Number of files to download in parallel."
"If network has enough seeders, then 1-3 slots are enough, otherwise please increase to 5-7"
" (too big value will slow down everything)")
cli.add_option("--torrent.download.slots", snapshot_settings.bittorrent_settings.active_downloads)
->description(
"Number of BitTorrent files to download in parallel."
" If network has enough seeders, then 1-3 slots are enough, otherwise please increase to 5-7"
" (too big value will slow down everything)")
->capture_default_str();
cli.add_flag("--torrent.warn_on_error_alerts", snapshot_settings.bittorrent_settings.warn_on_error_alerts)
->description("Flag indicating if BitTorrent errors must be logged as warnings")
->capture_default_str();
}

Expand Down
29 changes: 27 additions & 2 deletions silkworm/node/bittorrent/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <libtorrent/write_resume_data.hpp>
#include <magic_enum.hpp>

#include <silkworm/core/common/base.hpp>
#include <silkworm/core/common/util.hpp>
#include <silkworm/infra/common/ensure.hpp>
#include <silkworm/infra/common/log.hpp>
Expand Down Expand Up @@ -120,8 +121,8 @@ lt::session_params BitTorrentClient::load_or_create_session_parameters() const {
settings.set_int(lt::settings_pack::alert_mask,
lt::alert_category::error | lt::alert_category::storage |
lt::alert_category::status | lt::alert_category::performance_warning);
settings.set_int(lt::settings_pack::download_rate_limit, settings_.download_rate_limit);
settings.set_int(lt::settings_pack::upload_rate_limit, settings_.upload_rate_limit);
settings.set_int(lt::settings_pack::download_rate_limit, settings_.download_rate_limit * int(1_Mebi));
settings.set_int(lt::settings_pack::upload_rate_limit, settings_.upload_rate_limit * int(1_Mebi));
settings.set_int(lt::settings_pack::active_downloads, settings_.active_downloads);
settings.set_int(lt::settings_pack::max_out_request_queue, settings_.max_out_request_queue);
settings.set_int(lt::settings_pack::aio_threads, settings_.aio_threads);
Expand Down Expand Up @@ -351,6 +352,30 @@ bool BitTorrentClient::handle_alert(const lt::alert* alert) {
handled = true;
}

// When we receive any error alert, put it out as warning if required (there can be many)
if (settings_.warn_on_error_alerts) {
if (const auto tea = lt::alert_cast<lt::tracker_error_alert>(alert)) {
SILK_WARN << "tracker_error_alert: " << alert->message() << " [error=" << tea->error_message() << " reason=" << tea->failure_reason() << "]";
handled = true;
}
if (const auto sfa = lt::alert_cast<lt::scrape_failed_alert>(alert)) {
SILK_WARN << "scrape_failed_alert: " << alert->message() << " [error=" << sfa->error_message() << " what=" << sfa->what() << "]";
handled = true;
}
if (const auto sea = lt::alert_cast<lt::session_error_alert>(alert)) {
SILK_WARN << "session_error_alert: " << alert->message() << " [error_code=" << sea->error << " what=" << sea->what() << "]";
handled = true;
}
if (const auto pea = lt::alert_cast<lt::peer_error_alert>(alert)) {
SILK_WARN << "peer_error_alert: " << alert->message() << " [error_code=" << pea->error << " what=" << pea->what() << "]";
handled = true;
}
if (const auto tea = lt::alert_cast<lt::torrent_error_alert>(alert)) {
SILK_WARN << "torrent_error_alert: " << alert->message() << " [error_code=" << tea->error << " what=" << tea->what() << "]";
handled = true;
}
}

// When we receive any performance alert, put it out as warning
if (const auto pa = lt::alert_cast<lt::performance_alert>(alert)) {
SILK_WARN << alert->message() << " [warning_code=" << pa->warning_code << "]";
Expand Down
12 changes: 0 additions & 12 deletions silkworm/node/bittorrent/client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,6 @@ static inline std::vector<char> test_resume_data() {
return resume_data;
}

TEST_CASE("BitTorrentSettings", "[silkworm][snapshot][bittorrent]") {
BitTorrentSettings settings{};
CHECK(settings.repository_path == BitTorrentSettings::kDefaultTorrentRepoPath);
CHECK(!settings.magnets_file_path);
CHECK(settings.wait_between_alert_polls == BitTorrentSettings::kDefaultWaitBetweenAlertPolls);
CHECK(settings.resume_data_save_interval == BitTorrentSettings::kDefaultResumeDataSaveInterval);
CHECK(settings.seeding == BitTorrentSettings::kDefaultSeeding);
CHECK(settings.download_rate_limit == BitTorrentSettings::kDefaultDownloadRateLimit);
CHECK(settings.upload_rate_limit == BitTorrentSettings::kDefaultUploadRateLimit);
CHECK(settings.active_downloads == BitTorrentSettings::kDefaultActiveDownloads);
}

TEST_CASE("BitTorrentClient::BitTorrentClient", "[silkworm][snapshot][bittorrent]") {
SECTION("default settings") {
CHECK_NOTHROW(BitTorrentClient{BitTorrentSettings{}});
Expand Down
39 changes: 15 additions & 24 deletions silkworm/node/bittorrent/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ namespace silkworm {
//! The settings for handling BitTorrent protocol
struct BitTorrentSettings {
inline const static std::filesystem::path kDefaultTorrentRepoPath{".torrent"};
constexpr static std::chrono::seconds kDefaultWaitBetweenAlertPolls{1};
constexpr static int kDefaultNumberOfPollsBetweenStats{30};
constexpr static std::chrono::seconds kDefaultResumeDataSaveInterval{60};
constexpr static bool kDefaultVerifyOnStartup{false};
constexpr static bool kDefaultSeeding{false};

constexpr static int kDefaultDownloadRateLimit{64 * 1024 * 1024}; // 64MiB
constexpr static int kDefaultUploadRateLimit{4 * 1024 * 1024}; // 4MiB
constexpr static int kDefaultActiveDownloads{6};
constexpr static int kDefaultMaxOutRequestQueue{6000};
constexpr static bool kDefaultAnnounceToAllTiers{true};
constexpr static int kDefaultAsyncIOThreads{32};

/* BitTorrentClient configuration settings */
//! Directory path where torrent files will be stored
Expand All @@ -45,28 +33,31 @@ struct BitTorrentSettings {
//! Path for magnet links
std::optional<std::string> magnets_file_path;

//! Time interval between two alert polling loopsErigon forks 242
std::chrono::seconds wait_between_alert_polls{kDefaultWaitBetweenAlertPolls};
//! Time interval between two alert polling loops
std::chrono::seconds wait_between_alert_polls{1};

//! The number of alert polls between two contiguous stats requests
int number_of_polls_between_stats{kDefaultNumberOfPollsBetweenStats};
int number_of_polls_between_stats{30};

//! Time interval between two resume data savings
std::chrono::seconds resume_data_save_interval{kDefaultResumeDataSaveInterval};
std::chrono::seconds resume_data_save_interval{60};

//! Flag indicating if snapshots will be verified on startup
bool verify_on_startup{kDefaultVerifyOnStartup};
bool verify_on_startup{false};

//! Flag indicating if the client should seed torrents when done or not
bool seeding{kDefaultSeeding};
bool seeding{false};

//! Flag indicating if BitTorrent failure/error alerts should be treated as warnings
bool warn_on_error_alerts{false};

/* BitTorrent protocol settings */
int download_rate_limit{kDefaultDownloadRateLimit};
int upload_rate_limit{kDefaultUploadRateLimit};
int active_downloads{kDefaultActiveDownloads};
int max_out_request_queue{kDefaultMaxOutRequestQueue};
bool announce_to_all_tiers{kDefaultAnnounceToAllTiers};
int aio_threads{kDefaultAsyncIOThreads};
int download_rate_limit{64}; // 64MiB
int upload_rate_limit{4}; // 4MiB
int active_downloads{6};
int max_out_request_queue{6000};
bool announce_to_all_tiers{true};
int aio_threads{32};
};

} // namespace silkworm

0 comments on commit 77272b5

Please sign in to comment.