fix segfault on exit.

This commit is contained in:
Christopher Jeffrey 2014-12-12 11:00:24 -08:00
parent 70d19eb6d6
commit f552121fb2
1 changed files with 107 additions and 0 deletions

View File

@ -139,10 +139,12 @@ Bitcoin.instances = {};
Bitcoin.prototype.instances = Bitcoin.instances;
Bitcoin.__defineGetter__('global', function() {
if (bitcoin.stopping) return;
return Bitcoin.instances[Object.keys(Bitcoin.instances)[0]];
});
Bitcoin.prototype.__defineGetter__('global', function() {
if (bitcoin.stopping) return;
return Bitcoin.global;
});
@ -370,6 +372,7 @@ Bitcoin.prototype.start = function(options, callback) {
};
Bitcoin.prototype.getBlock = function(blockhash, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.getBlock(blockhash, function(err, block) {
if (err) return callback(err);
return callback(null, bitcoin.block(block));
@ -377,6 +380,7 @@ Bitcoin.prototype.getBlock = function(blockhash, callback) {
};
Bitcoin.prototype.getBlockHeight = function(height, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.getBlock(+height, function(err, block) {
if (err) return callback(err);
return callback(null, bitcoin.block(block));
@ -385,6 +389,7 @@ Bitcoin.prototype.getBlockHeight = function(height, callback) {
Bitcoin.prototype.getTransaction =
Bitcoin.prototype.getTx = function(txid, blockhash, callback) {
if (bitcoin.stopping) return;
if (typeof txid === 'object' && txid) {
var options = txid;
callback = blockhash;
@ -415,6 +420,7 @@ Bitcoin.prototype.getTx = function(txid, blockhash, callback) {
};
Bitcoin.prototype.getTransactionWithBlock = function(txid, blockhash, callback) {
if (bitcoin.stopping) return;
var slow = true;
if (typeof txid === 'object' && txid) {
@ -459,34 +465,42 @@ Bitcoin.prototype.getTransactionWithBlock = function(txid, blockhash, callback)
};
Bitcoin.prototype.getInfo = function() {
if (bitcoin.stopping) return;
return bitcoindjs.getInfo();
};
Bitcoin.prototype.getPeerInfo = function() {
if (bitcoin.stopping) return;
return bitcoindjs.getPeerInfo();
};
Bitcoin.prototype.getAddresses = function() {
if (bitcoin.stopping) return;
return bitcoindjs.getAddresses();
};
Bitcoin.prototype.getProgress = function(callback) {
if (bitcoin.stopping) return;
return bitcoindjs.getProgress(callback);
};
Bitcoin.prototype.setGenerate = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.setGenerate(options || {});
};
Bitcoin.prototype.getGenerate = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.getGenerate(options || {});
};
Bitcoin.prototype.getMiningInfo = function() {
if (bitcoin.stopping) return;
return bitcoindjs.getMiningInfo();
};
Bitcoin.prototype.getAddrTransactions = function(address, callback) {
if (bitcoin.stopping) return;
return bitcoin.db.get('addr-tx/' + address, function(err, records) {
var options = {
address: address,
@ -537,20 +551,24 @@ Bitcoin.prototype.getAddrTransactions = function(address, callback) {
};
Bitcoin.prototype.getBestBlock = function(callback) {
if (bitcoin.stopping) return;
var hash = bitcoindjs.getBestBlock();
return bitcoindjs.getBlock(hash, callback);
};
Bitcoin.prototype.getChainHeight = function() {
if (bitcoin.stopping) return;
return bitcoindjs.getChainHeight();
};
Bitcoin.prototype.__defineGetter__('chainHeight', function() {
if (bitcoin.stopping) return;
return this.getChainHeight();
});
Bitcoin.prototype.getBlockByTxid =
Bitcoin.prototype.getBlockByTx = function(txid, callback) {
if (bitcoin.stopping) return;
return bitcoin.db.get('block-tx/' + txid, function(err, block) {
if (block) {
return self.getBlock(block.hash, function(err, block) {
@ -571,6 +589,7 @@ Bitcoin.prototype.getBlockByTx = function(txid, callback) {
Bitcoin.prototype.getBlocksByDate =
Bitcoin.prototype.getBlocksByTime = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.getBlocksByTime(options, function(err, blocks) {
if (err) return callback(err);
return callback(null, blocks.map(function(block) {
@ -580,11 +599,13 @@ Bitcoin.prototype.getBlocksByTime = function(options, callback) {
};
Bitcoin.prototype.getLastFileIndex = function() {
if (bitcoin.stopping) return;
return bitcoindjs.getLastFileIndex();
};
Bitcoin.prototype.log =
Bitcoin.prototype.info = function() {
if (bitcoin.stopping) return;
if (this.options.silent) return;
if (typeof arguments[0] !== 'string') {
var out = util.inspect(arguments[0], null, 20, true);
@ -595,6 +616,7 @@ Bitcoin.prototype.info = function() {
};
Bitcoin.prototype.error = function() {
if (bitcoin.stopping) return;
if (this.options.silent) return;
if (typeof arguments[0] !== 'string') {
var out = util.inspect(arguments[0], null, 20, true);
@ -606,6 +628,7 @@ Bitcoin.prototype.error = function() {
Bitcoin.prototype.stop =
Bitcoin.prototype.close = function(callback) {
if (bitcoin.stopping) return;
var self = this;
return bitcoindjs.stop(function(err, status) {
if (err) {
@ -618,6 +641,22 @@ Bitcoin.prototype.close = function(callback) {
});
};
Bitcoin.prototype.__defineGetter__('stopping', function() {
return bitcoindjs.stopping() || bitcoindjs.stopped();
});
Bitcoin.prototype.__defineGetter__('stopped', function() {
return bitcoindjs.stopped();
});
Bitcoin.__defineGetter__('stopping', function() {
return bitcoindjs.stopping() || bitcoindjs.stopped();
});
Bitcoin.__defineGetter__('stopped', function() {
return bitcoindjs.stopped();
});
/**
* Block
*/
@ -635,6 +674,8 @@ function Block(data) {
return data;
}
if (bitcoin.stopping) return;
var self = this;
Object.keys(data).forEach(function(key) {
@ -661,14 +702,17 @@ Object.defineProperty(Block.prototype, '_blockFlag', {
});
Block.isBlock = function(block) {
if (bitcoin.stopping) return;
return block._blockFlag === Block.prototype._blockFlag;
};
Block.fromHex = function(hex) {
if (bitcoin.stopping) return;
return bitcoin.block(bitcoindjs.blockFromHex(hex));
};
Block.prototype.getHash = function(enc) {
if (bitcoin.stopping) return;
var data = bitcoindjs.getBlockHex(this);
if (!this.hash || this.hash !== data.hash) {
this.hash = data.hash;
@ -680,10 +724,12 @@ Block.prototype.getHash = function(enc) {
};
Block.prototype.verify = function() {
if (bitcoin.stopping) return;
return this.verified = this.verified || bitcoindjs.verifyBlock(this);
};
Block.prototype.toHex = function() {
if (bitcoin.stopping) return;
var hex = Block.toHex(this);
if (!this.hex || this.hex !== hex) {
this.hex = hex;
@ -692,15 +738,18 @@ Block.prototype.toHex = function() {
};
Block.toHex = function(block) {
if (bitcoin.stopping) return;
var data = bitcoindjs.getBlockHex(block);
return data.hex;
};
Block.prototype.toBinary = function() {
if (bitcoin.stopping) return;
return Block.toBinary(this);
};
Block.toBinary = function(block) {
if (bitcoin.stopping) return;
var data = bitcoindjs.getBlockHex(block);
return new Buffer(data.hex, 'hex');
};
@ -722,6 +771,8 @@ function Transaction(data) {
return data;
}
if (bitcoin.stopping) return;
var self = this;
Object.keys(data).forEach(function(key) {
@ -745,24 +796,29 @@ Object.defineProperty(Transaction.prototype, '_txFlag', {
Transaction.isTransaction =
Transaction.isTx = function(tx) {
if (bitcoin.stopping) return;
return tx._txFlag === Transaction.prototype._txFlag;
};
Transaction.fromHex = function(hex) {
if (bitcoin.stopping) return;
return bitcoin.tx(bitcoindjs.txFromHex(hex));
};
Transaction.prototype.verify = function() {
if (bitcoin.stopping) return;
return this.verified = this.verified || bitcoindjs.verifyTransaction(this);
};
Transaction.prototype.sign =
Transaction.prototype.fill = function(options) {
if (bitcoin.stopping) return;
return Transaction.fill(this, options);
};
Transaction.sign =
Transaction.fill = function(tx, options) {
if (bitcoin.stopping) return;
var isTx = bitcoin.tx.isTx(tx)
, newTx;
@ -784,6 +840,7 @@ Transaction.fill = function(tx, options) {
};
Transaction.prototype.getHash = function(enc) {
if (bitcoin.stopping) return;
var data = bitcoindjs.getTxHex(this);
if (!this.txid || this.txid !== data.hash) {
this.txid = data.hash;
@ -795,10 +852,12 @@ Transaction.prototype.getHash = function(enc) {
};
Transaction.prototype.isCoinbase = function() {
if (bitcoin.stopping) return;
return this.vin.length === 1 && this.vin[0].coinbase;
};
Transaction.prototype.toHex = function() {
if (bitcoin.stopping) return;
var hex = Transaction.toHex(this);
if (!this.hex || hex !== this.hex) {
this.hex = hex;
@ -807,20 +866,24 @@ Transaction.prototype.toHex = function() {
};
Transaction.toHex = function(tx) {
if (bitcoin.stopping) return;
var data = bitcoindjs.getTxHex(tx);
return data.hex;
};
Transaction.prototype.toBinary = function() {
if (bitcoin.stopping) return;
return Transaction.toBinary(this);
};
Transaction.toBinary = function(tx) {
if (bitcoin.stopping) return;
var data = bitcoindjs.getTxHex(tx);
return new Buffer(data.hex, 'hex');
};
Transaction.broadcast = function(tx, options, callback) {
if (bitcoin.stopping) return;
if (typeof tx === 'string') {
tx = { hex: tx };
}
@ -859,6 +922,7 @@ Transaction.broadcast = function(tx, options, callback) {
};
Transaction.prototype.broadcast = function(options, callback) {
if (bitcoin.stopping) return;
if (!callback) {
callback = options;
options = null;
@ -879,6 +943,8 @@ function Addresses(data) {
return data;
}
if (bitcoin.stopping) return;
var self = this;
Object.keys(data).forEach(function(key) {
@ -898,6 +964,7 @@ Object.defineProperty(Transaction.prototype, '_addrFlag', {
Addresses.isAddresses =
Addresses.isAddr = function(addr) {
if (bitcoin.stopping) return;
return addr._txFlag === Addresses.prototype._addrFlag;
};
@ -907,6 +974,7 @@ Addresses.isAddr = function(addr) {
*/
function Wallet() {
if (bitcoin.stopping) return;
var obj = function() {
return obj;
};
@ -917,74 +985,91 @@ function Wallet() {
}
Wallet.prototype.createAddress = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletNewAddress(options || {});
};
Wallet.prototype.getAccountAddress = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletGetAccountAddress(options || {});
};
Wallet.prototype.setAccount = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletSetAccount(options || {});
};
Wallet.prototype.getAccount = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletGetAccount(options || {});
};
Wallet.prototype.getRecipients = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletGetRecipients(options || {});
};
Wallet.prototype.getRecipient = function(options) {
if (bitcoin.stopping) return;
options = options || {};
var label = options.label || label;
return bitcoindjs.walletGetRecipients({ _label: label });
};
Wallet.prototype.setRecipient = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletSetRecipient(options || {});
};
Wallet.prototype.removeRecipient = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletRemoveRecipient(options || {});
};
Wallet.prototype.sendTo = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.walletSendTo(options || {}, callback);
};
Wallet.prototype.sendFrom = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.walletSendFrom(options || {}, callback);
};
Wallet.prototype.move = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletMove(options || {});
};
Wallet.prototype.signMessage = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletSignMessage(options || {});
};
Wallet.prototype.verifyMessage = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletVerifyMessage(options || {});
};
Wallet.prototype.createMultiSigAddress = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletCreateMultiSigAddress(options || {});
};
Wallet.prototype.getBalance = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletGetBalance(options || {});
};
Wallet.prototype.getUnconfirmedBalance = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletGetUnconfirmedBalance(options || {});
};
// XXX Wallet Transactions
Wallet.prototype.listTransactions =
Wallet.prototype.getTransactions = function(options, callback) {
if (bitcoin.stopping) return;
var txs = bitcoindjs.walletListTransactions(options || {});
if (callback) {
// Retrieve to regular TXs from disk:
@ -1007,16 +1092,19 @@ Wallet.prototype.getTransactions = function(options, callback) {
Wallet.prototype.listReceivedByAddress =
Wallet.prototype.getReceivedByAddress = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletReceivedByAddress(options || {});
};
Wallet.prototype.listAccounts =
Wallet.prototype.getAccounts = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletListAccounts(options || {});
};
// XXX Wallet Transaction
Wallet.prototype.getTransaction = function(options, callback) {
if (bitcoin.stopping) return;
var tx = bitcoindjs.walletGetTransaction(options || {});
if (callback) {
// Retrieve to regular TX from disk:
@ -1031,76 +1119,94 @@ Wallet.prototype.getTransaction = function(options, callback) {
};
Wallet.prototype.backup = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletBackup(options || {});
};
Wallet.prototype.decrypt =
Wallet.prototype.passphrase = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletPassphrase(options || {});
};
Wallet.prototype.passphraseChange = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletPassphraseChange(options || {});
};
Wallet.prototype.forgetPassphrase =
Wallet.prototype.lock = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletLock(options || {});
};
Wallet.prototype.encrypt = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletEncrypt(options || {});
};
Wallet.prototype.encrypted = function() {
if (bitcoin.stopping) return;
return bitcoindjs.walletEncrypted();
};
Wallet.prototype.isEncrypted = function() {
if (bitcoin.stopping) return;
return this.encrypted().encrypted;
};
Wallet.prototype.isLocked = function() {
if (bitcoin.stopping) return;
return this.encrypted().locked;
};
Wallet.prototype.dumpKey = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletDumpKey(options || {});
};
Wallet.prototype.importKey = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.walletImportKey(options || {}, callback);
};
Wallet.prototype.keyPoolRefill = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletKeyPoolRefill(options || {});
};
Wallet.prototype.setTxFee = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletSetTxFee(options || {});
};
Wallet.prototype.dump = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.walletDumpWallet(options || {}, callback);
};
Wallet.prototype.import = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.walletImportWallet(options || {}, callback);
};
Wallet.prototype.changeLabel = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletChangeLabel(options || {});
};
Wallet.prototype.deleteAccount = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletDeleteAccount(options || {});
};
Wallet.prototype.isMine = function(options) {
if (bitcoin.stopping) return;
return bitcoindjs.walletIsMine(options || {});
};
Wallet.prototype.rescan = function(options, callback) {
if (bitcoin.stopping) return;
return bitcoindjs.walletRescan(options || {}, callback);
};
@ -1113,6 +1219,7 @@ Wallet = new Wallet;
var utils = {};
utils.forEach = function(obj, iter, done) {
if (bitcoin.stopping) return;
var pending = obj.length;
if (!pending) return done();
var next = function() {