sqtracker icon indicating copy to clipboard operation
sqtracker copied to clipboard

Incorrect ratio tracking with multiple BitTorrent clients

Open Psmths opened this issue 1 month ago • 0 comments

I have encountered an issue with ratio tracking in the event that users are seeding from multiple BitTorrent clients. I think that this is caused by ratio for each torrent being tracked only by userId, and conflicting information between multiple clients under the same userId were causing the upload/download to increase indefinitely.

I was able to fix this issue by adding a peerId field to the schema:

const Progress = new mongoose.Schema({
  infoHash: String,
  userId: mongoose.Schema.ObjectId,
  peerId: String,
  uploaded: {
    session: Number,
    total: Number,
  },
  downloaded: {
    session: Number,
    total: Number,
  },
  left: Number,
});

And updating the announce API endpoint as follows:

const peerId = params.peer_id // Grabbing the peer ID from the announce query
const prevProgressRecord = await Progress.findOne({
    userId: user._id,
    peerId: peerId,
    infoHash,
}).lean();

...

await Progress.findOneAndUpdate(
    { userId: user._id, peerId: peerId, infoHash },
    {
      $set: {
        userId: user._id,
        infoHash,
        uploaded: {
          session: uploaded,
          total:
            (prevProgressRecord?.uploaded?.total ?? 0) + uploadDeltaSession,
        },
        left: Number(params.left),
      },
    },
    { upsert: true }
  );

Since making these changes it seems that ratio has been tracked correctly.

A possible issue with this is that the Peer ID changes when a BitTorrent client is restarted, and I believe some clients also change their Peer ID during runtime occasionally which could lead to a large amount of entries over time.

Psmths avatar May 09 '24 17:05 Psmths