chain: add a cacheLimit option to chain.

This commit is contained in:
Christopher Jeffrey 2014-05-26 12:46:04 -05:00
parent 7e38e4de7c
commit 1b88b355ac
2 changed files with 11 additions and 5 deletions

View File

@ -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++)

View File

@ -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,