txdb: refactor.

This commit is contained in:
Christopher Jeffrey 2016-08-12 13:14:45 -07:00
parent 5b1a7a89ac
commit 827f72ebc0
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 36 additions and 33 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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);
});
});
};