chaindb: cleanup. remove old functionality.
This commit is contained in:
parent
92619d408e
commit
dd4660eb15
@ -435,7 +435,7 @@ Block.prototype.verifyContext = function verifyContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BIP30 - Ensure there are no duplicate txids
|
// 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
|
// Blocks 91842 and 91880 created duplicate
|
||||||
// txids by using the same exact output script
|
// txids by using the same exact output script
|
||||||
// and extraNonce.
|
// and extraNonce.
|
||||||
|
|||||||
@ -43,11 +43,7 @@ function Chain(options) {
|
|||||||
size: 0
|
size: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
this.index = {
|
this.heightLookup = {};
|
||||||
heights: {},
|
|
||||||
count: 0,
|
|
||||||
lastTs: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
this.request = new utils.RequestCache();
|
this.request = new utils.RequestCache();
|
||||||
|
|
||||||
@ -62,9 +58,6 @@ function Chain(options) {
|
|||||||
height: 0
|
height: 0
|
||||||
}), true);
|
}), true);
|
||||||
|
|
||||||
// Last TS after preload, needed for fill percent
|
|
||||||
this.index.lastTs = this.tip.ts;
|
|
||||||
|
|
||||||
Chain.global = this;
|
Chain.global = this;
|
||||||
|
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
@ -108,7 +101,7 @@ Chain.prototype._init = function _init() {
|
|||||||
var count = self.db.count();
|
var count = self.db.count();
|
||||||
var i, entry;
|
var i, entry;
|
||||||
|
|
||||||
for (i = 1; i < self.index.count; i++)
|
for (i = 1; i < count; i++)
|
||||||
self._addIndex(self.db.get(i));
|
self._addIndex(self.db.get(i));
|
||||||
|
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
@ -123,8 +116,8 @@ Chain.prototype._addIndex = function _addIndex(entry, save) {
|
|||||||
var existing;
|
var existing;
|
||||||
|
|
||||||
// Already added
|
// Already added
|
||||||
if (this.index.heights[entry.hash] != null) {
|
if (this.heightLookup[entry.hash] != null) {
|
||||||
assert(this.index.heights[entry.hash] === entry.height);
|
assert(this.heightLookup[entry.hash] === entry.height);
|
||||||
return Chain.codes.unchanged;
|
return Chain.codes.unchanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,8 +143,7 @@ Chain.prototype._addIndex = function _addIndex(entry, save) {
|
|||||||
if (save)
|
if (save)
|
||||||
this.db.save(entry);
|
this.db.save(entry);
|
||||||
|
|
||||||
this.index.heights[entry.hash] = entry.height;
|
this.heightLookup[entry.hash] = entry.height;
|
||||||
this.index.count++;
|
|
||||||
|
|
||||||
if (!this.tip || entry.height > this.tip.height) {
|
if (!this.tip || entry.height > this.tip.height) {
|
||||||
this.tip = entry;
|
this.tip = entry;
|
||||||
@ -180,9 +172,10 @@ Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) {
|
|||||||
|
|
||||||
Chain.prototype.resetHeight = function resetHeight(height) {
|
Chain.prototype.resetHeight = function resetHeight(height) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var count = this.db.count();
|
||||||
var i, existing;
|
var i, existing;
|
||||||
|
|
||||||
assert(height < this.index.count);
|
assert(height < count);
|
||||||
|
|
||||||
// Reset the orphan map completely. There may
|
// Reset the orphan map completely. There may
|
||||||
// have been some orphans on a forked chain we
|
// 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.count = 0;
|
||||||
this.orphan.size = 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);
|
existing = this.db.get(i);
|
||||||
assert(existing);
|
assert(existing);
|
||||||
delete this.index.heights[existing.hash];
|
delete this.heightLookup[existing.hash];
|
||||||
this.db.del(i);
|
this.db.del(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tip = this.db.get(height);
|
this.tip = this.db.get(height);
|
||||||
this.index.count = height + 1;
|
|
||||||
this.emit('tip', this.tip);
|
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) {
|
Chain.prototype.resetTime = function resetTime(ts) {
|
||||||
@ -231,7 +214,7 @@ Chain.prototype.add = function add(block, peer) {
|
|||||||
prevHash = block.prevBlock;
|
prevHash = block.prevBlock;
|
||||||
|
|
||||||
// Find the previous block height/index.
|
// Find the previous block height/index.
|
||||||
prevHeight = this.index.heights[prevHash];
|
prevHeight = this.heightLookup[prevHash];
|
||||||
|
|
||||||
// Validate the block we want to add.
|
// Validate the block we want to add.
|
||||||
// This is only necessary for new
|
// This is only necessary for new
|
||||||
@ -297,7 +280,7 @@ Chain.prototype.add = function add(block, peer) {
|
|||||||
// there is another entry at its height)
|
// there is another entry at its height)
|
||||||
existing = this.db.get(entry.height);
|
existing = this.db.get(entry.height);
|
||||||
if (!existing || existing.hash !== hash) {
|
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
|
// A valid block with an already existing
|
||||||
// height came in, that spells fork. We
|
// height came in, that spells fork. We
|
||||||
@ -435,12 +418,12 @@ Chain.prototype.byHash = function byHash(hash) {
|
|||||||
else if (hash.hash)
|
else if (hash.hash)
|
||||||
hash = hash.hash('hex');
|
hash = hash.hash('hex');
|
||||||
|
|
||||||
return this.byHeight(this.index.heights[hash]);
|
return this.byHeight(this.heightLookup[hash]);
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.byTime = function byTime(ts) {
|
Chain.prototype.byTime = function byTime(ts) {
|
||||||
var start = 0;
|
var start = 0;
|
||||||
var end = this.index.count;
|
var end = this.db.count();
|
||||||
var pos, delta, entry;
|
var pos, delta, entry;
|
||||||
|
|
||||||
if (ts >= this.tip.ts)
|
if (ts >= this.tip.ts)
|
||||||
@ -500,9 +483,7 @@ Chain.prototype.isFull = function isFull() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.fillPercent = function fillPercent() {
|
Chain.prototype.fillPercent = function fillPercent() {
|
||||||
var total = (utils.now() - 40 * 60) - this.index.lastTs;
|
return Math.min(1, this.tip.ts / (utils.now() - 40 * 60));
|
||||||
var current = this.getTip().ts - this.index.lastTs;
|
|
||||||
return Math.max(0, Math.min(current / total, 1));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.hashRange = function hashRange(start, end) {
|
Chain.prototype.hashRange = function hashRange(start, end) {
|
||||||
@ -534,7 +515,7 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof start === 'string') {
|
if (typeof start === 'string') {
|
||||||
top = this.index.heights[start];
|
top = this.heightLookup[start];
|
||||||
if (top == null) {
|
if (top == null) {
|
||||||
// We could simply `return [start]` here,
|
// We could simply `return [start]` here,
|
||||||
// but there is no standardized "spacing"
|
// but there is no standardized "spacing"
|
||||||
@ -542,7 +523,7 @@ Chain.prototype.locatorHashes = function locatorHashes(start) {
|
|||||||
// is our tip. This is useful for getheaders
|
// is our tip. This is useful for getheaders
|
||||||
// when not using headers-first.
|
// when not using headers-first.
|
||||||
hashes.push(start);
|
hashes.push(start);
|
||||||
top = this.index.count - 1;
|
top = this.db.count() - 1;
|
||||||
}
|
}
|
||||||
} else if (typeof start === 'number') {
|
} else if (typeof start === 'number') {
|
||||||
top = start;
|
top = start;
|
||||||
@ -603,7 +584,7 @@ Chain.prototype.getNextBlock = function getNextBlock(hash) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.size = function size() {
|
Chain.prototype.size = function size() {
|
||||||
return this.index.count;
|
return this.db.count();
|
||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.height = function height() {
|
Chain.prototype.height = function height() {
|
||||||
@ -675,7 +656,12 @@ Chain.prototype.retarget = function retarget(last, first) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.toJSON = function toJSON() {
|
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 {
|
return {
|
||||||
v: 2,
|
v: 2,
|
||||||
|
|||||||
@ -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)
|
if (id)
|
||||||
this.watch(id);
|
this.watch(id);
|
||||||
|
|
||||||
done = function() {
|
done = function(empty) {
|
||||||
pending--;
|
e.emit('end', empty);
|
||||||
assert(pending >= 0);
|
clearInterval(timeout);
|
||||||
e.emit('progress', count - pending, count);
|
self.removeListener('block', listener);
|
||||||
if (pending === 0) {
|
if (id)
|
||||||
if (id)
|
self.unwatch(id);
|
||||||
self.unwatch(id);
|
|
||||||
e.emit('end');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
hashes.forEach(function(hash) {
|
this.on('block', listener = function(block) {
|
||||||
self._request('filtered', hash, { force: true }, done);
|
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;
|
return e;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user