From c71258103be60b2fe70b8d60539b56129d0abd54 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 2 May 2014 00:08:07 +0400 Subject: [PATCH] pool: wip --- lib/bcoin/chain.js | 2 +- lib/bcoin/peer.js | 2 +- lib/bcoin/pool.js | 52 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 596b5508..30afc97a 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -11,7 +11,7 @@ function Chain() { map: {}, count: 0 }; - this.bloom = new bcoin.bloom(28 * 1024 * 1024, 33, 0); + this.bloom = new bcoin.bloom(28 * 1024 * 1024, 33, 0xdeadbeef); this.last = null; this.add(new bcoin.block(constants.genesis)); } diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 77d240a6..5fa7f772 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -43,7 +43,7 @@ function Peer(pool, socket, options) { this._ping = { timer: null, - interval: this.options.pingInterval || 5000 + interval: this.options.pingInterval || 30000 }; this._init(); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 233dae2a..00f1efe8 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -8,16 +8,16 @@ function Pool(options) { this.options = options || {}; this.size = options.size || 16; - this.parallel = options.parallel || 4000; - this.loadTimeout = options.loadTimeout || 3000; - this.loadDelay = options.loadDelay || 750; + this.parallel = options.parallel || 8000; + this.loadTimeout = options.loadTimeout || 10000; + this.loadWindow = options.loadWindow || 2500; this.loadTimer = null; this.loadWatermark = { lo: options.lwm || 1000, - hi: options.hwm || 4000 + hi: options.hwm || 32000 }; this.maxRetries = options.maxRetries || 1000; - this.requestTimeout = options.requestTimeout || 30000; + this.requestTimeout = options.requestTimeout || 15000; this.chain = new bcoin.chain(); this.bloom = new bcoin.bloom(8 * 10 * 1024, 10, @@ -90,8 +90,10 @@ Pool.prototype._addLoader = function _addLoader() { // Split blocks and request them using multiple peers peer.on('blocks', function(hashes) { - if (hashes.length === 0) + if (hashes.length === 0) { + self.block.lastSeen = null; return; + } // Request each block hashes.forEach(function(hash) { @@ -115,10 +117,14 @@ Pool.prototype._load = function load() { // Load more blocks, starting from last hash var hash; - if (this.block.lastSeen) + if (this.block.lastSeen) { hash = this.block.lastSeen; - else + console.log('Loading from (last): ' + utils.toHex(hash.slice().reverse())); + } else { hash = this.chain.getLast().hash(); + console.log('Loading from (chain): ' + utils.toHex(hash.slice().reverse())); + } + this.peers.load.loadBlocks(hash); return true; @@ -163,6 +169,18 @@ Pool.prototype._addPeer = function _addPeer() { req.finish(block); }); + peer.on('notfound', function(items) { + items.forEach(function(item) { + if (item.type !== 'filtered') + return; + + var req = self.block.requests[utils.toHex(item.hash)]; + console.log('notfound', !!req); + if (req) + req.finish(null); + }); + }); + peer.on('tx', function(tx) { console.log('got tx', tx.hash('hex')); }); @@ -199,6 +217,18 @@ Pool.prototype._requestBlock = function _requestBlock(hash) { }; Pool.prototype._scheduleRequests = function _scheduleRequests() { + if (this.block.active > 100) + return; + + // No need to wait - already have enough data + if (this.block.queue.length > this.parallel) { + if (this.loadTimer !== null) + clearTimeout(this.loadTimer); + this.loadTimer = null; + return this._doRequests(); + } + + // Already waiting if (this.loadTimer !== null) return; @@ -206,7 +236,7 @@ Pool.prototype._scheduleRequests = function _scheduleRequests() { this.loadTimer = setTimeout(function() { self.loadTimer = null; self._doRequests(); - }, this.loadDelay); + }, this.loadWindow); }; Pool.prototype._doRequests = function _doRequests() { @@ -305,7 +335,7 @@ BlockRequest.prototype.retry = function retry() { this.peer.destroy(); }; -BlockRequest.prototype.finish = function finish(block) { +BlockRequest.prototype.finish = function finish() { if (this.active) { this.pool.block.active--; this.active = false; @@ -316,7 +346,7 @@ BlockRequest.prototype.finish = function finish(block) { if (index !== -1) this.pool.block.queue.splice(index, 1); } - delete this.pool.block.requests[block.hash('hex')]; + delete this.pool.block.requests[utils.toHex(this.hash)]; if (this.timer) clearTimeout(this.timer); this.timer = null;