chain: use binary search for byTime.
This commit is contained in:
parent
423b52b0b7
commit
9a575e9a26
@ -437,25 +437,32 @@ Chain.prototype.byHash = function byHash(hash) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.byTime = function byTime(ts) {
|
Chain.prototype.byTime = function byTime(ts) {
|
||||||
var i, delta, existing;
|
var start = 0;
|
||||||
var step = 1;
|
var end = this.index.count;
|
||||||
|
var pos, delta, entry;
|
||||||
|
|
||||||
if (ts >= this.tip.ts)
|
if (ts >= this.tip.ts)
|
||||||
return this.tip;
|
return this.tip;
|
||||||
|
|
||||||
for (i = this.index.count - 1; i >= 0; i -= step) {
|
// Do a binary search for a block
|
||||||
existing = this.db.get(i);
|
// mined within an hour of the
|
||||||
|
// timestamp.
|
||||||
|
while (start < end) {
|
||||||
|
pos = (start + end) >> 1;
|
||||||
|
entry = this.db.get(pos);
|
||||||
|
delta = Math.abs(ts - entry.ts);
|
||||||
|
|
||||||
if (ts >= existing.ts)
|
if (delta <= 60 * 60)
|
||||||
return existing;
|
return entry;
|
||||||
|
|
||||||
delta = existing.ts - ts;
|
if (ts < entry.ts) {
|
||||||
// If they're more than 1000 blocks apart
|
end = pos;
|
||||||
if (delta > 1000 * 60 * 10)
|
} else {
|
||||||
step *= 2;
|
start = pos + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.db.get(0);
|
return this.db.get(start);
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.hasBlock = function hasBlock(hash) {
|
Chain.prototype.hasBlock = function hasBlock(hash) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user