169 lines
3.9 KiB
JavaScript
169 lines
3.9 KiB
JavaScript
/**
|
|
* bitcoind.js
|
|
* Copyright (c) 2014, BitPay (MIT License)
|
|
* A bitcoind node.js binding.
|
|
*/
|
|
|
|
var net = require('net');
|
|
var EventEmitter = require('events').EventEmitter;
|
|
var bitcoindjs = require('../build/Release/bitcoindjs.node');
|
|
var util = require('util');
|
|
var net = require('net');
|
|
|
|
/**
|
|
* Bitcoin
|
|
*/
|
|
|
|
function Bitcoin(options) {
|
|
var self = this;
|
|
|
|
if (!(this instanceof Bitcoin)) {
|
|
return new Bitcoin(options);
|
|
}
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.options = options;
|
|
}
|
|
|
|
Bitcoin.prototype.__proto__ = EventEmitter.prototype;
|
|
|
|
Bitcoin.prototype.start = function(callback) {
|
|
var self = this;
|
|
|
|
var none = {};
|
|
var exitCaught = none;
|
|
var errorCaught = none;
|
|
|
|
this.log_pipe = bitcoindjs.start(function(err, status) {
|
|
process.on('SIGINT', self.stop.bind(self));
|
|
process.on('SIGHUP', self.stop.bind(self));
|
|
|
|
var exit = process.exit;
|
|
self._exit = function() {
|
|
return exit.apply(process, arguments);
|
|
};
|
|
|
|
process.exit = function(code) {
|
|
exitCaught = code || 0;
|
|
if (!self._shutdown) {
|
|
return self._exit(code);
|
|
}
|
|
self.stop();
|
|
};
|
|
|
|
process.on('uncaughtException', function(err) {
|
|
if (process.listeners('uncaughtException').length > 1) {
|
|
return;
|
|
}
|
|
errorCaught = err;
|
|
if (!self._shutdown) {
|
|
if (err && err.stack) {
|
|
console.error(err.stack);
|
|
}
|
|
self._exit(1);
|
|
return;
|
|
}
|
|
self.stop();
|
|
});
|
|
|
|
if (callback) {
|
|
callback(err);
|
|
callback = null;
|
|
}
|
|
|
|
if (err) {
|
|
self.emit('error', err);
|
|
} else {
|
|
self.emit('open', status);
|
|
}
|
|
});
|
|
|
|
// bitcoind's boost threads aren't in the thread pool
|
|
// or on node's event loop, so we need to keep node open.
|
|
this._shutdown = setInterval(function() {
|
|
if (!self._stoppingSaid && bitcoindjs.stopping()) {
|
|
self._stoppingSaid = true;
|
|
self.log('shutting down...');
|
|
}
|
|
|
|
if (bitcoindjs.stopped()) {
|
|
self.log('shut down.');
|
|
|
|
clearInterval(self._shutdown);
|
|
delete self._shutdown;
|
|
|
|
if (exitCaught !== none) {
|
|
return self._exit(0);
|
|
}
|
|
|
|
if (errorCaught !== none) {
|
|
if (errorCaught && errorCaught.stack) {
|
|
console.error(errorCaught.stack);
|
|
}
|
|
return self._exit(0);
|
|
}
|
|
}
|
|
}, 1000);
|
|
|
|
if (this.log_pipe !== -1) {
|
|
this.log('log pipe opened: %d', this.log_pipe);
|
|
this._pipe = new net.Socket(this.log_pipe);
|
|
this._pipe.on('data', function(data) {
|
|
return process.stdout.write('bitcoind: ' + data + '\n');
|
|
});
|
|
this._pipe.on('error', function(err) {
|
|
; // ignore for now
|
|
});
|
|
this._pipe.resume();
|
|
}
|
|
};
|
|
|
|
Bitcoin.prototype.getBlock = function(hash, callback) {
|
|
return bitcoindjs.getBlock(hash, function(err, block) {
|
|
if (err) return callback(err)
|
|
return callback(null, JSON.parse(block));
|
|
});
|
|
};
|
|
|
|
Bitcoin.prototype.log =
|
|
Bitcoin.prototype.info = function() {
|
|
if (typeof arguments[0] !== 'string') {
|
|
var out = util.inspect(arguments[0], null, 20, true);
|
|
return process.stdout.write('bitcoind.js: ' + out + '\n');
|
|
}
|
|
var out = util.format.apply(util, arguments);
|
|
return process.stdout.write('bitcoind.js: ' + out + '\n');
|
|
};
|
|
|
|
Bitcoin.prototype.error = function() {
|
|
if (typeof arguments[0] !== 'string') {
|
|
var out = util.inspect(arguments[0], null, 20, true);
|
|
return process.stderr.write('bitcoind.js: ' + out + '\n');
|
|
}
|
|
var out = util.format.apply(util, arguments);
|
|
return process.stderr.write('bitcoind.js: ' + out + '\n');
|
|
};
|
|
|
|
Bitcoin.prototype.stop =
|
|
Bitcoin.prototype.close = function(callback) {
|
|
var self = this;
|
|
return bitcoindjs.stop(function(err, status) {
|
|
if (err) {
|
|
self.error(err.message);
|
|
} else {
|
|
self.log(status);
|
|
}
|
|
if (!callback) return;
|
|
return callback(err, status);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Expose
|
|
*/
|
|
|
|
module.exports = exports = Bitcoin;
|
|
exports.Bitcoin = Bitcoin;
|
|
exports.native = bitcoindjs;
|