diff --git a/lib/chain/chain.js b/lib/chain/chain.js index 39d11bed..92e2004d 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -15,6 +15,7 @@ var assert = utils.assert; var VerifyError = bcoin.errors.VerifyError; var VerifyResult = utils.VerifyResult; var spawn = require('../utils/spawn'); +var co = spawn.co; /** * Represents a blockchain. @@ -183,7 +184,7 @@ Chain.prototype._init = function _init() { * @param {Function} callback */ -Chain.prototype._open = spawn.co(function* open() { +Chain.prototype._open = co(function* open() { var tip; this.logger.info('Chain is loading.'); @@ -252,7 +253,7 @@ Chain.prototype._lock = function _lock(block, force) { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Chain.prototype.verifyContext = spawn.co(function* verifyContext(block, prev) { +Chain.prototype.verifyContext = co(function* verifyContext(block, prev) { var state, view; state = yield this.verify(block, prev); @@ -288,7 +289,7 @@ Chain.prototype.isGenesis = function isGenesis(block) { * [{@link VerifyError}, {@link VerifyFlags}]. */ -Chain.prototype.verify = spawn.co(function* verify(block, prev) { +Chain.prototype.verify = co(function* verify(block, prev) { var ret = new VerifyResult(); var i, height, ts, tx, medianTime, commitmentHash, ancestors, state; @@ -418,7 +419,7 @@ Chain.prototype.verify = spawn.co(function* verify(block, prev) { * [{@link VerifyError}, {@link DeploymentState}]. */ -Chain.prototype.getDeployments = spawn.co(function* getDeployments(block, prev, ancestors) { +Chain.prototype.getDeployments = co(function* getDeployments(block, prev, ancestors) { var state = new DeploymentState(); var active; @@ -527,7 +528,7 @@ Chain.prototype.getDeployments = spawn.co(function* getDeployments(block, prev, * @param {Function} callback - Returns [{@link VerifyError}]. */ -Chain.prototype.checkDuplicates = spawn.co(function* checkDuplicates(block, prev) { +Chain.prototype.checkDuplicates = co(function* checkDuplicates(block, prev) { var height = prev.height + 1; var entry; @@ -568,7 +569,7 @@ Chain.prototype.checkDuplicates = spawn.co(function* checkDuplicates(block, prev * @param {Function} callback - Returns [{@link VerifyError}]. */ -Chain.prototype.findDuplicates = spawn.co(function* findDuplicates(block, prev) { +Chain.prototype.findDuplicates = co(function* findDuplicates(block, prev) { var height = prev.height + 1; var i, tx, result; @@ -607,7 +608,7 @@ Chain.prototype.findDuplicates = spawn.co(function* findDuplicates(block, prev) * @param {Function} callback - Returns [{@link VerifyError}]. */ -Chain.prototype.checkInputs = spawn.co(function* checkInputs(block, prev, state) { +Chain.prototype.checkInputs = co(function* checkInputs(block, prev, state) { var height = prev.height + 1; var historical = prev.isHistorical(); var sigops = 0; @@ -731,7 +732,7 @@ Chain.prototype._getCachedHeight = function _getCachedHeight(hash) { * @param {Function} callback - Returns [{@link Error}, {@link ChainEntry}]. */ -Chain.prototype.findFork = spawn.co(function* findFork(fork, longer) { +Chain.prototype.findFork = co(function* findFork(fork, longer) { while (fork.hash !== longer.hash) { while (longer.height > fork.height) { longer = yield longer.getPrevious(); @@ -761,7 +762,7 @@ Chain.prototype.findFork = spawn.co(function* findFork(fork, longer) { * @param {Function} callback */ -Chain.prototype.reorganize = spawn.co(function* reorganize(entry, block) { +Chain.prototype.reorganize = co(function* reorganize(entry, block) { var tip = this.tip; var fork = yield this.findFork(tip, entry); var disconnect = []; @@ -807,7 +808,7 @@ Chain.prototype.reorganize = spawn.co(function* reorganize(entry, block) { * @param {Function} callback */ -Chain.prototype.disconnect = spawn.co(function* disconnect(entry) { +Chain.prototype.disconnect = co(function* disconnect(entry) { var items = yield this.db.disconnect(entry); var block = items[1]; var prev = yield entry.getPrevious(); @@ -833,7 +834,7 @@ Chain.prototype.disconnect = spawn.co(function* disconnect(entry) { * @param {Function} callback */ -Chain.prototype.reconnect = spawn.co(function* reconnect(entry) { +Chain.prototype.reconnect = co(function* reconnect(entry) { var block = yield this.db.getBlock(entry.hash); var prev, view; @@ -880,7 +881,7 @@ Chain.prototype.reconnect = spawn.co(function* reconnect(entry) { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Chain.prototype.setBestChain = spawn.co(function* setBestChain(entry, block, prev) { +Chain.prototype.setBestChain = co(function* setBestChain(entry, block, prev) { var view; assert(this.tip); @@ -929,7 +930,7 @@ Chain.prototype.setBestChain = spawn.co(function* setBestChain(entry, block, pre * @param {Function} callback */ -Chain.prototype.reset = spawn.co(function* reset(height, force) { +Chain.prototype.reset = co(function* reset(height, force) { var unlock = yield this._lock(null, force); var result; @@ -957,7 +958,7 @@ Chain.prototype.reset = spawn.co(function* reset(height, force) { * @param {Function} callback */ -Chain.prototype.resetTime = spawn.co(function* resetTime(ts) { +Chain.prototype.resetTime = co(function* resetTime(ts) { var unlock = yield this._lock(); var entry = yield this.byTime(ts); @@ -998,7 +999,7 @@ Chain.prototype.isBusy = function isBusy() { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Chain.prototype.add = spawn.co(function* add(block) { +Chain.prototype.add = co(function* add(block) { var ret, unlock, initial, hash, prevBlock; var height, checkpoint, orphan, entry; var existing, prev; @@ -1363,7 +1364,7 @@ Chain.prototype.pruneOrphans = function pruneOrphans() { * @param {Function} callback - Returns [Error, Boolean]. */ -Chain.prototype.has = spawn.co(function* has(hash) { +Chain.prototype.has = co(function* has(hash) { if (this.hasOrphan(hash)) return true; @@ -1382,7 +1383,7 @@ Chain.prototype.has = spawn.co(function* has(hash) { * @param {Function} callback - Returns [Error, {@link ChainEntry}]. */ -Chain.prototype.byTime = spawn.co(function* byTime(ts) { +Chain.prototype.byTime = co(function* byTime(ts) { var start = 0; var end = this.height; var pos, delta, entry; @@ -1521,7 +1522,7 @@ Chain.prototype.getProgress = function getProgress() { * @param {Function} callback - Returns [Error, {@link Hash}[]]. */ -Chain.prototype.getLocator = spawn.co(function* getLocator(start) { +Chain.prototype.getLocator = co(function* getLocator(start) { var unlock = yield this._lock(); var hashes = []; var step = 1; @@ -1609,7 +1610,7 @@ Chain.prototype.getOrphanRoot = function getOrphanRoot(hash) { * (target is in compact/mantissa form). */ -Chain.prototype.getCurrentTarget = spawn.co(function* getCurrentTarget() { +Chain.prototype.getCurrentTarget = co(function* getCurrentTarget() { if (!this.tip) return this.network.pow.bits; return yield this.getTargetAsync(null, this.tip); @@ -1623,7 +1624,7 @@ Chain.prototype.getCurrentTarget = spawn.co(function* getCurrentTarget() { * (target is in compact/mantissa form). */ -Chain.prototype.getTargetAsync = spawn.co(function* getTargetAsync(block, prev) { +Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) { var ancestors; if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) { @@ -1718,7 +1719,7 @@ Chain.prototype.retarget = function retarget(prev, first) { * hash of the latest known block). */ -Chain.prototype.findLocator = spawn.co(function* findLocator(locator) { +Chain.prototype.findLocator = co(function* findLocator(locator) { var i, hash, main; for (i = 0; i < locator.length; i++) { @@ -1742,7 +1743,7 @@ Chain.prototype.findLocator = spawn.co(function* findLocator(locator) { * @param {Function} callback - Returns [Error, Number]. */ -Chain.prototype.isActive = spawn.co(function* isActive(prev, id) { +Chain.prototype.isActive = co(function* isActive(prev, id) { var state; if (prev.isHistorical()) @@ -1763,7 +1764,7 @@ Chain.prototype.isActive = spawn.co(function* isActive(prev, id) { * @param {Function} callback - Returns [Error, Number]. */ -Chain.prototype.getState = spawn.co(function* getState(prev, id) { +Chain.prototype.getState = co(function* getState(prev, id) { var period = this.network.minerWindow; var threshold = this.network.activationThreshold; var deployment = this.network.deployments[id]; @@ -1888,7 +1889,7 @@ Chain.prototype.getState = spawn.co(function* getState(prev, id) { * @param {Function} callback - Returns [Error, Number]. */ -Chain.prototype.computeBlockVersion = spawn.co(function* computeBlockVersion(prev) { +Chain.prototype.computeBlockVersion = co(function* computeBlockVersion(prev) { var keys = Object.keys(this.network.deployments); var version = 0; var i, id, deployment, state; @@ -1916,7 +1917,7 @@ Chain.prototype.computeBlockVersion = spawn.co(function* computeBlockVersion(pre * @param {Function} callback - Returns [Error, {@link DeploymentState}]. */ -Chain.prototype.getDeploymentState = spawn.co(function* getDeploymentState() { +Chain.prototype.getDeploymentState = co(function* getDeploymentState() { var prev, ancestors; if (!this.tip) @@ -1941,7 +1942,7 @@ Chain.prototype.getDeploymentState = spawn.co(function* getDeploymentState() { * @param {Function} callback - Returns [Error, Boolean]. */ -Chain.prototype.checkFinal = spawn.co(function* checkFinal(prev, tx, flags) { +Chain.prototype.checkFinal = co(function* checkFinal(prev, tx, flags) { var height = prev.height + 1; var ts; @@ -1966,7 +1967,7 @@ Chain.prototype.checkFinal = spawn.co(function* checkFinal(prev, tx, flags) { * [Error, Number(minTime), Number(minHeight)]. */ -Chain.prototype.getLocks = spawn.co(function* getLocks(prev, tx, flags) { +Chain.prototype.getLocks = co(function* getLocks(prev, tx, flags) { var mask = constants.sequence.MASK; var granularity = constants.sequence.GRANULARITY; var disableFlag = constants.sequence.DISABLE_FLAG; @@ -2015,7 +2016,7 @@ Chain.prototype.getLocks = spawn.co(function* getLocks(prev, tx, flags) { * @param {Function} callback - Returns [Error, Boolean]. */ -Chain.prototype.evalLocks = spawn.co(function* evalLocks(prev, minHeight, minTime) { +Chain.prototype.evalLocks = co(function* evalLocks(prev, minHeight, minTime) { var medianTime; if (minHeight >= prev.height + 1) @@ -2040,7 +2041,7 @@ Chain.prototype.evalLocks = spawn.co(function* evalLocks(prev, minHeight, minTim * @param {Function} callback - Returns [Error, Boolean]. */ -Chain.prototype.checkLocks = spawn.co(function* checkLocks(prev, tx, flags) { +Chain.prototype.checkLocks = co(function* checkLocks(prev, tx, flags) { var times = yield this.getLocks(prev, tx, flags); var minHeight = times[0]; var minTime = times[1]; diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index f8a77120..36aca9aa 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -16,6 +16,7 @@ var DUMMY = new Buffer([0]); var BufferWriter = require('../utils/writer'); var BufferReader = require('../utils/reader'); var spawn = require('../utils/spawn'); +var co = spawn.co; /* * Database Layout: @@ -214,7 +215,7 @@ ChainDB.layout = layout; * @param {Function} callback */ -ChainDB.prototype._open = spawn.co(function* open() { +ChainDB.prototype._open = co(function* open() { var result, genesis, block; this.logger.info('Starting chain load.'); @@ -318,7 +319,7 @@ ChainDB.prototype.drop = function drop() { * @param {Function} callback */ -ChainDB.prototype.commit = spawn.co(function* commit() { +ChainDB.prototype.commit = co(function* commit() { assert(this.current); assert(this.pending); @@ -389,7 +390,7 @@ ChainDB.prototype.getCache = function getCache(hash) { * @param {Function} callback - Returns [Error, Number]. */ -ChainDB.prototype.getHeight = spawn.co(function* getHeight(hash) { +ChainDB.prototype.getHeight = co(function* getHeight(hash) { var entry, height; checkHash(hash); @@ -423,7 +424,7 @@ ChainDB.prototype.getHeight = spawn.co(function* getHeight(hash) { * @param {Function} callback - Returns [Error, {@link Hash}]. */ -ChainDB.prototype.getHash = spawn.co(function* getHash(height) { +ChainDB.prototype.getHash = co(function* getHash(height) { var entry; checkHash(height); @@ -447,7 +448,7 @@ ChainDB.prototype.getHash = spawn.co(function* getHash(height) { * @param {Function} callback - Returns [Error, Number]. */ -ChainDB.prototype.getChainHeight = spawn.co(function* getChainHeight() { +ChainDB.prototype.getChainHeight = co(function* getChainHeight() { var entry = yield this.getTip(); if (!entry) return -1; @@ -461,7 +462,7 @@ ChainDB.prototype.getChainHeight = spawn.co(function* getChainHeight() { * @param {Function} callback - Returns [Error, {@link Hash}, Number]. */ -ChainDB.prototype.getBoth = spawn.co(function* getBoth(block) { +ChainDB.prototype.getBoth = co(function* getBoth(block) { var hash, height; checkHash(block); @@ -494,7 +495,7 @@ ChainDB.prototype.getBoth = spawn.co(function* getBoth(block) { * @param {Function} callback - Returns [Error, {@link ChainEntry}]. */ -ChainDB.prototype.getEntry = spawn.co(function* getEntry(hash) { +ChainDB.prototype.getEntry = co(function* getEntry(hash) { var self = this; var entry; @@ -521,7 +522,7 @@ ChainDB.prototype.getEntry = spawn.co(function* getEntry(hash) { * @param {Function} callback - Returns [Error, {@link ChainEntry}]. */ -ChainDB.prototype.get = spawn.co(function* get(hash) { +ChainDB.prototype.get = co(function* get(hash) { var entry = yield this.getEntry(hash); if (!entry) @@ -548,7 +549,7 @@ ChainDB.prototype.get = spawn.co(function* get(hash) { * @param {Function} callback */ -ChainDB.prototype.save = spawn.co(function* save(entry, block, view, connect) { +ChainDB.prototype.save = co(function* save(entry, block, view, connect) { var hash = block.hash(); var height = new Buffer(4); @@ -592,7 +593,7 @@ ChainDB.prototype.save = spawn.co(function* save(entry, block, view, connect) { * @param {Function} callback - Returns [Error, {@link ChainState}]. */ -ChainDB.prototype.initState = spawn.co(function* initState() { +ChainDB.prototype.initState = co(function* initState() { var state = yield this.db.fetch(layout.R, function(data) { return ChainState.fromRaw(data); }); @@ -622,7 +623,7 @@ ChainDB.prototype.getTip = function getTip() { * Returns [Error, {@link ChainEntry}, {@link Block}]. */ -ChainDB.prototype.reconnect = spawn.co(function* reconnect(entry, block, view) { +ChainDB.prototype.reconnect = co(function* reconnect(entry, block, view) { var hash = block.hash(); this.start(); @@ -654,7 +655,7 @@ ChainDB.prototype.reconnect = spawn.co(function* reconnect(entry, block, view) { * Returns [Error, {@link ChainEntry}, {@link Block}]. */ -ChainDB.prototype.disconnect = spawn.co(function* disconnect(entry) { +ChainDB.prototype.disconnect = co(function* disconnect(entry) { var block; this.start(); @@ -714,7 +715,7 @@ ChainDB.prototype.getNextHash = function getNextHash(hash) { * @param {Function} callback - Returns [Error, Boolean]. */ -ChainDB.prototype.isMainChain = spawn.co(function* isMainChain(hash) { +ChainDB.prototype.isMainChain = co(function* isMainChain(hash) { var query, height, existing; if (hash instanceof bcoin.chainentry) { @@ -745,7 +746,7 @@ ChainDB.prototype.isMainChain = spawn.co(function* isMainChain(hash) { * @param {Function} callback */ -ChainDB.prototype.reset = spawn.co(function* reset(block) { +ChainDB.prototype.reset = co(function* reset(block) { var entry = yield this.get(block); var tip; @@ -790,7 +791,7 @@ ChainDB.prototype.reset = spawn.co(function* reset(block) { * @param {Function} callback - Returns [Error, Boolean]. */ -ChainDB.prototype.has = spawn.co(function* has(height) { +ChainDB.prototype.has = co(function* has(height) { var items, hash; checkHash(height); @@ -809,7 +810,7 @@ ChainDB.prototype.has = spawn.co(function* has(height) { * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.saveBlock = spawn.co(function* saveBlock(block, view, connect) { +ChainDB.prototype.saveBlock = co(function* saveBlock(block, view, connect) { if (this.options.spv) return block; @@ -830,7 +831,7 @@ ChainDB.prototype.saveBlock = spawn.co(function* saveBlock(block, view, connect) * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.removeBlock = spawn.co(function* removeBlock(hash) { +ChainDB.prototype.removeBlock = co(function* removeBlock(hash) { var block = yield this.getBlock(hash); if (!block) @@ -847,7 +848,7 @@ ChainDB.prototype.removeBlock = spawn.co(function* removeBlock(hash) { * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.connectBlock = spawn.co(function* connectBlock(block, view) { +ChainDB.prototype.connectBlock = co(function* connectBlock(block, view) { var undo = new BufferWriter(); var i, j, tx, input, output, prev; var hashes, address, hash, coins, raw; @@ -946,7 +947,7 @@ ChainDB.prototype.connectBlock = spawn.co(function* connectBlock(block, view) { * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.disconnectBlock = spawn.co(function* disconnectBlock(block) { +ChainDB.prototype.disconnectBlock = co(function* disconnectBlock(block) { var i, j, tx, input, output, prev, view; var hashes, address, hash, coins, raw; @@ -1040,7 +1041,7 @@ ChainDB.prototype.disconnectBlock = spawn.co(function* disconnectBlock(block) { * @param {Function} callback - Returns [Error, {@link TX}]. */ -ChainDB.prototype.fillCoins = spawn.co(function* fillCoins(tx) { +ChainDB.prototype.fillCoins = co(function* fillCoins(tx) { var i, input, coin; if (tx.isCoinbase()) @@ -1067,7 +1068,7 @@ ChainDB.prototype.fillCoins = spawn.co(function* fillCoins(tx) { * @param {Function} callback - Returns [Error, {@link TX}]. */ -ChainDB.prototype.fillHistory = spawn.co(function* fillHistory(tx) { +ChainDB.prototype.fillHistory = co(function* fillHistory(tx) { var i, input, tx; if (!this.options.indexTX) @@ -1098,7 +1099,7 @@ ChainDB.prototype.fillHistory = spawn.co(function* fillHistory(tx) { * @param {Function} callback - Returns [Error, {@link Coin}]. */ -ChainDB.prototype.getCoin = spawn.co(function* getCoin(hash, index) { +ChainDB.prototype.getCoin = co(function* getCoin(hash, index) { var self = this; var coins = this.coinCache.get(hash); @@ -1117,7 +1118,7 @@ ChainDB.prototype.getCoin = spawn.co(function* getCoin(hash, index) { * @param {Function} callback - Returns [Error, {@link Coins}]. */ -ChainDB.prototype.getCoins = spawn.co(function* getCoins(hash) { +ChainDB.prototype.getCoins = co(function* getCoins(hash) { var self = this; var coins = this.coinCache.get(hash); @@ -1138,7 +1139,7 @@ ChainDB.prototype.getCoins = spawn.co(function* getCoins(hash) { * @param {Function} callback */ -ChainDB.prototype.scan = spawn.co(function* scan(start, filter, iter) { +ChainDB.prototype.scan = co(function* scan(start, filter, iter) { var total = 0; var i, j, entry, hashes, hash, tx, txs, block; @@ -1224,7 +1225,7 @@ ChainDB.prototype.hasTX = function hasTX(hash) { * @param {Function} callback - Returns [Error, {@link Coin}[]]. */ -ChainDB.prototype.getCoinsByAddress = spawn.co(function* getCoinsByAddress(addresses) { +ChainDB.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses) { var coins = []; var i, j, address, hash, keys, key, coin; @@ -1283,7 +1284,7 @@ ChainDB.prototype.getEntries = function getEntries() { * @param {Function} callback - Returns [Error, {@link Hash}[]]. */ -ChainDB.prototype.getHashesByAddress = spawn.co(function* getHashesByAddress(addresses) { +ChainDB.prototype.getHashesByAddress = co(function* getHashesByAddress(addresses) { var hashes = {}; var i, address, hash; @@ -1316,7 +1317,7 @@ ChainDB.prototype.getHashesByAddress = spawn.co(function* getHashesByAddress(add * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -ChainDB.prototype.getTXByAddress = spawn.co(function* getTXByAddress(addresses) { +ChainDB.prototype.getTXByAddress = co(function* getTXByAddress(addresses) { var txs = []; var i, hashes, hash, tx; @@ -1344,7 +1345,7 @@ ChainDB.prototype.getTXByAddress = spawn.co(function* getTXByAddress(addresses) * @param {Function} callback - Returns [Error, {@link TX}]. */ -ChainDB.prototype.getFullTX = spawn.co(function* getFullTX(hash) { +ChainDB.prototype.getFullTX = co(function* getFullTX(hash) { var tx; if (!this.options.indexTX) @@ -1366,7 +1367,7 @@ ChainDB.prototype.getFullTX = spawn.co(function* getFullTX(hash) { * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.getFullBlock = spawn.co(function* getFullBlock(hash) { +ChainDB.prototype.getFullBlock = co(function* getFullBlock(hash) { var block = yield this.getBlock(hash); var view; @@ -1384,7 +1385,7 @@ ChainDB.prototype.getFullBlock = spawn.co(function* getFullBlock(hash) { * @param {Function} callback - Returns [Error, {@link CoinView}]. */ -ChainDB.prototype.getCoinView = spawn.co(function* getCoinView(block, callback) { +ChainDB.prototype.getCoinView = co(function* getCoinView(block, callback) { var view = new bcoin.coinview(); var prevout = block.getPrevout(); var i, prev, coins; @@ -1425,7 +1426,7 @@ ChainDB.prototype.getUndoCoins = function getUndoCoins(hash) { * @param {Function} callback - Returns [Error, {@link CoinView}]. */ -ChainDB.prototype.getUndoView = spawn.co(function* getUndoView(block) { +ChainDB.prototype.getUndoView = co(function* getUndoView(block) { var i, j, k, tx, input, coin, view, coins; view = yield this.getCoinView(block); @@ -1459,7 +1460,7 @@ ChainDB.prototype.getUndoView = spawn.co(function* getUndoView(block) { * @param {Function} callback - Returns [Error, {@link Block}]. */ -ChainDB.prototype.getBlock = spawn.co(function* getBlock(hash) { +ChainDB.prototype.getBlock = co(function* getBlock(hash) { var items = yield this.getBoth(hash); var height; @@ -1495,7 +1496,7 @@ ChainDB.prototype.hasCoins = function hasCoins(hash) { * @param {Function} callback */ -ChainDB.prototype.pruneBlock = spawn.co(function* pruneBlock(block) { +ChainDB.prototype.pruneBlock = co(function* pruneBlock(block) { var futureHeight, key, hash; if (this.options.spv) diff --git a/lib/chain/chainentry.js b/lib/chain/chainentry.js index d1d3bea3..03b93927 100644 --- a/lib/chain/chainentry.js +++ b/lib/chain/chainentry.js @@ -16,6 +16,7 @@ var assert = utils.assert; var BufferWriter = require('../utils/writer'); var BufferReader = require('../utils/reader'); var spawn = require('../utils/spawn'); +var co = spawn.co; /** * Represents an entry in the chain. Unlike @@ -177,7 +178,7 @@ ChainEntry.prototype.getRetargetAncestors = function getRetargetAncestors() { * @param {Function} callback - Returns [Error, ChainEntry[]]. */ -ChainEntry.prototype.getAncestors = spawn.co(function* getAncestors(max) { +ChainEntry.prototype.getAncestors = co(function* getAncestors(max) { var entry = this; var ancestors = []; var cached; @@ -230,7 +231,7 @@ ChainEntry.prototype.isMainChain = function isMainChain() { * @param {Function} callback - Returns [Error, ChainEntry[]]. */ -ChainEntry.prototype.getAncestorByHeight = spawn.co(function* getAncestorByHeight(height) { +ChainEntry.prototype.getAncestorByHeight = co(function* getAncestorByHeight(height) { var main, entry; if (height < 0) @@ -262,7 +263,7 @@ ChainEntry.prototype.getAncestorByHeight = spawn.co(function* getAncestorByHeigh * @returns {Function} callback - Returns [Error, ChainEntry]. */ -ChainEntry.prototype.getAncestor = spawn.co(function* getAncestor(index) { +ChainEntry.prototype.getAncestor = co(function* getAncestor(index) { var ancestors; assert(index >= 0); @@ -289,7 +290,7 @@ ChainEntry.prototype.getPrevious = function getPrevious() { * @param {Function} callback - Returns [Error, ChainEntry]. */ -ChainEntry.prototype.getNext = spawn.co(function* getNext() { +ChainEntry.prototype.getNext = co(function* getNext() { var hash = yield this.chain.db.getNextHash(this.hash); if (!hash) return; @@ -322,7 +323,7 @@ ChainEntry.prototype.getMedianTime = function getMedianTime(ancestors) { * @param {Function} callback - Returns [Error, Number]. */ -ChainEntry.prototype.getMedianTimeAsync = spawn.co(function* getMedianTimeAsync() { +ChainEntry.prototype.getMedianTimeAsync = co(function* getMedianTimeAsync() { var MEDIAN_TIMESPAN = constants.block.MEDIAN_TIMESPAN; var ancestors = yield this.getAncestors(MEDIAN_TIMESPAN); return this.getMedianTime(ancestors); @@ -409,7 +410,7 @@ ChainEntry.prototype.isSuperMajority = function isSuperMajority(version, require * @returns {Boolean} */ -ChainEntry.prototype.isSuperMajorityAsync = spawn.co(function* isSuperMajorityAsync(version, required) { +ChainEntry.prototype.isSuperMajorityAsync = co(function* isSuperMajorityAsync(version, required) { var majorityWindow = this.network.block.majorityWindow; var ancestors = yield this.getAncestors(majorityWindow); return this.isSuperMajority(version, required, ancestors); diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index 073ecb22..c9025713 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -13,6 +13,7 @@ var scrypt = require('./scrypt'); var scryptAsync = require('./scrypt-async'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var native = require('../utils/native'); var nativeCrypto, hash, aes; diff --git a/lib/db/lowlevelup.js b/lib/db/lowlevelup.js index 072a9d50..1f5a5ec1 100644 --- a/lib/db/lowlevelup.js +++ b/lib/db/lowlevelup.js @@ -11,6 +11,7 @@ var utils = require('../utils/utils'); var assert = utils.assert; var AsyncObject = require('../utils/async'); var spawn = require('../utils/spawn'); +var co = spawn.co; var P = utils.P; var VERSION_ERROR; @@ -290,7 +291,7 @@ LowlevelUp.prototype.approximateSize = function approximateSize(start, end) { * @param {Function} callback - Returns [Error, Boolean]. */ -LowlevelUp.prototype.has = spawn.co(function* has(key) { +LowlevelUp.prototype.has = co(function* has(key) { var value = yield this.get(key); return value != null; }); @@ -303,7 +304,7 @@ LowlevelUp.prototype.has = spawn.co(function* has(key) { * @param {Function} callback - Returns [Error, Object]. */ -LowlevelUp.prototype.fetch = spawn.co(function* fetch(key, parse) { +LowlevelUp.prototype.fetch = co(function* fetch(key, parse) { var value = yield this.get(key); if (!value) return; @@ -317,7 +318,7 @@ LowlevelUp.prototype.fetch = spawn.co(function* fetch(key, parse) { * @param {Function} callback - Returns [Error, Array]. */ -LowlevelUp.prototype.iterate = spawn.co(function* iterate(options) { +LowlevelUp.prototype.iterate = co(function* iterate(options) { var items = []; var iter, kv, result; @@ -345,7 +346,7 @@ LowlevelUp.prototype.iterate = spawn.co(function* iterate(options) { * @param {Function} callback */ -LowlevelUp.prototype.checkVersion = spawn.co(function* checkVersion(key, version) { +LowlevelUp.prototype.checkVersion = co(function* checkVersion(key, version) { var data = yield this.get(key); if (!data) { @@ -367,7 +368,7 @@ LowlevelUp.prototype.checkVersion = spawn.co(function* checkVersion(key, version * @param {Function} callback */ -LowlevelUp.prototype.clone = spawn.co(function* clone(path) { +LowlevelUp.prototype.clone = co(function* clone(path) { var opt = { keys: true, values: true }; var options = utils.merge({}, this.options); var hwm = 256 << 20; diff --git a/lib/http/client.js b/lib/http/client.js index 9b435d25..2bbfcf89 100644 --- a/lib/http/client.js +++ b/lib/http/client.js @@ -12,6 +12,7 @@ var AsyncObject = require('../utils/async'); var RPCClient = require('./rpcclient'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; var request = require('./request').promise; @@ -56,7 +57,7 @@ utils.inherits(HTTPClient, AsyncObject); * @param {Function} callback */ -HTTPClient.prototype._open = spawn.co(function* _open() { +HTTPClient.prototype._open = co(function* _open() { var self = this; var IOClient; @@ -163,7 +164,7 @@ HTTPClient.prototype._close = function close() { * @param {Function} callback - Returns [Error, Object?]. */ -HTTPClient.prototype._request = spawn.co(function* _request(method, endpoint, json) { +HTTPClient.prototype._request = co(function* _request(method, endpoint, json) { var query, network, height, res; if (this.token) { @@ -565,7 +566,7 @@ HTTPClient.prototype.send = function send(id, options) { * @param {Function} callback */ -HTTPClient.prototype.retoken = spawn.co(function* retoken(id, passphrase) { +HTTPClient.prototype.retoken = co(function* retoken(id, passphrase) { var options = { passphrase: passphrase }; var body = yield this._post('/wallet/' + id + '/retoken', options); return body.token; diff --git a/lib/http/rpc.js b/lib/http/rpc.js index cd4355c5..b5ccc9f3 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -9,6 +9,7 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var crypto = require('../crypto/crypto'); var assert = utils.assert; var constants = bcoin.constants; @@ -293,7 +294,7 @@ RPC.prototype.execute = function execute(json) { * Overall control/query calls */ -RPC.prototype.getinfo = spawn.co(function* getinfo(args) { +RPC.prototype.getinfo = co(function* getinfo(args) { var balance; if (args.help || args.length !== 0) @@ -615,7 +616,7 @@ RPC.prototype._getSoftforks = function _getSoftforks() { ]; }; -RPC.prototype._getBIP9Softforks = spawn.co(function* _getBIP9Softforks() { +RPC.prototype._getBIP9Softforks = co(function* _getBIP9Softforks() { var forks = {}; var keys = Object.keys(this.network.deployments); var i, id, deployment, state; @@ -655,7 +656,7 @@ RPC.prototype._getBIP9Softforks = spawn.co(function* _getBIP9Softforks() { }); /* Block chain and UTXO */ -RPC.prototype.getblockchaininfo = spawn.co(function* getblockchaininfo(args) { +RPC.prototype.getblockchaininfo = co(function* getblockchaininfo(args) { if (args.help || args.length !== 0) throw new RPCError('getblockchaininfo'); @@ -716,7 +717,7 @@ RPC.prototype.getblockcount = function getblockcount(args) { return Promise.resolve(this.chain.tip.height); }; -RPC.prototype.getblock = spawn.co(function* getblock(args) { +RPC.prototype.getblock = co(function* getblock(args) { var hash, verbose, entry, block; if (args.help || args.length < 1 || args.length > 2) @@ -819,7 +820,7 @@ RPC.prototype._scriptToJSON = function scriptToJSON(script, hex) { return out; }; -RPC.prototype.getblockhash = spawn.co(function* getblockhash(args) { +RPC.prototype.getblockhash = co(function* getblockhash(args) { var height, entry; if (args.help || args.length !== 1) @@ -838,7 +839,7 @@ RPC.prototype.getblockhash = spawn.co(function* getblockhash(args) { return entry.rhash; }); -RPC.prototype.getblockheader = spawn.co(function* getblockheader(args) { +RPC.prototype.getblockheader = co(function* getblockheader(args) { var hash, verbose, entry; if (args.help || args.length < 1 || args.length > 2) @@ -865,7 +866,7 @@ RPC.prototype.getblockheader = spawn.co(function* getblockheader(args) { return yield this._headerToJSON(entry); }); -RPC.prototype._headerToJSON = spawn.co(function* _headerToJSON(entry) { +RPC.prototype._headerToJSON = co(function* _headerToJSON(entry) { var medianTime = yield entry.getMedianTimeAsync(); var nextHash = yield this.chain.db.getNextHash(entry.hash); @@ -887,7 +888,7 @@ RPC.prototype._headerToJSON = spawn.co(function* _headerToJSON(entry) { }; }); -RPC.prototype._blockToJSON = spawn.co(function* _blockToJSON(entry, block, txDetails) { +RPC.prototype._blockToJSON = co(function* _blockToJSON(entry, block, txDetails) { var self = this; var medianTime = yield entry.getMedianTimeAsync(); var nextHash = yield this.chain.db.getNextHash(entry.hash); @@ -918,7 +919,7 @@ RPC.prototype._blockToJSON = spawn.co(function* _blockToJSON(entry, block, txDet }; }); -RPC.prototype.getchaintips = spawn.co(function* getchaintips(args) { +RPC.prototype.getchaintips = co(function* getchaintips(args) { var i, tips, orphans, prevs, result; var orphan, entries, entry, main, fork; @@ -964,7 +965,7 @@ RPC.prototype.getchaintips = spawn.co(function* getchaintips(args) { return result; }); -RPC.prototype._findFork = spawn.co(function* _findFork(entry) { +RPC.prototype._findFork = co(function* _findFork(entry) { while (entry) { if (yield entry.isMainChain()) return entry; @@ -1147,7 +1148,7 @@ RPC.prototype._entryToJSON = function _entryToJSON(entry) { }; }; -RPC.prototype.gettxout = spawn.co(function* gettxout(args) { +RPC.prototype.gettxout = co(function* gettxout(args) { var hash, index, mempool, coin; if (args.help || args.length < 2 || args.length > 3) @@ -1187,7 +1188,7 @@ RPC.prototype.gettxout = spawn.co(function* gettxout(args) { }; }); -RPC.prototype.gettxoutproof = spawn.co(function* gettxoutproof(args) { +RPC.prototype.gettxoutproof = co(function* gettxoutproof(args) { var uniq = {}; var i, txids, block, hash, last, tx, coins; @@ -1253,7 +1254,7 @@ RPC.prototype.gettxoutproof = spawn.co(function* gettxoutproof(args) { return block.toRaw().toString('hex'); }); -RPC.prototype.verifytxoutproof = spawn.co(function* verifytxoutproof(args) { +RPC.prototype.verifytxoutproof = co(function* verifytxoutproof(args) { var res = []; var i, block, hash, entry; @@ -1313,7 +1314,7 @@ RPC.prototype.verifychain = function verifychain(args) { * Mining */ -RPC.prototype._submitwork = spawn.co(function* _submitwork(data) { +RPC.prototype._submitwork = co(function* _submitwork(data) { var attempt = this.attempt; var block, header, cb, cur; @@ -1370,7 +1371,7 @@ RPC.prototype._submitwork = spawn.co(function* _submitwork(data) { return true; }); -RPC.prototype._getwork = spawn.co(function* _getwork() { +RPC.prototype._getwork = co(function* _getwork() { var attempt = yield this._getAttempt(true); var data, abbr; @@ -1401,7 +1402,7 @@ RPC.prototype.getworklp = function getworklp(args) { }); }; -RPC.prototype.getwork = spawn.co(function* getwork(args) { +RPC.prototype.getwork = co(function* getwork(args) { var unlock = yield this.locker.lock(); var data, result; @@ -1439,7 +1440,7 @@ RPC.prototype.getwork = spawn.co(function* getwork(args) { return result; }); -RPC.prototype.submitblock = spawn.co(function* submitblock(args) { +RPC.prototype.submitblock = co(function* submitblock(args) { var unlock = yield this.locker.lock(); var block; @@ -1453,7 +1454,7 @@ RPC.prototype.submitblock = spawn.co(function* submitblock(args) { return yield this._submitblock(block); }); -RPC.prototype._submitblock = spawn.co(function* submitblock(block) { +RPC.prototype._submitblock = co(function* submitblock(block) { if (block.prevBlock !== this.chain.tip.hash) return 'rejected: inconclusive-not-best-prevblk'; @@ -1468,7 +1469,7 @@ RPC.prototype._submitblock = spawn.co(function* submitblock(block) { return null; }); -RPC.prototype.getblocktemplate = spawn.co(function* getblocktemplate(args) { +RPC.prototype.getblocktemplate = co(function* getblocktemplate(args) { var mode = 'template'; var version = -1; var coinbase = true; @@ -1537,7 +1538,7 @@ RPC.prototype.getblocktemplate = spawn.co(function* getblocktemplate(args) { return yield this._tmpl(version, coinbase, rules); }); -RPC.prototype._tmpl = spawn.co(function* _tmpl(version, coinbase, rules) { +RPC.prototype._tmpl = co(function* _tmpl(version, coinbase, rules) { var unlock = yield this.locker.lock(); var txs = []; var txIndex = {}; @@ -1745,7 +1746,7 @@ RPC.prototype._bindChain = function _bindChain() { }); }; -RPC.prototype._getAttempt = spawn.co(function* _getAttempt(update) { +RPC.prototype._getAttempt = co(function* _getAttempt(update) { var attempt = this.attempt; this._bindChain(); @@ -1771,7 +1772,7 @@ RPC.prototype._totalTX = function _totalTX() { return this.mempool ? this.mempool.totalTX : 0; }; -RPC.prototype.getmininginfo = spawn.co(function* getmininginfo(args) { +RPC.prototype.getmininginfo = co(function* getmininginfo(args) { var block, hashps; if (args.help || args.length !== 0) @@ -1853,7 +1854,7 @@ RPC.prototype.prioritisetransaction = function prioritisetransaction(args) { return Promise.resolve(true); }; -RPC.prototype._hashps = spawn.co(function* _hashps(lookup, height) { +RPC.prototype._hashps = co(function* _hashps(lookup, height) { var i, minTime, maxTime, pb0, time; var workDiff, timeDiff, ps, pb, entry; @@ -1921,7 +1922,7 @@ RPC.prototype.setgenerate = function setgenerate(args) { return Promise.resolve(this.mining); }; -RPC.prototype.generate = spawn.co(function* generate(args) { +RPC.prototype.generate = co(function* generate(args) { var unlock = yield this.locker.lock(); var numblocks; @@ -1935,7 +1936,7 @@ RPC.prototype.generate = spawn.co(function* generate(args) { return yield this._generate(numblocks); }); -RPC.prototype._generate = spawn.co(function* _generate(numblocks) { +RPC.prototype._generate = co(function* _generate(numblocks) { var hashes = []; var i, block; @@ -1948,7 +1949,7 @@ RPC.prototype._generate = spawn.co(function* _generate(numblocks) { return hashes; }); -RPC.prototype.generatetoaddress = spawn.co(function* generatetoaddress(args) { +RPC.prototype.generatetoaddress = co(function* generatetoaddress(args) { var unlock = yield this.locker.lock(); var numblocks, address, hashes; @@ -2105,7 +2106,7 @@ RPC.prototype.decodescript = function decodescript(args) { return Promise.resolve(script); }; -RPC.prototype.getrawtransaction = spawn.co(function* getrawtransaction(args) { +RPC.prototype.getrawtransaction = co(function* getrawtransaction(args) { var hash, verbose, json, tx; if (args.help || args.length < 1 || args.length > 2) @@ -2153,7 +2154,7 @@ RPC.prototype.sendrawtransaction = function sendrawtransaction(args) { return tx.rhash; }; -RPC.prototype.signrawtransaction = spawn.co(function* signrawtransaction(args) { +RPC.prototype.signrawtransaction = co(function* signrawtransaction(args) { var raw, p, txs, merged; if (args.help || args.length < 1 || args.length > 4) { @@ -2189,7 +2190,7 @@ RPC.prototype._fillCoins = function _fillCoins(tx) { return this.node.fillCoins(tx); }; -RPC.prototype._signrawtransaction = spawn.co(function* signrawtransaction(merged, txs, args) { +RPC.prototype._signrawtransaction = co(function* signrawtransaction(merged, txs, args) { var keys = []; var keyMap = {}; var k, i, secret, key; @@ -2298,7 +2299,7 @@ RPC.prototype._signrawtransaction = spawn.co(function* signrawtransaction(merged }; }); -RPC.prototype.fundrawtransaction = spawn.co(function* fundrawtransaction(args) { +RPC.prototype.fundrawtransaction = co(function* fundrawtransaction(args) { var tx, options, changeAddress, feeRate; if (args.help || args.length < 1 || args.length > 2) @@ -2336,7 +2337,7 @@ RPC.prototype.fundrawtransaction = spawn.co(function* fundrawtransaction(args) { }; }); -RPC.prototype._createRedeem = spawn.co(function* _createRedeem(args) { +RPC.prototype._createRedeem = co(function* _createRedeem(args) { var i, m, n, keys, hash, script, key, ring; if (!utils.isNumber(args[0]) @@ -2387,7 +2388,7 @@ RPC.prototype._createRedeem = spawn.co(function* _createRedeem(args) { }); /* - * Utility spawn.co(function* s + * Utility co(function* s */ RPC.prototype.createmultisig = function createmultisig(args) { @@ -2437,7 +2438,7 @@ RPC.prototype.createwitnessaddress = function createwitnessaddress(args) { }); }; -RPC.prototype.validateaddress = spawn.co(function* validateaddress(args) { +RPC.prototype.validateaddress = co(function* validateaddress(args) { var b58, address, json, path; if (args.help || args.length !== 1) @@ -2670,7 +2671,7 @@ RPC.prototype.setmocktime = function setmocktime(args) { * Wallet */ -RPC.prototype.resendwallettransactions = spawn.co(function* resendwallettransactions(args) { +RPC.prototype.resendwallettransactions = co(function* resendwallettransactions(args) { var hashes = []; var i, tx, txs; @@ -2703,7 +2704,7 @@ RPC.prototype.addwitnessaddress = function addwitnessaddress(args) { Promise.reject(new Error('Not implemented.')); }; -RPC.prototype.backupwallet = spawn.co(function* backupwallet(args) { +RPC.prototype.backupwallet = co(function* backupwallet(args) { var dest; if (args.help || args.length !== 1) @@ -2715,7 +2716,7 @@ RPC.prototype.backupwallet = spawn.co(function* backupwallet(args) { return null; }); -RPC.prototype.dumpprivkey = spawn.co(function* dumpprivkey(args) { +RPC.prototype.dumpprivkey = co(function* dumpprivkey(args) { var hash, ring; if (args.help || args.length !== 1) @@ -2737,7 +2738,7 @@ RPC.prototype.dumpprivkey = spawn.co(function* dumpprivkey(args) { return ring.toSecret(); }); -RPC.prototype.dumpwallet = spawn.co(function* dumpwallet(args) { +RPC.prototype.dumpwallet = co(function* dumpwallet(args) { var i, file, time, address, fmt, str, out, hash, hashes, ring; if (args.help || args.length !== 1) @@ -2795,7 +2796,7 @@ RPC.prototype.dumpwallet = spawn.co(function* dumpwallet(args) { return out; }); -RPC.prototype.encryptwallet = spawn.co(function* encryptwallet(args) { +RPC.prototype.encryptwallet = co(function* encryptwallet(args) { var passphrase; if (!this.wallet.master.encrypted && (args.help || args.help !== 1)) @@ -2814,7 +2815,7 @@ RPC.prototype.encryptwallet = spawn.co(function* encryptwallet(args) { return 'wallet encrypted; we do not need to stop!'; }); -RPC.prototype.getaccountaddress = spawn.co(function* getaccountaddress(args) { +RPC.prototype.getaccountaddress = co(function* getaccountaddress(args) { var account; if (args.help || args.length !== 1) @@ -2833,7 +2834,7 @@ RPC.prototype.getaccountaddress = spawn.co(function* getaccountaddress(args) { return account.receiveAddress.getAddress('base58'); }); -RPC.prototype.getaccount = spawn.co(function* getaccount(args) { +RPC.prototype.getaccount = co(function* getaccount(args) { var hash, path; if (args.help || args.length !== 1) @@ -2852,7 +2853,7 @@ RPC.prototype.getaccount = spawn.co(function* getaccount(args) { return path.name; }); -RPC.prototype.getaddressesbyaccount = spawn.co(function* getaddressesbyaccount(args) { +RPC.prototype.getaddressesbyaccount = co(function* getaddressesbyaccount(args) { var i, path, account, addrs, paths; if (args.help || args.length !== 1) @@ -2875,7 +2876,7 @@ RPC.prototype.getaddressesbyaccount = spawn.co(function* getaddressesbyaccount(a return addrs; }); -RPC.prototype.getbalance = spawn.co(function* getbalance(args) { +RPC.prototype.getbalance = co(function* getbalance(args) { var minconf = 0; var account, value, balance; @@ -2903,7 +2904,7 @@ RPC.prototype.getbalance = spawn.co(function* getbalance(args) { return +utils.btc(value); }); -RPC.prototype.getnewaddress = spawn.co(function* getnewaddress(args) { +RPC.prototype.getnewaddress = co(function* getnewaddress(args) { var account, address; if (args.help || args.length > 1) @@ -2920,7 +2921,7 @@ RPC.prototype.getnewaddress = spawn.co(function* getnewaddress(args) { return address.getAddress('base58'); }); -RPC.prototype.getrawchangeaddress = spawn.co(function* getrawchangeaddress(args) { +RPC.prototype.getrawchangeaddress = co(function* getrawchangeaddress(args) { var address; if (args.help || args.length > 1) @@ -2931,7 +2932,7 @@ RPC.prototype.getrawchangeaddress = spawn.co(function* getrawchangeaddress(args) return address.getAddress('base58'); }); -RPC.prototype.getreceivedbyaccount = spawn.co(function* getreceivedbyaccount(args) { +RPC.prototype.getreceivedbyaccount = co(function* getreceivedbyaccount(args) { var minconf = 0; var total = 0; var filter = {}; @@ -2984,7 +2985,7 @@ RPC.prototype.getreceivedbyaccount = spawn.co(function* getreceivedbyaccount(arg return +utils.btc(total); }); -RPC.prototype.getreceivedbyaddress = spawn.co(function* getreceivedbyaddress(args) { +RPC.prototype.getreceivedbyaddress = co(function* getreceivedbyaddress(args) { var self = this; var minconf = 0; var total = 0; @@ -3021,7 +3022,7 @@ RPC.prototype.getreceivedbyaddress = spawn.co(function* getreceivedbyaddress(arg return +utils.btc(total); }); -RPC.prototype._toWalletTX = spawn.co(function* _toWalletTX(tx) { +RPC.prototype._toWalletTX = co(function* _toWalletTX(tx) { var i, det, receive, member, sent, received, json, details; details = yield this.wallet.toDetails(tx); @@ -3098,7 +3099,7 @@ RPC.prototype._toWalletTX = spawn.co(function* _toWalletTX(tx) { return json; }); -RPC.prototype.gettransaction = spawn.co(function* gettransaction(args) { +RPC.prototype.gettransaction = co(function* gettransaction(args) { var hash, tx; if (args.help || args.length < 1 || args.length > 2) @@ -3117,7 +3118,7 @@ RPC.prototype.gettransaction = spawn.co(function* gettransaction(args) { return yield this._toWalletTX(tx); }); -RPC.prototype.abandontransaction = spawn.co(function* abandontransaction(args) { +RPC.prototype.abandontransaction = co(function* abandontransaction(args) { var hash, result; if (args.help || args.length !== 1) @@ -3136,7 +3137,7 @@ RPC.prototype.abandontransaction = spawn.co(function* abandontransaction(args) { return null; }); -RPC.prototype.getunconfirmedbalance = spawn.co(function* getunconfirmedbalance(args) { +RPC.prototype.getunconfirmedbalance = co(function* getunconfirmedbalance(args) { var balance; if (args.help || args.length > 0) @@ -3147,7 +3148,7 @@ RPC.prototype.getunconfirmedbalance = spawn.co(function* getunconfirmedbalance(a return +utils.btc(balance.unconfirmed); }); -RPC.prototype.getwalletinfo = spawn.co(function* getwalletinfo(args) { +RPC.prototype.getwalletinfo = co(function* getwalletinfo(args) { var balance, hashes; if (args.help || args.length !== 0) @@ -3171,7 +3172,7 @@ RPC.prototype.getwalletinfo = spawn.co(function* getwalletinfo(args) { }; }); -RPC.prototype.importprivkey = spawn.co(function* importprivkey(args) { +RPC.prototype.importprivkey = co(function* importprivkey(args) { var secret, label, rescan, key; if (args.help || args.length < 1 || args.length > 3) @@ -3200,7 +3201,7 @@ RPC.prototype.importprivkey = spawn.co(function* importprivkey(args) { return null; }); -RPC.prototype.importwallet = spawn.co(function* importwallet(args) { +RPC.prototype.importwallet = co(function* importwallet(args) { var file, keys, lines, line, parts; var i, secret, time, label, addr; var data, key; @@ -3260,7 +3261,7 @@ RPC.prototype.importaddress = function importaddress(args) { return Promise.reject(new Error('Not implemented.')); }; -RPC.prototype.importpubkey = spawn.co(function* importpubkey(args) { +RPC.prototype.importpubkey = co(function* importpubkey(args) { var pubkey, label, rescan, key; if (args.help || args.length < 1 || args.length > 4) @@ -3300,7 +3301,7 @@ RPC.prototype.keypoolrefill = function keypoolrefill(args) { return Promise.resolve(null); }; -RPC.prototype.listaccounts = spawn.co(function* listaccounts(args) { +RPC.prototype.listaccounts = co(function* listaccounts(args) { var i, map, accounts, account, balance; if (args.help || args.length > 2) @@ -3380,7 +3381,7 @@ RPC.prototype.listreceivedbyaddress = function listreceivedbyaddress(args) { return this._listReceived(minconf, includeEmpty, false); }; -RPC.prototype._listReceived = spawn.co(function* _listReceived(minconf, empty, account) { +RPC.prototype._listReceived = co(function* _listReceived(minconf, empty, account) { var out = []; var result = []; var map = {}; @@ -3472,7 +3473,7 @@ RPC.prototype._listReceived = spawn.co(function* _listReceived(minconf, empty, a return result; }); -RPC.prototype.listsinceblock = spawn.co(function* listsinceblock(args) { +RPC.prototype.listsinceblock = co(function* listsinceblock(args) { var block, conf, out, highest; var i, height, txs, tx, json; @@ -3526,7 +3527,7 @@ RPC.prototype.listsinceblock = spawn.co(function* listsinceblock(args) { }; }); -RPC.prototype._toListTX = spawn.co(function* _toListTX(tx) { +RPC.prototype._toListTX = co(function* _toListTX(tx) { var i, receive, member, det, sent, received, index; var sendMember, recMember, sendIndex, recIndex, json; var details; @@ -3604,7 +3605,7 @@ RPC.prototype._toListTX = spawn.co(function* _toListTX(tx) { return json; }); -RPC.prototype.listtransactions = spawn.co(function* listtransactions(args) { +RPC.prototype.listtransactions = co(function* listtransactions(args) { var i, account, count, txs, tx, json; if (args.help || args.length > 4) { @@ -3639,7 +3640,7 @@ RPC.prototype.listtransactions = spawn.co(function* listtransactions(args) { return txs; }); -RPC.prototype.listunspent = spawn.co(function* listunspent(args) { +RPC.prototype.listunspent = co(function* listunspent(args) { var minDepth = 1; var maxDepth = 9999999; var out = []; @@ -3771,7 +3772,7 @@ RPC.prototype.move = function move(args) { Promise.reject(new Error('Not implemented.')); }; -RPC.prototype._send = spawn.co(function* _send(account, address, amount, subtractFee) { +RPC.prototype._send = co(function* _send(account, address, amount, subtractFee) { var tx, options; options = { @@ -3808,7 +3809,7 @@ RPC.prototype.sendfrom = function sendfrom(args) { return this._send(account, address, amount, false); }; -RPC.prototype.sendmany = spawn.co(function* sendmany(args) { +RPC.prototype.sendmany = co(function* sendmany(args) { var account, sendTo, minDepth, comment, subtractFee; var i, outputs, keys, uniq, tx; var key, value, address, hash, output, options; @@ -3906,7 +3907,7 @@ RPC.prototype.settxfee = function settxfee(args) { return Promise.resolve(true); }; -RPC.prototype.signmessage = spawn.co(function* signmessage(args) { +RPC.prototype.signmessage = co(function* signmessage(args) { var address, msg, sig, ring; if (args.help || args.length !== 2) @@ -3948,7 +3949,7 @@ RPC.prototype.walletlock = function walletlock(args) { return null; }; -RPC.prototype.walletpassphrasechange = spawn.co(function* walletpassphrasechange(args) { +RPC.prototype.walletpassphrasechange = co(function* walletpassphrasechange(args) { var old, new_; if (args.help || (this.wallet.master.encrypted && args.length !== 2)) { @@ -3970,7 +3971,7 @@ RPC.prototype.walletpassphrasechange = spawn.co(function* walletpassphrasechange return null; }); -RPC.prototype.walletpassphrase = spawn.co(function* walletpassphrase(args) { +RPC.prototype.walletpassphrase = co(function* walletpassphrase(args) { var passphrase, timeout; if (args.help || (this.wallet.master.encrypted && args.length !== 2)) @@ -3993,7 +3994,7 @@ RPC.prototype.walletpassphrase = spawn.co(function* walletpassphrase(args) { return null; }); -RPC.prototype.importprunedfunds = spawn.co(function* importprunedfunds(args) { +RPC.prototype.importprunedfunds = co(function* importprunedfunds(args) { var tx, block, label, height, added; if (args.help || args.length < 2 || args.length > 3) { @@ -4037,7 +4038,7 @@ RPC.prototype.importprunedfunds = spawn.co(function* importprunedfunds(args) { return null; }); -RPC.prototype.removeprunedfunds = spawn.co(function* removeprunedfunds(args) { +RPC.prototype.removeprunedfunds = co(function* removeprunedfunds(args) { var hash, removed; if (args.help || args.length !== 1) diff --git a/lib/http/rpcclient.js b/lib/http/rpcclient.js index a0d90e02..e2782dd2 100644 --- a/lib/http/rpcclient.js +++ b/lib/http/rpcclient.js @@ -9,6 +9,7 @@ var Network = require('../protocol/network'); var request = require('./request'); var spawn = require('../utils/spawn'); +var co = spawn.co; /** * BCoin RPC client. @@ -44,7 +45,7 @@ function RPCClient(options) { * @param {Function} callback - Returns [Error, Object?]. */ -RPCClient.prototype.call = spawn.co(function* call(method, params) { +RPCClient.prototype.call = co(function* call(method, params) { var res = yield request.promise({ method: 'POST', uri: this.uri, diff --git a/lib/http/server.js b/lib/http/server.js index 80e0150b..efcfa9b7 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -16,6 +16,7 @@ var http = require('./'); var HTTPBase = http.base; var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var crypto = require('../crypto/crypto'); var assert = utils.assert; var RPC; /*= require('./rpc'); - load lazily */ @@ -1062,7 +1063,7 @@ HTTPServer.prototype._initIO = function _initIO() { * @param {Function} callback */ -HTTPServer.prototype.open = spawn.co(function* open() { +HTTPServer.prototype.open = co(function* open() { yield this.server.open(); this.logger.info('HTTP server loaded.'); diff --git a/lib/http/wallet.js b/lib/http/wallet.js index 3292cb6b..d0d1c58f 100644 --- a/lib/http/wallet.js +++ b/lib/http/wallet.js @@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter; var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var Client = require('./client'); /** @@ -89,7 +90,7 @@ HTTPWallet.prototype._init = function _init() { * @param {Function} callback */ -HTTPWallet.prototype.open = spawn.co(function* open(options) { +HTTPWallet.prototype.open = co(function* open(options) { var wallet; this.id = options.id; @@ -116,7 +117,7 @@ HTTPWallet.prototype.open = spawn.co(function* open(options) { * @param {Function} callback */ -HTTPWallet.prototype.create = spawn.co(function* create(options) { +HTTPWallet.prototype.create = co(function* create(options) { var wallet; yield this.client.open(); wallet = yield this.client.createWallet(options); @@ -292,7 +293,7 @@ HTTPWallet.prototype.setPassphrase = function setPassphrase(old, new_) { * @see Wallet#retoken */ -HTTPWallet.prototype.retoken = spawn.co(function* retoken(passphrase) { +HTTPWallet.prototype.retoken = co(function* retoken(passphrase) { var token = yield this.client.retoken(this.id, passphrase); this.token = token; diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 62280f74..689de518 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -12,6 +12,7 @@ var AsyncObject = require('../utils/async'); var constants = bcoin.constants; var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; var BufferWriter = require('../utils/writer'); var BufferReader = require('../utils/reader'); @@ -119,7 +120,7 @@ utils.inherits(Mempool, AsyncObject); * @param {Function} callback */ -Mempool.prototype._open = spawn.co(function* open() { +Mempool.prototype._open = co(function* open() { var size = (this.maxSize / 1024).toFixed(2); yield this.chain.open(); this.logger.info('Mempool loaded (maxsize=%dkb).', size); @@ -153,7 +154,7 @@ Mempool.prototype._lock = function _lock(tx, force) { * @param {Function} callback */ -Mempool.prototype.addBlock = spawn.co(function* addBlock(block) { +Mempool.prototype.addBlock = co(function* addBlock(block) { var unlock = yield this._lock(); var entries = []; var i, entry, tx, hash; @@ -199,7 +200,7 @@ Mempool.prototype.addBlock = spawn.co(function* addBlock(block) { * @param {Function} callback */ -Mempool.prototype.removeBlock = spawn.co(function* removeBlock(block) { +Mempool.prototype.removeBlock = co(function* removeBlock(block) { var unlock = yield this.lock(); var i, entry, tx, hash; @@ -524,7 +525,7 @@ Mempool.prototype.hasReject = function hasReject(hash) { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Mempool.prototype.addTX = spawn.co(function* addTX(tx) { +Mempool.prototype.addTX = co(function* addTX(tx) { var unlock = yield this._lock(tx); var missing; @@ -550,7 +551,7 @@ Mempool.prototype.addTX = spawn.co(function* addTX(tx) { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Mempool.prototype._addTX = spawn.co(function* _addTX(tx) { +Mempool.prototype._addTX = co(function* _addTX(tx) { var lockFlags = constants.flags.STANDARD_LOCKTIME_FLAGS; var hash = tx.hash('hex'); var ret, entry, missing; @@ -670,7 +671,7 @@ Mempool.prototype._addTX = spawn.co(function* _addTX(tx) { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Mempool.prototype.addUnchecked = spawn.co(function* addUnchecked(entry, force) { +Mempool.prototype.addUnchecked = co(function* addUnchecked(entry, force) { var unlock = yield this._lock(null, force); var i, resolved, tx, orphan; @@ -804,7 +805,7 @@ Mempool.prototype.getMinRate = function getMinRate() { * @param {Function} callback - Returns [{@link VerifyError}]. */ -Mempool.prototype.verify = spawn.co(function* verify(entry) { +Mempool.prototype.verify = co(function* verify(entry) { var height = this.chain.height + 1; var lockFlags = flags.STANDARD_LOCKTIME_FLAGS; var flags1 = flags.STANDARD_VERIFY_FLAGS; @@ -960,7 +961,7 @@ Mempool.prototype.verify = spawn.co(function* verify(entry) { * @param {Function} callback */ -Mempool.prototype.checkResult = spawn.co(function* checkResult(tx, flags) { +Mempool.prototype.checkResult = co(function* checkResult(tx, flags) { try { yield this.checkInputs(tx, flags); } catch (err) { @@ -979,7 +980,7 @@ Mempool.prototype.checkResult = spawn.co(function* checkResult(tx, flags) { * @param {Function} callback */ -Mempool.prototype.checkInputs = spawn.co(function* checkInputs(tx, flags) { +Mempool.prototype.checkInputs = co(function* checkInputs(tx, flags) { var result = yield tx.verifyAsync(flags); if (result) return; @@ -1387,7 +1388,7 @@ Mempool.prototype.fillAllHistory = function fillAllHistory(tx) { * @param {Function} callback - Returns [Error, {@link TX}]. */ -Mempool.prototype.fillAllCoins = spawn.co(function* fillAllCoins(tx) { +Mempool.prototype.fillAllCoins = co(function* fillAllCoins(tx) { var i, input, hash, index, coin; this.fillCoins(tx); @@ -1463,7 +1464,7 @@ Mempool.prototype.isDoubleSpend = function isDoubleSpend(tx) { * @param {Function} callback - Returns [Error, Number]. */ -Mempool.prototype.getConfidence = spawn.co(function* getConfidence(hash) { +Mempool.prototype.getConfidence = co(function* getConfidence(hash) { var tx, result; if (hash instanceof bcoin.tx) { diff --git a/lib/miner/miner.js b/lib/miner/miner.js index 6b713ead..e65251ac 100644 --- a/lib/miner/miner.js +++ b/lib/miner/miner.js @@ -10,6 +10,7 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; var AsyncObject = require('../utils/async'); var MinerBlock = require('./minerblock'); @@ -134,7 +135,7 @@ Miner.prototype._init = function _init() { * @param {Function} callback */ -Miner.prototype._open = spawn.co(function* open() { +Miner.prototype._open = co(function* open() { if (this.mempool) yield this.mempool.open(); else @@ -240,7 +241,7 @@ Miner.prototype.stop = function stop() { * @param {Function} callback - Returns [Error, {@link MinerBlock}]. */ -Miner.prototype.createBlock = spawn.co(function* createBlock(tip) { +Miner.prototype.createBlock = co(function* createBlock(tip) { var i, ts, attempt, txs, tx, target, version; if (!this.loaded) @@ -294,7 +295,7 @@ Miner.prototype.createBlock = spawn.co(function* createBlock(tip) { * @param {Function} callback - Returns [Error, [{@link Block}]]. */ -Miner.prototype.mineBlock = spawn.co(function* mineBlock(tip) { +Miner.prototype.mineBlock = co(function* mineBlock(tip) { // Create a new block and start hashing var attempt = yield this.createBlock(tip); return yield attempt.mineAsync(); diff --git a/lib/miner/minerblock.js b/lib/miner/minerblock.js index 04388551..2a0ca6c4 100644 --- a/lib/miner/minerblock.js +++ b/lib/miner/minerblock.js @@ -10,6 +10,7 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var crypto = require('../crypto/crypto'); var assert = utils.assert; var constants = bcoin.constants; @@ -349,7 +350,7 @@ MinerBlock.prototype.sendStatus = function sendStatus() { * @param {Function} callback - Returns [Error, {@link Block}]. */ -MinerBlock.prototype.mine = spawn.co(function* mine() { +MinerBlock.prototype.mine = co(function* mine() { yield this.wait(100); // Try to find a block: do one iteration of extraNonce @@ -391,7 +392,7 @@ MinerBlock.prototype.mineSync = function mineSync() { * @param {Function} callback - Returns [Error, {@link Block}]. */ -MinerBlock.prototype.mineAsync = spawn.co(function* mineAsync() { +MinerBlock.prototype.mineAsync = co(function* mineAsync() { var block; if (!this.workerPool) diff --git a/lib/net/peer.js b/lib/net/peer.js index 2e49f5a2..9e370230 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -11,6 +11,7 @@ var bcoin = require('../env'); var EventEmitter = require('events').EventEmitter; var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var Parser = require('./parser'); var Framer = require('./framer'); var packets = require('./packets'); @@ -1430,7 +1431,7 @@ Peer.prototype._handleMempool = function _handleMempool(packet) { * [Error, {@link Block}|{@link MempoolEntry}]. */ -Peer.prototype._getItem = spawn.co(function* _getItem(item) { +Peer.prototype._getItem = co(function* _getItem(item) { var entry = this.pool.invMap[item.hash]; if (entry) { @@ -2349,7 +2350,7 @@ Peer.prototype.reject = function reject(obj, code, reason, score) { * @param {Function} callback */ -Peer.prototype.resolveOrphan = spawn.co(function* resolveOrphan(tip, orphan) { +Peer.prototype.resolveOrphan = co(function* resolveOrphan(tip, orphan) { var root, locator; assert(orphan); @@ -2373,7 +2374,7 @@ Peer.prototype.resolveOrphan = spawn.co(function* resolveOrphan(tip, orphan) { * @param {Function} callback */ -Peer.prototype.getHeaders = spawn.co(function* getHeaders(tip, stop) { +Peer.prototype.getHeaders = co(function* getHeaders(tip, stop) { var locator = yield this.chain.getLocator(tip); this.sendGetHeaders(locator, stop); }); @@ -2385,7 +2386,7 @@ Peer.prototype.getHeaders = spawn.co(function* getHeaders(tip, stop) { * @param {Function} callback */ -Peer.prototype.getBlocks = spawn.co(function* getBlocks(tip, stop) { +Peer.prototype.getBlocks = co(function* getBlocks(tip, stop) { var locator = yield this.chain.getLocator(tip); this.sendGetBlocks(locator, stop); }); diff --git a/lib/net/pool.js b/lib/net/pool.js index e056bc84..f78a1315 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -13,6 +13,7 @@ var EventEmitter = require('events').EventEmitter; var utils = require('../utils/utils'); var IP = require('../utils/ip'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; var constants = bcoin.constants; var VerifyError = bcoin.errors.VerifyError; @@ -290,7 +291,7 @@ Pool.prototype._lock = function _lock(force) { * @param {Function} callback */ -Pool.prototype._open = spawn.co(function* _open() { +Pool.prototype._open = co(function* _open() { var ip, key; try { @@ -329,7 +330,7 @@ Pool.prototype._open = spawn.co(function* _open() { * @param {Function} callback */ -Pool.prototype._close = spawn.co(function* close() { +Pool.prototype._close = co(function* close() { var i, items, hashes, hash; this.stopSync(); @@ -720,7 +721,7 @@ Pool.prototype.stopSync = function stopSync() { * @param {Function} callback */ -Pool.prototype._handleHeaders = spawn.co(function* _handleHeaders(headers, peer) { +Pool.prototype._handleHeaders = co(function* _handleHeaders(headers, peer) { var i, unlock, ret, header, hash, last; if (!this.options.headers) @@ -789,7 +790,7 @@ Pool.prototype._handleHeaders = spawn.co(function* _handleHeaders(headers, peer) * @param {Function} callback */ -Pool.prototype._handleBlocks = spawn.co(function* _handleBlocks(hashes, peer) { +Pool.prototype._handleBlocks = co(function* _handleBlocks(hashes, peer) { var i, hash, exists; assert(!this.options.headers); @@ -857,7 +858,7 @@ Pool.prototype._handleBlocks = spawn.co(function* _handleBlocks(hashes, peer) { * @param {Function} callback */ -Pool.prototype._handleInv = spawn.co(function* _handleInv(hashes, peer) { +Pool.prototype._handleInv = co(function* _handleInv(hashes, peer) { var unlock = yield this._lock(); var i, hash; @@ -888,7 +889,7 @@ Pool.prototype._handleInv = spawn.co(function* _handleInv(hashes, peer) { * @param {Function} callback */ -Pool.prototype._handleBlock = spawn.co(function* _handleBlock(block, peer) { +Pool.prototype._handleBlock = co(function* _handleBlock(block, peer) { var requested; // Fulfill the load request. @@ -1292,7 +1293,7 @@ Pool.prototype.hasReject = function hasReject(hash) { * @param {Function} callback */ -Pool.prototype._handleTX = spawn.co(function* _handleTX(tx, peer) { +Pool.prototype._handleTX = co(function* _handleTX(tx, peer) { var i, requested, missing; // Fulfill the load request. @@ -1503,7 +1504,7 @@ Pool.prototype.watchAddress = function watchAddress(address) { * @param {Function} callback */ -Pool.prototype.getData = spawn.co(function* getData(peer, type, hash) { +Pool.prototype.getData = co(function* getData(peer, type, hash) { var self = this; var item, exists; @@ -1565,7 +1566,7 @@ Pool.prototype.getDataSync = function getDataSync(peer, type, hash) { * @param {Function} callback - Returns [Error, Boolean]. */ -Pool.prototype.has = spawn.co(function* has(peer, type, hash) { +Pool.prototype.has = co(function* has(peer, type, hash) { var exists = yield this.exists(type, hash); if (exists) @@ -1855,7 +1856,7 @@ Pool.prototype.isIgnored = function isIgnored(addr) { * @param {Function} callback */ -Pool.prototype.getIP = spawn.co(function* getIP() { +Pool.prototype.getIP = co(function* getIP() { var request, res, ip; if (utils.isBrowser) @@ -1887,7 +1888,7 @@ Pool.prototype.getIP = spawn.co(function* getIP() { * @param {Function} callback */ -Pool.prototype.getIP2 = spawn.co(function* getIP2() { +Pool.prototype.getIP2 = co(function* getIP2() { var request, res, ip; if (utils.isBrowser) diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index d6a21ed6..62d0ab9c 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -11,6 +11,7 @@ var bcoin = require('../env'); var constants = bcoin.constants; var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var Node = bcoin.node; /** @@ -223,7 +224,7 @@ Fullnode.prototype._init = function _init() { * @param {Function} callback */ -Fullnode.prototype._open = spawn.co(function* open() { +Fullnode.prototype._open = co(function* open() { yield this.chain.open(); yield this.mempool.open(); yield this.miner.open(); @@ -251,7 +252,7 @@ Fullnode.prototype._open = spawn.co(function* open() { * @param {Function} callback */ -Fullnode.prototype._close = spawn.co(function* close() { +Fullnode.prototype._close = co(function* close() { this.wallet = null; if (this.http) @@ -305,7 +306,7 @@ Fullnode.prototype.broadcast = function broadcast(item, callback) { * @param {TX} tx */ -Fullnode.prototype.sendTX = spawn.co(function* sendTX(tx) { +Fullnode.prototype.sendTX = co(function* sendTX(tx) { try { yield this.mempool.addTX(tx); } catch (err) { @@ -404,7 +405,7 @@ Fullnode.prototype.getCoin = function getCoin(hash, index) { * @param {Function} callback - Returns [Error, {@link Coin}[]]. */ -Fullnode.prototype.getCoinsByAddress = spawn.co(function* getCoinsByAddress(addresses) { +Fullnode.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses) { var coins = this.mempool.getCoinsByAddress(addresses); var i, blockCoins, coin, spent; @@ -428,7 +429,7 @@ Fullnode.prototype.getCoinsByAddress = spawn.co(function* getCoinsByAddress(addr * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -Fullnode.prototype.getTXByAddress = spawn.co(function* getTXByAddress(addresses) { +Fullnode.prototype.getTXByAddress = co(function* getTXByAddress(addresses) { var mempool = this.mempool.getTXByAddress(addresses); var txs = yield this.chain.db.getTXByAddress(addresses); return mempool.concat(txs); diff --git a/lib/node/node.js b/lib/node/node.js index 7a9ec16b..beb8d49d 100644 --- a/lib/node/node.js +++ b/lib/node/node.js @@ -11,6 +11,7 @@ var bcoin = require('../env'); var AsyncObject = require('../utils/async'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; /** @@ -233,7 +234,7 @@ Node.prototype.location = function location(name) { * @param {Function} callback */ -Node.prototype.openWallet = spawn.co(function* openWallet() { +Node.prototype.openWallet = co(function* openWallet() { var options, wallet; assert(!this.wallet); diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 8d42ee96..f118c083 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -10,6 +10,7 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var Node = bcoin.node; /** @@ -147,7 +148,7 @@ SPVNode.prototype._init = function _init() { * @param {Function} callback */ -SPVNode.prototype._open = spawn.co(function* open(callback) { +SPVNode.prototype._open = co(function* open(callback) { yield this.chain.open(); yield this.pool.open(); yield this.walletdb.open(); @@ -176,7 +177,7 @@ SPVNode.prototype._open = spawn.co(function* open(callback) { * @param {Function} callback */ -SPVNode.prototype._close = spawn.co(function* close() { +SPVNode.prototype._close = co(function* close() { this.wallet = null; if (this.http) yield this.http.close(); @@ -190,7 +191,7 @@ SPVNode.prototype._close = spawn.co(function* close() { * @param {Function} callback */ -SPVNode.prototype.openFilter = spawn.co(function* openFilter() { +SPVNode.prototype.openFilter = co(function* openFilter() { var hashes = yield this.walletdb.getAddressHashes(); var i; diff --git a/lib/utils/async.js b/lib/utils/async.js index 29087ed2..c9a88d39 100644 --- a/lib/utils/async.js +++ b/lib/utils/async.js @@ -8,6 +8,7 @@ var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; var EventEmitter = require('events').EventEmitter; var wait = utils.wait; @@ -53,7 +54,7 @@ AsyncObject.prototype._onClose = function _onClose() { }); }; -AsyncObject.prototype.open = spawn.co(function* open() { +AsyncObject.prototype.open = co(function* open() { var err, unlock; assert(!this.closing, 'Cannot open while closing.'); @@ -100,7 +101,7 @@ AsyncObject.prototype.open = spawn.co(function* open() { * @param {Function} callback */ -AsyncObject.prototype.close = spawn.co(function* close() { +AsyncObject.prototype.close = co(function* close() { var unlock, err; assert(!this.loading, 'Cannot close while loading.'); diff --git a/lib/wallet/account.js b/lib/wallet/account.js index efedd974..754d06a9 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -9,6 +9,7 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = utils.assert; var BufferReader = require('../utils/reader'); var BufferWriter = require('../utils/writer'); @@ -204,7 +205,7 @@ Account.MAX_LOOKAHEAD = 5; * @param {Function} callback */ -Account.prototype.init = spawn.co(function* init() { +Account.prototype.init = co(function* init() { // Waiting for more keys. if (this.keys.length !== this.n - 1) { assert(!this.initialized); @@ -304,7 +305,7 @@ Account.prototype.spliceKey = function spliceKey(key) { * @param {Function} callback */ -Account.prototype.addKey = spawn.co(function* addKey(key) { +Account.prototype.addKey = co(function* addKey(key) { var result = false; var exists; @@ -333,7 +334,7 @@ Account.prototype.addKey = spawn.co(function* addKey(key) { * @param {Function} callback */ -Account.prototype._checkKeys = spawn.co(function* _checkKeys() { +Account.prototype._checkKeys = co(function* _checkKeys() { var ring, hash, paths; if (this.initialized || this.type !== Account.types.MULTISIG) @@ -398,7 +399,7 @@ Account.prototype.createChange = function createChange() { * @param {Function} callback - Returns [Error, {@link KeyRing}]. */ -Account.prototype.createAddress = spawn.co(function* createAddress(change) { +Account.prototype.createAddress = co(function* createAddress(change) { var ring, lookahead; if (change) { @@ -560,7 +561,7 @@ Account.prototype.saveAddress = function saveAddress(rings) { * @param {Function} callback - Returns [Error, {@link KeyRing}, {@link KeyRing}]. */ -Account.prototype.setDepth = spawn.co(function* setDepth(receiveDepth, changeDepth) { +Account.prototype.setDepth = co(function* setDepth(receiveDepth, changeDepth) { var rings = []; var i, receive, change; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index e4e036dc..1a437f37 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -10,6 +10,7 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var assert = bcoin.utils.assert; var constants = bcoin.constants; var DUMMY = new Buffer([0]); @@ -229,7 +230,7 @@ TXDB.layout = layout; * @param {Function} callback */ -TXDB.prototype.open = spawn.co(function* open() { +TXDB.prototype.open = co(function* open() { this.balance = yield this.getBalance(); this.logger.info('TXDB loaded for %s.', this.wallet.id); this.logger.info( @@ -372,7 +373,7 @@ TXDB.prototype.iterate = function iterate(options) { * @param {Function} callback */ -TXDB.prototype.commit = spawn.co(function* commit() { +TXDB.prototype.commit = co(function* commit() { assert(this.current); try { yield this.current.write(); @@ -402,7 +403,7 @@ TXDB.prototype.getInfo = function getInfo(tx) { * @param {Function} callback - Returns [Error, Buffer]. */ -TXDB.prototype._addOrphan = spawn.co(function* _addOrphan(prevout, spender) { +TXDB.prototype._addOrphan = co(function* _addOrphan(prevout, spender) { var p = new BufferWriter(); var key = layout.o(prevout.hash, prevout.index); var data = yield this.get(key); @@ -423,7 +424,7 @@ TXDB.prototype._addOrphan = spawn.co(function* _addOrphan(prevout, spender) { * @param {Function} callback - Returns [Error, {@link Orphan}]. */ -TXDB.prototype._getOrphans = spawn.co(function* _getOrphans(hash, index) { +TXDB.prototype._getOrphans = co(function* _getOrphans(hash, index) { var items = []; var i, orphans, orphan, tx; @@ -458,7 +459,7 @@ TXDB.prototype._getOrphans = spawn.co(function* _getOrphans(hash, index) { * @param {Function} callback - Returns [Error]. */ -TXDB.prototype._verify = spawn.co(function* _verify(tx, info) { +TXDB.prototype._verify = co(function* _verify(tx, info) { var i, input, prevout, address, coin, spent, rtx, rinfo, result; for (i = 0; i < tx.inputs.length; i++) { @@ -539,7 +540,7 @@ TXDB.prototype._verify = spawn.co(function* _verify(tx, info) { * @param {Function} callback */ -TXDB.prototype._resolveOrphans = spawn.co(function* _resolveOrphans(tx, index) { +TXDB.prototype._resolveOrphans = co(function* _resolveOrphans(tx, index) { var hash = tx.hash('hex'); var i, orphans, coin, item, input, orphan; @@ -592,7 +593,7 @@ TXDB.prototype._resolveOrphans = spawn.co(function* _resolveOrphans(tx, index) { * @param {Function} callback */ -TXDB.prototype.add = spawn.co(function* add(tx, info) { +TXDB.prototype.add = co(function* add(tx, info) { var unlock = yield this._lock(); var hash, path, account; var i, result, input, output, coin; @@ -743,7 +744,7 @@ TXDB.prototype.add = spawn.co(function* add(tx, info) { * @param {Function} callback - Returns [Error, Boolean]. */ -TXDB.prototype._removeConflict = spawn.co(function* _removeConflict(hash, ref) { +TXDB.prototype._removeConflict = co(function* _removeConflict(hash, ref) { var tx = yield this.getTX(hash); var info; @@ -785,7 +786,7 @@ TXDB.prototype._removeConflict = spawn.co(function* _removeConflict(hash, ref) { * @param {Function} callback - Returns [Error, Boolean]. */ -TXDB.prototype._removeRecursive = spawn.co(function* _removeRecursive(tx) { +TXDB.prototype._removeRecursive = co(function* _removeRecursive(tx) { var hash = tx.hash('hex'); var i, spent, stx, info; @@ -830,7 +831,7 @@ TXDB.prototype._removeRecursive = spawn.co(function* _removeRecursive(tx) { * @param {Function} callback - Returns [Error, Boolean]. */ -TXDB.prototype.isDoubleSpend = spawn.co(function* isDoubleSpend(tx) { +TXDB.prototype.isDoubleSpend = co(function* isDoubleSpend(tx) { var i, input, prevout, spent; for (i = 0; i < tx.inputs.length; i++) { @@ -868,7 +869,7 @@ TXDB.prototype.isSpent = function isSpent(hash, index) { * transaction was confirmed, or should be ignored. */ -TXDB.prototype._confirm = spawn.co(function* _confirm(tx, info) { +TXDB.prototype._confirm = co(function* _confirm(tx, info) { var hash = tx.hash('hex'); var i, account, existing, output, coin; var address, key; @@ -962,7 +963,7 @@ TXDB.prototype._confirm = spawn.co(function* _confirm(tx, info) { * @param {Function} callback - Returns [Error]. */ -TXDB.prototype.remove = spawn.co(function* remove(hash, force) { +TXDB.prototype.remove = co(function* remove(hash, force) { var unlock = yield this._lock(force); var info = yield this._removeRecursive(); @@ -982,7 +983,7 @@ TXDB.prototype.remove = spawn.co(function* remove(hash, force) { * @param {Function} callback - Returns [Error]. */ -TXDB.prototype._lazyRemove = spawn.co(function* lazyRemove(tx) { +TXDB.prototype._lazyRemove = co(function* lazyRemove(tx) { var info = yield this.getInfo(tx); if (!info) return; @@ -998,7 +999,7 @@ TXDB.prototype._lazyRemove = spawn.co(function* lazyRemove(tx) { * @param {Function} callback - Returns [Error]. */ -TXDB.prototype._remove = spawn.co(function* remove(tx, info) { +TXDB.prototype._remove = co(function* remove(tx, info) { var hash = tx.hash('hex'); var i, path, account, key, prevout; var address, input, output, coin; @@ -1085,7 +1086,7 @@ TXDB.prototype._remove = spawn.co(function* remove(tx, info) { * @param {Function} callback */ -TXDB.prototype.unconfirm = spawn.co(function* unconfirm(hash, force) { +TXDB.prototype.unconfirm = co(function* unconfirm(hash, force) { var unlock = yield this._lock(force); var tx, info, result; @@ -1141,7 +1142,7 @@ TXDB.prototype.unconfirm = spawn.co(function* unconfirm(hash, force) { * @param {Function} callback */ -TXDB.prototype._unconfirm = spawn.co(function* unconfirm(tx, info) { +TXDB.prototype._unconfirm = co(function* unconfirm(tx, info) { var hash = tx.hash('hex'); var height = tx.height; var i, account, output, key, coin; @@ -1475,7 +1476,7 @@ TXDB.prototype.getRangeHashes = function getRangeHashes(account, options) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -TXDB.prototype.getRange = spawn.co(function* getRange(account, options) { +TXDB.prototype.getRange = co(function* getRange(account, options) { var txs = []; var i, hashes, hash, tx; @@ -1544,7 +1545,7 @@ TXDB.prototype.getHistory = function getHistory(account) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -TXDB.prototype.getAccountHistory = spawn.co(function* getAccountHistory(account) { +TXDB.prototype.getAccountHistory = co(function* getAccountHistory(account) { var txs = []; var i, hashes, hash, tx; @@ -1569,7 +1570,7 @@ TXDB.prototype.getAccountHistory = spawn.co(function* getAccountHistory(account) * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -TXDB.prototype.getUnconfirmed = spawn.co(function* getUnconfirmed(account) { +TXDB.prototype.getUnconfirmed = co(function* getUnconfirmed(account) { var txs = []; var i, hashes, hash, tx; @@ -1626,7 +1627,7 @@ TXDB.prototype.getCoins = function getCoins(account) { * @param {Function} callback - Returns [Error, {@link Coin}[]]. */ -TXDB.prototype.getAccountCoins = spawn.co(function* getCoins(account) { +TXDB.prototype.getAccountCoins = co(function* getCoins(account) { var coins = []; var i, hashes, key, coin; @@ -1678,7 +1679,7 @@ TXDB.prototype.fillHistory = function fillHistory(tx) { * @param {Function} callback - Returns [Error, {@link TX}]. */ -TXDB.prototype.fillCoins = spawn.co(function* fillCoins(tx) { +TXDB.prototype.fillCoins = co(function* fillCoins(tx) { var i, input, prevout, coin; if (tx.isCoinbase()) @@ -1718,7 +1719,7 @@ TXDB.prototype.getTX = function getTX(hash) { * @param {Function} callback - Returns [Error, {@link TXDetails}]. */ -TXDB.prototype.getDetails = spawn.co(function* getDetails(hash) { +TXDB.prototype.getDetails = co(function* getDetails(hash) { var tx = yield this.getTX(hash); if (!tx) @@ -1733,7 +1734,7 @@ TXDB.prototype.getDetails = spawn.co(function* getDetails(hash) { * @param {Function} callback */ -TXDB.prototype.toDetails = spawn.co(function* toDetails(tx) { +TXDB.prototype.toDetails = co(function* toDetails(tx) { var i, out, txs, details, info; if (Array.isArray(tx)) { @@ -1828,7 +1829,7 @@ TXDB.prototype.getSpentCoin = function getSpentCoin(spent, prevout) { * @param {Function} callback */ -TXDB.prototype.updateSpentCoin = spawn.co(function* updateSpentCoin(tx, i) { +TXDB.prototype.updateSpentCoin = co(function* updateSpentCoin(tx, i) { var prevout = bcoin.outpoint.fromTX(tx, i); var spent = yield this.isSpent(prevout.hash, prevout.index); var coin; @@ -1867,7 +1868,7 @@ TXDB.prototype.hasCoin = function hasCoin(hash, index) { * @param {Function} callback - Returns [Error, {@link Balance}]. */ -TXDB.prototype.getBalance = spawn.co(function* getBalance(account) { +TXDB.prototype.getBalance = co(function* getBalance(account) { var self = this; var balance; @@ -1905,7 +1906,7 @@ TXDB.prototype.getBalance = spawn.co(function* getBalance(account) { * @param {Function} callback - Returns [Error, {@link Balance}]. */ -TXDB.prototype.getAccountBalance = spawn.co(function* getBalance(account) { +TXDB.prototype.getAccountBalance = co(function* getBalance(account) { var balance = new Balance(this.wallet); var i, key, coin, hashes, hash, data; @@ -1940,7 +1941,7 @@ TXDB.prototype.getAccountBalance = spawn.co(function* getBalance(account) { * @param {Function} callback */ -TXDB.prototype.zap = spawn.co(function* zap(account, age) { +TXDB.prototype.zap = co(function* zap(account, age) { var unlock = yield this._lock(); var i, txs, tx, hash; @@ -1976,7 +1977,7 @@ TXDB.prototype.zap = spawn.co(function* zap(account, age) { * @param {Function} callback */ -TXDB.prototype.abandon = spawn.co(function* abandon(hash) { +TXDB.prototype.abandon = co(function* abandon(hash) { var result = yield this.has(layout.p(hash)); if (!result) throw new Error('TX not eligible.'); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index e6f63cc9..934e5ce9 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter; var constants = bcoin.constants; var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var crypto = require('../crypto/crypto'); var assert = utils.assert; var BufferReader = require('../utils/reader'); @@ -179,7 +180,7 @@ Wallet.fromOptions = function fromOptions(db, options) { * @param {Function} callback */ -Wallet.prototype.init = spawn.co(function* init(options) { +Wallet.prototype.init = co(function* init(options) { var account; assert(!this.initialized); @@ -203,7 +204,7 @@ Wallet.prototype.init = spawn.co(function* init(options) { * @param {Function} callback */ -Wallet.prototype.open = spawn.co(function* open() { +Wallet.prototype.open = co(function* open() { var account; assert(this.initialized); @@ -244,7 +245,7 @@ Wallet.prototype.destroy = function destroy() { * @param {Function} callback */ -Wallet.prototype.addKey = spawn.co(function* addKey(account, key) { +Wallet.prototype.addKey = co(function* addKey(account, key) { var unlock = yield this._lockWrite(); var result; @@ -286,7 +287,7 @@ Wallet.prototype.addKey = spawn.co(function* addKey(account, key) { * @param {Function} callback */ -Wallet.prototype.removeKey = spawn.co(function* removeKey(account, key) { +Wallet.prototype.removeKey = co(function* removeKey(account, key) { var unlock = yield this._lockWrite(); var result; @@ -328,7 +329,7 @@ Wallet.prototype.removeKey = spawn.co(function* removeKey(account, key) { * @param {Function} callback */ -Wallet.prototype.setPassphrase = spawn.co(function* setPassphrase(old, new_) { +Wallet.prototype.setPassphrase = co(function* setPassphrase(old, new_) { var unlock = yield this._lockWrite(); if (!new_) { @@ -358,7 +359,7 @@ Wallet.prototype.setPassphrase = spawn.co(function* setPassphrase(old, new_) { * @param {Function} callback */ -Wallet.prototype.retoken = spawn.co(function* retoken(passphrase) { +Wallet.prototype.retoken = co(function* retoken(passphrase) { var unlock = yield this._lockWrite(); var master; @@ -459,7 +460,7 @@ Wallet.prototype.getToken = function getToken(master, nonce) { * @param {Function} callback - Returns [Error, {@link Account}]. */ -Wallet.prototype.createAccount = spawn.co(function* createAccount(options) { +Wallet.prototype.createAccount = co(function* createAccount(options) { var unlock = yield this._lockWrite(); var passphrase = options.passphrase; var timeout = options.timeout; @@ -519,7 +520,7 @@ Wallet.prototype.createAccount = spawn.co(function* createAccount(options) { * @param {Function} callback - Returns [Error, {@link Account}]. */ -Wallet.prototype.ensureAccount = spawn.co(function* ensureAccount(options) { +Wallet.prototype.ensureAccount = co(function* ensureAccount(options) { var account = options.account; var exists; @@ -558,7 +559,7 @@ Wallet.prototype.getAddressHashes = function getAddressHashes() { * @param {Function} callback - Returns [Error, {@link Account}]. */ -Wallet.prototype.getAccount = spawn.co(function* getAccount(account) { +Wallet.prototype.getAccount = co(function* getAccount(account) { if (this.account) { if (account === 0 || account === 'default') return this.account; @@ -612,7 +613,7 @@ Wallet.prototype.createChange = function createChange(account) { * @param {Function} callback - Returns [Error, {@link KeyRing}]. */ -Wallet.prototype.createAddress = spawn.co(function* createAddress(account, change) { +Wallet.prototype.createAddress = co(function* createAddress(account, change) { var unlock = yield this._lockWrite(); var result; @@ -708,7 +709,7 @@ Wallet.prototype.hasAddress = function hasAddress(address) { * @param {Function} callback - Returns [Error, {@link Path}]. */ -Wallet.prototype.getPath = spawn.co(function* getPath(address) { +Wallet.prototype.getPath = co(function* getPath(address) { var hash = bcoin.address.getHash(address, 'hex'); var path; @@ -731,7 +732,7 @@ Wallet.prototype.getPath = spawn.co(function* getPath(address) { * @param {Function} callback - Returns [Error, {@link Path}]. */ -Wallet.prototype.getPaths = spawn.co(function* getPaths(account) { +Wallet.prototype.getPaths = co(function* getPaths(account) { var out = []; var i, account, paths, path; @@ -758,7 +759,7 @@ Wallet.prototype.getPaths = spawn.co(function* getPaths(account) { * @param {Function} callback */ -Wallet.prototype.importKey = spawn.co(function* importKey(account, ring, passphrase) { +Wallet.prototype.importKey = co(function* importKey(account, ring, passphrase) { var unlock = yield this._lockWrite(); var exists, account, raw, path; @@ -852,7 +853,7 @@ Wallet.prototype.importKey = spawn.co(function* importKey(account, ring, passphr * fee from existing outputs rather than adding more inputs. */ -Wallet.prototype.fund = spawn.co(function* fund(tx, options, force) { +Wallet.prototype.fund = co(function* fund(tx, options, force) { var unlock = yield this._lockFund(force); var rate, account, coins; @@ -924,7 +925,7 @@ Wallet.prototype.fund = spawn.co(function* fund(tx, options, force) { * @param {Function} callback - Returns [Error, {@link MTX}]. */ -Wallet.prototype.createTX = spawn.co(function* createTX(options, force) { +Wallet.prototype.createTX = co(function* createTX(options, force) { var outputs = options.outputs; var i, tx, total; @@ -975,7 +976,7 @@ Wallet.prototype.createTX = spawn.co(function* createTX(options, force) { * @param {Function} callback - Returns [Error, {@link TX}]. */ -Wallet.prototype.send = spawn.co(function* send(options) { +Wallet.prototype.send = co(function* send(options) { var unlock = yield this._lockFund(); var tx; @@ -1014,7 +1015,7 @@ Wallet.prototype.send = spawn.co(function* send(options) { * @param {Function} callback */ -Wallet.prototype.resend = spawn.co(function* resend() { +Wallet.prototype.resend = co(function* resend() { var txs = yield this.getUnconfirmed(); var i; @@ -1034,7 +1035,7 @@ Wallet.prototype.resend = spawn.co(function* resend() { * @param {Function} callback - Returns [Error, {@link KeyRing}[]]. */ -Wallet.prototype.deriveInputs = spawn.co(function* deriveInputs(tx) { +Wallet.prototype.deriveInputs = co(function* deriveInputs(tx) { var rings = []; var i, paths, path, account, ring; @@ -1062,7 +1063,7 @@ Wallet.prototype.deriveInputs = spawn.co(function* deriveInputs(tx) { * @param {Function} callback */ -Wallet.prototype.getKeyRing = spawn.co(function* getKeyRing(address) { +Wallet.prototype.getKeyRing = co(function* getKeyRing(address) { var hash = bcoin.address.getHash(address, 'hex'); var path, account; @@ -1088,7 +1089,7 @@ Wallet.prototype.getKeyRing = spawn.co(function* getKeyRing(address) { * @param {Function} callback - Returns [Error, {@link Path}[]]. */ -Wallet.prototype.getInputPaths = spawn.co(function* getInputPaths(tx) { +Wallet.prototype.getInputPaths = co(function* getInputPaths(tx) { var paths = []; var hashes = []; var i, hash, path; @@ -1126,7 +1127,7 @@ Wallet.prototype.getInputPaths = spawn.co(function* getInputPaths(tx) { * @param {Function} callback - Returns [Error, {@link Path}[]]. */ -Wallet.prototype.getOutputPaths = spawn.co(function* getOutputPaths(tx) { +Wallet.prototype.getOutputPaths = co(function* getOutputPaths(tx) { var paths = []; var hashes = []; var i, hash, path; @@ -1158,7 +1159,7 @@ Wallet.prototype.getOutputPaths = spawn.co(function* getOutputPaths(tx) { * (true if new addresses were allocated). */ -Wallet.prototype.syncOutputDepth = spawn.co(function* syncOutputDepth(info) { +Wallet.prototype.syncOutputDepth = co(function* syncOutputDepth(info) { var unlock = yield this._lockWrite(); var receive = []; var accounts = {}; @@ -1246,7 +1247,7 @@ Wallet.prototype.syncOutputDepth = spawn.co(function* syncOutputDepth(info) { * @param {Function} callback */ -Wallet.prototype.updateBalances = spawn.co(function* updateBalances() { +Wallet.prototype.updateBalances = co(function* updateBalances() { var balance; if (this.db.listeners('balance').length === 0 @@ -1268,7 +1269,7 @@ Wallet.prototype.updateBalances = spawn.co(function* updateBalances() { * @param {Function} callback */ -Wallet.prototype.handleTX = spawn.co(function* handleTX(info) { +Wallet.prototype.handleTX = co(function* handleTX(info) { yield this.syncOutputDepth(info); yield this.updateBalances(); }); @@ -1279,7 +1280,7 @@ Wallet.prototype.handleTX = spawn.co(function* handleTX(info) { * @returns {Script} */ -Wallet.prototype.getRedeem = spawn.co(function* getRedeem(hash) { +Wallet.prototype.getRedeem = co(function* getRedeem(hash) { var ring; if (typeof hash === 'string') @@ -1302,7 +1303,7 @@ Wallet.prototype.getRedeem = spawn.co(function* getRedeem(hash) { * (total number of scripts built). */ -Wallet.prototype.template = spawn.co(function* template(tx) { +Wallet.prototype.template = co(function* template(tx) { var total = 0; var i, rings, ring; @@ -1325,7 +1326,7 @@ Wallet.prototype.template = spawn.co(function* template(tx) { * of inputs scripts built and signed). */ -Wallet.prototype.sign = spawn.co(function* sign(tx, options) { +Wallet.prototype.sign = co(function* sign(tx, options) { var passphrase, timeout, master, rings; if (!options) @@ -1443,7 +1444,7 @@ Wallet.prototype.addTX = function addTX(tx) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -Wallet.prototype.getHistory = spawn.co(function* getHistory(account) { +Wallet.prototype.getHistory = co(function* getHistory(account) { account = yield this._getIndex(account); return this.tx.getHistory(account); }); @@ -1454,7 +1455,7 @@ Wallet.prototype.getHistory = spawn.co(function* getHistory(account) { * @param {Function} callback - Returns [Error, {@link Coin}[]]. */ -Wallet.prototype.getCoins = spawn.co(function* getCoins(account) { +Wallet.prototype.getCoins = co(function* getCoins(account) { account = yield this._getIndex(account); return yield this.tx.getCoins(account); }); @@ -1465,7 +1466,7 @@ Wallet.prototype.getCoins = spawn.co(function* getCoins(account) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -Wallet.prototype.getUnconfirmed = spawn.co(function* getUnconfirmed(account) { +Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(account) { account = yield this._getIndex(account); return yield this.tx.getUnconfirmed(account); }); @@ -1476,7 +1477,7 @@ Wallet.prototype.getUnconfirmed = spawn.co(function* getUnconfirmed(account) { * @param {Function} callback - Returns [Error, {@link Balance}]. */ -Wallet.prototype.getBalance = spawn.co(function* getBalance(account) { +Wallet.prototype.getBalance = co(function* getBalance(account) { account = yield this._getIndex(account); return yield this.tx.getBalance(account); }); @@ -1490,7 +1491,7 @@ Wallet.prototype.getBalance = spawn.co(function* getBalance(account) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -Wallet.prototype.getRange = spawn.co(function* getRange(account, options) { +Wallet.prototype.getRange = co(function* getRange(account, options) { if (account && typeof account === 'object') { options = account; account = null; @@ -1506,7 +1507,7 @@ Wallet.prototype.getRange = spawn.co(function* getRange(account, options) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -Wallet.prototype.getLast = spawn.co(function* getLast(account, limit) { +Wallet.prototype.getLast = co(function* getLast(account, limit) { account = yield this._getIndex(account); return yield this.tx.getLast(account, limit); }); @@ -1518,7 +1519,7 @@ Wallet.prototype.getLast = spawn.co(function* getLast(account, limit) { * @param {Function} callback - Returns [Error]. */ -Wallet.prototype.zap = spawn.co(function* zap(account, age) { +Wallet.prototype.zap = co(function* zap(account, age) { account = yield this._getIndex(account); return yield this.tx.zap(account, age); }); @@ -1541,7 +1542,7 @@ Wallet.prototype.abandon = function abandon(hash) { * @param {Function} callback */ -Wallet.prototype._getIndex = spawn.co(function* _getIndex(account) { +Wallet.prototype._getIndex = co(function* _getIndex(account) { var index; if (account == null) @@ -2017,7 +2018,7 @@ MasterKey.prototype._lock = function _lock(force) { * @param {Function} callback - Returns [Error, {@link HDPrivateKey}]. */ -MasterKey.prototype.unlock = spawn.co(function* _unlock(passphrase, timeout) { +MasterKey.prototype.unlock = co(function* _unlock(passphrase, timeout) { var unlock = yield this._lock(); var data, key; @@ -2135,7 +2136,7 @@ MasterKey.prototype.destroy = function destroy() { * @param {Function} callback */ -MasterKey.prototype.decrypt = spawn.co(function* decrypt(passphrase) { +MasterKey.prototype.decrypt = co(function* decrypt(passphrase) { var unlock = yield this._lock(); var data; @@ -2176,7 +2177,7 @@ MasterKey.prototype.decrypt = spawn.co(function* decrypt(passphrase) { * @param {Function} callback */ -MasterKey.prototype.encrypt = spawn.co(function* encrypt(passphrase) { +MasterKey.prototype.encrypt = co(function* encrypt(passphrase) { var unlock = yield this._lock(); var data, iv; diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index c716f7dc..5e2feba7 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -11,6 +11,7 @@ var bcoin = require('../env'); var AsyncObject = require('../utils/async'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var crypto = require('../crypto/crypto'); var assert = utils.assert; var constants = bcoin.constants; @@ -225,7 +226,7 @@ WalletDB.prototype._lockTX = function _lockTX(force) { * @param {Function} callback */ -WalletDB.prototype._open = spawn.co(function* open() { +WalletDB.prototype._open = co(function* open() { yield this.db.open(); yield this.db.checkVersion('V', 2); yield this.writeGenesis(); @@ -245,7 +246,7 @@ WalletDB.prototype._open = spawn.co(function* open() { * @param {Function} callback */ -WalletDB.prototype._close = spawn.co(function* close() { +WalletDB.prototype._close = co(function* close() { var keys = Object.keys(this.wallets); var i, key, wallet; @@ -274,7 +275,7 @@ WalletDB.prototype.backup = function backup(path) { * @param {Function} callback */ -WalletDB.prototype.getDepth = spawn.co(function* getDepth() { +WalletDB.prototype.getDepth = co(function* getDepth() { var kv, iter, depth; // This may seem like a strange way to do @@ -404,7 +405,7 @@ WalletDB.prototype.testFilter = function testFilter(hashes) { * @param {Function} callback - Returns [Error, Object]. */ -WalletDB.prototype.dump = spawn.co(function* dump() { +WalletDB.prototype.dump = co(function* dump() { var records = {}; yield this.db.iterate({ gte: ' ', @@ -472,7 +473,7 @@ WalletDB.prototype.getWalletID = function getWalletID(id) { * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype.get = spawn.co(function* get(wid) { +WalletDB.prototype.get = co(function* get(wid) { var self = this; var wallet, unlock; @@ -535,7 +536,7 @@ WalletDB.prototype.save = function save(wallet) { * @param {Function} callback */ -WalletDB.prototype.auth = spawn.co(function* auth(wid, token) { +WalletDB.prototype.auth = co(function* auth(wid, token) { var wallet = yield this.get(wid); if (!wallet) return; @@ -559,7 +560,7 @@ WalletDB.prototype.auth = spawn.co(function* auth(wid, token) { * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype.create = spawn.co(function* create(options) { +WalletDB.prototype.create = co(function* create(options) { var unlock, wallet, exists; if (!options) @@ -596,7 +597,7 @@ WalletDB.prototype.create = spawn.co(function* create(options) { * @param {Function} callback */ -WalletDB.prototype.has = spawn.co(function* has(id) { +WalletDB.prototype.has = co(function* has(id) { var wid = yield this.getWalletID(id); return wid != null; }); @@ -607,7 +608,7 @@ WalletDB.prototype.has = spawn.co(function* has(id) { * @param {Function} callback */ -WalletDB.prototype.ensure = spawn.co(function* ensure(options) { +WalletDB.prototype.ensure = co(function* ensure(options) { var wallet = yield this.get(options.id); if (wallet) return wallet; @@ -621,7 +622,7 @@ WalletDB.prototype.ensure = spawn.co(function* ensure(options) { * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype.getAccount = spawn.co(function* getAccount(wid, name) { +WalletDB.prototype.getAccount = co(function* getAccount(wid, name) { var index = yield this.getAccountIndex(wid, name); var account; @@ -666,7 +667,7 @@ WalletDB.prototype._getAccount = function getAccount(wid, index) { * @param {Function} callback - Returns [Error, Array]. */ -WalletDB.prototype.getAccounts = spawn.co(function* getAccounts(wid) { +WalletDB.prototype.getAccounts = co(function* getAccounts(wid) { var map = []; var i, accounts; @@ -699,7 +700,7 @@ WalletDB.prototype.getAccounts = spawn.co(function* getAccounts(wid) { * @param {Function} callback - Returns [Error, Number]. */ -WalletDB.prototype.getAccountIndex = spawn.co(function* getAccountIndex(wid, name) { +WalletDB.prototype.getAccountIndex = co(function* getAccountIndex(wid, name) { var index; if (!wid) @@ -744,7 +745,7 @@ WalletDB.prototype.saveAccount = function saveAccount(account) { * @param {Function} callback - Returns [Error, {@link Account}]. */ -WalletDB.prototype.createAccount = spawn.co(function* createAccount(options) { +WalletDB.prototype.createAccount = co(function* createAccount(options) { var exists = yield this.hasAccount(options.wid, options.name); var account; @@ -770,7 +771,7 @@ WalletDB.prototype.createAccount = spawn.co(function* createAccount(options) { * @param {Function} callback - Returns [Error, Boolean]. */ -WalletDB.prototype.hasAccount = spawn.co(function* hasAccount(wid, account) { +WalletDB.prototype.hasAccount = co(function* hasAccount(wid, account) { var index, key; if (!wid) @@ -798,7 +799,7 @@ WalletDB.prototype.hasAccount = spawn.co(function* hasAccount(wid, account) { * @param {Function} callback */ -WalletDB.prototype.saveAddress = spawn.co(function* saveAddress(wid, rings) { +WalletDB.prototype.saveAddress = co(function* saveAddress(wid, rings) { var i, ring, path; for (i = 0; i < rings.length; i++) { @@ -823,7 +824,7 @@ WalletDB.prototype.saveAddress = spawn.co(function* saveAddress(wid, rings) { * @param {Function} callback */ -WalletDB.prototype.writeAddress = spawn.co(function* writeAddress(wid, address, path) { +WalletDB.prototype.writeAddress = co(function* writeAddress(wid, address, path) { var hash = address.getHash('hex'); var batch = this.batch(wid); var paths; @@ -854,7 +855,7 @@ WalletDB.prototype.writeAddress = spawn.co(function* writeAddress(wid, address, * @param {Function} callback */ -WalletDB.prototype.getAddressPaths = spawn.co(function* getAddressPaths(hash) { +WalletDB.prototype.getAddressPaths = co(function* getAddressPaths(hash) { var paths; if (!hash) @@ -885,7 +886,7 @@ WalletDB.prototype.getAddressPaths = spawn.co(function* getAddressPaths(hash) { * @param {Function} callback */ -WalletDB.prototype.hasAddress = spawn.co(function* hasAddress(wid, hash) { +WalletDB.prototype.hasAddress = co(function* hasAddress(wid, hash) { var paths = yield this.getAddressPaths(hash); if (!paths || !paths[wid]) @@ -961,7 +962,7 @@ WalletDB.prototype.getWallets = function getWallets() { * @param {Function} callback */ -WalletDB.prototype.rescan = spawn.co(function* rescan(chaindb, height) { +WalletDB.prototype.rescan = co(function* rescan(chaindb, height) { var self = this; var unlock = yield this._lockTX(); var hashes; @@ -1024,7 +1025,7 @@ WalletDB.prototype.getPendingKeys = function getPendingKeys() { * @param {Function} callback */ -WalletDB.prototype.resend = spawn.co(function* resend() { +WalletDB.prototype.resend = co(function* resend() { var self = this; var i, keys, key, tx; @@ -1050,7 +1051,7 @@ WalletDB.prototype.resend = spawn.co(function* resend() { * @param {Function} callback - Returns [Error, {@link PathInfo[]}]. */ -WalletDB.prototype.mapWallets = spawn.co(function* mapWallets(tx) { +WalletDB.prototype.mapWallets = co(function* mapWallets(tx) { var hashes = tx.getHashes('hex'); var table; @@ -1071,7 +1072,7 @@ WalletDB.prototype.mapWallets = spawn.co(function* mapWallets(tx) { * @param {Function} callback - Returns [Error, {@link PathInfo}]. */ -WalletDB.prototype.getPathInfo = spawn.co(function* getPathInfo(wallet, tx) { +WalletDB.prototype.getPathInfo = co(function* getPathInfo(wallet, tx) { var hashes = tx.getHashes('hex'); var table = yield this.getTable(hashes); var info; @@ -1091,7 +1092,7 @@ WalletDB.prototype.getPathInfo = spawn.co(function* getPathInfo(wallet, tx) { * @param {Function} callback - Returns [Error, {@link AddressTable}]. */ -WalletDB.prototype.getTable = spawn.co(function* getTable(hashes) { +WalletDB.prototype.getTable = co(function* getTable(hashes) { var table = {}; var count = 0; var i, j, keys, values, hash, paths; @@ -1128,7 +1129,7 @@ WalletDB.prototype.getTable = spawn.co(function* getTable(hashes) { * @param {Function} callback */ -WalletDB.prototype.writeGenesis = spawn.co(function* writeGenesis() { +WalletDB.prototype.writeGenesis = co(function* writeGenesis() { var block = yield this.getTip(); if (block) { this.tip = block.hash; @@ -1156,7 +1157,7 @@ WalletDB.prototype.getTip = function getTip() { * @param {Function} callback */ -WalletDB.prototype.setTip = spawn.co(function* setTip(hash, height) { +WalletDB.prototype.setTip = co(function* setTip(hash, height) { var block = new WalletBlock(hash, height); yield this.db.put(layout.R, block.toTip()); @@ -1234,7 +1235,7 @@ WalletDB.prototype.getWalletsByTX = function getWalletsByTX(hash) { * @param {Function} callback */ -WalletDB.prototype.addBlock = spawn.co(function* addBlock(entry, txs, force) { +WalletDB.prototype.addBlock = co(function* addBlock(entry, txs, force) { var unlock = yield this._lockTX(force); var i, block, matches, hash, tx, wallets; @@ -1302,7 +1303,7 @@ WalletDB.prototype.addBlock = spawn.co(function* addBlock(entry, txs, force) { * @param {Function} callback */ -WalletDB.prototype.removeBlock = spawn.co(function* removeBlock(entry) { +WalletDB.prototype.removeBlock = co(function* removeBlock(entry) { var unlock = yield this._lockTX(); var i, j, block, data, hash, wallets, wid, wallet; @@ -1360,7 +1361,7 @@ WalletDB.prototype.removeBlock = spawn.co(function* removeBlock(entry) { * @param {Function} callback - Returns [Error]. */ -WalletDB.prototype.addTX = spawn.co(function* addTX(tx, force) { +WalletDB.prototype.addTX = co(function* addTX(tx, force) { var unlock = yield this._lockTX(force); var i, wallets, info, wallet; @@ -1417,7 +1418,7 @@ WalletDB.prototype.addTX = spawn.co(function* addTX(tx, force) { * @param {Function} callback */ -WalletDB.prototype.getAddressPath = spawn.co(function* getAddressPath(wid, hash) { +WalletDB.prototype.getAddressPath = co(function* getAddressPath(wid, hash) { var paths = yield this.getAddressPaths(hash); var path; diff --git a/lib/workers/workers.js b/lib/workers/workers.js index 8055598f..39873583 100644 --- a/lib/workers/workers.js +++ b/lib/workers/workers.js @@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter; var bn = require('bn.js'); var utils = require('../utils/utils'); var spawn = require('../utils/spawn'); +var co = spawn.co; var global = utils.global; var assert = utils.assert; var BufferWriter = require('../utils/writer'); @@ -247,7 +248,7 @@ Workers.prototype.verify = function verify(tx, flags) { * @param {Function} callback */ -Workers.prototype.sign = spawn.co(function* sign(tx, ring, type) { +Workers.prototype.sign = co(function* sign(tx, ring, type) { var i, result, input, sig, sigs, total; result = yield this.execute('sign', [tx, ring, type], -1);