mem usage.
This commit is contained in:
parent
a598a4850c
commit
93660e829a
@ -48,12 +48,67 @@ function ChainDB(chain, options) {
|
|||||||
else
|
else
|
||||||
this._cacheWindow = network.block.majorityWindow + 1;
|
this._cacheWindow = network.block.majorityWindow + 1;
|
||||||
|
|
||||||
this.cacheHash = new bcoin.lru(this._cacheWindow);
|
// this.cacheHash = new bcoin.lru(this._cacheWindow);
|
||||||
this.cacheHeight = 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();
|
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);
|
utils.inherits(ChainDB, EventEmitter);
|
||||||
|
|
||||||
ChainDB.prototype._init = function _init() {
|
ChainDB.prototype._init = function _init() {
|
||||||
|
|||||||
@ -593,13 +593,13 @@ Peer.prototype._handleInv = function handleInv(items) {
|
|||||||
this.emit('inv', items);
|
this.emit('inv', items);
|
||||||
|
|
||||||
txs = items.filter(function(item) {
|
txs = items.filter(function(item) {
|
||||||
return item.type === 'tx';
|
return item.type === constants.inv.tx;
|
||||||
}).map(function(item) {
|
}).map(function(item) {
|
||||||
return item.hash;
|
return item.hash;
|
||||||
});
|
});
|
||||||
|
|
||||||
blocks = items.filter(function(item) {
|
blocks = items.filter(function(item) {
|
||||||
return item.type === 'block';
|
return item.type === constants.inv.block;
|
||||||
}).map(function(item) {
|
}).map(function(item) {
|
||||||
return item.hash;
|
return item.hash;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -107,18 +107,20 @@ function Pool(node, options) {
|
|||||||
this.block = {
|
this.block = {
|
||||||
versionHeight: 0,
|
versionHeight: 0,
|
||||||
bestHash: null,
|
bestHash: null,
|
||||||
type: !options.spv ? 'block' : 'filtered'
|
type: !options.spv
|
||||||
|
? constants.inv.block
|
||||||
|
: constants.inv.filtered
|
||||||
};
|
};
|
||||||
|
|
||||||
this.tx = {
|
this.tx = {
|
||||||
state: {},
|
state: {},
|
||||||
count: 0,
|
count: 0,
|
||||||
type: 'tx'
|
type: constants.inv.tx
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.options.witness) {
|
if (this.options.witness) {
|
||||||
this.block.type = 'witness' + this.block.type;
|
this.block.type |= constants.invWitnessMask;
|
||||||
this.tx.type = 'witness' + this.tx.type;
|
this.tx.type |= constants.invWitnessMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.request = {
|
this.request = {
|
||||||
|
|||||||
@ -36,6 +36,11 @@ exports.invByVal = {
|
|||||||
3: 'filtered'
|
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 = {
|
exports.filterFlags = {
|
||||||
none: 0,
|
none: 0,
|
||||||
all: 1,
|
all: 1,
|
||||||
|
|||||||
@ -229,6 +229,7 @@ Framer.verack = function verack() {
|
|||||||
|
|
||||||
Framer._inv = function _inv(items, writer) {
|
Framer._inv = function _inv(items, writer) {
|
||||||
var p = new BufferWriter(writer);
|
var p = new BufferWriter(writer);
|
||||||
|
var type;
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
assert(items.length <= 50000);
|
assert(items.length <= 50000);
|
||||||
@ -236,7 +237,11 @@ Framer._inv = function _inv(items, writer) {
|
|||||||
p.writeVarint(items.length);
|
p.writeVarint(items.length);
|
||||||
|
|
||||||
for (i = 0; i < items.length; i++) {
|
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);
|
p.writeHash(items[i].hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -231,8 +231,9 @@ Parser.parseInvList = function parseInvList(p) {
|
|||||||
|
|
||||||
for (i = 0; i < count; i++) {
|
for (i = 0; i < count; i++) {
|
||||||
items.push({
|
items.push({
|
||||||
type: constants.invByVal[p.readU32()],
|
// type: constants.invByVal[p.readU32()],
|
||||||
hash: p.readHash('hex')
|
type: p.readU32(),
|
||||||
|
hash: p.readHash()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user