diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 173d8a79..073e7e35 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -305,12 +305,12 @@ Chain.prototype._preload = function _preload(callback) { return headers; } - function save(entry) { + function save(entry, header) { var unlock = locker.lock(save, [entry]); if (!unlock) return; - self.db.save(entry, null, null, true, function(err) { + self.db.save(entry, header, null, true, function(err) { if (err) { stream.destroy(); locker.destroy(); @@ -414,7 +414,7 @@ Chain.prototype._preload = function _preload(callback) { entry = new bcoin.chainentry(self, data, lastEntry); if (entry.height > chainHeight) - save(entry); + save(entry, block); if ((height + 1) % 50000 === 0) bcoin.debug('Received %d headers from electrum.org.', height + 1); diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index c5b5e32f..4adb94a2 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -555,17 +555,10 @@ ChainDB.prototype.get = function get(hash, callback) { */ ChainDB.prototype.save = function save(entry, block, view, connect, callback) { - var batch, hash, height; + var batch = this.db.batch(); + var hash = block.hash(); + var height = new Buffer(4); - callback = utils.ensure(callback); - - assert(entry.height >= 0); - - batch = this.db.batch(); - - hash = new Buffer(entry.hash, 'hex'); - - height = new Buffer(4); height.writeUInt32LE(height, 0, true); batch.put(layout.h(entry.hash), height); @@ -625,7 +618,7 @@ ChainDB.prototype.getTip = function getTip(callback) { ChainDB.prototype.connect = function connect(entry, block, view, callback) { var batch = this.db.batch(); - var hash = new Buffer(entry.hash, 'hex'); + var hash = block.hash(); batch.put(layout.n(entry.prevBlock), hash); batch.put(layout.H(entry.height), hash); @@ -656,9 +649,7 @@ ChainDB.prototype.connect = function connect(entry, block, view, callback) { ChainDB.prototype.disconnect = function disconnect(entry, callback) { var self = this; - var batch; - - batch = this.db.batch(); + var batch = this.db.batch(); batch.del(layout.n(entry.prevBlock)); batch.del(layout.H(entry.height));