sync, status and last block hash finished

This commit is contained in:
tenthirtyone 2017-08-15 03:12:23 -04:00
parent 4b71ae91ae
commit 69538704f6
3 changed files with 78 additions and 24 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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();
}
});
});