From 93660e829a9c37175bdc8582ba83fbf98dbb5e2f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 28 Mar 2016 17:54:56 -0700 Subject: [PATCH] mem usage. --- lib/bcoin/chaindb.js | 59 +++++++++++++++++++++++++++++++-- lib/bcoin/peer.js | 4 +-- lib/bcoin/pool.js | 10 +++--- lib/bcoin/protocol/constants.js | 5 +++ lib/bcoin/protocol/framer.js | 7 +++- lib/bcoin/protocol/parser.js | 5 +-- 6 files changed, 79 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index d5d511af..f68765f5 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -48,12 +48,67 @@ function ChainDB(chain, options) { else this._cacheWindow = network.block.majorityWindow + 1; - this.cacheHash = new bcoin.lru(this._cacheWindow); - this.cacheHeight = new bcoin.lru(this._cacheWindow); + // this.cacheHash = new bcoin.lru(this._cacheWindow); + // this.cacheHeight = new bcoin.lru(this._cacheWindow); + this.cacheHash = new DumbCache(this._cacheWindow * 10); + this.cacheHeight = new DumbCache(this._cacheWindow * 10); this._init(); } +function DumbCache(size) { + this.data = {}; + this.count = 0; + this.size = size; +} + +DumbCache.prototype.set = function set(key, value) { + key = key + ''; + + assert(value !== undefined); + + if (this.count > this.size) + this.reset(); + + if (this.data[key] === undefined) + this.count++; + + this.data[key] = value; +}; + +DumbCache.prototype.remove = function remove(key) { + key = key + ''; + + if (this.data[key] === undefined) + return; + + this.count--; + delete this.data[key]; +}; + +DumbCache.prototype.get = function get(key) { + key = key + ''; + return this.data[key]; +}; + +DumbCache.prototype.has = function has(key) { + key = key + ''; + return this.data[key] !== undefined; +}; + +DumbCache.prototype.reset = function reset() { + this.data = {}; + this.count = 0; +}; + +function NullCache(size) {} + +NullCache.prototype.set = function set(key, value) {}; +NullCache.prototype.remove = function remove(key) {}; +NullCache.prototype.get = function get(key) {}; +NullCache.prototype.has = function has(key) {}; +NullCache.prototype.reset = function reset() {}; + utils.inherits(ChainDB, EventEmitter); ChainDB.prototype._init = function _init() { diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 7e18acb3..316df468 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -593,13 +593,13 @@ Peer.prototype._handleInv = function handleInv(items) { this.emit('inv', items); txs = items.filter(function(item) { - return item.type === 'tx'; + return item.type === constants.inv.tx; }).map(function(item) { return item.hash; }); blocks = items.filter(function(item) { - return item.type === 'block'; + return item.type === constants.inv.block; }).map(function(item) { return item.hash; }); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 0446cd0c..8c576dcc 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -107,18 +107,20 @@ function Pool(node, options) { this.block = { versionHeight: 0, bestHash: null, - type: !options.spv ? 'block' : 'filtered' + type: !options.spv + ? constants.inv.block + : constants.inv.filtered }; this.tx = { state: {}, count: 0, - type: 'tx' + type: constants.inv.tx }; if (this.options.witness) { - this.block.type = 'witness' + this.block.type; - this.tx.type = 'witness' + this.tx.type; + this.block.type |= constants.invWitnessMask; + this.tx.type |= constants.invWitnessMask; } this.request = { diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 497c90be..de9526cc 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -36,6 +36,11 @@ exports.invByVal = { 3: 'filtered' }; +exports.invByVal[1 | (1 << 30)] = 'witnesstx'; +exports.invByVal[2 | (1 << 30)] = 'witnessblock'; +exports.invByVal[3 | (1 << 30)] = 'witnessfiltered'; +exports.invWitnessMask = 1 << 30; + exports.filterFlags = { none: 0, all: 1, diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 4f8c265c..1ff08a4a 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -229,6 +229,7 @@ Framer.verack = function verack() { Framer._inv = function _inv(items, writer) { var p = new BufferWriter(writer); + var type; var i; assert(items.length <= 50000); @@ -236,7 +237,11 @@ Framer._inv = function _inv(items, writer) { p.writeVarint(items.length); for (i = 0; i < items.length; i++) { - p.writeU32(constants.inv[items[i].type]); + type = items[i].type; + if (typeof type === 'string') + type = constants.inv[items[i].type]; + assert(constants.invByVal[type] != null); + p.writeU32(type); p.writeHash(items[i].hash); } diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 0a813114..cdb198c9 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -231,8 +231,9 @@ Parser.parseInvList = function parseInvList(p) { for (i = 0; i < count; i++) { items.push({ - type: constants.invByVal[p.readU32()], - hash: p.readHash('hex') + // type: constants.invByVal[p.readU32()], + type: p.readU32(), + hash: p.readHash() }); }