From 69538704f6380ae5a3de1f403489625f17c563f7 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Tue, 15 Aug 2017 03:12:23 -0400 Subject: [PATCH] sync, status and last block hash finished --- server/lib/api/block.js | 1 - server/lib/api/index.js | 2 + server/lib/api/status.js | 99 ++++++++++++++++++++++++++++++---------- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/server/lib/api/block.js b/server/lib/api/block.js index 36e0b53..c7c9dd6 100644 --- a/server/lib/api/block.js +++ b/server/lib/api/block.js @@ -116,7 +116,6 @@ module.exports = function BlockAPI(router) { res.status(501).send(); logger.log('err', err); } - if (block[0]) { res.json({ blockHash: block[0].hash, diff --git a/server/lib/api/index.js b/server/lib/api/index.js index f97e539..0d4610e 100644 --- a/server/lib/api/index.js +++ b/server/lib/api/index.js @@ -10,10 +10,12 @@ app.use(cors); // Serve insight ui front end from root dir public folder app.use('/', express.static('./public')); app.use('/blocks', express.static('./public')); +app.use('/block-index', express.static('./public')); app.use('/blocks-date/:date', express.static('./public')); app.use('/block/:blockhash', express.static('./public')); app.use('/tx/:txid', express.static('./public')); app.use('/address/:addr', express.static('./public')); +app.use('/status', express.static('./public')); app.set('json spaces', config.api.json_spaces); diff --git a/server/lib/api/status.js b/server/lib/api/status.js index 78d2266..758e55a 100644 --- a/server/lib/api/status.js +++ b/server/lib/api/status.js @@ -1,34 +1,87 @@ +const request = require('request'); +const Block = require('../../models/block'); const pkg = require('../../package.json'); - +const config = require('../../config'); +const netCfg = require('bcoin/lib/net/common'); const logger = require('../logger'); +// Here comes the ugly. Moo who haha +function getStatus(cb) { + request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/`, (err, localRes, body) => { + if (err) { + logger.log('error', + `${err}`); + } + try { + body = JSON.parse(body); + } catch (e) { + logger.log('error', + `${err}`); + cb(e); + } + cb(null, body); + }); +} + module.exports = function statusAPI(router) { router.get('/status', (req, res) => { - res.json({ - info: { - version: 0, - protocolversion: 0, - blocks: 0, - timeoffset: 0, - connections: 0, - proxy: '', - difficulty: 0, - testnet: false, - relayfee: 0, - errors: '', - network: 'main', - }, - }); + if (req.query.q === 'getLastBlockHash') { + Block.findOne({}, { 'hash': 1 }, { sort: { 'height': -1 } }, (err, block) => { + if (err) { + logger.log('error', + `${err}`); + res.status(501).send(err); + } else { + res.send({ + syncTipHash: block.hash, + lastblockhash: block.hash, + }); + } + }); + } else { + getStatus((err, status) => { + if (err) { + res.status(501).send(err); + } else if (status) { + res.json({ + info: { + version: status.version, + protocolversion: netCfg.PROTOCOL_VERSION, + blocks: status.chain.height, + timeoffset: status.time.offset, + connections: status.pool.outbound, + proxy: '', + difficulty: 0, + testnet: status.network !== 'main', + relayfee: 0, + errors: '', + network: status.network, + }, + }); + } else { + res.send(); + } + }); + } + }); router.get('/sync', (req, res) => { - res.json({ - status: '', - blockChainHeight: 0, - syncPercentage: 0, - height: 0, - error: null, - type: 'bitcore node', + getStatus((err, status) => { + if (err) { + res.status(501).send(err); + } else if (status) { + res.json({ + status: 'syncing', + blockChainHeight: status.chain.height, + syncPercentage: Math.round(status.chain.progress * 100), + height: status.chain.height, + error: null, + type: 'bcoin node', + }); + } else { + res.send(); + } }); });