From b273bec120da3957862353006014c39c30d39bc8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 21 Jan 2016 03:39:36 -0800 Subject: [PATCH] chaindb: checksum. --- lib/bcoin/chain.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index ca2c82a4..161bdadd 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -191,7 +191,7 @@ Chain.prototype.resetHeight = function resetHeight(height) { existing = this.db.get(i); assert(existing); delete this.heightLookup[existing.hash]; - this.db.del(i); + this.db.remove(i); } this.tip = this.db.get(height); @@ -690,6 +690,7 @@ Chain.prototype.fromJSON = function fromJSON(json) { */ var BLOCK_SIZE = 112; +// var BLOCK_SIZE = 116; function ChainDB(chain, options) { if (!(this instanceof ChainDB)) @@ -897,7 +898,7 @@ ChainDB.prototype.saveAsync = function save(entry, callback) { }); }; -ChainDB.prototype.del = function del(height) { +ChainDB.prototype.remove = function remove(height) { assert(height >= 0); this._write(this._nullBlock, height * BLOCK_SIZE); @@ -908,7 +909,12 @@ ChainDB.prototype.del = function del(height) { if ((height + 1) * BLOCK_SIZE === this.size) { while (this.isNull(height)) height--; + + if (height < 0) + height = 0; + fs.ftruncateSync(this.fd, (height + 1) * BLOCK_SIZE); + this.size = (height + 1) * BLOCK_SIZE; this.tip = height; } @@ -1097,11 +1103,14 @@ ChainBlock.prototype.toRaw = function toRaw() { utils.writeU32(res, this.bits, 72); utils.writeU32(res, this.nonce, 76); utils.copy(this.chainwork.toArray('be', 32), res, 80); + // utils.copy(utils.checksum(res.slice(0, 112)), res, 112); return res; }; ChainBlock.fromRaw = function fromRaw(chain, height, p) { + // if (!utils.isEqual(utils.checksum(p.slice(0, 112)), p.slice(112, 116))) + // throw new Error('Bad checksum'); return new ChainBlock(chain, { height: height, hash: utils.toHex(utils.dsha256(p.slice(0, 80))),