memleak work.

This commit is contained in:
Christopher Jeffrey 2016-03-14 13:25:02 -07:00
parent 18145ad541
commit 00dfad25da
6 changed files with 66 additions and 51 deletions

View File

@ -75,8 +75,8 @@ ChainDB.prototype._init = function _init() {
writeBufferSize: 8 * 1024 * 1024 writeBufferSize: 8 * 1024 * 1024
}); });
if (!bcoin.isBrowser) { if (!bcoin.isBrowser) {
var DataStore = require('./data' + 'store'); //var DataStore = require('./data' + 'store');
this.db = new DataStore(this.db); //this.db = new DataStore(this.db);
} }
}; };
@ -555,11 +555,11 @@ ChainDB.prototype.saveBlock = function saveBlock(block, batch, callback) {
if (this.options.spv) if (this.options.spv)
return callback(); return callback();
batch.put('b/b/' + block.hash('hex'), block.toCompact()); // batch.put('b/b/' + block.hash('hex'), block.toCompact());
block.txs.forEach(function(tx, i) { // block.txs.forEach(function(tx, i) {
batch.put('t/t/' + tx.hash('hex'), tx.toExtended()); // batch.put('t/t/' + tx.hash('hex'), tx.toExtended());
}); // });
this.connectBlock(block, batch, callback); this.connectBlock(block, batch, callback);
}; };
@ -650,7 +650,7 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) {
batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY); batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY);
} }
batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toExtended()); batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toRaw());
}); });
}); });
@ -707,7 +707,7 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(hash, batch, callba
batch.put('u/t/' batch.put('u/t/'
+ input.prevout.hash + input.prevout.hash
+ '/' + input.prevout.index, + '/' + input.prevout.index,
input.output.toExtended()); input.output.toRaw());
}); });
tx.outputs.forEach(function(output, i) { tx.outputs.forEach(function(output, i) {
@ -918,7 +918,9 @@ ChainDB.prototype.getCoin = function getCoin(hash, index, callback) {
} }
try { try {
coin = bcoin.coin.fromExtended(data); coin = bcoin.coin.fromRaw(data);
coin.hash = hash;
coin.index = index;
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
@ -1289,7 +1291,7 @@ ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) {
batch.put('u/x/' batch.put('u/x/'
+ input.prevout.hash + input.prevout.hash
+ '/' + input.prevout.index, + '/' + input.prevout.index,
input.output.toExtended()); input.output.toRaw());
batch.put('u/q/' batch.put('u/q/'
+ futureHeight + futureHeight
@ -1389,7 +1391,9 @@ ChainDB.prototype._getPruneCoin = function _getPruneCoin(hash, index, callback)
return self.getCoin(hash, index, callback); return self.getCoin(hash, index, callback);
try { try {
coin = bcoin.coin.fromExtended(data); coin = bcoin.coin.fromRaw(data);
coin.hash = hash;
coin.index = index;
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }

View File

@ -170,10 +170,11 @@ exports.snapshot = function snapshot(name, callback) {
if (bcoin.debug) { if (bcoin.debug) {
mem = process.memoryUsage(); mem = process.memoryUsage();
utils.debug('Memory: rss=%dmb, js-heap=%dmb native-heap=%dmb', utils.debug('Memory: rss=%dmb, js-heap=%d/%dmb native-heap=%dmb',
utils.mb(mem.rss), utils.mb(mem.rss),
utils.mb(mem.heapUsed), utils.mb(mem.heapUsed),
utils.mb(mem.rss - mem.heapUsed)); utils.mb(mem.heapTotal),
utils.mb(mem.rss - mem.heapTotal));
} }
if (!profiler) if (!profiler)

View File

@ -389,6 +389,8 @@ Framer.input = function _input(input) {
off += utils.writeU32(p, input.prevout.index, off); off += utils.writeU32(p, input.prevout.index, off);
off += utils.writeIntv(p, s.length, off); off += utils.writeIntv(p, s.length, off);
if (!s.copy)
console.log(s);
off += utils.copy(s, p, off); off += utils.copy(s, p, off);
off += utils.writeU32(p, input.sequence, off); off += utils.writeU32(p, input.sequence, off);

View File

@ -36,7 +36,7 @@ Parser.prototype._error = function _error(str) {
}; };
Parser.prototype.feed = function feed(data) { Parser.prototype.feed = function feed(data) {
var chunk, i, off, len; var chunk, off, len;
this.pendingTotal += data.length; this.pendingTotal += data.length;
this.pending.push(data); this.pending.push(data);
@ -45,16 +45,16 @@ Parser.prototype.feed = function feed(data) {
// Concat chunks // Concat chunks
chunk = new Buffer(this.waiting); chunk = new Buffer(this.waiting);
i = 0;
off = 0; off = 0;
len = 0; len = 0;
for (; off < chunk.length; i++) { while (off < chunk.length) {
len = utils.copy(this.pending[0], chunk, off); len = utils.copy(this.pending[0], chunk, off);
if (len === this.pending[0].length) if (len === this.pending[0].length)
this.pending.shift(); this.pending.shift();
else else
this.pending[0] = this.pending[0].slice(len); this.pending[0] = this.pending[0].slice(len);
//this.pending[0] = this.pending[0].slice(len);
off += len; off += len;
} }
@ -107,7 +107,7 @@ Parser.prototype.parseHeader = function parseHeader(h) {
if (i === 12) if (i === 12)
return this._error('Not NULL-terminated cmd'); return this._error('Not NULL-terminated cmd');
cmd = h.slice(4, 4 + i).toString('ascii'); cmd = h.toString('ascii', 4, 4 + i);
this.waiting = utils.readU32(h, 16); this.waiting = utils.readU32(h, 16);
if (this.waiting > constants.maxMessage) if (this.waiting > constants.maxMessage)
@ -196,7 +196,7 @@ Parser.parseVersion = function parseVersion(p) {
// User agent length // User agent length
result = utils.readIntv(p, 80); result = utils.readIntv(p, 80);
off = result.off; off = result.off;
agent = p.slice(off, off + result.r).toString('ascii'); agent = p.toString('ascii', off, off + result.r);
off += result.r; off += result.r;
// Start height // Start height
@ -249,7 +249,7 @@ Parser.parseInvList = function parseInvList(p) {
for (i = 0, off = 0; i < count; i++, off += 36) { for (i = 0, off = 0; i < count; i++, off += 36) {
items.push({ items.push({
type: constants.invByVal[utils.readU32(p, off)], type: constants.invByVal[utils.readU32(p, off)],
hash: new Buffer(p.slice(off + 4, off + 36)) hash: utils.slice(p, off + 4, off + 36)
}); });
} }
@ -272,7 +272,7 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) {
hashes = new Array(hashCount); hashes = new Array(hashCount);
for (i = 0; i < hashCount; i++) for (i = 0; i < hashCount; i++)
hashes[i] = new Buffer(p.slice(off + i * 32, off + (i + 1) * 32)); hashes[i] = utils.slice(p, off + i * 32, off + (i + 1) * 32);
off = off + 32 * hashCount; off = off + 32 * hashCount;
flagCount = utils.readIntv(p, off); flagCount = utils.readIntv(p, off);
@ -282,12 +282,12 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) {
if (off + flagCount > p.length) if (off + flagCount > p.length)
throw new Error('Invalid flag count'); throw new Error('Invalid flag count');
flags = new Buffer(p.slice(off, off + flagCount)); flags = utils.slice(p, off, off + flagCount);
return { return {
version: utils.read32(p, 0), version: utils.read32(p, 0),
prevBlock: new Buffer(p.slice(4, 36)), prevBlock: utils.slice(p, 4, 36),
merkleRoot: new Buffer(p.slice(36, 68)), merkleRoot: utils.slice(p, 36, 68),
ts: utils.readU32(p, 68), ts: utils.readU32(p, 68),
bits: utils.readU32(p, 72), bits: utils.readU32(p, 72),
nonce: utils.readU32(p, 76), nonce: utils.readU32(p, 76),
@ -317,9 +317,9 @@ Parser.parseHeaders = function parseHeaders(p) {
start = off; start = off;
header.version = utils.read32(p, off); header.version = utils.read32(p, off);
off += 4; off += 4;
header.prevBlock = new Buffer(p.slice(off, off + 32)); header.prevBlock = utils.slice(p, off, off + 32);
off += 32; off += 32;
header.merkleRoot = new Buffer(p.slice(off, off + 32)); header.merkleRoot = utils.slice(p, off, off + 32);
off += 32; off += 32;
header.ts = utils.readU32(p, off); header.ts = utils.readU32(p, off);
off += 4; off += 4;
@ -360,8 +360,8 @@ Parser.parseBlock = function parseBlock(p) {
return { return {
version: utils.read32(p, 0), version: utils.read32(p, 0),
prevBlock: new Buffer(p.slice(4, 36)), prevBlock: utils.slice(p, 4, 36),
merkleRoot: new Buffer(p.slice(36, 68)), merkleRoot: utils.slice(p, 36, 68),
ts: utils.readU32(p, 68), ts: utils.readU32(p, 68),
bits: utils.readU32(p, 72), bits: utils.readU32(p, 72),
nonce: utils.readU32(p, 76), nonce: utils.readU32(p, 76),
@ -409,8 +409,8 @@ Parser.parseBlockCompact = function parseBlockCompact(p) {
return { return {
version: version, version: version,
prevBlock: new Buffer(p.slice(4, 36)), prevBlock: utils.slice(p, 4, 36),
merkleRoot: new Buffer(p.slice(36, 68)), merkleRoot: utils.slice(p, 36, 68),
ts: utils.readU32(p, 68), ts: utils.readU32(p, 68),
bits: utils.readU32(p, 72), bits: utils.readU32(p, 72),
nonce: utils.readU32(p, 76), nonce: utils.readU32(p, 76),
@ -438,10 +438,10 @@ Parser.parseInput = function parseInput(p) {
return { return {
_size: off + scriptLen + 4, _size: off + scriptLen + 4,
prevout: { prevout: {
hash: new Buffer(p.slice(0, 32)), hash: utils.slice(p, 0, 32),
index: utils.readU32(p, 32) index: utils.readU32(p, 32)
}, },
script: bcoin.script.decode(new Buffer(p.slice(off, off + scriptLen))), script: bcoin.script.decode(utils.slice(p, off, off + scriptLen)),
sequence: utils.readU32(p, off + scriptLen) sequence: utils.readU32(p, off + scriptLen)
}; };
}; };
@ -462,7 +462,7 @@ Parser.parseOutput = function parseOutput(p) {
return { return {
_size: off + scriptLen, _size: off + scriptLen,
value: utils.read64(p, 0), value: utils.read64(p, 0),
script: bcoin.script.decode(new Buffer(p.slice(off, off + scriptLen))) script: bcoin.script.decode(utils.slice(p, off, off + scriptLen))
}; };
}; };
@ -491,11 +491,11 @@ Parser.parseCoin = function parseCoin(p, extended) {
if (off + scriptLen > p.length - (extended ? 37 : 0)) if (off + scriptLen > p.length - (extended ? 37 : 0))
throw new Error('Invalid utxo script length'); throw new Error('Invalid utxo script length');
script = bcoin.script.decode(new Buffer(p.slice(off, off + scriptLen))); script = bcoin.script.decode(utils.slice(p, off, off + scriptLen));
off += scriptLen; off += scriptLen;
if (extended) { if (extended) {
hash = new Buffer(p.slice(off, off + 32)); hash = utils.slice(p, off, off + 32);
off += 32; off += 32;
index = utils.readU32(p, off); index = utils.readU32(p, off);
@ -504,7 +504,7 @@ Parser.parseCoin = function parseCoin(p, extended) {
spent = utils.readU8(p, off) === 1; spent = utils.readU8(p, off) === 1;
off += 1; off += 1;
} else { } else {
hash = new Buffer(constants.zeroHash); hash = utils.slice(constants.zeroHash);
index = 0xffffffff; index = 0xffffffff;
spent = false; spent = false;
} }
@ -588,10 +588,10 @@ Parser.parseTX = function parseTX(p, block) {
locktime = utils.readU32(p, off); locktime = utils.readU32(p, off);
off += 4; off += 4;
raw = p.length !== off ? p.slice(0, off) : p; // raw = p.length !== off ? p.slice(0, off) : p;
if (block) // if (block)
raw = new Buffer(raw); // raw = utils.slice(raw);
return { return {
version: version, version: version,
@ -599,7 +599,7 @@ Parser.parseTX = function parseTX(p, block) {
outputs: txOut, outputs: txOut,
locktime: locktime, locktime: locktime,
_witnessSize: 0, _witnessSize: 0,
_raw: raw, // _raw: raw,
_size: off _size: off
}; };
}; };
@ -703,10 +703,10 @@ Parser.parseWitnessTX = function parseWitnessTX(p, block) {
locktime = utils.readU32(p, off); locktime = utils.readU32(p, off);
off += 4; off += 4;
raw = p.length !== off ? p.slice(0, off) : p; // raw = p.length !== off ? p.slice(0, off) : p;
if (block) // if (block)
raw = new Buffer(raw); // raw = utils.slice(raw);
return { return {
version: version, version: version,
@ -715,7 +715,7 @@ Parser.parseWitnessTX = function parseWitnessTX(p, block) {
inputs: txIn, inputs: txIn,
outputs: txOut, outputs: txOut,
locktime: locktime, locktime: locktime,
_raw: raw, // _raw: raw,
_size: off, _size: off,
_witnessSize: witnessSize + 2 _witnessSize: witnessSize + 2
}; };
@ -734,7 +734,7 @@ Parser.parseWitness = function parseWitness(p) {
chunkSize = utils.readIntv(p, off); chunkSize = utils.readIntv(p, off);
off = chunkSize.off; off = chunkSize.off;
chunkSize = chunkSize.r; chunkSize = chunkSize.r;
item = new Buffer(p.slice(off, off + chunkSize)); item = utils.slice(p, off, off + chunkSize);
off += chunkSize; off += chunkSize;
witness.push(item); witness.push(item);
if (off > p.length) if (off > p.length)
@ -777,7 +777,7 @@ Parser.parseReject = function parseReject(p) {
off += reasonLen; off += reasonLen;
data = new Buffer(p.slice(off, off + 32)); data = utils.slice(p, off, off + 32);
return { return {
message: message, message: message,
@ -803,7 +803,7 @@ Parser.parseAddress = function parseAddress(p, off, full) {
services = utils.readU64(p, off); services = utils.readU64(p, off);
off += 8; off += 8;
ip = new Buffer(p.slice(off, off + 16)); ip = utils.slice(p, off, off + 16);
off += 16; off += 16;
port = utils.readU16BE(p, off); port = utils.readU16BE(p, off);

View File

@ -82,7 +82,7 @@ TX.prototype.witnessHash = function witnessHash(enc) {
if (this.isCoinbase()) { if (this.isCoinbase()) {
return enc === 'hex' return enc === 'hex'
? utils.toHex(constants.zeroHash) ? utils.toHex(constants.zeroHash)
: new Buffer(constants.zeroHash); : utils.slice(constants.zeroHash);
} }
if (!this.hasWitness()) if (!this.hasWitness())
@ -126,11 +126,11 @@ TX.prototype.getRaw = function getRaw() {
else else
raw = bcoin.protocol.framer.tx(this); raw = bcoin.protocol.framer.tx(this);
this._raw = raw; // this._raw = raw;
this._size = raw.length; this._size = raw.length;
this._witnessSize = raw._witnessSize; this._witnessSize = raw._witnessSize;
return this._raw; return raw;
}; };
TX.prototype.getVirtualSize = function getVirtualSize() { TX.prototype.getVirtualSize = function getVirtualSize() {
@ -254,7 +254,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, s, type) {
// Bitcoind used to return 1 as an error code: // Bitcoind used to return 1 as an error code:
// it ended up being treated like a hash. // it ended up being treated like a hash.
if (index >= copy.outputs.length) if (index >= copy.outputs.length)
return new Buffer(constants.oneHash); return utils.slice(constants.oneHash);
// Drop all the outputs after the input index. // Drop all the outputs after the input index.
copy.outputs.length = index + 1; copy.outputs.length = index + 1;

View File

@ -15,7 +15,15 @@ var util = require('util');
*/ */
utils.slice = function slice(buf, start, end) { utils.slice = function slice(buf, start, end) {
return new Buffer(buf.slice(start, end)); var clone;
buf = buf.slice(start || 0, end || buf.length);
clone = new Buffer(buf.length);
buf.copy(clone, 0, 0, buf.length);
return clone;
}; };
utils.toBuffer = function toBuffer(msg, enc) { utils.toBuffer = function toBuffer(msg, enc) {