refactor. numbers.

This commit is contained in:
Christopher Jeffrey 2016-03-25 15:17:43 -07:00
parent 6c238c844f
commit 569016e752
4 changed files with 57 additions and 59 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;