chain: fix requesting known blocks

This commit is contained in:
Fedor Indutny 2014-05-02 15:00:31 +04:00
parent c3239b7f36
commit b3c6eb06bc
2 changed files with 17 additions and 11 deletions

View File

@ -49,7 +49,7 @@ Chain.prototype.add = function add(block) {
var last = (this.hashes.length && pos > 0) ? this.hashes[pos - 1] : null;
// Add orphan
if (last && prev !== last) {
if (last && prev !== last && !this.bloom.test(rhash)) {
if (!this.bloom.test(rhash) && !this.orphan.map[prev]) {
this.orphan.count++;
this.orphan.map[prev] = block;
@ -59,16 +59,19 @@ Chain.prototype.add = function add(block) {
break;
}
this.bloom.add(rhash);
// Sorted insert
var pos = utils.binaryInsert(this.ts, block.ts, function(a, b) {
return a - b;
}, true);
var hash = block.hash('hex');
this.ts.splice(pos, 0, block.ts);
this.hashes.splice(pos, 0, hash);
// It may be a re-requested block
if (!this.bloom.test(rhash)) {
// Sorted insert
var pos = utils.binaryInsert(this.ts, block.ts, function(a, b) {
return a - b;
}, true);
this.ts.splice(pos, 0, block.ts);
this.hashes.splice(pos, 0, hash);
this.bloom.add(rhash);
}
// Blocks is a FIFO queue, acting like a cache for the requests
this.blocks.push(block);
@ -117,6 +120,8 @@ Chain.prototype.getLast = function getLast() {
};
Chain.prototype.has = function has(hash) {
if (!Array.isArray(hash))
hash = utils.toArray(hash, 'hex');
return this.bloom.test(hash) && !this.requests[utils.toHex(hash)];
};
@ -128,11 +133,11 @@ Chain.prototype.get = function get(hash, cb) {
if (this.blocks[i].hash('hex') === hash)
return cb(this.blocks[i]);
this.emit('missing', hash);
if (this.requests[hash])
this.requests[hash].push(cb);
else
this.requests[hash] = [ cb ];
this.emit('missing', hash);
};
Chain.prototype.isFull = function isFull() {

View File

@ -26,6 +26,7 @@
"hash.js": "^0.2.0"
},
"devDependencies": {
"browserify": "^3.44.2",
"mocha": "^1.18.2"
}
}