From 7317076243423afafc9308ad5901690c2324c1fb Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 26 Jun 2017 22:47:55 -0700 Subject: [PATCH] bcoin: remove env.js. --- browser/index.js | 4 +- lib/bcoin-browser.js | 277 ++++++++++++++++++++++++++++++++ lib/bcoin.js | 290 +++++++++++++++++++++++++++++++--- lib/env-browser.js | 305 ----------------------------------- lib/env.js | 324 -------------------------------------- lib/workers/workerpool.js | 36 ++++- package.json | 2 +- webpack.config.js | 2 +- 8 files changed, 577 insertions(+), 663 deletions(-) create mode 100644 lib/bcoin-browser.js delete mode 100644 lib/env-browser.js delete mode 100644 lib/env.js diff --git a/browser/index.js b/browser/index.js index 66863e58..94fb830e 100644 --- a/browser/index.js +++ b/browser/index.js @@ -222,9 +222,7 @@ function formatWallet(wallet) { }); } -bcoin.set({ - useWorkers: true -}); +bcoin.workerpool.enable(); node = new bcoin.fullnode({ hash: true, diff --git a/lib/bcoin-browser.js b/lib/bcoin-browser.js new file mode 100644 index 00000000..61ef9601 --- /dev/null +++ b/lib/bcoin-browser.js @@ -0,0 +1,277 @@ +/*! + * bcoin.js - a javascript bitcoin library. + * Copyright (c) 2014-2015, Fedor Indutny (MIT License). + * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + */ + +'use strict'; + +var util = require('./utils/util'); +var global = util.global; + +/** + * A bcoin "environment" which exposes all + * constructors for primitives, the blockchain, + * mempool, wallet, etc. It also exposes a + * global worker pool. + * + * @exports bcoin + * @type {Object} + * + * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. + * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. + * + * @property {Object} bip70 - See {@link module:bip70}. + * + * @property {Object} blockchain - See {@link module:blockchain}. + * @property {Function} chain - See {@link module:blockchain.Chain}. + * @property {Function} chaindb - See {@link module:blockchain.ChainDB}. + * @property {Function} chainentry - See {@link module:blockchain.ChainEntry}. + * + * @property {Object} btc + * @property {Function} amount + * @property {Function} uri + * + * @property {Object} coins + * @property {Function} coinview + * + * @property {Object} crypto + * @property {Object} ec + * @property {Object} pk + * @property {Object} schnorr + * + * @property {Object} db + * @property {Object} ldb + * + * @property {Object} hd + * + * @property {Object} http + * @property {Object} rpc + * + * @property {Object} txmempool + * @property {Object} fees + * @property {Object} mempool + * @property {Object} mempoolentry + * + * @property {Object} mining + * @property {Object} miner + * @property {Object} minerblock + * + * @property {Object} net + * @property {Object} bip150 + * @property {Object} bip151 + * @property {Object} bip152 + * @property {Object} dns + * @property {Object} packets + * @property {Object} peer + * @property {Object} pool + * @property {Object} tcp + * + * @property {Object} node + * @property {Object} config + * @property {Object} fullnode + * @property {Object} logger + * @property {Object} spvnode + * + * @property {Object} primitives + * @property {Object} address + * @property {Object} block + * @property {Object} coin + * @property {Object} headers + * @property {Object} input + * @property {Object} invitem + * @property {Object} keyring + * @property {Object} merkleblock + * @property {Object} mtx + * @property {Object} netaddress + * @property {Object} outpoint + * @property {Object} output + * @property {Object} tx + * + * @property {Object} protocol + * @property {Object} consensus + * @property {Object} errors + * @property {Object} network + * @property {Object} networks + * @property {Object} policy + * @property {Object} timedata + * + * @property {Object} txscript + * @property {Object} opcodes + * @property {Object} program + * @property {Object} script + * @property {Object} sigcache + * @property {Object} stack + * @property {Object} witness + * + * @property {Object} utils + * @property {Object} base32 + * @property {Object} base58 + * @property {Object} bloom + * @property {Object} co + * @property {Object} encoding + * @property {Object} lock + * @property {Object} reader + * @property {Object} staticwriter + * @property {Object} util + * @property {Object} writer + * + * @property {Object} wallet + * @property {Object} path + * @property {Object} walletkey + * @property {Object} walletdb + * + * @property {Object} workers + * @property {Object} workerpool + */ + +var bcoin = exports; + +// Horrible BIP +bcoin.bip70 = require('./bip70'); + +// Blockchain +bcoin.blockchain = require('./blockchain'); +bcoin.chain = require('./blockchain/chain'); +bcoin.chaindb = require('./blockchain/chaindb'); +bcoin.chainentry = require('./blockchain/chainentry'); + +// BTC +bcoin.btc = require('./btc'); +bcoin.amount = require('./btc/amount'); +bcoin.uri = require('./btc/uri'); + +// Coins +bcoin.coins = require('./coins'); +bcoin.coinview = require('./coins/coinview'); + +// Crypto +bcoin.crypto = require('./crypto'); +bcoin.bn = require('./crypto/bn'); +bcoin.ec = require('./crypto/ec'); +bcoin.pk = require('./crypto/pk'); +bcoin.schnorr = require('./crypto/schnorr'); + +// DB +bcoin.db = require('./db'); +bcoin.ldb = require('./db/ldb'); + +// HD +bcoin.hd = require('./hd/hd'); + +// HTTP +bcoin.http = require('./http'); +bcoin.rpc = require('./http/rpc'); + +// Mempool +bcoin.txmempool = require('./mempool'); +bcoin.fees = require('./mempool/fees'); +bcoin.mempool = require('./mempool/mempool'); +bcoin.mempoolentry = require('./mempool/mempoolentry'); + +// Miner +bcoin.mining = require('./mining'); +bcoin.miner = require('./mining/miner'); +bcoin.template = require('./mining/template'); + +// Net +bcoin.net = require('./net'); +bcoin.bip150 = require('./net/bip150'); +bcoin.bip151 = require('./net/bip151'); +bcoin.bip152 = require('./net/bip152'); +bcoin.dns = require('./net/dns'); +bcoin.packets = require('./net/packets'); +bcoin.peer = require('./net/peer'); +bcoin.pool = require('./net/pool'); +bcoin.tcp = require('./net/tcp'); + +// Node +bcoin.node = require('./node'); +bcoin.config = require('./node/config'); +bcoin.fullnode = require('./node/fullnode'); +bcoin.logger = require('./node/logger'); +bcoin.spvnode = require('./node/spvnode'); + +// Primitives +bcoin.primitives = require('./primitives'); +bcoin.address = require('./primitives/address'); +bcoin.block = require('./primitives/block'); +bcoin.coin = require('./primitives/coin'); +bcoin.headers = require('./primitives/headers'); +bcoin.input = require('./primitives/input'); +bcoin.invitem = require('./primitives/invitem'); +bcoin.keyring = require('./primitives/keyring'); +bcoin.merkleblock = require('./primitives/merkleblock'); +bcoin.mtx = require('./primitives/mtx'); +bcoin.netaddress = require('./primitives/netaddress'); +bcoin.outpoint = require('./primitives/outpoint'); +bcoin.output = require('./primitives/output'); +bcoin.tx = require('./primitives/tx'); + +// Protocol +bcoin.protocol = require('./protocol'); +bcoin.consensus = require('./protocol/consensus'); +bcoin.errors = require('./protocol/errors'); +bcoin.network = require('./protocol/network'); +bcoin.networks = require('./protocol/networks'); +bcoin.policy = require('./protocol/policy'); +bcoin.timedata = require('./protocol/timedata'); + +// Script +bcoin.txscript = require('./script'); +bcoin.opcode = require('./script/opcode'); +bcoin.program = require('./script/program'); +bcoin.script = require('./script/script'); +bcoin.sigcache = require('./script/sigcache'); +bcoin.stack = require('./script/stack'); +bcoin.witness = require('./script/witness'); + +// Utils +bcoin.utils = require('./utils'); +bcoin.base32 = require('./utils/base32'); +bcoin.base58 = require('./utils/base58'); +bcoin.bloom = require('./utils/bloom'); +bcoin.co = require('./utils/co'); +bcoin.encoding = require('./utils/encoding'); +bcoin.int64 = require('./utils/int64'); +bcoin.lock = require('./utils/lock'); +bcoin.reader = require('./utils/reader'); +bcoin.staticwriter = require('./utils/staticwriter'); +bcoin.util = require('./utils/util'); +bcoin.writer = require('./utils/writer'); + +// Wallet +bcoin.wallet = require('./wallet'); +bcoin.path = require('./wallet/path'); +bcoin.walletkey = require('./wallet/walletkey'); +bcoin.walletdb = require('./wallet/walletdb'); +bcoin.walletplugin = require('./wallet/plugin'); + +// Workers +bcoin.workers = require('./workers'); +bcoin.workerpool = require('./workers/workerpool'); + +/** + * Set the default network. + * @param {String} network + */ + +bcoin.set = function set(network) { + bcoin.network.set(network); + return bcoin; +}; + +/** + * Cache all necessary modules. + */ + +bcoin.cache = function cache() { + ; +}; + +/* + * Expose + */ + +global.bcoin = bcoin; diff --git a/lib/bcoin.js b/lib/bcoin.js index ad0bfbeb..7faceb1e 100644 --- a/lib/bcoin.js +++ b/lib/bcoin.js @@ -2,37 +2,285 @@ * bcoin.js - a javascript bitcoin library. * Copyright (c) 2014-2015, Fedor Indutny (MIT License). * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). - * https://github.com/indutny/bcoin + * https://github.com/bcoin-org/bcoin */ 'use strict'; /** - * Exposes the global environment. - * An instance of {@link Environment}. - * @module bcoin - * @see {Environment} - * @license - * Copyright (c) 2014-2015, Fedor Indutny (MIT License). - * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). - * https://github.com/indutny/bcoin + * A bcoin "environment" which exposes all + * constructors for primitives, the blockchain, + * mempool, wallet, etc. It also exposes a + * global worker pool. + * + * @exports bcoin + * @type {Object} + * + * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. + * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. + * + * @property {Object} bip70 - See {@link module:bip70}. + * + * @property {Object} blockchain - See {@link module:blockchain}. + * @property {Function} chain - See {@link module:blockchain.Chain}. + * @property {Function} chaindb - See {@link module:blockchain.ChainDB}. + * @property {Function} chainentry - See {@link module:blockchain.ChainEntry}. + * + * @property {Object} btc + * @property {Function} amount + * @property {Function} uri + * + * @property {Object} coins + * @property {Function} coinview + * + * @property {Object} crypto + * @property {Object} ec + * @property {Object} pk + * @property {Object} schnorr + * + * @property {Object} db + * @property {Object} ldb + * + * @property {Object} hd + * + * @property {Object} http + * @property {Object} rpc + * + * @property {Object} txmempool + * @property {Object} fees + * @property {Object} mempool + * @property {Object} mempoolentry + * + * @property {Object} mining + * @property {Object} miner + * @property {Object} minerblock + * + * @property {Object} net + * @property {Object} bip150 + * @property {Object} bip151 + * @property {Object} bip152 + * @property {Object} dns + * @property {Object} packets + * @property {Object} peer + * @property {Object} pool + * @property {Object} tcp + * + * @property {Object} node + * @property {Object} config + * @property {Object} fullnode + * @property {Object} logger + * @property {Object} spvnode + * + * @property {Object} primitives + * @property {Object} address + * @property {Object} block + * @property {Object} coin + * @property {Object} headers + * @property {Object} input + * @property {Object} invitem + * @property {Object} keyring + * @property {Object} merkleblock + * @property {Object} mtx + * @property {Object} netaddress + * @property {Object} outpoint + * @property {Object} output + * @property {Object} tx + * + * @property {Object} protocol + * @property {Object} consensus + * @property {Object} errors + * @property {Object} network + * @property {Object} networks + * @property {Object} policy + * @property {Object} timedata + * + * @property {Object} txscript + * @property {Object} opcodes + * @property {Object} program + * @property {Object} script + * @property {Object} sigcache + * @property {Object} stack + * @property {Object} witness + * + * @property {Object} utils + * @property {Object} base32 + * @property {Object} base58 + * @property {Object} bloom + * @property {Object} co + * @property {Object} encoding + * @property {Object} lock + * @property {Object} reader + * @property {Object} staticwriter + * @property {Object} util + * @property {Object} writer + * + * @property {Object} wallet + * @property {Object} path + * @property {Object} walletkey + * @property {Object} walletdb + * + * @property {Object} workers + * @property {Object} workerpool */ -var env = require('./env'); -var util = require('./utils/util'); -var global = util.global; +var bcoin = exports; -/* - * Expose bcoin globally in the browser. +// Horrible BIP +bcoin.define('bip70', './bip70'); + +// Blockchain +bcoin.define('blockchain', './blockchain'); +bcoin.define('chain', './blockchain/chain'); +bcoin.define('chaindb', './blockchain/chaindb'); +bcoin.define('chainentry', './blockchain/chainentry'); + +// BTC +bcoin.define('btc', './btc'); +bcoin.define('amount', './btc/amount'); +bcoin.define('uri', './btc/uri'); + +// Coins +bcoin.define('coins', './coins'); +bcoin.define('coinview', './coins/coinview'); + +// Crypto +bcoin.define('crypto', './crypto'); +bcoin.define('bn', './crypto/bn'); +bcoin.define('ec', './crypto/ec'); +bcoin.define('pk', './crypto/pk'); +bcoin.define('schnorr', './crypto/schnorr'); + +// DB +bcoin.define('db', './db'); +bcoin.define('ldb', './db/ldb'); + +// HD +bcoin.define('hd', './hd/hd'); + +// HTTP +bcoin.define('http', './http'); +bcoin.define('rpc', './http/rpc'); + +// Mempool +bcoin.define('txmempool', './mempool'); +bcoin.define('fees', './mempool/fees'); +bcoin.define('mempool', './mempool/mempool'); +bcoin.define('mempoolentry', './mempool/mempoolentry'); + +// Miner +bcoin.define('mining', './mining'); +bcoin.define('miner', './mining/miner'); +bcoin.define('template', './mining/template'); + +// Net +bcoin.define('net', './net'); +bcoin.define('bip150', './net/bip150'); +bcoin.define('bip151', './net/bip151'); +bcoin.define('bip152', './net/bip152'); +bcoin.define('dns', './net/dns'); +bcoin.define('packets', './net/packets'); +bcoin.define('peer', './net/peer'); +bcoin.define('pool', './net/pool'); +bcoin.define('tcp', './net/tcp'); + +// Node +bcoin.define('node', './node'); +bcoin.define('config', './node/config'); +bcoin.define('fullnode', './node/fullnode'); +bcoin.define('logger', './node/logger'); +bcoin.define('spvnode', './node/spvnode'); + +// Primitives +bcoin.define('primitives', './primitives'); +bcoin.define('address', './primitives/address'); +bcoin.define('block', './primitives/block'); +bcoin.define('coin', './primitives/coin'); +bcoin.define('headers', './primitives/headers'); +bcoin.define('input', './primitives/input'); +bcoin.define('invitem', './primitives/invitem'); +bcoin.define('keyring', './primitives/keyring'); +bcoin.define('merkleblock', './primitives/merkleblock'); +bcoin.define('mtx', './primitives/mtx'); +bcoin.define('netaddress', './primitives/netaddress'); +bcoin.define('outpoint', './primitives/outpoint'); +bcoin.define('output', './primitives/output'); +bcoin.define('tx', './primitives/tx'); + +// Protocol +bcoin.define('protocol', './protocol'); +bcoin.define('consensus', './protocol/consensus'); +bcoin.define('errors', './protocol/errors'); +bcoin.define('network', './protocol/network'); +bcoin.define('networks', './protocol/networks'); +bcoin.define('policy', './protocol/policy'); +bcoin.define('timedata', './protocol/timedata'); + +// Script +bcoin.define('txscript', './script'); +bcoin.define('opcode', './script/opcode'); +bcoin.define('program', './script/program'); +bcoin.define('script', './script/script'); +bcoin.define('sigcache', './script/sigcache'); +bcoin.define('stack', './script/stack'); +bcoin.define('witness', './script/witness'); + +// Utils +bcoin.define('utils', './utils'); +bcoin.define('base32', './utils/base32'); +bcoin.define('base58', './utils/base58'); +bcoin.define('bloom', './utils/bloom'); +bcoin.define('co', './utils/co'); +bcoin.define('encoding', './utils/encoding'); +bcoin.define('int64', './utils/int64'); +bcoin.define('lock', './utils/lock'); +bcoin.define('reader', './utils/reader'); +bcoin.define('staticwriter', './utils/staticwriter'); +bcoin.define('util', './utils/util'); +bcoin.define('writer', './utils/writer'); + +// Wallet +bcoin.define('wallet', './wallet'); +bcoin.define('path', './wallet/path'); +bcoin.define('walletkey', './wallet/walletkey'); +bcoin.define('walletdb', './wallet/walletdb'); +bcoin.define('walletplugin', './wallet/plugin'); + +// Workers +bcoin.define('workers', './workers'); +bcoin.define('workerpool', './workers/workerpool'); + +/** + * Set the default network. + * @param {String} network */ -if (typeof window !== 'undefined' - || typeof self !== 'undefined') { - global.bcoin = env; -} +bcoin.set = function set(network) { + bcoin.network.set(network); + return bcoin; +}; -/* - * Expose +/** + * Cache all necessary modules. */ -module.exports = env; +bcoin.cache = function cache() { + bcoin.bip70; + bcoin.common; + bcoin.crypto; + bcoin.fullnode; + bcoin.http; + bcoin.spvnode; +}; + +/** + * Cache all necessary modules. + */ + +bcoin.define = function _require(name, path) { + var cache; + bcoin.__defineGetter__(name, function() { + if (!cache) + cache = require(path); + return cache; + }); +}; diff --git a/lib/env-browser.js b/lib/env-browser.js deleted file mode 100644 index 0c740bfe..00000000 --- a/lib/env-browser.js +++ /dev/null @@ -1,305 +0,0 @@ -/*! - * env.js - environment for bcoin - * Copyright (c) 2014-2015, Fedor Indutny (MIT License). - * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -/** - * A bcoin "environment" which exposes all - * constructors for primitives, the blockchain, - * mempool, wallet, etc. It also exposes a - * global worker pool. - * @exports Environment - * @constructor - * @property {Function} env - See {@link Environment}. - * @property {Function} require - See {@link module:utils/lazy}. - * - * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. - * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. - * - * @property {Object} bip70 - See {@link module:bip70}. - * - * @property {Object} blockchain - See {@link module:blockchain}. - * @property {Function} chain - See {@link module:blockchain.Chain}. - * @property {Function} chaindb - See {@link module:blockchain.ChainDB}. - * @property {Function} chainentry - See {@link module:blockchain.ChainEntry}. - * - * @property {Object} btc - * @property {Function} amount - * @property {Function} uri - * - * @property {Object} coins - * @property {Function} coinview - * - * @property {Object} crypto - * @property {Object} ec - * @property {Object} pk - * @property {Object} schnorr - * - * @property {Object} db - * @property {Object} ldb - * - * @property {Object} hd - * - * @property {Object} http - * @property {Object} rpc - * - * @property {Object} txmempool - * @property {Object} fees - * @property {Object} mempool - * @property {Object} mempoolentry - * - * @property {Object} mining - * @property {Object} miner - * @property {Object} minerblock - * - * @property {Object} net - * @property {Object} bip150 - * @property {Object} bip151 - * @property {Object} bip152 - * @property {Object} dns - * @property {Object} packets - * @property {Object} peer - * @property {Object} pool - * @property {Object} tcp - * - * @property {Object} node - * @property {Object} config - * @property {Object} fullnode - * @property {Object} logger - * @property {Object} spvnode - * - * @property {Object} primitives - * @property {Object} address - * @property {Object} block - * @property {Object} coin - * @property {Object} headers - * @property {Object} input - * @property {Object} invitem - * @property {Object} keyring - * @property {Object} merkleblock - * @property {Object} mtx - * @property {Object} netaddress - * @property {Object} outpoint - * @property {Object} output - * @property {Object} tx - * - * @property {Object} protocol - * @property {Object} consensus - * @property {Object} errors - * @property {Object} network - * @property {Object} networks - * @property {Object} policy - * @property {Object} timedata - * - * @property {Object} txscript - * @property {Object} opcodes - * @property {Object} program - * @property {Object} script - * @property {Object} sigcache - * @property {Object} stack - * @property {Object} witness - * - * @property {Object} utils - * @property {Object} base32 - * @property {Object} base58 - * @property {Object} bloom - * @property {Object} co - * @property {Object} encoding - * @property {Object} lock - * @property {Object} reader - * @property {Object} staticwriter - * @property {Object} util - * @property {Object} writer - * - * @property {Object} wallet - * @property {Object} path - * @property {Object} walletkey - * @property {Object} walletdb - * - * @property {Object} workers - * @property {Object} workerpool - */ - -function Environment() { - this.env = Environment; - - // Horrible BIP - this.bip70 = require('./bip70'); - - // Blockchain - this.blockchain = require('./blockchain'); - this.chain = require('./blockchain/chain'); - this.chaindb = require('./blockchain/chaindb'); - this.chainentry = require('./blockchain/chainentry'); - - // BTC - this.btc = require('./btc'); - this.amount = require('./btc/amount'); - this.uri = require('./btc/uri'); - - // Coins - this.coins = require('./coins'); - this.coinview = require('./coins/coinview'); - - // Crypto - this.crypto = require('./crypto'); - this.bn = require('./crypto/bn'); - this.ec = require('./crypto/ec'); - this.pk = require('./crypto/pk'); - this.schnorr = require('./crypto/schnorr'); - - // DB - this.db = require('./db'); - this.ldb = require('./db/ldb'); - - // HD - this.hd = require('./hd/hd'); - - // HTTP - this.http = require('./http'); - this.rpc = require('./http/rpc'); - - // Mempool - this.txmempool = require('./mempool'); - this.fees = require('./mempool/fees'); - this.mempool = require('./mempool/mempool'); - this.mempoolentry = require('./mempool/mempoolentry'); - - // Miner - this.mining = require('./mining'); - this.miner = require('./mining/miner'); - this.template = require('./mining/template'); - - // Net - this.net = require('./net'); - this.bip150 = require('./net/bip150'); - this.bip151 = require('./net/bip151'); - this.bip152 = require('./net/bip152'); - this.dns = require('./net/dns'); - this.packets = require('./net/packets'); - this.peer = require('./net/peer'); - this.pool = require('./net/pool'); - this.tcp = require('./net/tcp'); - - // Node - this.node = require('./node'); - this.config = require('./node/config'); - this.fullnode = require('./node/fullnode'); - this.logger = require('./node/logger'); - this.spvnode = require('./node/spvnode'); - - // Primitives - this.primitives = require('./primitives'); - this.address = require('./primitives/address'); - this.block = require('./primitives/block'); - this.coin = require('./primitives/coin'); - this.headers = require('./primitives/headers'); - this.input = require('./primitives/input'); - this.invitem = require('./primitives/invitem'); - this.keyring = require('./primitives/keyring'); - this.merkleblock = require('./primitives/merkleblock'); - this.mtx = require('./primitives/mtx'); - this.netaddress = require('./primitives/netaddress'); - this.outpoint = require('./primitives/outpoint'); - this.output = require('./primitives/output'); - this.tx = require('./primitives/tx'); - - // Protocol - this.protocol = require('./protocol'); - this.consensus = require('./protocol/consensus'); - this.errors = require('./protocol/errors'); - this.network = require('./protocol/network'); - this.networks = require('./protocol/networks'); - this.policy = require('./protocol/policy'); - this.timedata = require('./protocol/timedata'); - - // Script - this.txscript = require('./script'); - this.opcode = require('./script/opcode'); - this.program = require('./script/program'); - this.script = require('./script/script'); - this.sigcache = require('./script/sigcache'); - this.stack = require('./script/stack'); - this.witness = require('./script/witness'); - - // Utils - this.utils = require('./utils'); - this.base32 = require('./utils/base32'); - this.base58 = require('./utils/base58'); - this.bloom = require('./utils/bloom'); - this.co = require('./utils/co'); - this.encoding = require('./utils/encoding'); - this.int64 = require('./utils/int64'); - this.lock = require('./utils/lock'); - this.reader = require('./utils/reader'); - this.staticwriter = require('./utils/staticwriter'); - this.util = require('./utils/util'); - this.writer = require('./utils/writer'); - - // Wallet - this.wallet = require('./wallet'); - this.path = require('./wallet/path'); - this.walletkey = require('./wallet/walletkey'); - this.walletdb = require('./wallet/walletdb'); - this.walletplugin = require('./wallet/plugin'); - - // Workers - this.workers = require('./workers'); - this.workerpool = require('./workers/workerpool'); -} - -/** - * Set the default network. - * @param {String} options - */ - -Environment.prototype.set = function set(options) { - if (typeof options === 'string') - options = { network: options }; - - if (!options) - options = {}; - - if (options.network) - this.network.set(options.network); - - this.workerpool.set(options); - - if (options.sigcacheSize != null) - this.sigcache.resize(options.sigcacheSize); - - return this; -}; - -/** - * Get the adjusted time of - * the default network. - * @returns {Number} Adjusted time. - */ - -Environment.prototype.now = function now() { - return this.network.primary.now(); -}; - -/** - * Cache all necessary modules. - */ - -Environment.prototype.cache = function cache() { - ; -}; - -/* - * Expose by converting `exports` to an - * Environment. - */ - -exports.cache = Environment.prototype.cache; -exports.set = Environment.prototype.set; -exports.now = Environment.prototype.now; - -Environment.call(exports); diff --git a/lib/env.js b/lib/env.js deleted file mode 100644 index 9be55ba6..00000000 --- a/lib/env.js +++ /dev/null @@ -1,324 +0,0 @@ -/*! - * env.js - environment for bcoin - * Copyright (c) 2014-2015, Fedor Indutny (MIT License). - * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -/** - * A bcoin "environment" which exposes all - * constructors for primitives, the blockchain, - * mempool, wallet, etc. It also exposes a - * global worker pool. - * @exports Environment - * @constructor - * @property {Function} env - See {@link Environment}. - * @property {Function} require - See {@link module:utils/lazy}. - * - * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. - * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. - * - * @property {Object} bip70 - See {@link module:bip70}. - * - * @property {Object} blockchain - See {@link module:blockchain}. - * @property {Function} chain - See {@link module:blockchain.Chain}. - * @property {Function} chaindb - See {@link module:blockchain.ChainDB}. - * @property {Function} chainentry - See {@link module:blockchain.ChainEntry}. - * - * @property {Object} btc - * @property {Function} amount - * @property {Function} uri - * - * @property {Object} coins - * @property {Function} coinview - * - * @property {Object} crypto - * @property {Object} ec - * @property {Object} pk - * @property {Object} schnorr - * - * @property {Object} db - * @property {Object} ldb - * - * @property {Object} hd - * - * @property {Object} http - * @property {Object} rpc - * - * @property {Object} txmempool - * @property {Object} fees - * @property {Object} mempool - * @property {Object} mempoolentry - * - * @property {Object} mining - * @property {Object} miner - * @property {Object} minerblock - * - * @property {Object} net - * @property {Object} bip150 - * @property {Object} bip151 - * @property {Object} bip152 - * @property {Object} dns - * @property {Object} packets - * @property {Object} peer - * @property {Object} pool - * @property {Object} tcp - * - * @property {Object} node - * @property {Object} config - * @property {Object} fullnode - * @property {Object} logger - * @property {Object} spvnode - * - * @property {Object} primitives - * @property {Object} address - * @property {Object} block - * @property {Object} coin - * @property {Object} headers - * @property {Object} input - * @property {Object} invitem - * @property {Object} keyring - * @property {Object} merkleblock - * @property {Object} mtx - * @property {Object} netaddress - * @property {Object} outpoint - * @property {Object} output - * @property {Object} tx - * - * @property {Object} protocol - * @property {Object} consensus - * @property {Object} errors - * @property {Object} network - * @property {Object} networks - * @property {Object} policy - * @property {Object} timedata - * - * @property {Object} txscript - * @property {Object} opcodes - * @property {Object} program - * @property {Object} script - * @property {Object} sigcache - * @property {Object} stack - * @property {Object} witness - * - * @property {Object} utils - * @property {Object} base32 - * @property {Object} base58 - * @property {Object} bloom - * @property {Object} co - * @property {Object} encoding - * @property {Object} lock - * @property {Object} reader - * @property {Object} staticwriter - * @property {Object} util - * @property {Object} writer - * - * @property {Object} wallet - * @property {Object} path - * @property {Object} walletkey - * @property {Object} walletdb - * - * @property {Object} workers - * @property {Object} workerpool - */ - -function Environment() { - this.env = Environment; - - // Horrible BIP - this.require('bip70', './bip70'); - - // Blockchain - this.require('blockchain', './blockchain'); - this.require('chain', './blockchain/chain'); - this.require('chaindb', './blockchain/chaindb'); - this.require('chainentry', './blockchain/chainentry'); - - // BTC - this.require('btc', './btc'); - this.require('amount', './btc/amount'); - this.require('uri', './btc/uri'); - - // Coins - this.require('coins', './coins'); - this.require('coinview', './coins/coinview'); - - // Crypto - this.require('crypto', './crypto'); - this.require('bn', './crypto/bn'); - this.require('ec', './crypto/ec'); - this.require('pk', './crypto/pk'); - this.require('schnorr', './crypto/schnorr'); - - // DB - this.require('db', './db'); - this.require('ldb', './db/ldb'); - - // HD - this.require('hd', './hd/hd'); - - // HTTP - this.require('http', './http'); - this.require('rpc', './http/rpc'); - - // Mempool - this.require('txmempool', './mempool'); - this.require('fees', './mempool/fees'); - this.require('mempool', './mempool/mempool'); - this.require('mempoolentry', './mempool/mempoolentry'); - - // Miner - this.require('mining', './mining'); - this.require('miner', './mining/miner'); - this.require('template', './mining/template'); - - // Net - this.require('net', './net'); - this.require('bip150', './net/bip150'); - this.require('bip151', './net/bip151'); - this.require('bip152', './net/bip152'); - this.require('dns', './net/dns'); - this.require('packets', './net/packets'); - this.require('peer', './net/peer'); - this.require('pool', './net/pool'); - this.require('tcp', './net/tcp'); - - // Node - this.require('node', './node'); - this.require('config', './node/config'); - this.require('fullnode', './node/fullnode'); - this.require('logger', './node/logger'); - this.require('spvnode', './node/spvnode'); - - // Primitives - this.require('primitives', './primitives'); - this.require('address', './primitives/address'); - this.require('block', './primitives/block'); - this.require('coin', './primitives/coin'); - this.require('headers', './primitives/headers'); - this.require('input', './primitives/input'); - this.require('invitem', './primitives/invitem'); - this.require('keyring', './primitives/keyring'); - this.require('merkleblock', './primitives/merkleblock'); - this.require('mtx', './primitives/mtx'); - this.require('netaddress', './primitives/netaddress'); - this.require('outpoint', './primitives/outpoint'); - this.require('output', './primitives/output'); - this.require('tx', './primitives/tx'); - - // Protocol - this.require('protocol', './protocol'); - this.require('consensus', './protocol/consensus'); - this.require('errors', './protocol/errors'); - this.require('network', './protocol/network'); - this.require('networks', './protocol/networks'); - this.require('policy', './protocol/policy'); - this.require('timedata', './protocol/timedata'); - - // Script - this.require('txscript', './script'); - this.require('opcode', './script/opcode'); - this.require('program', './script/program'); - this.require('script', './script/script'); - this.require('sigcache', './script/sigcache'); - this.require('stack', './script/stack'); - this.require('witness', './script/witness'); - - // Utils - this.require('utils', './utils'); - this.require('base32', './utils/base32'); - this.require('base58', './utils/base58'); - this.require('bloom', './utils/bloom'); - this.require('co', './utils/co'); - this.require('encoding', './utils/encoding'); - this.require('int64', './utils/int64'); - this.require('lock', './utils/lock'); - this.require('reader', './utils/reader'); - this.require('staticwriter', './utils/staticwriter'); - this.require('util', './utils/util'); - this.require('writer', './utils/writer'); - - // Wallet - this.require('wallet', './wallet'); - this.require('path', './wallet/path'); - this.require('walletkey', './wallet/walletkey'); - this.require('walletdb', './wallet/walletdb'); - this.require('walletplugin', './wallet/plugin'); - - // Workers - this.require('workers', './workers'); - this.require('workerpool', './workers/workerpool'); -} - -/** - * Set the default network. - * @param {String} options - */ - -Environment.prototype.set = function set(options) { - if (typeof options === 'string') - options = { network: options }; - - if (!options) - options = {}; - - if (options.network) - this.network.set(options.network); - - this.workerpool.set(options); - - if (options.sigcacheSize != null) - this.sigcache.resize(options.sigcacheSize); - - return this; -}; - -/** - * Get the adjusted time of - * the default network. - * @returns {Number} Adjusted time. - */ - -Environment.prototype.now = function now() { - return this.network.primary.now(); -}; - -/** - * Cache all necessary modules. - */ - -Environment.prototype.cache = function cache() { - this.bip70; - this.common; - this.crypto; - this.fullnode; - this.http; - this.spvnode; -}; - -/** - * Cache all necessary modules. - */ - -Environment.prototype.require = function _require(name, path) { - var cache; - this.__defineGetter__(name, function() { - if (!cache) - cache = require(path); - return cache; - }); -}; - -/* - * Expose by converting `exports` to an - * Environment. - */ - -exports.cache = Environment.prototype.cache; -exports.require = Environment.prototype.require; -exports.set = Environment.prototype.set; -exports.now = Environment.prototype.now; - -Environment.call(exports); diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index a443db9f..f90c3a02 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -175,6 +175,22 @@ WorkerPool.prototype.set = function set(options) { } }; +/** + * Enable the worker pool. + */ + +WorkerPool.prototype.enable = function enable() { + this.enabled = true; +}; + +/** + * Disable the worker pool. + */ + +WorkerPool.prototype.disable = function disable() { + this.enabled = true; +}; + /** * Spawn a new worker. * @param {Number} id - Worker ID. @@ -886,17 +902,21 @@ function getCores() { exports.pool = new WorkerPool(); exports.set = function set(options) { - this.pool.set({ - enabled: options.useWorkers, - size: options.maxWorkers || null, - timeout: options.workerTimeout || null - }); + this.pool.set(options); +}; + +exports.enable = function enable() { + this.pool.enable(); +}; + +exports.disable = function disable() { + this.pool.disable(); }; exports.set({ - useWorkers: HAS_CP && +process.env.BCOIN_USE_WORKERS !== 0, - maxWorkers: +process.env.BCOIN_MAX_WORKERS, - workerTimeout: +process.env.BCOIN_WORKER_TIMEOUT + enabled: HAS_CP && +process.env.BCOIN_WORKERS_ENABLED !== 0, + size: +process.env.BCOIN_WORKERS_SIZE || null, + timeout: +process.env.BCOIN_WORKERS_TIMEOUT || null }); /* diff --git a/package.json b/package.json index af342237..0c7d2922 100644 --- a/package.json +++ b/package.json @@ -77,12 +77,12 @@ "os": "./browser/empty.js", "secp256k1": "./browser/empty.js", "socket.io": "./browser/empty.js", + "./lib/bcoin": "./lib/bcoin-browser.js", "./lib/blockchain/layout.js": "./lib/blockchain/layout-browser.js", "./lib/crypto/backend.js": "./lib/crypto/backend-browser.js", "./lib/crypto/ec.js": "./lib/crypto/ec-elliptic.js", "./lib/crypto/pk.js": "./lib/crypto/pk-browser.js", "./lib/db/backends.js": "./lib/db/backends-browser.js", - "./lib/env.js": "./lib/env-browser.js", "./lib/hd/wordlist": "./lib/hd/wordlist-browser.js", "./lib/http/base": "./browser/empty.js", "./lib/http/client": "./browser/empty.js", diff --git a/webpack.config.js b/webpack.config.js index 77c89ddf..36a85f94 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -6,7 +6,7 @@ var path = require('path'); module.exports = { target: 'web', entry: { - 'bcoin': './lib/bcoin', + 'bcoin': './lib/bcoin-browser', 'bcoin-master': './lib/workers/master' }, output: {