more utils cleanup.
This commit is contained in:
parent
69ddca52a8
commit
f8d49f0653
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
|
var assert = utils.assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bloom Filter
|
* Bloom Filter
|
||||||
@ -67,7 +68,8 @@ Bloom.prototype.reset = function reset() {
|
|||||||
Bloom.prototype.add = function add(val, enc) {
|
Bloom.prototype.add = function add(val, enc) {
|
||||||
var i, bit, pos, shift;
|
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++) {
|
for (i = 0; i < this.n; i++) {
|
||||||
bit = this.hash(val, i);
|
bit = this.hash(val, i);
|
||||||
@ -89,7 +91,8 @@ Bloom.prototype.add = function add(val, enc) {
|
|||||||
Bloom.prototype.test = function test(val, enc) {
|
Bloom.prototype.test = function test(val, enc) {
|
||||||
var i, bit, pos, shift;
|
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++) {
|
for (i = 0; i < this.n; i++) {
|
||||||
bit = this.hash(val, i);
|
bit = this.hash(val, i);
|
||||||
@ -176,7 +179,7 @@ function murmur(data, seed) {
|
|||||||
var hash = seed;
|
var hash = seed;
|
||||||
var i, w, r, j;
|
var i, w, r, j;
|
||||||
|
|
||||||
data = utils.toBuffer(data);
|
assert(Buffer.isBuffer(data));
|
||||||
|
|
||||||
for (i = 0; i + 4 <= data.length; i += 4) {
|
for (i = 0; i + 4 <= data.length; i += 4) {
|
||||||
w = data[i]
|
w = data[i]
|
||||||
|
|||||||
@ -1668,7 +1668,7 @@ Chain.prototype.purgePending = function purgePending() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the chain to see if it has a block, orphan, or pending block.
|
* 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].
|
* @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) {
|
Chain.prototype.hasBlock = function hasBlock(hash, callback) {
|
||||||
if (typeof hash === 'number')
|
|
||||||
return this.db.has(hash, callback);
|
|
||||||
|
|
||||||
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) {
|
Chain.prototype.getEntry = function getEntry(hash, callback) {
|
||||||
if (typeof hash === 'number')
|
|
||||||
return this.db.get(hash, callback);
|
|
||||||
return this.db.get(hash, callback);
|
return this.db.get(hash, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,9 @@ function MerkleBlock(data) {
|
|||||||
bcoin.abstractblock.call(this, data);
|
bcoin.abstractblock.call(this, data);
|
||||||
|
|
||||||
this.hashes = (data.hashes || []).map(function(hash) {
|
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 || [];
|
this.flags = data.flags || [];
|
||||||
|
|||||||
@ -68,37 +68,10 @@ utils.slice = function slice(buf, start, end) {
|
|||||||
return clone;
|
return clone;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Convert objects to a Buffer.
|
* Base58
|
||||||
* @param {Buffer|Array|String} msg
|
|
||||||
* @param {String} enc - Any buffer-supported
|
|
||||||
* encoding as well as `base58`.
|
|
||||||
* @returns {Buffer}
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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'
|
var base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ'
|
||||||
+ 'abcdefghijkmnopqrstuvwxyz';
|
+ 'abcdefghijkmnopqrstuvwxyz';
|
||||||
|
|
||||||
|
|||||||
@ -59,7 +59,7 @@ function createGenesisBlock(options) {
|
|||||||
block = {
|
block = {
|
||||||
version: options.version,
|
version: options.version,
|
||||||
prevBlock: constants.NULL_HASH,
|
prevBlock: constants.NULL_HASH,
|
||||||
merkleRoot: utils.toHex(utils.dsha256(txRaw)),
|
merkleRoot: utils.dsha256(txRaw).toString('hex'),
|
||||||
ts: options.ts,
|
ts: options.ts,
|
||||||
bits: options.bits,
|
bits: options.bits,
|
||||||
nonce: options.nonce,
|
nonce: options.nonce,
|
||||||
@ -71,7 +71,7 @@ function createGenesisBlock(options) {
|
|||||||
block = parser.parseBlock(blockRaw);
|
block = parser.parseBlock(blockRaw);
|
||||||
|
|
||||||
block._hash = utils.dsha256(blockRaw.slice(0, 80));
|
block._hash = utils.dsha256(blockRaw.slice(0, 80));
|
||||||
block.hash = utils.toHex(block._hash);
|
block.hash = block._hash.toString('hex');
|
||||||
block._raw = blockRaw;
|
block._raw = blockRaw;
|
||||||
block._size = blockRaw.length;
|
block._size = blockRaw.length;
|
||||||
block._witnessSize = 0;
|
block._witnessSize = 0;
|
||||||
@ -81,7 +81,7 @@ function createGenesisBlock(options) {
|
|||||||
tx.height = 0;
|
tx.height = 0;
|
||||||
tx.ts = block.ts;
|
tx.ts = block.ts;
|
||||||
tx._hash = block.merkleRoot;
|
tx._hash = block.merkleRoot;
|
||||||
tx.hash = utils.toHex(tx._hash);
|
tx.hash = tx._hash.toString('hex');
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,25 +5,25 @@ describe('Bloom', function() {
|
|||||||
it('should do proper murmur3', function() {
|
it('should do proper murmur3', function() {
|
||||||
var h = bcoin.bloom.hash;
|
var h = bcoin.bloom.hash;
|
||||||
|
|
||||||
assert.equal(h('', 0), 0);
|
assert.equal(h(new Buffer('', 'ascii'), 0), 0);
|
||||||
assert.equal(h('', 0xfba4c795), 0x6a396f08);
|
assert.equal(h(new Buffer('', 'ascii'), 0xfba4c795), 0x6a396f08);
|
||||||
assert.equal(h('00', 0xfba4c795), 0x2a101837);
|
assert.equal(h(new Buffer('00', 'ascii'), 0xfba4c795), 0x2a101837);
|
||||||
assert.equal(h('hello world', 0), 0x5e928f0f);
|
assert.equal(h(new Buffer('hello world', 'ascii'), 0), 0x5e928f0f);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should test and add stuff', function() {
|
it('should test and add stuff', function() {
|
||||||
var b = bcoin.bloom(512, 10, 156);
|
var b = bcoin.bloom(512, 10, 156);
|
||||||
|
|
||||||
b.add('hello');
|
b.add('hello', 'ascii');
|
||||||
assert(b.test('hello'));
|
assert(b.test('hello', 'ascii'));
|
||||||
assert(!b.test('hello!'));
|
assert(!b.test('hello!', 'ascii'));
|
||||||
assert(!b.test('ping'));
|
assert(!b.test('ping', 'ascii'));
|
||||||
|
|
||||||
b.add('hello!');
|
b.add('hello!', 'ascii');
|
||||||
assert(b.test('hello!'));
|
assert(b.test('hello!', 'ascii'));
|
||||||
assert(!b.test('ping'));
|
assert(!b.test('ping', 'ascii'));
|
||||||
|
|
||||||
b.add('ping');
|
b.add('ping', 'ascii');
|
||||||
assert(b.test('ping'));
|
assert(b.test('ping', 'ascii'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user