diff --git a/lib/bcoin/network.js b/lib/bcoin/network.js index a06e742f..4cd7b8c4 100644 --- a/lib/bcoin/network.js +++ b/lib/bcoin/network.js @@ -55,6 +55,7 @@ function Network(options) { this.maxRate = options.maxRate; this.selfConnect = options.selfConnect; this.requestMempool = options.requestMempool; + this.batchSize = options.batchSize; } /** @@ -127,6 +128,26 @@ Network.prototype.getRate = function getRate() { return Math.min(this.feeRate, this.maxRate); }; +/** + * Determine how many blocks to request + * based on current height of the chain. + * @param {Number} height + * @returns {Number} + */ + +Network.prototype.getBatchSize = function getBatchSize(height) { + var batch = this.batchSize; + var last = batch.length - 1; + var i; + + for (i = 0; i < last; i++) { + if (height <= batch[i][0]) + return batch[i][1]; + } + + return batch[last][0]; +}; + /** * Create a network. Get existing network if possible. * @param {NetworkType|Object} options diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 7fc83ad5..dccd842c 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1600,17 +1600,7 @@ Pool.prototype._sendRequests = function _sendRequests(peer) { items = peer.queue.block.slice(); peer.queue.block.length = 0; } else { - // Blocks start getting big after 150k. - if (this.chain.height <= 100000) - size = 500; - else if (this.chain.height <= 150000) - size = 250; - else if (this.chain.height <= 250000) - size = 150; - else if (this.chain.height <= 350000) - size = 50; - else - size = 20; + size = this.network.getBatchSize(this.chain.height); if (this.request.activeBlocks >= size) return; diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index 651f4e69..34e189f6 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -444,6 +444,19 @@ main.selfConnect = false; main.requestMempool = false; +/** + * Number of blocks to request based on chain height. + * @const {Array} + */ + +main.batchSize = [ + [100000, 500], + [150000, 250], + [250000, 150], + [350000, 50], + [20] +]; + /* * Testnet (v3) * https://en.bitcoin.it/wiki/Testnet @@ -591,6 +604,11 @@ testnet.selfConnect = false; testnet.requestMempool = true; +testnet.batchSize = [ + [100000, 500], + [250] +]; + /* * Regtest */ @@ -731,6 +749,10 @@ regtest.selfConnect = false; regtest.requestMempool = true; +regtest.batchSize = [ + [500] +]; + /* * segnet3 */ @@ -850,6 +872,11 @@ segnet3.selfConnect = false; segnet3.requestMempool = true; +segnet3.batchSize = [ + [20000, 500], + [250] +]; + /* * segnet4 */ @@ -983,3 +1010,8 @@ segnet4.maxRate = 40000; segnet4.selfConnect = false; segnet4.requestMempool = true; + +segnet4.batchSize = [ + [17000, 500], + [250] +];