db refactor.
This commit is contained in:
parent
40f9c62b6e
commit
b5f8f7291a
@ -10,6 +10,7 @@ var assert = utils.assert;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var network = bcoin.protocol.network;
|
||||
var DUMMY = new Buffer([]);
|
||||
var pad32 = utils.pad32;
|
||||
|
||||
/**
|
||||
* BlockDB
|
||||
@ -190,7 +191,7 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) {
|
||||
if (!batch)
|
||||
batch = self.db.batch();
|
||||
|
||||
batch.put('b/h/' + block.height, block.hash());
|
||||
batch.put('b/h/' + pad32(block.height), block.hash());
|
||||
|
||||
block.txs.forEach(function(tx, i) {
|
||||
var hash = tx.hash('hex');
|
||||
@ -288,7 +289,7 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat
|
||||
if (typeof hash === 'string')
|
||||
assert(block.hash('hex') === hash);
|
||||
|
||||
batch.del('b/h/' + block.height);
|
||||
batch.del('b/h/' + pad32(block.height));
|
||||
|
||||
block.txs.forEach(function(tx, i) {
|
||||
var hash = tx.hash('hex');
|
||||
@ -763,7 +764,7 @@ BlockDB.prototype._getHash = function _getHash(height, callback) {
|
||||
if (typeof height === 'string')
|
||||
return callback(null, height);
|
||||
|
||||
this.db.get('b/h/' + height, function(err, hash) {
|
||||
this.db.get('b/h/' + pad32(height), function(err, hash) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
if (!hash)
|
||||
@ -824,7 +825,7 @@ BlockDB.prototype.hasBlock = function hasBlock(hash, callback) {
|
||||
var id = 'b/b/' + hash;
|
||||
|
||||
if (typeof hash === 'number')
|
||||
id = 'b/h/' + hash;
|
||||
id = 'b/h/' + pad32(hash);
|
||||
|
||||
this.db.get(id, function(err, data) {
|
||||
if (err && err.type !== 'NotFoundError')
|
||||
|
||||
@ -12,7 +12,6 @@ var constants = bcoin.protocol.constants;
|
||||
var network = bcoin.protocol.network;
|
||||
var utils = bcoin.utils;
|
||||
var assert = utils.assert;
|
||||
var fs = bcoin.fs;
|
||||
|
||||
/**
|
||||
* Chain
|
||||
|
||||
@ -12,9 +12,7 @@ var constants = bcoin.protocol.constants;
|
||||
var network = bcoin.protocol.network;
|
||||
var utils = bcoin.utils;
|
||||
var assert = utils.assert;
|
||||
var fs = bcoin.fs;
|
||||
|
||||
var BLOCK_SIZE = bcoin.chainblock.BLOCK_SIZE;
|
||||
var pad32 = utils.pad32;
|
||||
|
||||
/**
|
||||
* ChainDB
|
||||
@ -37,7 +35,7 @@ function ChainDB(node, chain, options) {
|
||||
|
||||
if (!this.file) {
|
||||
this.file = bcoin.prefix
|
||||
+ '/chainindex-'
|
||||
+ '/chain-'
|
||||
+ (options.spv ? 'spv-' : '')
|
||||
+ network.type
|
||||
+ '.db';
|
||||
@ -132,7 +130,7 @@ ChainDB.prototype.load = function load(callback) {
|
||||
callback();
|
||||
}
|
||||
|
||||
this.db.get('c/h/' + genesis.hash, function(err, exists) {
|
||||
this.db.get('c/b/' + genesis.hash, function(err, exists) {
|
||||
if (err && err.type !== 'NotFoundError')
|
||||
throw err;
|
||||
|
||||
@ -190,7 +188,7 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) {
|
||||
// if (!this.bloom.test(hash, 'hex'))
|
||||
// return callback(null, -1);
|
||||
|
||||
this.db.get('c/h/' + hash, function(err, height) {
|
||||
this.db.get('c/b/' + hash, function(err, height) {
|
||||
if (err && err.type !== 'NotFoundError')
|
||||
return callback(err);
|
||||
|
||||
@ -211,7 +209,7 @@ ChainDB.prototype.getHash = function getHash(height, callback) {
|
||||
if (this.cacheHeight.has(height))
|
||||
return callback(null, this.cacheHeight.get(height).hash);
|
||||
|
||||
this.db.get('c/b/' + height, function(err, hash) {
|
||||
this.db.get('c/h/' + pad32(height), function(err, hash) {
|
||||
if (err && err.type !== 'NotFoundError')
|
||||
return callback(err);
|
||||
|
||||
@ -376,8 +374,8 @@ ChainDB.prototype.save = function save(entry, callback) {
|
||||
height = new Buffer(4);
|
||||
utils.writeU32(height, entry.height, 0);
|
||||
|
||||
batch.put('c/b/' + entry.height, new Buffer(entry.hash, 'hex'));
|
||||
batch.put('c/h/' + entry.hash, height);
|
||||
batch.put('c/h/' + pad32(entry.height), new Buffer(entry.hash, 'hex'));
|
||||
batch.put('c/b/' + entry.hash, height);
|
||||
batch.put('c/c/' + entry.hash, entry.toRaw());
|
||||
batch.put('c/n/' + entry.prevBlock, new Buffer(entry.hash, 'hex'));
|
||||
batch.put('c/t', new Buffer(entry.hash, 'hex'));
|
||||
@ -416,7 +414,7 @@ ChainDB.prototype.connect = function connect(block, callback, emit) {
|
||||
|
||||
batch = self.db.batch();
|
||||
|
||||
batch.put('c/b/' + entry.height, new Buffer(entry.hash, 'hex'));
|
||||
batch.put('c/h/' + pad32(entry.height), new Buffer(entry.hash, 'hex'));
|
||||
batch.put('c/t', new Buffer(entry.hash, 'hex'));
|
||||
|
||||
self.cacheHeight.set(entry.height, entry);
|
||||
@ -442,7 +440,7 @@ ChainDB.prototype.disconnect = function disconnect(block, callback) {
|
||||
|
||||
batch = self.db.batch();
|
||||
|
||||
batch.del('c/b/' + entry.height);
|
||||
batch.del('c/h/' + pad32(entry.height));
|
||||
batch.put('c/t', new Buffer(entry.prevBlock, 'hex'));
|
||||
|
||||
self.cacheHeight.remove(entry.height);
|
||||
@ -505,8 +503,8 @@ ChainDB.prototype.reset = function reset(block, callback, emit) {
|
||||
return batch.write(callback);
|
||||
}
|
||||
|
||||
batch.del('c/b/' + tip.height);
|
||||
batch.del('c/h/' + tip.hash);
|
||||
batch.del('c/h/' + pad32(tip.height));
|
||||
batch.del('c/b/' + tip.hash);
|
||||
batch.del('c/c/' + tip.hash);
|
||||
batch.del('c/n/' + tip.prevBlock);
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ var utils = bcoin.utils;
|
||||
var assert = bcoin.utils.assert;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var DUMMY = new Buffer([]);
|
||||
var pad32 = utils.pad32;
|
||||
|
||||
/**
|
||||
* TXPool
|
||||
@ -1377,13 +1378,6 @@ TXPool.prototype.getBalance = function getBalance(callback) {
|
||||
return this.getBalanceByAddress(null, callback);
|
||||
};
|
||||
|
||||
function pad32(num) {
|
||||
num = num + '';
|
||||
while (num.length < 10)
|
||||
num = '0' + num;
|
||||
return num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose
|
||||
*/
|
||||
|
||||
@ -111,7 +111,7 @@ utils.toBase58 = function toBase58(arr) {
|
||||
|
||||
end = n.cmpn(0) === 0;
|
||||
|
||||
utils.assert.equal(r.length, 1);
|
||||
assert.equal(r.length, 1);
|
||||
r = r.words[0];
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
@ -122,7 +122,7 @@ utils.toBase58 = function toBase58(arr) {
|
||||
break;
|
||||
res = base58[c] + res;
|
||||
}
|
||||
utils.assert.equal(r, 0);
|
||||
assert.equal(r, 0);
|
||||
} while (!end);
|
||||
|
||||
// Add leading "zeroes"
|
||||
@ -433,16 +433,23 @@ utils.revHex = function revHex(s) {
|
||||
return r;
|
||||
};
|
||||
|
||||
utils.assert = function assert(val, msg) {
|
||||
function assert(val, msg) {
|
||||
if (!val)
|
||||
throw new Error(msg || 'Assertion failed');
|
||||
};
|
||||
}
|
||||
|
||||
utils.assert.equal = function assertEqual(l, r, msg) {
|
||||
assert.equal = function assertEqual(l, r, msg) {
|
||||
if (l != r)
|
||||
throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
|
||||
};
|
||||
|
||||
assert.noError = function noError(err) {
|
||||
if (err)
|
||||
throw err;
|
||||
};
|
||||
|
||||
utils.assert = assert;
|
||||
|
||||
utils.btc =
|
||||
utils.toBTC = function toBTC(satoshi, strict) {
|
||||
var m = new bn(10000000).mul(new bn(10));
|
||||
@ -555,7 +562,7 @@ utils.toFloat = function toFloat(val) {
|
||||
utils.parseHost = function parseHost(addr) {
|
||||
var parts;
|
||||
|
||||
utils.assert(addr);
|
||||
assert(addr);
|
||||
|
||||
if (typeof addr === 'object')
|
||||
return addr;
|
||||
@ -587,8 +594,8 @@ utils.isIP = function isIP(ip) {
|
||||
utils.ip2version = function ip2version(ip, version) {
|
||||
var b, i, j;
|
||||
|
||||
utils.assert(Buffer.isBuffer(ip));
|
||||
utils.assert(version === 4 || version === 6);
|
||||
assert(Buffer.isBuffer(ip));
|
||||
assert(version === 4 || version === 6);
|
||||
|
||||
if (version === 4) {
|
||||
// Check to see if this an
|
||||
@ -661,7 +668,7 @@ utils.ip2version = function ip2version(ip, version) {
|
||||
utils.ip2array = function ip2array(ip, version) {
|
||||
var type = utils.isIP(ip);
|
||||
|
||||
utils.assert(version === 4 || version === 6);
|
||||
assert(version === 4 || version === 6);
|
||||
|
||||
if (type === 0) {
|
||||
if (!Buffer.isBuffer(ip))
|
||||
@ -670,10 +677,10 @@ utils.ip2array = function ip2array(ip, version) {
|
||||
ip = new Buffer(ip.split('.').map(function(n) {
|
||||
return +n;
|
||||
}));
|
||||
utils.assert(ip.length <= 4);
|
||||
assert(ip.length <= 4);
|
||||
} else if (type === 6) {
|
||||
ip = new Buffer(ip.replace(/:/g, ''), 'hex');
|
||||
utils.assert(ip.length <= 16);
|
||||
assert(ip.length <= 16);
|
||||
}
|
||||
|
||||
return utils.ip2version(ip, version);
|
||||
@ -689,8 +696,8 @@ utils.array2ip = function array2ip(ip, version) {
|
||||
ip = new Buffer([0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
utils.assert(version === 4 || version === 6);
|
||||
utils.assert(ip.length <= 16);
|
||||
assert(version === 4 || version === 6);
|
||||
assert(ip.length <= 16);
|
||||
|
||||
ip = utils.ip2version(ip, version);
|
||||
|
||||
@ -1172,7 +1179,7 @@ utils.writeU64 = function writeU64(dst, num, off) {
|
||||
|
||||
num = num.toArray('le', 8);
|
||||
|
||||
utils.assert.equal(num.length, 8);
|
||||
assert.equal(num.length, 8);
|
||||
|
||||
for (i = 0; i < num.length; i++)
|
||||
dst[off++] = num[i] & 0xff;
|
||||
@ -1198,7 +1205,7 @@ utils.writeU64BE = function writeU64BE(dst, num, off) {
|
||||
|
||||
num = num.toArray('be', 8);
|
||||
|
||||
utils.assert.equal(num.length, 8);
|
||||
assert.equal(num.length, 8);
|
||||
|
||||
for (i = 0; i < num.length; i++)
|
||||
dst[off++] = num[i] & 0xff;
|
||||
@ -1271,7 +1278,7 @@ utils.write64 = function write64(dst, num, off) {
|
||||
|
||||
num = num.toArray('le', 8);
|
||||
|
||||
utils.assert.equal(num.length, 8);
|
||||
assert.equal(num.length, 8);
|
||||
|
||||
for (i = 0; i < num.length; i++)
|
||||
dst[off++] = num[i] & 0xff;
|
||||
@ -1301,7 +1308,7 @@ utils.write64BE = function write64BE(dst, num, off) {
|
||||
|
||||
num = num.toArray('be', 8);
|
||||
|
||||
utils.assert.equal(num.length, 8);
|
||||
assert.equal(num.length, 8);
|
||||
|
||||
for (i = 0; i < num.length; i++)
|
||||
dst[off++] = num[i] & 0xff;
|
||||
@ -1426,8 +1433,8 @@ utils.ccmp = function(a, b) {
|
||||
var res = 0;
|
||||
var i;
|
||||
|
||||
utils.assert(Buffer.isBuffer(a));
|
||||
utils.assert(Buffer.isBuffer(b));
|
||||
assert(Buffer.isBuffer(a));
|
||||
assert(Buffer.isBuffer(b));
|
||||
|
||||
if (a.length !== b.length)
|
||||
return false;
|
||||
@ -1448,7 +1455,7 @@ utils.forRange = function forRange(from, to, iter, callback) {
|
||||
return callback();
|
||||
|
||||
function next(err) {
|
||||
utils.assert(pending > 0);
|
||||
assert(pending > 0);
|
||||
if (err)
|
||||
error = err;
|
||||
if (!--pending)
|
||||
@ -1469,7 +1476,7 @@ utils.forEach = function forEach(arr, iter, callback) {
|
||||
return callback();
|
||||
|
||||
function next(err) {
|
||||
utils.assert(pending > 0);
|
||||
assert(pending > 0);
|
||||
if (err)
|
||||
error = err;
|
||||
if (!--pending)
|
||||
@ -1487,7 +1494,7 @@ utils.forRangeSerial = function forRangeSerial(from, to, iter, callback) {
|
||||
callback = utils.asyncify(callback);
|
||||
|
||||
(function next(err) {
|
||||
utils.assert(!called);
|
||||
assert(!called);
|
||||
if (err) {
|
||||
called = true;
|
||||
return callback(err);
|
||||
@ -1511,7 +1518,7 @@ utils.forEachSerial = function forEachSerial(arr, iter, callback) {
|
||||
|
||||
(function next(err) {
|
||||
var item;
|
||||
utils.assert(!called);
|
||||
assert(!called);
|
||||
if (err) {
|
||||
called = true;
|
||||
return callback(err);
|
||||
@ -1539,7 +1546,7 @@ utils.every = function every(arr, iter, callback) {
|
||||
return callback(null, result);
|
||||
|
||||
function next(err, res) {
|
||||
utils.assert(pending > 0);
|
||||
assert(pending > 0);
|
||||
if (err)
|
||||
error = err;
|
||||
if (!res)
|
||||
@ -1564,7 +1571,7 @@ utils.everySerial = function everySerial(arr, iter, callback) {
|
||||
|
||||
(function next(err, res) {
|
||||
var item;
|
||||
utils.assert(!called);
|
||||
assert(!called);
|
||||
if (err) {
|
||||
called = true;
|
||||
return callback(err);
|
||||
@ -1703,8 +1710,8 @@ utils.checkMerkleBranch = function checkMerkleBranch(hash, branch, index) {
|
||||
utils.indexOf = function indexOf(arr, buf) {
|
||||
var i;
|
||||
|
||||
utils.assert(Array.isArray(arr));
|
||||
utils.assert(Buffer.isBuffer(buf));
|
||||
assert(Array.isArray(arr));
|
||||
assert(Buffer.isBuffer(buf));
|
||||
|
||||
for (i = 0; i < arr.length; i++) {
|
||||
if (!Buffer.isBuffer(arr[i]))
|
||||
@ -1715,3 +1722,11 @@ utils.indexOf = function indexOf(arr, buf) {
|
||||
|
||||
return -1;
|
||||
};
|
||||
|
||||
utils.pad32 = function pad32(num) {
|
||||
num = num + '';
|
||||
while (num.length < 10)
|
||||
num = '0' + num;
|
||||
assert(num.length === 10);
|
||||
return num;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user