alanmcgovern / monotorrent

The official repository for MonoTorrent, a bittorrent library for .NET
https://github.com/alanmcgovern/monotorrent
MIT License
1.16k stars 397 forks source link

Is possible to skip files hash checks? #676

Closed Kaidoz closed 3 months ago

Kaidoz commented 3 months ago

I tried to find a solution from this link: https://github.com/alanmcgovern/monotorrent/issues/136

But code of MonoTorrent has changed quite noticeably and many fields have become inaccessible/read-only.

There were attempts to work with FastResume, but it seems that I was working in wrong direction.

There is a serious lack of information on this.

When seeding torrent, there are cases when it is 100% known that the torrent files are correct and it is necessary to skip skipping files in order to start seeding torrent faster.

alanmcgovern commented 3 months ago

There are a few options.

1) Rely on EngineSettings.AutoSaveLoadFastResume to save/restore fast resume state every time your application restarts. This defaults to true so it should Just Work (tm).

2) If you're hashing a file and creating a torrent which you are immediately seeding. and want to skip hash checking, you need to construct/fake your own fastresume object.

Create it here:

https://github.com/alanmcgovern/monotorrent/blob/master/src/MonoTorrent.Client/MonoTorrent.Client/FastResume/FastResume.cs#L58-L68

Load it here: https://github.com/alanmcgovern/monotorrent/blob/master/src/MonoTorrent.Client/MonoTorrent.Client/Managers/TorrentManager.cs#L1091-L1092

An example from the tests: https://github.com/alanmcgovern/monotorrent/blob/1d2df66e3f31f811d6dc2ddf7291a98a38c59b30/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/InitialSeedingModeTest.cs#L62-L64

The engine supports partial seeding, and partial hashing, so you can download+seed a subset of a torrent without downloading/hashing the rest. This is why there are two bitfields.

The first bitfield indicates which pieces should be treated as available (set the bits to 'true'). The second bitfield indicates which pieces have not been hashed yet, and so their status is unknown. Set these bits to 'false' to indicate everything is known/hashed).

alanmcgovern commented 3 months ago

There is a serious lack of information on this.

I've never found a good documentation tool so if you have experience with getting the skeleton of documentation up and running on github pages ( https://alanmcgovern.github.io/monotorrent/articles/selective_downloading.html ) I can begin augmenting with more information. My most recent attempt at this is here ( https://github.com/alanmcgovern/monotorrent/tree/documentation-generator ) but i'm open to using any reasonable tool for the job.

Kaidoz commented 3 months ago

There are a few options.

1. Rely on `EngineSettings.AutoSaveLoadFastResume` to save/restore fast resume state every time your application restarts. This defaults to true so it should Just Work (tm).

2. If you're hashing a file and creating a torrent which you are immediately seeding. and want to skip hash checking, you need to construct/fake your own fastresume object.

Create it here:

https://github.com/alanmcgovern/monotorrent/blob/master/src/MonoTorrent.Client/MonoTorrent.Client/FastResume/FastResume.cs#L58-L68

Load it here: https://github.com/alanmcgovern/monotorrent/blob/master/src/MonoTorrent.Client/MonoTorrent.Client/Managers/TorrentManager.cs#L1091-L1092

An example from the tests:

https://github.com/alanmcgovern/monotorrent/blob/1d2df66e3f31f811d6dc2ddf7291a98a38c59b30/src/Tests/Tests.MonoTorrent.Client/MonoTorrent.Client.Modes/InitialSeedingModeTest.cs#L62-L64

The engine supports partial seeding, and partial hashing, so you can download+seed a subset of a torrent without downloading/hashing the rest. This is why there are two bitfields.

The first bitfield indicates which pieces should be treated as available (set the bits to 'true'). The second bitfield indicates which pieces have not been hashed yet, and so their status is unknown. Set these bits to 'false' to indicate everything is known/hashed).

I tried FastResume, based on the results it seemed to only work in the case of downloading, not seeding. I was shutting down correctly torrent engine and fast resume files were being created, but it kept checking for files

cache.zip

alanmcgovern commented 3 months ago

Ah - this is a bug in HybridTorrents! Thanks for sharing the cache zip - it took a few minutes to figure the issue out!

EDIT: Actually that's not it - there's probably more to it than just that :)

alanmcgovern commented 3 months ago

To test my theory - can you either construct a fastresume manually, like in the tests, and see if that allows you to seed without running a hashcheck:

// set all the pieces to 'true' to indicate they're all downloaded
 var bf = new BitField (torrentManager.Bitfield).SetAll (true); 

// set all the unhashed pieces to 'false' to indicate all pieces have been hash checked
 var unhashed = new BitField (bf).SetAll (false); 

// Load a new fastresume which tricks the engine into treating all the data as existing on disk.
 await torrentManager.LoadFastResumeAsync (new FastResume (torrentManager.InfoHashes, bf, unhashed)); 

Alternatively, can you test with a different torrent - one that has only V1 hashes, or one which has only V2 hashes. I believe the issue you're expeirencing is exclusively related to hybrid torrents, so if you can't reproduce the problem with a regular torrent, or a v2-only torrent, then i've resolved the issue.

If you reproduce it with either a V2-only or V1-only torrent i'll have to go exploring a bit more to see what the issue could be

alanmcgovern commented 3 months ago

Let me know if you have any other issues! I'll close this for now as everything appears to be fine in my own testing after the most recent fix.