more changes for services
This commit is contained in:
parent
290874a8fb
commit
da6b6e3622
1
.gitignore
vendored
1
.gitignore
vendored
@ -29,3 +29,4 @@ libbitcoind
|
|||||||
libbitcoind*
|
libbitcoind*
|
||||||
libbitcoind.includes
|
libbitcoind.includes
|
||||||
*.log
|
*.log
|
||||||
|
.DS_Store
|
||||||
|
|||||||
@ -145,6 +145,7 @@ Node.prototype._instantiateService = function(service) {
|
|||||||
|
|
||||||
var config = service.config;
|
var config = service.config;
|
||||||
config.node = this;
|
config.node = this;
|
||||||
|
config.name = service.name;
|
||||||
var mod = new service.module(config);
|
var mod = new service.module(config);
|
||||||
|
|
||||||
// include in loaded services
|
// include in loaded services
|
||||||
|
|||||||
@ -9,16 +9,7 @@ var path = require('path');
|
|||||||
var packageFile = require('../../package.json');
|
var packageFile = require('../../package.json');
|
||||||
var mkdirp = require('mkdirp');
|
var mkdirp = require('mkdirp');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var defaultConfig = require('./default-config');
|
||||||
var BASE_CONFIG = {
|
|
||||||
name: 'My Node',
|
|
||||||
services: [
|
|
||||||
'address'
|
|
||||||
],
|
|
||||||
datadir: './data',
|
|
||||||
network: 'livenet',
|
|
||||||
port: 3001
|
|
||||||
};
|
|
||||||
|
|
||||||
var version;
|
var version;
|
||||||
if (packageFile.version.match('-dev')) {
|
if (packageFile.version.match('-dev')) {
|
||||||
@ -70,7 +61,9 @@ function createConfigDirectory(configDir, name, datadir, isGlobal, done) {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = BASE_CONFIG;
|
var configInfo = defaultConfig();
|
||||||
|
var config = configInfo.config;
|
||||||
|
|
||||||
config.name = name || 'Bitcore Node';
|
config.name = name || 'Bitcore Node';
|
||||||
config.datadir = datadir;
|
config.datadir = datadir;
|
||||||
var configJSON = JSON.stringify(config, null, 2);
|
var configJSON = JSON.stringify(config, null, 2);
|
||||||
|
|||||||
@ -10,6 +10,7 @@ function getDefaultConfig() {
|
|||||||
return {
|
return {
|
||||||
path: process.cwd(),
|
path: process.cwd(),
|
||||||
config: {
|
config: {
|
||||||
|
name: 'Bitcore Node',
|
||||||
datadir: process.env.BITCORENODE_DIR || path.resolve(process.env.HOME, '.bitcoin'),
|
datadir: process.env.BITCORENODE_DIR || path.resolve(process.env.HOME, '.bitcoin'),
|
||||||
network: process.env.BITCORENODE_NETWORK || 'livenet',
|
network: process.env.BITCORENODE_NETWORK || 'livenet',
|
||||||
port: Number(process.env.BITCORENODE_PORT) || 3001,
|
port: Number(process.env.BITCORENODE_PORT) || 3001,
|
||||||
|
|||||||
@ -7,6 +7,7 @@ var Service = function(options) {
|
|||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
this.node = options.node;
|
this.node = options.node;
|
||||||
|
this.name = options.name;
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(Service, EventEmitter);
|
util.inherits(Service, EventEmitter);
|
||||||
@ -81,4 +82,10 @@ Service.prototype.setupRoutes = function(app) {
|
|||||||
// Setup express routes here
|
// Setup express routes here
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Service.prototype.getRoutePrefix = function() {
|
||||||
|
return this.name;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = Service;
|
module.exports = Service;
|
||||||
|
|||||||
@ -435,12 +435,28 @@ DB.prototype.getHashes = function getHashes(tipHash, callback) {
|
|||||||
if (hash === self.genesis.hash) {
|
if (hash === self.genesis.hash) {
|
||||||
// Stop at the genesis block
|
// Stop at the genesis block
|
||||||
self.cache.chainHashes[tipHash] = hashes;
|
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);
|
callback(null, hashes);
|
||||||
} else if(self.cache.chainHashes[hash]) {
|
} else if(self.cache.chainHashes[hash]) {
|
||||||
hashes.shift();
|
hashes.shift();
|
||||||
hashes = self.cache.chainHashes[hash].concat(hashes);
|
hashes = self.cache.chainHashes[hash].concat(hashes);
|
||||||
delete self.cache.chainHashes[hash];
|
|
||||||
self.cache.chainHashes[tipHash] = hashes;
|
self.cache.chainHashes[tipHash] = hashes;
|
||||||
|
if(hash !== tipHash) {
|
||||||
|
delete self.cache.chainHashes[hash];
|
||||||
|
}
|
||||||
callback(null, hashes);
|
callback(null, hashes);
|
||||||
} else {
|
} else {
|
||||||
// Continue with the previous hash
|
// 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
|
// This block doesn't progress the current tip, so we'll attempt
|
||||||
// to rewind the chain to the common ancestor of the block and
|
// to rewind the chain to the common ancestor of the block and
|
||||||
// then we can resume syncing.
|
// 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) {
|
}, function(err) {
|
||||||
|
|||||||
@ -50,7 +50,9 @@ WebService.prototype.stop = function(callback) {
|
|||||||
|
|
||||||
WebService.prototype.setupAllRoutes = function() {
|
WebService.prototype.setupAllRoutes = function() {
|
||||||
for(var key in this.node.services) {
|
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 events = self.node.getAllPublishEvents();
|
||||||
|
var eventNames = [];
|
||||||
|
|
||||||
events.forEach(function(event) {
|
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) {
|
if(socket.connected) {
|
||||||
var results = [];
|
var results = [];
|
||||||
|
|
||||||
@ -99,7 +115,7 @@ WebService.prototype.socketHandler = function(socket) {
|
|||||||
results.push(arguments[i]);
|
results.push(arguments[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
var params = [event.name].concat(results);
|
var params = [eventName].concat(results);
|
||||||
socket.emit.apply(socket, params);
|
socket.emit.apply(socket, params);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user