chain: fixes

This commit is contained in:
Fedor Indutny 2014-05-08 17:47:51 +04:00
parent 8043be6e40
commit 292da0625e
2 changed files with 28 additions and 18 deletions

View File

@ -51,12 +51,12 @@ function compareTs(a, b) {
}
Chain.prototype._init = function _init() {
if (!this._storage)
if (!this.storage)
return;
this.loading = true;
var self = this;
var s = this._storage.createReadStream({
var s = this.storage.createReadStream({
start: this._prefix,
end: this._prefix + 'z'
});
@ -177,7 +177,7 @@ Chain.prototype.add = function add(block) {
var range = this._getRange(hash, block.ts, true);
var hashes = this.index.hashes.slice(range.start, range.end + 1);
this.emit('missing', prev, hashes);
this.emit('missing', prev, hashes, block);
break;
}
@ -234,13 +234,13 @@ Chain.prototype.has = function has(hash, noProbe, cb) {
}
if (this.loading) {
this.once('load', function() {
this.has(hash, noProbe, cb);
this.has(hash, cb);
});
return;
}
cb = utils.asyncify(cb);
if (noProbe && this.block.bloom.test(hash, 'hex')) {
if (this.block.bloom.test(hash, 'hex')) {
if (this.strict) {
for (var i = 0; i < this.block.list.length; i++)
if (this.block.list[i].hash('hex') === hash)
@ -250,7 +250,12 @@ Chain.prototype.has = function has(hash, noProbe, cb) {
}
}
return cb(!noProbe && this._probeIndex(hash) && !!this.orphan.map[hash]);
if (!noProbe && this.index.bloom.test(hash, 'hex')) {
// XXX find hash
return true;
}
return cb(!!this.orphan.map[hash]);
};
Chain.prototype.hasMerkle = function hasMerkle(hash) {
@ -285,7 +290,7 @@ Chain.prototype.get = function get(hash, cb) {
}
if (this.request.add(hash, cb))
this.emit('missing', hash);
this.emit('missing', hash, null, null);
};
Chain.prototype.isFull = function isFull() {
@ -309,7 +314,7 @@ Chain.prototype.hashesInRange = function hashesInRange(start, end, cb) {
start--;
end = utils.binaryInsert(ts, end, compareTs, true);
return this.index.hashes.slice(start, end);
return cb(this.index.hashes.slice(start, end));
};
Chain.prototype.getLast = function getLast(cb) {
@ -387,6 +392,7 @@ Chain.prototype.fromJSON = function fromJSON(json) {
if (this.index.hashes.length === 0)
this.add(new bcoin.block(constants.genesis));
for (var i = 0; i < this.index.hashes.length; i++)
for (var i = 0; i < this.index.hashes.length; i++) {
this.index.bloom.add(this.index.hashes[i], 'hex');
}
};

View File

@ -84,7 +84,7 @@ Pool.prototype._init = function _init() {
this._load();
var self = this;
this.chain.on('missing', function(hash, preload) {
this.chain.on('missing', function(hash, preload, parent) {
self._request('block', hash, { force: true });
self._scheduleRequests();
self._loadRange(preload);
@ -161,8 +161,10 @@ Pool.prototype._addLoader = function _addLoader() {
cb(!res);
});
}, function(allNew) {
if (!allNew)
if (!allNew) {
self.block.lastHash = null;
return;
}
// Store last hash to continue global load
self.block.lastHash = hashes[hashes.length - 1];
@ -176,7 +178,7 @@ Pool.prototype._addLoader = function _addLoader() {
});
};
Pool.prototype._loadRange = function _loadRange(hashes) {
Pool.prototype._loadRange = function _loadRange(hashes, force) {
if (!hashes)
return;
@ -185,7 +187,7 @@ Pool.prototype._loadRange = function _loadRange(hashes) {
// Limit number of requests
var now = +new Date;
if (now - this.load.lastRange < this.load.rangeWindow)
if (!force && now - this.load.lastRange < this.load.rangeWindow)
return;
this.load.lastRange = now;
@ -193,8 +195,9 @@ Pool.prototype._loadRange = function _loadRange(hashes) {
this._addLoader();
var last = hashes[hashes.length - 1];
function rev(s) { var r = ''; for (var i = 0; i < s.length; i += 2) r = s.slice(i, i + 2) + r; return r}
this.peers.load.loadBlocks([ hashes[0] ], last);
hashes.slice(0, -1).forEach(function(hash) {
this.peers.load.loadBlocks([ hash ], last);
}, this);
};
Pool.prototype._load = function _load() {
@ -377,7 +380,7 @@ Pool.prototype.search = function search(id, range) {
if (id)
self.watch(id);
self._loadRange(hashes);
self._loadRange(hashes, true);
hashes.slice().reverse().forEach(function(hash) {
// Get the block that is in index
self.chain.get(hash, loadBlock);
@ -428,7 +431,7 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
// Block should be not in chain, or be requested
if (type === 'block')
return this.chain.has(hash, options.force, next)
return this.chain.has(hash, true, next)
else
return next(false);
@ -444,8 +447,9 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
Pool.prototype._response = function _response(entity) {
var req = this.request.map[entity.hash('hex')];
if (!req)
return;
return false;
req.finish(entity);
return true;
};
Pool.prototype._scheduleRequests = function _scheduleRequests() {