From dbd3b5025a96ada1a51f989b27580627d94da665 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 11 Jun 2014 18:44:27 -0500 Subject: [PATCH] tx-pool: handle noWrite better. update tx.block along with tx.ts. --- lib/bcoin/tx-pool.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index a4334966..fae9ab23 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -54,16 +54,17 @@ TXPool.prototype.add = function add(tx, noWrite) { // Ignore stale pending transactions if (tx.ts === 0 && tx.ps + 2 * 24 * 3600 < +new Date() / 1000) { - this._removeTX(tx); + this._removeTX(tx, noWrite); return; } // Do not add TX two times if (this._all[hash]) { // Transaction was confirmed, update it in storage - if (this._storage && tx.ts !== 0 && this._all[hash].ts === 0) { + if (tx.ts !== 0 && this._all[hash].ts === 0) { this._all[hash].ts = tx.ts; - this._storeTX(hash, tx); + this._all[hash].block = tx.block; + this._storeTX(hash, tx, noWrite); } return false; } @@ -107,8 +108,9 @@ TXPool.prototype.add = function add(tx, noWrite) { this.emit('update', this._lastTs, tx); // Save spending TXs without adding unspents - if (this._storage && this._wallet.ownInput(tx)) - this._storeTX(hash, tx); + if (this._wallet.ownInput(tx)) { + this._storeTX(hash, tx, noWrite); + } return; } @@ -118,7 +120,7 @@ TXPool.prototype.add = function add(tx, noWrite) { // Verify that input script is correct, if not - add output to unspent // and remove orphan from storage if (!orphan.tx.verify(index)) { - this._removeTX(orphan.tx); + this._removeTX(orphan.tx, noWrite); return false; } return true; @@ -149,16 +151,15 @@ TXPool.prototype.add = function add(tx, noWrite) { if (updated) this.emit('update', this._lastTs, tx); - if (!noWrite) - this._storeTX(hash, tx); + this._storeTX(hash, tx, noWrite); this.emit('tx', tx); return true; }; -TXPool.prototype._storeTX = function _storeTX(hash, tx) { - if (!this._storage) +TXPool.prototype._storeTX = function _storeTX(hash, tx, noWrite) { + if (!this._storage || noWrite) return; var self = this; @@ -168,12 +169,13 @@ TXPool.prototype._storeTX = function _storeTX(hash, tx) { }); }; -TXPool.prototype._removeTX = function _removeTX(tx) { +TXPool.prototype._removeTX = function _removeTX(tx, noWrite) { for (var i = 0; i < tx.outputs.length; i++) delete this._unspent[tx.hash('hex') + '/' + i]; - if (!this._storage) + if (!this._storage || noWrite) return; + var self = this; this._storage.del(this._prefix + tx.hash('hex'), function(err) { if (err)