From 242fbd0a597488b643f11d112c22697be43e1894 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 9 Aug 2016 21:12:53 -0700 Subject: [PATCH] rpc: optimize getchaintips. --- lib/bcoin/http/rpc.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index 6e57f09e..67a45ef5 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -956,7 +956,7 @@ RPC.prototype.getchaintips = function getchaintips(args, callback) { tips.push(self.chain.tip); utils.forEachSerial(tips, function(entry, next) { - self.chain.findFork(entry, self.chain.tip, function(err, fork) { + self._findFork(entry, function(err, fork) { if (err) return next(err); @@ -984,6 +984,20 @@ RPC.prototype.getchaintips = function getchaintips(args, callback) { }); }; +RPC.prototype._findFork = function _findFork(entry, callback) { + (function next(err, entry) { + entry.isMainChain(function(err, main) { + if (err) + return callback(err); + + if (main) + return callback(null, entry); + + entry.getPrevious(next); + }); + })(null, entry); +}; + RPC.prototype.getdifficulty = function getdifficulty(args, callback) { if (args.help || args.length !== 0) return callback(new RPCError('getdifficulty'));