more changes for services

This commit is contained in:
Patrick Nagurny 2015-09-03 16:07:35 -04:00
parent 290874a8fb
commit da6b6e3622
7 changed files with 58 additions and 16 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ libbitcoind
libbitcoind*
libbitcoind.includes
*.log
.DS_Store

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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