diff --git a/lib/http/base.js b/lib/http/base.js index 127e1512..d7cb80de 100644 --- a/lib/http/base.js +++ b/lib/http/base.js @@ -213,7 +213,7 @@ HTTPBase.prototype._initIO = function _initIO() { HTTPBase.prototype._open = function open() { assert(typeof this.options.port === 'number', 'Port required.'); - this.listen(this.options.port, this.options.host); + return this.listen(this.options.port, this.options.host); }; /** diff --git a/lib/http/rpc.js b/lib/http/rpc.js index fb21702d..5b0628ce 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -3934,17 +3934,17 @@ RPC.prototype.signmessage = co(function* signmessage(args) { return sig.toString('base64'); }); -RPC.prototype.walletlock = function walletlock(args) { +RPC.prototype.walletlock = co(function* walletlock(args) { if (args.help || (this.wallet.master.encrypted && args.length !== 0)) throw new RPCError('walletlock'); if (!this.wallet.master.encrypted) throw new RPCError('Wallet is not encrypted.'); - this.wallet.lock(); + yield this.wallet.lock(); return null; -}; +}); RPC.prototype.walletpassphrasechange = co(function* walletpassphrasechange(args) { var old, new_; diff --git a/lib/http/server.js b/lib/http/server.js index e7158adb..8c43323c 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -883,7 +883,7 @@ HTTPServer.prototype._initIO = function _initIO() { socket.join(id); callback(); - }).catch(function(err) { + }, function(err) { self.logger.info('Wallet auth failure for %s: %s.', id, err.message); return callback({ error: 'Bad token.' }); }); @@ -943,10 +943,13 @@ HTTPServer.prototype._initIO = function _initIO() { socket.on('scan chain', function(args, callback) { var start = args[0]; - if (!utils.isHex256(start) && !utils.isNumber(start)) + if (!utils.isHex256(start) && !utils.isUInt32(start)) return callback({ error: 'Invalid parameter.' }); - socket.scan(start).then(callback).catch(function(err) { + if (typeof start === 'string') + start = utils.revHex(start); + + socket.scan(start).then(callback, function(err) { callback({ error: err.message }); }); }); @@ -1062,8 +1065,8 @@ HTTPServer.prototype.del = function del(path, callback) { * @see HTTPBase#listen */ -HTTPServer.prototype.listen = function listen(port, host, callback) { - this.server.listen(port, host, callback); +HTTPServer.prototype.listen = function listen(port, host) { + return this.server.listen(port, host); }; /** @@ -1257,27 +1260,40 @@ ClientSocket.prototype.testFilter = function testFilter(tx) { } }; -ClientSocket.prototype.scan = function scan(start) { - var self = this; - var i; +ClientSocket.prototype.scan = co(function* scan(start) { + var scanner = this.scanner.bind(this); + var entry; - if (typeof start === 'string') - start = utils.revHex(start); + if (this.chain.db.options.spv) { + entry = yield this.chain.db.get(start); - if (this.chain.db.options.spv) - return this.chain.reset(start); + if (!entry) + throw new Error('Block not found.'); + + if (!entry.isGenesis()) + start = entry.prevBlock; + + yield this.chain.reset(start); + + return; + } if (this.chain.db.options.prune) - return Promise.reject(new Error('Cannot scan in pruned mode.')); + throw new Error('Cannot scan in pruned mode.'); - return this.chain.db.scan(start, this.filter, co(function *(entry, txs) { - for (i = 0; i < txs.length; i++) - txs[i] = txs[i].toJSON(); + yield this.chain.db.scan(start, this.filter, scanner); +}); - self.emit('block tx', entry.toJSON(), txs); +ClientSocket.prototype.scanner = function scanner(entry, txs) { + var json = new Array(txs.length); + var i; - yield utils.wait(); - })); + for (i = 0; i < txs.length; i++) + json[i] = txs[i].toJSON(); + + this.emit('block tx', entry.toJSON(), json); + + return Promise.resolve(null); }; ClientSocket.prototype.join = function join(id) { diff --git a/lib/utils/spawn.js b/lib/utils/spawn.js index 05e849f9..2f34305e 100644 --- a/lib/utils/spawn.js +++ b/lib/utils/spawn.js @@ -77,31 +77,6 @@ function co(generator) { }; } -/** - * Wrap a generator function to be - * executed into a function that - * returns a promise (with a lock). - * @param {String} name - lock name. - * @param {GeneratorFunction} - * @returns {Function} - */ - -function col(name, generator) { - var func = co(generator); - return co(function *() { - var unlock = yield this[name].lock(); - var result; - try { - result = yield func.apply(this, arguments); - } catch (e) { - unlock(); - throw e; - } - unlock(); - return result; - }); -} - /** * Wrap a generator function to be * executed into a function that @@ -235,11 +210,11 @@ function wrap(resolve, reject) { function call(func) { var self = this; - var args = new Array(arguments.length); + var args = new Array(Math.max(0, arguments.length - 1)); var i; for (i = 1; i < arguments.length; i++) - args[i] = arguments[i]; + args[i - 1] = arguments[i]; return new Promise(function(resolve, reject) { args.push(wrap(resolve, reject)); @@ -284,7 +259,6 @@ exports = spawn; exports.exec = exec; exports.spawn = spawn; exports.co = co; -exports.col = col; exports.cob = cob; exports.con = con; exports.cb = cb;