txdb: optimize.

This commit is contained in:
Christopher Jeffrey 2016-08-11 16:02:27 -07:00
parent 9c94fae2a8
commit 817a6c3031
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -215,7 +215,7 @@ TXDB.prototype.mapAddresses = function mapAddresses(address, callback) {
* @param {Function} callback - Returns [Error, Buffer].
*/
TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) {
TXDB.prototype._addOrphan = function _addOrphan(key, outpoint, callback) {
var self = this;
var p;
@ -228,7 +228,7 @@ TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) {
if (buf)
p.writeBytes(buf);
bcoin.outpoint(hash, index).toRaw(p);
p.writeBytes(outpoint);
self.batch().put('o/' + key, p.render());
@ -631,8 +631,7 @@ TXDB.prototype._add = function add(tx, info, callback) {
hash = tx.hash('hex');
self.start();
batch = self.batch();
batch = self.start();
batch.put('t/' + hash, tx.toExtended());
if (tx.ts === 0)
@ -655,7 +654,7 @@ TXDB.prototype._add = function add(tx, info, callback) {
// Consume unspent money or add orphans
utils.forEachSerial(tx.inputs, function(input, next, i) {
var prevout = input.prevout;
var key, address;
var key, address, outpoint;
if (tx.isCoinbase())
return next();
@ -670,11 +669,12 @@ TXDB.prototype._add = function add(tx, info, callback) {
key = prevout.hash + '/' + prevout.index;
// s/[outpoint-key] -> [spender-hash]|[spender-input-index]
batch.put('s/' + key, bcoin.outpoint.fromTX(tx, i).toRaw());
outpoint = bcoin.outpoint.fromTX(tx, i).toRaw();
batch.put('s/' + key, outpoint);
if (!input.coin) {
// Add orphan, if no parent transaction is yet known
return self._addOrphan(key, hash, i, next);
return self._addOrphan(key, outpoint, next);
}
updated = true;
@ -943,8 +943,7 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback) {
// and remove pending flag to mark as confirmed.
assert(tx.height >= 0);
self.start();
batch = self.batch();
batch = self.start();
batch.put('t/' + hash, tx.toExtended());
batch.del('p/' + hash);
@ -1129,7 +1128,7 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
coin = input.coin.toRaw();
batch.put('c/' + key, coin);
batch.del('d/' + tx.hash('hex') + '/' + pad32(i));
batch.del('d/' + hash + '/' + pad32(i));
batch.del('s/' + key);
batch.del('o/' + key);
@ -1232,8 +1231,6 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
hash = tx.hash('hex');
height = tx.height;
batch = this.db.batch();
if (height !== -1)
return callback(null, false, info);