more utils cleanup.

This commit is contained in:
Christopher Jeffrey 2016-04-18 01:11:02 -07:00
parent 69ddca52a8
commit f8d49f0653
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 28 additions and 55 deletions

View File

@ -6,6 +6,7 @@
*/
var utils = require('./utils');
var assert = utils.assert;
/**
* Bloom Filter
@ -67,7 +68,8 @@ Bloom.prototype.reset = function reset() {
Bloom.prototype.add = function add(val, enc) {
var i, bit, pos, shift;
val = utils.toBuffer(val, enc);
if (typeof val === 'string')
val = new Buffer(val, enc);
for (i = 0; i < this.n; i++) {
bit = this.hash(val, i);
@ -89,7 +91,8 @@ Bloom.prototype.add = function add(val, enc) {
Bloom.prototype.test = function test(val, enc) {
var i, bit, pos, shift;
val = utils.toBuffer(val, enc);
if (typeof val === 'string')
val = new Buffer(val, enc);
for (i = 0; i < this.n; i++) {
bit = this.hash(val, i);
@ -176,7 +179,7 @@ function murmur(data, seed) {
var hash = seed;
var i, w, r, j;
data = utils.toBuffer(data);
assert(Buffer.isBuffer(data));
for (i = 0; i + 4 <= data.length; i += 4) {
w = data[i]

View File

@ -1668,7 +1668,7 @@ Chain.prototype.purgePending = function purgePending() {
/**
* Test the chain to see if it has a block, orphan, or pending block.
* @param {Block|MerkleBlock|Buffer|Hash} hash
* @param {Hash} hash
* @param {Function} callback - Returns [Error, Boolean].
*/
@ -1747,9 +1747,6 @@ Chain.prototype.byTime = function byTime(ts, callback, force) {
*/
Chain.prototype.hasBlock = function hasBlock(hash, callback) {
if (typeof hash === 'number')
return this.db.has(hash, callback);
return this.db.has(hash, callback);
};
@ -1780,8 +1777,6 @@ Chain.prototype.hasPending = function hasPending(hash) {
*/
Chain.prototype.getEntry = function getEntry(hash, callback) {
if (typeof hash === 'number')
return this.db.get(hash, callback);
return this.db.get(hash, callback);
};

View File

@ -42,7 +42,9 @@ function MerkleBlock(data) {
bcoin.abstractblock.call(this, data);
this.hashes = (data.hashes || []).map(function(hash) {
return utils.toBuffer(hash, 'hex');
if (typeof hash === 'string')
hash = new Buffer(hash, 'hex');
return hash;
});
this.flags = data.flags || [];

View File

@ -68,37 +68,10 @@ utils.slice = function slice(buf, start, end) {
return clone;
};
/**
* Convert objects to a Buffer.
* @param {Buffer|Array|String} msg
* @param {String} enc - Any buffer-supported
* encoding as well as `base58`.
* @returns {Buffer}
/*
* Base58
*/
utils.toBuffer = function toBuffer(msg, enc) {
if (Buffer.isBuffer(msg))
return msg;
if (Array.isArray(msg))
return new Buffer(msg);
if (!msg)
return new Buffer([]);
if (typeof msg === 'string') {
if (!enc)
return new Buffer(msg, 'ascii');
if (enc === 'base58')
return utils.fromBase58(msg);
return new Buffer(msg, enc);
}
assert(false);
};
var base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ'
+ 'abcdefghijkmnopqrstuvwxyz';

View File

@ -59,7 +59,7 @@ function createGenesisBlock(options) {
block = {
version: options.version,
prevBlock: constants.NULL_HASH,
merkleRoot: utils.toHex(utils.dsha256(txRaw)),
merkleRoot: utils.dsha256(txRaw).toString('hex'),
ts: options.ts,
bits: options.bits,
nonce: options.nonce,
@ -71,7 +71,7 @@ function createGenesisBlock(options) {
block = parser.parseBlock(blockRaw);
block._hash = utils.dsha256(blockRaw.slice(0, 80));
block.hash = utils.toHex(block._hash);
block.hash = block._hash.toString('hex');
block._raw = blockRaw;
block._size = blockRaw.length;
block._witnessSize = 0;
@ -81,7 +81,7 @@ function createGenesisBlock(options) {
tx.height = 0;
tx.ts = block.ts;
tx._hash = block.merkleRoot;
tx.hash = utils.toHex(tx._hash);
tx.hash = tx._hash.toString('hex');
return block;
}

View File

@ -5,25 +5,25 @@ describe('Bloom', function() {
it('should do proper murmur3', function() {
var h = bcoin.bloom.hash;
assert.equal(h('', 0), 0);
assert.equal(h('', 0xfba4c795), 0x6a396f08);
assert.equal(h('00', 0xfba4c795), 0x2a101837);
assert.equal(h('hello world', 0), 0x5e928f0f);
assert.equal(h(new Buffer('', 'ascii'), 0), 0);
assert.equal(h(new Buffer('', 'ascii'), 0xfba4c795), 0x6a396f08);
assert.equal(h(new Buffer('00', 'ascii'), 0xfba4c795), 0x2a101837);
assert.equal(h(new Buffer('hello world', 'ascii'), 0), 0x5e928f0f);
});
it('should test and add stuff', function() {
var b = bcoin.bloom(512, 10, 156);
b.add('hello');
assert(b.test('hello'));
assert(!b.test('hello!'));
assert(!b.test('ping'));
b.add('hello', 'ascii');
assert(b.test('hello', 'ascii'));
assert(!b.test('hello!', 'ascii'));
assert(!b.test('ping', 'ascii'));
b.add('hello!');
assert(b.test('hello!'));
assert(!b.test('ping'));
b.add('hello!', 'ascii');
assert(b.test('hello!', 'ascii'));
assert(!b.test('ping', 'ascii'));
b.add('ping');
assert(b.test('ping'));
b.add('ping', 'ascii');
assert(b.test('ping', 'ascii'));
});
});