152 lines
3.3 KiB
JavaScript
152 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
var Common = require('./common');
|
|
|
|
function StatusController(node) {
|
|
this.node = node;
|
|
this.common = new Common({log: this.node.log});
|
|
this._block = this.node.services.block;
|
|
this._header = this.node.services.header;
|
|
this._block = this.node.services.block;
|
|
}
|
|
|
|
StatusController.prototype.show = function(req, res) {
|
|
var self = this;
|
|
var option = req.query.q;
|
|
|
|
switch(option) {
|
|
case 'getDifficulty':
|
|
this.getDifficulty(function(err, result) {
|
|
if (err) {
|
|
return self.common.handleErrors(err, res);
|
|
}
|
|
res.jsonp(result);
|
|
});
|
|
break;
|
|
case 'getLastBlockHash':
|
|
res.jsonp(this.getLastBlockHash());
|
|
break;
|
|
case 'getBestBlockHash':
|
|
this.getBestBlockHash(function(err, result) {
|
|
if (err) {
|
|
return self.common.handleErrors(err, res);
|
|
}
|
|
res.jsonp(result);
|
|
});
|
|
break;
|
|
case 'getInfo':
|
|
default:
|
|
this.getInfo(function(err, result) {
|
|
if (err) {
|
|
return self.common.handleErrors(err, res);
|
|
}
|
|
res.jsonp({
|
|
info: result
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
StatusController.prototype.getInfo = function(callback) {
|
|
this._block.getInfo(function(err, result) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
var info = {
|
|
version: result.version,
|
|
protocolversion: result.protocolVersion,
|
|
blocks: result.blocks,
|
|
timeoffset: result.timeOffset,
|
|
connections: result.connections,
|
|
proxy: result.proxy,
|
|
difficulty: result.difficulty,
|
|
testnet: result.testnet,
|
|
relayfee: result.relayFee,
|
|
errors: result.errors,
|
|
network: result.network
|
|
};
|
|
callback(null, info);
|
|
});
|
|
};
|
|
|
|
StatusController.prototype.getLastBlockHash = function() {
|
|
var hash = this._block.getTip().hash;
|
|
return {
|
|
syncTipHash: hash,
|
|
lastblockhash: hash
|
|
};
|
|
};
|
|
|
|
StatusController.prototype.getBestBlockHash = function(callback) {
|
|
this._block.getBestBlockHash(function(err, hash) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
callback(null, {
|
|
bestblockhash: hash
|
|
});
|
|
});
|
|
};
|
|
|
|
StatusController.prototype.getDifficulty = function(callback) {
|
|
this.getInfo(function(err, info) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
callback(null, {
|
|
difficulty: info.difficulty
|
|
});
|
|
});
|
|
};
|
|
|
|
StatusController.prototype.sync = function(req, res) {
|
|
var self = this;
|
|
var status = 'syncing';
|
|
|
|
this._block.isSynced(function(err, synced) {
|
|
if (err) {
|
|
return self.common.handleErrors(err, res);
|
|
}
|
|
if (synced) {
|
|
status = 'finished';
|
|
}
|
|
|
|
self._block.syncPercentage(function(err, percentage) {
|
|
if (err) {
|
|
return self.common.handleErrors(err, res);
|
|
}
|
|
var info = {
|
|
status: status,
|
|
blockChainHeight: self._block.getTip().height,
|
|
syncPercentage: Math.round(percentage),
|
|
height: self._block.getTip().height,
|
|
error: null,
|
|
type: 'bitcore node'
|
|
};
|
|
|
|
res.jsonp(info);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
// Hard coded to make insight ui happy, but not applicable
|
|
StatusController.prototype.peer = function(req, res) {
|
|
res.jsonp({
|
|
connected: true,
|
|
host: '127.0.0.1',
|
|
port: null
|
|
});
|
|
};
|
|
|
|
StatusController.prototype.version = function(req, res) {
|
|
var pjson = require('../package.json');
|
|
res.jsonp({
|
|
version: pjson.version
|
|
});
|
|
};
|
|
|
|
module.exports = StatusController;
|