diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 2e8222ba..17f260d6 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -75,8 +75,8 @@ ChainDB.prototype._init = function _init() { writeBufferSize: 8 * 1024 * 1024 }); if (!bcoin.isBrowser) { - var DataStore = require('./data' + 'store'); - this.db = new DataStore(this.db); + //var DataStore = require('./data' + 'store'); + //this.db = new DataStore(this.db); } }; @@ -555,11 +555,11 @@ ChainDB.prototype.saveBlock = function saveBlock(block, batch, callback) { if (this.options.spv) 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) { - batch.put('t/t/' + tx.hash('hex'), tx.toExtended()); - }); + // block.txs.forEach(function(tx, i) { + // batch.put('t/t/' + tx.hash('hex'), tx.toExtended()); + // }); 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/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/' + input.prevout.hash + '/' + input.prevout.index, - input.output.toExtended()); + input.output.toRaw()); }); tx.outputs.forEach(function(output, i) { @@ -918,7 +918,9 @@ ChainDB.prototype.getCoin = function getCoin(hash, index, callback) { } try { - coin = bcoin.coin.fromExtended(data); + coin = bcoin.coin.fromRaw(data); + coin.hash = hash; + coin.index = index; } catch (e) { return callback(e); } @@ -1289,7 +1291,7 @@ ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) { batch.put('u/x/' + input.prevout.hash + '/' + input.prevout.index, - input.output.toExtended()); + input.output.toRaw()); batch.put('u/q/' + futureHeight @@ -1389,7 +1391,9 @@ ChainDB.prototype._getPruneCoin = function _getPruneCoin(hash, index, callback) return self.getCoin(hash, index, callback); try { - coin = bcoin.coin.fromExtended(data); + coin = bcoin.coin.fromRaw(data); + coin.hash = hash; + coin.index = index; } catch (e) { return callback(e); } diff --git a/lib/bcoin/profiler.js b/lib/bcoin/profiler.js index 30df2205..54033592 100644 --- a/lib/bcoin/profiler.js +++ b/lib/bcoin/profiler.js @@ -170,10 +170,11 @@ exports.snapshot = function snapshot(name, callback) { if (bcoin.debug) { 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.heapUsed), - utils.mb(mem.rss - mem.heapUsed)); + utils.mb(mem.heapTotal), + utils.mb(mem.rss - mem.heapTotal)); } if (!profiler) diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 9f4ed84c..a106d8f4 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -389,6 +389,8 @@ Framer.input = function _input(input) { off += utils.writeU32(p, input.prevout.index, off); off += utils.writeIntv(p, s.length, off); + if (!s.copy) + console.log(s); off += utils.copy(s, p, off); off += utils.writeU32(p, input.sequence, off); diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 57faad95..ceeaa926 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -36,7 +36,7 @@ Parser.prototype._error = function _error(str) { }; Parser.prototype.feed = function feed(data) { - var chunk, i, off, len; + var chunk, off, len; this.pendingTotal += data.length; this.pending.push(data); @@ -45,16 +45,16 @@ Parser.prototype.feed = function feed(data) { // Concat chunks chunk = new Buffer(this.waiting); - i = 0; off = 0; len = 0; - for (; off < chunk.length; i++) { + while (off < chunk.length) { len = utils.copy(this.pending[0], chunk, off); if (len === this.pending[0].length) this.pending.shift(); else this.pending[0] = this.pending[0].slice(len); + //this.pending[0] = this.pending[0].slice(len); off += len; } @@ -107,7 +107,7 @@ Parser.prototype.parseHeader = function parseHeader(h) { if (i === 12) 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); if (this.waiting > constants.maxMessage) @@ -196,7 +196,7 @@ Parser.parseVersion = function parseVersion(p) { // User agent length result = utils.readIntv(p, 80); off = result.off; - agent = p.slice(off, off + result.r).toString('ascii'); + agent = p.toString('ascii', off, off + result.r); off += result.r; // Start height @@ -249,7 +249,7 @@ Parser.parseInvList = function parseInvList(p) { for (i = 0, off = 0; i < count; i++, off += 36) { items.push({ 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); 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; flagCount = utils.readIntv(p, off); @@ -282,12 +282,12 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) { if (off + flagCount > p.length) throw new Error('Invalid flag count'); - flags = new Buffer(p.slice(off, off + flagCount)); + flags = utils.slice(p, off, off + flagCount); return { version: utils.read32(p, 0), - prevBlock: new Buffer(p.slice(4, 36)), - merkleRoot: new Buffer(p.slice(36, 68)), + prevBlock: utils.slice(p, 4, 36), + merkleRoot: utils.slice(p, 36, 68), ts: utils.readU32(p, 68), bits: utils.readU32(p, 72), nonce: utils.readU32(p, 76), @@ -317,9 +317,9 @@ Parser.parseHeaders = function parseHeaders(p) { start = off; header.version = utils.read32(p, off); off += 4; - header.prevBlock = new Buffer(p.slice(off, off + 32)); + header.prevBlock = utils.slice(p, off, off + 32); off += 32; - header.merkleRoot = new Buffer(p.slice(off, off + 32)); + header.merkleRoot = utils.slice(p, off, off + 32); off += 32; header.ts = utils.readU32(p, off); off += 4; @@ -360,8 +360,8 @@ Parser.parseBlock = function parseBlock(p) { return { version: utils.read32(p, 0), - prevBlock: new Buffer(p.slice(4, 36)), - merkleRoot: new Buffer(p.slice(36, 68)), + prevBlock: utils.slice(p, 4, 36), + merkleRoot: utils.slice(p, 36, 68), ts: utils.readU32(p, 68), bits: utils.readU32(p, 72), nonce: utils.readU32(p, 76), @@ -409,8 +409,8 @@ Parser.parseBlockCompact = function parseBlockCompact(p) { return { version: version, - prevBlock: new Buffer(p.slice(4, 36)), - merkleRoot: new Buffer(p.slice(36, 68)), + prevBlock: utils.slice(p, 4, 36), + merkleRoot: utils.slice(p, 36, 68), ts: utils.readU32(p, 68), bits: utils.readU32(p, 72), nonce: utils.readU32(p, 76), @@ -438,10 +438,10 @@ Parser.parseInput = function parseInput(p) { return { _size: off + scriptLen + 4, prevout: { - hash: new Buffer(p.slice(0, 32)), + hash: utils.slice(p, 0, 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) }; }; @@ -462,7 +462,7 @@ Parser.parseOutput = function parseOutput(p) { return { _size: off + scriptLen, 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)) 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; if (extended) { - hash = new Buffer(p.slice(off, off + 32)); + hash = utils.slice(p, off, off + 32); off += 32; index = utils.readU32(p, off); @@ -504,7 +504,7 @@ Parser.parseCoin = function parseCoin(p, extended) { spent = utils.readU8(p, off) === 1; off += 1; } else { - hash = new Buffer(constants.zeroHash); + hash = utils.slice(constants.zeroHash); index = 0xffffffff; spent = false; } @@ -588,10 +588,10 @@ Parser.parseTX = function parseTX(p, block) { locktime = utils.readU32(p, off); off += 4; - raw = p.length !== off ? p.slice(0, off) : p; + // raw = p.length !== off ? p.slice(0, off) : p; - if (block) - raw = new Buffer(raw); + // if (block) + // raw = utils.slice(raw); return { version: version, @@ -599,7 +599,7 @@ Parser.parseTX = function parseTX(p, block) { outputs: txOut, locktime: locktime, _witnessSize: 0, - _raw: raw, + // _raw: raw, _size: off }; }; @@ -703,10 +703,10 @@ Parser.parseWitnessTX = function parseWitnessTX(p, block) { locktime = utils.readU32(p, off); off += 4; - raw = p.length !== off ? p.slice(0, off) : p; + // raw = p.length !== off ? p.slice(0, off) : p; - if (block) - raw = new Buffer(raw); + // if (block) + // raw = utils.slice(raw); return { version: version, @@ -715,7 +715,7 @@ Parser.parseWitnessTX = function parseWitnessTX(p, block) { inputs: txIn, outputs: txOut, locktime: locktime, - _raw: raw, + // _raw: raw, _size: off, _witnessSize: witnessSize + 2 }; @@ -734,7 +734,7 @@ Parser.parseWitness = function parseWitness(p) { chunkSize = utils.readIntv(p, off); off = chunkSize.off; chunkSize = chunkSize.r; - item = new Buffer(p.slice(off, off + chunkSize)); + item = utils.slice(p, off, off + chunkSize); off += chunkSize; witness.push(item); if (off > p.length) @@ -777,7 +777,7 @@ Parser.parseReject = function parseReject(p) { off += reasonLen; - data = new Buffer(p.slice(off, off + 32)); + data = utils.slice(p, off, off + 32); return { message: message, @@ -803,7 +803,7 @@ Parser.parseAddress = function parseAddress(p, off, full) { services = utils.readU64(p, off); off += 8; - ip = new Buffer(p.slice(off, off + 16)); + ip = utils.slice(p, off, off + 16); off += 16; port = utils.readU16BE(p, off); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 3e441627..7e1b213a 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -82,7 +82,7 @@ TX.prototype.witnessHash = function witnessHash(enc) { if (this.isCoinbase()) { return enc === 'hex' ? utils.toHex(constants.zeroHash) - : new Buffer(constants.zeroHash); + : utils.slice(constants.zeroHash); } if (!this.hasWitness()) @@ -126,11 +126,11 @@ TX.prototype.getRaw = function getRaw() { else raw = bcoin.protocol.framer.tx(this); - this._raw = raw; + // this._raw = raw; this._size = raw.length; this._witnessSize = raw._witnessSize; - return this._raw; + return raw; }; 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: // it ended up being treated like a hash. if (index >= copy.outputs.length) - return new Buffer(constants.oneHash); + return utils.slice(constants.oneHash); // Drop all the outputs after the input index. copy.outputs.length = index + 1; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index d2c48f56..bee43892 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -15,7 +15,15 @@ var util = require('util'); */ 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) {