chain: fixes
This commit is contained in:
parent
8043be6e40
commit
292da0625e
@ -51,12 +51,12 @@ function compareTs(a, b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Chain.prototype._init = function _init() {
|
Chain.prototype._init = function _init() {
|
||||||
if (!this._storage)
|
if (!this.storage)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
var self = this;
|
var self = this;
|
||||||
var s = this._storage.createReadStream({
|
var s = this.storage.createReadStream({
|
||||||
start: this._prefix,
|
start: this._prefix,
|
||||||
end: this._prefix + 'z'
|
end: this._prefix + 'z'
|
||||||
});
|
});
|
||||||
@ -177,7 +177,7 @@ Chain.prototype.add = function add(block) {
|
|||||||
|
|
||||||
var range = this._getRange(hash, block.ts, true);
|
var range = this._getRange(hash, block.ts, true);
|
||||||
var hashes = this.index.hashes.slice(range.start, range.end + 1);
|
var hashes = this.index.hashes.slice(range.start, range.end + 1);
|
||||||
this.emit('missing', prev, hashes);
|
this.emit('missing', prev, hashes, block);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,13 +234,13 @@ Chain.prototype.has = function has(hash, noProbe, cb) {
|
|||||||
}
|
}
|
||||||
if (this.loading) {
|
if (this.loading) {
|
||||||
this.once('load', function() {
|
this.once('load', function() {
|
||||||
this.has(hash, noProbe, cb);
|
this.has(hash, cb);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cb = utils.asyncify(cb);
|
cb = utils.asyncify(cb);
|
||||||
|
|
||||||
if (noProbe && this.block.bloom.test(hash, 'hex')) {
|
if (this.block.bloom.test(hash, 'hex')) {
|
||||||
if (this.strict) {
|
if (this.strict) {
|
||||||
for (var i = 0; i < this.block.list.length; i++)
|
for (var i = 0; i < this.block.list.length; i++)
|
||||||
if (this.block.list[i].hash('hex') === hash)
|
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) {
|
Chain.prototype.hasMerkle = function hasMerkle(hash) {
|
||||||
@ -285,7 +290,7 @@ Chain.prototype.get = function get(hash, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.request.add(hash, cb))
|
if (this.request.add(hash, cb))
|
||||||
this.emit('missing', hash);
|
this.emit('missing', hash, null, null);
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.isFull = function isFull() {
|
Chain.prototype.isFull = function isFull() {
|
||||||
@ -309,7 +314,7 @@ Chain.prototype.hashesInRange = function hashesInRange(start, end, cb) {
|
|||||||
start--;
|
start--;
|
||||||
end = utils.binaryInsert(ts, end, compareTs, true);
|
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) {
|
Chain.prototype.getLast = function getLast(cb) {
|
||||||
@ -387,6 +392,7 @@ Chain.prototype.fromJSON = function fromJSON(json) {
|
|||||||
if (this.index.hashes.length === 0)
|
if (this.index.hashes.length === 0)
|
||||||
this.add(new bcoin.block(constants.genesis));
|
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');
|
this.index.bloom.add(this.index.hashes[i], 'hex');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -84,7 +84,7 @@ Pool.prototype._init = function _init() {
|
|||||||
this._load();
|
this._load();
|
||||||
|
|
||||||
var self = this;
|
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._request('block', hash, { force: true });
|
||||||
self._scheduleRequests();
|
self._scheduleRequests();
|
||||||
self._loadRange(preload);
|
self._loadRange(preload);
|
||||||
@ -161,8 +161,10 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
cb(!res);
|
cb(!res);
|
||||||
});
|
});
|
||||||
}, function(allNew) {
|
}, function(allNew) {
|
||||||
if (!allNew)
|
if (!allNew) {
|
||||||
|
self.block.lastHash = null;
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Store last hash to continue global load
|
// Store last hash to continue global load
|
||||||
self.block.lastHash = hashes[hashes.length - 1];
|
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)
|
if (!hashes)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -185,7 +187,7 @@ Pool.prototype._loadRange = function _loadRange(hashes) {
|
|||||||
|
|
||||||
// Limit number of requests
|
// Limit number of requests
|
||||||
var now = +new Date;
|
var now = +new Date;
|
||||||
if (now - this.load.lastRange < this.load.rangeWindow)
|
if (!force && now - this.load.lastRange < this.load.rangeWindow)
|
||||||
return;
|
return;
|
||||||
this.load.lastRange = now;
|
this.load.lastRange = now;
|
||||||
|
|
||||||
@ -193,8 +195,9 @@ Pool.prototype._loadRange = function _loadRange(hashes) {
|
|||||||
this._addLoader();
|
this._addLoader();
|
||||||
|
|
||||||
var last = hashes[hashes.length - 1];
|
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}
|
hashes.slice(0, -1).forEach(function(hash) {
|
||||||
this.peers.load.loadBlocks([ hashes[0] ], last);
|
this.peers.load.loadBlocks([ hash ], last);
|
||||||
|
}, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype._load = function _load() {
|
Pool.prototype._load = function _load() {
|
||||||
@ -377,7 +380,7 @@ Pool.prototype.search = function search(id, range) {
|
|||||||
if (id)
|
if (id)
|
||||||
self.watch(id);
|
self.watch(id);
|
||||||
|
|
||||||
self._loadRange(hashes);
|
self._loadRange(hashes, true);
|
||||||
hashes.slice().reverse().forEach(function(hash) {
|
hashes.slice().reverse().forEach(function(hash) {
|
||||||
// Get the block that is in index
|
// Get the block that is in index
|
||||||
self.chain.get(hash, loadBlock);
|
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
|
// Block should be not in chain, or be requested
|
||||||
if (type === 'block')
|
if (type === 'block')
|
||||||
return this.chain.has(hash, options.force, next)
|
return this.chain.has(hash, true, next)
|
||||||
else
|
else
|
||||||
return next(false);
|
return next(false);
|
||||||
|
|
||||||
@ -444,8 +447,9 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
|
|||||||
Pool.prototype._response = function _response(entity) {
|
Pool.prototype._response = function _response(entity) {
|
||||||
var req = this.request.map[entity.hash('hex')];
|
var req = this.request.map[entity.hash('hex')];
|
||||||
if (!req)
|
if (!req)
|
||||||
return;
|
return false;
|
||||||
req.finish(entity);
|
req.finish(entity);
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
Pool.prototype._scheduleRequests = function _scheduleRequests() {
|
Pool.prototype._scheduleRequests = function _scheduleRequests() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user