Skip to content

Commit

Permalink
Merge branch 'micahriggan-feature/buffer-performance'
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Mar 11, 2018
2 parents 0064012 + 8ddec3e commit 026ddb4
Show file tree
Hide file tree
Showing 27 changed files with 99 additions and 95 deletions.
2 changes: 1 addition & 1 deletion benchmark/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var bitcore = require('..');
var async = require('async');
var blockData = require('./block-357238.json');

var maxTime = 10;
var maxTime = 30;

console.log('Benchmarking Script');
console.log('---------------------------------------');
Expand Down
9 changes: 4 additions & 5 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Address._transformObject = function(data) {
$.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property');
$.checkArgument(data.type, 'Must provide a `type` property');
return {
hashBuffer: data.hash ? new Buffer(data.hash, 'hex') : data.hashBuffer,
hashBuffer: data.hash ? Buffer.from(data.hash, 'hex') : data.hashBuffer,
network: Networks.get(data.network) || Networks.defaultNetwork,
type: data.type
};
Expand Down Expand Up @@ -399,7 +399,7 @@ Address.fromObject = function fromObject(obj) {
JSUtil.isHexa(obj.hash),
'Unexpected hash property, "' + obj.hash + '", expected to be hex.'
);
var hashBuffer = new Buffer(obj.hash, 'hex');
var hashBuffer = Buffer.from(obj.hash, 'hex');
return new Address(hashBuffer, obj.network, obj.type);
};

Expand Down Expand Up @@ -467,9 +467,8 @@ Address.prototype.isPayToScriptHash = function() {
* @returns {Buffer} Bitcoin address buffer
*/
Address.prototype.toBuffer = function() {
var version = new Buffer([this.network[this.type]]);
var buf = Buffer.concat([version, this.hashBuffer]);
return buf;
var version = Buffer.from([this.network[this.type]]);
return Buffer.concat([version, this.hashBuffer]);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/block/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Block.fromBuffer = function fromBuffer(buf) {
* @returns {Block} - A hex encoded string of the block
*/
Block.fromString = function fromString(str) {
var buf = new Buffer(str, 'hex');
var buf = Buffer.from(str, 'hex');
return Block.fromBuffer(buf);
};

Expand All @@ -127,7 +127,7 @@ Block.fromString = function fromString(str) {
*/
Block.fromRawBlock = function fromRawBlock(data) {
if (!BufferUtil.isBuffer(data)) {
data = new Buffer(data, 'binary');
data = Buffer.from(data, 'binary');
}
var br = BufferReader(data);
br.pos = Block.Values.START_OF_BLOCK;
Expand Down Expand Up @@ -275,7 +275,7 @@ Block.prototype.inspect = function inspect() {

Block.Values = {
START_OF_BLOCK: 8, // Start of block in raw block data
NULL_HASH: new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
NULL_HASH: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
};

module.exports = Block;
8 changes: 4 additions & 4 deletions lib/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ BlockHeader._fromObject = function _fromObject(data) {
var prevHash = data.prevHash;
var merkleRoot = data.merkleRoot;
if (_.isString(data.prevHash)) {
prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex'));
prevHash = BufferUtil.reverse(Buffer.from(data.prevHash, 'hex'));
}
if (_.isString(data.merkleRoot)) {
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex'));
merkleRoot = BufferUtil.reverse(Buffer.from(data.merkleRoot, 'hex'));
}
var info = {
hash: data.hash,
Expand Down Expand Up @@ -103,7 +103,7 @@ BlockHeader.fromObject = function fromObject(obj) {
*/
BlockHeader.fromRawBlock = function fromRawBlock(data) {
if (!BufferUtil.isBuffer(data)) {
data = new Buffer(data, 'binary');
data = Buffer.from(data, 'binary');
}
var br = BufferReader(data);
br.pos = BlockHeader.Constants.START_OF_HEADER;
Expand All @@ -125,7 +125,7 @@ BlockHeader.fromBuffer = function fromBuffer(buf) {
* @returns {BlockHeader} - An instance of block header
*/
BlockHeader.fromString = function fromString(str) {
var buf = new Buffer(str, 'hex');
var buf = Buffer.from(str, 'hex');
return BlockHeader.fromBuffer(buf);
};

Expand Down
6 changes: 3 additions & 3 deletions lib/block/merkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ MerkleBlock.prototype.toBufferWriter = function toBufferWriter(bw) {
bw.writeUInt32LE(this.numTransactions);
bw.writeVarintNum(this.hashes.length);
for (var i = 0; i < this.hashes.length; i++) {
bw.write(new Buffer(this.hashes[i], 'hex'));
bw.write(Buffer.from(this.hashes[i], 'hex'));
}
bw.writeVarintNum(this.flags.length);
for (i = 0; i < this.flags.length; i++) {
Expand Down Expand Up @@ -219,7 +219,7 @@ MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, p
if(depth === 0 && isParentOfMatch) {
opts.txs.push(hash);
}
return new Buffer(hash, 'hex');
return Buffer.from(hash, 'hex');
} else {
var left = this._traverseMerkleTree(depth-1, pos*2, opts);
var right = left;
Expand Down Expand Up @@ -270,7 +270,7 @@ MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) {
var hash = tx;
if(tx instanceof Transaction) {
// We need to reverse the id hash for the lookup
hash = BufferUtil.reverse(new Buffer(tx.id, 'hex')).toString('hex');
hash = BufferUtil.reverse(Buffer.from(tx.id, 'hex')).toString('hex');
}

var txs = [];
Expand Down
16 changes: 8 additions & 8 deletions lib/crypto/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var $ = require('../util/preconditions');
var _ = require('lodash');

var reversebuf = function(buf) {
var buf2 = new Buffer(buf.length);
var buf2 = Buffer.alloc(buf.length);
for (var i = 0; i < buf.length; i++) {
buf2[i] = buf[buf.length - 1 - i];
}
Expand Down Expand Up @@ -42,7 +42,7 @@ BN.fromBuffer = function(buf, opts) {
BN.fromSM = function(buf, opts) {
var ret;
if (buf.length === 0) {
return BN.fromBuffer(new Buffer([0]));
return BN.fromBuffer(Buffer.from([0]));
}

var endian = 'big';
Expand Down Expand Up @@ -73,7 +73,7 @@ BN.prototype.toBuffer = function(opts) {
if (opts && opts.size) {
hex = this.toString(16, 2);
var natlen = hex.length / 2;
buf = new Buffer(hex, 'hex');
buf = Buffer.from(hex, 'hex');

if (natlen === opts.size) {
buf = buf;
Expand All @@ -84,7 +84,7 @@ BN.prototype.toBuffer = function(opts) {
}
} else {
hex = this.toString(16, 2);
buf = new Buffer(hex, 'hex');
buf = Buffer.from(hex, 'hex');
}

if (typeof opts !== 'undefined' && opts.endian === 'little') {
Expand All @@ -99,19 +99,19 @@ BN.prototype.toSMBigEndian = function() {
if (this.cmp(BN.Zero) === -1) {
buf = this.neg().toBuffer();
if (buf[0] & 0x80) {
buf = Buffer.concat([new Buffer([0x80]), buf]);
buf = Buffer.concat([Buffer.from([0x80]), buf]);
} else {
buf[0] = buf[0] | 0x80;
}
} else {
buf = this.toBuffer();
if (buf[0] & 0x80) {
buf = Buffer.concat([new Buffer([0x00]), buf]);
buf = Buffer.concat([Buffer.from([0x00]), buf]);
}
}

if (buf.length === 1 & buf[0] === 0) {
buf = new Buffer([]);
buf = Buffer.from([]);
}
return buf;
};
Expand Down Expand Up @@ -189,7 +189,7 @@ BN.trim = function(buf, natlen) {
};

BN.pad = function(buf, natlen, size) {
var rbuf = new Buffer(size);
var rbuf = Buffer.alloc(size);
for (var i = 0; i < buf.length; i++) {
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
}
Expand Down
12 changes: 6 additions & 6 deletions lib/crypto/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ ECDSA.prototype.deterministicK = function(badrs) {
if (_.isUndefined(badrs)) {
badrs = 0;
}
var v = new Buffer(32);
var v = Buffer.alloc(32);
v.fill(0x01);
var k = new Buffer(32);
var k = Buffer.alloc(32);
k.fill(0x00);
var x = this.privkey.bn.toBuffer({
size: 32
});
var hashbuf = this.endian === 'little' ? BufferUtil.reverse(this.hashbuf) : this.hashbuf
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00]), x, hashbuf]), k);
k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00]), x, hashbuf]), k);
v = Hash.sha256hmac(v, k);
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x01]), x, hashbuf]), k);
k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x01]), x, hashbuf]), k);
v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k);
var T = BN.fromBuffer(v);
var N = Point.getN();

// also explained in 3.2, we must ensure T is in the proper range (0, N)
for (var i = 0; i < badrs || !(T.lt(N) && T.gt(BN.Zero)); i++) {
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00])]), k);
k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00])]), k);
v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k);
T = BN.fromBuffer(v);
Expand Down Expand Up @@ -192,7 +192,7 @@ ECDSA.prototype.sigError = function() {
ECDSA.toLowS = function(s) {
//enforce low s
//see BIP 62, "low S values in signatures"
if (s.gt(BN.fromBuffer(new Buffer('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex')))) {
if (s.gt(BN.fromBuffer(Buffer.from('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex')))) {
s = Point.getN().sub(s);
}
return s;
Expand Down
10 changes: 5 additions & 5 deletions lib/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ Hash.hmac = function(hashf, data, key) {
if (key.length > blocksize) {
key = hashf(key);
} else if (key < blocksize) {
var fill = new Buffer(blocksize);
var fill = Buffer.alloc(blocksize);
fill.fill(0);
key.copy(fill);
key = fill;
}

var o_key = new Buffer(blocksize);
var o_key = Buffer.alloc(blocksize);
o_key.fill(0x5c);

var i_key = new Buffer(blocksize);
var i_key = Buffer.alloc(blocksize);
i_key.fill(0x36);

var o_key_pad = new Buffer(blocksize);
var i_key_pad = new Buffer(blocksize);
var o_key_pad = Buffer.alloc(blocksize);
var i_key_pad = Buffer.alloc(blocksize);
for (var i = 0; i < blocksize; i++) {
o_key_pad[i] = o_key[i] ^ key[i];
i_key_pad[i] = i_key[i] ^ key[i];
Expand Down
4 changes: 2 additions & 2 deletions lib/crypto/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ Point.pointToCompressed = function pointToCompressed(point) {
var prefix;
var odd = ybuf[ybuf.length - 1] % 2;
if (odd) {
prefix = new Buffer([0x03]);
prefix = Buffer.from([0x03]);
} else {
prefix = new Buffer([0x02]);
prefix = Buffer.from([0x02]);
}
return BufferUtil.concat([prefix, xbuf]);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Random.getRandomBufferBrowser = function(size) {

var bbuf = new Uint8Array(size);
crypto.getRandomValues(bbuf);
var buf = new Buffer(bbuf);
var buf = Buffer.from(bbuf);

return buf;
};

/* insecure random bytes, but it never fails */
Random.getPseudoRandomBuffer = function(size) {
var b32 = 0x100000000;
var b = new Buffer(size);
var b = Buffer.alloc(size);
var r;

for (var i = 0; i <= size; i++) {
Expand Down
12 changes: 6 additions & 6 deletions lib/crypto/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Signature.fromTxFormat = function(buf) {
};

Signature.fromString = function(str) {
var buf = new Buffer(str, 'hex');
var buf = Buffer.from(str, 'hex');
return Signature.fromDER(buf);
};

Expand Down Expand Up @@ -155,7 +155,7 @@ Signature.prototype.toCompact = function(i, compressed) {
if (compressed === false) {
val = val - 4;
}
var b1 = new Buffer([val]);
var b1 = Buffer.from([val]);
var b2 = this.r.toBuffer({
size: 32
});
Expand All @@ -172,8 +172,8 @@ Signature.prototype.toBuffer = Signature.prototype.toDER = function() {
var rneg = rnbuf[0] & 0x80 ? true : false;
var sneg = snbuf[0] & 0x80 ? true : false;

var rbuf = rneg ? Buffer.concat([new Buffer([0x00]), rnbuf]) : rnbuf;
var sbuf = sneg ? Buffer.concat([new Buffer([0x00]), snbuf]) : snbuf;
var rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf;
var sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf;

var rlength = rbuf.length;
var slength = sbuf.length;
Expand All @@ -182,7 +182,7 @@ Signature.prototype.toBuffer = Signature.prototype.toDER = function() {
var sheader = 0x02;
var header = 0x30;

var der = Buffer.concat([new Buffer([header, length, rheader, rlength]), rbuf, new Buffer([sheader, slength]), sbuf]);
var der = Buffer.concat([Buffer.from([header, length, rheader, rlength]), rbuf, Buffer.from([sheader, slength]), sbuf]);
return der;
};

Expand Down Expand Up @@ -300,7 +300,7 @@ Signature.prototype.hasDefinedHashtype = function() {

Signature.prototype.toTxFormat = function() {
var derbuf = this.toDER();
var buf = new Buffer(1);
var buf = Buffer.alloc(1);
buf.writeUInt8(this.nhashtype, 0);
return Buffer.concat([derbuf, buf]);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/encoding/base58.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Base58.decode = function(str) {
if (typeof str !== 'string') {
throw new Error('Input should be a string');
}
return new Buffer(bs58.decode(str));
return Buffer.from(bs58.decode(str));
};

Base58.prototype.fromBuffer = function(buf) {
Expand Down
4 changes: 2 additions & 2 deletions lib/encoding/base58check.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Base58Check.decode = function(s) {
if (typeof s !== 'string')
throw new Error('Input must be a string');

var buf = new Buffer(Base58.decode(s));
var buf = Buffer.from(Base58.decode(s));

if (buf.length < 4)
throw new Error("Input string too short");
Expand All @@ -66,7 +66,7 @@ Base58Check.checksum = function(buffer) {
Base58Check.encode = function(buf) {
if (!Buffer.isBuffer(buf))
throw new Error('Input must be a buffer');
var checkedBuf = new Buffer(buf.length + 4);
var checkedBuf = Buffer.alloc(buf.length + 4);
var hash = Base58Check.checksum(buf);
buf.copy(checkedBuf);
hash.copy(checkedBuf, buf.length);
Expand Down
4 changes: 2 additions & 2 deletions lib/encoding/bufferreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var BufferReader = function BufferReader(buf) {
});
} else if (_.isString(buf)) {
this.set({
buf: new Buffer(buf, 'hex'),
buf: Buffer.from(buf, 'hex'),
});
} else if (_.isObject(buf)) {
var obj = buf;
Expand Down Expand Up @@ -178,7 +178,7 @@ BufferReader.prototype.readVarintBN = function() {
};

BufferReader.prototype.reverse = function() {
var buf = new Buffer(this.buf.length);
var buf = Buffer.alloc(this.buf.length);
for (var i = 0; i < buf.length; i++) {
buf[i] = this.buf[this.buf.length - 1 - i];
}
Expand Down
Loading

0 comments on commit 026ddb4

Please sign in to comment.