From a284edbdba5acc6af4629c6932c76500845cc3f4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 27 Apr 2015 15:22:29 -0300 Subject: [PATCH] add /v1/node api call --- api/test/http.js | 2 ++ api/test/v1/node.js | 2 +- lib/blockchain.js | 6 +++++- lib/networkmonitor.js | 10 +++++++++- lib/node.js | 20 ++++++++++++++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/api/test/http.js b/api/test/http.js index 2bcac25c..2b70bf48 100644 --- a/api/test/http.js +++ b/api/test/http.js @@ -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() { diff --git a/api/test/v1/node.js b/api/test/v1/node.js index 16a5a375..ff45b99c 100644 --- a/api/test/v1/node.js +++ b/api/test/v1/node.js @@ -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() { diff --git a/lib/blockchain.js b/lib/blockchain.js index 4de9ae4d..2ba21d43 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -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; diff --git a/lib/networkmonitor.js b/lib/networkmonitor.js index 2bc57d87..fa2bff76 100644 --- a/lib/networkmonitor.js +++ b/lib/networkmonitor.js @@ -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) { diff --git a/lib/node.js b/lib/node.js index e97c8964..14ae583d 100644 --- a/lib/node.js +++ b/lib/node.js @@ -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(); }); };