txdb: refactor.
This commit is contained in:
parent
5b1a7a89ac
commit
827f72ebc0
@ -419,7 +419,7 @@ TXDB.prototype._resolveOrphans = function _resolveOrphans(tx, index, callback) {
|
|||||||
// Verify that input script is correct, if not - add
|
// Verify that input script is correct, if not - add
|
||||||
// output to unspent and remove orphan from storage
|
// output to unspent and remove orphan from storage
|
||||||
if (!self.options.verify || orphan.verifyInput(input.index)) {
|
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);
|
return callback(null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,15 +514,13 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
|||||||
outpoint = bcoin.outpoint.fromTX(tx, i).toRaw();
|
outpoint = bcoin.outpoint.fromTX(tx, i).toRaw();
|
||||||
self.put('s/' + key, outpoint);
|
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);
|
return self._addOrphan(key, outpoint, next);
|
||||||
}
|
|
||||||
|
|
||||||
self.del('C/' + path.account + '/' + key);
|
|
||||||
|
|
||||||
self.del('c/' + 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);
|
self.coinCache.remove(key);
|
||||||
|
|
||||||
@ -539,9 +537,6 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
|||||||
var key = hash + '/' + i;
|
var key = hash + '/' + i;
|
||||||
var coin;
|
var coin;
|
||||||
|
|
||||||
if (output.script.isUnspendable())
|
|
||||||
return next();
|
|
||||||
|
|
||||||
path = info.getPath(address);
|
path = info.getPath(address);
|
||||||
|
|
||||||
// Do not add unspents for outputs that aren't ours.
|
// Do not add unspents for outputs that aren't ours.
|
||||||
@ -555,13 +550,10 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
|||||||
if (orphans)
|
if (orphans)
|
||||||
return next();
|
return next();
|
||||||
|
|
||||||
coin = bcoin.coin.fromTX(tx, i);
|
coin = bcoin.coin.fromTX(tx, i).toRaw();
|
||||||
|
|
||||||
self.put('C/' + path.account + '/' + key, DUMMY);
|
|
||||||
|
|
||||||
coin = coin.toRaw();
|
|
||||||
|
|
||||||
self.put('c/' + key, coin);
|
self.put('c/' + key, coin);
|
||||||
|
self.put('C/' + path.account + '/' + key, DUMMY);
|
||||||
|
|
||||||
self.coinCache.set(key, coin);
|
self.coinCache.set(key, coin);
|
||||||
|
|
||||||
@ -925,12 +917,11 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
|||||||
if (!path)
|
if (!path)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
self.put('C/' + path.account + '/' + key, DUMMY);
|
|
||||||
|
|
||||||
coin = input.coin.toRaw();
|
coin = input.coin.toRaw();
|
||||||
|
|
||||||
self.put('c/' + key, coin);
|
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('s/' + key);
|
||||||
self.del('o/' + key);
|
self.del('o/' + key);
|
||||||
|
|
||||||
@ -942,17 +933,13 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
|||||||
key = hash + '/' + i;
|
key = hash + '/' + i;
|
||||||
address = output.getHash('hex');
|
address = output.getHash('hex');
|
||||||
|
|
||||||
if (output.script.isUnspendable())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
path = info.getPath(address);
|
path = info.getPath(address);
|
||||||
|
|
||||||
if (!path)
|
if (!path)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
self.del('C/' + path.account + '/' + key);
|
|
||||||
|
|
||||||
self.del('c/' + key);
|
self.del('c/' + key);
|
||||||
|
self.del('C/' + path.account + '/' + key);
|
||||||
|
|
||||||
self.coinCache.remove(key);
|
self.coinCache.remove(key);
|
||||||
}
|
}
|
||||||
@ -1497,8 +1484,8 @@ TXDB.prototype.fillHistory = function fillHistory(tx, callback) {
|
|||||||
hash = tx.hash('hex');
|
hash = tx.hash('hex');
|
||||||
|
|
||||||
this.iterate({
|
this.iterate({
|
||||||
gte: 'd/' + hash + '/' + pad32(0),
|
gte: 'd/' + hash + '/',
|
||||||
lte: 'd/' + hash + '/' + pad32(0xffffffff),
|
lte: 'd/' + hash + '/~',
|
||||||
keys: true,
|
keys: true,
|
||||||
values: true,
|
values: true,
|
||||||
parse: function(value, key) {
|
parse: function(value, key) {
|
||||||
|
|||||||
@ -544,6 +544,16 @@ Wallet.prototype.getAccount = function getAccount(account, callback) {
|
|||||||
return this.db.getAccount(this.id, 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).
|
* Create a new receiving address (increments receiveDepth).
|
||||||
* @param {(Number|String)?} account
|
* @param {(Number|String)?} account
|
||||||
|
|||||||
@ -1382,13 +1382,11 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (!wallets)
|
if (!wallets)
|
||||||
return callback(null, false);
|
return callback();
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'Incoming transaction for %d wallets.',
|
'Incoming transaction for %d wallets (%s).',
|
||||||
wallets.length);
|
wallets.length, tx.rhash);
|
||||||
|
|
||||||
self.logger.debug(wallets);
|
|
||||||
|
|
||||||
utils.forEachSerial(wallets, function(info, next) {
|
utils.forEachSerial(wallets, function(info, next) {
|
||||||
self.get(info.id, function(err, wallet) {
|
self.get(info.id, function(err, wallet) {
|
||||||
@ -1398,6 +1396,8 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) {
|
|||||||
if (!wallet)
|
if (!wallet)
|
||||||
return next();
|
return next();
|
||||||
|
|
||||||
|
self.logger.debug('Adding tx to wallet: %s', info.id);
|
||||||
|
|
||||||
wallet.tx.add(tx, info, function(err) {
|
wallet.tx.add(tx, info, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -1688,14 +1688,20 @@ WalletDB.prototype.getInfo = function getInfo(id, callback) {
|
|||||||
|
|
||||||
WalletDB.prototype.ensureAccount = function ensureAccount(id, options, callback) {
|
WalletDB.prototype.ensureAccount = function ensureAccount(id, options, callback) {
|
||||||
var self = this;
|
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) {
|
this.fetchWallet(id, callback, function(wallet, callback) {
|
||||||
self.hasAccount(wallet.id, account, function(err, exists) {
|
wallet.hasAccount(account, function(err, exists) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (exists)
|
if (exists)
|
||||||
return wallet.getAccount(account, callback);
|
return wallet.getAccount(account, callback);
|
||||||
return wallet.createAccount(options, callback);
|
|
||||||
|
wallet.createAccount(options, callback);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user