From 569016e752503500ed8f078b6a70f255ec625f54 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 25 Mar 2016 15:17:43 -0700 Subject: [PATCH] refactor. numbers. --- lib/bcoin/chaindb.js | 13 ++------ lib/bcoin/lru.js | 2 +- lib/bcoin/reader.js | 28 +++++++++++++++++ lib/bcoin/utils.js | 73 +++++++++++++++----------------------------- 4 files changed, 57 insertions(+), 59 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 3cd83ce4..6d372d1a 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -65,15 +65,10 @@ ChainDB.prototype._init = function _init() { this.db = bcoin.ldb((this.options.spv ? 'spv' : '') + 'chain', { compression: false, - cacheSize: 16 * 1024 * 1024, - writeBufferSize: 8 * 1024 * 1024 + cacheSize: 16 << 20, + writeBufferSize: 8 << 20 }); - if (!bcoin.isBrowser) { - //var DataStore = require('./data' + 'store'); - //this.db = new DataStore(this.db); - } - utils.debug('Starting chain load.'); this.db.open(function(err) { @@ -327,8 +322,6 @@ ChainDB.prototype.get = function get(height, callback) { if (!entry) return callback(); - // Cache the past 1001 blocks in memory - // (necessary for isSuperMajority) self.addCache(entry); return callback(null, entry); @@ -357,7 +350,7 @@ ChainDB.prototype.save = function save(entry, block, callback) { this.emit('add entry', entry); - self.saveBlock(block, batch, function(err) { + this.saveBlock(block, batch, function(err) { if (err) return callback(err); diff --git a/lib/bcoin/lru.js b/lib/bcoin/lru.js index 8d6fa50f..f94bbf9c 100644 --- a/lib/bcoin/lru.js +++ b/lib/bcoin/lru.js @@ -247,7 +247,7 @@ LRU.prototype.keys = function keys() { return keys; }; -LRU.prototype.items = function items() { +LRU.prototype.toArray = function toArray() { var items = []; var item; diff --git a/lib/bcoin/reader.js b/lib/bcoin/reader.js index 6be309a8..8f78631b 100644 --- a/lib/bcoin/reader.js +++ b/lib/bcoin/reader.js @@ -108,6 +108,20 @@ BufferReader.prototype.readU64BE = function readU64BE() { return ret; }; +BufferReader.prototype.readU64N = function readU64N() { + assert(this.offset + 8 <= this.data.length); + var ret = utils.readU64N(this.data, this.offset); + this.offset += 8; + return ret; +}; + +BufferReader.prototype.readU64NBE = function readU64NBE() { + assert(this.offset + 8 <= this.data.length); + var ret = utils.readU64NBE(this.data, this.offset); + this.offset += 8; + return ret; +}; + BufferReader.prototype.read8 = function read8() { assert(this.offset + 1 <= this.data.length); var ret = utils.read8(this.data, this.offset); @@ -157,6 +171,20 @@ BufferReader.prototype.read64BE = function read64BE() { return ret; }; +BufferReader.prototype.read64N = function read64N() { + assert(this.offset + 8 <= this.data.length); + var ret = utils.read64N(this.data, this.offset); + this.offset += 8; + return ret; +}; + +BufferReader.prototype.read64NBE = function read64NBE() { + assert(this.offset + 8 <= this.data.length); + var ret = utils.read64NBE(this.data, this.offset); + this.offset += 8; + return ret; +}; + BufferReader.prototype.readBytes = function readBytes(size) { var ret; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index e1609721..a2fcd3c9 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1190,55 +1190,11 @@ utils.writeU32BE = function writeU32BE(dst, num, off) { }; utils.writeU64 = function writeU64(dst, num, off) { - var i; - - if (!bn.isBN(num)) - num = new bn(+num); - - off = off >>> 0; - - // We shouldn't think of - // this as negative. - if (num.isNeg()) - num = num.neg(); - - if (num.bitLength() > 64) - num = num.uand(utils.U64); - - num = num.toArray('le', 8); - - assert.equal(num.length, 8); - - for (i = 0; i < num.length; i++) - dst[off++] = num[i] & 0xff; - - return 8; + return utils.write64(dst, num, off); }; utils.writeU64BE = function writeU64BE(dst, num, off) { - var i; - - if (!bn.isBN(num)) - num = new bn(+num); - - off = off >>> 0; - - // We shouldn't think of - // this as negative. - if (num.isNeg()) - num = num.neg(); - - if (num.bitLength() > 64) - num = num.uand(utils.U64); - - num = num.toArray('be', 8); - - assert.equal(num.length, 8); - - for (i = 0; i < num.length; i++) - dst[off++] = num[i] & 0xff; - - return 8; + return utils.write64BE(dst, num, off); }; utils.writeU64N = function writeU64N(dst, num, off) { @@ -1249,11 +1205,17 @@ utils.writeU64NBE = function writeU64NBE(dst, num, off) { return utils.write64NBE(dst, num, off); }; +utils.MAX_SAFE_INTEGER = 0x1fffffffffffff; +utils.MAX_SAFE_HI = 0x1fffff; + utils.write64N = function write64N(dst, num, off) { var neg, hi, lo, one, i, b; + num = +num; off = off >>> 0; + assert(num <= utils.MAX_SAFE_INTEGER, 'Number exceeds 2^53-1'); + if (num < 0) neg = true; @@ -1286,8 +1248,11 @@ utils.write64N = function write64N(dst, num, off) { utils.write64NBE = function write64NBE(dst, num, off) { var neg, hi, lo, one, i, b; + num = +num; off = off >>> 0; + assert(num <= utils.MAX_SAFE_INTEGER, 'Number exceeds 2^53-1'); + if (num < 0) neg = true; @@ -1321,6 +1286,7 @@ utils.readU64N = function readU64N(dst, off) { off = off >>> 0; var hi = utils.readU32(dst, off + 4); var lo = utils.readU32(dst, off); + assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1'); return (hi * 0x100000000) + lo; }; @@ -1328,6 +1294,7 @@ utils.readU64NBE = function readU64NBE(dst, off) { off = off >>> 0; var hi = utils.readU32BE(dst, off); var lo = utils.readU32BE(dst, off + 4); + assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1'); return (hi * 0x100000000) + lo; }; @@ -1338,8 +1305,10 @@ utils.read64N = function read64N(dst, off) { if (hi & 0x80000000) { hi = ~hi + 1; lo = ~lo + 1; + assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1'); return -(hi * 0x100000000 + lo); } + assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1'); return (hi * 0x100000000) + lo; }; @@ -1350,8 +1319,10 @@ utils.read64NBE = function read64NBE(dst, off) { if (hi & 0x80000000) { hi = ~hi + 1; lo = ~lo + 1; + assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1'); return -(hi * 0x100000000 + lo); } + assert(hi <= utils.MAX_SAFE_HI, 'Number exceeds 2^53-1'); return (hi * 0x100000000) + lo; }; @@ -1447,8 +1418,11 @@ utils.write32BE = function write32BE(dst, num, off) { utils.write64 = function write64(dst, num, off) { var i; + // if (!bn.isBN(num)) + // num = new bn(+num); + if (!bn.isBN(num)) - num = new bn(+num); + return utils.write64N(dst, num, off); off = off >>> 0; @@ -1477,8 +1451,11 @@ utils.write64 = function write64(dst, num, off) { utils.write64BE = function write64BE(dst, num, off) { var i; + // if (!bn.isBN(num)) + // num = new bn(+num); + if (!bn.isBN(num)) - num = new bn(+num); + return utils.write64NBE(dst, num, off); off = off >>> 0;