From 95ff41dde9e1c51c19c493c947ae49e248874182 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 21 Jan 2016 01:56:01 -0800 Subject: [PATCH] move saveAsync elsewhere. --- lib/bcoin/chain.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 63164e89..f7b77d4e 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -763,10 +763,14 @@ ChainDB.prototype.getSize = function getSize() { }; ChainDB.prototype.count = function count() { - return this.size / BLOCK_SIZE | 0; + var len = this.size / BLOCK_SIZE; + assert(len === (len | 0)); + return len; }; ChainDB.prototype.cache = function cache(entry) { + // Could use this.count() - 1 here as + // long as we're not using saveAsync. assert.equal(this.tip, this.count() - 1); if (entry.height > this.tip) { this.tip = entry.height; @@ -809,7 +813,22 @@ ChainDB.prototype.get = function get(height) { return entry; }; -ChainDB.prototype.save = function save(entry, callback) { +ChainDB.prototype.save = function save(entry) { + var self = this; + var raw, offset; + + // Cache the past 1001 blocks in memory + // (necessary for isSuperMajority) + this.cache(entry); + + raw = entry.toRaw(); + offset = entry.height * BLOCK_SIZE; + + return this._writeSync(raw, offset); +}; + +// This causes weird race conditions with size/count/tip. +ChainDB.prototype.saveAsync = function save(entry, callback) { var self = this; var raw, offset; @@ -834,10 +853,6 @@ ChainDB.prototype.save = function save(entry, callback) { raw = entry.toRaw(); offset = entry.height * BLOCK_SIZE; - // Avoid using buffered writes for now. - delete this._queue[entry.height]; - return this._writeSync(raw, offset); - return this._write(raw, offset, function(err, success) { var item = self._queue[entry.height];