This commit is contained in:
Chris Kleeschulte 2017-07-05 09:34:51 -04:00
parent 54d08d57bf
commit d0ee9c18a9
3 changed files with 32 additions and 30 deletions

View File

@ -9,8 +9,6 @@ var log = index.log;
var shuttingDown = false; var shuttingDown = false;
var fs = require('fs'); var fs = require('fs');
log.debug = function() {};
/** /**
* This function will instantiate and start a Node, requiring the necessary service * This function will instantiate and start a Node, requiring the necessary service

View File

@ -110,7 +110,7 @@ BlockService.prototype._startSyncHeaders = function() {
return; 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._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]; 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 }); this._p2p.getHeaders({ startHash: this._latestHeaderHash });
return; return;
} }
log,info('Header sync complete.');
log.info('Header sync complete.');
this._startSyncBlocks(); this._startSyncBlocks();
}; };
@ -151,7 +152,7 @@ BlockService.prototype._syncBlocks = function() {
return; return;
} }
log,info('Header sync complete.'); log.info('Block sync complete.');
this._startSyncBlocks(); this._startSyncBlocks();
@ -188,7 +189,7 @@ BlockService.prototype._startSubscriptions = function() {
BlockService.prototype._onHeaders = function(headers) { BlockService.prototype._onHeaders = function(headers) {
log.info('New headers received.'); log.debug('New headers received, count: ' + headers.length);
this._cacheHeaders(headers); this._cacheHeaders(headers);
this._latestBlockHash = headers[headers.length - 1].hash; this._latestBlockHash = headers[headers.length - 1].hash;
@ -235,7 +236,7 @@ BlockService.prototype._onBlock = function(block) {
} }
// 2. log the reception // 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 // 3. store the block for safe keeping
this._cacheBlock(block); this._cacheBlock(block);
@ -422,7 +423,7 @@ BlockService.prototype._handleReorg = function(block) {
return; 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._setTip(block);
this._broadcast(this.subscriptions.reorg, 'block/reorg', [block, commonAncestor]); this._broadcast(this.subscriptions.reorg, 'block/reorg', [block, commonAncestor]);
this._reorging = false; 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 // 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 // 2. get operations
var operations = this._getHeaderOperations(block); 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) { BlockService.prototype._cacheBlock = function(block) {
@ -640,15 +643,15 @@ BlockService.prototype._onDbError = function(err) {
this.node.stop(); this.node.stop();
}; };
BlockService.prototype._getHeaderOperations = function(block) { BlockService.prototype._getHeaderOperations = function(header) {
var self = this; var self = this;
// block or [block] // header or [header]
if (_.isArray(block)) { if (_.isArray(header)) {
var ops = []; var ops = [];
_.forEach(block, function(b) { _.forEach(header, function(h) {
ops.push(self._getBlockOperations(b)); ops.push(self._getBlockOperations(h));
}); });
return _.flatten(ops); return _.flatten(ops);
} }
@ -658,18 +661,19 @@ BlockService.prototype._getHeaderOperations = function(block) {
// hash // hash
operations.push({ operations.push({
type: 'put', type: 'put',
key: self._encoding.encodeHashKey(block.hash), key: self._encoding.encodeHashKey(header.hash),
value: self._encoding.encodeHeaderValue(block.header) value: self._encoding.encodeHeaderValue(header)
}); });
// height // height
operations.push({ operations.push({
type: 'put', type: 'put',
key: self._encoding.encodeHeightKey(block.height), key: self._encoding.encodeHeightKey(header.height),
value: self._encoding.encodeHeaderValue(block.header) value: self._encoding.encodeHeaderValue(header)
}); });
return operations; return operations;
}; };
BlockService.prototype._getBlockOperations = function(block) { BlockService.prototype._getBlockOperations = function(block) {

View File

@ -164,7 +164,7 @@ DB.prototype.batch = function(ops, options, callback) {
} }
if (!this._stopping) { if (!this._stopping) {
this._store.batch(ops, opts, cb); this._store.batch(ops, opts, cb);
} else { } else if (cb) {
setImmediate(cb); setImmediate(cb);
} }
}; };