Skip to content

Commit

Permalink
BitTorrentClient related refactorings (#2279)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored Sep 1, 2024
1 parent b7bcc1e commit c6f3cb3
Show file tree
Hide file tree
Showing 47 changed files with 176 additions and 245 deletions.
2 changes: 1 addition & 1 deletion cmd/capi/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <silkworm/db/access_layer.hpp>
#include <silkworm/db/mdbx/mdbx.hpp>
#include <silkworm/db/snapshot_bundle_factory_impl.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/infra/common/directories.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/rpc/daemon.hpp>
Expand Down
2 changes: 1 addition & 1 deletion cmd/common/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#pragma once

#include <silkworm/db/snapshots/settings.hpp>
#include <silkworm/db/snapshots/snapshot_settings.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/node/settings.hpp>
#include <silkworm/rpc/settings.hpp>
Expand Down
2 changes: 1 addition & 1 deletion cmd/common/snapshot_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <CLI/CLI.hpp>

#include <silkworm/db/snapshots/settings.hpp>
#include <silkworm/db/snapshots/snapshot_settings.hpp>

namespace silkworm::cmd::common {

Expand Down
2 changes: 1 addition & 1 deletion cmd/dev/check_changes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <silkworm/db/access_layer.hpp>
#include <silkworm/db/buffer.hpp>
#include <silkworm/db/snapshot_bundle_factory_impl.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/infra/common/directories.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/concurrency/signal_handler.hpp>
Expand Down
4 changes: 2 additions & 2 deletions cmd/dev/db_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
#include <silkworm/db/mdbx/mdbx.hpp>
#include <silkworm/db/prune_mode.hpp>
#include <silkworm/db/snapshot_bundle_factory_impl.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/settings.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/db/snapshots/snapshot_settings.hpp>
#include <silkworm/db/stage_scheduler.hpp>
#include <silkworm/db/stages.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
Expand Down
23 changes: 12 additions & 11 deletions cmd/dev/snapshots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
#include <silkworm/db/snapshot_sync.hpp>
#include <silkworm/db/snapshots/bittorrent/client.hpp>
#include <silkworm/db/snapshots/bittorrent/web_seed_client.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/seg/seg_zip.hpp>
#include <silkworm/db/snapshots/snapshot_reader.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/db/transactions/txn_index.hpp>
#include <silkworm/db/transactions/txn_queries.hpp>
#include <silkworm/db/transactions/txn_to_block_index.hpp>
Expand Down Expand Up @@ -80,6 +80,7 @@ struct DownloadSettings : public bittorrent::BitTorrentSettings {
ChainId chain_id{kMainnetConfig.chain_id};
std::string url_seed;
bool download_web_seed_torrents{false};
std::optional<std::string> magnet_uri;
};

static const auto kTorrentRepoPath{bittorrent::BitTorrentSettings::kDefaultTorrentRepoPath};
Expand Down Expand Up @@ -142,8 +143,6 @@ void parse_command_line(int argc, char* argv[], CLI::App& app, SnapshotToolboxSe
auto& snapshot_settings = settings.snapshot_settings;
auto& bittorrent_settings = settings.download_settings;

bittorrent_settings.magnets_file_path = ".magnet_links";

add_logging_options(app, log_settings);

std::map<SnapshotTool, CLI::App*> commands;
Expand Down Expand Up @@ -182,9 +181,7 @@ void parse_command_line(int argc, char* argv[], CLI::App& app, SnapshotToolboxSe
add_option_chain(*cmd, bittorrent_settings.chain_id);
cmd->add_option("--torrent_dir", bittorrent_settings.repository_path, "Path to torrent file repository")
->capture_default_str();
cmd->add_option("--magnet_file",
bittorrent_settings.magnets_file_path,
"File containing magnet links to download")
cmd->add_option("--magnet", bittorrent_settings.magnet_uri, "Magnet link to download")
->capture_default_str();
cmd->add_option("--url_seed", bittorrent_settings.url_seed, "URL seed to download from")
->capture_default_str();
Expand Down Expand Up @@ -431,7 +428,7 @@ static void download_bittorrent(bittorrent::BitTorrentClient& client) {
});
std::thread scheduler_thread{[&scheduler]() { scheduler.run(); }};

client.execute_loop();
client.execution_loop();

scheduler_thread.join();
}
Expand All @@ -452,12 +449,16 @@ void download(const DownloadSettings& settings) {
}
download_bittorrent(client);
}
} else {
// Download the target files by using the magnet links contained in input file (i.e. settings.magnets_file_path)
} else if (settings.magnet_uri) {
// Download the magnet link
bittorrent::BitTorrentClient client{settings}; // NOLINT(cppcoreguidelines-slicing)
SILK_INFO << "Bittorrent async download started for magnet file: " << *settings.magnets_file_path;
SILK_INFO << "Bittorrent async download started for magnet file: " << *settings.magnet_uri;
client.add_magnet_uri(*settings.magnet_uri);
download_bittorrent(client);
SILK_INFO << "Bittorrent async download completed for magnet file: " << *settings.magnets_file_path;
SILK_INFO << "Bittorrent async download completed for magnet file: " << *settings.magnet_uri;
} else {
SILK_WARN << "No download source. Pass either --url_seed or --magnet";
return;
}

std::chrono::duration elapsed{std::chrono::steady_clock::now() - start};
Expand Down
2 changes: 1 addition & 1 deletion silkworm/capi/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <boost/asio/cancellation_signal.hpp>

#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/concurrency/context_pool_settings.hpp>
#include <silkworm/node/stagedsync/execution_engine.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <silkworm/db/blocks/headers/header_queries.hpp>
#include <silkworm/db/mdbx/bitmap.hpp>
#include <silkworm/db/receipt_cbor.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/db/tables.hpp>
#include <silkworm/db/transactions/txn_queries.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/blocks/bodies/body_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <silkworm/core/common/bytes.hpp>
#include <silkworm/db/snapshots/index_builder.hpp>
#include <silkworm/db/snapshots/path.hpp>
#include <silkworm/db/snapshots/snapshot_path.hpp>
#include <silkworm/infra/common/memory_mapped_file.hpp>

namespace silkworm::snapshots {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/blocks/headers/header_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <silkworm/core/common/bytes.hpp>
#include <silkworm/db/snapshots/index_builder.hpp>
#include <silkworm/db/snapshots/path.hpp>
#include <silkworm/db/snapshots/snapshot_path.hpp>
#include <silkworm/infra/common/memory_mapped_file.hpp>

namespace silkworm::snapshots {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/freezer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#include "blocks/headers/header_snapshot_freezer.hpp"
#include "prune_mode.hpp"
#include "snapshot_freezer.hpp"
#include "snapshots/path.hpp"
#include "snapshots/snapshot_bundle.hpp"
#include "snapshots/snapshot_path.hpp"
#include "snapshots/snapshot_writer.hpp"
#include "transactions/txn_snapshot_freezer.hpp"

Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/freezer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "data_migration.hpp"
#include "mdbx/mdbx.hpp"
#include "snapshots/repository.hpp"
#include "snapshots/snapshot_repository.hpp"
#include "stage_scheduler.hpp"

namespace silkworm::db {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/snapshot_bundle_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <silkworm/db/blocks/bodies/body_index.hpp>
#include <silkworm/db/blocks/headers/header_index.hpp>
#include <silkworm/db/snapshots/path.hpp>
#include <silkworm/db/snapshots/snapshot_path.hpp>
#include <silkworm/db/transactions/txn_index.hpp>
#include <silkworm/db/transactions/txn_to_block_index.hpp>

Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/snapshot_merger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <silkworm/infra/common/filesystem.hpp>
#include <silkworm/infra/common/log.hpp>

#include "snapshots/path.hpp"
#include "snapshots/seg/compressor.hpp"
#include "snapshots/snapshot_bundle.hpp"
#include "snapshots/snapshot_path.hpp"
#include "snapshots/snapshot_writer.hpp"

namespace silkworm::db {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/snapshot_merger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#pragma once

#include "data_migration.hpp"
#include "snapshots/repository.hpp"
#include "snapshots/snapshot_repository.hpp"

namespace silkworm::db {

Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/snapshot_recompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "blocks/bodies/body_snapshot.hpp"
#include "blocks/headers/header_snapshot.hpp"
#include "snapshots/path.hpp"
#include "snapshots/snapshot_path.hpp"
#include "transactions/txn_snapshot.hpp"

namespace silkworm::snapshots {
Expand Down
2 changes: 1 addition & 1 deletion silkworm/db/snapshot_repository_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <silkworm/db/blocks/headers/header_index.hpp>
#include <silkworm/db/snapshot_bundle_factory_impl.hpp>
#include <silkworm/db/snapshots/index_builder.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/db/test_util/temp_snapshots.hpp>
#include <silkworm/db/transactions/txn_index.hpp>
#include <silkworm/db/transactions/txn_queries.hpp>
Expand Down
18 changes: 3 additions & 15 deletions silkworm/db/snapshot_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <silkworm/db/mdbx/etl_mdbx_collector.hpp>
#include <silkworm/db/snapshots/config.hpp>
#include <silkworm/db/snapshots/index_builder.hpp>
#include <silkworm/db/snapshots/path.hpp>
#include <silkworm/db/snapshots/snapshot_path.hpp>
#include <silkworm/db/stages.hpp>
#include <silkworm/infra/common/ensure.hpp>
#include <silkworm/infra/common/environment.hpp>
Expand Down Expand Up @@ -93,18 +93,6 @@ bool SnapshotSync::download_snapshots(const std::vector<std::string>& snapshot_f
SILK_WARN << "SnapshotSync: downloading missing snapshots";
}

for (auto [block_from, block_to] : missing_block_ranges) {
for (const auto type : magic_enum::enum_values<SnapshotType>()) {
auto snapshot_path = SnapshotPath::from(repository_->path(), kSnapshotV1, block_from, block_to, type);
if (!snapshot_path.torrent_file_needed()) {
continue;
}
SILK_INFO << "SnapshotSync: seeding a new snapshot [DOING NOTHING NOW]";
// TODO(canepat) create torrent file
// client.add_torrent(snapshot_path.create_torrent_file());
}
}

const auto snapshot_config = Config::lookup_known_config(config_.chain_id, snapshot_file_names);
if (snapshot_config.preverified_snapshots().empty()) {
SILK_ERROR << "SnapshotSync: no preverified snapshots found";
Expand Down Expand Up @@ -156,9 +144,9 @@ bool SnapshotSync::download_snapshots(const std::vector<std::string>& snapshot_f
client_thread_ = std::thread([&]() {
log::set_thread_name("bit-torrent");
try {
client_.execute_loop();
client_.execution_loop();
} catch (const std::exception& ex) {
SILK_CRIT << "SnapshotSync: BitTorrentClient execute_loop exception: " << ex.what();
SILK_CRIT << "SnapshotSync: BitTorrentClient execution_loop exception: " << ex.what();
std::terminate();
}
});
Expand Down
4 changes: 2 additions & 2 deletions silkworm/db/snapshot_sync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <silkworm/core/chain/config.hpp>
#include <silkworm/db/access_layer.hpp>
#include <silkworm/db/snapshots/bittorrent/client.hpp>
#include <silkworm/db/snapshots/repository.hpp>
#include <silkworm/db/snapshots/settings.hpp>
#include <silkworm/db/snapshots/snapshot_repository.hpp>
#include <silkworm/db/snapshots/snapshot_settings.hpp>
#include <silkworm/infra/concurrency/stoppable.hpp>

namespace silkworm::db {
Expand Down
Loading

0 comments on commit c6f3cb3

Please sign in to comment.