tx-pool: handle noWrite better. update tx.block along with tx.ts.

This commit is contained in:
Christopher Jeffrey 2014-06-11 18:44:27 -05:00
parent 96e1cbb0e1
commit dbd3b5025a

View File

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