diff --git a/lib/torrent.js b/lib/torrent.js index 6b58f776..cceb8ba5 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -80,6 +80,7 @@ function Torrent (torrentId, opts) { self.numBlockedPeers = 0 self.files = null self.done = false + self.verifyingPieces = false self._amInterested = false self._selections = [] @@ -358,11 +359,10 @@ Torrent.prototype._onMetadata = function (metadata) { self._onWireWithMetadata(wire) }) - debug('verifying existing torrent data') - parallel(self.pieces.map(function (piece, index) { - return function (cb) { - self.store.get(index, function (err, buf) { - if (err) return cb(null) // ignore error + // Verify pieces + var verifyPiece = function (index) { + self.store.get(index, function (err, buf) { + if (!err) { sha1(buf, function (hash) { if (hash === self._hashes[index]) { if (!self.pieces[index]) return @@ -373,17 +373,23 @@ Torrent.prototype._onMetadata = function (metadata) { } else { debug('piece invalid %s', index) } - cb(null) }) - }) - } - }), function (err) { - if (err) return self._onError(err) - debug('done verifying') - self._onStore() - }) + } + // Verify next piece? + if (index + 1 < self.pieces.length) { + setImmediate(function () { verifyPiece(index + 1) }) + } else { + self.verifyingPieces = false + debug('done verifying') + } + }) + } + debug('verifying existing torrent data') + self.verifyingPieces = true + verifyPiece(0) self.emit('metadata') + self._onStore() } /**