diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index db40ab22..88551ecc 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -17,6 +17,7 @@ function Chain(options) { this.prefix = 'bt/chain/'; this.storage = this.options.storage; this.strict = this.options.strict || false; + this.cacheLimit = this.options.cacheLimit || 2000; this.block = { list: [], @@ -256,12 +257,12 @@ Chain.prototype.add = function add(block) { }; Chain.prototype._compress = function compress() { - // Keep at least 1000 blocks and at most 2000 - if (this.block.list.length < 2000) + // Keep at least 1000 blocks and at most 2000 by default + if (this.block.list.length < this.cacheLimit) return; // Bloom filter rebuilt is needed - this.block.list = this.block.list.slice(-1000); + this.block.list = this.block.list.slice(-(this.cacheLimit / 2 | 0)); this.block.bloom.reset(); for (var i = 0; i < this.block.list.length; i++) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b41aa8e5..a21185c9 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -13,8 +13,8 @@ function Pool(options) { EventEmitter.call(this); this.options = options || {}; - this.storage = this.options.storage; this.options.relay = this.options.relay !== false; + this.storage = this.options.storage; this.destroyed = false; this.size = options.size || 32; this.parallel = options.parallel || 2000; @@ -36,7 +36,12 @@ function Pool(options) { }; this.maxRetries = options.maxRetries || 42; this.requestTimeout = options.requestTimeout || 10000; - this.chain = new bcoin.chain({ storage: this.storage }); + this.chain = new bcoin.chain({ + storage: this.storage, + // Since regular blocks contain transactions and full merkle + // trees, it's risky to cache 2000 blocks. Let's do 100. + cacheLimit: !this.options.relay ? 100 : null + }); this.watchMap = {}; this.bloom = new bcoin.bloom(8 * 1024, 10,