From 7d6ebd0201b18d08228ed00fd7cb568802e63f7a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 14 Mar 2017 05:47:12 -0700 Subject: [PATCH] node: make hostlist persistence optional. --- bin/node | 2 ++ bin/spvnode | 2 ++ lib/net/hostlist.js | 36 +++++++++++++++++++++++++++--------- lib/net/pool.js | 6 ++++++ lib/node/config.js | 9 +++++++++ lib/node/fullnode.js | 3 ++- lib/node/node.js | 12 +++++++++--- lib/node/spvnode.js | 1 + 8 files changed, 58 insertions(+), 13 deletions(-) diff --git a/bin/node b/bin/node index aedb5f9c..eebd3d30 100755 --- a/bin/node +++ b/bin/node @@ -16,6 +16,7 @@ node = new bcoin.fullnode({ logConsole: true, logLevel: 'debug', db: 'leveldb', + persistent: true, listen: true, loader: require }); @@ -25,6 +26,7 @@ node.on('error', function(err) { }); co.spawn(function *() { + yield node.ensure(); yield node.open(); yield node.connect(); node.startSync(); diff --git a/bin/spvnode b/bin/spvnode index a940a9fe..de52af6b 100755 --- a/bin/spvnode +++ b/bin/spvnode @@ -18,6 +18,7 @@ node = bcoin.spvnode({ logConsole: true, logLevel: 'debug', db: 'leveldb', + persistent: true, listen: true, loader: require }); @@ -27,6 +28,7 @@ node.on('error', function(err) { }); co.spawn(function *() { + yield node.ensure(); yield node.open(); yield node.connect(); diff --git a/lib/net/hostlist.js b/lib/net/hostlist.js index b51cb43a..e380ae07 100644 --- a/lib/net/hostlist.js +++ b/lib/net/hostlist.js @@ -196,7 +196,10 @@ HostList.prototype.close = co(function* close() { */ HostList.prototype.start = function start() { - if (!this.options.location) + if (!this.options.persistent) + return; + + if (!this.options.filename) return; assert(this.timer == null); @@ -208,7 +211,10 @@ HostList.prototype.start = function start() { */ HostList.prototype.stop = function stop() { - if (!this.options.location) + if (!this.options.persistent) + return; + + if (!this.options.filename) return; assert(this.timer != null); @@ -250,12 +256,15 @@ HostList.prototype.injectSeeds = function injectSeeds() { */ HostList.prototype.loadFile = co(function* loadFile() { - var filename = this.options.location; + var filename = this.options.filename; var data, json; if (fs.unsupported) return; + if (!this.options.persistent) + return; + if (!filename) return; @@ -279,12 +288,15 @@ HostList.prototype.loadFile = co(function* loadFile() { */ HostList.prototype.flush = co(function* flush() { - var filename = this.options.location; + var filename = this.options.filename; var json, data; if (fs.unsupported) return; + if (!this.options.persistent) + return; + if (!filename) return; @@ -1487,7 +1499,8 @@ function HostListOptions(options) { this.maxEntries = 50; this.prefix = null; - this.location = null; + this.filename = null; + this.persistent = false; this.flushInterval = 120000; if (options) @@ -1583,15 +1596,20 @@ HostListOptions.prototype.fromOptions = function fromOptions(options) { this.maxEntries = options.maxEntries; } + if (options.persistent != null) { + assert(typeof options.persistent === 'boolean'); + this.persistent = options.persistent; + } + if (options.prefix != null) { assert(typeof options.prefix === 'string'); this.prefix = options.prefix; - this.location = this.prefix + '/hosts.json'; + this.filename = this.prefix + '/hosts.json'; } - if (options.location != null) { - assert(typeof options.location === 'string'); - this.location = options.location; + if (options.filename != null) { + assert(typeof options.filename === 'string'); + this.filename = options.filename; } if (options.flushInterval != null) { diff --git a/lib/net/pool.js b/lib/net/pool.js index ddf0d767..e05c6619 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -3760,6 +3760,7 @@ function PoolOptions(options) { this.blockMode = 0; this.services = common.LOCAL_SERVICES; this.requiredServices = common.REQUIRED_SERVICES; + this.persistent = false; this.fromOptions(options); } @@ -3985,6 +3986,11 @@ PoolOptions.prototype.fromOptions = function fromOptions(options) { this.blockMode = options.blockMode; } + if (options.persistent != null) { + assert(typeof options.persistent === 'boolean'); + this.persistent = options.persistent; + } + if (this.spv) { this.requiredServices |= common.services.BLOOM; this.services &= ~common.services.NETWORK; diff --git a/lib/node/config.js b/lib/node/config.js index d4744be6..059a1c76 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -537,6 +537,15 @@ Config.prototype.getFile = function getFile(file) { return this.prefix + '/' + file; }; +/** + * Ensure prefix. + * @returns {Promise} + */ + +Config.prototype.ensure = function ensure() { + return fs.mkdirp(this.prefix); +}; + /** * Create a file path using `prefix`. * @param {String} file diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index 31830182..9ad480c3 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -110,7 +110,8 @@ function FullNode(options) { publicPort: this.config.num('public-port'), host: this.config.str('host'), port: this.config.num('port'), - listen: this.config.bool('listen') + listen: this.config.bool('listen'), + persistent: this.config.bool('persistent') }); // Miner needs access to the chain and mempool. diff --git a/lib/node/node.js b/lib/node/node.js index 32f82baf..0be2247d 100644 --- a/lib/node/node.js +++ b/lib/node/node.js @@ -16,7 +16,6 @@ var Logger = require('./logger'); var workerPool = require('../workers/workerpool').pool; var ec = require('../crypto/ec'); var native = require('../utils/native'); -var fs = require('../utils/fs'); var Config = require('./config'); /** @@ -114,6 +113,15 @@ Node.prototype.init = function init() { }); }; +/** + * Ensure prefix directory. + * @returns {Promise} + */ + +Node.prototype.ensure = function ensure() { + return this.config.ensure(); +}; + /** * Open node. Bind all events. * @private @@ -122,8 +130,6 @@ Node.prototype.init = function init() { Node.prototype.handlePreopen = co(function* handlePreopen() { var self = this; - yield fs.mkdirp(this.config.prefix); - yield this.logger.open(); this.bind(this.network.time, 'offset', function(offset) { diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 4c00bfc4..bc9daf4f 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -71,6 +71,7 @@ function SPVNode(options) { bip150: this.config.bool('bip150'), identityKey: this.config.buf('identity-key'), maxOutbound: this.config.num('max-outbound'), + persistent: this.config.bool('persistent'), selfish: true, listen: false });