From da6b6e36229a5d719a833876aca59ec2a529ef46 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Thu, 3 Sep 2015 16:07:35 -0400 Subject: [PATCH] more changes for services --- .gitignore | 1 + lib/node.js | 1 + lib/scaffold/create.js | 15 ++++----------- lib/scaffold/default-config.js | 1 + lib/service.js | 7 +++++++ lib/services/db.js | 27 +++++++++++++++++++++++++-- lib/services/web.js | 22 +++++++++++++++++++--- 7 files changed, 58 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 1bafe798..48655ce2 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ libbitcoind libbitcoind* libbitcoind.includes *.log +.DS_Store diff --git a/lib/node.js b/lib/node.js index 16828a19..a6d2c152 100644 --- a/lib/node.js +++ b/lib/node.js @@ -145,6 +145,7 @@ Node.prototype._instantiateService = function(service) { var config = service.config; config.node = this; + config.name = service.name; var mod = new service.module(config); // include in loaded services diff --git a/lib/scaffold/create.js b/lib/scaffold/create.js index 951c026b..8be332e9 100644 --- a/lib/scaffold/create.js +++ b/lib/scaffold/create.js @@ -9,16 +9,7 @@ var path = require('path'); var packageFile = require('../../package.json'); var mkdirp = require('mkdirp'); var fs = require('fs'); - -var BASE_CONFIG = { - name: 'My Node', - services: [ - 'address' - ], - datadir: './data', - network: 'livenet', - port: 3001 -}; +var defaultConfig = require('./default-config'); var version; if (packageFile.version.match('-dev')) { @@ -70,7 +61,9 @@ function createConfigDirectory(configDir, name, datadir, isGlobal, done) { throw err; } - var config = BASE_CONFIG; + var configInfo = defaultConfig(); + var config = configInfo.config; + config.name = name || 'Bitcore Node'; config.datadir = datadir; var configJSON = JSON.stringify(config, null, 2); diff --git a/lib/scaffold/default-config.js b/lib/scaffold/default-config.js index 843688f3..cc29dab9 100644 --- a/lib/scaffold/default-config.js +++ b/lib/scaffold/default-config.js @@ -10,6 +10,7 @@ function getDefaultConfig() { return { path: process.cwd(), config: { + name: 'Bitcore Node', datadir: process.env.BITCORENODE_DIR || path.resolve(process.env.HOME, '.bitcoin'), network: process.env.BITCORENODE_NETWORK || 'livenet', port: Number(process.env.BITCORENODE_PORT) || 3001, diff --git a/lib/service.js b/lib/service.js index 97fed380..cc47ce1a 100644 --- a/lib/service.js +++ b/lib/service.js @@ -7,6 +7,7 @@ var Service = function(options) { EventEmitter.call(this); this.node = options.node; + this.name = options.name; }; util.inherits(Service, EventEmitter); @@ -81,4 +82,10 @@ Service.prototype.setupRoutes = function(app) { // Setup express routes here }; +Service.prototype.getRoutePrefix = function() { + return this.name; +}; + + + module.exports = Service; diff --git a/lib/services/db.js b/lib/services/db.js index 3527416d..48c15cd6 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -435,12 +435,28 @@ DB.prototype.getHashes = function getHashes(tipHash, callback) { if (hash === self.genesis.hash) { // Stop at the genesis block self.cache.chainHashes[tipHash] = hashes; + + // Only store 2 chains of hashes in memory + var sorted = Object.keys(self.cache.chainHashes).sort(function(a, b) { + return self.cache.chainHashes[a].length < self.cache.chainHashes[b].length; + }); + for(var i = 0; i < sorted.length; i++) { + if(i > 2) { + log.debug('Removing chainHashes ' + sorted[i] + ' ' + (self.cache.chainHashes[sorted[i]].length)); + delete self.cache.chainHashes[sorted[i]]; + } else { + log.debug('Keeping chainHashes ' + sorted[i] + ' ' + (self.cache.chainHashes[sorted[i]].length)); + } + } + callback(null, hashes); } else if(self.cache.chainHashes[hash]) { hashes.shift(); hashes = self.cache.chainHashes[hash].concat(hashes); - delete self.cache.chainHashes[hash]; self.cache.chainHashes[tipHash] = hashes; + if(hash !== tipHash) { + delete self.cache.chainHashes[hash]; + } callback(null, hashes); } else { // Continue with the previous hash @@ -643,8 +659,15 @@ DB.prototype.sync = function() { // This block doesn't progress the current tip, so we'll attempt // to rewind the chain to the common ancestor of the block and // then we can resume syncing. - self.syncRewind(block, done); + log.warn('Beginning reorg! Current tip: ' + self.tip.hash + '; New tip: ' + block.hash); + self.syncRewind(block, function(err) { + if(err) { + return done(err); + } + log.warn('Reorg complete. New tip is ' + self.tip.hash); + done(); + }); } }); }, function(err) { diff --git a/lib/services/web.js b/lib/services/web.js index 63fac376..89e7af07 100644 --- a/lib/services/web.js +++ b/lib/services/web.js @@ -50,7 +50,9 @@ WebService.prototype.stop = function(callback) { WebService.prototype.setupAllRoutes = function() { for(var key in this.node.services) { - this.node.services[key].setupRoutes(this.app); + var subApp = new express.Router(); + this.app.use('/' + this.node.services[key].getRoutePrefix(), subApp); + this.node.services[key].setupRoutes(subApp, express); } }; @@ -89,9 +91,23 @@ WebService.prototype.socketHandler = function(socket) { }); var events = self.node.getAllPublishEvents(); + var eventNames = []; events.forEach(function(event) { - bus.on(event.name, function() { + eventNames.push(event.name); + + if(event.extraEvents) { + eventNames = eventNames.concat(event.extraEvents); + } + }); + + eventNames = eventNames.filter(function(value, index, self) { + // remove any duplicates + return self.indexOf(value) === index; + }); + + eventNames.forEach(function(eventName) { + bus.on(eventName, function() { if(socket.connected) { var results = []; @@ -99,7 +115,7 @@ WebService.prototype.socketHandler = function(socket) { results.push(arguments[i]); } - var params = [event.name].concat(results); + var params = [eventName].concat(results); socket.emit.apply(socket, params); } });