From b76a382f7856921c789092df1c9660dd208681a9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 21 Oct 2016 00:33:55 -0700 Subject: [PATCH] walletdb: refactor var names. --- lib/wallet/walletdb.js | 126 +++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 56 deletions(-) diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index bc71cd40..4762ae10 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -618,17 +618,18 @@ WalletDB.prototype.renameAccount = function renameAccount(account, name) { /** * Test an api key against a wallet's api key. * @param {WalletID} wid - * @param {String} token + * @param {String|Buffer} token * @returns {Promise} */ WalletDB.prototype.auth = co(function* auth(wid, token) { var wallet = yield this.get(wid); + if (!wallet) return; if (typeof token === 'string') { - if (!utils.isHex(token)) + if (!utils.isHex256(token)) throw new Error('Authentication error.'); token = new Buffer(token, 'hex'); } @@ -816,22 +817,22 @@ WalletDB.prototype.hasAccount = co(function* hasAccount(wid, index) { */ WalletDB.prototype.getWalletsByHash = co(function* getWalletsByHash(hash) { - var wallets = this.pathMapCache.get(hash); + var wids = this.pathMapCache.get(hash); var data; - if (wallets) - return wallets; + if (wids) + return wids; data = yield this.db.get(layout.p(hash)); if (!data) return; - wallets = parseWallets(data); + wids = parseWallets(data); - this.pathMapCache.get(hash, wallets); + this.pathMapCache.get(hash, wids); - return wallets; + return wids; }); /** @@ -861,27 +862,27 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) { var wid = wallet.wid; var hash = path.hash; var batch = this.batch(wallet); - var wallets, result; + var wids, result; this.addFilter(hash); this.emit('path', path); - wallets = yield this.getWalletsByHash(hash); + wids = yield this.getWalletsByHash(hash); - if (!wallets) - wallets = []; + if (!wids) + wids = []; // Keep these motherfuckers sorted. - result = utils.binaryInsert(wallets, wid, cmp, true); + result = utils.binaryInsert(wids, wid, cmp, true); if (result === -1) return; - this.pathMapCache.set(hash, wallets); + this.pathMapCache.set(hash, wids); wallet.pathCache.set(hash, path); - batch.put(layout.p(hash), serializeWallets(wallets)); + batch.put(layout.p(hash), serializeWallets(wids)); batch.put(layout.P(wid, hash), path.toRaw()); }); @@ -1246,7 +1247,7 @@ WalletDB.prototype.getWalletsByHashes = co(function* getWalletsByHashes(tx) { */ WalletDB.prototype.getWalletsByInsert = co(function* getWalletsByInsert(tx) { - var i, j, result, hashes, input, hash, wids; + var i, j, result, hashes, input, prevout, hash, wids; if (this.options.resolution) return yield this.getWalletsByHashes(tx); @@ -1256,11 +1257,12 @@ WalletDB.prototype.getWalletsByInsert = co(function* getWalletsByInsert(tx) { for (i = 0; i < tx.inputs.length; i++) { input = tx.inputs[i]; + prevout = input.prevout; - if (!this.testFilter(input.prevout.hash)) + if (!this.testFilter(prevout.hash)) continue; - wids = yield this.getWalletsByTX(input.prevout.hash); + wids = yield this.getWalletsByTX(prevout.hash); if (!wids) continue; @@ -1343,7 +1345,7 @@ WalletDB.prototype.setTip = co(function* setTip(hash, height) { WalletDB.prototype.writeBlock = function writeBlock(block, matches) { var batch = this.db.batch(); - var i, hash, wallets; + var i, hash, wids; batch.put(layout.R, block.toTip()); @@ -1354,8 +1356,8 @@ WalletDB.prototype.writeBlock = function writeBlock(block, matches) { for (i = 0; i < block.hashes.length; i++) { hash = block.hashes[i]; - wallets = matches[i]; - batch.put(layout.e(hash), serializeWallets(wallets)); + wids = matches[i]; + batch.put(layout.e(hash), serializeWallets(wids)); } return batch.write(); @@ -1430,7 +1432,7 @@ WalletDB.prototype.addBlock = co(function* addBlock(entry, txs) { */ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) { - var i, block, matches, hash, tx, wallets; + var i, block, matches, hash, tx, wids; if (this.options.useCheckpoints) { if (entry.height <= this.network.checkpoints.lastHeight) { @@ -1452,9 +1454,9 @@ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) { // the database back into the correct state. for (i = 0; i < txs.length; i++) { tx = txs[i]; - wallets = yield this._insertTX(tx); + wids = yield this._insertTX(tx); - if (!wallets) + if (!wids) continue; hash = tx.hash('hex'); @@ -1462,7 +1464,7 @@ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) { this.addFilter(hash); block.hashes.push(hash); - matches.push(wallets); + matches.push(wids); } if (block.hashes.length > 0) { @@ -1553,19 +1555,19 @@ WalletDB.prototype.addTX = co(function* addTX(tx) { */ WalletDB.prototype._addTX = co(function* addTX(tx) { - var wallets = yield this._insertTX(tx); + var wids = yield this._insertTX(tx); var hash; - if (!wallets) + if (!wids) return; hash = tx.hash('hex'); - yield this.db.put(layout.e(hash), serializeWallets(wallets)); + yield this.db.put(layout.e(hash), serializeWallets(wids)); this.addFilter(hash); - return wallets; + return wids; }); /** @@ -1577,27 +1579,29 @@ WalletDB.prototype._addTX = co(function* addTX(tx) { WalletDB.prototype._insertTX = co(function* insertTX(tx) { var result = false; - var i, wallets, wid, wallet; + var i, wids, wid, wallet; assert(!tx.mutable, 'Cannot add mutable TX to wallet.'); - wallets = yield this.getWalletsByInsert(tx); + wids = yield this.getWalletsByInsert(tx); - if (!wallets) + if (!wids) return; this.logger.info( 'Incoming transaction for %d wallets (%s).', - wallets.length, tx.rhash); + wids.length, tx.rhash); - for (i = 0; i < wallets.length; i++) { - wid = wallets[i]; + for (i = 0; i < wids.length; i++) { + wid = wids[i]; wallet = yield this.get(wid); assert(wallet); if (yield wallet.add(tx)) { - this.logger.debug('Added transaction to wallet: %s', wallet.id); + this.logger.debug( + 'Added transaction to wallet: %s (%d).', + wallet.id, wid); result = true; } } @@ -1605,7 +1609,7 @@ WalletDB.prototype._insertTX = co(function* insertTX(tx) { if (!result) return; - return wallets; + return wids; }); /** @@ -1632,14 +1636,14 @@ WalletDB.prototype.unconfirmTX = co(function* unconfirmTX(hash) { */ WalletDB.prototype._unconfirmTX = co(function* unconfirmTX(hash) { - var wallets = yield this.getWalletsByTX(hash); + var wids = yield this.getWalletsByTX(hash); var i, wid, wallet; - if (!wallets) + if (!wids) return; - for (i = 0; i < wallets.length; i++) { - wid = wallets[i]; + for (i = 0; i < wids.length; i++) { + wid = wids[i]; wallet = yield this.get(wid); assert(wallet); yield wallet.unconfirm(hash); @@ -1669,11 +1673,11 @@ WalletDB.prototype.zap = co(function* zap(age) { */ WalletDB.prototype._zap = co(function* zap(age) { - var wallets = yield this.getPendingWallets(); + var wids = yield this.getPendingWallets(); var i, wid, wallet; - for (i = 0; i < wallets.length; i++) { - wid = wallets[i]; + for (i = 0; i < wids.length; i++) { + wid = wids[i]; wallet = yield this.get(wid); assert(wallet); yield wallet.zap(age); @@ -1799,11 +1803,16 @@ WalletBlock.fromTip = function fromTip(data) { * @returns {Buffer} */ -WalletBlock.prototype.toTip = function toTip() { - var p = new BufferWriter(); +WalletBlock.prototype.toTip = function toTip(writer) { + var p = new BufferWriter(writer); + p.writeHash(this.hash); p.writeU32(this.height); - return p.render(); + + if (!writer) + p = p.render(); + + return p; }; /** @@ -1812,8 +1821,8 @@ WalletBlock.prototype.toTip = function toTip() { * @returns {Buffer} */ -WalletBlock.prototype.toRaw = function toRaw() { - var p = new BufferWriter(); +WalletBlock.prototype.toRaw = function toRaw(writer) { + var p = new BufferWriter(writer); var i; p.writeU32(this.height); @@ -1821,7 +1830,10 @@ WalletBlock.prototype.toRaw = function toRaw() { for (i = 0; i < this.hashes.length; i++) p.writeHash(this.hashes[i]); - return p.render(); + if (!writer) + p = p.render(); + + return p; }; /** @@ -1842,18 +1854,20 @@ WalletBlock.prototype.toJSON = function toJSON() { function parseWallets(data) { var p = new BufferReader(data); - var wallets = []; + var wids = []; + while (p.left()) - wallets.push(p.readU32()); - return wallets; + wids.push(p.readU32()); + + return wids; } -function serializeWallets(wallets) { +function serializeWallets(wids) { var p = new BufferWriter(); var i, wid; - for (i = 0; i < wallets.length; i++) { - wid = wallets[i]; + for (i = 0; i < wids.length; i++) { + wid = wids[i]; p.writeU32(wid); }