chain and search fixes. options.

This commit is contained in:
Christopher Jeffrey 2016-01-04 04:31:09 -08:00
parent 0f8819c78e
commit 6758d4fe8a
3 changed files with 33 additions and 15 deletions

View File

@ -564,13 +564,13 @@ Chain.prototype.fillPercent = function fillPercent() {
Chain.prototype.hashRange = function hashRange(start, end) {
var hashes;
start = this.chain.byTime(start);
end = this.chain.byTime(end);
start = this.byTime(start);
end = this.byTime(end);
if (!start || !end)
return [];
hashes = this.chain.index.hashes.slice(start.index, end.index + 1);
hashes = this.index.hashes.slice(start.index, end.index + 1);
return hashes;
};

View File

@ -396,13 +396,13 @@ Chain.prototype.fillPercent = function fillPercent() {
Chain.prototype.hashRange = function hashRange(start, end) {
var hashes;
start = this.chain.byTime(start);
end = this.chain.byTime(end);
start = this.byTime(start);
end = this.byTime(end);
if (!start || !end)
return [];
hashes = this.chain.index.hashes.slice(start.height, end.height + 1);
hashes = this.index.hashes.slice(start.height, end.height + 1);
return hashes;
};

View File

@ -33,15 +33,22 @@ function Pool(options) {
this.options.fullNode = !!this.options.fullNode;
this.options.headers = !!this.options.headers;
this.options.multiplePeers = !!this.options.multiplePeers;
this.options.relay = this.options.relay == null
? (this.options.fullNode ? true : false)
: this.options.relay;
this.storage = this.options.storage;
this.destroyed = false;
this.size = options.size || 32;
this.parallel = options.parallel || 2000;
this.redundancy = options.redundancy || 2;
if (!this.options.fullNode) {
this.options.headers = true;
this.options.multiplePeers = true;
}
this.backoff = {
delta: options.backoffDelta || 500,
max: options.backoffMax || 5000
@ -296,7 +303,7 @@ Pool.prototype._addLoader = function _addLoader() {
this._startTimer();
};
Pool.prototype._handleHeaders = function _handleHeaders(hashes, peer) {
Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
var i, header, last, block;
assert(this.options.headers);
@ -457,10 +464,12 @@ Pool.prototype._addIndex = function _addIndex(block, peer) {
if (this.chain.hasOrphan(block)) {
// Resolve orphan chain
peer.loadBlocks(
this.chain.locatorHashes(),
this.chain.getOrphanRoot(block)
);
if (!this.options.headers) {
peer.loadBlocks(
this.chain.locatorHashes(),
this.chain.getOrphanRoot(block)
);
}
// Emit our orphan if it is new
if (!orphan)
return true;
@ -898,6 +907,7 @@ Pool.prototype.searchWallet = function(w) {
self.emit('debug', 'Wallet time: %s', new Date(ts * 1000));
});
// this.search(ts);
this.chain.resetTime(ts);
};
@ -911,9 +921,9 @@ Pool.prototype.search = function search(id, range, e) {
e = e || new EventEmitter();
// Optional id argument
if (id !== null
if ((id !== null
&& typeof id === 'object'
&& !Array.isArray(id)
&& !Array.isArray(id))
|| typeof id === 'number') {
range = id;
id = null;
@ -936,7 +946,7 @@ Pool.prototype.search = function search(id, range, e) {
if (!range.start)
range.start = utils.now() - 432000;
if (range.start < this.chain.lastTs) {
if (range.start < this.chain.index.lastTs) {
if (id)
this.watch(id);
@ -953,11 +963,19 @@ Pool.prototype.search = function search(id, range, e) {
done();
});
// Estimated number of blocks in time range
total = (range.end - range.start) / network.powTargetSpacing | 0;
if (total === 0)
total = 1;
timeout = setTimeout(done.bind(null, true), total * 10 * 1000);
// 500 blocks every 3 seconds
total = (total / 500 | 0) * 3;
// Add half the total time and convert to ms
total = (total + Math.ceil(total / 2)) * 1000;
timeout = setTimeout(done.bind(null, true), total);
this.chain.resetTime(range.start);