Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous hashing #250

Merged
merged 6 commits into from Jan 21, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

continuous hashing

  • Loading branch information
astro committed Jan 21, 2015
commit f6884b79c907fc0afb3901c247f8fd30ae07ab29
@@ -451,18 +451,22 @@ function drawTorrent (torrent) {
}
var bar = ''
for (var j = 0; j < piece.blocks.length; j++) {
switch(piece.blocks[j]) {
case 0:
bar += '{red:█}';
break;
case 1:
bar += '{blue:█}';
break;
case 2:
if (j < piece.blocksHashed) {
bar += '{green:█}';
break;
default:
throw 'Invalid block state: ' + piece.blocks[j]
} else {
switch(piece.blocks[j]) {
case 0:
bar += '{red:█}';
break;
case 1:
bar += '{yellow:█}';
break;
case 2:
bar += '{blue:█}';
break;
default:
throw 'Invalid block state: ' + piece.blocks[j]
}
}
}
clivas.line('{4+cyan:' + i + '} ' + bar);
@@ -0,0 +1,28 @@
var through2 = require('through2')
var crypto = require('crypto')


// encapsulated a crypto stream in order to:
// * lazily instantiate the underlying implementation
// * move to webworkers later on
module.exports = function SHA1 () {
var hash
function spawnOnDemand () {
if (!hash)
hash = crypto.createHash('sha1')
}

var self = through2(function (buffer, enc, callback) {
spawnOnDemand()
hash.update(buffer)
callback()
}, function (callback) {
spawnOnDemand()
var digest = hash.digest('hex')
self.hexDigest = digest
this.push(digest)
this.push(null)
callback()
})
return self
}
@@ -11,7 +11,7 @@ var FileStream = require('./file-stream')
var inherits = require('inherits')
var MultiStream = require('multistream')
var once = require('once')
var sha1 = require('simple-sha1')
var sha1 = require('./sha1')

var BLOCK_LENGTH = 16 * 1024

@@ -74,13 +74,11 @@ Piece.prototype.writeBlock = function (offset, buffer, cb) {
return cb(null)
}

debug('piece ' + self.index + ' writeBlock@' + offset + '+' + buffer.length)
buffer.copy(self.buffer, offset)
self.blocks[i] = BLOCK_WRITTEN
self.blocksWritten += 1

if (self.blocksWritten === self.blocks.length) {
self.verify()
}
self._hashNext()

cb(null)
}
@@ -123,36 +121,53 @@ Piece.prototype._reset = function () {
self.blocks = new Buffer(Math.ceil(self.length / BLOCK_LENGTH))
self.blocks.fill(0)
self.blocksWritten = 0
self.sha = sha1()
// pull any received blocks
self.sha.on('drain', self._hashNext.bind(self))
// triggered by _hashNext():
self.sha.on('end', self._onHashed.bind(self))
self.blocksHashed = 0
// put into flowing mode
// we don't provide a sink, just waiting for 'end'
self.sha.resume()
}

// called by writeBlock() once all blocks have been written
Piece.prototype.verify = function (buffer) {
var self = this
buffer = buffer || self.buffer
if (self.verified || !buffer) {
// called by writeBlock()
Piece.prototype._hashNext = function () {
if (this.verified || !this.buffer) {
return
}

if (self.noVerify) {
self.verified = true
onResult()
return
}

var expectedHash
sha1(buffer, function (_expectedHash) {
expectedHash = _expectedHash
self.verified = (expectedHash === self.hash)
onResult()
})

function onResult () {
if (self.verified) {
self.emit('done')
debug('piece ' + this.index + ' _hashNext: blocksHashed=' + this.blocksHashed + '/' + this.blocks.length)
if (this.blocks[this.blocksHashed] === BLOCK_WRITTEN) {
debug('piece ' + this.index + ' _hashNext: write')
var block = this.buffer.slice(this.blocksHashed * BLOCK_LENGTH, (this.blocksHashed + 1) * BLOCK_LENGTH)
this.blocksHashed += 1
if (this.sha.write(block)) {
debug('piece ' + this.index + ' _hashNext: more')
// recurse
process.nextTick(this._hashNext.bind(this))
} else {
self.emit('warning', new Error('piece ' + self.index + ' failed verification; ' + expectedHash + ' expected ' + self.hash))
self._reset()
debug('piece ' + this.index + ' _hashNext: wait for sha readable')
}
} else if (this.blocksHashed >= this.blocks.length) {
// will trigger 'done' event
debug('piece ' + this.index + ' _hashNext: end')
this.sha.end()
} else {
debug('piece ' + this.index + ' _hashNext: wait more data')
}
}

Piece.prototype._onHashed = function () {
debug('piece ' + this.index + ' verified: ' + this.sha.hexDigest + ' == ' + this.hash)
this.verified = (this.sha.hexDigest === this.hash)

if (this.verified) {
this.emit('done')
} else {
this.emit('warning', new Error('piece ' + this.index + ' failed verification; ' + this.sha.hexDigest + ' expected ' + this.hash))
this._reset()
}
}

@@ -19,7 +19,8 @@
"bittorrent-swarm": "webtorrent-swarm",
"load-ip-set": false,
"simple-get": false,
"ut_pex": false
"ut_pex": false,
"crypto": "crypto-browserify"
},
"browserify": {
"transform": [
@@ -61,7 +62,6 @@
"rimraf": "^2.2.5",
"run-parallel": "^1.0.0",
"simple-get": "^1.0.0",
"simple-sha1": "^2.0.0",
"speedometer": "^0.1.2",
"torrent-discovery": "^2.0.1",
"ut_metadata": "^2.1.0",
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.