From 827f72ebc00fd8187952151ad9e96231d7b7ddff Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 12 Aug 2016 13:14:45 -0700 Subject: [PATCH] txdb: refactor. --- lib/bcoin/txdb.js | 37 ++++++++++++------------------------- lib/bcoin/wallet.js | 10 ++++++++++ lib/bcoin/walletdb.js | 22 ++++++++++++++-------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index d812873d..d5d57a01 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -419,7 +419,7 @@ TXDB.prototype._resolveOrphans = function _resolveOrphans(tx, index, callback) { // Verify that input script is correct, if not - add // output to unspent and remove orphan from storage if (!self.options.verify || orphan.verifyInput(input.index)) { - self.put('d/' + input.hash + '/' + pad32(input.index), coin.toRaw()); + self.put('d/' + input.hash + '/' + input.index, coin.toRaw()); return callback(null, true); } @@ -514,15 +514,13 @@ TXDB.prototype.add = function add(tx, info, callback) { outpoint = bcoin.outpoint.fromTX(tx, i).toRaw(); self.put('s/' + key, outpoint); - if (!input.coin) { - // Add orphan, if no parent transaction is yet known + // Add orphan, if no parent transaction is yet known + if (!input.coin) return self._addOrphan(key, outpoint, next); - } - - self.del('C/' + path.account + '/' + key); self.del('c/' + key); - self.put('d/' + hash + '/' + pad32(i), input.coin.toRaw()); + self.put('d/' + hash + '/' + i, input.coin.toRaw()); + self.del('C/' + path.account + '/' + key); self.coinCache.remove(key); @@ -539,9 +537,6 @@ TXDB.prototype.add = function add(tx, info, callback) { var key = hash + '/' + i; var coin; - if (output.script.isUnspendable()) - return next(); - path = info.getPath(address); // Do not add unspents for outputs that aren't ours. @@ -555,13 +550,10 @@ TXDB.prototype.add = function add(tx, info, callback) { if (orphans) return next(); - coin = bcoin.coin.fromTX(tx, i); - - self.put('C/' + path.account + '/' + key, DUMMY); - - coin = coin.toRaw(); + coin = bcoin.coin.fromTX(tx, i).toRaw(); self.put('c/' + key, coin); + self.put('C/' + path.account + '/' + key, DUMMY); self.coinCache.set(key, coin); @@ -925,12 +917,11 @@ TXDB.prototype._remove = function remove(tx, info, callback) { if (!path) continue; - self.put('C/' + path.account + '/' + key, DUMMY); - coin = input.coin.toRaw(); self.put('c/' + key, coin); - self.del('d/' + hash + '/' + pad32(i)); + self.put('C/' + path.account + '/' + key, DUMMY); + self.del('d/' + hash + '/' + i); self.del('s/' + key); self.del('o/' + key); @@ -942,17 +933,13 @@ TXDB.prototype._remove = function remove(tx, info, callback) { key = hash + '/' + i; address = output.getHash('hex'); - if (output.script.isUnspendable()) - continue; - path = info.getPath(address); if (!path) continue; - self.del('C/' + path.account + '/' + key); - self.del('c/' + key); + self.del('C/' + path.account + '/' + key); self.coinCache.remove(key); } @@ -1497,8 +1484,8 @@ TXDB.prototype.fillHistory = function fillHistory(tx, callback) { hash = tx.hash('hex'); this.iterate({ - gte: 'd/' + hash + '/' + pad32(0), - lte: 'd/' + hash + '/' + pad32(0xffffffff), + gte: 'd/' + hash + '/', + lte: 'd/' + hash + '/~', keys: true, values: true, parse: function(value, key) { diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 8348af63..0a560d9b 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -544,6 +544,16 @@ Wallet.prototype.getAccount = function getAccount(account, callback) { return this.db.getAccount(this.id, account, callback); }; +/** + * Test whether an account exists. + * @param {Number|String} account + * @param {Function} callback - Returns [Error, {@link Boolean}]. + */ + +Wallet.prototype.hasAccount = function hasAccount(account, callback) { + this.db.hasAccount(this.id, account, callback); +}; + /** * Create a new receiving address (increments receiveDepth). * @param {(Number|String)?} account diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 51262d27..8779ec6e 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -1382,13 +1382,11 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) { return callback(err); if (!wallets) - return callback(null, false); + return callback(); self.logger.info( - 'Incoming transaction for %d wallets.', - wallets.length); - - self.logger.debug(wallets); + 'Incoming transaction for %d wallets (%s).', + wallets.length, tx.rhash); utils.forEachSerial(wallets, function(info, next) { self.get(info.id, function(err, wallet) { @@ -1398,6 +1396,8 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) { if (!wallet) return next(); + self.logger.debug('Adding tx to wallet: %s', info.id); + wallet.tx.add(tx, info, function(err) { if (err) return next(err); @@ -1688,14 +1688,20 @@ WalletDB.prototype.getInfo = function getInfo(id, callback) { WalletDB.prototype.ensureAccount = function ensureAccount(id, options, callback) { var self = this; - var account = options.name || options.account; + var account = options.account; + + if (typeof options.name === 'string') + account = options.name; + this.fetchWallet(id, callback, function(wallet, callback) { - self.hasAccount(wallet.id, account, function(err, exists) { + wallet.hasAccount(account, function(err, exists) { if (err) return callback(err); + if (exists) return wallet.getAccount(account, callback); - return wallet.createAccount(options, callback); + + wallet.createAccount(options, callback); }); }); };