diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index fd716117..6a411249 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -1267,6 +1267,24 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call }); }; +/** + * Get all entries. + * @param {Function} callback - Returns [Error, {@link ChainEntry}[]]. + */ + +ChainDB.prototype.getEntries = function getEntries(callback) { + var self = this; + this.db.iterate({ + gte: layout.e(constants.ZERO_HASH), + lte: layout.e(constants.MAX_HASH), + values: true, + keyAsBuffer: true, + parse: function(data) { + return bcoin.chainentry.fromRaw(self.chain, data); + } + }, callback); +}; + /** * Get all transactions pertinent to an address. * @param {Base58Address|Base58Address[]} addresses diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js index 1247c26a..7abf74dd 100644 --- a/lib/bcoin/fullnode.js +++ b/lib/bcoin/fullnode.js @@ -107,7 +107,7 @@ function Fullnode(options) { proxyServer: this.options.proxyServer, preferredSeed: this.options.preferredSeed, ignoreDiscovery: this.options.ignoreDiscovery, - pool: this.options.port, + port: this.options.port, listen: this.options.listen, spv: false }); diff --git a/lib/bcoin/http/base.js b/lib/bcoin/http/base.js index 336204e8..e53436e7 100644 --- a/lib/bcoin/http/base.js +++ b/lib/bcoin/http/base.js @@ -418,11 +418,11 @@ function send(res, code, msg, type) { else if (type === 'text') res.setHeader('Content-Type', 'text/plain; charset=utf-8'); else if (type === 'json') - res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.setHeader('Content-Type', 'application/json'); else if (type === 'js') res.setHeader('Content-Type', 'application/javascript; charset=utf-8'); else if (type === 'binary') - res.setHeader('Content-Type', 'application/octet-stream; charset=utf-8'); + res.setHeader('Content-Type', 'application/octet-stream'); len = typeof msg === 'string' ? Buffer.byteLength(msg, 'utf8') diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index f84528c4..6e57f09e 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -578,10 +578,11 @@ RPC.prototype._getSoftforks = function _getSoftforks() { RPC.prototype._getBIP9Softforks = function _getBIP9Softforks(callback) { var self = this; - var forks = []; + var forks = {}; var keys = Object.keys(this.network.deployments); utils.forEachSerial(keys, function(id, next) { + var deployment = self.network.deployments[id]; self.chain.getState(self.chain.tip, id, function(err, state) { if (err) return next(err); @@ -604,10 +605,12 @@ RPC.prototype._getBIP9Softforks = function _getBIP9Softforks(callback) { break; } - forks.push({ - id: id, - state: state - }); + forks[id] = { + status: state, + bit: deployment.bit, + startTime: deployment.startTime, + timeout: deployment.timeout + }; next(); }); @@ -913,15 +916,72 @@ RPC.prototype.blockToJSON = function blockToJSON(entry, block, txDetails, callba }; RPC.prototype.getchaintips = function getchaintips(args, callback) { + var self = this; + var i, tips, orphans, prevs, result, orphan; + if (args.help || args.length !== 0) return callback(new RPCError('getchaintips')); - callback(null, [{ - height: this.chain.height, - hash: this.chain.tip.rhash, - branchlen: 0, - status: 'active' - }]); + tips = []; + orphans = []; + prevs = {}; + result = []; + + this.chain.db.getEntries(function(err, entries) { + if (err) + return callback(err); + + utils.forEachSerial(entries, function(entry, next) { + entry.isMainChain(function(err, main) { + if (err) + return next(err); + + if (!main) { + orphans.push(entry); + prevs[entry.prevBlock] = true; + } + + next(); + }); + }, function(err) { + if (err) + return callback(err); + + for (i = 0; i < orphans.length; i++) { + orphan = orphans[i]; + if (!prevs[orphan.hash]) + tips.push(orphan); + } + + tips.push(self.chain.tip); + + utils.forEachSerial(tips, function(entry, next) { + self.chain.findFork(entry, self.chain.tip, function(err, fork) { + if (err) + return next(err); + + entry.isMainChain(function(err, main) { + if (err) + return next(err); + + result.push({ + height: entry.height, + hash: entry.rhash, + branchlen: entry.height - fork.height, + status: main ? 'active' : 'valid-headers' + }); + + next(); + }); + }); + }, function(err) { + if (err) + return callback(err); + + callback(null, result); + }); + }); + }); }; RPC.prototype.getdifficulty = function getdifficulty(args, callback) { @@ -1746,7 +1806,7 @@ RPC.prototype._createRedeem = function _createRedeem(args, callback) { try { script = bcoin.script.fromMultisig(m, n, keys); } catch (e) { - return next(new RPCError('Invalid parameters.')); + return callback(new RPCError('Invalid parameters.')); } if (script.toRaw().length > constants.script.MAX_PUSH) diff --git a/lib/bcoin/protocol/packets.js b/lib/bcoin/protocol/packets.js index 5273238a..e6a4090a 100644 --- a/lib/bcoin/protocol/packets.js +++ b/lib/bcoin/protocol/packets.js @@ -218,7 +218,7 @@ VersionPacket.prototype.fromRaw = function fromRaw(data) { assert(this.version >= 0, 'Version is negative.'); assert(this.ts >= 0, 'Timestamp is negative.'); - assert(this.height >= 0, 'Height is negative.'); + // assert(this.height >= 0, 'Height is negative.'); return this; };