rpc: improve perf for getchaintips.

This commit is contained in:
Christopher Jeffrey 2016-11-11 14:02:22 -08:00
parent 3854f04624
commit ef8b205a38
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -933,40 +933,24 @@ RPC.prototype._blockToJSON = co(function* _blockToJSON(entry, block, txDetails)
});
RPC.prototype.getchaintips = co(function* getchaintips(args) {
var i, tips, orphans, prevs, result;
var orphan, entries, entry, main, fork;
var i, hash, tips, result, entry, fork, main;
if (args.help || args.length !== 0)
throw new RPCError('getchaintips');
tips = [];
orphans = [];
prevs = {};
tips = yield this.chain.db.getTips();
result = [];
entries = yield this.chain.db.getEntries();
for (i = 0; i < entries.length; i++) {
entry = entries[i];
main = yield entry.isMainChain();
if (!main) {
orphans.push(entry);
prevs[entry.prevBlock] = true;
}
}
for (i = 0; i < orphans.length; i++) {
orphan = orphans[i];
if (!prevs[orphan.hash])
tips.push(orphan);
}
tips.push(this.chain.tip);
for (i = 0; i < tips.length; i++) {
entry = tips[i];
hash = tips[i];
entry = yield this.chain.db.get(hash);
if (!entry)
continue;
fork = yield this._findFork(entry);
main = yield entry.isMainChain();
result.push({
height: entry.height,
hash: entry.rhash,