node: make hostlist persistence optional.

This commit is contained in:
Christopher Jeffrey 2017-03-14 05:47:12 -07:00
parent a986a4c6b8
commit 7d6ebd0201
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 58 additions and 13 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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) {

View File

@ -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;

View File

@ -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

View File

@ -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.

View File

@ -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) {

View File

@ -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
});