bitcore-node-zcash/lib/node.js

465 lines
11 KiB
JavaScript
Raw Normal View History

'use strict';
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var async = require('async');
var bitcore = require('bitcore');
2015-08-27 07:10:07 -07:00
var BufferUtil = bitcore.util.buffer;
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;
var Chain = require('./chain');
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');
function Node(config) {
if(!(this instanceof Node)) {
return new Node(config);
}
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;
this._loadConfiguration(config);
this._initialize();
}
util.inherits(Node, EventEmitter);
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);
};
}
});
};
Node.prototype.getAllAPIMethods = function() {
2015-08-31 06:00:00 -07:00
var methods = [];
2015-08-27 13:09:27 -07:00
for(var i in this.modules) {
var mod = this.modules[i];
methods = methods.concat(mod.getAPIMethods());
}
return methods;
};
Node.prototype.getAllPublishEvents = function() {
2015-08-31 06:00:00 -07:00
var events = [];
2015-08-27 13:09:27 -07:00
for (var i in this.modules) {
var mod = this.modules[i];
events = events.concat(mod.getPublishEvents());
}
return events;
};
Node.prototype._loadConfiguration = function(config) {
this._loadNetwork(config);
this._loadConsensus(config);
};
/**
* 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.
* @param {Block} block - The new tip that forks the current chain.
* @param {Function} done - A callback function that is called when complete.
*/
Node.prototype._syncBitcoindAncestor = function(block, done) {
var self = this;
// The current chain of hashes will likely already be available in a cache.
self.chain.getHashes(self.chain.tip.hash, function(err, currentHashes) {
if (err) {
done(err);
}
// 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');
// 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-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);
});
};
/**
* 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) {
var self = this;
self._syncBitcoindAncestor(block, function(err, ancestorHash) {
2015-08-21 08:17:26 -07:00
if (err) {
return done(err);
}
// 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;
},
function(removeDone) {
2015-07-24 10:45:31 -07:00
var tip = self.chain.tip;
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) {
if (err) {
removeDone(err);
}
2015-07-24 10:45:31 -07:00
// Undo the related indexes for this block
2015-08-31 06:00:00 -07:00
self.modules.db.disconnectBlock(tip, function(err) {
if (err) {
return removeDone(err);
}
2015-07-24 10:45:31 -07:00
// Set the new tip
previousTip.__height = self.chain.tip.__height - 1;
self.chain.tip = previousTip;
self.chain.saveMetadata();
2015-07-24 10:45:31 -07:00
self.chain.emit('removeblock', tip);
removeDone();
});
});
}, done
);
});
};
/**
* 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.
*/
Node.prototype._syncBitcoind = function() {
var self = this;
if (self.bitcoindSyncing) {
return;
}
if (!self.chain.tip) {
return;
}
self.bitcoindSyncing = true;
self.chain.lastSavedMetadataThreshold = 30000;
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;
}, function(done) {
2015-08-31 06:00:00 -07:00
self.modules.bitcoind.getBlock(height + 1, function(err, blockBuffer) {
if (err) {
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) {
// 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) {
if (err) {
return done(err);
}
2015-08-18 14:36:54 -07:00
// Create indexes
2015-08-31 06:00:00 -07:00
self.modules.db.connectBlock(block, function(err) {
2015-08-18 14:36:54 -07:00
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
});
});
} 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);
}
});
}, function(err) {
if (err) {
Error.captureStackTrace(err);
return self.emit('error', err);
}
2015-08-21 13:13:11 -07:00
if(self.stopping) {
return;
}
self.bitcoindSyncing = false;
self.chain.lastSavedMetadataThreshold = 0;
// If bitcoind is completely synced
2015-08-31 06:00:00 -07:00
if (self.modules.bitcoind.isSynced()) {
self.emit('synced');
}
});
};
Node.prototype._loadNetwork = function(config) {
if (config.network === 'testnet') {
this.network = Networks.get('testnet');
} 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');
} else {
this.network = Networks.get('livenet');
}
2015-07-21 06:09:59 -07:00
$.checkState(this.network, 'Unrecognized network');
};
Node.prototype._loadConsensus = function(config) {
var options;
if (!config) {
options = {};
} else {
options = _.clone(config.consensus || {});
}
options.node = this;
this.chain = new Chain(options);
};
2015-08-20 14:50:14 -07:00
Node.prototype._initialize = function() {
var self = this;
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._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
});
};
Node.prototype.getServices = function() {
2015-08-27 13:09:27 -07:00
var services = [
{
name: 'chain',
dependencies: ['db']
}
];
services = services.concat(this._unloadedModules);
return services;
};
2015-08-27 13:09:27 -07:00
Node.prototype.getServiceOrder = function() {
var services = this.getServices();
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-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-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-08-20 14:50:14 -07:00
Node.prototype.start = function(callback) {
var self = this;
var servicesOrder = this.getServiceOrder();
2015-08-20 14:50:14 -07:00
async.eachSeries(
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-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
);
};
module.exports = Node;