refactor: cleanup.

This commit is contained in:
Christopher Jeffrey 2016-09-21 10:59:29 -07:00
parent df23810b0c
commit 2899219033
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 46 additions and 45 deletions

View File

@ -1231,7 +1231,7 @@ ChainDB.prototype.scan = function scan(start, filter, iter) {
}
}
yield iter(entry, txs);
yield* iter(entry, txs);
entry = yield entry.getNext();
}

View File

@ -285,7 +285,7 @@ RPC.prototype.execute = function execute(json) {
return this.getmemory(json.params);
default:
return callback(new Error('Method not found: ' + json.method + '.'));
throw new Error('Method not found: ' + json.method + '.');
}
};
@ -432,7 +432,7 @@ RPC.prototype.getaddednodeinfo = function getaddednodeinfo(args) {
addr = NetworkAddress.fromHostname(host, this.network);
peer = this.pool.peers.get(addr);
if (!peer)
return callback(new RPCError('Node has not been added.'));
return Promise.reject(new RPCError('Node has not been added.'));
peers = [peer];
} else {
peers = this.pool.peers.all;
@ -842,7 +842,7 @@ RPC.prototype.getblockhash = function getblockhash(args) {
entry = yield this.chain.db.get(height);
if (!entry)
return callback(new RPCError('Not found.'));
throw new RPCError('Not found.');
return entry.rhash;
}, this);
@ -934,10 +934,10 @@ RPC.prototype._blockToJSON = function _blockToJSON(entry, block, txDetails) {
}, this);
};
RPC.prototype.getchaintips = function getchaintips(args, callback) {
RPC.prototype.getchaintips = function getchaintips(args) {
return spawn(function *() {
var i, tips, orphans, prevs, result;
var orphan, entires, entry, main, fork;
var orphan, entries, entry, main, fork;
if (args.help || args.length !== 0)
throw new RPCError('getchaintips');
@ -1211,7 +1211,6 @@ RPC.prototype.gettxout = function gettxout(args) {
RPC.prototype.gettxoutproof = function gettxoutproof(args) {
return spawn(function *() {
var self = this;
var uniq = {};
var i, txids, block, hash, last, tx, coins;
@ -1281,7 +1280,7 @@ RPC.prototype.gettxoutproof = function gettxoutproof(args) {
RPC.prototype.verifytxoutproof = function verifytxoutproof(args) {
return spawn(function *() {
var res = [];
var i, block, hash;
var i, block, hash, entry;
if (args.help || args.length !== 1)
throw new RPCError('verifytxoutproof "proof"');
@ -1323,7 +1322,7 @@ RPC.prototype.gettxoutsetinfo = function gettxoutsetinfo(args) {
});
};
RPC.prototype.verifychain = function verifychain(args, callback) {
RPC.prototype.verifychain = function verifychain(args) {
if (args.help || args.length > 2)
return Promise.reject(new RPCError('verifychain ( checklevel numblocks )'));
@ -1522,7 +1521,7 @@ RPC.prototype.getblocktemplate = function getblocktemplate(args) {
if (opt.mode != null) {
mode = opt.mode;
if (mode !== 'template' && mode !== 'proposal')
return callback(new RPCError('Invalid mode.'));
throw new RPCError('Invalid mode.');
}
lpid = opt.longpollid;
@ -1905,13 +1904,13 @@ RPC.prototype._hashps = function _hashps(lookup, height) {
pb = this.chain.tip;
if (height >= 0 && height < this.chain.tip.height)
pb = yield this.chain.db.get(height, callback);
pb = yield this.chain.db.get(height);
if (!pb)
return 0;
if (lookup <= 0)
lookup = pb.height % self.network.pow.retargetInterval + 1;
lookup = pb.height % this.network.pow.retargetInterval + 1;
if (lookup > pb.height)
lookup = pb.height;
@ -1987,6 +1986,7 @@ RPC.prototype.generate = function generate(args) {
RPC.prototype._generate = function _generate(numblocks) {
return spawn(function *() {
var hashes = [];
var i, block;
for (i = 0; i < numblocks; i++) {
block = yield this.miner.mineBlock();
@ -2261,7 +2261,7 @@ RPC.prototype._signrawtransaction = function signrawtransaction(merged, txs, arg
secret = k[i];
if (!utils.isBase58(secret))
return callback(new RPCError('Invalid parameter'));
throw new RPCError('Invalid parameter');
key = bcoin.keyring.fromSecret(secret);
keyMap[key.getPublicKey('hex')] = key;
@ -2277,7 +2277,7 @@ RPC.prototype._signrawtransaction = function signrawtransaction(merged, txs, arg
prev = prevout[i];
if (!prev)
return callback(new RPCError('Invalid parameter'));
throw new RPCError('Invalid parameter');
hash = toHash(prev.txid);
index = prev.vout;
@ -2288,7 +2288,7 @@ RPC.prototype._signrawtransaction = function signrawtransaction(merged, txs, arg
|| !utils.isNumber(index)
|| index < 0
|| !utils.isHex(script)) {
return callback(new RPCError('Invalid parameter'));
throw new RPCError('Invalid parameter');
}
script = bcoin.script.fromRaw(script, 'hex');
@ -2330,12 +2330,12 @@ RPC.prototype._signrawtransaction = function signrawtransaction(merged, txs, arg
parts = args[3].split('|');
type = constants.hashType[parts[0]];
if (type == null)
return callback(new RPCError('Invalid parameter'));
throw new RPCError('Invalid parameter');
if (parts.length > 2)
return callback(new RPCError('Invalid parameter'));
throw new RPCError('Invalid parameter');
if (parts.length === 2) {
if (parts[1] !== 'ANYONECANPAY')
return callback(new RPCError('Invalid parameter'));
throw new RPCError('Invalid parameter');
type |= constants.hashType.ANYONECANPAY;
}
}
@ -2522,7 +2522,7 @@ RPC.prototype.validateaddress = function validateaddress(args) {
json = {
isvalid: true,
address: address.toBase58(self.network),
address: address.toBase58(this.network),
scriptPubKey: address.toScript().toJSON(),
ismine: path ? true : false,
iswatchonly: false
@ -2764,7 +2764,7 @@ RPC.prototype.addmultisigaddress = function addmultisigaddress(args) {
Promise.reject(new Error('Not implemented.'));
};
RPC.prototype.addwitnessaddress = function addwitnessaddress(args, callback) {
RPC.prototype.addwitnessaddress = function addwitnessaddress(args) {
if (args.help || args.length < 1 || args.length > 1)
return Promise.reject(new RPCError('addwitnessaddress "address"'));
// Unlikely to be implemented.
@ -2950,7 +2950,7 @@ RPC.prototype.getaddressesbyaccount = function getaddressesbyaccount(args) {
for (i = 0; i < paths.length; i++) {
path = paths[i];
addrs.push(path.toAddress().toBase58(self.network));
addrs.push(path.toAddress().toBase58(this.network));
}
return addrs;
@ -3113,7 +3113,7 @@ RPC.prototype.getreceivedbyaddress = function getreceivedbyaddress(args) {
}, this);
};
RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) {
RPC.prototype._toWalletTX = function _toWalletTX(tx) {
return spawn(function *() {
var i, det, receive, member, sent, received, json, details;
@ -3192,7 +3192,7 @@ RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) {
}, this);
};
RPC.prototype.gettransaction = function gettransaction(args, callback) {
RPC.prototype.gettransaction = function gettransaction(args) {
return spawn(function *() {
var hash, tx;
@ -3337,11 +3337,7 @@ RPC.prototype.importwallet = function importwallet(args) {
if (parts.length < 4)
throw new RPCError('Malformed wallet.');
try {
secret = bcoin.keyring.fromSecret(parts[0]);
} catch (e) {
return callback(e);
}
secret = bcoin.keyring.fromSecret(parts[0]);
time = +parts[1];
label = parts[2];
@ -3591,7 +3587,7 @@ RPC.prototype._listReceived = function _listReceived(minconf, empty, account) {
RPC.prototype.listsinceblock = function listsinceblock(args) {
return spawn(function *() {
var block, conf, out, highest;
var i, height, txs, tx;
var i, height, txs, tx, json;
if (args.help) {
throw new RPCError('listsinceblock'
@ -3789,10 +3785,10 @@ RPC.prototype.listunspent = function listunspent(args) {
hash = bcoin.address.getHash(address, 'hex');
if (!hash)
return callback(new RPCError('Invalid address.'));
throw new RPCError('Invalid address.');
if (addresses[hash])
return callback(new RPCError('Duplicate address.'));
throw new RPCError('Duplicate address.');
addresses[hash] = true;
}
@ -3857,7 +3853,7 @@ RPC.prototype.lockunspent = function lockunspent(args) {
if (args.length === 1) {
if (unlock)
this.wallet.tx.unlockCoins();
return callback(null, true);
return Promise.resolve(true);
}
outputs = toArray(args[1]);
@ -4036,7 +4032,7 @@ RPC.prototype.settxfee = function settxfee(args) {
RPC.prototype.signmessage = function signmessage(args) {
return spawn(function *() {
var address, msg, sig;
var address, msg, sig, ring;
if (args.help || args.length !== 2)
throw new RPCError('signmessage "bitcoinaddress" "message"');
@ -4282,7 +4278,13 @@ function reverseEndian(data) {
function writeFile(file, data) {
return new Promise(function(resolve, reject) {
fs.writeFile(file, out, utils.P(resolve, reject));
fs.writeFile(file, data, utils.P(resolve, reject));
});
}
function readFile(file, enc) {
return new Promise(function(resolve, reject) {
fs.readFile(file, enc, utils.P(resolve, reject));
});
}

View File

@ -1338,13 +1338,13 @@ ClientSocket.prototype.scan = function scan(start) {
if (this.chain.db.options.prune)
return Promise.reject(new Error('Cannot scan in pruned mode.'));
return this.chain.db.scan(start, this.filter, function(entry, txs) {
return this.chain.db.scan(start, this.filter, function *(entry, txs) {
for (i = 0; i < txs.length; i++)
txs[i] = txs[i].toJSON();
self.emit('block tx', entry.toJSON(), txs);
return Promise.resolve(null);
yield utils.wait();
});
};

View File

@ -1741,7 +1741,6 @@ Pool.prototype.broadcast = function broadcast(msg) {
return new Promise(function(resolve, reject) {
item.addCallback(utils.P(resolve, reject));
return item;
});
};
@ -1876,7 +1875,7 @@ Pool.prototype.isIgnored = function isIgnored(addr) {
Pool.prototype.getIP = function getIP() {
return spawn(function *() {
var request, ip, res;
var request, res, ip;
if (utils.isBrowser)
throw new Error('Could not find IP.');
@ -1910,7 +1909,7 @@ Pool.prototype.getIP = function getIP() {
Pool.prototype.getIP2 = function getIP2() {
return spawn(function *() {
var request, ip;
var request, res, ip;
if (utils.isBrowser)
throw new Error('Could not find IP.');

View File

@ -545,7 +545,7 @@ WalletDB.prototype.save = function save(wallet) {
* @param {Function} callback
*/
WalletDB.prototype.auth = function auth(wid, token, callback) {
WalletDB.prototype.auth = function auth(wid, token) {
return spawn(function *() {
var wallet = yield this.get(wid);
if (!wallet)
@ -1011,8 +1011,8 @@ WalletDB.prototype.rescan = function rescan(chaindb, height) {
this.logger.info('Scanning for %d addresses.', hashes.length);
try {
yield chaindb.scan(height, hashes, function(block, txs) {
return self.addBlock(block, txs, true);
yield chaindb.scan(height, hashes, function *(block, txs) {
yield self.addBlock(block, txs, true);
});
} catch (e) {
unlock();

View File

@ -530,7 +530,7 @@ Worker.prototype.destroy = function destroy() {
Worker.prototype._execute = function _execute(method, args, timeout, callback) {
var self = this;
var job = this.uid;
var event, timer, callback;
var event, timer;
if (++this.uid === 0x100000000)
this.uid = 0;

View File

@ -248,9 +248,9 @@ describe('Chain', function() {
var total = 0;
c(walletdb.getAddressHashes(), function(err, hashes) {
assert.ifError(err);
c(chain.db.scan(null, hashes, function(block, txs) {
c(chain.db.scan(null, hashes, function *(block, txs) {
total += txs.length;
return Promise.resolve(null);
yield utils.wait();
}), function(err) {
assert.ifError(err);
assert.equal(total, 25);