diff --git a/lib/blockstore/abstract.js b/lib/blockstore/abstract.js index 69113e73..e12d0488 100644 --- a/lib/blockstore/abstract.js +++ b/lib/blockstore/abstract.js @@ -30,6 +30,16 @@ class AbstractBlockStore { this.logger = Logger.global.context('blockstore'); } + /** + * This method ensures that resources are available + * before opening. + * @returns {Promise} + */ + + async ensure() { + throw new Error('Abstract method.'); + } + /** * This method opens any necessary resources and * initializes the store to be ready to be queried. diff --git a/lib/blockstore/file.js b/lib/blockstore/file.js index 9b176575..132a388f 100644 --- a/lib/blockstore/file.js +++ b/lib/blockstore/file.js @@ -129,6 +129,17 @@ class FileBlockStore extends AbstractBlockStore { } } + /** + * This method ensures that both the block storage directory + * and index directory exist. + * before opening. + * @returns {Promise} + */ + + async ensure() { + return fs.mkdirp(this.indexLocation); + } + /** * Opens the file block store. It will regenerate necessary block * indexing if the index is missing or inconsistent. diff --git a/lib/blockstore/level.js b/lib/blockstore/level.js index 83b9db7d..f745e370 100644 --- a/lib/blockstore/level.js +++ b/lib/blockstore/level.js @@ -7,6 +7,7 @@ 'use strict'; const bdb = require('bdb'); +const fs = require('bfile'); const AbstractBlockStore = require('./abstract'); const layout = require('./layout'); const {types} = require('./common'); @@ -37,6 +38,16 @@ class LevelBlockStore extends AbstractBlockStore { }); } + /** + * This method ensures that the storage directory exists + * before opening. + * @returns {Promise} + */ + + async ensure() { + return fs.mkdirp(this.location); + } + /** * Opens the block storage. * @returns {Promise} diff --git a/lib/node/node.js b/lib/node/node.js index 099f4985..b407020e 100644 --- a/lib/node/node.js +++ b/lib/node/node.js @@ -57,6 +57,7 @@ class Node extends EventEmitter { this.workers = null; this.spv = false; + this.blocks = null; this.chain = null; this.fees = null; this.mempool = null; @@ -135,6 +136,9 @@ class Node extends EventEmitter { if (this.memory) return undefined; + if (this.blocks) + await this.blocks.ensure(); + return fs.mkdirp(this.config.prefix); } diff --git a/test/blockstore-test.js b/test/blockstore-test.js index 62ef22e1..fc69db65 100644 --- a/test/blockstore-test.js +++ b/test/blockstore-test.js @@ -10,7 +10,6 @@ const common = require('./util/common'); const {resolve} = require('path'); const fs = require('bfile'); const {rimraf} = require('./util/common'); -const {mkdirp} = require('bfile'); const random = require('bcrypto/lib/random'); const vectors = [ @@ -347,13 +346,13 @@ describe('BlockStore', function() { beforeEach(async () => { await rimraf(location); - await mkdirp(location); store = new FileBlockStore({ location: location, maxFileLength: 1024 }); + await store.ensure(); await store.open(); }); @@ -657,13 +656,13 @@ describe('BlockStore', function() { beforeEach(async () => { await rimraf(location); - await mkdirp(location); store = new FileBlockStore({ location: location, maxFileLength: 1024 * 1024 }); + await store.ensure(); await store.open(); }); @@ -709,12 +708,12 @@ describe('BlockStore', function() { beforeEach(async () => { await rimraf(location); - await mkdirp(location); store = new LevelBlockStore({ location: location }); + await store.ensure(); await store.open(); });