From 32859a8da8f090f1ca74d7ac6b5d05fcc75be764 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Fri, 16 Aug 2013 01:45:55 -0400 Subject: [PATCH] Transaction: prefer to directly use a Buffer, where possible --- Transaction.js | 41 +++++++++++++++++++---------------------- util/util.js | 9 +++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Transaction.js b/Transaction.js index 28fbd98..b96dcef 100644 --- a/Transaction.js +++ b/Transaction.js @@ -38,14 +38,11 @@ function spec(b) { }; TransactionIn.prototype.serialize = function serialize() { - var bytes = Put(); - - bytes.put(this.o); - bytes.varint(this.s.length); - bytes.put(this.s); - bytes.word32le(this.q); + var slen = util.varIntBuf(this.s.length); + var qbuf = new Buffer(4); + qbuf.writeUInt32LE(this.q, 0); - return bytes.buffer(); + return Buffer.concat([this.o, slen, this.s, qbuf]); }; TransactionIn.prototype.getOutpointHash = function getOutpointIndex() { @@ -88,13 +85,8 @@ function spec(b) { }; TransactionOut.prototype.serialize = function serialize() { - var bytes = Put(); - - bytes.put(this.v); - bytes.varint(this.s.length); - bytes.put(this.s); - - return bytes.buffer(); + var slen = util.varIntBuf(this.s.length); + return Buffer.concat([this.v, slen, this.s]); }; function Transaction(data) { @@ -143,22 +135,27 @@ function spec(b) { }; Transaction.prototype.serialize = function serialize() { - var bytes = Put(); + var bufs = []; - bytes.word32le(this.version); - bytes.varint(this.ins.length); + var buf = new Buffer(4); + buf.writeUInt32LE(this.version, 0); + bufs.push(buf); + + bufs.push(util.varIntBuf(this.ins.length)); this.ins.forEach(function (txin) { - bytes.put(txin.serialize()); + bufs.push(txin.serialize()); }); - bytes.varint(this.outs.length); + bufs.push(util.varIntBuf(this.outs.length)); this.outs.forEach(function (txout) { - bytes.put(txout.serialize()); + bufs.push(txout.serialize()); }); - bytes.word32le(this.lock_time); + var buf = new Buffer(4); + buf.writeUInt32LE(this.lock_time, 0); + bufs.push(buf); - return this._buffer = bytes.buffer(); + return this._buffer = Buffer.concat(bufs); }; Transaction.prototype.getBuffer = function getBuffer() { diff --git a/util/util.js b/util/util.js index 076951f..fe170a1 100644 --- a/util/util.js +++ b/util/util.js @@ -323,6 +323,15 @@ var varStrBuf = exports.varStrBuf = function varStrBuf(s) { return Buffer.concat(varIntBuf(s.length), s); }; +var buf64 = exports.buf64 = function buf64(n) { + var lo = n & 0xffffffff; + var hi = (n >>> 32); + var buf = new Buffer(4 + 4); + buf.writeUInt32LE(lo, 0); + buf.writeUInt32LE(hi, 4); + return buf; +}; + // Initializations exports.NULL_HASH = new Buffer(32).fill(0); exports.EMPTY_BUFFER = new Buffer(0);