This commit is contained in:
Chris Kleeschulte 2017-07-05 10:10:12 -04:00
parent d0ee9c18a9
commit 314c37db49

View File

@ -103,6 +103,102 @@ BlockService.prototype.unsubscribe = function(name, emitter) {
};
// --- start private prototype functions
BlockService.prototype._blockAlreadyProcessed = function(block) {
return this._blockHeaderQueue.get(block.hash) ? true : false;
};
BlockService.prototype._broadcast = function(subscribers, name, entity) {
for (var i = 0; i < subscribers.length; i++) {
subscribers[i].emit(name, entity);
}
};
BlockService.prototype._cacheHeader = function(header) {
// 1. save to in-memory cache first
this._blockHeaderQueue.set(header.hash, header.toObject());
// 2. get operations
return this._getHeaderOperations(header);
};
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) {
log.debug('Setting block: "' + block.hash + '" in the block cache.');
// 1. set the block queue, which holds full blocks in memory
this._blockQueue.set(block.hash, block);
// 2. store the block in the database
var operations = this._getBlockOperations(block);
this._db.batch(operations, function(err) {
if(err) {
log.error('There was an error attempting to save block for hash: ' + block.hash);
this._db.emit('error', err);
return;
}
log.debug('Success saving block hash ' + block.hash);
});
};
BlockService.prototype._checkChain = function(tip) {
var _tip = tip;
for(var i = 0; i < this._maxLookback; i++) {
if (_tip === this._tip.hash) {
return true;
}
var prevHeader = this._blockHeaderQueue.get(_tip);
if (!prevHeader) {
return false;
}
_tip = prevHeader.prevHash;
}
return false;
};
BlockService.prototype._computeChainwork = function(bits, prev) {
var target = consensus.fromCompact(bits);
if (target.isNeg() || target.cmpn(0) === 0) {
return new BN(0);
}
var proof = BlockService.MAX_CHAINWORK.div(target.iaddn(1));
if (!prev) {
return proof;
}
return proof.iadd(prev);
};
BlockService.prototype._startSyncHeaders = function() {
var numHeadersNeeded = this._bestHeight - this._tip.height;
@ -195,12 +291,6 @@ BlockService.prototype._onHeaders = function(headers) {
};
BlockService.prototype._blockAlreadyProcessed = function(block) {
return this._blockHeaderQueue.get(block.hash) ? true : false;
};
BlockService.prototype._updateChainTips = function(block, state) {
var prevHash = utils.reverseBufferToString(block.header.prevHash);
@ -314,24 +404,6 @@ BlockService.prototype._getChainwork = function(tipHash) {
return chainwork;
};
BlockService.prototype._computeChainwork = function(bits, prev) {
var target = consensus.fromCompact(bits);
if (target.isNeg() || target.cmpn(0) === 0) {
return new BN(0);
}
var proof = BlockService.MAX_CHAINWORK.div(target.iaddn(1));
if (!prev) {
return proof;
}
return proof.iadd(prev);
};
BlockService.prototype._getDelta = function(tip) {
var blocks = [];
@ -348,25 +420,6 @@ BlockService.prototype._getDelta = function(tip) {
};
BlockService.prototype._checkChain = function(tip) {
var _tip = tip;
for(var i = 0; i < this._maxLookback; i++) {
if (_tip === this._tip.hash) {
return true;
}
var prevHeader = this._blockHeaderQueue.get(_tip);
if (!prevHeader) {
return false;
}
_tip = prevHeader.prevHash;
}
return false;
};
BlockService.prototype._sendDelta = function() {
// when this function is called, we know, for sure, that we have a complete chain of unsent block(s).
@ -525,57 +578,6 @@ BlockService.prototype._isChainReorganizing = function(block) {
};
BlockService.prototype._broadcast = function(subscribers, name, entity) {
for (var i = 0; i < subscribers.length; i++) {
subscribers[i].emit(name, entity);
}
};
BlockService.prototype._cacheHeader = function(header) {
// 1. save to in-memory cache first
this._blockHeaderQueue.set(header.hash, header.toObject());
// 2. get operations
return this._getHeaderOperations(header);
};
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) {
log.debug('Setting block: "' + block.hash + '" in the block cache.');
// 1. set the block queue, which holds full blocks in memory
this._blockQueue.set(block.hash, block);
// 2. store the block in the database
var operations = this._getBlockOperations(block);
this._db.batch(operations, function(err) {
if(err) {
log.error('There was an error attempting to save block for hash: ' + block.hash);
this._db.emit('error', err);
return;
}
log.debug('Success saving block hash ' + block.hash);
});
};
BlockService.prototype._getHeader = function(block) {
return {