diff --git a/lib/services/block/index.js b/lib/services/block/index.js index 93b883c4..74c47434 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -56,13 +56,20 @@ BlockService.prototype.getTip = function() { BlockService.prototype.getBlock = function(arg, callback) { - var hash = this._getHash(arg); + var self = this; + self._getHash(arg, function(err, hash) { - if (!hash) { - return callback(); - } + if (err) { + return callback(err); + } + + if (!hash) { + return callback(); + } + + self._getBlock(hash, callback); + }); - this._getBlock(hash, callback); }; BlockService.prototype.getBlockOverview = function(hash, callback) { diff --git a/lib/services/fee/index.js b/lib/services/fee/index.js index 895784f0..45d0b7b8 100644 --- a/lib/services/fee/index.js +++ b/lib/services/fee/index.js @@ -39,11 +39,16 @@ FeeService.prototype.getAPIMethods = function() { }; FeeService.prototype.estimateFee = function(blocks, callback) { - this._client.estimateFee(blocks || 4, callback); + this._client.estimateFee(blocks || 4, function(err, res) { + if (err) { + return callback(err); + } + if (!res) { + callback(); + } + callback(null, res.result); + }); }; - - - module.exports = FeeService; diff --git a/lib/services/header/index.js b/lib/services/header/index.js index e5853b5e..fe177d82 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -24,7 +24,7 @@ var HeaderService = function(options) { this.subscriptions = {}; this.subscriptions.block = []; - this._checkpoint = options.checkpoint || 2000; + this._checkpoint = options.checkpoint || 2000; // set to -1 to resync all headers. this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.network]; this._lastHeader = null; this.blockServiceSyncing = true; @@ -61,6 +61,7 @@ HeaderService.prototype.getAPIMethods = function() { var methods = [ ['getAllHeaders', this, this.getAllHeaders, 0], ['getBestHeight', this, this.getBestHeight, 0], + ['getInfo', this, this.getInfo, 0], ['getBlockHeader', this, this.getBlockHeader, 1] ]; @@ -68,6 +69,27 @@ HeaderService.prototype.getAPIMethods = function() { }; +HeaderService.prototype.getCurrentDifficulty = function() { + var target = bcoin.mining.common.getTarget(this._lastHeader.bits); + return bcoin.mining.common.getDifficulty(target); +}; + +HeaderService.prototype.getInfo = function(callback) { + callback(null, { + blocks: this._lastHeader.height, + connections: this._p2p.getNumberOfPeers(), + timeoffset: 0, + proxy: '', + testnet: this.node.network === 'livenet' ? false: true, + errors: '', + network: this.node.network, + relayFee: 0, + version: 'bitcore-1.1.2', + protocolversion: 700001, + difficulty: this.getCurrentDifficulty() + }); +}; + HeaderService.prototype.getAllHeaders = function(callback) { var self = this; @@ -195,7 +217,6 @@ HeaderService.prototype.stop = function(callback) { HeaderService.prototype._startHeaderSubscription = function() { - this._bus.on('p2p/headers', this._onHeaders.bind(this)); this._bus.subscribe('p2p/headers'); @@ -524,7 +545,7 @@ HeaderService.prototype._onBestHeight = function(height) { HeaderService.prototype._startSync = function() { - this._numNeeded = Math.max(this._bestHeight - this._tip.height, this._checkpoint); + this._numNeeded = this._bestHeight - this._tip.height; log.info('Header Service: Gathering: ' + this._numNeeded + ' ' + 'header(s) from the peer-to-peer network.'); @@ -594,7 +615,12 @@ HeaderService.prototype._getLastHeader = function(callback) { var self = this; - if (self._tip.height > self._checkpoint) { + // redo all headers + if (this._checkpoint === -1) { + this._checkpoint = this._tip.height; + } + + if (self._tip.height >= self._checkpoint) { self._tip.height -= self._checkpoint; } diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index edaecfb0..6c414451 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -23,6 +23,7 @@ var P2P = function(options) { this._initPubSub(); this._bcoin = null; this._currentBestHeight = null; + this._latestBits = 0x1d00ffff; }; util.inherits(P2P, BaseService); @@ -38,13 +39,16 @@ P2P.prototype.getAPIMethods = function() { ['clearInventoryCache', this, this.clearInventoryCache, 0], ['getBlocks', this, this.getBlocks, 1], ['getHeaders', this, this.getHeaders, 1], - ['getInfo', this, this.getInfo, 0], ['getMempool', this, this.getMempool, 0], ['sendTransaction', this, this.sendTransaction, 1] ]; return methods; }; +P2P.prototype.getNumberOfPeers = function() { + return this._pool.numberConnected; +}; + P2P.prototype.getBlocks = function(filter) { var peer = this._getPeer(); @@ -61,13 +65,6 @@ P2P.prototype.getHeaders = function(filter) { }; -P2P.prototype.getInfo = function(callback) { - callback(null, { - blocks: this._getBestHeight(), - connections: this._pool.numberConnected - }); -}; - P2P.prototype.getMempool = function(filter) { var peer = this._getPeer(); diff --git a/lib/services/timestamp/index.js b/lib/services/timestamp/index.js index 48612896..184feedd 100644 --- a/lib/services/timestamp/index.js +++ b/lib/services/timestamp/index.js @@ -36,7 +36,6 @@ TimestampService.prototype.getBlockHashesByTimestamp = function(high, low, callb var self = this; var result = []; - var lastEntry; var start = self._encoding.encodeTimestampBlockKey(low); var end = self._encoding.encodeTimestampBlockKey(high); @@ -50,9 +49,7 @@ TimestampService.prototype.getBlockHashesByTimestamp = function(high, low, callb tsStream.on('data', function(data) { var value = self._encoding.decodeTimestampBlockValue(data.value); - console.log(value); result.push(value); - lastEntry = value; }); var streamErr; @@ -70,7 +67,7 @@ TimestampService.prototype.getBlockHashesByTimestamp = function(high, low, callb return callback(); } - return callback(null, result.push(lastEntry)); + return callback(null, result); });