2015-07-15 15:13:41 -07:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-21 06:09:59 -07:00
|
|
|
var fs = require('fs');
|
2015-07-15 15:13:41 -07:00
|
|
|
var util = require('util');
|
2015-08-26 12:18:58 -07:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var async = require('async');
|
2015-07-22 14:33:28 -07:00
|
|
|
var mkdirp = require('mkdirp');
|
2015-07-15 15:13:41 -07:00
|
|
|
var bitcore = require('bitcore');
|
2015-08-27 07:10:07 -07:00
|
|
|
var BufferUtil = bitcore.util.buffer;
|
2015-07-15 15:13:41 -07:00
|
|
|
var Networks = bitcore.Networks;
|
|
|
|
var _ = bitcore.deps._;
|
2015-07-21 06:09:59 -07:00
|
|
|
var $ = bitcore.util.preconditions;
|
2015-08-27 07:10:07 -07:00
|
|
|
var Block = bitcore.Block;
|
2015-08-26 12:18:58 -07:00
|
|
|
var Chain = require('./chain');
|
|
|
|
var DB = require('./db');
|
|
|
|
var index = require('./');
|
|
|
|
var log = index.log;
|
2015-07-29 10:36:23 -07:00
|
|
|
var Bus = require('./bus');
|
2015-08-27 13:09:27 -07:00
|
|
|
var BaseModule = require('./module');
|
2015-07-15 15:13:41 -07:00
|
|
|
|
|
|
|
function Node(config) {
|
2015-08-26 12:18:58 -07:00
|
|
|
if(!(this instanceof Node)) {
|
|
|
|
return new Node(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.db = null;
|
|
|
|
this.chain = null;
|
|
|
|
this.network = null;
|
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
this.modules = {};
|
|
|
|
this._unloadedModules = [];
|
|
|
|
|
|
|
|
// TODO type check the arguments of config.modules
|
|
|
|
if (config.modules) {
|
|
|
|
$.checkArgument(Array.isArray(config.modules));
|
|
|
|
this._unloadedModules = config.modules;
|
|
|
|
}
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
$.checkState(config.datadir, 'Node config expects "datadir"');
|
|
|
|
this.datadir = config.datadir;
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
this._loadConfiguration(config);
|
|
|
|
this._initialize();
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
util.inherits(Node, EventEmitter);
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-07-29 10:36:23 -07:00
|
|
|
Node.prototype.openBus = function() {
|
2015-08-28 13:16:51 -07:00
|
|
|
return new Bus({node: this});
|
2015-07-29 10:36:23 -07:00
|
|
|
};
|
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
Node.prototype.addModule = function(service) {
|
|
|
|
var self = this;
|
|
|
|
var mod = new service.module({
|
|
|
|
node: this
|
|
|
|
});
|
|
|
|
|
|
|
|
$.checkState(
|
|
|
|
mod instanceof BaseModule,
|
|
|
|
'Unexpected module instance type for module:' + service.name
|
|
|
|
);
|
|
|
|
|
|
|
|
// include in loaded modules
|
|
|
|
this.modules[service.name] = mod;
|
|
|
|
|
|
|
|
// add API methods
|
|
|
|
var methodData = mod.getAPIMethods();
|
|
|
|
methodData.forEach(function(data) {
|
|
|
|
var name = data[0];
|
|
|
|
var instance = data[1];
|
|
|
|
var method = data[2];
|
|
|
|
|
|
|
|
if (self[name]) {
|
|
|
|
throw new Error('Existing API method exists:' + name);
|
|
|
|
} else {
|
|
|
|
self[name] = function() {
|
|
|
|
return method.apply(instance, arguments);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-29 17:34:44 -07:00
|
|
|
Node.prototype.getAllAPIMethods = function() {
|
2015-08-04 13:34:53 -07:00
|
|
|
var methods = this.db.getAPIMethods();
|
2015-08-27 13:09:27 -07:00
|
|
|
for(var i in this.modules) {
|
|
|
|
var mod = this.modules[i];
|
2015-07-29 17:34:44 -07:00
|
|
|
methods = methods.concat(mod.getAPIMethods());
|
|
|
|
}
|
|
|
|
return methods;
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.getAllPublishEvents = function() {
|
2015-08-06 13:19:36 -07:00
|
|
|
var events = this.db.getPublishEvents();
|
2015-08-27 13:09:27 -07:00
|
|
|
for (var i in this.modules) {
|
|
|
|
var mod = this.modules[i];
|
2015-07-29 17:34:44 -07:00
|
|
|
events = events.concat(mod.getPublishEvents());
|
|
|
|
}
|
|
|
|
return events;
|
|
|
|
};
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
Node.prototype._loadConfiguration = function(config) {
|
2015-08-26 12:18:58 -07:00
|
|
|
this._loadNetwork(config);
|
|
|
|
this._loadDB(config);
|
|
|
|
this._loadAPI();
|
|
|
|
this._loadConsensus(config);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-07-23 06:32:46 -07:00
|
|
|
/**
|
2015-07-23 20:02:31 -07:00
|
|
|
* This function will find the common ancestor between the current chain and a forked block,
|
|
|
|
* by moving backwards from the forked block until it meets the current chain.
|
2015-07-23 06:32:46 -07:00
|
|
|
* @param {Block} block - The new tip that forks the current chain.
|
|
|
|
* @param {Function} done - A callback function that is called when complete.
|
|
|
|
*/
|
2015-07-23 20:02:31 -07:00
|
|
|
Node.prototype._syncBitcoindAncestor = function(block, done) {
|
2015-07-23 06:32:46 -07:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
// The current chain of hashes will likely already be available in a cache.
|
2015-07-23 06:32:46 -07:00
|
|
|
self.chain.getHashes(self.chain.tip.hash, function(err, currentHashes) {
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
// Create a hash map for faster lookups
|
|
|
|
var currentHashesMap = {};
|
|
|
|
var length = currentHashes.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
currentHashesMap[currentHashes[i]] = true;
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:10:07 -07:00
|
|
|
// TODO: expose prevHash as a string from bitcore
|
|
|
|
var ancestorHash = BufferUtil.reverse(block.header.prevHash).toString('hex');
|
2015-07-23 20:02:31 -07:00
|
|
|
|
|
|
|
// We only need to go back until we meet the main chain for the forked block
|
|
|
|
// and thus don't need to find the entire chain of hashes.
|
|
|
|
|
2015-08-18 14:36:54 -07:00
|
|
|
while(ancestorHash && !currentHashesMap[ancestorHash]) {
|
2015-08-31 06:00:00 -07:00
|
|
|
var blockIndex = self.modules.bitcoind.getBlockIndex(ancestorHash);
|
2015-08-18 14:36:54 -07:00
|
|
|
ancestorHash = blockIndex ? blockIndex.prevHash : null;
|
|
|
|
}
|
2015-07-23 20:02:31 -07:00
|
|
|
|
2015-08-18 14:36:54 -07:00
|
|
|
// Hash map is no-longer needed, quickly let
|
|
|
|
// scavenging garbage collection know to cleanup
|
|
|
|
currentHashesMap = null;
|
|
|
|
|
|
|
|
if (!ancestorHash) {
|
|
|
|
return done(new Error('Unknown common ancestor.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
done(null, ancestorHash);
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
});
|
|
|
|
};
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
/**
|
|
|
|
* This function will attempt to rewind the chain to the common ancestor
|
|
|
|
* between the current chain and a forked block.
|
|
|
|
* @param {Block} block - The new tip that forks the current chain.
|
|
|
|
* @param {Function} done - A callback function that is called when complete.
|
|
|
|
*/
|
|
|
|
Node.prototype._syncBitcoindRewind = function(block, done) {
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
var self = this;
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
self._syncBitcoindAncestor(block, function(err, ancestorHash) {
|
2015-08-21 08:17:26 -07:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-07-23 20:02:31 -07:00
|
|
|
// Rewind the chain to the common ancestor
|
2015-07-24 10:45:31 -07:00
|
|
|
async.whilst(
|
|
|
|
function() {
|
|
|
|
// Wait until the tip equals the ancestor hash
|
|
|
|
return self.chain.tip.hash !== ancestorHash;
|
|
|
|
},
|
2015-07-23 20:02:31 -07:00
|
|
|
function(removeDone) {
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-24 10:45:31 -07:00
|
|
|
var tip = self.chain.tip;
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-08-27 07:10:07 -07:00
|
|
|
// TODO: expose prevHash as a string from bitcore
|
|
|
|
var prevHash = BufferUtil.reverse(tip.header.prevHash).toString('hex');
|
|
|
|
|
|
|
|
self.getBlock(prevHash, function(err, previousTip) {
|
2015-07-23 20:02:31 -07:00
|
|
|
if (err) {
|
|
|
|
removeDone(err);
|
|
|
|
}
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-24 10:45:31 -07:00
|
|
|
// Undo the related indexes for this block
|
|
|
|
self.db._onChainRemoveBlock(tip, function(err) {
|
2015-07-23 06:32:46 -07:00
|
|
|
if (err) {
|
2015-07-23 20:02:31 -07:00
|
|
|
return removeDone(err);
|
2015-07-23 06:32:46 -07:00
|
|
|
}
|
|
|
|
|
2015-07-24 10:45:31 -07:00
|
|
|
// Set the new tip
|
|
|
|
previousTip.__height = self.chain.tip.__height - 1;
|
2015-07-23 20:02:31 -07:00
|
|
|
self.chain.tip = previousTip;
|
|
|
|
self.chain.saveMetadata();
|
2015-07-24 10:45:31 -07:00
|
|
|
self.chain.emit('removeblock', tip);
|
2015-07-23 20:02:31 -07:00
|
|
|
removeDone();
|
2015-07-23 06:32:46 -07:00
|
|
|
});
|
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
});
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-23 20:02:31 -07:00
|
|
|
}, done
|
|
|
|
);
|
2015-07-23 06:32:46 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will synchronize additional indexes for the chain based on
|
|
|
|
* the current active chain in the bitcoin daemon. In the event that there is
|
|
|
|
* a reorganization in the daemon, the chain will rewind to the last common
|
|
|
|
* ancestor and then resume syncing.
|
|
|
|
*/
|
2015-07-15 15:13:41 -07:00
|
|
|
Node.prototype._syncBitcoind = function() {
|
|
|
|
var self = this;
|
|
|
|
|
2015-07-23 06:32:46 -07:00
|
|
|
if (self.bitcoindSyncing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self.chain.tip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bitcoindSyncing = true;
|
2015-08-06 14:53:10 -07:00
|
|
|
self.chain.lastSavedMetadataThreshold = 30000;
|
2015-07-23 06:32:46 -07:00
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
var height;
|
|
|
|
|
|
|
|
async.whilst(function() {
|
|
|
|
height = self.chain.tip.__height;
|
2015-08-31 06:00:00 -07:00
|
|
|
return height < self.modules.bitcoind.height && !self.stopping;
|
2015-07-23 06:32:46 -07:00
|
|
|
}, function(done) {
|
2015-08-31 06:00:00 -07:00
|
|
|
self.modules.bitcoind.getBlock(height + 1, function(err, blockBuffer) {
|
2015-07-15 15:13:41 -07:00
|
|
|
if (err) {
|
2015-07-23 06:32:46 -07:00
|
|
|
return done(err);
|
|
|
|
}
|
2015-08-21 08:53:20 -07:00
|
|
|
|
2015-08-27 07:10:07 -07:00
|
|
|
var block = Block.fromBuffer(blockBuffer);
|
|
|
|
|
|
|
|
// TODO: expose prevHash as a string from bitcore
|
|
|
|
var prevHash = BufferUtil.reverse(block.header.prevHash).toString('hex');
|
2015-08-21 08:17:26 -07:00
|
|
|
|
2015-08-27 07:10:07 -07:00
|
|
|
if (prevHash === self.chain.tip.hash) {
|
2015-07-23 06:32:46 -07:00
|
|
|
|
|
|
|
// This block appends to the current chain tip and we can
|
|
|
|
// immediately add it to the chain and create indexes.
|
|
|
|
|
|
|
|
// Populate height
|
|
|
|
block.__height = self.chain.tip.__height + 1;
|
|
|
|
|
2015-08-18 14:36:54 -07:00
|
|
|
// Update chain.cache.hashes
|
2015-08-27 07:10:07 -07:00
|
|
|
self.chain.cache.hashes[block.hash] = prevHash;
|
2015-07-24 20:18:14 -07:00
|
|
|
|
2015-08-18 14:36:54 -07:00
|
|
|
// Update chain.cache.chainHashes
|
|
|
|
self.chain.getHashes(block.hash, function(err, hashes) {
|
2015-07-23 06:32:46 -07:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-08-18 14:36:54 -07:00
|
|
|
// Create indexes
|
|
|
|
self.db._onChainAddBlock(block, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
self.chain.tip = block;
|
|
|
|
log.debug('Saving metadata');
|
|
|
|
self.chain.saveMetadata();
|
|
|
|
log.debug('Chain added block to main chain');
|
|
|
|
self.chain.emit('addblock', block);
|
|
|
|
setImmediate(done);
|
2015-08-24 11:46:48 -07:00
|
|
|
});
|
2015-07-23 06:32:46 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// 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._syncBitcoindRewind(block, done);
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
Error.captureStackTrace(err);
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
2015-08-11 14:16:04 -07:00
|
|
|
|
2015-08-21 13:13:11 -07:00
|
|
|
if(self.stopping) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bitcoindSyncing = false;
|
|
|
|
self.chain.lastSavedMetadataThreshold = 0;
|
|
|
|
|
2015-08-11 14:16:04 -07:00
|
|
|
// If bitcoind is completely synced
|
2015-08-31 06:00:00 -07:00
|
|
|
if (self.modules.bitcoind.isSynced()) {
|
2015-08-11 14:16:04 -07:00
|
|
|
self.emit('synced');
|
|
|
|
}
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype._loadNetwork = function(config) {
|
2015-07-21 10:47:25 -07:00
|
|
|
if (config.network === 'testnet') {
|
2015-07-15 15:13:41 -07:00
|
|
|
this.network = Networks.get('testnet');
|
2015-07-21 10:47:25 -07:00
|
|
|
} else if (config.network === 'regtest') {
|
|
|
|
Networks.remove(Networks.testnet);
|
|
|
|
Networks.add({
|
|
|
|
name: 'regtest',
|
|
|
|
alias: 'regtest',
|
|
|
|
pubkeyhash: 0x6f,
|
|
|
|
privatekey: 0xef,
|
|
|
|
scripthash: 0xc4,
|
|
|
|
xpubkey: 0x043587cf,
|
|
|
|
xprivkey: 0x04358394,
|
|
|
|
networkMagic: 0xfabfb5da,
|
|
|
|
port: 18444,
|
|
|
|
dnsSeeds: [ ]
|
|
|
|
});
|
|
|
|
this.network = Networks.get('regtest');
|
2015-07-15 15:13:41 -07:00
|
|
|
} else {
|
|
|
|
this.network = Networks.get('livenet');
|
|
|
|
}
|
2015-07-21 06:09:59 -07:00
|
|
|
$.checkState(this.network, 'Unrecognized network');
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype._loadDB = function(config) {
|
2015-08-24 13:33:44 -07:00
|
|
|
var options = _.clone(config.db || {});
|
|
|
|
|
2015-07-15 15:13:41 -07:00
|
|
|
if (config.DB) {
|
|
|
|
// Other modules can inherit from our DB and replace it with their own
|
|
|
|
DB = config.DB;
|
|
|
|
}
|
2015-07-16 12:53:44 -07:00
|
|
|
|
2015-07-21 06:09:59 -07:00
|
|
|
// Store the additional indexes in a new directory
|
|
|
|
// based on the network configuration and the datadir
|
2015-07-21 08:52:08 -07:00
|
|
|
$.checkArgument(config.datadir, 'Please specify "datadir" in configuration options');
|
|
|
|
$.checkState(this.network, 'Network property not defined');
|
2015-07-21 10:47:25 -07:00
|
|
|
var regtest = Networks.get('regtest');
|
|
|
|
if (this.network === Networks.livenet) {
|
2015-08-24 16:00:31 -07:00
|
|
|
options.path = config.datadir + '/bitcore-node.db';
|
2015-07-21 10:47:25 -07:00
|
|
|
} else if (this.network === Networks.testnet) {
|
2015-08-24 16:00:31 -07:00
|
|
|
options.path = config.datadir + '/testnet3/bitcore-node.db';
|
2015-07-21 10:47:25 -07:00
|
|
|
} else if (this.network === regtest) {
|
2015-08-24 16:00:31 -07:00
|
|
|
options.path = config.datadir + '/regtest/bitcore-node.db';
|
2015-07-21 08:52:08 -07:00
|
|
|
} else {
|
|
|
|
throw new Error('Unknown network: ' + this.network);
|
2015-07-21 06:09:59 -07:00
|
|
|
}
|
2015-08-24 13:33:44 -07:00
|
|
|
options.network = this.network;
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-24 13:33:44 -07:00
|
|
|
if (!fs.existsSync(options.path)) {
|
|
|
|
mkdirp.sync(options.path);
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
2015-07-21 06:09:59 -07:00
|
|
|
|
2015-08-24 13:33:44 -07:00
|
|
|
options.node = this;
|
|
|
|
|
|
|
|
this.db = new DB(options);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype._loadConsensus = function(config) {
|
2015-08-24 13:33:44 -07:00
|
|
|
var options;
|
|
|
|
if (!config) {
|
|
|
|
options = {};
|
2015-07-15 15:13:41 -07:00
|
|
|
} else {
|
2015-08-24 13:33:44 -07:00
|
|
|
options = _.clone(config.consensus || {});
|
2015-07-15 15:13:41 -07:00
|
|
|
}
|
2015-08-24 13:33:44 -07:00
|
|
|
options.node = this;
|
|
|
|
this.chain = new Chain(options);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-08-26 12:18:58 -07:00
|
|
|
Node.prototype._loadAPI = function() {
|
|
|
|
var self = this;
|
|
|
|
var methodData = self.db.getAPIMethods();
|
|
|
|
methodData.forEach(function(data) {
|
|
|
|
var name = data[0];
|
|
|
|
var instance = data[1];
|
|
|
|
var method = data[2];
|
|
|
|
|
|
|
|
self[name] = function() {
|
|
|
|
return method.apply(instance, arguments);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
Node.prototype._initialize = function() {
|
2015-07-15 15:13:41 -07:00
|
|
|
var self = this;
|
|
|
|
|
2015-08-24 11:46:48 -07:00
|
|
|
this._initializeDatabase();
|
2015-08-24 13:33:44 -07:00
|
|
|
this._initializeChain();
|
2015-08-24 11:46:48 -07:00
|
|
|
|
|
|
|
this.start(function(err) {
|
|
|
|
if(err) {
|
|
|
|
return self.emit('error', err);
|
|
|
|
}
|
|
|
|
self.emit('ready');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype._initializeDatabase = function() {
|
|
|
|
var self = this;
|
2015-07-15 15:13:41 -07:00
|
|
|
this.db.on('ready', function() {
|
|
|
|
log.info('Bitcoin Database Ready');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.db.on('error', function(err) {
|
|
|
|
Error.captureStackTrace(err);
|
|
|
|
self.emit('error', err);
|
|
|
|
});
|
2015-08-24 11:46:48 -07:00
|
|
|
};
|
2015-08-07 10:14:30 -07:00
|
|
|
|
2015-08-24 11:46:48 -07:00
|
|
|
Node.prototype._initializeChain = function() {
|
2015-08-31 06:00:00 -07:00
|
|
|
|
2015-08-24 11:46:48 -07:00
|
|
|
var self = this;
|
|
|
|
this.chain.on('ready', function() {
|
|
|
|
log.info('Bitcoin Chain Ready');
|
2015-08-31 06:00:00 -07:00
|
|
|
|
|
|
|
// Notify that there is a new tip
|
|
|
|
self.modules.bitcoind.on('tip', function(height) {
|
|
|
|
if(!self.stopping) {
|
|
|
|
var percentage = self.modules.bitcoind.syncPercentage();
|
|
|
|
log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage);
|
|
|
|
self._syncBitcoind();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-24 11:46:48 -07:00
|
|
|
});
|
|
|
|
this.chain.on('error', function(err) {
|
|
|
|
Error.captureStackTrace(err);
|
|
|
|
self.emit('error', err);
|
2015-08-20 14:50:14 -07:00
|
|
|
});
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
2015-08-24 13:33:44 -07:00
|
|
|
Node.prototype.getServices = function() {
|
2015-08-27 13:09:27 -07:00
|
|
|
var services = [
|
|
|
|
{
|
|
|
|
name: 'db',
|
|
|
|
dependencies: ['bitcoind'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'chain',
|
|
|
|
dependencies: ['db']
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services = services.concat(this._unloadedModules);
|
|
|
|
|
|
|
|
return services;
|
2015-08-24 13:33:44 -07:00
|
|
|
};
|
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
Node.prototype.getServiceOrder = function() {
|
2015-08-24 13:33:44 -07:00
|
|
|
|
|
|
|
var services = this.getServices();
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
// organize data for sorting
|
|
|
|
var names = [];
|
|
|
|
var servicesByName = {};
|
|
|
|
for (var i = 0; i < services.length; i++) {
|
|
|
|
var service = services[i];
|
|
|
|
names.push(service.name);
|
|
|
|
servicesByName[service.name] = service;
|
2015-08-20 14:50:14 -07:00
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-27 13:09:27 -07:00
|
|
|
var stackNames = {};
|
|
|
|
var stack = [];
|
|
|
|
|
|
|
|
function addToStack(names) {
|
|
|
|
for(var i = 0; i < names.length; i++) {
|
|
|
|
|
|
|
|
var name = names[i];
|
|
|
|
var service = servicesByName[name];
|
2015-08-31 06:00:00 -07:00
|
|
|
$.checkState(service, 'Required dependency "' + name + '" not available.');
|
2015-08-27 13:09:27 -07:00
|
|
|
|
|
|
|
// first add the dependencies
|
|
|
|
addToStack(service.dependencies);
|
|
|
|
|
|
|
|
// add to the stack if it hasn't been added
|
|
|
|
if(!stackNames[name]) {
|
|
|
|
stack.push(service);
|
|
|
|
stackNames[name] = true;
|
|
|
|
}
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
}
|
|
|
|
}
|
2015-08-27 13:09:27 -07:00
|
|
|
|
|
|
|
addToStack(names);
|
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
return stack;
|
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
Node.prototype.start = function(callback) {
|
|
|
|
var self = this;
|
2015-08-24 13:33:44 -07:00
|
|
|
var servicesOrder = this.getServiceOrder();
|
2015-08-20 14:50:14 -07:00
|
|
|
|
|
|
|
async.eachSeries(
|
2015-08-24 13:33:44 -07:00
|
|
|
servicesOrder,
|
2015-08-20 14:50:14 -07:00
|
|
|
function(service, next) {
|
2015-08-27 13:09:27 -07:00
|
|
|
log.info('Starting ' + service.name);
|
|
|
|
|
|
|
|
if (service.module) {
|
|
|
|
self.addModule(service);
|
|
|
|
self.modules[service.name].start(next);
|
|
|
|
} else {
|
|
|
|
// TODO: implement bitcoind, chain and db as modules
|
|
|
|
self[service.name].start(next);
|
|
|
|
}
|
2015-08-20 14:50:14 -07:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
2015-07-15 15:13:41 -07:00
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
Node.prototype.stop = function(callback) {
|
2015-08-21 12:39:46 -07:00
|
|
|
log.info('Beginning shutdown');
|
2015-08-20 14:50:14 -07:00
|
|
|
var self = this;
|
|
|
|
var services = this.getServiceOrder().reverse();
|
|
|
|
|
2015-08-21 08:53:20 -07:00
|
|
|
this.stopping = true;
|
|
|
|
this.emit('stopping');
|
|
|
|
|
2015-08-20 14:50:14 -07:00
|
|
|
async.eachSeries(
|
|
|
|
services,
|
|
|
|
function(service, next) {
|
2015-08-27 13:09:27 -07:00
|
|
|
log.info('Stopping ' + service.name);
|
|
|
|
if (service.module) {
|
|
|
|
self.modules[service.name].stop(next);
|
|
|
|
} else {
|
|
|
|
self[service.name].stop(next);
|
|
|
|
}
|
2015-08-20 14:50:14 -07:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2015-07-15 15:13:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Node;
|