From 1cf3d6ccd28837e08b14320e8893e9285610d39c Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 27 Jun 2017 16:40:10 -0400 Subject: [PATCH] wip --- lib/services/block/index.js | 102 +++++------------------------------- 1 file changed, 13 insertions(+), 89 deletions(-) diff --git a/lib/services/block/index.js b/lib/services/block/index.js index eb704776..c38da518 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -1,12 +1,10 @@ 'use strict'; var BaseService = require('../../service'); -var bitcore = require('bitcore-lib'); var inherits = require('util').inherits; var Encoding = require('./encoding'); var index = require('../../'); var log = index.log; -var BufferUtil = bitcore.util.buffer; var LRU = require('lru-cache'); var utils = require('../../utils'); var _ = require('lodash'); @@ -137,84 +135,6 @@ BlockService.prototype.processBlockOperations = function(opts, callback) { }; -BlockService.prototype.getTipOperation = function(block, add, tipType) { - - var heightBuffer = new Buffer(4); - var tipData; - - if (add) { - heightBuffer.writeUInt32BE(block.__height); - tipData = Buffer.concat([new Buffer(block.hash, 'hex'), heightBuffer]); - } else { - heightBuffer.writeUInt32BE(block.__height - 1); - tipData = Buffer.concat([BufferUtil.reverse(block.header.prevHash), heightBuffer]); - } - - var type = tipType || 'tip'; - - return { - type: 'put', - key: this.dbPrefix + type, - value: tipData - }; -}; - - - -BlockService.prototype.getBlocks = function(startHash, endHash) { - - var self = this; - assert(startHash && startHash.length === 64, 'startHash is required to getBlocks'); - - // if start and end hash are the same, the caller is getting, at most, one block - // otherwise, the caller gets all blocks from startHash to endHash, inclusive. - - // check our memory cache first, then db, then go out to p2p network - - // LRU in-memory - var results = self._getCachedBlocks(startHash, endHash); - - // in db - if (!results) { - results = self._getBlocksInDb(startHash, endHash); - } - - var lockedOut = self._getBlocksLockedOut(); - if (!results && !lockedOut) { - self._p2p.getBlocks({ startHash: startHash }); - return true; - } - - if (lockedOut) { - log.debug('Block Service: getBlocks called, but service is still in a lock out period.'); - return false; - } -}; - -BlockService.prototype._checkCache = function(key, cache) { - return cache.get(key); -}; - -BlockService.prototype.getBlockHeader = function(hash, callback) { - - var self = this; - var header = self._checkCache(hash, self._blockHeaderQueue); - - if (header) { - return callback(null, header); - } - - self._p2p.getBlockHeaders(hash); - var timer = setInterval(function() { - var header = self._checkCache(hash, self._blockHeaderQueue); - if (header) { - clearInterval(timer); - callback(null, header); - } - }, 250); - timer.unref(); -}; - BlockService.prototype._startSubscriptions = function() { var self = this; @@ -240,8 +160,6 @@ BlockService.prototype._onHeaders = function(headers) { }; BlockService.prototype._blockAlreadyProcessed = function(block) { - // if the block is not in our block header queue, we probably have not seen it, or - // it is older than about 1000 blocks return this._blockHeaderQueue.get(block.hash); }; @@ -336,15 +254,17 @@ BlockService.prototype._onBlock = function(block) { this.emit('reorg', block); break; default: - this._sendAllUnsentBlocksFromMainChain(block); + var activeChainTip = this._selectActiveChain(block); + this._sendAllUnsentBlocksFromAcitveChain(activeChainTip); break; } }; -BlockService.prototype._sendAllUnsentBlocksFromMainChain = function(block) { +BlockService.prototype._selectActiveChain = function(block) { +}; - var blocksToSend = [block]; +BlockService.prototype._getAllUnsentBlocksFromActiveChain = function(tip) { if (!this._chainTips.get(block.hash)) { @@ -365,13 +285,17 @@ BlockService.prototype._sendAllUnsentBlocksFromMainChain = function(block) { } } } +}; - for(var j = 0; j < blocksToSend.length; j++) { - var block = blocksToSend[j]; - self._broadcast(self._subscriptions.block, 'block/block', block); +BlockService.prototype._sendAllUnsentBlocksFromActiveChain = function(tip) { + + var blocks = this._getAllUnsentBlocksFromActiveChain(tip); + + for(var j = 0; j < blocks.length; j++) { + this._broadcast(this._subscriptions.block, 'block/block', blocks[j]); } - self._setTip(blocksToSend[j-1]); + this._setTip(blocks[j-1]); };