From 3bffbed25aa0d248ddbceecaa01b12b2cb695c4d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 24 Oct 2016 17:52:51 -0700 Subject: [PATCH] global: lint. --- lib/chain/chaindb.js | 21 +++++++++- lib/http/server.js | 32 +++++++-------- lib/node/fullnode.js | 4 +- lib/node/spvnode.js | 2 +- lib/wallet/txdb.js | 1 - lib/wallet/wallet.js | 2 +- lib/wallet/walletdb.js | 93 ++++++++++++++++++++++++------------------ 7 files changed, 93 insertions(+), 62 deletions(-) diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index 3a9e08a1..13b221dc 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -20,7 +20,6 @@ var Coins = require('./coins'); var ldb = require('../db/ldb'); var LRU = require('../utils/lru'); var Block = require('../primitives/block'); -var MerkleBlock = require('../primitives/merkleblock'); var Coin = require('../primitives/coin'); var TX = require('../primitives/tx'); 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) { var total = 0; var i, j, entry, hashes, hash, tx, txs, block; + var input, prevout, found, txid; if (start == null) 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++) { 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'); for (j = 0; j < hashes.length; j++) { hash = hashes[j]; if (filter[hash]) { txs.push(tx); + txid = tx.hash('hex'); + filter[txid] = true; break; } } diff --git a/lib/http/server.js b/lib/http/server.js index 091a5532..7f280f8b 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -1208,17 +1208,17 @@ HTTPServer.prototype._initIO = function _initIO() { callback(); }); - socket.on('watch address', function(args, callback) { - var addresses = args[0]; + socket.on('watch hash', function(args, callback) { + var hashes = args[0]; - if (!Array.isArray(addresses)) + if (!Array.isArray(hashes)) return callback({ error: 'Invalid parameter.' }); if (!socket.api) return callback({ error: 'Not authorized.' }); try { - socket.addFilter(addresses); + socket.addFilter(hashes); } catch (e) { return callback({ error: e.message }); } @@ -1226,17 +1226,17 @@ HTTPServer.prototype._initIO = function _initIO() { callback(); }); - socket.on('unwatch address', function(args, callback) { - var addresses = args[0]; + socket.on('unwatch hash', function(args, callback) { + var hashes = args[0]; - if (!Array.isArray(addresses)) + if (!Array.isArray(hashes)) return callback({ error: 'Invalid parameter.' }); if (!socket.api) return callback({ error: 'Not authorized.' }); try { - socket.removeFilter(addresses); + socket.removeFilter(hashes); } catch (e) { 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; - for (i = 0; i < addresses.length; i++) { - hash = Address.getHash(addresses[i], 'hex'); + for (i = 0; i < hashes.length; i++) { + hash = Address.getHash(hashes[i], 'hex'); if (!hash) - throw new Error('Bad address.'); + throw new Error('Bad hash.'); if (!this.filter[hash]) { 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; - for (i = 0; i < addresses.length; i++) { - hash = Address.getHash(addresses[i], 'hex'); + for (i = 0; i < hashes.length; i++) { + hash = Address.getHash(hashes[i], 'hex'); if (!hash) - throw new Error('Bad address.'); + throw new Error('Bad hash.'); if (this.filter[hash]) { delete this.filter[hash]; diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index ab9dbbb3..0fb5b946 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -276,12 +276,12 @@ FullNode.prototype._close = co(function* close() { }); /** - * Watch address hashes (nop). + * Watch address or tx hashes (nop). * @param {Hash[]} hashes * @returns {Promise} */ -FullNode.prototype.watchAddress = function watchAddress(hashes) { +FullNode.prototype.watchHash = function watchHash(hashes) { return Promise.resolve(); }; diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 4acc5713..1e2d19a9 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -200,7 +200,7 @@ SPVNode.prototype._close = co(function* close() { * @returns {Promise} */ -SPVNode.prototype.watchAddress = function watchAddress(hashes) { +SPVNode.prototype.watchHash = function watchHash(hashes) { var i; this.logger.info('Adding %d addresses to filter.', hashes.length); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 9b5f79be..b92ac301 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -897,7 +897,6 @@ TXDB.prototype.addTXRecord = co(function* addTXRecord(tx) { TXDB.prototype.removeTXRecord = co(function* removeTXRecord(tx) { var hash = tx.hash('hex'); var map = yield this.walletdb.getTXMap(hash); - var block; if (!map) return; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 56b732cc..6c618722 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1501,7 +1501,7 @@ Wallet.prototype.resend = co(function* resend() { this.logger.info('Rebroadcasting %d transactions.', txs.length); for (i = 0; i < txs.length; i++) - yield this.db.send(tx); + yield this.db.send(txs[i]); return txs; }); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 678817c4..818e1813 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -16,8 +16,6 @@ var crypto = require('../crypto/crypto'); var assert = require('assert'); var constants = require('../protocol/constants'); var Network = require('../protocol/network'); -var BufferReader = require('../utils/reader'); -var BufferWriter = require('../utils/writer'); var Path = require('./path'); var Wallet = require('./wallet'); var Account = require('./account'); @@ -295,9 +293,9 @@ WalletDB.prototype._connect = co(function* connect() { // yield this.client.connect(); // 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) { 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.'); - hashes = yield this.getHashes(); + hashes = yield this.getFilterHashes(); - yield this.client.watchAddress(hashes); + yield this.client.watchHash(hashes); yield this.scan(height, hashes); }); @@ -385,7 +383,7 @@ WalletDB.prototype.scan = co(function* scan(height, hashes) { 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) { 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 * @returns {Promise} */ -WalletDB.prototype.watchAddress = co(function* watchAddress(hashes) { +WalletDB.prototype.watchHash = co(function* watchHash(hashes) { if (!this.client) { - this.emit('watch address', hashes); + this.emit('watch hash', hashes); 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() { - var iter, item, hash; + var i, hashes, hash; if (!this.filter) return; - iter = this.db.iterator({ - gte: layout.p(constants.NULL_HASH), - lte: layout.p(constants.HIGH_HASH) - }); + hashes = yield this.getFilterHashes(); - for (;;) { - item = yield iter.next(); - - 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); + for (i = 0; i < hashes.length; i++) { + hash = hashes[i]; this.filter.add(hash, 'hex'); } }); @@ -1159,11 +1134,11 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) { var wid = wallet.wid; var hash = path.hash; var batch = this.batch(wallet); - var map, result; + var map; this.addFilter(hash); - yield this.watchAddress([path.hash]); + yield this.watchHash([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. * @param {WalletID} wid @@ -1648,6 +1660,7 @@ WalletDB.prototype.writeTXMap = function writeTXMap(wallet, hash, map) { var batch = this.batch(wallet); batch.put(layout.e(hash), map.toRaw()); this.addFilter(hash); + this.watchHash([hash]); }; /**