add /v1/node api call

This commit is contained in:
Manuel Araoz 2015-04-27 15:22:29 -03:00
parent 7ef9fee1ff
commit a284edbdba
5 changed files with 35 additions and 5 deletions

View File

@ -2,6 +2,7 @@
var chai = require('chai');
var should = chai.should();
var sinon = require('sinon');
var EventEmitter = require('eventemitter2').EventEmitter2;
@ -19,6 +20,7 @@ describe('BitcoreHTTP', function() {
var nodeMock;
beforeEach(function() {
nodeMock = new EventEmitter();
nodeMock.start = sinon.spy();
});
describe('instantiates', function() {
it('from constructor', function() {

View File

@ -18,7 +18,7 @@ describe('BitcoreHTTP v1 node routes', function() {
nodeMock = new EventEmitter();
nodeMock.status = {
sync: 0.75,
peer_count: 8,
peerCount: 8,
version: 'test'
};
nodeMock.getStatus = function() {

View File

@ -159,7 +159,7 @@ BlockChain.prototype.getBlockLocator = function() {
$.checkState(!_.isUndefined(this.height[this.tip]));
var result = [];
var currentHeight = this.height[this.tip];
var currentHeight = this.getCurrentHeight();
var exponentialBackOff = 1;
for (var i = 0; i < 10; i++) {
if (currentHeight >= 0) {
@ -174,4 +174,8 @@ BlockChain.prototype.getBlockLocator = function() {
return result;
};
BlockChain.prototype.getCurrentHeight = function() {
return this.height[this.tip];
};
module.exports = BlockChain;

View File

@ -38,7 +38,10 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
var self = this;
peer.on('ready', function() {
self.emit('ready');
self.emit('ready', self.maxHeight);
});
peer.on('version', function(m) {
self.maxHeight = m.startHeight;
});
peer.on('inv', function(m) {
self.emit('inv', m.inventory);
@ -86,6 +89,11 @@ NetworkMonitor.prototype.stop = function(reason) {
console.log('Stopping network, reason:', reason);
};
NetworkMonitor.prototype.getConnectedPeers = function() {
// TODO: update when using Pool instead of Peer
return this.peer.status === Peer.STATUS.READY ? 1 : 0;
};
NetworkMonitor.prototype.abort = function(reason) {
this.peer.disconnect();
if (reason) {

View File

@ -2,6 +2,7 @@
var util = require('util');
var EventEmitter = require('eventemitter2').EventEmitter2;
var pjson = require('../package.json');
var bitcore = require('bitcore');
var _ = bitcore.deps._;
@ -78,7 +79,8 @@ BitcoreNode.prototype.initialize = function() {
var prevHeight = 0;
var statTimer = 5 * 1000;
setInterval(function() {
console.log(process.memoryUsage().heapTotal / 1024 / 1024, 'mb used');
console.log('MB used:', process.memoryUsage().heapTotal / 1024 / 1024,
100 * self.getSyncProgress() + '% synced');
if (!self.blockchain) {
// not ready yet
console.log('No blockchain yet');
@ -160,6 +162,19 @@ BitcoreNode.prototype.stop = function(reason) {
this.networkMonitor.abort(reason);
};
BitcoreNode.prototype.getStatus = function() {
return Promise.resolve({
sync: this.getSyncProgress(),
peerCount: this.networkMonitor.getConnectedPeers(),
version: pjson.version,
});
};
BitcoreNode.prototype.getSyncProgress = function() {
return !_.isUndefined(this.reportedMaxHeight) ?
(this.blockchain.getCurrentHeight() / this.reportedMaxHeight) : 0;
};
BitcoreNode.prototype._requestFromTip = function() {
var locator = this.blockchain.getBlockLocator();
//console.log('requesting blocks, locator size:', locator.length);
@ -168,7 +183,8 @@ BitcoreNode.prototype._requestFromTip = function() {
BitcoreNode.prototype.sync = function() {
var self = this;
this.networkMonitor.on('ready', function() {
this.networkMonitor.on('ready', function(reportedMaxHeight) {
self.reportedMaxHeight = reportedMaxHeight;
self._requestFromTip();
});
};