From cdf53d1fc5f5bcaffe2329101de156bc77aedb73 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 12:19:04 -0300 Subject: [PATCH 01/11] migrating to buffertools best practices --- Transaction.js | 3 ++- util/util.js | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Transaction.js b/Transaction.js index 36bbbeb..154015e 100644 --- a/Transaction.js +++ b/Transaction.js @@ -11,12 +11,13 @@ function spec(b) { var Put = b.Put || require('bufferput'); var Parser = b.Parser || require('./util/BinaryParser').class(); var Step = b.Step || require('step'); + var buffertools = require('buffertools'); var error = b.error || require('./util/error'); var VerificationError = error.VerificationError; var MissingSourceError = error.MissingSourceError; - var COINBASE_OP = util.NULL_HASH.concat(new Buffer("FFFFFFFF", 'hex')); + var COINBASE_OP = buffertools.concat(util.NULL_HASH, new Buffer("FFFFFFFF", 'hex')); function TransactionIn(data) { if ("object" !== typeof data) { diff --git a/util/util.js b/util/util.js index 9ae61cb..e67ae83 100644 --- a/util/util.js +++ b/util/util.js @@ -3,7 +3,7 @@ var crypto = require('crypto'); var bignum = require('bignum'); var Binary = require('binary'); var Put = require('bufferput'); -require('buffertools').extend(); +var buffertools = require('buffertools'); var sha256 = exports.sha256 = function (data) { return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary'); @@ -321,13 +321,15 @@ var varIntBuf = exports.varIntBuf = function varIntBuf(n) { }; var varStrBuf = exports.varStrBuf = function varStrBuf(s) { - return Buffer.concat(varIntBuf(s.length), s); + return buffertools.concat(varIntBuf(s.length), s); }; // Initializations -exports.NULL_HASH = new Buffer(32).fill(0); +exports.NULL_HASH = new Buffer(32) +buffertools.fill(exports.NULL_HASH, 0); exports.EMPTY_BUFFER = new Buffer(0); -exports.ZERO_VALUE = new Buffer(8).fill(0); +exports.ZERO_VALUE = new Buffer(8) +buffertools.fill(exports.ZERO_VALUE, 0); var INT64_MAX = new Buffer('ffffffffffffffff', 'hex'); exports.INT64_MAX = INT64_MAX; From 3d901a12f225f0f791ecc41652527177b8140d79 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 12:47:15 -0300 Subject: [PATCH 02/11] added tests to Block and Peer, and migrated buffertools usage --- Block.js | 7 ++++--- Peer.js | 3 ++- bitcore.js | 2 ++ test/test.Block.js | 28 ++++++++++++++++++++++++++++ test/test.Peer.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 test/test.Block.js create mode 100644 test/test.Peer.js diff --git a/Block.js b/Block.js index cfe976b..f0abb5e 100644 --- a/Block.js +++ b/Block.js @@ -7,6 +7,7 @@ function spec(b) { var Bignum = b.Bignum || require('bignum'); var Binary = b.Binary || require('binary'); var Step = b.Step || require('step'); + var buffertools = b.buffertools || require('buffertools'); var Transaction = b.Transaction || require('./Transaction').class(); var TransactionIn = Transaction.In; var TransactionOut = Transaction.Out; @@ -79,7 +80,7 @@ function spec(b) { Block.prototype.checkHash = function checkHash() { if (!this.hash || !this.hash.length) return false; - return this.calcHash().compare(this.hash) == 0; + return buffertools.compare(this.calcHash(), this.hash) == 0; }; Block.prototype.getHash = function getHash() { @@ -95,7 +96,7 @@ function spec(b) { // endian so we don't have to reverse both buffers before comparing. this.hash.reverse(); - if (this.hash.compare(target) > 0) { + if (buffertools.compare(this.hash, target) > 0) { throw new VerificationError('Difficulty target not met'); } @@ -199,7 +200,7 @@ function spec(b) { throw new VerificationError('No merkle root'); } - if (this.calcMerkleRoot().compare(this.merkle_root) == 0) { + if (buffertools.compare(this.calcMerkleRoot(), this.merkle_root) == 0) { throw new VerificationError('Merkle root incorrect'); } diff --git a/Peer.js b/Peer.js index b27a5e2..54d6635 100644 --- a/Peer.js +++ b/Peer.js @@ -3,6 +3,7 @@ require('classtool'); function spec(b) { var Net = b.Net || require('net'); var Binary = b.Binary || require('binary'); + var buffertools = b.buffertools || require('buffertools'); function Peer(host, port, services) { if ("string" === typeof host) { @@ -17,7 +18,7 @@ function spec(b) { this.host = host.host; this.port = host.port; } else if (Buffer.isBuffer(host)) { - if (host.slice(0, 12).compare(Peer.IPV6_IPV4_PADDING) != 0) { + if (buffertools.compare(Peer.IPV6_IPV4_PADDING, host.slice(0, 12)) != 0) { throw new Error('IPV6 not supported yet! Cannot instantiate host.'); } this.host = Array.prototype.slice.apply(host.slice(12)).join('.'); diff --git a/bitcore.js b/bitcore.js index d712bc4..e99952f 100644 --- a/bitcore.js +++ b/bitcore.js @@ -15,6 +15,8 @@ module.exports.util = require('./util/util'); module.exports.Script = require('./Script'); module.exports.SINKey = require('./SINKey'); module.exports.Transaction = require('./Transaction'); +module.exports.Peer = require('./Peer'); +module.exports.Block = require('./Block'); if (typeof process.versions === 'undefined') { diff --git a/test/test.Block.js b/test/test.Block.js new file mode 100644 index 0000000..8932d6b --- /dev/null +++ b/test/test.Block.js @@ -0,0 +1,28 @@ +'use strict'; + +var chai = require('chai'); +var bitcore = require('../bitcore'); + +var should = chai.should(); + +var BlockModule = bitcore.Block; +var Block; + +describe('Block', function() { + it('should initialze the main object', function() { + should.exist(BlockModule); + }); + it('should be able to create class', function() { + Block = BlockModule.class(); + should.exist(Block); + }); + it('should be able to create instance', function() { + var p = new Block("localhost", 8333); + should.exist(p); + }); +}); + + + + + diff --git a/test/test.Peer.js b/test/test.Peer.js new file mode 100644 index 0000000..a2ff648 --- /dev/null +++ b/test/test.Peer.js @@ -0,0 +1,28 @@ +'use strict'; + +var chai = require('chai'); +var bitcore = require('../bitcore'); + +var should = chai.should(); + +var PeerModule = bitcore.Peer; +var Peer; + +describe('Peer', function() { + it('should initialze the main object', function() { + should.exist(PeerModule); + }); + it('should be able to create class', function() { + Peer = PeerModule.class(); + should.exist(Peer); + }); + it('should be able to create instance', function() { + var p = new Peer("localhost", 8333); + should.exist(p); + }); +}); + + + + + From d2e7c7fc1907984b2ea5ab1e1b7b063d9a36b3e7 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 13:01:19 -0300 Subject: [PATCH 03/11] fix buffertools in Transaction --- Transaction.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Transaction.js b/Transaction.js index 154015e..685b0a5 100644 --- a/Transaction.js +++ b/Transaction.js @@ -11,7 +11,7 @@ function spec(b) { var Put = b.Put || require('bufferput'); var Parser = b.Parser || require('./util/BinaryParser').class(); var Step = b.Step || require('step'); - var buffertools = require('buffertools'); + var buffertools = b.buffertools || require('buffertools'); var error = b.error || require('./util/error'); var VerificationError = error.VerificationError; @@ -29,14 +29,14 @@ function spec(b) { this.s = Buffer.isBuffer(data.s) ? data.s : Buffer.isBuffer(data.script) ? data.script : util.EMPTY_BUFFER; this.q = data.q ? data.q : data.sequence; - }; + } TransactionIn.prototype.getScript = function getScript() { return new Script(this.s); }; TransactionIn.prototype.isCoinBase = function isCoinBase() { - return this.o.compare(COINBASE_OP) === 0; + return buffertools.compare(this.o, COINBASE_OP) === 0; }; TransactionIn.prototype.serialize = function serialize() { @@ -153,11 +153,12 @@ function spec(b) { bufs.push(txout.serialize()); }); - var buf = new Buffer(4); + buf = new Buffer(4); buf.writeUInt32LE(this.lock_time, 0); bufs.push(buf); - return this._buffer = Buffer.concat(bufs); + this._buffer = Buffer.concat(bufs); + return this._buffer; }; Transaction.prototype.getBuffer = function getBuffer() { @@ -174,7 +175,7 @@ function spec(b) { Transaction.prototype.checkHash = function checkHash() { if (!this.hash || !this.hash.length) return false; - return this.calcHash().compare(this.hash) == 0; + return buffertools.compare(this.calcHash(), this.hash) === 0; }; Transaction.prototype.getHash = function getHash() { @@ -315,7 +316,7 @@ function spec(b) { // Spent output detected, retrieve transaction that spends it blockChain.getConflictingTransactions(outpoints, function (err, results) { if (results.length) { - if (results[0].getHash().compare(self.getHash()) == 0) { + if (buffertools.compare(results[0].getHash(), self.getHash()) === 0) { log.warn("Detected tx re-add (recoverable db corruption): " + util.formatHashAlt(results[0].getHash())); // TODO: Needs to return an error for the memory pool case? From 00962380049045c814d4a2e8d0545b1808479962 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 13:17:23 -0300 Subject: [PATCH 04/11] migrating buffertools compare method and test --- Block.js | 2 +- Connection.js | 5 +++-- Script.js | 5 +++-- ScriptInterpreter.js | 5 +++-- bitcore.js | 1 + test/test.Block.js | 2 +- test/test.Connection.js | 29 +++++++++++++++++++++++++++++ test/test.Peer.js | 2 +- test/test.ScriptInterpreter.js | 28 ++++++++++++++++++++++++++++ 9 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 test/test.Connection.js create mode 100644 test/test.ScriptInterpreter.js diff --git a/Block.js b/Block.js index f0abb5e..9854aa0 100644 --- a/Block.js +++ b/Block.js @@ -35,7 +35,7 @@ function spec(b) { this.active = data.active || false; this.chainWork = data.chainWork || util.EMPTY_BUFFER; this.txs = data.txs || []; - }; + } Block.prototype.getHeader = function getHeader() { var buf = new Buffer(80); diff --git a/Connection.js b/Connection.js index 6e9e365..6466417 100644 --- a/Connection.js +++ b/Connection.js @@ -17,6 +17,7 @@ function spec(b) { var Transaction = require('./Transaction').class(); var util = b.util || require('./util/util'); var Parser = b.Parser || require('./util/BinaryParser').class(); + var buffertools = b.buffertools || require('buffertools'); var doubleSha256 = b.doubleSha256 || util.twoSha256; var nonce = util.generateNonce(); @@ -114,7 +115,7 @@ function spec(b) { switch (message.command) { case 'version': // Did we connect to ourself? - if (nonce.compare(message.nonce) === 0) { + if (buffertools.compare(nonce, message.nonce) === 0) { this.socket.end(); return; } @@ -376,7 +377,7 @@ function spec(b) { if (checksum !== null) { var checksumConfirm = doubleSha256(payload).slice(0, 4); - if (checksumConfirm.compare(checksum) !== 0) { + if (buffertools.compare(checksumConfirm, checksum) !== 0) { log.err('['+this.peer+'] '+ 'Checksum failed', { cmd: command, diff --git a/Script.js b/Script.js index 33c4dee..5d9856c 100644 --- a/Script.js +++ b/Script.js @@ -4,7 +4,8 @@ function spec(b) { var config = b.config || require('./config'); var log = b.log || require('./util/log'); - var Opcode = require('./Opcode').class(); + var Opcode = b.Opcode || require('./Opcode').class(); + var buffertools = b.buffertools || require('buffertools'); // Make opcodes available as pseudo-constants for (var i in Opcode.map) { @@ -396,7 +397,7 @@ function spec(b) { if (Buffer.isBuffer(chunk)) { for (var i = 0, l = this.chunks.length; i < l; i++) { if (Buffer.isBuffer(this.chunks[i]) && - this.chunks[i].compare(chunk) == 0) { + buffertools.compare(this.chunks[i], chunk) === 0) { this.chunks.splice(i, 1); dirty = true; } diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index c83d18d..515e0be 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -5,7 +5,8 @@ function spec(b) { var config = b.config || require('./config'); var log = b.log || require('./util/log'); - var Opcode = require('./Opcode').class(); + var Opcode = b.Opcode || require('./Opcode').class(); + var buffertools = b.buffertools || require('buffertools'); // Make opcodes available as pseudo-constants for (var i in Opcode.map) { @@ -385,7 +386,7 @@ function spec(b) { // (x1 x2 - bool) var v1 = this.stackTop(2); var v2 = this.stackTop(1); - var value = v1.compare(v2) == 0; + var value = buffertools.compare(v1, v2) === 0; // OP_NOTEQUAL is disabled because it would be too easy to say // something like n != 1 and have some wiseguy pass in 1 with extra diff --git a/bitcore.js b/bitcore.js index e99952f..a2c33ac 100644 --- a/bitcore.js +++ b/bitcore.js @@ -17,6 +17,7 @@ module.exports.SINKey = require('./SINKey'); module.exports.Transaction = require('./Transaction'); module.exports.Peer = require('./Peer'); module.exports.Block = require('./Block'); +module.exports.Connection = require('./Connection'); if (typeof process.versions === 'undefined') { diff --git a/test/test.Block.js b/test/test.Block.js index 8932d6b..b65baa5 100644 --- a/test/test.Block.js +++ b/test/test.Block.js @@ -17,7 +17,7 @@ describe('Block', function() { should.exist(Block); }); it('should be able to create instance', function() { - var p = new Block("localhost", 8333); + var p = new Block(); should.exist(p); }); }); diff --git a/test/test.Connection.js b/test/test.Connection.js new file mode 100644 index 0000000..0c568c0 --- /dev/null +++ b/test/test.Connection.js @@ -0,0 +1,29 @@ +'use strict'; + +var chai = require('chai'); +var bitcore = require('../bitcore'); + +var should = chai.should(); + +var ConnectionModule = bitcore.Connection; +var Connection; + +describe('Connection', function() { + it('should initialze the main object', function() { + should.exist(ConnectionModule); + }); + it('should be able to create class', function() { + Connection = ConnectionModule.class(); + should.exist(Connection); + }); + it('should be able to create instance', function() { + var mSocket, mPeer; + var c = new Connection(mSocket, mPeer); + should.exist(c); + }); +}); + + + + + diff --git a/test/test.Peer.js b/test/test.Peer.js index a2ff648..477574b 100644 --- a/test/test.Peer.js +++ b/test/test.Peer.js @@ -17,7 +17,7 @@ describe('Peer', function() { should.exist(Peer); }); it('should be able to create instance', function() { - var p = new Peer("localhost", 8333); + var p = new Peer('localhost', 8333); should.exist(p); }); }); diff --git a/test/test.ScriptInterpreter.js b/test/test.ScriptInterpreter.js new file mode 100644 index 0000000..909a427 --- /dev/null +++ b/test/test.ScriptInterpreter.js @@ -0,0 +1,28 @@ +'use strict'; + +var chai = require('chai'); +var bitcore = require('../bitcore'); + +var should = chai.should(); + +var ScriptInterpreterModule = bitcore.ScriptInterpreter; +var ScriptInterpreter; + +describe('ScriptInterpreter', function() { + it('should initialze the main object', function() { + should.exist(ScriptInterpreterModule); + }); + it('should be able to create class', function() { + ScriptInterpreter = ScriptInterpreterModule.class(); + should.exist(ScriptInterpreter); + }); + it('should be able to create instance', function() { + var si = new ScriptInterpreter(); + should.exist(si); + }); +}); + + + + + From 1dcd9413312e720ce3b9cc649afd9fc017c94567 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 13:22:27 -0300 Subject: [PATCH 05/11] fixed various test problems --- bitcore.js | 1 + test/test.Connection.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bitcore.js b/bitcore.js index a2c33ac..f9b9ce0 100644 --- a/bitcore.js +++ b/bitcore.js @@ -18,6 +18,7 @@ module.exports.Transaction = require('./Transaction'); module.exports.Peer = require('./Peer'); module.exports.Block = require('./Block'); module.exports.Connection = require('./Connection'); +module.exports.ScriptInterpreter = require('./ScriptInterpreter'); if (typeof process.versions === 'undefined') { diff --git a/test/test.Connection.js b/test/test.Connection.js index 0c568c0..d1be479 100644 --- a/test/test.Connection.js +++ b/test/test.Connection.js @@ -7,6 +7,7 @@ var should = chai.should(); var ConnectionModule = bitcore.Connection; var Connection; +var nop = function() {}; describe('Connection', function() { it('should initialze the main object', function() { @@ -17,7 +18,8 @@ describe('Connection', function() { should.exist(Connection); }); it('should be able to create instance', function() { - var mSocket, mPeer; + var mSocket = {server: null, addListener: nop}, + mPeer; var c = new Connection(mSocket, mPeer); should.exist(c); }); From d96181898a8985c2dfb9262229bf65be2286fa2a Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 14:27:19 -0300 Subject: [PATCH 06/11] fixed concat references --- Block.js | 2 +- Script.js | 2 +- ScriptInterpreter.js | 6 +++--- Transaction.js | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Block.js b/Block.js index 9854aa0..c7c255c 100644 --- a/Block.js +++ b/Block.js @@ -182,7 +182,7 @@ function spec(b) { var i2 = Math.min(i + 1, size - 1); var a = tree[j + i]; var b = tree[j + i2]; - tree.push(util.twoSha256(a.concat(b))); + tree.push(util.twoSha256(buffertools.concat(a,b))); } j += size; } diff --git a/Script.js b/Script.js index 5d9856c..b174b5a 100644 --- a/Script.js +++ b/Script.js @@ -382,7 +382,7 @@ function spec(b) { Script.prototype.writeBytes = function (data) { var newSize = this.buffer.length + prefixSize(data.length) + data.length; - this.buffer = Buffer.concat([this.buffer, encodeLen(data.length), data]); + this.buffer = buffertools.concat(this.buffer, encodeLen(data.length), data); this.chunks.push(data); }; diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 515e0be..83253e9 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -299,11 +299,11 @@ function spec(b) { case OP_CAT: // (x1 x2 -- out) - var v1 = this.stackTop(2); - var v2 = this.stackTop(1); + v1 = this.stackTop(2); + v2 = this.stackTop(1); this.stackPop(); this.stackPop(); - this.stack.push(v1.concat(v2)); + this.stack.push(buffertools.concat(v1, v2)); break; case OP_SUBSTR: diff --git a/Transaction.js b/Transaction.js index 685b0a5..9fc5052 100644 --- a/Transaction.js +++ b/Transaction.js @@ -17,7 +17,7 @@ function spec(b) { var VerificationError = error.VerificationError; var MissingSourceError = error.MissingSourceError; - var COINBASE_OP = buffertools.concat(util.NULL_HASH, new Buffer("FFFFFFFF", 'hex')); + var COINBASE_OP = buffertools.concat(util.NULL_HASH, new Buffer('FFFFFFFF', 'hex')); function TransactionIn(data) { if ("object" !== typeof data) { @@ -44,7 +44,7 @@ function spec(b) { var qbuf = new Buffer(4); qbuf.writeUInt32LE(this.q, 0); - return Buffer.concat([this.o, slen, this.s, qbuf]); + return buffertools.concat(this.o, slen, this.s, qbuf); }; TransactionIn.prototype.getOutpointHash = function getOutpointHash() { @@ -88,7 +88,7 @@ function spec(b) { TransactionOut.prototype.serialize = function serialize() { var slen = util.varIntBuf(this.s.length); - return Buffer.concat([this.v, slen, this.s]); + return buffertools.concat(his.v, slen, this.s); }; function Transaction(data) { @@ -537,7 +537,7 @@ function spec(b) { var buffer = bytes.buffer(); // Append hashType - buffer = buffer.concat(new Buffer([parseInt(hashType), 0, 0, 0])); + buffer = buffertools.concat(buffer, new Buffer([parseInt(hashType), 0, 0, 0])); return util.twoSha256(buffer); }; @@ -612,7 +612,7 @@ function spec(b) { var voutBuf = new Buffer(4); voutBuf.writeUInt32LE(vout, 0); - txin.o = Buffer.concat([hash, voutBuf]); + txin.o = buffertools.concat(hash, voutBuf); txobj.ins.push(txin); }); From bf0010b8ac7b8438a20e44d69c2d2e5f9ae054a4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 14:36:35 -0300 Subject: [PATCH 07/11] fix buffertool's fill calls --- bitcore.js | 1 + networks.js | 5 +++-- util/util.js | 9 ++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bitcore.js b/bitcore.js index f9b9ce0..c6c9d94 100644 --- a/bitcore.js +++ b/bitcore.js @@ -19,6 +19,7 @@ module.exports.Peer = require('./Peer'); module.exports.Block = require('./Block'); module.exports.Connection = require('./Connection'); module.exports.ScriptInterpreter = require('./ScriptInterpreter'); +module.exports.networks = require('./networks'); if (typeof process.versions === 'undefined') { diff --git a/networks.js b/networks.js index 97a1fa9..07e10f3 100644 --- a/networks.js +++ b/networks.js @@ -1,4 +1,5 @@ var Put = require('bufferput'); +var buffertools = require('buffertools'); var hex = function(hex) {return new Buffer(hex, 'hex');}; exports.livenet = { @@ -10,7 +11,7 @@ exports.livenet = { nonce: 2083236893, version: 1, hash: hex('6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000'), - prev_hash: new Buffer(32).fill(0), + prev_hash: buffertools.fill(new Buffer(32), 0), timestamp: 1231006505, merkle_root: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'), bits: 486604799 @@ -53,7 +54,7 @@ exports.testnet = { nonce: 414098458, version: 1, hash: hex('43497FD7F826957108F4A30FD9CEC3AEBA79972084E90EAD01EA330900000000'), - prev_hash: new Buffer(32).fill(0), + prev_hash: buffertools.fill(new Buffer(32), 0), timestamp: 1296688602, merkle_root: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'), bits: 486604799, diff --git a/util/util.js b/util/util.js index e67ae83..dec16ff 100644 --- a/util/util.js +++ b/util/util.js @@ -225,7 +225,8 @@ var decodeDiffBits = exports.decodeDiffBits = function (diffBits, asBigInt) { // Convert to buffer var diffBuf = target.toBuffer(); - var targetBuf = new Buffer(32).fill(0); + var targetBuf = new Buffer(32); + buffertools.fill(targetBuf, 0); diffBuf.copy(targetBuf, 32-diffBuf.length); return targetBuf; }; @@ -325,11 +326,9 @@ var varStrBuf = exports.varStrBuf = function varStrBuf(s) { }; // Initializations -exports.NULL_HASH = new Buffer(32) -buffertools.fill(exports.NULL_HASH, 0); +exports.NULL_HASH = buffertools.fill(new Buffer(32), 0); exports.EMPTY_BUFFER = new Buffer(0); -exports.ZERO_VALUE = new Buffer(8) -buffertools.fill(exports.ZERO_VALUE, 0); +exports.ZERO_VALUE = buffertools.fill(new Buffer(8), 0); var INT64_MAX = new Buffer('ffffffffffffffff', 'hex'); exports.INT64_MAX = INT64_MAX; From 966b8988e1e74342eec47be2313332ba35915f3a Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 15:10:58 -0300 Subject: [PATCH 08/11] fix reverse buffertools use --- Block.js | 4 ++-- ScriptInterpreter.js | 10 +++++----- Transaction.js | 4 ++-- util/util.js | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Block.js b/Block.js index c7c255c..c1ce8fb 100644 --- a/Block.js +++ b/Block.js @@ -94,14 +94,14 @@ function spec(b) { // TODO: Create a compare method in node-buffertools that uses the correct // endian so we don't have to reverse both buffers before comparing. - this.hash.reverse(); + buffertools.reverse(this.hash); if (buffertools.compare(this.hash, target) > 0) { throw new VerificationError('Difficulty target not met'); } // Return the hash to its normal order - this.hash.reverse(); + buffertools.reverse(this.hash); return true; }; diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 83253e9..dbe4bb5 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -836,7 +836,7 @@ function spec(b) { var w = new Buffer(v.length); v.copy(w); - w.reverse(); + w = buffertools.reverse(w); if (w[0] & 0x80) { w[0] &= 0x7f; return bignum.fromBuffer(w).neg(); @@ -859,9 +859,9 @@ function spec(b) { c = new Buffer(b.length + 1); b.copy(c, 1); c[0] = 0; - return c.reverse(); + return buffertools.reverse(c); } else { - return b.reverse(); + return buffertools.reverse(b); } } else if (cmp == 0) { return new Buffer([]); @@ -871,10 +871,10 @@ function spec(b) { c = new Buffer(b.length + 1); b.copy(c, 1); c[0] = 0x80; - return c.reverse(); + return buffertools.reverse(c); } else { b[0] |= 0x80; - return b.reverse(); + return buffertools.reverse(b); } } }; diff --git a/Transaction.js b/Transaction.js index 9fc5052..546ede2 100644 --- a/Transaction.js +++ b/Transaction.js @@ -557,7 +557,7 @@ function spec(b) { var ins = this.ins.map(function (txin) { var txinObj = { prev_out: { - hash: new Buffer(txin.getOutpointHash()).reverse().toString('hex'), + hash: buffertools.reverse(new Buffer(txin.getOutpointHash())).toString('hex'), n: txin.getOutpointIndex() } }; @@ -607,7 +607,7 @@ function spec(b) { txin.q = 0xffffffff; var hash = new Buffer(inputobj.txid, 'hex'); - hash.reverse(); + hash = buffertools.reverse(hash); var vout = parseInt(inputobj.vout); var voutBuf = new Buffer(4); voutBuf.writeUInt32LE(vout, 0); diff --git a/util/util.js b/util/util.js index dec16ff..e33a676 100644 --- a/util/util.js +++ b/util/util.js @@ -32,7 +32,7 @@ var formatHash = exports.formatHash = function (hash) { // Make a copy, because reverse() and toHex() are destructive. var hashEnd = new Buffer(10); hash.copy(hashEnd, 0, 22, 32); - return hashEnd.reverse().toString('hex'); + return buffertools.reverse(hashEnd).toString('hex'); }; /** @@ -42,7 +42,7 @@ var formatHashFull = exports.formatHashFull = function (hash) { // Make a copy, because reverse() and toHex() are destructive. var copy = new Buffer(hash.length); hash.copy(copy); - var hex = copy.reverse().toHex(); + var hex = buffertools.reverse(copy).toHex(); return hex; }; From 4da3285930708d6536f02c88d7fe9d15693c199b Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 15:14:56 -0300 Subject: [PATCH 09/11] fix toHex use --- Connection.js | 6 +++--- ScriptInterpreter.js | 4 ++-- util/util.js | 6 ++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Connection.js b/Connection.js index 6466417..990e723 100644 --- a/Connection.js +++ b/Connection.js @@ -52,7 +52,7 @@ function spec(b) { } this.setupHandlers(); - }; + } Connection.superclass = b.superclass || require('events').EventEmitter; Connection.prototype.setupHandlers = function () { @@ -63,8 +63,8 @@ function spec(b) { var dumpLen = 35; log.debug('['+this.peer+'] '+ 'Recieved '+data.length+' bytes of data:'); - log.debug('... '+ data.slice(0, dumpLen > data.length ? - data.length : dumpLen).toHex() + + log.debug('... '+ buffertools.toHex(data.slice(0, dumpLen > data.length ? + data.length : dumpLen)) + (data.length > dumpLen ? '...' : '')); }).bind(this)); this.socket.addListener('data', this.handleData.bind(this)); diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index dbe4bb5..1af1f51 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -798,13 +798,13 @@ function spec(b) { ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() { return this.stack.map(function (entry) { if (entry.length > 2) { - return entry.slice(0).toHex(); + return buffertools.toHex(entry.slice(0)); } var num = castBigint(entry); if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) { return num.toNumber(); } else { - return entry.slice(0).toHex(); + return buffertools.toHex(entry.slice(0)); } }); }; diff --git a/util/util.js b/util/util.js index e33a676..4621774 100644 --- a/util/util.js +++ b/util/util.js @@ -29,7 +29,6 @@ var sha256ripe160 = exports.sha256ripe160 = function (data) { * Format a block hash like the official client does. */ var formatHash = exports.formatHash = function (hash) { - // Make a copy, because reverse() and toHex() are destructive. var hashEnd = new Buffer(10); hash.copy(hashEnd, 0, 22, 32); return buffertools.reverse(hashEnd).toString('hex'); @@ -39,10 +38,9 @@ var formatHash = exports.formatHash = function (hash) { * Display the whole hash, as hex, in correct endian order. */ var formatHashFull = exports.formatHashFull = function (hash) { - // Make a copy, because reverse() and toHex() are destructive. var copy = new Buffer(hash.length); hash.copy(copy); - var hex = buffertools.reverse(copy).toHex(); + var hex = buffertools.toHex(buffertools.reverse(copy)); return hex; }; @@ -71,7 +69,7 @@ var formatBuffer = exports.formatBuffer = function (buffer, maxLen) { buffer.copy(temp, 0, 0, maxLen); // Format as string - var output = temp.toHex(); + var output = buffertools.toHex(temp); if (temp.length < buffer.length) { output += "..."; } From e0233f02dd513460e3299175ec0e7fd6aa792b66 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 16:03:29 -0300 Subject: [PATCH 10/11] fix Buffer.concat use --- Block.js | 2 +- Script.js | 2 +- ScriptInterpreter.js | 2 +- Transaction.js | 10 +++++----- util/util.js | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Block.js b/Block.js index c1ce8fb..c1f5238 100644 --- a/Block.js +++ b/Block.js @@ -182,7 +182,7 @@ function spec(b) { var i2 = Math.min(i + 1, size - 1); var a = tree[j + i]; var b = tree[j + i2]; - tree.push(util.twoSha256(buffertools.concat(a,b))); + tree.push(util.twoSha256(Buffer.concat([a,b]))); } j += size; } diff --git a/Script.js b/Script.js index b174b5a..5d9856c 100644 --- a/Script.js +++ b/Script.js @@ -382,7 +382,7 @@ function spec(b) { Script.prototype.writeBytes = function (data) { var newSize = this.buffer.length + prefixSize(data.length) + data.length; - this.buffer = buffertools.concat(this.buffer, encodeLen(data.length), data); + this.buffer = Buffer.concat([this.buffer, encodeLen(data.length), data]); this.chunks.push(data); }; diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 1af1f51..2050ccf 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -303,7 +303,7 @@ function spec(b) { v2 = this.stackTop(1); this.stackPop(); this.stackPop(); - this.stack.push(buffertools.concat(v1, v2)); + this.stack.push(Buffer.concat([v1, v2])); break; case OP_SUBSTR: diff --git a/Transaction.js b/Transaction.js index 546ede2..f0bcd1f 100644 --- a/Transaction.js +++ b/Transaction.js @@ -17,7 +17,7 @@ function spec(b) { var VerificationError = error.VerificationError; var MissingSourceError = error.MissingSourceError; - var COINBASE_OP = buffertools.concat(util.NULL_HASH, new Buffer('FFFFFFFF', 'hex')); + var COINBASE_OP = Buffer.concat([util.NULL_HASH, new Buffer('FFFFFFFF', 'hex')]); function TransactionIn(data) { if ("object" !== typeof data) { @@ -44,7 +44,7 @@ function spec(b) { var qbuf = new Buffer(4); qbuf.writeUInt32LE(this.q, 0); - return buffertools.concat(this.o, slen, this.s, qbuf); + return Buffer.concat([this.o, slen, this.s, qbuf]); }; TransactionIn.prototype.getOutpointHash = function getOutpointHash() { @@ -88,7 +88,7 @@ function spec(b) { TransactionOut.prototype.serialize = function serialize() { var slen = util.varIntBuf(this.s.length); - return buffertools.concat(his.v, slen, this.s); + return Buffer.concat([his.v, slen, this.s]); }; function Transaction(data) { @@ -537,7 +537,7 @@ function spec(b) { var buffer = bytes.buffer(); // Append hashType - buffer = buffertools.concat(buffer, new Buffer([parseInt(hashType), 0, 0, 0])); + buffer = Buffer.concat([buffer, new Buffer([parseInt(hashType), 0, 0, 0])]); return util.twoSha256(buffer); }; @@ -612,7 +612,7 @@ function spec(b) { var voutBuf = new Buffer(4); voutBuf.writeUInt32LE(vout, 0); - txin.o = buffertools.concat(hash, voutBuf); + txin.o = Buffer.concat([hash, voutBuf]); txobj.ins.push(txin); }); diff --git a/util/util.js b/util/util.js index 4621774..c3eb7f4 100644 --- a/util/util.js +++ b/util/util.js @@ -320,7 +320,7 @@ var varIntBuf = exports.varIntBuf = function varIntBuf(n) { }; var varStrBuf = exports.varStrBuf = function varStrBuf(s) { - return buffertools.concat(varIntBuf(s.length), s); + return Buffer.concat([varIntBuf(s.length), s]); }; // Initializations From 1c9b89f4f320c633f04f2e936822103162f572e9 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Feb 2014 17:58:16 -0300 Subject: [PATCH 11/11] reverted some unwanted changes --- ScriptInterpreter.js | 4 ++-- Transaction.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 2050ccf..35ec400 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -299,8 +299,8 @@ function spec(b) { case OP_CAT: // (x1 x2 -- out) - v1 = this.stackTop(2); - v2 = this.stackTop(1); + var v1 = this.stackTop(2); + var v2 = this.stackTop(1); this.stackPop(); this.stackPop(); this.stack.push(Buffer.concat([v1, v2])); diff --git a/Transaction.js b/Transaction.js index f0bcd1f..d362039 100644 --- a/Transaction.js +++ b/Transaction.js @@ -88,7 +88,7 @@ function spec(b) { TransactionOut.prototype.serialize = function serialize() { var slen = util.varIntBuf(this.s.length); - return Buffer.concat([his.v, slen, this.s]); + return Buffer.concat([this.v, slen, this.s]); }; function Transaction(data) { @@ -153,7 +153,7 @@ function spec(b) { bufs.push(txout.serialize()); }); - buf = new Buffer(4); + var buf = new Buffer(4); buf.writeUInt32LE(this.lock_time, 0); bufs.push(buf);