global: lint.

This commit is contained in:
Christopher Jeffrey 2016-10-24 17:52:51 -07:00
parent 510742e79c
commit 3bffbed25a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 93 additions and 62 deletions

View File

@ -20,7 +20,6 @@ var Coins = require('./coins');
var ldb = require('../db/ldb'); var ldb = require('../db/ldb');
var LRU = require('../utils/lru'); var LRU = require('../utils/lru');
var Block = require('../primitives/block'); var Block = require('../primitives/block');
var MerkleBlock = require('../primitives/merkleblock');
var Coin = require('../primitives/coin'); var Coin = require('../primitives/coin');
var TX = require('../primitives/tx'); var TX = require('../primitives/tx');
var Address = require('../primitives/address'); var Address = require('../primitives/address');
@ -1108,6 +1107,7 @@ ChainDB.prototype.getTXByAddress = co(function* getTXByAddress(addresses) {
ChainDB.prototype.scan = co(function* scan(start, filter, iter) { ChainDB.prototype.scan = co(function* scan(start, filter, iter) {
var total = 0; var total = 0;
var i, j, entry, hashes, hash, tx, txs, block; var i, j, entry, hashes, hash, tx, txs, block;
var input, prevout, found, txid;
if (start == null) if (start == null)
start = this.network.genesis.hash; start = this.network.genesis.hash;
@ -1147,12 +1147,31 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) {
for (i = 0; i < block.txs.length; i++) { for (i = 0; i < block.txs.length; i++) {
tx = block.txs[i]; tx = block.txs[i];
found = false;
for (j = 0; j < tx.inputs.length; j++) {
input = tx.inputs[j];
prevout = input.prevout;
if (filter[prevout.hash]) {
txs.push(tx);
txid = tx.hash('hex');
filter[txid] = true;
found = true;
break;
}
}
if (found)
continue;
hashes = tx.getOutputHashes('hex'); hashes = tx.getOutputHashes('hex');
for (j = 0; j < hashes.length; j++) { for (j = 0; j < hashes.length; j++) {
hash = hashes[j]; hash = hashes[j];
if (filter[hash]) { if (filter[hash]) {
txs.push(tx); txs.push(tx);
txid = tx.hash('hex');
filter[txid] = true;
break; break;
} }
} }

View File

@ -1208,17 +1208,17 @@ HTTPServer.prototype._initIO = function _initIO() {
callback(); callback();
}); });
socket.on('watch address', function(args, callback) { socket.on('watch hash', function(args, callback) {
var addresses = args[0]; var hashes = args[0];
if (!Array.isArray(addresses)) if (!Array.isArray(hashes))
return callback({ error: 'Invalid parameter.' }); return callback({ error: 'Invalid parameter.' });
if (!socket.api) if (!socket.api)
return callback({ error: 'Not authorized.' }); return callback({ error: 'Not authorized.' });
try { try {
socket.addFilter(addresses); socket.addFilter(hashes);
} catch (e) { } catch (e) {
return callback({ error: e.message }); return callback({ error: e.message });
} }
@ -1226,17 +1226,17 @@ HTTPServer.prototype._initIO = function _initIO() {
callback(); callback();
}); });
socket.on('unwatch address', function(args, callback) { socket.on('unwatch hash', function(args, callback) {
var addresses = args[0]; var hashes = args[0];
if (!Array.isArray(addresses)) if (!Array.isArray(hashes))
return callback({ error: 'Invalid parameter.' }); return callback({ error: 'Invalid parameter.' });
if (!socket.api) if (!socket.api)
return callback({ error: 'Not authorized.' }); return callback({ error: 'Not authorized.' });
try { try {
socket.removeFilter(addresses); socket.removeFilter(hashes);
} catch (e) { } catch (e) {
return callback({ error: e.message }); return callback({ error: e.message });
} }
@ -1480,14 +1480,14 @@ ClientSocket.prototype._init = function _init() {
}); });
}; };
ClientSocket.prototype.addFilter = function addFilter(addresses) { ClientSocket.prototype.addFilter = function addFilter(hashes) {
var i, hash; var i, hash;
for (i = 0; i < addresses.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = Address.getHash(addresses[i], 'hex'); hash = Address.getHash(hashes[i], 'hex');
if (!hash) if (!hash)
throw new Error('Bad address.'); throw new Error('Bad hash.');
if (!this.filter[hash]) { if (!this.filter[hash]) {
this.filter[hash] = true; this.filter[hash] = true;
@ -1498,14 +1498,14 @@ ClientSocket.prototype.addFilter = function addFilter(addresses) {
} }
}; };
ClientSocket.prototype.removeFilter = function removeFilter(addresses) { ClientSocket.prototype.removeFilter = function removeFilter(hashes) {
var i, hash; var i, hash;
for (i = 0; i < addresses.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = Address.getHash(addresses[i], 'hex'); hash = Address.getHash(hashes[i], 'hex');
if (!hash) if (!hash)
throw new Error('Bad address.'); throw new Error('Bad hash.');
if (this.filter[hash]) { if (this.filter[hash]) {
delete this.filter[hash]; delete this.filter[hash];

View File

@ -276,12 +276,12 @@ FullNode.prototype._close = co(function* close() {
}); });
/** /**
* Watch address hashes (nop). * Watch address or tx hashes (nop).
* @param {Hash[]} hashes * @param {Hash[]} hashes
* @returns {Promise} * @returns {Promise}
*/ */
FullNode.prototype.watchAddress = function watchAddress(hashes) { FullNode.prototype.watchHash = function watchHash(hashes) {
return Promise.resolve(); return Promise.resolve();
}; };

View File

@ -200,7 +200,7 @@ SPVNode.prototype._close = co(function* close() {
* @returns {Promise} * @returns {Promise}
*/ */
SPVNode.prototype.watchAddress = function watchAddress(hashes) { SPVNode.prototype.watchHash = function watchHash(hashes) {
var i; var i;
this.logger.info('Adding %d addresses to filter.', hashes.length); this.logger.info('Adding %d addresses to filter.', hashes.length);

View File

@ -897,7 +897,6 @@ TXDB.prototype.addTXRecord = co(function* addTXRecord(tx) {
TXDB.prototype.removeTXRecord = co(function* removeTXRecord(tx) { TXDB.prototype.removeTXRecord = co(function* removeTXRecord(tx) {
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var map = yield this.walletdb.getTXMap(hash); var map = yield this.walletdb.getTXMap(hash);
var block;
if (!map) if (!map)
return; return;

View File

@ -1501,7 +1501,7 @@ Wallet.prototype.resend = co(function* resend() {
this.logger.info('Rebroadcasting %d transactions.', txs.length); this.logger.info('Rebroadcasting %d transactions.', txs.length);
for (i = 0; i < txs.length; i++) for (i = 0; i < txs.length; i++)
yield this.db.send(tx); yield this.db.send(txs[i]);
return txs; return txs;
}); });

View File

@ -16,8 +16,6 @@ var crypto = require('../crypto/crypto');
var assert = require('assert'); var assert = require('assert');
var constants = require('../protocol/constants'); var constants = require('../protocol/constants');
var Network = require('../protocol/network'); var Network = require('../protocol/network');
var BufferReader = require('../utils/reader');
var BufferWriter = require('../utils/writer');
var Path = require('./path'); var Path = require('./path');
var Wallet = require('./wallet'); var Wallet = require('./wallet');
var Account = require('./account'); var Account = require('./account');
@ -295,9 +293,9 @@ WalletDB.prototype._connect = co(function* connect() {
// yield this.client.connect(); // yield this.client.connect();
// yield this.client.watchChain(); // yield this.client.watchChain();
hashes = yield this.getHashes(); hashes = yield this.getFilterHashes();
yield this.client.watchAddress(hashes); yield this.client.watchHash(hashes);
if (this.options.noScan) { if (this.options.noScan) {
tip = yield this.client.getTip(); tip = yield this.client.getTip();
@ -351,9 +349,9 @@ WalletDB.prototype._rescan = co(function* rescan(height) {
assert(utils.isNumber(height), 'Must pass in a height.'); assert(utils.isNumber(height), 'Must pass in a height.');
hashes = yield this.getHashes(); hashes = yield this.getFilterHashes();
yield this.client.watchAddress(hashes); yield this.client.watchHash(hashes);
yield this.scan(height, hashes); yield this.scan(height, hashes);
}); });
@ -385,7 +383,7 @@ WalletDB.prototype.scan = co(function* scan(height, hashes) {
yield this.rollback(height); yield this.rollback(height);
this.logger.info('Scanning for %d addresses.', hashes.length); this.logger.info('Scanning for %d hashes.', hashes.length);
yield this.client.scan(this.tip.hash, hashes, function(block, txs) { yield this.client.scan(this.tip.hash, hashes, function(block, txs) {
return self._addBlock(block, txs); return self._addBlock(block, txs);
@ -393,18 +391,18 @@ WalletDB.prototype.scan = co(function* scan(height, hashes) {
}); });
/** /**
* Add addresses to chain server filter. * Add address or tx hashes to chain server filter.
* @param {Hashes[]} hashes * @param {Hashes[]} hashes
* @returns {Promise} * @returns {Promise}
*/ */
WalletDB.prototype.watchAddress = co(function* watchAddress(hashes) { WalletDB.prototype.watchHash = co(function* watchHash(hashes) {
if (!this.client) { if (!this.client) {
this.emit('watch address', hashes); this.emit('watch hash', hashes);
return; return;
} }
yield this.client.watchAddress(hashes); yield this.client.watchHash(hashes);
}); });
/** /**
@ -657,38 +655,15 @@ WalletDB.prototype.commit = co(function* commit(wallet) {
*/ */
WalletDB.prototype.loadFilter = co(function* loadFilter() { WalletDB.prototype.loadFilter = co(function* loadFilter() {
var iter, item, hash; var i, hashes, hash;
if (!this.filter) if (!this.filter)
return; return;
iter = this.db.iterator({ hashes = yield this.getFilterHashes();
gte: layout.p(constants.NULL_HASH),
lte: layout.p(constants.HIGH_HASH)
});
for (;;) { for (i = 0; i < hashes.length; i++) {
item = yield iter.next(); hash = hashes[i];
if (!item)
break;
hash = layout.pp(item.key);
this.filter.add(hash, 'hex');
}
iter = this.db.iterator({
gte: layout.e(constants.NULL_HASH),
lte: layout.e(constants.HIGH_HASH)
});
for (;;) {
item = yield iter.next();
if (!item)
break;
hash = layout.ee(item.key);
this.filter.add(hash, 'hex'); this.filter.add(hash, 'hex');
} }
}); });
@ -1159,11 +1134,11 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) {
var wid = wallet.wid; var wid = wallet.wid;
var hash = path.hash; var hash = path.hash;
var batch = this.batch(wallet); var batch = this.batch(wallet);
var map, result; var map;
this.addFilter(hash); this.addFilter(hash);
yield this.watchAddress([path.hash]); yield this.watchHash([hash]);
map = yield this.getPathMap(hash); map = yield this.getPathMap(hash);
@ -1226,6 +1201,43 @@ WalletDB.prototype.getHashes = function getHashes() {
}); });
}; };
/**
* Get all tx hashes.
* @returns {Promise}
*/
WalletDB.prototype.getTXHashes = function getTXHashes() {
return this.db.keys({
gte: layout.e(constants.NULL_HASH),
lte: layout.e(constants.HIGH_HASH),
parse: layout.ee
});
};
/**
* Get hashes required for rescan filter.
* @returns {Promise}
*/
WalletDB.prototype.getFilterHashes = co(function* getFilterHashes() {
var hashes = [];
var addr = yield this.getHashes();
var tx = yield this.getTXHashes();
var i, hash;
for (i = 0; i < addr.length; i++) {
hash = addr[i];
hashes.push(hash);
}
for (i = 0; i < tx.length; i++) {
hash = tx[i];
hashes.push(hash);
}
return hashes;
});
/** /**
* Get all address hashes. * Get all address hashes.
* @param {WalletID} wid * @param {WalletID} wid
@ -1648,6 +1660,7 @@ WalletDB.prototype.writeTXMap = function writeTXMap(wallet, hash, map) {
var batch = this.batch(wallet); var batch = this.batch(wallet);
batch.put(layout.e(hash), map.toRaw()); batch.put(layout.e(hash), map.toRaw());
this.addFilter(hash); this.addFilter(hash);
this.watchHash([hash]);
}; };
/** /**