diff --git a/lib/scaffold/start.js b/lib/scaffold/start.js index 6f4b71ee..0f31ee96 100644 --- a/lib/scaffold/start.js +++ b/lib/scaffold/start.js @@ -9,8 +9,6 @@ var log = index.log; var shuttingDown = false; var fs = require('fs'); -log.debug = function() {}; - /** * This function will instantiate and start a Node, requiring the necessary service diff --git a/lib/services/block/index.js b/lib/services/block/index.js index ac54401b..d1f87003 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -110,7 +110,7 @@ BlockService.prototype._startSyncHeaders = function() { return; } - log.info('Gathering: ' + numHeadersNeeded + ' block headers from the peer-to-peer network.'); + log.debug('Gathering: ' + numHeadersNeeded + ' block headers from the peer-to-peer network.'); this._p2pHeaderCallsNeeded = numHeadersNeeded % 2000 + 1; // we may make one extra call, but this is ok this._latestHeaderHash = this._tip.hash || constants.BITCOIN_GENESIS_HASH[this.node.getNetworkName]; @@ -124,7 +124,8 @@ BlockService.prototype._syncHeaders = function() { this._p2p.getHeaders({ startHash: this._latestHeaderHash }); return; } - log,info('Header sync complete.'); + + log.info('Header sync complete.'); this._startSyncBlocks(); }; @@ -151,7 +152,7 @@ BlockService.prototype._syncBlocks = function() { return; } - log,info('Header sync complete.'); + log.info('Block sync complete.'); this._startSyncBlocks(); @@ -188,7 +189,7 @@ BlockService.prototype._startSubscriptions = function() { BlockService.prototype._onHeaders = function(headers) { - log.info('New headers received.'); + log.debug('New headers received, count: ' + headers.length); this._cacheHeaders(headers); this._latestBlockHash = headers[headers.length - 1].hash; @@ -235,7 +236,7 @@ BlockService.prototype._onBlock = function(block) { } // 2. log the reception - log.info('New block received: ' + block.hash); + log.debug('New block received: ' + block.hash); // 3. store the block for safe keeping this._cacheBlock(block); @@ -422,7 +423,7 @@ BlockService.prototype._handleReorg = function(block) { return; } - log.info('A common ancestor block was found to at hash: ' + commonAncestor + '.'); + log.warn('A common ancestor block was found to at hash: ' + commonAncestor + '.'); this._setTip(block); this._broadcast(this.subscriptions.reorg, 'block/reorg', [block, commonAncestor]); this._reorging = false; @@ -530,24 +531,26 @@ BlockService.prototype._broadcast = function(subscribers, name, entity) { } }; -BlockService.prototype._cacheHeaders = function(headers) { +BlockService.prototype._cacheHeader = function(header) { // 1. save to in-memory cache first - this._blockHeaderQueue.set(block.hash, block.header.toObject()); + this._blockHeaderQueue.set(header.hash, header.toObject()); - // 2. save in db - var operations = this._getHeaderOperations(block); + // 2. get operations + return this._getHeaderOperations(header); - this._db.batch(operations, function(err) { - if(err) { - log.error('There was an error attempting to save header for block hash: ' + block.hash); - this._db.emit('error', err); - return; - } +}; - log.debug('Success saving block hash ' + block.hash); - }); +BlockService.prototype._cacheHeaders = function(headers) { + + var self = this; + var operations = headers.map(self._cacheHeader.bind(self)); + operations = _.flatten(operations); + + log.debug('Saving: ' + headers.length + ' headers to the database.'); + self._db.batch(operations); + self._syncHeaders(); }; BlockService.prototype._cacheBlock = function(block) { @@ -640,15 +643,15 @@ BlockService.prototype._onDbError = function(err) { this.node.stop(); }; -BlockService.prototype._getHeaderOperations = function(block) { +BlockService.prototype._getHeaderOperations = function(header) { var self = this; - // block or [block] - if (_.isArray(block)) { + // header or [header] + if (_.isArray(header)) { var ops = []; - _.forEach(block, function(b) { - ops.push(self._getBlockOperations(b)); + _.forEach(header, function(h) { + ops.push(self._getBlockOperations(h)); }); return _.flatten(ops); } @@ -658,18 +661,19 @@ BlockService.prototype._getHeaderOperations = function(block) { // hash operations.push({ type: 'put', - key: self._encoding.encodeHashKey(block.hash), - value: self._encoding.encodeHeaderValue(block.header) + key: self._encoding.encodeHashKey(header.hash), + value: self._encoding.encodeHeaderValue(header) }); // height operations.push({ type: 'put', - key: self._encoding.encodeHeightKey(block.height), - value: self._encoding.encodeHeaderValue(block.header) + key: self._encoding.encodeHeightKey(header.height), + value: self._encoding.encodeHeaderValue(header) }); return operations; + }; BlockService.prototype._getBlockOperations = function(block) { diff --git a/lib/services/db/index.js b/lib/services/db/index.js index 77efa725..b5288b0c 100644 --- a/lib/services/db/index.js +++ b/lib/services/db/index.js @@ -164,7 +164,7 @@ DB.prototype.batch = function(ops, options, callback) { } if (!this._stopping) { this._store.batch(ops, opts, cb); - } else { + } else if (cb) { setImmediate(cb); } };