chaindb: cleanup. remove old functionality.

This commit is contained in:
Christopher Jeffrey 2016-01-21 01:40:40 -08:00
parent 92619d408e
commit dd4660eb15
3 changed files with 55 additions and 103 deletions

View File

@ -435,7 +435,7 @@ Block.prototype.verifyContext = function verifyContext() {
}
// BIP30 - Ensure there are no duplicate txids
if (this.chain.index[tx.hash('hex')]) {
if (this.chain[tx.hash('hex')]) {
// Blocks 91842 and 91880 created duplicate
// txids by using the same exact output script
// and extraNonce.

View File

@ -43,11 +43,7 @@ function Chain(options) {
size: 0
};
this.index = {
heights: {},
count: 0,
lastTs: 0
};
this.heightLookup = {};
this.request = new utils.RequestCache();
@ -62,9 +58,6 @@ function Chain(options) {
height: 0
}), true);
// Last TS after preload, needed for fill percent
this.index.lastTs = this.tip.ts;
Chain.global = this;
this.loading = false;
@ -108,7 +101,7 @@ Chain.prototype._init = function _init() {
var count = self.db.count();
var i, entry;
for (i = 1; i < self.index.count; i++)
for (i = 1; i < count; i++)
self._addIndex(self.db.get(i));
self.loading = false;
@ -123,8 +116,8 @@ Chain.prototype._addIndex = function _addIndex(entry, save) {
var existing;
// Already added
if (this.index.heights[entry.hash] != null) {
assert(this.index.heights[entry.hash] === entry.height);
if (this.heightLookup[entry.hash] != null) {
assert(this.heightLookup[entry.hash] === entry.height);
return Chain.codes.unchanged;
}
@ -150,8 +143,7 @@ Chain.prototype._addIndex = function _addIndex(entry, save) {
if (save)
this.db.save(entry);
this.index.heights[entry.hash] = entry.height;
this.index.count++;
this.heightLookup[entry.hash] = entry.height;
if (!this.tip || entry.height > this.tip.height) {
this.tip = entry;
@ -180,9 +172,10 @@ Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) {
Chain.prototype.resetHeight = function resetHeight(height) {
var self = this;
var count = this.db.count();
var i, existing;
assert(height < this.index.count);
assert(height < count);
// Reset the orphan map completely. There may
// have been some orphans on a forked chain we
@ -192,25 +185,15 @@ Chain.prototype.resetHeight = function resetHeight(height) {
this.orphan.count = 0;
this.orphan.size = 0;
for (i = height + 1; height < this.index.count; i++) {
for (i = height + 1; height < count; i++) {
existing = this.db.get(i);
assert(existing);
delete this.index.heights[existing.hash];
delete this.heightLookup[existing.hash];
this.db.del(i);
}
this.tip = this.db.get(height);
this.index.count = height + 1;
this.emit('tip', this.tip);
// The lastTs is supposed to be the last ts
// after the preload, but we're not sure where
// we're resetting to. It may be lower, it may
// be higher. Reset it if necessary.
this.index.lastTs = Math.min(
this.index.lastTs,
this.tip.ts
);
};
Chain.prototype.resetTime = function resetTime(ts) {
@ -231,7 +214,7 @@ Chain.prototype.add = function add(block, peer) {
prevHash = block.prevBlock;
// Find the previous block height/index.
prevHeight = this.index.heights[prevHash];
prevHeight = this.heightLookup[prevHash];
// Validate the block we want to add.
// This is only necessary for new
@ -297,7 +280,7 @@ Chain.prototype.add = function add(block, peer) {
// there is another entry at its height)
existing = this.db.get(entry.height);
if (!existing || existing.hash !== hash) {
assert(this.index.heights[entry.hash] == null);
assert(this.heightLookup[entry.hash] == null);
// A valid block with an already existing
// height came in, that spells fork. We
@ -435,12 +418,12 @@ Chain.prototype.byHash = function byHash(hash) {
else if (hash.hash)
hash = hash.hash('hex');
return this.byHeight(this.index.heights[hash]);
return this.byHeight(this.heightLookup[hash]);
};
Chain.prototype.byTime = function byTime(ts) {
var start = 0;
var end = this.index.count;
var end = this.db.count();
var pos, delta, entry;
if (ts >= this.tip.ts)
@ -500,9 +483,7 @@ Chain.prototype.isFull = function isFull() {
};
Chain.prototype.fillPercent = function fillPercent() {
var total = (utils.now() - 40 * 60) - this.index.lastTs;
var current = this.getTip().ts - this.index.lastTs;
return Math.max(0, Math.min(current / total, 1));
return Math.min(1, this.tip.ts / (utils.now() - 40 * 60));
};
Chain.prototype.hashRange = function hashRange(start, end) {
@ -534,7 +515,7 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
}
if (typeof start === 'string') {
top = this.index.heights[start];
top = this.heightLookup[start];
if (top == null) {
// We could simply `return [start]` here,
// but there is no standardized "spacing"
@ -542,7 +523,7 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
// is our tip. This is useful for getheaders
// when not using headers-first.
hashes.push(start);
top = this.index.count - 1;
top = this.db.count() - 1;
}
} else if (typeof start === 'number') {
top = start;
@ -603,7 +584,7 @@ Chain.prototype.getNextBlock = function getNextBlock(hash) {
};
Chain.prototype.size = function size() {
return this.index.count;
return this.db.count();
};
Chain.prototype.height = function height() {
@ -675,7 +656,12 @@ Chain.prototype.retarget = function retarget(last, first) {
};
Chain.prototype.toJSON = function toJSON() {
var entries = this.index.entries;
var entries = [];
var count = this.db.count();
var i;
for (i = 0; i < count; i++)
entries.push(this.db.get(i));
return {
v: 2,

View File

@ -1211,78 +1211,44 @@ Pool.prototype.search = function search(id, range, e) {
});
}
if (range.start < this.chain.index.lastTs) {
if (id)
this.watch(id);
done = function(empty) {
e.emit('end', empty);
clearInterval(timeout);
self.removeListener('block', listener);
if (id)
self.unwatch(id);
};
this.on('block', listener = function(block) {
if (block.ts >= range.end)
done();
});
// Estimated number of blocks in time range
total = (range.end - range.start) / network.powTargetSpacing | 0;
if (total === 0)
total = 1;
// 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);
this.stopSync();
if (this.peers.load)
this.peers.load.destroy();
this.startSync();
return e;
}
hashes = this.chain.hashRange(range.start, range.end);
pending = hashes.length;
if (hashes.length === 0) {
bcoin.utils.nextTick(function() {
e.emit('end', true);
});
return e;
}
if (id)
this.watch(id);
done = function() {
pending--;
assert(pending >= 0);
e.emit('progress', count - pending, count);
if (pending === 0) {
if (id)
self.unwatch(id);
e.emit('end');
}
done = function(empty) {
e.emit('end', empty);
clearInterval(timeout);
self.removeListener('block', listener);
if (id)
self.unwatch(id);
};
hashes.forEach(function(hash) {
self._request('filtered', hash, { force: true }, done);
this.on('block', listener = function(block) {
if (block.ts >= range.end)
done();
});
this._scheduleRequests();
// Estimated number of blocks in time range
total = (range.end - range.start) / network.powTargetSpacing | 0;
if (total === 0)
total = 1;
// 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);
this.stopSync();
if (this.peers.load)
this.peers.load.destroy();
this.startSync();
return e;
};