From 3ee513785189347576053a9eb1598013ff0dc088 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 1 May 2016 21:27:09 -0700 Subject: [PATCH] rename method. --- lib/bcoin.js | 16 +++++++------ lib/bcoin/chaindb.js | 55 +++----------------------------------------- lib/bcoin/env.js | 14 +++++------ lib/bcoin/utils.js | 24 +++++++++---------- test/tx-test.js | 2 +- test/wallet-test.js | 1 + 6 files changed, 32 insertions(+), 80 deletions(-) diff --git a/lib/bcoin.js b/lib/bcoin.js index e027b34d..df7c3f23 100644 --- a/lib/bcoin.js +++ b/lib/bcoin.js @@ -8,7 +8,7 @@ */ var Environment = require('./bcoin/env'); -var networks = {}; +var env = {}; /** * Create a new Environment. Note that this will @@ -18,7 +18,7 @@ var networks = {}; * @returns {Environment} */ -module.exports = function BCoin(options) { +function BCoin(options) { var network = 'main'; if (options) { @@ -28,10 +28,12 @@ module.exports = function BCoin(options) { network = options; } - if (!networks[network]) - networks[network] = new Environment(options); + if (!env[network]) + env[network] = new Environment(options); - return networks[network]; -}; + return env[network]; +} -module.exports.env = Environment; +BCoin.env = Environment; + +module.exports = BCoin; diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 33b82e73..ab551614 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -1310,12 +1310,7 @@ ChainDB.prototype.getFullBlock = function getFullBlock(hash, callback) { if (!block) return callback(); - return self.fillHistory(block.txs, function(err) { - if (err) - return callback(err); - - return callback(null, block); - }); + return self.fillHistoryBlock(block, callback); }); }; @@ -1342,15 +1337,7 @@ ChainDB.prototype._ensureHistory = function _ensureHistory(hash, callback) { if (hash instanceof bcoin.block) return utils.asyncify(callback)(null, hash); - return this.getBlock(hash, function(err, block) { - if (err) - return callback(err); - - if (!block) - return callback(); - - return self.fillUndoBlock(block, callback); - }); + return this.getFullBlock(hash, callback); }; /** @@ -1389,42 +1376,6 @@ ChainDB.prototype.fillBlock = function fillBlock(block, callback) { }); }; -/** - * Fill a block with coins (historical). - * @param {Block} block - * @param {Function} callback - Returns [Error, {@link Block}]. - */ - -ChainDB.prototype.fillHistoryBlock = function fillHistoryBlock(block, callback) { - return this.fillHistory(block.txs, function(err) { - var coins, i, tx, hash, j, input, key; - - if (err) - return callback(err); - - coins = {}; - - for (i = 0; i < block.txs.length; i++) { - tx = block.txs[i]; - hash = tx.hash('hex'); - - for (j = 0; j < tx.inputs.length; j++) { - input = tx.inputs[j]; - key = input.prevout.hash + '/' + input.prevout.index; - if (!input.coin && coins[key]) { - input.coin = coins[key]; - delete coins[key]; - } - } - - for (j = 0; j < tx.outputs.length; j++) - coins[hash + '/' + j] = bcoin.coin(tx, j); - } - - return callback(null, block); - }); -}; - /** * Get coins necessary to be resurrected during a reorg. * @param {Hash} hash @@ -1463,7 +1414,7 @@ ChainDB.prototype.getUndoCoins = function getUndoCoins(hash, callback) { * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.fillUndoBlock = function fillUndoBlock(block, callback) { +ChainDB.prototype.fillHistoryBlock = function fillHistoryBlock(block, callback) { var i, j, k, tx, input; return this.getUndoCoins(block.hash('hex'), function(err, coins) { diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index d3945d23..4e7addfc 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -121,9 +121,7 @@ function Environment(options) { this._ensured = false; this._debug = null; - this.isBrowser = - (typeof process !== 'undefined' && process.browser) - || typeof window !== 'undefined'; + this.isBrowser = utils.isBrowser; this.prefix = options.prefix || process.env.BCOIN_PREFIX @@ -261,9 +259,9 @@ Environment.prototype.debug = function debug() { if (this.isBrowser) { if (this.debugLogs) { - msg = typeof args[0] === 'object' - ? args[0] - : utils.format(args, false).slice(0, -1); + msg = typeof args[0] !== 'object' + ? utils.format(args, false) + : args[0]; console.error(msg); } return; @@ -271,7 +269,7 @@ Environment.prototype.debug = function debug() { if (this.debugLogs) { msg = utils.format(args, true); - process.stderr.write(msg); + process.stderr.write(msg + '\n'); } if (this.debugFile) { @@ -280,7 +278,7 @@ Environment.prototype.debug = function debug() { this._debug = fs.createWriteStream(this.debugFile, { flags: 'a' }); } msg = utils.format(args, false); - this._debug.write(process.pid + ': ' + msg); + this._debug.write(process.pid + ': ' + msg + '\n'); } }; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 342b9115..633875fe 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -951,7 +951,7 @@ utils.array2ip = function array2ip(ip, version) { * @return {String} */ -utils._inspect = function _inspect(obj, color) { +utils.inspectify = function inspectify(obj, color) { return typeof obj !== 'string' ? util.inspect(obj, null, 20, color !== false) : obj; @@ -966,12 +966,12 @@ utils._inspect = function _inspect(obj, color) { utils.format = function format(args, color) { color = color - ? (process.stdout ? process.stdout.isTTY : true) + ? (process.stdout ? process.stdout.isTTY : false) : false; return typeof args[0] === 'object' - ? utils._inspect(args[0], color) + '\n' - : util.format.apply(util, args) + '\n'; + ? utils.inspectify(args[0], color) + : util.format.apply(util, args); }; /** @@ -985,15 +985,15 @@ utils.print = function print() { var msg; if (utils.isBrowser) { - msg = typeof args[0] === 'object' - ? args[0] - : utils.format(args, false).slice(0, -1); + msg = typeof args[0] !== 'object' + ? utils.format(args, false) + : args[0]; console.log(msg); return; } msg = utils.format(args, true); - process.stdout.write(msg); + process.stdout.write(msg + '\n'); }; /** @@ -1007,15 +1007,15 @@ utils.error = function error() { var msg; if (utils.isBrowser) { - msg = typeof args[0] === 'object' - ? args[0] - : utils.format(args, false).slice(0, -1); + msg = typeof args[0] !== 'object' + ? utils.format(args, false) + : args[0]; console.error(msg); return; } msg = utils.format(args, true); - process.stderr.write(msg); + process.stderr.write(msg + '\n'); }; /** diff --git a/test/tx-test.js b/test/tx-test.js index c74d86dc..470b8d81 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -170,7 +170,7 @@ describe('TX', function() { tx: tx, flags: flags, comments: tx.hasCoins() - ? utils._inspect(tx.inputs[0].coin.script, false).slice(0, -1) + ? utils.inspectify(tx.inputs[0].coin.script, false) : 'coinbase', data: data }; diff --git a/test/wallet-test.js b/test/wallet-test.js index 044aab3a..b7f34611 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -563,5 +563,6 @@ describe('Wallet', function() { it('should cleanup', function(cb) { constants.tx.COINBASE_MATURITY = 100; + cb(); }); });