add /v1/node api call
This commit is contained in:
parent
7ef9fee1ff
commit
a284edbdba
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
var chai = require('chai');
|
var chai = require('chai');
|
||||||
var should = chai.should();
|
var should = chai.should();
|
||||||
|
var sinon = require('sinon');
|
||||||
|
|
||||||
var EventEmitter = require('eventemitter2').EventEmitter2;
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
|
||||||
@ -19,6 +20,7 @@ describe('BitcoreHTTP', function() {
|
|||||||
var nodeMock;
|
var nodeMock;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
nodeMock = new EventEmitter();
|
nodeMock = new EventEmitter();
|
||||||
|
nodeMock.start = sinon.spy();
|
||||||
});
|
});
|
||||||
describe('instantiates', function() {
|
describe('instantiates', function() {
|
||||||
it('from constructor', function() {
|
it('from constructor', function() {
|
||||||
|
|||||||
@ -18,7 +18,7 @@ describe('BitcoreHTTP v1 node routes', function() {
|
|||||||
nodeMock = new EventEmitter();
|
nodeMock = new EventEmitter();
|
||||||
nodeMock.status = {
|
nodeMock.status = {
|
||||||
sync: 0.75,
|
sync: 0.75,
|
||||||
peer_count: 8,
|
peerCount: 8,
|
||||||
version: 'test'
|
version: 'test'
|
||||||
};
|
};
|
||||||
nodeMock.getStatus = function() {
|
nodeMock.getStatus = function() {
|
||||||
|
|||||||
@ -159,7 +159,7 @@ BlockChain.prototype.getBlockLocator = function() {
|
|||||||
$.checkState(!_.isUndefined(this.height[this.tip]));
|
$.checkState(!_.isUndefined(this.height[this.tip]));
|
||||||
|
|
||||||
var result = [];
|
var result = [];
|
||||||
var currentHeight = this.height[this.tip];
|
var currentHeight = this.getCurrentHeight();
|
||||||
var exponentialBackOff = 1;
|
var exponentialBackOff = 1;
|
||||||
for (var i = 0; i < 10; i++) {
|
for (var i = 0; i < 10; i++) {
|
||||||
if (currentHeight >= 0) {
|
if (currentHeight >= 0) {
|
||||||
@ -174,4 +174,8 @@ BlockChain.prototype.getBlockLocator = function() {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BlockChain.prototype.getCurrentHeight = function() {
|
||||||
|
return this.height[this.tip];
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = BlockChain;
|
module.exports = BlockChain;
|
||||||
|
|||||||
@ -38,7 +38,10 @@ NetworkMonitor.prototype.setupPeer = function(peer) {
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
peer.on('ready', function() {
|
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) {
|
peer.on('inv', function(m) {
|
||||||
self.emit('inv', m.inventory);
|
self.emit('inv', m.inventory);
|
||||||
@ -86,6 +89,11 @@ NetworkMonitor.prototype.stop = function(reason) {
|
|||||||
console.log('Stopping network, reason:', 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) {
|
NetworkMonitor.prototype.abort = function(reason) {
|
||||||
this.peer.disconnect();
|
this.peer.disconnect();
|
||||||
if (reason) {
|
if (reason) {
|
||||||
|
|||||||
20
lib/node.js
20
lib/node.js
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var EventEmitter = require('eventemitter2').EventEmitter2;
|
var EventEmitter = require('eventemitter2').EventEmitter2;
|
||||||
|
var pjson = require('../package.json');
|
||||||
|
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var _ = bitcore.deps._;
|
var _ = bitcore.deps._;
|
||||||
@ -78,7 +79,8 @@ BitcoreNode.prototype.initialize = function() {
|
|||||||
var prevHeight = 0;
|
var prevHeight = 0;
|
||||||
var statTimer = 5 * 1000;
|
var statTimer = 5 * 1000;
|
||||||
setInterval(function() {
|
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) {
|
if (!self.blockchain) {
|
||||||
// not ready yet
|
// not ready yet
|
||||||
console.log('No blockchain yet');
|
console.log('No blockchain yet');
|
||||||
@ -160,6 +162,19 @@ BitcoreNode.prototype.stop = function(reason) {
|
|||||||
this.networkMonitor.abort(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() {
|
BitcoreNode.prototype._requestFromTip = function() {
|
||||||
var locator = this.blockchain.getBlockLocator();
|
var locator = this.blockchain.getBlockLocator();
|
||||||
//console.log('requesting blocks, locator size:', locator.length);
|
//console.log('requesting blocks, locator size:', locator.length);
|
||||||
@ -168,7 +183,8 @@ BitcoreNode.prototype._requestFromTip = function() {
|
|||||||
|
|
||||||
BitcoreNode.prototype.sync = function() {
|
BitcoreNode.prototype.sync = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.networkMonitor.on('ready', function() {
|
this.networkMonitor.on('ready', function(reportedMaxHeight) {
|
||||||
|
self.reportedMaxHeight = reportedMaxHeight;
|
||||||
self._requestFromTip();
|
self._requestFromTip();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user