From 1aee4bb30f894934046cb6624ec498c7bfca192a Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 28 Nov 2026 11:49:56 -0500 Subject: [PATCH] verify completed torrent --- libi2pd_client/TorrentsTunnel.cpp | 64 +++++++++++++++++++++++++------ libi2pd_client/TorrentsTunnel.h | 8 +++- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index 89e30c4f..c396636c 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -365,6 +365,42 @@ namespace torrents }); } + void TorrentsTunnel::VerifyAndCompleteTorrent (std::shared_ptr torrent) + { + if (!torrent || torrent->IsComplete ()) return; + boost::asio::post (GetDiskIOService (), [this, torrent]() + { + // we start verification from disk I/O thread to make sure all pending pieces get written + if (!StartVerifyingTorrent (torrent, false, [this, torrent](bool success) + { + boost::asio::post (GetService (), [this, torrent, success]() + { + if (success) + { + LogPrint (eLogDebug, "TorrentsTunnels: Torrent verified ", torrent->GetName ()); + CompleteTorrent (torrent); + } + else + { + LogPrint (eLogWarning, "TorrentsTunnels: Torrent verification failed ", torrent->GetName ()); + // we must close connections to send new bitfield + auto connections = torrent->GetConnections (); + for (auto it: connections) + it->Close (); + }; + }); + })) + { + // TODO: add to queue + LogPrint (eLogInfo, "TorrentsTunnels: Can't verify torrent ", torrent->GetName ()); + boost::asio::post (GetService (), [this, torrent]() + { + CompleteTorrent (torrent); + }); + } + }); + } + std::shared_ptr TorrentsTunnel::FindTorrent (const Torrent::InfoHash& infoHash) const { std::lock_guard l(m_TorrentsMutex); @@ -719,13 +755,18 @@ namespace torrents } boost::asio::post (GetService (), [this, torrent]() { - torrent->SetStopped (false); - // inform trackers that we started - RequestTorrentTrackers (torrent, eTrackerAnnounceEventStarted); + StartTorrent (torrent); }); return true; } + void TorrentsTunnel::StartTorrent (std::shared_ptr torrent) + { + torrent->SetStopped (false); + // inform trackers that we started + RequestTorrentTrackers (torrent, eTrackerAnnounceEventStarted); + } + void TorrentsTunnel::StartAllTorrents () { std::list > torrentsToStart; @@ -737,10 +778,7 @@ namespace torrents boost::asio::post (GetService (), [this, torrentsToStart = std::move (torrentsToStart)]() { for (auto it: torrentsToStart) // TODO: announce with random intervals - { - it->SetStopped (false); - RequestTorrentTrackers (it, eTrackerAnnounceEventStarted); - } + StartTorrent (it); }); } @@ -1140,7 +1178,7 @@ namespace torrents if (it.second->UpdateStatus (ts)) { if (!it.second->IsComplete ()) - CompleteTorrent (it.second); + VerifyAndCompleteTorrent (it.second); } else UpdatePeersPerPiece (it.second); @@ -1421,18 +1459,19 @@ namespace torrents }); } - bool TorrentsTunnel::StartVerifyingTorrent (std::shared_ptr torrent, bool stopped) + bool TorrentsTunnel::StartVerifyingTorrent (std::shared_ptr torrent, + bool stopped, std::function verificationComplete) { if (!torrent || torrent->IsVerifying ()) return false; bool isVerifying = m_VerifyingTorrent.valid (); if (isVerifying && m_VerifyingTorrent.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { - (void)m_VerifyingTorrent.get (); // reap finished task, result not needed + m_VerifyingTorrent.get (); isVerifying = false; } if (!isVerifying) { - m_VerifyingTorrent = std::async (std::launch::async, [this, torrent, stopped]() + m_VerifyingTorrent = std::async (std::launch::async, [this, torrent, stopped, verificationComplete]() { torrent->SetVerifying (true); bool success = torrent->Verify (); @@ -1443,7 +1482,8 @@ namespace torrents torrent->SetStopped (false); RequestTorrentTrackers (torrent, eTrackerAnnounceEventStarted); }); - return success; + if (verificationComplete) + verificationComplete (success); }); } return !isVerifying; diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 5204f9a3..8bea6d59 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include "HTTP.h" @@ -149,7 +150,9 @@ namespace torrents int FindTorrentID (std::shared_ptr torrent) const; // 0 if not found bool CreateAndReserveFile (const std::filesystem::path& filePath, size_t reserve); void CompleteTorrent (std::shared_ptr torrent); + void VerifyAndCompleteTorrent (std::shared_ptr torrent); void StopTorrent (std::shared_ptr torrent); + void StartTorrent (std::shared_ptr torrent); bool RequestTracker (size_t trackerID, std::shared_ptr torrent, TrackerAnnounceEvent event); void RequestTorrentTrackers (std::shared_ptr torrent, TrackerAnnounceEvent event); void TrackerRequestSent (const boost::beast::error_code& ecode, size_t bytes_transferred, @@ -182,7 +185,8 @@ namespace torrents std::shared_ptr torrent, std::string_view dest, uint16_t port, uint16_t fromPort, TrackerAnnounceEvent event); - bool StartVerifyingTorrent (std::shared_ptr torrent, bool stopped); + bool StartVerifyingTorrent (std::shared_ptr torrent, bool stopped, + std::function verificationComplete = nullptr); private: @@ -200,7 +204,7 @@ namespace torrents std::unique_ptr m_DHT; bool m_Web; //verifying - std::future m_VerifyingTorrent; + std::future m_VerifyingTorrent; }; }