Skip to content

Commit

Permalink
fix(Blockchain): handle closed BlockStore when initializing
Browse files Browse the repository at this point in the history
Fixes errors in the Blockchain tests.
  • Loading branch information
mappum committed Jun 2, 2015
1 parent 82634e4 commit 4864dae
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (process.browser) {
function noop () {}

var TIME_MARGIN = 2 * 60 * 60
var storeClosedError = new Error('Store is closed')

var Blockchain = module.exports = function (opts) {
var self = this
Expand All @@ -40,7 +41,8 @@ var Blockchain = module.exports = function (opts) {

this.store = opts.store || new BlockStore({ path: opts.path })
this._initStore(function (err) {
if (err) return self._error(err)
if (err && err !== storeClosedError) return self._error(err)
else if (err && err === storeClosedError) return
self.initialized = true
self.emit('ready')
})
Expand Down Expand Up @@ -68,7 +70,7 @@ Blockchain.prototype._initStore = function (cb) {
return function (cb) {
self.store.get(block.hash, function (err) {
if (err && !err.notFound) return cb(err)
if (self.closed || self.store.isClosed()) return cb(new Error('Store is closed'))
if (self.closed || self.store.isClosed()) return cb(storeClosedError)
self.store.put(block, cb)
})
}
Expand Down

0 comments on commit 4864dae

Please sign in to comment.