chaindb: minor improvements.

This commit is contained in:
Christopher Jeffrey 2016-01-20 20:06:43 -08:00
parent 43a365962b
commit d5d3dd69e2

View File

@ -693,7 +693,6 @@ Chain.prototype.fromJSON = function fromJSON(json) {
* ChainDB * ChainDB
*/ */
// var BLOCK_SIZE = 80;
var BLOCK_SIZE = 112; var BLOCK_SIZE = 112;
function ChainDB(chain) { function ChainDB(chain) {
@ -789,7 +788,7 @@ ChainDB.prototype.save = function save(entry, callback) {
this._queue[entry.height] = entry; this._queue[entry.height] = entry;
// Write asynchronously to the db. // Write asynchronously to the db.
raw = new Buffer(entry.toRaw()); raw = entry.toRaw();
offset = entry.height * BLOCK_SIZE; offset = entry.height * BLOCK_SIZE;
return this._write(raw, offset, function(err, success) { return this._write(raw, offset, function(err, success) {
@ -801,7 +800,7 @@ ChainDB.prototype.save = function save(entry, callback) {
// Synchronously write it and get it over with. // Synchronously write it and get it over with.
try { try {
if (item !== entry) if (item !== entry)
success = self._writeSync(new Buffer(item.toRaw()), offset); success = self._writeSync(item.toRaw(), offset);
} catch (e) { } catch (e) {
err = e; err = e;
} }
@ -838,7 +837,7 @@ ChainDB.prototype.del = function del(height) {
}; };
ChainDB.prototype.isNull = function isNull(height) { ChainDB.prototype.isNull = function isNull(height) {
var data = this._read(1, height * BLOCK_SIZE); var data = this._read(4, height * BLOCK_SIZE);
if (!data) if (!data)
return false; return false;
return utils.read32(data, 0) === 0; return utils.read32(data, 0) === 0;
@ -928,7 +927,6 @@ function ChainBlock(chain, data) {
this.bits = data.bits; this.bits = data.bits;
this.nonce = data.nonce; this.nonce = data.nonce;
this.height = data.height; this.height = data.height;
// this.chainwork = data.chainwork || new bn(0);
this.chainwork = data.chainwork || this.getChainwork(); this.chainwork = data.chainwork || this.getChainwork();
} }
@ -1009,7 +1007,7 @@ ChainBlock.fromJSON = function(chain, json) {
}; };
ChainBlock.prototype.toRaw = function toRaw() { ChainBlock.prototype.toRaw = function toRaw() {
var res = new Array(BLOCK_SIZE); var res = new Buffer(BLOCK_SIZE);
utils.writeU32(res, this.version, 0); utils.writeU32(res, this.version, 0);
utils.copy(utils.toArray(this.prevBlock, 'hex'), res, 4); utils.copy(utils.toArray(this.prevBlock, 'hex'), res, 4);
@ -1032,7 +1030,7 @@ ChainBlock.fromRaw = function fromRaw(chain, height, p) {
bits: utils.readU32(p, 72), bits: utils.readU32(p, 72),
nonce: utils.readU32(p, 76), nonce: utils.readU32(p, 76),
height: height, height: height,
chainwork: new bn(p.slice(80, 112)) chainwork: new bn(p.slice(80, 112), 'be')
}); });
}; };